Question: GenomicRanges error constructing Granges
0
gravatar for rhooads7
8 months ago by
rhooads70
rhooads70 wrote:

I have tried several ways to construct a genomic ranges object from an existing data.frame. Can someone help me resolve the error I keep getting:

" Error in (function (classes, fdef, mtable)  : 
  unable to find an inherited method for function ‘strand’ for signature ‘"character"’ "

Code and session info below:

> require(dplyr)
> require(tidyr)
> peakinfo  = peak.data %>% 
+   tibble::rownames_to_column() %>% 
+   separate(rowname, c("chr", "start", "end"), "-")
> peakinfo$strand = rep( c("*"), length(rownames(peakinfo)) )
> peakinfo = select(peakinfo, chr, start, end, strand, sums)
> peakinfo$start = as.integer(peakinfo$start)
> peakinfo$end = as.integer(peakinfo$end)
> head(peakinfo)
   chr     start       end strand sums
1 chr1  10007157  10007666      *    2
2 chr1  10007166  10007661      *  139
3 chr1  10008686  10009664      *  841
4 chr1  10014364  10014900      *  317
5 chr1 100163764 100164204      *   48
6 chr1 100231827 100232398      *  218

> class(peakinfo)
[1] "data.frame"

> str(peakinfo)
'data.frame':   139091 obs. of  5 variables:

 $ chr   : chr  "chr1" "chr1" "chr1" "chr1" ...
 $ start : int  10007157 10007166 10008686 10014364 100163764 100231827 100369362 100369395 10037254 10037336 ...
 $ end   : int  10007666 10007661 10009664 10014900 100164204 100232398 100370466 100370364 10040606 10038649 ...
 $ strand: chr  "*" "*" "*" "*" ...
 $ sums  : num  2 139 841 317 48 ...

# Remove all packages
> pkg = names(sessionInfo()$otherPkgs)
> pkgs = paste('package:', pkg, sep = "")
> lapply(pkgs, detach, character.only = TRUE, unload = TRUE)

require(GenomicRanges)

# make Granges from "peakinfo" dataframe

