annotate SMART/DiffExpAnal/countNumber_parallel_unSQL.py @ 31:0ab839023fe4

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