no method for coercing this S4 class to a vector
1
0
Entering edit mode
Thiago Maia ▴ 40
@thiago-maia-5793
Last seen 9.6 years ago
Hello - Sorry if this message appear twice, before I sent from the wrong e-mail address(not registered) I trying to make a function in R but I couldnt figure out whats happening. I loadded a fasta file. seq2 <- readDNAStringSet("c:/teste/testfile.txt","fasta") I would like to get the length. If I execute directly all works > nchar(seq2[1]) Test_4489 90 But if I create a function function (N) { nchar(seq2[1]) } when I execute > fn1(1) Error in nchar(seq2[1]) : no method for coercing this S4 class to a vector I tried things like as.vector(nchar(seq2[1])) but got the same message. Does anybody have a suggestion what I have to do or read. thanks a lot Thiago Maia --
• 9.3k views
ADD COMMENT
0
Entering edit mode
Thiago Maia ▴ 40
@thiago-maia-5793
Last seen 9.6 years ago
Ops... sorry I just found (after many hours), it was so simple, how could take that long.. heheh nchar(as.character(seq2[i])) thanks Thiago Maia > Hello > > - Sorry if this message appear twice, before I sent from the > wrong e-mail address(not registered) > > I trying to make a function in R but I couldnt figure out whats > happening. > > I loadded a fasta file. > seq2 <- readDNAStringSet("c:/teste/testfile.txt","fasta") > > I would like to get the length. > If I execute directly all works > > > nchar(seq2[1]) > Test_4489 > 90 > > But if I create a function > function (N) { > nchar(seq2[1]) > } > > when I execute > > fn1(1) > Error in nchar(seq2[1]) : > no method for coercing this S4 class to a vector > > I tried things like as.vector(nchar(seq2[1])) but got the same > message. > > Does anybody have a suggestion what I have to do or read. > > thanks a lot > > Thiago Maia > > > -- > > _______________________________________________ > 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 --
ADD COMMENT
0
Entering edit mode
On 03/11/2013 06:43 AM, Thiago Maia wrote: > Ops... sorry I just found (after many hours), it was so simple, how could take > that long.. heheh > > nchar(as.character(seq2[i])) I do not think you've identified your problem correctly; please provide a reproducible example -- what is 'l' ? For instance, after library(Biostrings) example(readDNAStringSet) I can > seq2 = readDNAStringSet(filepath, format="fastq") > fn1 = function(N) nchar(seq2[l]) > l = 2 > fn1(l) [1] 36 (but the function doesn't make much sense, usually the body of the function references arguments passed in to it). This is with > sessionInfo() R version 2.15.2 Patched (2012-12-23 r61401) Platform: x86_64-unknown-linux-gnu (64-bit) 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=C LC_NAME=C [9] LC_ADDRESS=C LC_TELEPHONE=C [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] BSgenome.Celegans.UCSC.ce2_1.3.19 BSgenome_1.26.1 [3] GenomicRanges_1.10.7 Biostrings_2.26.3 [5] IRanges_1.16.6 BiocGenerics_0.4.0 [7] BiocInstaller_1.8.3 loaded via a namespace (and not attached): [1] parallel_2.15.2 stats4_2.15.2 tools_2.15.2 Martin > > thanks > > Thiago Maia >> Hello >> >> - Sorry if this message appear twice, before I sent from the wrong >> e-mail address(not registered) >> >> I trying to make a function in R but I couldnt figure out whats >> happening. >> >> I loadded a fasta file. >> seq2 <- readDNAStringSet("c:/teste/testfile.txt","fasta") >> >> I would like to get the length. >> If I execute directly all works >> >> > nchar(seq2[1]) >> Test_4489 >> 90 >> >> But if I create a function >> function (N) { >> nchar(seq2[1]) >> } >> >> when I execute >> > fn1(1) >> Error in nchar(seq2[1]) : >> no method for coercing this S4 class to a vector >> >> I tried things like as.vector(nchar(seq2[1])) but got the same >> message. >> >> Does anybody have a suggestion what I have to do or read. >> >> thanks a lot >> >> Thiago Maia >> >> >> -- >> >> _______________________________________________ >> 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 > > > -- > > _______________________________________________ > 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 -- Computational Biology / Fred Hutchinson Cancer Research Center 1100 Fairview Ave. N. PO Box 19024 Seattle, WA 98109 Location: Arnold Building M1 B861 Phone: (206) 667-2793
ADD REPLY
0
Entering edit mode
Hello Martin You can ignore the number 1. function () { print(nchar(as.character(seq2[1]))) } to execute fn1() actually the function became like this function (N) { for(i in as.numeric(1:N)) { print(substr(seq2[i],nchar(as.character(seq2[i]))-2,nchar(as. character(seq2[i])))) } } > fn1(3) Test_4489 "GGC" Test_0393 "CTC" Test_3575 "AAT" with as.character started to give the right result thanks a lot Thiago Maia > On 03/11/2013 06:43 AM, Thiago Maia wrote: >> Ops... sorry I just found (after many hours), it was so simple, how >> could take >> that long.. heheh >> >> nchar(as.character(seq2[i])) > > I do not think you've identified your problem correctly; please provide > a reproducible example -- what is 'l' ? For instance, after > > library(Biostrings) > example(readDNAStringSet) > > I can > > > seq2 = readDNAStringSet(filepath, format="fastq") > > fn1 = function(N) nchar(seq2[l]) > > l = 2 > > fn1(l) > [1] 36 > > (but the function doesn't make much sense, usually the body of the > function references arguments passed in to it). This is with > > > sessionInfo() > R version 2.15.2 Patched (2012-12-23 r61401) > Platform: x86_64-unknown-linux-gnu (64-bit) > > 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=C LC_NAME=C > [9] LC_ADDRESS=C LC_TELEPHONE=C > [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C > > attached base packages: > [1] stats graphics grDevices utils datasets methods base > > other attached packages: > [1] BSgenome.Celegans.UCSC.ce2_1.3.19 BSgenome_1.26.1 > [3] GenomicRanges_1.10.7 Biostrings_2.26.3 > [5] IRanges_1.16.6 BiocGenerics_0.4.0 > [7] BiocInstaller_1.8.3 > > loaded via a namespace (and not attached): > [1] parallel_2.15.2 stats4_2.15.2 tools_2.15.2 > > Martin >> >> thanks >> >> Thiago Maia >>> Hello >>> >>> - Sorry if this message appear twice, before I sent from the >>> wrong >>> e-mail address(not registered) >>> >>> I trying to make a function in R but I couldnt figure out whats >>> happening. >>> >>> I loadded a fasta file. >>> seq2 <- readDNAStringSet("c:/teste/testfile.txt","fasta") >>> >>> I would like to get the length. >>> If I execute directly all works >>> >>> > nchar(seq2[1]) >>> Test_4489 >>> 90 >>> >>> But if I create a function >>> function (N) { >>> nchar(seq2[1]) >>> } >>> >>> when I execute >>> > fn1(1) >>> Error in nchar(seq2[1]) : >>> no method for coercing this S4 class to a vector >>> >>> I tried things like as.vector(nchar(seq2[1])) but got the same >>> message. >>> >>> Does anybody have a suggestion what I have to do or read. >>> >>> thanks a lot >>> >>> Thiago Maia >>> >>> >>> -- >>> >>> _______________________________________________ >>> 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 >> >> >> -- >> >> _______________________________________________ >> 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 > > --
ADD REPLY
0
Entering edit mode
On 03/11/2013 07:06 AM, Thiago Maia wrote: > function () { > print(nchar(as.character(seq2[1]))) > } > > to execute > fn1() > > > actually the function became like this > function (N) { > for(i in as.numeric(1:N)) { > > print(substr(seq2[i],nchar(as.character(seq2[i]))-2,nchar(as.charact er(seq2[i])))) > } > } Hi Thiago -- but the as.character() are not necessary function (N) { for(i in as.numeric(1:N)) { print(substr(seq2[i], nchar(seq2[i])-2, nchar(seq2[i]))) } } and remember that R is 'vectorized', so fn1 <- function(N) { ## better: function(N, seq2) i <- 1:N ## better: i <- seq_len(N) substr(seq2[i], nchar(seq2[i]) - 2, nchar(seq2[i])) } so after library(Biostrings) example(readDNAStringSet) we have > seq2 <- readDNAStringSet(filepath, format="fastq") > fn1(10) HWI-EAS88_1_1_1_1001_499 HWI-EAS88_1_1_1_898_392 HWI- EAS88_1_1_1_922_465 "TGT" "GAC" "GGT" HWI-EAS88_1_1_1_895_493 HWI-EAS88_1_1_1_953_493 HWI- EAS88_1_1_1_868_763 "AAA" "CTT" "GCG" HWI-EAS88_1_1_1_819_788 HWI-EAS88_1_1_1_801_123 HWI- EAS88_1_1_1_885_419 "CTT" "CTT" "GTT" HWI-EAS88_1_1_1_941_477 "AAT" -- Computational Biology / Fred Hutchinson Cancer Research Center 1100 Fairview Ave. N. PO Box 19024 Seattle, WA 98109 Location: Arnold Building M1 B861 Phone: (206) 667-2793
ADD REPLY
0
Entering edit mode
Hello Martin Thanks a lot for your help. For me, if I try without as.character it doesnt work. But how you show, for you it works I realize that you are using format="fastq" and I'm using format="fasta" I attached the file. But I dont want to use your time any more, you already help me a lot. I'm going to try to convert to fastq to see if I can get the same result as you. thank you very much Thiago Maia On 03/11/2013 07:06 AM, Thiago Maia wrote: > function () { > print(nchar(as.character(seq2[1]))) > } > > to execute > fn1() > > > actually the function became like this > function (N) { > for(i in as.numeric(1:N)) { > > print(substr(seq2[i],nchar(as.character(seq2[i]))-2,nchar(as.charact er(seq2[i])))) > } > } Hi Thiago -- but the as.character() are not necessary function (N) { for(i in as.numeric(1:N)) { print(substr(seq2[i], nchar(seq2[i])-2, nchar(seq2[i]))) } } and remember that R is 'vectorized', so fn1 <- function(N) { ## better: function(N, seq2) i <- 1:N ## better: i <- seq_len(N) substr(seq2[i], nchar(seq2[i]) - 2, nchar(seq2[i])) } so after library(Biostrings) example(readDNAStringSet) we have > seq2 <- readDNAStringSet(filepath, format="fastq") > fn1(10) HWI-EAS88_1_1_1_1001_499 HWI-EAS88_1_1_1_898_392 HWI- EAS88_1_1_1_922_465 "TGT" "GAC" "GGT" HWI-EAS88_1_1_1_895_493 HWI-EAS88_1_1_1_953_493 HWI-EAS88_1_1_1_868_763 "AAA" "CTT" "GCG" HWI-EAS88_1_1_1_819_788 HWI-EAS88_1_1_1_801_123 HWI-EAS88_1_1_1_885_419 "CTT" "CTT" "GTT" HWI-EAS88_1_1_1_941_477 "AAT" -- Computational Biology / Fred Hutchinson Cancer Research Center 1100 Fairview Ave. N. PO Box 19024 Seattle, WA 98109 Location: Arnold Building M1 B861 Phone: (206) 667-2793 -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: rosalind_grph.txt URL: <https: stat.ethz.ch="" pipermail="" bioconductor="" attachments="" 20130311="" f13ae85f="" attachment.txt="">
ADD REPLY
0
Entering edit mode
Hi -- On 03/11/2013 07:50 AM, Martin Morgan wrote: > Hello Martin > > Thanks a lot for your help. For me, if I try without as.character it > doesnt work. Please show an entire session! There is something that you are doing that I have not understood. Here is the content of the file that I have, test.R library(Biostrings) fn1 <- function(N) { ## better: function(N, seq2) i <- 1:N ## better: i <- seq_len(N) substr(seq2[i], nchar(seq2[i]) - 2, nchar(seq2[i])) } seq2 <- readDNAStringSet("rosalind_grph.txt") fn1(10) sessionInfo() I can run it from within R with source("test.R", echo=TRUE, max=Inf) provided the path to to test.R and rosalind_grph.txt is correct, and get > source("test.R", echo=TRUE, max=Inf) > library(Biostrings) > fn1 <- function(N) { ## better: function(N, seq2) + i <- 1:N ## better: i <- seq_len(N) + substr(seq2[i], nchar(seq2[i]) - 2, nchar(seq2[i])) + } > seq2 <- readDNAStringSet("rosalind_grph.txt") > fn1(10) Rosalind_4489 Rosalind_0393 Rosalind_3575 Rosalind_5369 Rosalind_0109 "GGC" "CTC" "AAT" "ATT" "GAG" Rosalind_6578 Rosalind_8695 Rosalind_9187 Rosalind_2515 Rosalind_0826 "TGG" "TGG" "CGA" "CGC" "AGT" > sessionInfo() R version 2.15.2 Patched (2012-12-23 r61401) Platform: x86_64-unknown-linux-gnu (64-bit) 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=C LC_NAME=C [9] LC_ADDRESS=C LC_TELEPHONE=C [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] Biostrings_2.26.3 IRanges_1.16.6 BiocGenerics_0.4.0 loaded via a namespace (and not attached): [1] parallel_2.15.2 stats4_2.15.2 What do you get? > But how you show, for you it works > I realize that you are using format="fastq" and I'm using format="fasta" > I attached the file. But I dont want to use your time any more, you > already help me a lot. > I'm going to try to convert to fastq to see if I can get the same result > as you. you have successfully read in your data, so the input format is not an issue. You indicated that nchar(seq2[1]) works at the command line. There is no reason why the same command would not work inside a function fn1 = function() nchar(seq2[1]) Maybe you are writing the function in a package and have not specified the NAMESPACE file corrrectly, or running the function in a new R session when you have not loaded Biostrings, or loading another package that is somehow interfering with how nchar() works, or defining seq2 to be something other than the data used at the command line? Martin > > thank you very much > > Thiago Maia > > On 03/11/2013 07:06 AM, Thiago Maia wrote: >> function () { >> print(nchar(as.character(seq2[1]))) >> } >> >> to execute >> fn1() >> >> >> actually the function became like this >> function (N) { >> for(i in as.numeric(1:N)) { >> >> print(substr(seq2[i],nchar(as.character(seq2[i]))-2,nchar(as.charac ter(seq2[i])))) >> >> } >> } > > Hi Thiago -- but the as.character() are not necessary > > function (N) { > for(i in as.numeric(1:N)) { > print(substr(seq2[i], nchar(seq2[i])-2, nchar(seq2[i]))) > } > } > > > and remember that R is 'vectorized', so > > fn1 <- function(N) { ## better: function(N, seq2) > i <- 1:N ## better: i <- seq_len(N) > substr(seq2[i], nchar(seq2[i]) - 2, nchar(seq2[i])) > } > > so after > > library(Biostrings) > example(readDNAStringSet) > > we have > > > seq2 <- readDNAStringSet(filepath, format="fastq") > > fn1(10) > HWI-EAS88_1_1_1_1001_499 HWI-EAS88_1_1_1_898_392 HWI- EAS88_1_1_1_922_465 > "TGT" "GAC" "GGT" > HWI-EAS88_1_1_1_895_493 HWI-EAS88_1_1_1_953_493 HWI- EAS88_1_1_1_868_763 > "AAA" "CTT" "GCG" > HWI-EAS88_1_1_1_819_788 HWI-EAS88_1_1_1_801_123 HWI- EAS88_1_1_1_885_419 > "CTT" "CTT" "GTT" > HWI-EAS88_1_1_1_941_477 > "AAT" > > > > > > _______________________________________________ > 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 > -- Computational Biology / Fred Hutchinson Cancer Research Center 1100 Fairview Ave. N. PO Box 19024 Seattle, WA 98109 Location: Arnold Building M1 B861 Phone: (206) 667-2793
ADD REPLY

Login before adding your answer.

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