| 18 | 1 #! /usr/bin/env python | 
|  | 2 | 
|  | 3 | 
|  | 4 import optparse, os, sys, tarfile, random | 
|  | 5 from optparse import OptionParser | 
|  | 6 from commons.core.launcher.Launcher import Launcher | 
|  | 7 from commons.core.sql.TableJobAdaptatorFactory import TableJobAdaptatorFactory | 
|  | 8 | 
|  | 9 def stop_err(msg): | 
|  | 10 	sys.stderr.write('%s\n' % msg) | 
|  | 11 	sys.exit() | 
|  | 12 | 
|  | 13 def toTar(tarFileName, outCountNames): | 
|  | 14 	dir = os.path.dirname(tarFileName) | 
|  | 15 	tfile = tarfile.open(tarFileName + ".tmp.tar", "w") | 
|  | 16 	currentPath = os.getcwd() | 
|  | 17 	os.chdir(dir) | 
|  | 18 	for file in outCountNames: | 
|  | 19 		relativeFileName = os.path.basename(file) | 
|  | 20 		tfile.add(relativeFileName) | 
|  | 21 	os.system("mv %s %s" % (tarFileName + ".tmp.tar", tarFileName)) | 
|  | 22 	tfile.close() | 
|  | 23 	os.chdir(currentPath) | 
|  | 24 | 
|  | 25 def _map(iLauncher, cmd, cmdStart, cmdFinish ): | 
|  | 26 	lCmds = [] | 
|  | 27 	lCmds.append(cmd) | 
|  | 28 	lCmdStart = [] | 
|  | 29 	lCmdStart.append(cmdStart) | 
|  | 30 	lCmdFinish = [] | 
|  | 31 	lCmdFinish.append(cmdFinish) | 
|  | 32 	return(iLauncher.prepareCommands_withoutIndentation(lCmds, lCmdStart, lCmdFinish)) | 
|  | 33 | 
|  | 34 def _createCountNumberCommand(iLauncher, inputFile, outputFile): | 
|  | 35 		lArgs = [] | 
|  | 36 		lArgs.append("%s" % inputFile) | 
|  | 37 		lArgs.append("%s" %  outputFile) | 
|  | 38 		return iLauncher.getSystemCommand("perl %s/SMART/DiffExpAnal/countNumber.pl " %  os.environ["REPET_PATH"], lArgs) | 
|  | 39 | 
|  | 40 def __main__(): | 
|  | 41 	#Parse Command Line | 
|  | 42 	parser = optparse.OptionParser() | 
|  | 43 	parser.add_option("-i", "--input", dest="inputFile", help="input txt file, a list of overlapping results files.") | 
|  | 44 	parser.add_option("-o", "--output", dest="outputFile", help="Out txt file.") | 
|  | 45 	parser.add_option("-t", "--tar", dest="outputTar", default=None, help="output all count results in a tar file.") | 
|  | 46 	(options, args) = parser.parse_args() | 
|  | 47 | 
|  | 48 	#Parse the input txt file and read a list of transcripts files. | 
|  | 49 	file = open(options.inputFile, "r") | 
|  | 50 	lines = file.readlines() | 
|  | 51 	inputFileNames = [] | 
|  | 52 	outCountNames = [] | 
|  | 53 	outputName = options.outputFile | 
|  | 54 	resDirName = os.path.dirname(outputName) + '/' | 
|  | 55 | 
|  | 56 	#Write output txt file and define all output count file names | 
|  | 57 	out = open(outputName, "w") | 
|  | 58 	out.write("label\tfiles\tgroup\n") | 
|  | 59 	for line in lines: | 
|  | 60 		tab = line.split() | 
|  | 61 		inputFileNames.append(tab[1]) | 
|  | 62 		outCountName = resDirName + tab[0] + "_outCount_%s.csv" % random.randrange(0, 10000) | 
|  | 63 		outCountNames.append(outCountName) | 
|  | 64 		out.write(tab[0] + '\t' + outCountName + '\t' + tab[0][5] + '\n') | 
|  | 65 	file.close() | 
|  | 66 	out.close() | 
|  | 67 | 
|  | 68 	#Launch on nodes | 
|  | 69 	acronym = "countNumber" | 
|  | 70 	jobdb = TableJobAdaptatorFactory.createJobInstance() | 
|  | 71 	iLauncher = Launcher(jobdb, os.getcwd(), "", "", os.getcwd(), os.getcwd(), "jobs", "", acronym, acronym, False, True) | 
|  | 72 	lCmdsTuples = [] | 
|  | 73 	for i in range(len(inputFileNames)):	#Construct the lines commands | 
|  | 74 		inputFile = inputFileNames[i] | 
|  | 75 		outputFile = outCountNames[i] | 
|  | 76 		cmd2Launch = _createCountNumberCommand(iLauncher, inputFile, outputFile) | 
|  | 77 		cmdStart = "" | 
|  | 78 		cmdFinish = "" | 
|  | 79 		lCmdsTuples.append(_map(iLauncher, cmd2Launch, cmdStart, cmdFinish)) | 
|  | 80 | 
|  | 81 | 
|  | 82 | 
|  | 83 	iLauncher.runLauncherForMultipleJobs(acronym, lCmdsTuples, True) | 
|  | 84 | 
|  | 85 | 
|  | 86 | 
|  | 87 	if options.outputTar != None: | 
|  | 88 		toTar(options.outputTar, outCountNames) | 
|  | 89 | 
|  | 90 | 
|  | 91 if __name__=="__main__":__main__() |