Functions
1
0
Entering edit mode
Guest User ★ 13k
@guest-user-4897
Last seen 11.2 years ago
Hi I am trying to do some data analysis using R and Bioconductor. I have a function to read my data called "ReadAffyData" and a function to plot the data called "preqc". I want to know if there is any way I can extract information (read: Data) from the ReadAffyData function into preqc in order to produce plots? I have not completely understood the inheritance in R and I am getting errors similar to the one below. I am new to R and could do with any help in this aspect! Code: ReadAffyData <- function( filename ) { require(affy) require(annotate) Cov <- read.table( filename, sep="\t", header=1, quote="", comment="" ) if( ! all( c( "Filename", "Label", "Repl", "Trt" ) %in% colnames( Cov ) ) ) { stop( "Missing mandatory column" ) } Cov <- Cov[order(Cov$Trt),] i <- table(Cov$Trt) Cov$Repl <- unlist( lapply( i, function( j ) 1:j ) ) Cov$Label <- paste( as.character( Cov$Trt ), Cov$Repl, sep=":" ) rownames(Cov) <- as.character(Cov$Label) tmp1 <- colnames( Cov ) tmp2 <- rep( "", length( Cov ) ) for( i in 1:length( tmp2 ) ) { tmp2[i] <- paste( sort( unique( as.character( Cov[,i] ) ) ), collapse="/" ) } labelDescription <- data.frame( labelDescription=tmp2 ) rownames( labelDescription ) <- tmp1 tmp <- new( "AnnotatedDataFrame", data=Cov, varMetadata=labelDescription) Data <- ReadAffy( sampleNames=as.character( Cov$Label ),phenoData=tmp, verbose=TRUE ) } preQC <- function(name){ ReadAffyData(name) plotDensity( log2( pm( Data ) ), xlab="Log2( Intensity )", ylab="Density", main="Raw(PM)") } preQC("cov.txt") 1 reading C:/CEL/GSM311471.CEL ...instantiating an AffyBatch (intensity a 506944x24 matrix)...done. Reading in : C:/CEL/GSM311471.CEL Reading in : C:/CEL/GSM311472.CEL Reading in : C:/CEL/GSM311473.CEL Reading in : C:/CEL/GSM311474.CEL Reading in : C:/CEL/GSM311475.CEL . . . Error in pm(Data) : error in evaluating the argument 'object' in selecting a method for function 'pm': Error: object 'Data' not found -- output of sessionInfo(): sessionInfo() R version 3.0.1 (2013-05-16) Platform: i386-w64-mingw32/i386 (32-bit) locale: [1] LC_COLLATE=English_India.1252 LC_CTYPE=English_India.1252 LC_MONETARY=English_India.1252 [4] LC_NUMERIC=C LC_TIME=English_India.1252 attached base packages: [1] parallel stats graphics grDevices utils datasets methods base other attached packages: [1] annotate_1.38.0 AnnotationDbi_1.22.6 affy_1.38.1 Biobase_2.20.0 BiocGenerics_0.6.0 loaded via a namespace (and not attached): [1] affyio_1.28.0 BiocInstaller_1.10.1 DBI_0.2-7 IRanges_1.18.1 preprocessCore_1.22.0 [6] RSQLite_0.11.4 stats4_3.0.1 tools_3.0.1 XML_3.96-1.1 xtable_1.7-1 [11] zlibbioc_1.6.0 -- Sent via the guest posting facility at bioconductor.org.
• 1.0k views
ADD COMMENT
0
Entering edit mode
@james-w-macdonald-5106
Last seen 10 hours ago
United States
Hi Ipsitha, On 6/7/2013 12:26 PM, Ipsitha [guest] wrote: > Hi > > I am trying to do some data analysis using R and Bioconductor. I have a function to read my data called "ReadAffyData" and a function to plot the data called "preqc". I want to know if there is any way I can extract information (read: Data) from the ReadAffyData function into preqc in order to produce plots? I have not completely understood the inheritance in R and I am getting errors similar to the one below. I am new to R and could do with any help in this aspect! > > Code: > ReadAffyData<- function( filename ) { > > require(affy) > require(annotate) > > Cov<- read.table( filename, sep="\t", header=1, quote="", comment="" ) > if( ! all( c( "Filename", "Label", "Repl", "Trt" ) %in% colnames( Cov ) ) ) > { > stop( "Missing mandatory column" ) > } > > Cov<- Cov[order(Cov$Trt),] > i<- table(Cov$Trt) > Cov$Repl<- unlist( lapply( i, function( j ) 1:j ) ) > Cov$Label<- paste( as.character( Cov$Trt ), Cov$Repl, sep=":" ) > rownames(Cov)<- as.character(Cov$Label) > > tmp1<- colnames( Cov ) > tmp2<- rep( "", length( Cov ) ) > > for( i in 1:length( tmp2 ) ) > { > tmp2[i]<- paste( sort( unique( as.character( Cov[,i] ) ) ), collapse="/" ) > } > > labelDescription<- data.frame( labelDescription=tmp2 ) > > rownames( labelDescription )<- tmp1 > tmp<- new( "AnnotatedDataFrame", data=Cov, varMetadata=labelDescription) > > Data<- ReadAffy( sampleNames=as.character( Cov$Label ),phenoData=tmp, verbose=TRUE ) > } This function doesn't return anything. You can end the function in one of three ways. The last lines can be ReadAffy(sampleNames=as.character( Cov$Label ),phenoData=tmp, verbose=TRUE ) } or Data <- ReadAffy( sampleNames=as.character( Cov$Label ),phenoData=tmp, verbose=TRUE ) Data } or Data <- ReadAffy( sampleNames=as.character( Cov$Label ),phenoData=tmp, verbose=TRUE ) return(Data) } But your function as written creates the Data object and then closes without returning that object to you. > > preQC<- function(name){ > ReadAffyData(name) > plotDensity( log2( pm( Data ) ), xlab="Log2( Intensity )", ylab="Density", main="Raw(PM)") > } And even if you fix your ReadAffyData function to return the Data object, this function won't work, because you aren't capturing the output in an object. You need to do this: preQC<- function(name){ Data<- ReadAffyData(name) plotDensity( log2( pm( Data ) ), xlab="Log2( Intensity )", ylab="Density", main="Raw(PM)") } Best, Jim > > preQC("cov.txt") > 1 reading C:/CEL/GSM311471.CEL ...instantiating an AffyBatch (intensity a 506944x24 matrix)...done. > Reading in : C:/CEL/GSM311471.CEL > Reading in : C:/CEL/GSM311472.CEL > Reading in : C:/CEL/GSM311473.CEL > Reading in : C:/CEL/GSM311474.CEL > Reading in : C:/CEL/GSM311475.CEL > . > . > . > Error in pm(Data) : > error in evaluating the argument 'object' in selecting a method for function 'pm': Error: object 'Data' not found > > > -- output of sessionInfo(): > > sessionInfo() > R version 3.0.1 (2013-05-16) > Platform: i386-w64-mingw32/i386 (32-bit) > > locale: > [1] LC_COLLATE=English_India.1252 LC_CTYPE=English_India.1252 LC_MONETARY=English_India.1252 > [4] LC_NUMERIC=C LC_TIME=English_India.1252 > > attached base packages: > [1] parallel stats graphics grDevices utils datasets methods base > > other attached packages: > [1] annotate_1.38.0 AnnotationDbi_1.22.6 affy_1.38.1 Biobase_2.20.0 BiocGenerics_0.6.0 > > loaded via a namespace (and not attached): > [1] affyio_1.28.0 BiocInstaller_1.10.1 DBI_0.2-7 IRanges_1.18.1 preprocessCore_1.22.0 > [6] RSQLite_0.11.4 stats4_3.0.1 tools_3.0.1 XML_3.96-1.1 xtable_1.7-1 > [11] zlibbioc_1.6.0 > > > -- > Sent via the guest posting facility at bioconductor.org. > > _______________________________________________ > 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
... au contraire mon frere ... On Mon, Jun 10, 2013 at 2:06 PM, James W. MacDonald <jmacdon at="" uw.edu=""> wrote: [snip] >> Data<- ReadAffy( sampleNames=as.character( Cov$Label >> ),phenoData=tmp, verbose=TRUE ) >> } > > > This function doesn't return anything. You can end the function in one of > three ways. The last lines can be As the opportunity to correct Jim doesn't arise too often, you've got to keep your sense sharp and your keyboard ready plugged in. R> nothing <- function(x) { y <- x + 10 } R> something <- nothing(10) R> something [1] 20 Behold: we have created something from .. well .. you get the idea. And now for my next trick: I'll make myself disappear. -ste**poof**
ADD REPLY
0
Entering edit mode
Hi Steve, On 6/10/2013 7:36 PM, Steve Lianoglou wrote: > ... au contraire mon frere ... > > On Mon, Jun 10, 2013 at 2:06 PM, James W. MacDonald<jmacdon at="" uw.edu=""> wrote: > [snip] >>> Data<- ReadAffy( sampleNames=as.character( Cov$Label >>> ),phenoData=tmp, verbose=TRUE ) >>> } >> >> This function doesn't return anything. You can end the function in one of >> three ways. The last lines can be > As the opportunity to correct Jim doesn't arise too often, you've got > to keep your sense sharp and your keyboard ready plugged in. > > R> nothing<- function(x) { > y<- x + 10 > } > > R> something<- nothing(10) > R> something > [1] 20 Hrmm. Grumble grumble grumble. Oddly enough (to me at least) what you say is true. Howeva, it is dependent on assignment to an object: > nothing <- function(x) y <- x+10 > nothing(10) > something <- nothing(10) > something [1] 20 > nothing2 <- function(x) { + y <- x+10 + y + } > nothing2(10) [1] 20 Best, Jim > > Behold: we have created something from .. well .. you get the idea. > > And now for my next trick: I'll make myself disappear. > > -ste**poof** -- James W. MacDonald, M.S. Biostatistician University of Washington Environmental and Occupational Health Sciences 4225 Roosevelt Way NE, # 100 Seattle WA 98105-6099
ADD REPLY
0
Entering edit mode
Hi, On Tue, Jun 11, 2013 at 7:23 AM, James W. MacDonald <jmacdon at="" uw.edu=""> wrote: > Hi Steve, > > > On 6/10/2013 7:36 PM, Steve Lianoglou wrote: >> >> ... au contraire mon frere ... >> >> On Mon, Jun 10, 2013 at 2:06 PM, James W. MacDonald<jmacdon at="" uw.edu=""> >> wrote: >> [snip] >>>> >>>> Data<- ReadAffy( sampleNames=as.character( Cov$Label >>>> ),phenoData=tmp, verbose=TRUE ) >>>> } >>> >>> >>> This function doesn't return anything. You can end the function in one of >>> three ways. The last lines can be >> >> As the opportunity to correct Jim doesn't arise too often, you've got >> to keep your sense sharp and your keyboard ready plugged in. >> >> R> nothing<- function(x) { >> y<- x + 10 >> } >> >> R> something<- nothing(10) >> R> something >> [1] 20 > > > Hrmm. Grumble grumble grumble. > > Oddly enough (to me at least) what you say is true. Howeva, it is dependent > on assignment to an object: > > >> nothing <- function(x) y <- x+10 >> nothing(10) >> something <- nothing(10) >> something > [1] 20 >> nothing2 <- function(x) { > + y <- x+10 > + y > + } >> nothing2(10) > [1] 20 Indeed -- it kind of feels like ending a function with an assignment is almost like returning "that thing" invisibly, though I'm not quite sure what (if any) the differences are. Anyway, apologies if I've derailed a helpful reply a bit too far by jumping in with some random R bar trivia. -steve -- Steve Lianoglou Computational Biologist Bioinformatics and Computational Biology Genentech
ADD REPLY

Login before adding your answer.

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