Skip to content

[Bioc-devel] GenomicRanges: Concatenation of GRanges with matrices in mcols

4 messages · Julian Gehring, Hervé Pagès, Michael Lawrence

#
Hi,

If I want to bind two GRanges object with a matrix in the meta columns, 
the concatenation of the two fails in bioc-stable (GenomicRanges 1.16.3) 
and bioc-devel (GenomicRanges 1.17.13) with:

'''
Error in validObject(.Object) :
   invalid class ?GRanges? object: number of rows in DataTable 
'mcols(x)' must match length of 'x'
'''

If multiple columns are used, the class of of the first column seem to 
determine the behavior:

#+BEGIN_SRC R
   library(GenomicRanges)

   ## sample data, two identical GRanges
   gr1 = gr2 = GRanges(1, IRanges(1:2, width = 1))
   m = matrix(1:4, 2)

   ## the vector alone works
   mcols(gr1) = mcols(gr2) = DataFrame(x = 1)
   c(gr1, gr2) ## works

   ## vector first, matrix second works
   mcols(gr1) = mcols(gr2) = DataFrame(x = 1, m = I(m))
   c(gr1, gr2) ## works

   ## the matrix alone fails
   mcols(gr1) = mcols(gr2) = DataFrame(m = I(m))
   c(gr1, gr2) ## fails

   ## matrix first, vector second fails
   mcols(gr1) = mcols(gr2) = DataFrame(m = I(m), x = 1)
   c(gr1, gr2) ## fails
#+END_SRC

Best wishes
Julian
#
Hi Julian,

At the root of the problem is what rbind() does on DataFrames containing
matrices:

   m <- matrix(1:4, nrow=2)
   df <- DataFrame(m=I(m))
   df2 <- rbind(df, df)

Then:

   > df2
   DataFrame with 8 rows and 1 column
            m
     <matrix>
   1      1 3
   2      2 4
   3      1 3
   4      2 4

   > nrow(df2)
   [1] 8

Too many rows!

   > str(df2)
   Formal class 'DataFrame' [package "IRanges"] with 6 slots
     ..@ rownames       : NULL
     ..@ nrows          : int 12
     ..@ listData       :List of 1
     .. ..$ m: int [1:6, 1:2] 1 2 3 1 2 3 4 5 6 4 ...
     ..@ elementType    : chr "ANY"
     ..@ elementMetadata: NULL
     ..@ metadata       : list()

   > validObject(df2)
   [1] TRUE

I'll leave this to Michael.

Thanks,
H.
On 05/20/2014 01:22 AM, Julian Gehring wrote:

  
    
#
On 05/20/2014 12:49 PM, Herv? Pag?s wrote:
Sorry, I mixed up outputs from different sessions. Correct str() output:

   > str(df2)
   Formal class 'DataFrame' [package "IRanges"] with 6 slots
     ..@ rownames       : NULL
     ..@ nrows          : int 8
     ..@ listData       :List of 1
     .. ..$ m: int [1:4, 1:2] 1 2 1 2 3 4 3 4
     ..@ elementType    : chr "ANY"
     ..@ elementMetadata: NULL
     ..@ metadata       : list()

H.