annotate commons/tools/LaunchMatcherInParallel.py @ 18:94ab73e8a190

Uploaded
author m-zytnicki
date Mon, 29 Apr 2013 03:20:15 -0400
parents
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 # Copyright INRA (Institut National de la Recherche Agronomique)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
4 # http://www.inra.fr
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
5 # http://urgi.versailles.inra.fr
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
6 #
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
7 # This software is governed by the CeCILL license under French law and
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
8 # abiding by the rules of distribution of free software. You can use,
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
9 # modify and/ or redistribute the software under the terms of the CeCILL
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
10 # license as circulated by CEA, CNRS and INRIA at the following URL
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
11 # "http://www.cecill.info".
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
12 #
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
13 # As a counterpart to the access to the source code and rights to copy,
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
14 # modify and redistribute granted by the license, users are provided only
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
15 # with a limited warranty and the software's author, the holder of the
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
16 # economic rights, and the successive licensors have only limited
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
17 # liability.
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
18 #
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
19 # In this respect, the user's attention is drawn to the risks associated
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
20 # with loading, using, modifying and/or developing or reproducing the
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
21 # software by the user in light of its specific status of free software,
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
22 # that may mean that it is complicated to manipulate, and that also
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
23 # therefore means that it is reserved for developers and experienced
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
24 # professionals having in-depth computer knowledge. Users are therefore
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
25 # encouraged to load and test the software's suitability as regards their
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
26 # requirements in conditions enabling the security of their systems and/or
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
27 # data to be ensured and, more generally, to use and operate it in the
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
28 # same conditions as regards security.
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
29 #
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
30 # The fact that you are presently reading this means that you have had
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
31 # knowledge of the CeCILL license and that you accept its terms.
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
32
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
33 from commons.core.LoggerFactory import LoggerFactory
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
34 from commons.core.sql.DbFactory import DbFactory
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
35 from commons.core.sql.TableJobAdaptatorFactory import TableJobAdaptatorFactory
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
36 from commons.core.launcher.Launcher import Launcher
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
37 from commons.core.launcher.LauncherUtils import LauncherUtils
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
38 from commons.core.utils.FileUtils import FileUtils
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
39 from commons.core.utils.RepetOptionParser import RepetOptionParser
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
40 from commons.core.checker.ConfigChecker import ConfigRules, ConfigChecker
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
41 from commons.core.coord.AlignUtils import AlignUtils
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
42 import shutil
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
43 import os
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
44
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
45 LOG_DEPTH = "repet.tools"
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
46
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
47
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
48 class LaunchMatcherInParallel(object):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
49 def __init__(self, align="", queryFileName="", subjectFileName="", evalue="1e-10", doJoin=False, keepConflict=False, prefix="", alignPattern = ".*\.align", \
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
50 config = "", groupId = "", maxFileSize = 1000000, mergeResults=True, workingDir="tmpMatcher", doClean = False, verbosity = 0):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
51 self._alignFileName = align
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
52 self._queryFileName = queryFileName
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
53 self.setSubjectFileName(subjectFileName)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
54 self.setOutPrefix(prefix)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
55 self._alignPattern = alignPattern
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
56 self._doJoin = doJoin
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
57 self._eValue = evalue
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
58 self._keepConflict = keepConflict
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
59 self._configFileName = config
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
60 self.setGroupId(groupId)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
61 self._maxFileSize = maxFileSize
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
62 self._mergeResults = mergeResults
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
63 self._doClean = doClean
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
64 self._workingDir = workingDir
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
65 self._verbosity = verbosity
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
66 self._log = LoggerFactory.createLogger("%s.%s" % (LOG_DEPTH, self.__class__.__name__), self._verbosity)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
67 self._jobSectionName = "jobs"
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
68
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
69 def setAttributesFromCmdLine(self):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
70 description = "Launch Matcher in parallel."
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
71 epilog = "\nExample 1: launch without verbosity and keep temporary files.\n"
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
72 epilog += "\t$ python LaunchMatcherInParallel.py -a in.align -v 0"
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
73 epilog += "\n\t"
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
74 epilog += "\nExample 2: launch with verbosity to have errors (level 1) and basic information (level 2), and delete temporary files.\n"
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
75 epilog += "\t$ python LaunchMatcherInParallel.py -a in.align -q query.fa -s subject.fa -o query -c -v 2"
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
76 parser = RepetOptionParser(description = description, epilog = epilog)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
77 parser.add_option("-a", "--align", dest = "align", action = "store", type = "string", help = "input align file name [compulsory] [format: align]", default = "")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
78 parser.add_option("-q", "--query", dest = "query", action = "store", type = "string", help = "query fasta file name [optional] [format: fasta]", default = "")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
79 parser.add_option("-s", "--subject", dest = "subject", action = "store", type = "string", help = "subject fasta file name [optional] [format: fasta]", default = "")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
80 parser.add_option("-e", "--evalue", dest = "evalue", action = "store", type = "string", help = "E-value filter [default: 1e10]", default = "1e-10")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
81 parser.add_option("-j", "--join", dest = "doJoin", action = "store_true", help = "join matches [default: False]", default = False)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
82 parser.add_option("-k", "--keepConflict",dest = "keepConflict", action = "store_true", help = "keep conflicting subjects [default: False]", default = False)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
83 parser.add_option("-o", "--outPrefix", dest = "outPrefix", action = "store", type = "string", help = "output file prefix [default: align file name]", default = "")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
84 parser.add_option("-p", "--alignPattern",dest = "alignPattern", action = "store", type = "string", help = "align file pattern [default: .*\.align]", default = ".*\.align")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
85 parser.add_option("-n", "--maxFileSize",dest = "maxFileSize", action = "store", type = "int", help = "max file size (1 file for 1 job) [default: 100000]", default = 10000)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
86 parser.add_option("-m", "--notMergeResults",dest = "notMergeResults", action = "store_false", help = "don't merge results files [default: True]", default = True)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
87 parser.add_option("-w", "--workingDir",dest = "workingDir", action = "store", type = "string", help = "working directory [default: tmpMatcher]", default = "tmpMatcher")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
88 parser.add_option("-c", "--clean", dest = "doClean", action = "store_true", help = "clean temporary files [default: False]", default = False)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
89 parser.add_option("-v", "--verbosity", dest = "verbosity", action = "store", type = "int", help = "verbosity [default: 1]", default = 1)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
90 options = parser.parse_args()[0]
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
91 self._setAttributesFromOptions(options)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
92
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
93 def _setAttributesFromOptions(self, options):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
94 self.setAlignFileName(options.align)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
95 self.setQueryFileName(options.query)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
96 self.setSubjectFileName(options.subject)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
97 self.setEvalue(options.evalue)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
98 self.setDoJoin(options.doJoin)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
99 self.setKeepConflicts(options.keepConflict)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
100 self.setOutPrefix(options.outPrefix)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
101 self.setAlignPattern(options.alignPattern)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
102 self.setMaxFileSize(options.maxFileSize)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
103 self.setMergeResults(options.notMergeResults)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
104 self.setWorkingDir(options.workingDir)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
105 self.setDoClean(options.doClean)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
106 self.setVerbosity(options.verbosity)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
107
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
108 def setAlignFileName(self, alignFileName):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
109 self._alignFileName = alignFileName
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
110
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
111 def setQueryFileName(self, queryFileName):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
112 self._queryFileName = queryFileName
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
113
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
114 def setSubjectFileName(self, subjectFileName):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
115 self._subjectFileName = subjectFileName
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
116
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
117 def setEvalue(self, evalue):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
118 self._eValue = evalue
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
119
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
120 def setDoJoin(self, doJoin):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
121 self._doJoin = doJoin
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
122
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
123 def setKeepConflicts(self, keepConflict):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
124 self._keepConflict = keepConflict
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
125
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
126 def setOutPrefix(self, outPrefix):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
127 if outPrefix == "":
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
128 self._outPrefix = self._alignFileName
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
129 else:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
130 self._outPrefix = outPrefix
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
131
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
132 def setAlignPattern(self, alignPattern):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
133 self._alignPattern = alignPattern
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
134
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
135 def setGroupId(self, groupId):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
136 if groupId == "":
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
137 self._groupId = "Matcher_%s" % os.getpid()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
138 else:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
139 self._groupId = groupId
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
140
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
141 def setMaxFileSize(self, maxFileSize):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
142 self._maxFileSize = maxFileSize
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
143
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
144 def setMergeResults(self, mergeResults):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
145 self._mergeResults = mergeResults
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
146
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
147 def setWorkingDir(self, workingDir):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
148 self._workingDir = workingDir
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
149
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
150 def setDoClean(self, doClean):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
151 self._doClean = doClean
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
152
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
153 def setVerbosity(self, verbosity):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
154 self._verbosity = verbosity
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
155
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
156 def _checkOptions(self):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
157 if self._alignFileName == "":
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
158 self._logAndRaise("ERROR: Missing input align file name")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
159
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
160 def _logAndRaise(self, errorMsg):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
161 self._log.error(errorMsg)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
162 raise Exception(errorMsg)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
163
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
164 def _checkConfig(self):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
165 iConfigRules = ConfigRules()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
166 iConfigRules.addRuleSection(section=self._jobSectionName, mandatory=True)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
167 iConfigRules.addRuleOption(section=self._jobSectionName, option ="resources", mandatory=True, type="string")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
168 iConfigRules.addRuleOption(section=self._jobSectionName, option ="tmpDir", mandatory=True, type="string")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
169 iConfigRules.addRuleOption(section=self._jobSectionName, option ="copy", mandatory=True, type="bool")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
170 iConfigRules.addRuleOption(section=self._jobSectionName, option ="clean", mandatory=True, type="bool")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
171
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
172 iConfigChecker = ConfigChecker(self._configFileName, iConfigRules)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
173 self._iConfig = iConfigChecker.getConfig()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
174 self._setAttributesFromConfig()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
175
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
176 def _setAttributesFromConfig(self):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
177 self._resources = self._iConfig.get(self._jobSectionName, "resources")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
178 self._tmpDir = self._iConfig.get(self._jobSectionName, "tmpDir")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
179 self._isCopyOnNode = self._iConfig.get(self._jobSectionName, "copy")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
180 self._doClean = self._iConfig.get(self._jobSectionName, "clean")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
181
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
182 if self._isCopyOnNode and not self._tmpDir:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
183 self._isCopyOnNode = False
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
184 self._log.debug("The copy option is: %s." % self._isCopyOnNode)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
185
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
186 def _getLaunchMatcherCmd(self, iLauncher, file):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
187 lArgs = []
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
188 lArgs.append("-a %s" % file)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
189 if self._queryFileName:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
190 lArgs.append("-q %s" % self._queryFileName)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
191 if self._subjectFileName:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
192 lArgs.append("-s %s" % self._subjectFileName)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
193 lArgs.append("-e %s" % self._eValue)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
194 lArgs.append("-o %s" % file)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
195 if self._doJoin:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
196 lArgs.append("-j")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
197 if self._keepConflict:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
198 lArgs.append("-k")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
199 lArgs.append("-v %i" % (self._verbosity - 1))
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
200 return iLauncher.getSystemCommand("LaunchMatcher.py", lArgs)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
201
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
202 def _splitAlignFilePerSeq(self):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
203 lAlign = AlignUtils.getAlignListFromFile(self._alignFileName)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
204 lAlignList = AlignUtils.splitAlignListByQueryName(lAlign)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
205 inputFileNameWithoutExtension = os.path.splitext(os.path.basename(self._alignFileName))[0]
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
206 AlignUtils.createAlignFiles(lAlignList, inputFileNameWithoutExtension, self._workingDir)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
207
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
208 def _writeTabHeader(self, outTabFileName):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
209 with open(outTabFileName, 'w') as f:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
210 f.write("query.name\tquery.start\tquery.end\tquery.length\tquery.length.%\tmatch.length.%\tsubject.name\tsubject.start\tsubject.end\tsubject.length\tsubject.length.%\tE.value\tScore\tIdentity\tpath\n")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
211
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
212 def run(self):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
213 LoggerFactory.setLevel(self._log, self._verbosity)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
214 self._checkConfig()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
215 self._checkOptions()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
216
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
217 self._log.info("START LaunchMatcherInParallel")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
218 self._log.debug("Align file name: %s" % self._alignFileName)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
219 self._log.debug("Query file name: %s" % self._queryFileName)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
220 self._log.debug("Subject file name: %s" % self._subjectFileName)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
221 if not os.path.exists(self._workingDir):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
222 os.makedirs(self._workingDir)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
223 else:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
224 self._doClean = False
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
225 self._splitAlignFilePerSeq()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
226 os.chdir(self._workingDir)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
227 os.symlink("../%s" % self._queryFileName, self._queryFileName)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
228 if self._queryFileName != self._subjectFileName:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
229 os.symlink("../%s" % self._subjectFileName, self._subjectFileName)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
230
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
231 cDir = os.getcwd()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
232 if not self._tmpDir:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
233 self._tmpDir = cDir
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
234
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
235 acronym = "Matcher"
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
236 iDb = DbFactory.createInstance()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
237 jobdb = TableJobAdaptatorFactory.createInstance(iDb, "jobs")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
238 iLauncher = Launcher(jobdb, os.getcwd(), "", "", cDir, self._tmpDir, "jobs", self._resources, self._groupId, acronym, chooseTemplateWithCopy = self._isCopyOnNode)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
239
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
240 lCmdsTuples = []
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
241 lCmdSize = []
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
242 lCmdCopy = []
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
243
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
244 lFiles = FileUtils.getFileNamesList(".", self._alignPattern)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
245 lFileSizeTuples = []
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
246 for fileName in lFiles:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
247 fileSize = os.path.getsize(fileName)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
248 lFileSizeTuples.append((fileName, fileSize))
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
249 lFileSizeList = LauncherUtils.createHomogeneousSizeList(lFileSizeTuples, self._maxFileSize)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
250
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
251 for lFiles in lFileSizeList:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
252 lCmds = []
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
253 lCmdStart = []
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
254 lCmdFinish = []
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
255 if self._queryFileName:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
256 lCmdStart.append("os.symlink(\"%s/%s\", \"%s\")" % (cDir, self._queryFileName, self._queryFileName))
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
257 if self._subjectFileName and self._subjectFileName != self._queryFileName:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
258 lCmdStart.append("os.symlink(\"%s/%s\", \"%s\")" % (cDir, self._subjectFileName, self._subjectFileName))
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
259 for file in lFiles:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
260 lCmds.append(self._getLaunchMatcherCmd(iLauncher, file))
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
261 lCmdStart.append("os.symlink(\"%s/%s\", \"%s\")" % (cDir, file, file))
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
262 lCmdFinish.append("if os.path.exists(\"%s.match.path\"):" % file)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
263 lCmdFinish.append("\tshutil.move(\"%s.match.path\", \"%s/.\" )" % (file, cDir))
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
264 lCmdFinish.append("if os.path.exists(\"%s.match.tab\"):" % file)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
265 lCmdFinish.append("\tshutil.move(\"%s.match.tab\", \"%s/.\" )" % (file, cDir))
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
266 lCmdsTuples.append(iLauncher.prepareCommands_withoutIndentation(lCmds, lCmdStart, lCmdFinish, lCmdSize, lCmdCopy))
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
267
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
268 iLauncher.runLauncherForMultipleJobs("Matcher", lCmdsTuples, self._doClean, self._isCopyOnNode)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
269
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
270 if self._mergeResults:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
271 FileUtils.catFilesByPattern("*.match.path", "../%s.match.path" % self._outPrefix)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
272 if self._queryFileName or self._subjectFileName:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
273 outTabFileName = "../%s.match.tab" % self._outPrefix
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
274 self._writeTabHeader(outTabFileName)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
275 FileUtils.catFilesByPattern("*.match.tab", outTabFileName, skipHeaders = True)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
276 os.chdir("..")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
277 if self._doClean and self._mergeResults:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
278 self._log.warning("Working directory will be cleaned")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
279 shutil.rmtree(self._workingDir)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
280 self._log.info("END LaunchMatchInParallel")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
281
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
282 if __name__ == "__main__":
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
283 iLaunch = LaunchMatcherInParallel()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
284 iLaunch.setAttributesFromCmdLine()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
285 iLaunch.run()