Help on sapply() with GRanges
3
0
Entering edit mode
@prashantha-hebbar-3526
Last seen 4.4 years ago
Hello friends, I am trying to get genome cordinate information for list of genes using sapply function. I find an error saying " Error in c(values, x) : all arguments in '...' must be CompressedList objects " Following is the code I am using. library(VariantAnnotation) library(TxDb.Hsapiens.UCSC.hg19.knownGene) txdb = TxDb.Hsapiens.UCSC.hg19.knownGene txg = transcriptsBy(txdb,by="gene") allGeneRange <- GRanges() genes<-c("47","4067","2017") gRangeFun <- function(i){       geneRange <- reduce(txg[i])       allGeneRange<-append(allGeneRange, geneRange)       return(allGeneRange) } x<-sapply(genes, gRangeFun ) print(x) I wanted to have coordination of all genes in one GRange object to process it further. when I print x following error appears. Can somebody tell me what is going wrong with sapply() usage? Here is sessionInfo() > sessionInfo() R version 2.15.2 (2012-10-26) Platform: i686-pc-linux-gnu (32-bit) locale: [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C LC_TIME=C LC_COLLATE=C [5] LC_MONETARY=C LC_MESSAGES=C LC_PAPER=C LC_NAME=C [9] LC_ADDRESS=C LC_TELEPHONE=C LC_MEASUREMENT=C LC_IDENTIFICATION=C attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] VariantAnnotation_1.4.12 Rsamtools_1.10.2 [3] Biostrings_2.26.3 TxDb.Hsapiens.UCSC.hg19.knownGene_2.8.0 [5] GenomicFeatures_1.10.2 AnnotationDbi_1.20.7 [7] Biobase_2.18.0 GenomicRanges_1.10.7 [9] IRanges_1.16.6 BiocGenerics_0.4.0 loaded via a namespace (and not attached): [1] BSgenome_1.26.1 DBI_0.2-7 RCurl_1.95-4.1 RSQLite_0.11.4 [5] XML_3.98-1.1 biomaRt_2.14.0 bitops_1.0-5 parallel_2.15.2 [9] rtracklayer_1.18.2 stats4_2.15.2 tools_2.15.2 zlibbioc_1.4.0 Regards, Prashantha [[alternative HTML version deleted]]
• 1.7k views
ADD COMMENT
0
Entering edit mode
@michael-lawrence-3846
Last seen 2.4 years ago
United States
On Tue, Nov 26, 2013 at 10:00 AM, Prashantha Hebbar < prashantha.hebbar@yahoo.com> wrote: > Hello friends, > > I am trying to get genome cordinate information for list of genes using > sapply function. I find an error saying " > Error in c(values, x) : all arguments in '...' must be CompressedList > objects > > " > > Following is the code I am using. > > library(VariantAnnotation) > library(TxDb.Hsapiens.UCSC.hg19.knownGene) > txdb = TxDb.Hsapiens.UCSC.hg19.knownGene > txg = transcriptsBy(txdb,by="gene") > allGeneRange <- GRanges() > genes<-c("47","4067","2017") > gRangeFun <- function(i){ > geneRange <- reduce(txg[i]) > allGeneRange<-append(allGeneRange, geneRange) > return(allGeneRange) > } > x<-sapply(genes, gRangeFun ) > print(x) > > I wanted to have coordination of all genes in one GRange object to process > it further. when I print x following error appears. Can somebody tell me > what is going wrong with sapply() usage? > > When using *apply() to iterate, you're calling a function on each element, and the return values from the function calls are automatically concatenated into a list, with the same order as the input. In the case of sapply, there is an attempt to simplify the list. But the bottom-line is that function calls should not (and will not without special effort) have side-effects outside of their scope. So one way to do this would be: gRangeFun <- function(i){ reduce(txg[i]) } x<-sapply(genes, gRangeFun) But 'x' will be a list, because sapply does not know how to simplify the list of S4 objects. So to concatenate the list elements into a GRanges, we can do: do.call(c, unname(x)). However, it is often not necessary to explicitly iterate in R, and this is also true of the GenomicRanges API. Operations like reduce() are implicitly vectorized over GRangesList, so the above code simplifies to: geneRange <- unlist(reduce(txg[genes])) However, I think what you really want here is to call range(), not reduce(), geneRange <- unlist(range(txg[genes])) Hope that helps, Michael Here is sessionInfo() > > > sessionInfo() R version 2.15.2 (2012-10-26) > Platform: i686-pc-linux-gnu (32-bit) locale: [1] LC_CTYPE=en_US.UTF-8 > LC_NUMERIC=C LC_TIME=C LC_COLLATE=C [5] > LC_MONETARY=C LC_MESSAGES=C LC_PAPER=C LC_NAME=C > [9] LC_ADDRESS=C LC_TELEPHONE=C LC_MEASUREMENT=C > LC_IDENTIFICATION=C attached base packages: > [1] stats graphics grDevices utils datasets methods base > other attached packages: [1] VariantAnnotation_1.4.12 > Rsamtools_1.10.2 [3] Biostrings_2.26.3 > TxDb.Hsapiens.UCSC.hg19.knownGene_2.8.0 [5] > GenomicFeatures_1.10.2 AnnotationDbi_1.20.7 > [7] Biobase_2.18.0 GenomicRanges_1.10.7 > [9] IRanges_1.16.6 BiocGenerics_0.4.0 > loaded via a namespace (and not attached): [1] > BSgenome_1.26.1 DBI_0.2-7 RCurl_1.95-4.1 RSQLite_0.11.4 > [5] XML_3.98-1.1 biomaRt_2.14.0 bitops_1.0-5 > parallel_2.15.2 [9] rtracklayer_1.18.2 stats4_2.15.2 tools_2.15.2 > zlibbioc_1.4.0 > > Regards, > Prashantha > [[alternative HTML version deleted]] > > > _______________________________________________ > Bioconductor mailing list > Bioconductor@r-project.org > https://stat.ethz.ch/mailman/listinfo/bioconductor > Search the archives: > http://news.gmane.org/gmane.science.biology.informatics.conductor > [[alternative HTML version deleted]]
ADD COMMENT
0
Entering edit mode
@james-w-macdonald-5106
Last seen 6 hours ago
United States
Hi Prashanatha, On Tuesday, November 26, 2013 1:00:13 PM, Prashantha Hebbar wrote: > Hello friends, > > I am trying to get genome cordinate information for list of genes using sapply function. I find an error saying " > Error in c(values, x) : all arguments in '...' must be CompressedList objects > > " > > Following is the code I am using. > > library(VariantAnnotation) > library(TxDb.Hsapiens.UCSC.hg19.knownGene) > txdb = TxDb.Hsapiens.UCSC.hg19.knownGene > txg = transcriptsBy(txdb,by="gene") z <- txg[genes] zr <- reduce(z) unlist(zr) GRanges with 3 ranges and 0 metadata columns: seqnames ranges strand <rle> <iranges> <rle> 47 chr17 [40023179, 40075272] - 4067 chr8 [56792386, 56925006] + 2017 chr11 [70244612, 70282690] + --- seqlengths: chr1 chr2 ... chrUn_gl000249 249250621 243199373 ... 38502 Is that what you are trying to do? Best, Jim > allGeneRange <- GRanges() > genes<-c("47","4067","2017") > gRangeFun <- function(i){ > geneRange <- reduce(txg[i]) > allGeneRange<-append(allGeneRange, geneRange) > return(allGeneRange) > } > x<-sapply(genes, gRangeFun ) > print(x) > > I wanted to have coordination of all genes in one GRange object to process it further. when I print x following error appears. Can somebody tell me what is going wrong with sapply() usage? > > Here is sessionInfo() > >> sessionInfo() R version 2.15.2 (2012-10-26) > Platform: i686-pc-linux-gnu (32-bit) locale: [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C LC_TIME=C LC_COLLATE=C [5] LC_MONETARY=C LC_MESSAGES=C LC_PAPER=C LC_NAME=C [9] LC_ADDRESS=C LC_TELEPHONE=C LC_MEASUREMENT=C LC_IDENTIFICATION=C attached base packages: > [1] stats graphics grDevices utils datasets methods base other attached packages: [1] VariantAnnotation_1.4.12 Rsamtools_1.10.2 [3] Biostrings_2.26.3 TxDb.Hsapiens.UCSC.hg19.knownGene_2.8.0 [5] GenomicFeatures_1.10.2 AnnotationDbi_1.20.7 [7] Biobase_2.18.0 GenomicRanges_1.10.7 [9] IRanges_1.16.6 BiocGenerics_0.4.0 loaded via a namespace (and not attached): [1] BSgenome_1.26.1 DBI_0.2-7 RCurl_1.95-4.1 RSQLite_0.11.4 [5] XML_3.98-1.1 biomaRt_2.14.0 bitops_1.0-5 parallel_2.15.2 [9] rtracklayer_1.18.2 stats4_2.15.2 tools_2.15.2 zlibbioc_1.4.0 > > Regards, > Prashantha > [[alternative HTML version deleted]] > > > > _______________________________________________ > Bioconductor mailing list > Bioconductor at r-project.org > https://stat.ethz.ch/mailman/listinfo/bioconductor > Search the archives: http://news.gmane.org/gmane.science.biology.informatics.conductor -- James W. MacDonald, M.S. Biostatistician University of Washington Environmental and Occupational Health Sciences 4225 Roosevelt Way NE, # 100 Seattle WA 98105-6099
ADD COMMENT
0
Entering edit mode
Krunal • 0
@Krunal-24460
Last seen 3.3 years ago

The sapply() function in R works like lapply(), but it tries to interpret the output to the most fundamental data structure possible, which is either Vector or Matrix. The sapply() is a “wrapper” function for lapply().

The apply() function returns the vector or array by applying a function to the margins of the array or matrix. The lapply() function is useful for performing operations on list objects and returns the list object of the same length as the original set.

ADD COMMENT

Login before adding your answer.

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