extending GenomicRanges::reduce warning, why ?
1
0
Entering edit mode
@hauken_heyken-13992
Last seen 2.1 years ago
Bergen

Ok, so I want my own definition of reduce, that calls GenomicRanges::reduce() if keep.names = F, the extra argument I want.

I can not find the original setGeneric for reduce, I guess it's auto-created ?

my generic is:

setGeneric("reduce", function(x, ...) standardGeneric("reduce")) # i extend with ...

It works, and all is good, but when doing check on package I get warning:

Warning: multiple methods tables found for ‘reduce’

How to fix this ?

 

My functions are:

#' Generic for reduce in ORFik
#'
#' @param x a GRangesList
#' @param ... see \code{\link[GenomicRanges]{reduce}}
#' @importFrom methods setGeneric
#' @export
setGeneric("reduce", function(x, ...) standardGeneric("reduce"))

#' Reduce GRanges / GRangesList
#'
#' Extends function \code{\link[GenomicRanges]{reduce}}
#' by trying to keep names and meta columns if it is a
#' GRangesList. If keep.names == F, it's just the normal
#' GenomicRanges::reduce.
#'
#' Only tested for orfik, might not work for other naming conventions.
#' @param x a \code{\link[GenomicRanges]{GRangesList}} or GRanges object
#' @param drop.empty.ranges (FALSE) if a group is empty (width 0), delete it.
#' @param min.gapwidth (1L) how long gap can it be to say they belong together
#' @param with.revmap (FALSE) return info on which mapped to which
#' @param with.inframe.attrib (FALSE) For internal use.
#' @param ignore.strand (FALSE), can different strands be reduced together.
#' @param keep.names (FALSE) keep the names and meta columns of the GRangesList
#' @importFrom methods setMethod
#' @export
#' @return A reduced GRangesList
setMethod("reduce", signature = signature(x = "GRangesList"),
                    function(x, drop.empty.ranges = FALSE,
                             min.gapwidth = 1L, with.revmap = FALSE,
                             with.inframe.attrib = FALSE,
                             ignore.strand = FALSE , keep.names = FALSE){  #### <- here keep.names is defined, my variable.

  if (keep.names) { # return with names

    gr <- unlist(GenomicRanges::reduce(x), use.names = TRUE)
    if (length(gr) == 0) return(GRangesList())

    return(matchNaming(gr, x))
  } else { # return original
    return(GenomicRanges::reduce(x, drop.empty.ranges, min.gapwidth,
           with.revmap, with.inframe.attrib,
           ignore.strand))
  }
})
GenomicRanges reduce setMethod setGeneric • 1.4k views
ADD COMMENT
1
Entering edit mode
@martin-morgan-1513
Last seen 4 months ago
United States

Probably a better question for the bioc-devel mailing list.

I have

getGeneric("reduce")
standardGeneric for "reduce" defined from package "IRanges"

function (x, drop.empty.ranges = FALSE, ...) 
standardGeneric("reduce")
<bytecode: 0xb07d538>
<environment: 0xb04f978>
Methods may be defined for arguments: x
Use  showMethods("reduce")  for currently available ones.

which indicates that the generic is defined in the IRanges package with a particular signature. If one had a new class, one could define the method by first using importFrom(IRanges, reduce) and then setMethod(reduce, "MyClass", function(x, drop.empty.ranges = FALSE, ...) {}).

It is not a good idea to redefine a method. Think of the generic as offering a contract, and a method as implementing the contract. Either the method implements the contract and shouldn't be changed, or the method doesn't implement the contract, and should be fixed. So either live with the contract, or report a bug or feature request to the author of the method.

If you'd like a different contract, create a different generic.

ADD COMMENT
0
Entering edit mode

Thanks for the help Martin, slowly learning the way of R.

 

I renamed the function to reduce_keep_attr

It tries to keep the meta columns and naming if it makes sense after reduction.

ADD REPLY

Login before adding your answer.

Traffic: 666 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6