Mercurial > repos > yufei-luo > s_mart
diff commons/launcher/LaunchMatcher.py @ 31:0ab839023fe4
Uploaded
author | m-zytnicki |
---|---|
date | Tue, 30 Apr 2013 14:33:21 -0400 |
parents | 94ab73e8a190 |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/commons/launcher/LaunchMatcher.py Tue Apr 30 14:33:21 2013 -0400 @@ -0,0 +1,168 @@ +#!/usr/bin/env python + +# Copyright INRA (Institut National de la Recherche Agronomique) +# http://www.inra.fr +# http://urgi.versailles.inra.fr +# +# This software is governed by the CeCILL license under French law and +# abiding by the rules of distribution of free software. You can use, +# modify and/ or redistribute the software under the terms of the CeCILL +# license as circulated by CEA, CNRS and INRIA at the following URL +# "http://www.cecill.info". +# +# As a counterpart to the access to the source code and rights to copy, +# modify and redistribute granted by the license, users are provided only +# with a limited warranty and the software's author, the holder of the +# economic rights, and the successive licensors have only limited +# liability. +# +# In this respect, the user's attention is drawn to the risks associated +# with loading, using, modifying and/or developing or reproducing the +# software by the user in light of its specific status of free software, +# that may mean that it is complicated to manipulate, and that also +# therefore means that it is reserved for developers and experienced +# professionals having in-depth computer knowledge. Users are therefore +# encouraged to load and test the software's suitability as regards their +# requirements in conditions enabling the security of their systems and/or +# data to be ensured and, more generally, to use and operate it in the +# same conditions as regards security. +# +# The fact that you are presently reading this means that you have had +# knowledge of the CeCILL license and that you accept its terms. + +from commons.core.LoggerFactory import LoggerFactory +from commons.core.utils.RepetOptionParser import RepetOptionParser +import subprocess + +LOG_DEPTH = "repet.tools" + +##Launch MATCHER +# +class LaunchMatcher(object): + + def __init__(self, align="", queryFileName="", subjectFileName="", evalue="1e-10", doJoin=False, keepConflict=False, prefix="", doClean = False, verbosity = 0): + self._alignFileName = align + self._queryFileName = queryFileName + self.setSubjectFileName(subjectFileName) + self.setOutPrefix(prefix) + self._doJoin = doJoin + self._eValue = evalue + self._keepConflict = keepConflict + + self._doClean = doClean + self._verbosity = verbosity + self._log = LoggerFactory.createLogger("%s.%s" % (LOG_DEPTH, self.__class__.__name__), self._verbosity) + + def setAttributesFromCmdLine(self): + description = "Launch Matcher." + epilog = "\nExample 1: launch without verbosity and keep temporary files.\n" + epilog += "\t$ python LaunchMatcher.py -a in.align -v 0" + epilog += "\n\t" + epilog += "\nExample 2: launch with verbosity to have errors (level 1) and basic information (level 2), and delete temporary files.\n" + epilog += "\t$ python LaunchMatcher.py -a in.align -q query.fa -s nr.fa -c -v 2" + parser = RepetOptionParser(description = description, epilog = epilog) + parser.add_option("-a", "--align", dest = "align", action = "store", type = "string", help = "input align file name [compulsory] [format: align]", default = "") + parser.add_option("-q", "--query", dest = "query", action = "store", type = "string", help = "query fasta file name [optional] [format: fasta]", default = "") + parser.add_option("-s", "--subject", dest = "subject", action = "store", type = "string", help = "subject fasta file name [optional] [format: fasta]", default = "") + parser.add_option("-e", "--evalue", dest = "evalue", action = "store", type = "string", help = "E-value filter [default: 1e-10]", default = "1e-10") + parser.add_option("-j", "--join", dest = "doJoin", action = "store_true", help = "join matches [default: False]", default = False) + parser.add_option("-k", "--keepConflict",dest = "keepConflict", action = "store_true", help = "keep conflicting subjects [default: False]", default = False) + parser.add_option("-o", "--outPrefix", dest = "outPrefix", action = "store", type = "string", help = "output file prefix [default: align file name]", default = "") + parser.add_option("-c", "--clean", dest = "doClean", action = "store_true", help = "clean temporary files [default: False]", default = False) + parser.add_option("-v", "--verbosity", dest = "verbosity", action = "store", type = "int", help = "verbosity [default: 1]", default = 1) + options = parser.parse_args()[0] + self._setAttributesFromOptions(options) + + def _setAttributesFromOptions(self, options): + self.setAlignFileName(options.align) + self.setQueryFileName(options.query) + self.setSubjectFileName(options.subject) + self.setEvalue(options.evalue) + self.setDoJoin(options.doJoin) + self.setKeepConflicts(options.keepConflict) + self.setOutPrefix(options.outPrefix) + self.setDoClean(options.doClean) + self.setVerbosity(options.verbosity) + + def setAlignFileName(self, alignFileName): + self._alignFileName = alignFileName + + def setQueryFileName(self, queryFileName): + self._queryFileName = queryFileName + + def setSubjectFileName(self, subjectFileName): + self._subjectFileName = subjectFileName + + def setEvalue(self, evalue): + self._eValue = evalue + + def setDoJoin(self, doJoin): + self._doJoin = doJoin + + def setKeepConflicts(self, keepConflict): + self._keepConflict = keepConflict + + def setOutPrefix(self, outPrefix): + if outPrefix == "": + self._outPrefix = self._alignFileName + else: + self._outPrefix = outPrefix + + def setDoClean(self, doClean): + self._doClean = doClean + + def setVerbosity(self, verbosity): + self._verbosity = verbosity + + def _checkOptions(self): + if self._alignFileName == "": + self._logAndRaise("ERROR: Missing input align file name") + + def _logAndRaise(self, errorMsg): + self._log.error(errorMsg) + raise Exception(errorMsg) + + def _getMatcherCmd(self): + lArgs = [] + lArgs.append("-m %s" % self._alignFileName) + if self._queryFileName: + lArgs.append("-q %s" % self._queryFileName) + if self._subjectFileName: + lArgs.append("-s %s" % self._subjectFileName) + if self._doJoin: + lArgs.append("-j") + lArgs.append("-E %s" % self._eValue) + lArgs.append("-B %s" % self._outPrefix) + if self._keepConflict: + lArgs.append("-a") + lArgs.append("-v %i" % (self._verbosity - 1)) + return self._getSystemCommand("matcher", lArgs) + + def _getSystemCommand(self, prg, lArgs): + systemCmd = prg + for arg in lArgs: + systemCmd += " " + arg + return systemCmd + + def run(self): + LoggerFactory.setLevel(self._log, self._verbosity) + self._checkOptions() + self._log.info("START LaunchMatcher") + self._log.debug("Align file name: %s" % self._alignFileName) + self._log.debug("Query file name: %s" % self._queryFileName) + self._log.debug("Subject file name: %s" % self._subjectFileName) + #TODO: clean files +# if self._doClean: +# self._log.warning("Files will be cleaned") + cmd = self._getMatcherCmd() + process = subprocess.Popen(cmd, shell = True) + self._log.debug("Running : %s" % cmd) + process.communicate() + if process.returncode != 0: + self._logAndRaise("ERROR when launching '%s'" % cmd) + self._log.info("END LaunchMatcher") + +if __name__ == "__main__": + iLaunch = LaunchMatcher() + iLaunch.setAttributesFromCmdLine() + iLaunch.run() \ No newline at end of file