comparison commons/launcher/LaunchMatcher.py @ 18:94ab73e8a190

Uploaded
author m-zytnicki
date Mon, 29 Apr 2013 03:20:15 -0400
parents
children
comparison
equal deleted inserted replaced
17:b0e8584489e6 18:94ab73e8a190
1 #!/usr/bin/env python
2
3 # Copyright INRA (Institut National de la Recherche Agronomique)
4 # http://www.inra.fr
5 # http://urgi.versailles.inra.fr
6 #
7 # This software is governed by the CeCILL license under French law and
8 # abiding by the rules of distribution of free software. You can use,
9 # modify and/ or redistribute the software under the terms of the CeCILL
10 # license as circulated by CEA, CNRS and INRIA at the following URL
11 # "http://www.cecill.info".
12 #
13 # As a counterpart to the access to the source code and rights to copy,
14 # modify and redistribute granted by the license, users are provided only
15 # with a limited warranty and the software's author, the holder of the
16 # economic rights, and the successive licensors have only limited
17 # liability.
18 #
19 # In this respect, the user's attention is drawn to the risks associated
20 # with loading, using, modifying and/or developing or reproducing the
21 # software by the user in light of its specific status of free software,
22 # that may mean that it is complicated to manipulate, and that also
23 # therefore means that it is reserved for developers and experienced
24 # professionals having in-depth computer knowledge. Users are therefore
25 # encouraged to load and test the software's suitability as regards their
26 # requirements in conditions enabling the security of their systems and/or
27 # data to be ensured and, more generally, to use and operate it in the
28 # same conditions as regards security.
29 #
30 # The fact that you are presently reading this means that you have had
31 # knowledge of the CeCILL license and that you accept its terms.
32
33 from commons.core.LoggerFactory import LoggerFactory
34 from commons.core.utils.RepetOptionParser import RepetOptionParser
35 import subprocess
36
37 LOG_DEPTH = "repet.tools"
38
39 ##Launch MATCHER
40 #
41 class LaunchMatcher(object):
42
43 def __init__(self, align="", queryFileName="", subjectFileName="", evalue="1e-10", doJoin=False, keepConflict=False, prefix="", doClean = False, verbosity = 0):
44 self._alignFileName = align
45 self._queryFileName = queryFileName
46 self.setSubjectFileName(subjectFileName)
47 self.setOutPrefix(prefix)
48 self._doJoin = doJoin
49 self._eValue = evalue
50 self._keepConflict = keepConflict
51
52 self._doClean = doClean
53 self._verbosity = verbosity
54 self._log = LoggerFactory.createLogger("%s.%s" % (LOG_DEPTH, self.__class__.__name__), self._verbosity)
55
56 def setAttributesFromCmdLine(self):
57 description = "Launch Matcher."
58 epilog = "\nExample 1: launch without verbosity and keep temporary files.\n"
59 epilog += "\t$ python LaunchMatcher.py -a in.align -v 0"
60 epilog += "\n\t"
61 epilog += "\nExample 2: launch with verbosity to have errors (level 1) and basic information (level 2), and delete temporary files.\n"
62 epilog += "\t$ python LaunchMatcher.py -a in.align -q query.fa -s nr.fa -c -v 2"
63 parser = RepetOptionParser(description = description, epilog = epilog)
64 parser.add_option("-a", "--align", dest = "align", action = "store", type = "string", help = "input align file name [compulsory] [format: align]", default = "")
65 parser.add_option("-q", "--query", dest = "query", action = "store", type = "string", help = "query fasta file name [optional] [format: fasta]", default = "")
66 parser.add_option("-s", "--subject", dest = "subject", action = "store", type = "string", help = "subject fasta file name [optional] [format: fasta]", default = "")
67 parser.add_option("-e", "--evalue", dest = "evalue", action = "store", type = "string", help = "E-value filter [default: 1e-10]", default = "1e-10")
68 parser.add_option("-j", "--join", dest = "doJoin", action = "store_true", help = "join matches [default: False]", default = False)
69 parser.add_option("-k", "--keepConflict",dest = "keepConflict", action = "store_true", help = "keep conflicting subjects [default: False]", default = False)
70 parser.add_option("-o", "--outPrefix", dest = "outPrefix", action = "store", type = "string", help = "output file prefix [default: align file name]", default = "")
71 parser.add_option("-c", "--clean", dest = "doClean", action = "store_true", help = "clean temporary files [default: False]", default = False)
72 parser.add_option("-v", "--verbosity", dest = "verbosity", action = "store", type = "int", help = "verbosity [default: 1]", default = 1)
73 options = parser.parse_args()[0]
74 self._setAttributesFromOptions(options)
75
76 def _setAttributesFromOptions(self, options):
77 self.setAlignFileName(options.align)
78 self.setQueryFileName(options.query)
79 self.setSubjectFileName(options.subject)
80 self.setEvalue(options.evalue)
81 self.setDoJoin(options.doJoin)
82 self.setKeepConflicts(options.keepConflict)
83 self.setOutPrefix(options.outPrefix)
84 self.setDoClean(options.doClean)
85 self.setVerbosity(options.verbosity)
86
87 def setAlignFileName(self, alignFileName):
88 self._alignFileName = alignFileName
89
90 def setQueryFileName(self, queryFileName):
91 self._queryFileName = queryFileName
92
93 def setSubjectFileName(self, subjectFileName):
94 self._subjectFileName = subjectFileName
95
96 def setEvalue(self, evalue):
97 self._eValue = evalue
98
99 def setDoJoin(self, doJoin):
100 self._doJoin = doJoin
101
102 def setKeepConflicts(self, keepConflict):
103 self._keepConflict = keepConflict
104
105 def setOutPrefix(self, outPrefix):
106 if outPrefix == "":
107 self._outPrefix = self._alignFileName
108 else:
109 self._outPrefix = outPrefix
110
111 def setDoClean(self, doClean):
112 self._doClean = doClean
113
114 def setVerbosity(self, verbosity):
115 self._verbosity = verbosity
116
117 def _checkOptions(self):
118 if self._alignFileName == "":
119 self._logAndRaise("ERROR: Missing input align file name")
120
121 def _logAndRaise(self, errorMsg):
122 self._log.error(errorMsg)
123 raise Exception(errorMsg)
124
125 def _getMatcherCmd(self):
126 lArgs = []
127 lArgs.append("-m %s" % self._alignFileName)
128 if self._queryFileName:
129 lArgs.append("-q %s" % self._queryFileName)
130 if self._subjectFileName:
131 lArgs.append("-s %s" % self._subjectFileName)
132 if self._doJoin:
133 lArgs.append("-j")
134 lArgs.append("-E %s" % self._eValue)
135 lArgs.append("-B %s" % self._outPrefix)
136 if self._keepConflict:
137 lArgs.append("-a")
138 lArgs.append("-v %i" % (self._verbosity - 1))
139 return self._getSystemCommand("matcher", lArgs)
140
141 def _getSystemCommand(self, prg, lArgs):
142 systemCmd = prg
143 for arg in lArgs:
144 systemCmd += " " + arg
145 return systemCmd
146
147 def run(self):
148 LoggerFactory.setLevel(self._log, self._verbosity)
149 self._checkOptions()
150 self._log.info("START LaunchMatcher")
151 self._log.debug("Align file name: %s" % self._alignFileName)
152 self._log.debug("Query file name: %s" % self._queryFileName)
153 self._log.debug("Subject file name: %s" % self._subjectFileName)
154 #TODO: clean files
155 # if self._doClean:
156 # self._log.warning("Files will be cleaned")
157 cmd = self._getMatcherCmd()
158 process = subprocess.Popen(cmd, shell = True)
159 self._log.debug("Running : %s" % cmd)
160 process.communicate()
161 if process.returncode != 0:
162 self._logAndRaise("ERROR when launching '%s'" % cmd)
163 self._log.info("END LaunchMatcher")
164
165 if __name__ == "__main__":
166 iLaunch = LaunchMatcher()
167 iLaunch.setAttributesFromCmdLine()
168 iLaunch.run()