Search code examples
rbioinformaticsbioconductorgenomicrangesiranges

Can convert Granges to data.frame


I have follow data:

# you can input data by run following script in Rstudio R_gui or R 
# this is a dput output
# so you can run it for import data

library(GenomicRanges)

gr_test <- new("GRanges", seqnames = new("Rle", values = structure(1L,
                                                                   levels = c("2",  "6"), class = "factor"), lengths = 1L,
                                         elementMetadata = NULL, 
                                         metadata = list()), ranges = new("IRanges", start = 46550131L, 
                                                                          width = 26786L, NAMES = NULL, elementType = "ANY", elementMetadata = NULL, 
                                                                          metadata = list()), strand = new("Rle", values = structure(3L, levels = c("+",  "-", "*"), class = "factor"), lengths = 1L,
                                                                                                           elementMetadata = NULL, 
                                                                                                           metadata = list()), seqinfo = new("Seqinfo", seqnames = c("2",  "6"), seqlengths = c(243199373L, 171115067L), is_circular = c(FALSE, 
                                                                                                                                                                                                                                         FALSE), genome = c("GRCh37.p13", "GRCh37.p13")), elementMetadata =
                 new("DFrame", 
                     rownames = NULL, nrows = 1L, elementType = "ANY", elementMetadata = NULL, 
                     metadata = list(), listData = list(hap_id = "2_46500819_46600894", 
                                                        chr = 2L, hap_start = 46500819L, hap_end = 46600894L, 
                                                        hap_num_id = 1113L, alelles_start = 46550130, alelles_end = 46576916, 
                                                        alelles_count = 12L, HAN_freq = 0.0512821, TIB_freq = 0.7763155, 
                                                        HAN_count = 36L, TIB_count = 57L, min_TIB_count = 36L, 
                                                        min_HAN_count = 57L, HAN_freq_mean = 0.0630342083333333, 
                                                        TIB_freq_mean = 0.75438575, diff_median = 0.6724019, 
                                                        diff_mean = 0.691351541666667, gene_symbol = "EPAS1", 
                                                        name = "NM_001430", score = 0, itemRgb = NA_character_, 
                                                        thick = new("IRanges", start = 46525051L, width = 86749L, 
                                                                    NAMES = NULL, elementType = "integer", elementMetadata = NULL, 
                                                                    metadata = list()), blocks = new("CompressedIRangesList", 
                                                                                                     unlistData = new("IRanges", start = c(1L, 49472L, 
                                                                                                                                           58750L, 59323L, 63237L, 63484L, 72426L, 78289L, 79138L, 
                                                                                                                                           80493L, 81256L, 82826L, 84195L, 84574L, 85024L, 87108L
                                                                                                     ), width = c(536L, 191L, 152L, 85L, 119L, 206L, 107L, 
                                                                                                                  148L, 215L, 194L, 111L, 491L, 127L, 115L, 174L, 2195L
                                                                                                     ), NAMES = NULL, elementType = "integer", elementMetadata = NULL, 
                                                                                                     metadata = list()), elementType = "IRanges", 
                                                                                                     elementMetadata = NULL, metadata = list(), partitioning = new("PartitioningByEnd", 
                                                                                                                                                                   end = 16L, NAMES = NULL, elementType = "ANY", 
                                                                                                                                                                   elementMetadata = NULL, metadata = list())), 
                                                        SYMBOL = "EPAS1")), elementType = "ANY", metadata = list())

I try to convert it to data.frame, but I see some error I dont know why

 df <- as.data.frame(gr_test)
 df
   Error in h(simpleError(msg, call)) : 
    在为'unname'函数选择方法时评估'obj'参数出了错: IRanges objects don't support [[, as.list(), lapply(), or unlist() at the moment

Solution

  • The problem is that mcols(gr_test)$blocks is an IRangesList and there is no way to convert that meaningfully to a column of a data.frame since your GRanges object has one row while the IRanges object in that list has many. You can remove it to make the conversion possible mcols(gr_test)$blocks<-NULL.