view xcms_differential_analysis.r @ 62:9bd2597c8851 default tip

r
author pieter.lukasse@wur.nl
date Fri, 06 Feb 2015 15:49:26 +0100
parents 543958f75e83
children
line wrap: on
line source

## read args:
args <- commandArgs(TRUE)
#cat("args <- \"\"\n")
## a xcms xset saved as .RData
args.xsetData <- args[1]
#cat(paste("args.xsetData <- \"", args[1], "\"\n", sep=""))

args.class1 <- args[2]
args.class2 <- args[3]
#cat(paste("args.class1 <- \"", args[2], "\"\n", sep=""))
#cat(paste("args.class2 <- \"", args[3], "\"\n", sep=""))

args.topcount <- strtoi(args[4]) 
#cat(paste("args.topcount <- ", args[4], "\n", sep=""))

args.outTable <- args[5]

## report files
args.htmlReportFile <- args[6]
args.htmlReportFile.files_path <- args[7]
#cat(paste("args.htmlReportFile <- \"", args[6], "\"\n", sep=""))
#cat(paste("args.htmlReportFile.files_path <- \"", args[7], "\"\n", sep=""))


if (length(args) == 8)
{
	args.outLogFile <- args[8]
	# suppress messages:
	# Send all STDERR to STDOUT using sink() see http://mazamascience.com/WorkingWithData/?p=888
	msg <- file(args.outLogFile, open="wt")
	sink(msg, type="message") 
	sink(msg, type="output")
}

tryCatch(
        {
        	library(metaMS)
        	library(xcms)
	        #library("R2HTML")
	
			# load the xset data :
			xsetData <- readRDS(args.xsetData)
			# if here to support both scenarios:
			if ("xcmsSet" %in% slotNames(xsetData) )
			{
				xsetData <- xsetData@xcmsSet
			}
			
			
			# info: levels(xcmsSet@phenoData$class) also gives access to the class names
			dir.create(file.path(args.htmlReportFile.files_path), showWarnings = FALSE, recursive = TRUE)
			# set cairo as default for png plots:
			png = function (...) grDevices::png(...,type='cairo')
			# run diffreport
			reporttab <- diffreport(xsetData, args.class1, args.class2, paste(args.htmlReportFile.files_path,"/fig", sep=""), args.topcount, metlin = 0.15, h=480, w=640)
			
			# write out tsv table:
			write.table(reporttab, args.outTable, sep="\t", row.names=FALSE)
			
			message("\nGenerating report.........")
			
			cat("<html><body><h1>Differential analysis report</h1>", file= args.htmlReportFile)
			#HTML(reporttab[1:args.topcount,], file= args.htmlReportFile)
			figuresPath <- paste(args.htmlReportFile.files_path, "/fig_eic", sep="")
			message(figuresPath)
			listOfFiles <- list.files(path = figuresPath)
			for (i in 1:length(listOfFiles))  
			{
				figureName <- listOfFiles[i]
				# maybe we still need to copy the figures to the args.htmlReportFile.files_path
				cat(paste("<img src='fig_eic/", figureName,"' />", sep=""), file= args.htmlReportFile, append=TRUE)
				cat(paste("<img src='fig_box/", figureName,"' />", sep=""), file= args.htmlReportFile, append=TRUE)
			}
			
			message("finished generating report")
			cat("\nWarnings================:\n")
			str( warnings() ) 
		},
        error=function(cond) {
            sink(NULL, type="message") # default setting
			sink(stderr(), type="output")
            message("\nERROR: ===========\n")
            print(cond)
        }
    )