annotate SMART/Java/Python/ncList/Benchmark.py @ 68:85e80c21b1f7 draft

Uploaded
author m-zytnicki
date Mon, 16 Nov 2015 12:00:32 -0500
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 import os, os.path, random, glob, subprocess, threading, time, resource
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
2 from optparse import OptionParser
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
3 from SMART.Java.Python.misc.Progress import *
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
4 from SMART.Java.Python.getRandomRegions import RandomRegionsGenerator
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
5 from commons.core.writer.TranscriptWriter import TranscriptWriter
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
6 from SMART.Java.Python.structure.Transcript import Transcript
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
7 from commons.core.parsing.GffParser import GffParser
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
8
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
9 #TYPES = ("bin", "has", "seg", "fj", "nc", "new")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
10 TYPES = ("new", )
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
11
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
12 class RunCmd(threading.Thread):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
13 def __init__(self, cmd, out, err, time, memory):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
14 threading.Thread.__init__(self)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
15 self._cmd = cmd
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
16 self._out = out
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
17 self._err = err
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
18 self._time = time
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
19 self._memory = memory
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
20 self._id = os.getpid()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
21 self._mem = 0.0
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
22 self._outputFileName = "tmp_%d.out" % (self._id)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
23
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
24 def run(self):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
25 self._p = subprocess.Popen(self._cmd, stdout = self._out, stderr = self._err, shell = True)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
26 #self._p.wait()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
27
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
28 def _runShellCommand(self, command):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
29 p = subprocess.call("%s > %s" % (command, self._outputFileName), shell=True)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
30 handle = open(self._outputFileName)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
31 data = [line.split() for line in handle.readlines()[1:] if line]
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
32 handle.close()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
33 os.remove(self._outputFileName)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
34 return data
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
35
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
36 def _getPid(self):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
37 self._pid = None
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
38 cpt = 1
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
39 while True:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
40 commandsFound = []
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
41 for line in self._runShellCommand("ps -o pid,cmd"):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
42 if line[1:] == self._cmd.split(" "):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
43 self._pid = int(line[0])
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
44 commandsFound.append(" ".join(line[1:]))
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
45 if self._pid != None:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
46 return True
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
47 time.sleep(1)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
48 if cpt % 100 == 0:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
49 print "pid of '%s' not found after %d seconds. Found: %s" % (self._cmd, cpt, " --- ".join(commandsFound))
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
50 cpt += 1
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
51 if cpt > 300:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
52 return False
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
53
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
54 def _fetchMemory(self):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
55 lines = self._runShellCommand("ps u -p %d" % (self._pid))
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
56 for line in lines:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
57 self._mem = max(self._mem, float(line[3]))
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
58 return self._mem >= self._memory
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
59 #print "Cannot find the memory of the current PID (%d) in: %s" % (self._pid, " --- ".join([" ".join(line) for line in lines]))
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
60 return False
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
61
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
62 def getMemory(self):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
63 return self._mem
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
64
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
65 def _abort(self):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
66 try:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
67 self._p.terminate()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
68 except Exception:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
69 pass
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
70 self._killSubThreads()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
71
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
72 def _killSubThreads(self):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
73 for line in self._runShellCommand("ps --ppid %d -o pid" % (self._pid)):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
74 self._runShellCommand("kill %s" % (line[0]))
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
75 self._runShellCommand("kill %s" % (self._pid))
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
76
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
77 def go(self):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
78 startTime = time.time()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
79 self.run()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
80 #self.start()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
81 while not self._getPid():
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
82 #self.start()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
83 self.run()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
84 while True:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
85 if self._time != None and time.time() - startTime > self._time:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
86 print "\nCommand '%s' did not finish in time. Aborting it." % (self._cmd)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
87 self._abort()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
88 break
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
89 if self._memory != None and self._fetchMemory():
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
90 print "\nCommand '%s' required too much memory (%f). Aborting it." % (self._cmd, self._mem)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
91 self._abort()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
92 break
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
93 #self.join(0.1)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
94 time.sleep(0.1)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
95 #if not self.isAlive():
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
96 if self._p.poll() != None:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
97 return True
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
98 return False
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
99
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
100
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
101 class DataStructure(object):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
102 def __init__(self):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
103 self._structure = {}
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
104
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
105 def addData(self, data):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
106 if data._nbRefs not in self._structure:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
107 self._structure[data._nbRefs] = {}
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
108 if data._nbQueries not in self._structure[data._nbRefs]:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
109 self._structure[data._nbRefs][data._nbQueries] = {}
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
110 if data._genomeSize not in self._structure[data._nbRefs][data._nbQueries]:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
111 self._structure[data._nbRefs][data._nbQueries][data._genomeSize] = {}
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
112 if data._type not in self._structure[data._nbRefs][data._nbQueries][data._genomeSize]:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
113 self._structure[data._nbRefs][data._nbQueries][data._genomeSize][data._type] = []
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
114 self._structure[data._nbRefs][data._nbQueries][data._genomeSize][data._type].append(data._group)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
115
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
116 def export(self):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
117 outputString = "#refs\t#queries\tgenome size\ttype\t# written\t# overlaps\tbuild t.\trun t.\tmem\n"
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
118 for nbRefs in sorted(self._structure.keys()):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
119 for nbQueries in sorted(self._structure[nbRefs].keys()):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
120 for genomeSize in sorted(self._structure[nbRefs][nbQueries].keys()):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
121 for type in TYPES:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
122 if type not in self._structure[nbRefs][nbQueries][genomeSize]:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
123 outputString += "NA\tNA\tNA\t%s\tNA\tNA\tNA\tNA\tNA\tNA\n" % (type)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
124 else:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
125 for group in self._structure[nbRefs][nbQueries][genomeSize][type]:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
126 outputString += "%d\t%d\t%d\t%s\t%d\t%d\t%f\t%f\t%f\n" % (nbRefs, nbQueries, genomeSize, type, group._nbOutputs, group._nbOverlaps, group._buildTime, group._runTime, group._mem)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
127 return outputString
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
128
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
129
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
130 class Data(object):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
131 def __init__(self, type, buildTime, runTime, mem, nbRefs, nbQueries, nbOutputs, nbOverlaps, genomeSize):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
132 self._type = type
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
133 self._nbRefs = nbRefs
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
134 self._nbQueries = nbQueries
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
135 self._genomeSize = genomeSize
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
136 self._group = Group(nbOutputs, nbOverlaps, buildTime, runTime, mem)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
137
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
138 def checkConsistency(self, data):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
139 return self._group.checkConsistency(data._group)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
140
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
141
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
142 class Group(object):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
143 def __init__(self, nbOutputs, nbOverlaps, buildTime, runTime, mem):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
144 self._buildTime = buildTime
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
145 self._runTime = runTime
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
146 self._mem = mem
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
147 self._nbOutputs = nbOutputs
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
148 self._nbOverlaps = nbOverlaps
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
149
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
150 def checkConsistency(self, group):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
151 if (self._buildTime == "NA" or group._buildTime == "NA"):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
152 return True
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
153 return (self._nbOutputs == group._nbOutputs and self._nbOverlaps == group._nbOverlaps)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
154
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
155
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
156 class Benchmark(object):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
157
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
158 def __init__(self, verbosity = 1):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
159 self._verbosity = verbosity
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
160 self._checkEnvironmentVariable()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
161 self._toolName = {"bin": os.path.join(os.environ["SMARTPATH"], "ncList", "FindOverlapsWithSeveralIntervalsBin.py"), \
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
162 "has": os.path.join(os.environ["SMARTPATH"], "ncList", "FindOverlapsWithSeveralIntervalsHashBin.py"), \
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
163 "seg": os.path.join(os.environ["SMARTPATH"], "ncList", "FindOverlapsWithSeveralIntervalsBinSegment.py"), \
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
164 "fj": os.path.join(os.environ["SMARTPATH"], "ncList", "FindOverlapsFJoin.py"), \
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
165 "nc": os.path.join(os.environ["SMARTPATH"], "ncList", "FindOverlapsWithSeveralIntervals.py"), \
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
166 "new": os.path.join(os.environ["SMARTPATH"], "FindOverlapsOptim.py")}
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
167 self._structure = DataStructure()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
168 self._pid = os.getpid()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
169 self._count = 0
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
170 self._time = None
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
171 self._memory = None
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
172
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
173 def _checkEnvironmentVariable(self):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
174 if "SMARTPATH" not in os.environ:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
175 raise Exception("'SMARTPATH' is not set. Please set it to '<installation-direction>/S-mart/Java/Python'.")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
176
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
177 def _createTmpFileName(self, name, extension):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
178 self._count += 1
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
179 return "tmp_%d_%d_%s.%s" % (self._pid, self._count, name, extension)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
180
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
181 def _dumpAndReturn(self, fileName, exception):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
182 handle = open(fileName)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
183 print "Error in parsing file '%s':" % (fileName)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
184 for line in handle:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
185 print line.strip()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
186 print "Command is: '%s'" % (self._command)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
187 raise exception
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
188
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
189 def setNbReferences(self, nbReferences):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
190 self._nbReferences = nbReferences
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
191
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
192 def setNbQueries(self, nbQueries):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
193 self._nbQueries = nbQueries
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
194
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
195 def setGenomeSizes(self, nbGenomeSizes):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
196 self._nbGenomeSizes = nbGenomeSizes
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
197
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
198 def setNbReplicates(self, nbReplicates):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
199 self._nbReplicates = nbReplicates
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
200
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
201 def setChromosomeName(self, chromosome):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
202 self._chromosomeName = chromosome
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
203
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
204 def setSizes(self, minSize, maxSize):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
205 self._minSize = minSize
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
206 self._maxSize = maxSize
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
207
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
208 def setOutputFileName(self, fileName):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
209 self._outputFileName = fileName
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
210
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
211 def setLimits(self, time, memory):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
212 self._time = time
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
213 self._memory = memory
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
214
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
215 def _generateIntervals(self, nbElements, genomeSize):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
216 fileName = self._createTmpFileName("intervals", "gff3")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
217 iRR = RandomRegionsGenerator(0)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
218 iRR.setMinSize(self._minSize)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
219 iRR.setMaxSize(self._maxSize)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
220 iRR.setGenomeSize(genomeSize)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
221 iRR.setChromosomeName(self._chromosomeName)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
222 iRR.setStrands(False)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
223 iRR.setNumber(nbElements)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
224 iRR.setOutputFile(fileName)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
225 iRR.run()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
226 return fileName
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
227
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
228 def _startTool(self, type, refFileName, queryFileName, nbReferences, nbQueries, genomeSize):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
229 outputFileName = self._createTmpFileName("output", "gff3")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
230 outFileName = self._createTmpFileName("out", "out")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
231 errFileName = self._createTmpFileName("err", "err")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
232 outHandle = open(outFileName, "w")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
233 errHandle = open(errFileName, "w")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
234 self._command = "python %s -i %s -f gff3 -j %s -g gff3 -o %s -v 3" % (self._toolName[type], queryFileName, refFileName, outputFileName)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
235 thread = RunCmd(self._command, outHandle, errHandle, self._time, self._memory)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
236 over = thread.go()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
237 self._mem = thread.getMemory()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
238 if os.path.exists(outputFileName):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
239 os.remove(outputFileName)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
240 outHandle.close()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
241 errHandle.close()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
242 errData = open(errFileName).readlines()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
243 if errData:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
244 print "Error output: \n%s\n" % ("\n".join(errData))
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
245 if not over:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
246 errHandle = open(errFileName, "r")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
247 error = errHandle.readlines()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
248 errHandle.close()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
249 if error:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
250 for line in error:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
251 print line.strip()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
252 print "Previous process failed"
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
253 os.remove(errFileName)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
254 if not over:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
255 return False
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
256 return outFileName
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
257
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
258 def _parseTrace(self, type, fileName, genomeSize):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
259 handle = open(fileName)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
260 buildTime = 0
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
261 try:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
262 for line in handle:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
263 line = line.strip()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
264 if "time spent" in line:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
265 buildTime += float(line.split()[-1][:-1])
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
266 elif "done" in line:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
267 buildTime += float(line.split("(")[1][:-2])
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
268 elif "# queries" in line:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
269 nbQueries = int(line.split()[-1])
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
270 elif "# refs" in line:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
271 nbRefs = int(line.split()[-1])
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
272 elif "# written" in line:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
273 nbOutputs = int(line.split()[2])
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
274 nbOverlaps = int(line.split()[3][1:])
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
275 elif "time" in line:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
276 runTime = float(line.split()[-1][:-1])
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
277 except Exception, e:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
278 handle.close()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
279 self._dumpAndReturn(fileName, e)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
280 handle.close()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
281 try:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
282 return Data(type, buildTime, runTime, self._mem, nbRefs, nbQueries, nbOutputs, nbOverlaps, genomeSize)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
283 except Exception, e:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
284 handle.close()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
285 self._dumpAndReturn(fileName, e)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
286
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
287 def _cleanTmpFiles(self, really = False):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
288 files = glob.glob("tmp_%d_*.pkl" % (self._pid)) + glob.glob("tmp_%d_*.bin" % (self._pid))
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
289 if really:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
290 files += glob.glob("tmp_%d_*.gff3" % (self._pid)) + glob.glob("tmp_%d_*.out" % (self._pid))
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
291 for fileName in files:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
292 os.remove(fileName)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
293
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
294 def run(self):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
295 progress = Progress(len(self._nbReferences) * len(self._nbQueries) * len(self._nbGenomeSizes) * self._nbReplicates, "Processing", self._verbosity)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
296 for nbReferences in self._nbReferences:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
297 for queriesRatio in self._nbQueries:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
298 nbQueries = int(nbReferences * queriesRatio)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
299 for genomeSizeRatio in self._nbGenomeSizes:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
300 genomeSize = int(nbReferences * genomeSizeRatio)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
301 for replicate in range(self._nbReplicates):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
302 refFileName = self._generateIntervals(nbReferences, genomeSize)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
303 queryFileName = self._generateIntervals(nbQueries, genomeSize)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
304 data = {}
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
305 for type in TYPES:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
306 fileName = self._startTool(type, refFileName, queryFileName, nbReferences, nbQueries, genomeSize)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
307 if not fileName:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
308 data[type] = Data(type, "NA", "NA", "NA", nbReferences, nbQueries, "NA", "NA", genomeSize)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
309 else:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
310 data[type] = self._parseTrace(type, fileName, genomeSize)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
311 self._structure.addData(data[type])
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
312 os.remove(fileName)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
313 self._cleanTmpFiles()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
314 self._cleanTmpFiles(True)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
315 firstType = TYPES[0]
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
316 for type in TYPES[1:]:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
317 if not data[firstType].checkConsistency(data[type]):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
318 raise Exception("Outputs are not consistent.\n # outputs: %d vs %d.\n # overlaps: %d vs %d.\n %s: %f + %f; %s: %f + %f.\n Files are %s and %s." % (data[firstType]._group._nbOutputs, data[type]._group._nbOutputs, data[firstType]._group._nbOverlaps, data[type]._group._nbOverlaps, firstType, data[firstType]._group._buildTime, data[firstType]._group._runTime, data[firstType]._group._mem, type, data[type]._group._buildTime, data[type]._group._runTime, data[type]._group._mem, refFileName, queryFileName))
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
319 for fileName in (queryFileName, refFileName):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
320 if os.path.exists(fileName):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
321 os.remove(fileName)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
322 progress.inc()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
323 progress.done()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
324 handle = open(self._outputFileName, "w")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
325 handle.write(self._structure.export())
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
326 handle.close()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
327
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
328
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
329
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
330 if __name__ == "__main__":
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
331
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
332 description = "Benchmark v1.0.2: Compare NC-List with other tools. Only work under Linux. [Category: Other]"
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
333 parser = OptionParser(description = description)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
334 parser.add_option("-r", "--nbReferences", dest="nbReferences", action="store", default=None, type="string", help="number of references (list of integers separated by commas) [compulsory] [format: string]")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
335 parser.add_option("-q", "--nbQueries", dest="nbQueries", action="store", default=None, type="string", help="number of queries as a factor of the number of references (list of floats separated by commas) [compulsory] [format: string]")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
336 parser.add_option("-R", "--nbReplicates", dest="nbReplicates", action="store", default=None, type="int", help="number of replicates [compulsory] [format: int]")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
337 parser.add_option("-s", "--genomeSizes", dest="genomeSizes", action="store", default=None, type="string", help="genome size as a factor of the number of references (list of floats separated by commas) [compulsory] [format: string]")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
338 parser.add_option("-c", "--chromosome", dest="chromosome", action="store", default="chr1", type="string", help="name of the chromosome [default: chr1] [format: string]")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
339 parser.add_option("-z", "--minSize", dest="minSize", action="store", default=None, type="int", help="minimum size of the reads [compulsory] [format: int]")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
340 parser.add_option("-Z", "--maxSize", dest="maxSize", action="store", default=None, type="int", help="maximum size of the reads [compulsory] [format: int]")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
341 parser.add_option("-o", "--output", dest="outputFileName", action="store", type="string", help="output file [compulsory] [format: output file in TXT format]")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
342 parser.add_option("-t", "--time", dest="time", action="store", default=None, type="int", help="maximum time to wait (in seconds) [default: None] [format: int]")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
343 parser.add_option("-m", "--memory", dest="memory", action="store", default=None, type="float", help="maximum memory usage (in %) [default: None] [format: float]")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
344 parser.add_option("-v", "--verbosity", dest="verbosity", action="store", default=1, type="int", help="trace level [format: int]")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
345 (options, args) = parser.parse_args()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
346
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
347 benchmark = Benchmark(options.verbosity)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
348 benchmark.setNbReferences(map(int, options.nbReferences.split(",")))
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
349 benchmark.setNbQueries(map(float, options.nbQueries.split(",")))
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
350 benchmark.setGenomeSizes(map(float, options.genomeSizes.split(",")))
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
351 benchmark.setNbReplicates(options.nbReplicates)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
352 benchmark.setChromosomeName(options.chromosome)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
353 benchmark.setSizes(options.minSize, options.maxSize)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
354 benchmark.setLimits(options.time, options.memory)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
355 benchmark.setOutputFileName(options.outputFileName)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
356 benchmark.run()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
357