annotate commons/launcher/LaunchMatcher.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 # 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.utils.RepetOptionParser import RepetOptionParser
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
35 import subprocess
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
36
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
37 LOG_DEPTH = "repet.tools"
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
38
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
39 ##Launch MATCHER
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
40 #
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
41 class LaunchMatcher(object):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
42
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
43 def __init__(self, align="", queryFileName="", subjectFileName="", evalue="1e-10", doJoin=False, keepConflict=False, prefix="", doClean = False, verbosity = 0):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
44 self._alignFileName = align
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
45 self._queryFileName = queryFileName
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
46 self.setSubjectFileName(subjectFileName)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
47 self.setOutPrefix(prefix)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
48 self._doJoin = doJoin
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
49 self._eValue = evalue
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
50 self._keepConflict = keepConflict
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
51
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
52 self._doClean = doClean
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
53 self._verbosity = verbosity
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
54 self._log = LoggerFactory.createLogger("%s.%s" % (LOG_DEPTH, self.__class__.__name__), self._verbosity)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
55
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
56 def setAttributesFromCmdLine(self):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
57 description = "Launch Matcher."
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
58 epilog = "\nExample 1: launch without verbosity and keep temporary files.\n"
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
59 epilog += "\t$ python LaunchMatcher.py -a in.align -v 0"
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
60 epilog += "\n\t"
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
61 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
62 epilog += "\t$ python LaunchMatcher.py -a in.align -q query.fa -s nr.fa -c -v 2"
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
63 parser = RepetOptionParser(description = description, epilog = epilog)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
64 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
65 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
66 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
67 parser.add_option("-e", "--evalue", dest = "evalue", action = "store", type = "string", help = "E-value filter [default: 1e-10]", default = "1e-10")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
68 parser.add_option("-j", "--join", dest = "doJoin", action = "store_true", help = "join matches [default: False]", default = False)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
69 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
70 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
71 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
72 parser.add_option("-v", "--verbosity", dest = "verbosity", action = "store", type = "int", help = "verbosity [default: 1]", default = 1)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
73 options = parser.parse_args()[0]
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
74 self._setAttributesFromOptions(options)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
75
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
76 def _setAttributesFromOptions(self, options):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
77 self.setAlignFileName(options.align)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
78 self.setQueryFileName(options.query)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
79 self.setSubjectFileName(options.subject)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
80 self.setEvalue(options.evalue)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
81 self.setDoJoin(options.doJoin)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
82 self.setKeepConflicts(options.keepConflict)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
83 self.setOutPrefix(options.outPrefix)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
84 self.setDoClean(options.doClean)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
85 self.setVerbosity(options.verbosity)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
86
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
87 def setAlignFileName(self, alignFileName):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
88 self._alignFileName = alignFileName
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
89
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
90 def setQueryFileName(self, queryFileName):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
91 self._queryFileName = queryFileName
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
92
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
93 def setSubjectFileName(self, subjectFileName):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
94 self._subjectFileName = subjectFileName
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
95
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
96 def setEvalue(self, evalue):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
97 self._eValue = evalue
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
98
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
99 def setDoJoin(self, doJoin):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
100 self._doJoin = doJoin
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
101
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
102 def setKeepConflicts(self, keepConflict):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
103 self._keepConflict = keepConflict
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
104
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
105 def setOutPrefix(self, outPrefix):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
106 if outPrefix == "":
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
107 self._outPrefix = self._alignFileName
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
108 else:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
109 self._outPrefix = outPrefix
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
110
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
111 def setDoClean(self, doClean):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
112 self._doClean = doClean
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
113
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
114 def setVerbosity(self, verbosity):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
115 self._verbosity = verbosity
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
116
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
117 def _checkOptions(self):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
118 if self._alignFileName == "":
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
119 self._logAndRaise("ERROR: Missing input align file name")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
120
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
121 def _logAndRaise(self, errorMsg):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
122 self._log.error(errorMsg)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
123 raise Exception(errorMsg)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
124
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
125 def _getMatcherCmd(self):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
126 lArgs = []
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
127 lArgs.append("-m %s" % self._alignFileName)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
128 if self._queryFileName:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
129 lArgs.append("-q %s" % self._queryFileName)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
130 if self._subjectFileName:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
131 lArgs.append("-s %s" % self._subjectFileName)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
132 if self._doJoin:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
133 lArgs.append("-j")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
134 lArgs.append("-E %s" % self._eValue)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
135 lArgs.append("-B %s" % self._outPrefix)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
136 if self._keepConflict:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
137 lArgs.append("-a")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
138 lArgs.append("-v %i" % (self._verbosity - 1))
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
139 return self._getSystemCommand("matcher", lArgs)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
140
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
141 def _getSystemCommand(self, prg, lArgs):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
142 systemCmd = prg
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
143 for arg in lArgs:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
144 systemCmd += " " + arg
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
145 return systemCmd
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
146
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
147 def run(self):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
148 LoggerFactory.setLevel(self._log, self._verbosity)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
149 self._checkOptions()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
150 self._log.info("START LaunchMatcher")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
151 self._log.debug("Align file name: %s" % self._alignFileName)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
152 self._log.debug("Query file name: %s" % self._queryFileName)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
153 self._log.debug("Subject file name: %s" % self._subjectFileName)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
154 #TODO: clean files
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
155 # if self._doClean:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
156 # self._log.warning("Files will be cleaned")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
157 cmd = self._getMatcherCmd()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
158 process = subprocess.Popen(cmd, shell = True)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
159 self._log.debug("Running : %s" % cmd)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
160 process.communicate()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
161 if process.returncode != 0:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
162 self._logAndRaise("ERROR when launching '%s'" % cmd)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
163 self._log.info("END LaunchMatcher")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
164
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
165 if __name__ == "__main__":
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
166 iLaunch = LaunchMatcher()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
167 iLaunch.setAttributesFromCmdLine()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
168 iLaunch.run()