diff r-gpca.R @ 1:d93629e79f5e draft

planemo upload for repository https://github.com/blankenberglab/galaxy-tools-blankenberg/tree/master/tools/r-gpca commit 3b791d44853d8928046914cd93112bdcf89d4965
author blankenberglab
date Tue, 27 Nov 2018 11:02:46 -0500
parents ed24bacaa4d0
children 4865e7f4e9c5
line wrap: on
line diff
--- a/r-gpca.R	Mon Nov 19 10:15:19 2018 -0500
+++ b/r-gpca.R	Tue Nov 27 11:02:46 2018 -0500
@@ -1,6 +1,7 @@
 #!/usr/bin/env Rscript 
 library(optparse)
 library(gPCA)
+options(bitmapType='cairo')
 
 
 # parse options
@@ -105,82 +106,92 @@
   stop((paste('File for data', opt$data, 'does not exist')))
 }
 
-batch<-as.numeric(factor(as.matrix(read.delim(opt$batch, header=TRUE, row.names=1, sep="\t"))))
-
+dir.create(opt$path);
 data<-t(as.matrix(read.delim(opt$data, row.names=1, header=TRUE, sep="\t")))
+batch<-as.matrix(read.delim(opt$batch, header=TRUE, row.names=1, sep="\t"))
 