> gr = makeGRangesFromDataFrame(peakinfo,
+                               keep.extra.columns=TRUE,
+                               ignore.strand=TRUE,
+                               seqnames.field="chr",
+                               start.field="start",
+                               end.field="end")
Error in (function (classes, fdef, mtable)  : 
  unable to find an inherited method for function ‘strand’ for signature ‘"character"’

> gr = makeGRangesFromDataFrame(peakinfo,
+                               keep.extra.columns=TRUE,
+                               seqnames.field="chr",
+                               start.field="start",
+                               end.field="end",
+                               strand.field="strand")
Error in (function (classes, fdef, mtable)  : 
  unable to find an inherited method for function ‘strand’ for signature ‘"character"’

> ir <- IRanges(start = peakinfo$start, end = peakinfo$end)
> ir
IRanges object with 139091 ranges and 0 metadata columns:
               start       end     width
           <integer> <integer> <integer>
       [1]  10007157  10007666       510
       [2]  10007166  10007661       496
       [3]  10008686  10009664       979
       [4]  10014364  10014900       537
       [5] 100163764 100164204       441
       ...       ...       ...       ...
  [139087]  90802835  90803891      1057
  [139088]  90802903  90803891       989
  [139089]  90804503  90805443       941
  [139090]  90806943  90809113      2171
  [139091]  90810546  90813165      2620

> gr <- GRanges(
+   seqnames = Rle(peakinfo$chr),
+   ranges = ir,
+   strand = Rle( peakinfo$strand ),
+   sums = peakinfo$sums
+   )
Error in (function (classes, fdef, mtable)  : 
  unable to find an inherited method for function ‘strand’ for signature ‘"character"’

> gr <- GRanges(
+   seqnames = Rle(peakinfo$chr),
+   ranges = ir
+ )
Error in (function (classes, fdef, mtable)  : 
  unable to find an inherited method for function ‘strand’ for signature ‘"character"’

> sessionInfo()
R version 3.5.2 (2018-12-20)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: CentOS Linux 7 (Core)

Matrix products: default
BLAS/LAPACK: /usr/local/intel/compilers_and_libraries_2018.1.163/linux/mkl/lib/intel64_lin/libmkl_rt.so

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8    LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C             LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] parallel  stats4    stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] GenomicRanges_1.34.0 GenomeInfoDb_1.18.2  IRanges_2.16.0       S4Vectors_0.20.1     BiocGenerics_0.28.0 

loaded via a namespace (and not attached):
 [1] Rtsne_0.15             colorspace_1.4-0       grr_0.9.5              ggridges_0.5.1        
 [5] XVector_0.22.0         rstudioapi_0.9.0       listenv_0.7.0          npsurv_0.4-0          
 [9] ggrepel_0.8.0          codetools_0.2-16       splines_3.5.2          R.methodsS3_1.7.1     
[13] lsei_1.2-0             jsonlite_1.6           ica_1.0-2              cluster_2.0.7-1       
[17] png_0.1-7              R.oo_1.22.0            compiler_3.5.2         httr_1.4.0            
[21] assertthat_0.2.0       Matrix_1.2-15          lazyeval_0.2.1         htmltools_0.3.6       
[25] tools_3.5.2            bindrcpp_0.2.2         rsvd_1.0.0             igraph_1.2.4          
[29] gtable_0.2.0           glue_1.3.0             GenomeInfoDbData_1.2.0 RANN_2.6.1            
[33] dplyr_0.7.8            Rcpp_1.0.0             gdata_2.18.0           ape_5.2               
[37] nlme_3.1-137           gbRd_0.4-11            lmtest_0.9-36          stringr_1.4.0         
[41] globals_0.12.4         irlba_2.3.3            gtools_3.8.1           future_1.11.1.1       
[45] MASS_7.3-51.1          zlibbioc_1.28.0        zoo_1.8-4              scales_1.0.0          
[49] RColorBrewer_1.1-2     yaml_2.2.0             reticulate_1.10        pbapply_1.4-0         
[53] ggplot2_3.1.0          stringi_1.3.1          caTools_1.17.1.1       bibtex_0.4.2          
[57] Rdpack_0.10-1          SDMTools_1.1-221       rlang_0.3.1            pkgconfig_2.0.2       
[61] bitops_1.0-6           lattice_0.20-38        ROCR_1.0-7             purrr_0.3.0           
[65] bindr_0.1.1            htmlwidgets_1.3        cowplot_0.9.4          tidyselect_0.2.5      
[69] plyr_1.8.4             magrittr_1.5           R6_2.3.0               gplots_3.0.1.1        
[73] pillar_1.3.1           fitdistrplus_1.0-14    survival_2.43-3        RCurl_1.95-4.11       
[77] tibble_2.0.1           future.apply_1.1.0     tsne_0.1-3             crayon_1.3.4          
[81] KernSmooth_2.23-15     plotly_4.8.0           grid_3.5.2             data.table_1.12.0     
[85] metap_1.1              digest_0.6.18          tidyr_0.8.2            R.utils_2.7.0         
[89] munsell_0.5.0          viridisLite_0.3.0     
>
granges • 308 views
ADD COMMENTlink modified 8 months ago by James W. MacDonald52k • written 8 months ago by rhooads70
Answer: GenomicRanges error constructing Granges
0
gravatar for James W. MacDonald
8 months ago by
United States
James W. MacDonald52k wrote:

You are doing a bunch of stuff on the front end, and also for some reason deciding halfway through to try to detach everything (?). Can you not just start R, load GenomicRanges and make a GRanges object? If that works, then you can try adding in the other stuff you are doing.

Also, the output from traceback after the error will tell us more than your code is showing. You are getting an error for a function strand, which comes from BiocGenerics, and certainly should have a method for character.

> d.f <- data.frame(chr = paste0("chr", 1:3), start = 1:3, end = 5:7, strand = rep("*", 3), sums = 7:9)
> makeGRangesFromDataFrame(d.f, keep.extra.columns = TRUE)
GRanges object with 3 ranges and 1 metadata column:
      seqnames    ranges strand |      sums
         <Rle> <IRanges>  <Rle> | <integer>
  [1]     chr1       1-5      * |         7
  [2]     chr2       2-6      * |         8
  [3]     chr3       3-7      * |         9
  -------
  seqinfo: 3 sequences from an unspecified genome; no seqlengths
> sessionInfo()
R version 3.5.1 (2018-07-02)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Debian GNU/Linux 8 (jessie)

Matrix products: default
BLAS: /data/oldR/R-3.5.1/lib64/R/lib/libRblas.so
LAPACK: /data/oldR/R-3.5.1/lib64/R/lib/libRlapack.so

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats4    parallel  stats     graphics  grDevices utils     datasets 
[8] methods   base     

other attached packages:
 [1] GenomicAlignments_1.18.1    SummarizedExperiment_1.12.0
 [3] DelayedArray_0.8.0          BiocParallel_1.16.6        
 [5] matrixStats_0.54.0          ensembldb_2.6.7            
 [7] AnnotationFilter_1.6.0      GenomicFeatures_1.35.9     
 [9] AnnotationDbi_1.44.0        AnnotationHub_2.14.5       
[11] Glimma_1.10.1               tximport_1.10.1            
[13] readr_1.3.1                 xtable_1.8-3               
[15] BiocStyle_2.10.0            htmlwidgets_1.3            
[17] threejs_0.3.1               igraph_1.2.4               
[19] Rsubread_1.32.4             ReportingTools_2.22.1      
[21] knitr_1.22                  affycoretools_1.54.0       
[23] Biobase_2.42.0              ggplot2_3.1.0              
[25] reshape2_1.4.3              openxlsx_4.1.0             
[27] edgeR_3.24.3                limma_3.38.3               
[29] Rsamtools_1.34.1            Biostrings_2.50.2          
[31] XVector_0.22.0              GenomicRanges_1.34.0       
[33] GenomeInfoDb_1.18.2         IRanges_2.16.0             
[35] S4Vectors_0.21.10           BiocGenerics_0.28.0        

ADD COMMENTlink written 8 months ago by James W. MacDonald52k

Restarting R, loading only GenomicRanges and running the commands in your answer worked. Thanks

ADD REPLYlink written 8 months ago by rhooads70
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 16.09
Traffic: 214 users visited in the last hour