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
>
Restarting R, loading only GenomicRanges and running the commands in your answer worked. Thanks