coercion to Granges from empty data.frame ==> unexpected behavior ??
0
0
Entering edit mode
Malcolm Cook ★ 1.6k
@malcolm-cook-6293
Last seen 3 months ago
United States

Hello,

I find it odd that coersion to GRanges with as fails in the case of an empty data.frame:

This works fine:

> as(data.frame(SeqID=c('s'),start=c(1),end=c(2)),'GRanges')
GRanges object with 1 range and 0 metadata columns:
      seqnames    ranges strand
         <Rle> <IRanges>  <Rle>
  [1]        s    [1, 2]      *
  -------
  seqinfo: 1 sequence from an unspecified genome; no seqlengths

But if the data.frame is empty and untyped, no dice:

> as(data.frame(SeqID=c(),start=c(),end=c()),'GRanges')
Error in .find_start_end_cols(df_colnames0, start.field0, end.field0) : 
  cannnot determine start/end columns

Taking the diagnostic literally, I type the columns in the data.frame and get a little further

as(data.frame(SeqID=character(),start=integer(),end=integer()),'GRanges')
Error in validObject(.Object) : 
  invalid class "GRanges" object: NROW(strand(x)) != length(x)

Almost across the line. Let's add the strand in too:

> as(data.frame(SeqID=character(),start=integer(),end=integer(),strand=character()),'GRanges')
GRanges object with 0 ranges and 0 metadata columns:
   seqnames    ranges strand
      <Rle> <IRanges>  <Rle>
  -------
  seqinfo: no sequences

That works.

But, is this truely by design?

At the very least, I would hope that the 3rd example would have worked, with the strand column optional...

Or is there a "good reason" it works just as it does?

Thanks!

granges coercion • 1.1k views
ADD COMMENT

Login before adding your answer.

Traffic: 783 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