view metaMS_cmd_interface.r @ 41:e67149fbff20

small changes/improvements; new metams and xcms tools
author pieter.lukasse@wur.nl
date Thu, 06 Nov 2014 16:14:44 +0100
parents
children 664ccd5f7cf8
line wrap: on
line source

## read args:
args <- commandArgs(TRUE)
## the constructed DB, e.g. "E:/Rworkspace/metaMS/data/LCDBtest.RData"
args.constructedDB <- args[1]
## data files, e.g. "E:/Rworkspace/metaMS/data/data.zip" (with e.g. .CDF files) and unzip output dir, e.g. "E:/"
args.dataZip <- args[2]
args.zipExtrDir <- paste(args[2],"dir/")
## settings file, e.g. "E:/Rworkspace/metaMS/data/settings.r", should contain assignment to an object named "customMetaMSsettings" 
args.settings <- args[3]

## output file names, e.g. "E:/Rworkspace/metaMS/data/out.txt"
args.outAnnotationTable <- args[4]
args.outLogFile <- args[5]
args.xsetOut <- args[6]

## report files
args.htmlReportFile <- args[7]
args.htmlReportFile.files_path <- args[8]

# 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")

cat("\nSettings used===============:\n")
cat(readChar(args.settings, 1e5))


tryCatch(
        {
	        library(metaMS)
	
			## load the constructed DB :
			tempEnv <- new.env()
			testDB <- load(args.constructedDB, envir=tempEnv)
			
			## load the data files from a zip file
			files <- unzip(args.dataZip, exdir=args.zipExtrDir)
			
			## load settings "script" into "customMetaMSsettings" 
			source(args.settings, local=tempEnv)
			message(paste(" loaded : ", args.settings))
			
			# Just to highlight: if you want to use more than one 
			# trigger runLC: 
			LC <- runLC(files, settings = tempEnv[["customMetaMSsettings"]], DB = tempEnv[[testDB[1]]]$DB, nSlaves=20, returnXset = TRUE)
			
			# write out runLC annotation results:
			write.table(LC$Annotation$annotation.table, args.outAnnotationTable, sep="\t", row.names=FALSE)
			
			# the used constructed DB (write to log):
			cat("\nConstructed DB info===============:\n")
			str(tempEnv[[testDB[1]]]$Info)
			cat("\nConstructed DB table===============:\n") 
			write.table(tempEnv[[testDB[1]]]$DB, args.outLogFile, append=TRUE, row.names=FALSE)
			write.table(tempEnv[[testDB[1]]]$Reftable, args.outLogFile, sep="\t", append=TRUE, row.names=FALSE)
			# save xset as rdata:
			xsetData <- LC$xset@xcmsSet
			saveRDS(xsetData, file=args.xsetOut)
			
			message("\nGenerating report.........")
			# report
			dir.create(file.path(args.htmlReportFile.files_path), showWarnings = FALSE)
			setwd(file.path(args.htmlReportFile.files_path))
			html <- "<html><body><h1>Extracted Ion Chromatograms of groups with more than 3 peaks</h1>" 
			
			LC$xset@xcmsSet
			gt <- groups(LC$xset@xcmsSet)
			colnames(gt)
			groupidx1 <- which(gt[,"rtmed"] > 0 & gt[,"rtmed"] < 3000 & gt[,"npeaks"] > 3)
			if (length(groupidx1) > 0)
			{
				eiccor <- getEIC(LC$xset@xcmsSet, groupidx = c(groupidx1))
				eicraw <- getEIC(LC$xset@xcmsSet, groupidx = c(groupidx1), rt = "raw")
				for (i in 1:length(groupidx1)) 
				{
					figureName <- paste(args.htmlReportFile.files_path, "/figure", i,".png", sep="")
					html <- paste(html,"<img src='", "figure", i,".png' />", sep="") 
					png( figureName ) 
					plot(eiccor, LC$xset@xcmsSet, groupidx = i)
					devname = dev.off()
				}
			}
			
			
			html <- paste(html,"</body><html>")
			message("finished generating report")
			write(html,file=args.htmlReportFile)
			# unlink(args.htmlReportFile)
			cat("\nWarnings================:\n")
			str( warnings() ) 
		},
        error=function(cond) {
            sink(NULL, type="message") # default setting
			sink(stderr(), type="output")
            message("\nERROR: ===========\n")
            print(cond)
        }
    )