18
+ − 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.checker.CheckerUtils import CheckerUtils
+ − 34 from commons.core.utils.FileUtils import FileUtils
+ − 35 from commons.core.utils.RepetOptionParser import RepetOptionParser
+ − 36 import subprocess
+ − 37 from commons.core.LoggerFactory import LoggerFactory
+ − 38 import os
+ − 39
+ − 40 LOG_DEPTH = "repet.tools"
+ − 41
+ − 42 class LaunchPromer(object):
+ − 43
+ − 44 def __init__(self,queryFileName="", refFileName ="", prefix = None, genCoords=False, showCoords = False, mum=False, maxGaps=30, minMatch=6, nooptimize=False,mincluster=20, verbosity=0):
+ − 45 self._queryFileName = queryFileName
+ − 46 self._refFileName = refFileName
+ − 47 self._prefix = prefix
+ − 48 self._genCoords = genCoords
+ − 49 self._showCoords = showCoords
+ − 50 self._mum = mum
+ − 51 self._maxgaps = maxGaps
+ − 52 self._minMatch = minMatch
+ − 53 self._nooptimize = nooptimize
+ − 54 self._mincluster = mincluster
+ − 55 self.verbosity = verbosity
+ − 56 self._log = LoggerFactory.createLogger("%s.%s" % (LOG_DEPTH, self.__class__.__name__), self.verbosity)
+ − 57
+ − 58 def setMincluster(self, value):
+ − 59 self._mincluster = value
+ − 60 def getMincluster(self):
+ − 61 return self._mincluster
+ − 62
+ − 63 mincluster = property(getMincluster, setMincluster)
+ − 64
+ − 65 def setAttributesFromCmdLine(self):
+ − 66 description = "LaunchPromer runs the Promer program (part of the mummer package) ."
+ − 67 parser = RepetOptionParser(description = description)
+ − 68 parser.add_option("-q", "--query", dest="queryFileName", default = "", action="store", type="string", help="input query file [compulsory] [format: fasta]")
+ − 69 parser.add_option("-r", "--ref", dest="refFileName", default = "", action="store", type="string", help="input ref file [compulsory] [format: fasta]")
+ − 70 parser.add_option("-p", "--prefix", dest="prefix", default = None, action="store", type="string", help="prefix name [optional]")
+ − 71 parser.add_option("-o","--gencoords", dest="genCoords",action="store_true", help="generate coords file with minimal option (show-coords -r) [optional] ")
+ − 72 parser.add_option("-s","--showcoords", dest="showCoords",action="store_true", help="generate coords file with: show-coords -r -c -l -d -I 50 -L 100 -T [optional] ")
+ − 73 parser.add_option("-m", "--mum", dest="mum", action="store_true", help="Use anchor matches that are unique in both the reference and query [optional] ")
+ − 74 parser.add_option("-g", "--maxgaps", dest="maxgaps", default = 30, action="store", type="int", help="Maximum gap between two adjacent matches in a cluster (default 30) [optional] ")
+ − 75 parser.add_option("-l", "--minmatch", dest="minMatch", default = 6, action="store", type="int", help="Minimum length of an maximal exact match (default 6) [optional] ")
+ − 76 parser.add_option("-n", "--nooptimize", dest="nooptimize", action="store_true", help="nooptimize (default --optimize) [optional] ")
+ − 77 parser.add_option("-j", "--mincluster", dest="mincluster", default = 20, action="store", type="int", help="Minimum length of a cluster of matches (default 20) [optional] ")
+ − 78 parser.add_option("-v", "--verbosity", dest="verbosity", default = 0, action="store", type="int", help="verbosity [optional] ")
+ − 79
+ − 80 (self._options, args) = parser.parse_args()
+ − 81 self._setAttributesFromOptions(self._options)
+ − 82
+ − 83 def _setAttributesFromOptions(self, options):
+ − 84 self._queryFileName = options.queryFileName
+ − 85 self._refFileName = options.refFileName
+ − 86 self._prefix = options.prefix
+ − 87 self._genCoords = options.genCoords
+ − 88 self._showCoords = options.showCoords
+ − 89 self._mum = options.mum
+ − 90 self._maxgaps = options.maxgaps
+ − 91 self._minMatch = options.minMatch
+ − 92 self._nooptimize = options.nooptimize
+ − 93 self._mincluster = options.mincluster
+ − 94 self.verbosity = options.verbosity
+ − 95
+ − 96 def _logAndRaise(self, errorMsg):
+ − 97 self._log.error(errorMsg)
+ − 98 raise Exception(errorMsg)
+ − 99
+ − 100 def checkOptions(self):
+ − 101 if self._queryFileName != "":
+ − 102 if not FileUtils.isRessourceExists(self._queryFileName):
+ − 103 self._logAndRaise("ERROR: Query file: %s does not exist!" % self._queryFileName)
+ − 104 else:
+ − 105 self._logAndRaise("ERROR: No specified --query option!")
+ − 106
+ − 107 if self._refFileName != "":
+ − 108 if not FileUtils.isRessourceExists(self._refFileName):
+ − 109 self._logAndRaise("ERROR: Ref file does not exist!"% self._refFileName)
+ − 110 else:
+ − 111 self._logAndRaise("ERROR: No specified --ref option!")
+ − 112
+ − 113 def run(self):
+ − 114 if not CheckerUtils.isExecutableInUserPath("promer") :
+ − 115 self._logAndRaise("ERROR: promer must be in your path")
+ − 116 self.checkOptions()
+ − 117
+ − 118 genCoords = ""
+ − 119 if self._genCoords:
+ − 120 genCoords = "-o"
+ − 121 mum = ""
+ − 122 if self._mum:
+ − 123 mum = "--mum"
+ − 124 nooptimize = "--optimize"
+ − 125 if self._nooptimize:
+ − 126 nooptimize = "--nooptimize"
+ − 127 prefix = ""
+ − 128 if self._prefix is not None:
+ − 129 prefix = "--prefix=%s" %(self._prefix)
+ − 130
+ − 131 cmd = "promer %s %s %s %s %s -g=%d -l=%d %s -c=%d" % (self._refFileName,self._queryFileName, prefix, genCoords, mum, self._maxgaps, self._minMatch, nooptimize, self._mincluster)
+ − 132 self._log.debug("Running promer with following commands : %s" %cmd)
+ − 133 cmd = cmd.split()
+ − 134 process = subprocess.Popen(cmd)
+ − 135 process.wait()
+ − 136
+ − 137 if self._showCoords:
+ − 138 #use of os.system because redirect on process is broken in python < 3.0
+ − 139 cmd = "show-coords -r -c -l -d -I 50 -L 100 -T %s.delta > %s.coords" % (self._prefix, self._prefix)
+ − 140 os.system(cmd)
+ − 141
+ − 142 return process.returncode
+ − 143
+ − 144 if __name__ == "__main__":
+ − 145 iLaunchPromer = LaunchPromer()
+ − 146 iLaunchPromer.setAttributesFromCmdLine()
+ − 147 iLaunchPromer.run()