-out<-gPCA.batchdetect(x=data, batch=batch, center=opt$center, scaleY=opt$scaleY, nperm=opt$nperm, filt=opt$filt, seed=opt$seed)
+write('<html><body><a name="top"></a>',file = opt$html_outfile, append=TRUE)
+write('#Batch\tDelta\tP-value\tCumulative Variance',file = opt$numbers_outfile, append=TRUE)
+write('<div align="left><ul id =""><p><b>Table of Contents</b></p></ul></div>', file=opt$html_outfile, append=TRUE)
+
+for (row in 1:nrow(batch)) {
+  write(paste0('<li><a href="#',row.names(batch)[row],'">',row.names(batch)[row],'</a></li>'), file=opt$html_outfile, append=TRUE)
+}
+for (row in 1:nrow(batch)) {
+  batch1<-as.numeric(factor(batch[row,]))
+
+out<-gPCA.batchdetect(x=data, batch=batch1, center=opt$center, scaleY=opt$scaleY, nperm=opt$nperm, filt=opt$filt, seed=opt$seed)
 
 out$varPCg1<-((out$varPCg1-out$varPCu1)/out$varPCg1)*100
 
-dir.create(opt$path);  
-
-write.table(data.frame(out$delta, out$p.val, out$varPCg1),file=opt$numbers_outfile, sep="\t", row.name=FALSE, quote=FALSE)
+write(paste(row.names(batch)[row],out$delta,out$p.val,out$varPCg1,sep="\t"),file = opt$numbers_outfile, append=TRUE)
 
 # General Distribution 
 par(mai=c(0.8,0.8,0.1,0.1),cex=0.8)
-png(paste(opt$path,'/gDist.png', sep=""), width=1020, height=800, units='px')
+png(paste(opt$path,'/gDist_',row,'.png', sep=""), width=1020, height=800, units='px')
 gDist(out)
 invisible(dev.off())
 
 
 # Guided/Unguided 1v2
 par(mai=c(0.8,0.8,0.1,0.1),cex=0.8)
-png(paste(opt$path,'/guided_1v2.png', sep=""), width=1020, height=800, units='px')
+png(paste(opt$path,'/guided_1v2_',row,'.png', sep=""), width=1020, height=800, units='px')
 PCplot(out,ug="unguided",type="1v2")
 invisible(dev.off())
 
 par(mai=c(0.8,0.8,0.1,0.1),cex=0.8)
-png(paste(opt$path,'/unguided_1v2.png', sep=""), width=1020, height=800, units='px')
+png(paste(opt$path,'/unguided_1v2_',row,'.png', sep=""), width=1020, height=800, units='px')
 PCplot(out,ug="unguided",type="1v2")
 invisible(dev.off())
 
 
 # Guided/Unguided comp,3
 par(mai=c(0.65,0.65,0.1,0.1),cex=0.8)
-png(paste(opt$path,'/guided_npcs.png', sep=""), width=1020, height=800, units='px')
+png(paste(opt$path,'/guided_npcs_',row,'.png', sep=""), width=1020, height=800, units='px')
 PCplot(out,ug="guided",type="comp",npcs=opt$npcs)
 invisible(dev.off())
 
 par(mai=c(0.65,0.65,0.1,0.1),cex=0.8)
-png(paste(opt$path,'/unguided_npcs.png', sep=""), width=1020, height=800, units='px')
+png(paste(opt$path,'/unguided_npcs_',row,'.png', sep=""), width=1020, height=800, units='px')
 PCplot(out,ug="unguided",type="comp",npcs=opt$npcs)
 invisible(dev.off())
 
 
 # Guided/Unguided CumlativeVarPlot
 par(mai=c(0.8,0.8,0.1,0.1),cex=0.8)
-png(paste(opt$path,'/guided_var.png', sep=""), width=1020, height=800, units='px')
+png(paste(opt$path,'/guided_var_',row,'.png', sep=""), width=1020, height=800, units='px')
 CumulativeVarPlot(out,ug="guided",col="blue")
 invisible(dev.off())
 
 par(mai=c(0.8,0.8,0.1,0.1),cex=0.8)
-png(paste(opt$path,'/unguided_var.png', sep=""), width=1020, height=800, units='px')
+png(paste(opt$path,'/unguided_var_',row,'.png', sep=""), width=1020, height=800, units='px')
 CumulativeVarPlot(out,ug="unguided",col="blue")
 invisible(dev.off())
 
 
-write(paste('<html>
-<table id="r-gpca-wrap" align="center" border="1">
+write(paste0('
+<header id=',row.names(batch)[row],'><center><font size="11">',row.names(batch)[row],'</font><center></header>
+<br>           
+<table align="center" border="1">
  <tr>
   <th>Delta</th><th>P-value</th><th>varPCg1</th>
  </tr>
  <tr>
-  <td id=delta>',out$delta,'</td><td id=p.val>',out$p.val,'</td><td id=varPCg1>',out$varPCg1,'</td>
+  <td id="#">',out$delta,'</td><td id=p.val>',out$p.val,'</td><td id=varPCg1>',out$varPCg1,'</td>
  </tr>
 </table>
+<center><img src="gDist_',row,'.png"/></center><br>
+<center><title>Guided 1v2</title><br><img src="guided_1v2_',row,'.png"/></center><br>
+<center><title>Unguided 1v2</title><br><img src="unguided_1v2_',row,'.png"/></center><br>
 
-<center><img src="gDist.png"/></center><br>
-<center><title>Guided 1v2</title><br><img src="guided_1v2.png"/></center><br>
-<center><title>Unguided 1v2</title><br><img src="unguided_1v2.png"/></center><br>
+<center><title>Guided Compare to ',opt$npcs,'</title><br><img src="guided_npcs_',row,'.png"/></center><br>
+<center><title>Unguided Compare to ',opt$npcs,'</title><br><img src="unguided_npcs_',row,'.png"/></center><br>
 
-<center><title>Guided Compare to ',opt$npcs,'</title><br><img src="guided_npcs.png"/></center><br>
-<center><title>Unguided Compare to ',opt$npcs,'</title><br><img src="unguided_npcs.png"/></center><br>
+<center><title>Guided Cumulative Variance</title><br><img src="guided_var_',row,'.png"/></center><br>
+<center><title>Unguided Cumulative Variance</title><br><img src="unguided_var_',row,'.png"/></center><br>
 
-<center><title>Guided Cumulative Variance</title><br><img src="guided_var.png"/></center><br>
-<center><title>Unguided Cumulative Variance</title><br><img src="unguided_var.png"/></center><br>
-
-</html>'
-),file = opt$html_outfile)
+'), file = opt$html_outfile, append=TRUE)
+}
+write(paste('<p><center><a href="#top">Back to Top</a></center></p></body></html>'),file = opt$html_outfile, append=TRUE)
 
 sessionInfo()