| 18 | 1 #! /usr/bin/env python | 
|  | 2 | 
|  | 3 | 
|  | 4 import optparse, os, sys, subprocess, tempfile, shutil, tarfile, random | 
|  | 5 from optparse import OptionParser | 
|  | 6 | 
|  | 7 def stop_err(msg): | 
|  | 8 	sys.stderr.write('%s\n' % msg) | 
|  | 9 	sys.exit() | 
|  | 10 | 
|  | 11 def toTar(tarFileName, outCountNames): | 
|  | 12 	dir = os.path.dirname(tarFileName) | 
|  | 13 	tfile = tarfile.open(tarFileName + ".tmp.tar", "w") | 
|  | 14 	currentPath = os.getcwd() | 
|  | 15 	os.chdir(dir) | 
|  | 16 	for file in outCountNames: | 
|  | 17 		relativeFileName = os.path.basename(file) | 
|  | 18 		tfile.add(relativeFileName) | 
|  | 19 	os.system("mv %s %s" % (tarFileName + ".tmp.tar", tarFileName)) | 
|  | 20 	tfile.close() | 
|  | 21 	os.chdir(currentPath) | 
|  | 22 | 
|  | 23 | 
|  | 24 def __main__(): | 
|  | 25 	#Parse Command Line | 
|  | 26 	parser = optparse.OptionParser() | 
|  | 27 	parser.add_option("-i", "--input", dest="inputFile", help="input txt file, a list of overlapping results files.") | 
|  | 28 	parser.add_option("-o", "--output", dest="outputFile", help="Out txt file.") | 
|  | 29 	parser.add_option("-t", "--tar", dest="outputTar", default=None, help="output all count results in a tar file.") | 
|  | 30 	(options, args) = parser.parse_args() | 
|  | 31 | 
|  | 32 	#Parse the input txt file and read a list of transcripts files. | 
|  | 33 	file = open(options.inputFile, "r") | 
|  | 34 	lines = file.readlines() | 
|  | 35 	inputFileNames = [] | 
|  | 36 	outCountNames = [] | 
|  | 37 	outputName = options.outputFile | 
|  | 38 	resDirName = os.path.dirname(outputName) + '/' | 
|  | 39 | 
|  | 40 	#Write output txt file and define all output count file names | 
|  | 41 	out = open(outputName, "w") | 
|  | 42 	out.write("label\tfiles\tgroup\n") | 
|  | 43 	for line in lines: | 
|  | 44 		tab = line.split() | 
|  | 45 		inputFileNames.append(tab[1]) | 
|  | 46 		outCountName = resDirName + tab[0] + "_outCount_%s.csv" % random.randrange(0, 10000) | 
|  | 47 		outCountNames.append(outCountName) | 
|  | 48 		out.write(tab[0] + '\t' + outCountName + '\t' + tab[0][5] + '\n') | 
|  | 49 	file.close() | 
|  | 50 	out.close() | 
|  | 51 | 
|  | 52 	#Construct the lines commands | 
|  | 53 	cmds = [] | 
|  | 54 	for i in range(len(inputFileNames)): | 
|  | 55 		cmd = "perl %s/SMART/DiffExpAnal/countNumber.pl " %  os.environ["REPET_PATH"] | 
|  | 56 		opts = "%s %s " % (inputFileNames[i], outCountNames[i]) | 
|  | 57 		cmd += opts | 
|  | 58 		cmds.append(cmd) | 
|  | 59 | 
|  | 60 	tmp_files = [] | 
|  | 61 	for i in range(len(cmds)): | 
|  | 62 		try: | 
|  | 63 			tmp_out = tempfile.NamedTemporaryFile().name | 
|  | 64 			tmp_files.append(tmp_out) | 
|  | 65 			tmp_stdout = open(tmp_out, 'wb') | 
|  | 66 			tmp_err = tempfile.NamedTemporaryFile().name | 
|  | 67 			tmp_files.append(tmp_err) | 
|  | 68 			tmp_stderr = open(tmp_err, 'wb') | 
|  | 69 			proc = subprocess.Popen(args=cmds[i], shell=True, cwd=".", stdout=tmp_stdout, stderr=tmp_stderr) | 
|  | 70 			returncode = proc.wait() | 
|  | 71 			tmp_stderr.close() | 
|  | 72 			#get stderr, allowing for case where it's very large | 
|  | 73 			tmp_stderr = open(tmp_err, 'rb') | 
|  | 74 			stderr = '' | 
|  | 75 			buffsize = 1048576 | 
|  | 76 			try: | 
|  | 77 				while True: | 
|  | 78 					stderr += tmp_stderr.read(buffsize) | 
|  | 79 					if not stderr or len(stderr) % buffsize != 0: | 
|  | 80 						break | 
|  | 81 			except OverflowError: | 
|  | 82 				pass | 
|  | 83 			tmp_stdout.close() | 
|  | 84 			tmp_stderr.close() | 
|  | 85 			if returncode != 0: | 
|  | 86 				raise Exception, stderr | 
|  | 87 		except Exception, e: | 
|  | 88 			stop_err('Error in :\n' + str(e)) | 
|  | 89 | 
|  | 90 	if options.outputTar != None: | 
|  | 91 		toTar(options.outputTar, outCountNames) | 
|  | 92 | 
|  | 93 	for tmp_file in tmp_files: | 
|  | 94 		os.remove(tmp_file) | 
|  | 95 | 
|  | 96 if __name__=="__main__":__main__() |