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 import os
|
|
34 import sys
|
|
35
|
|
36 if not "REPET_PATH" in os.environ.keys():
|
|
37 print "ERROR: no environment variable REPET_PATH"
|
|
38 sys.exit(1)
|
|
39 sys.path.append(os.environ["REPET_PATH"])
|
|
40 if not "PYTHONPATH" in os.environ.keys():
|
|
41 os.environ["PYTHONPATH"] = os.environ["REPET_PATH"]
|
|
42 else:
|
|
43 os.environ["PYTHONPATH"] = "%s:%s" % (os.environ["REPET_PATH"], os.environ["PYTHONPATH"])
|
|
44
|
|
45 from commons.core.LoggerFactory import LoggerFactory
|
|
46 from commons.core.utils.RepetOptionParser import RepetOptionParser
|
|
47 from commons.core.checker.ConfigChecker import ConfigRules
|
|
48 from commons.core.checker.ConfigChecker import ConfigChecker
|
|
49 from commons.core.seq.FastaUtils import FastaUtils
|
|
50 from denovo_pipe.ReverseComplementAccordingToClassif import ReverseComplementAccordingToClassif
|
|
51 from denovo_pipe.RenameHeaderClassif import RenameHeaderClassif
|
|
52 from denovo_pipe.DetectTEFeatures import DetectTEFeatures
|
|
53 from denovo_pipe.LaunchPASTEC import LaunchPASTEC
|
|
54 from PASTEC.StatPastec import StatPastec
|
|
55
|
|
56 LOG_DEPTH = "repet.tools"
|
|
57 #LOG_FORMAT = "%(message)s"
|
|
58
|
|
59 ####TEclassifier PASTEC Edition
|
|
60 #
|
|
61 class TEclassifierPE(object):
|
|
62
|
|
63 def __init__(self, fastaFileName = "", configFileName = "", addWickerCode = False, reverseComp = False, doClean = False, verbosity = 0):
|
|
64 self._fastaFileName = fastaFileName
|
|
65 self._addWickerCode = addWickerCode
|
|
66 self._reverseComp = reverseComp
|
|
67 self._configFileName = configFileName
|
|
68 self._doClean = doClean
|
|
69 self._verbosity = verbosity
|
|
70 self._projectName = ""
|
|
71 self._log = LoggerFactory.createLogger("%s.%s" % (LOG_DEPTH, self.__class__.__name__), self._verbosity)
|
|
72
|
|
73 def setAttributesFromCmdLine(self):
|
|
74 description = "TE classifier PASTEC Edition.\n"
|
|
75 description += "Detect TE features on consensus and classify them. Give some classification statistics.\n"
|
|
76 description += "Can rename headers with classification info and Wicker's code at the beginning.\n"
|
|
77 description += "Can reverse-complement consensus if they are detected in reverse strand.\n"
|
|
78 description += "Warning : it's highly advised to use sequences in upper case.\n"
|
|
79 epilog = "\n"
|
|
80 epilog += "Example 1: launch and clean temporary files\n"
|
|
81 epilog += "\t$ python TEclassifierPE.py -i consensus.fa -C TEclassifier.cfg -c\n"
|
|
82 epilog += "\n"
|
|
83 epilog += "Example 2: launch with 'rename headers' and 'reverse-complement' options\n"
|
|
84 epilog += "\t$ python TEclassifierPE.py -i consensus.fa -C TEclassifier.cfg -c -w -r\n"
|
|
85 parser = RepetOptionParser(description = description, epilog = epilog)
|
|
86 parser.add_option("-i", "--fasta", dest = "fastaFileName", action = "store", type = "string", help = "input fasta file name [compulsory] [format: fasta]", default = "")
|
|
87 parser.add_option("-C", "--config", dest = "configFileName",action = "store", type = "string", help = "configuration file name (e.g. TEclassifier.cfg) [compulsory]", default = "")
|
|
88 parser.add_option("-w", "--wicker", dest = "addWickerCode", action = "store_true", help = "add classification info and Wicker's code at the beginning of the headers [optional] [default: False]", default = False)
|
|
89 parser.add_option("-r", "--reverse", dest = "reverseComp", action = "store_true", help = "reverse-complement consensus if they are detected in reverse strand [optional] [default: False]", default = False)
|
|
90 parser.add_option("-c", "--clean", dest = "doClean", action = "store_true", help = "clean temporary files [optional] [default: False]", default = False)
|
|
91 parser.add_option("-v", "--verbosity", dest = "verbosity", action = "store", type = "int", help = "verbosity [optional] [default: 3, from 1 to 4]", default = 3)
|
|
92 options = parser.parse_args()[0]
|
|
93 self._setAttributesFromOptions(options)
|
|
94
|
|
95 def _setAttributesFromOptions(self, options):
|
|
96 self.setFastaFileName(options.fastaFileName)
|
|
97 self.setAddWickerCode(options.addWickerCode)
|
|
98 self.setReverseComp(options.reverseComp)
|
|
99 self.setConfigFileName(options.configFileName)
|
|
100 self.setDoClean(options.doClean)
|
|
101 self.setVerbosity(options.verbosity)
|
|
102
|
|
103 def _checkConfig(self):
|
|
104 iConfigRules = ConfigRules()
|
|
105 iConfigRules.addRuleOption(section="project", option ="project_name", mandatory=True, type="string")
|
|
106 sectionName = "classif_consensus"
|
|
107 iConfigRules.addRuleOption(section=sectionName, option ="clean", mandatory=True, type="bool")
|
|
108 iConfigChecker = ConfigChecker(self._configFileName, iConfigRules)
|
|
109 iConfig = iConfigChecker.getConfig()
|
|
110 self._setAttributesFromConfig(iConfig)
|
|
111
|
|
112 def _setAttributesFromConfig(self, iConfig):
|
|
113 self.setProjectName(iConfig.get("project", "project_name"))
|
|
114 sectionName = "classif_consensus"
|
|
115 self.setDoClean(iConfig.get(sectionName, "clean"))
|
|
116
|
|
117 def setFastaFileName(self, fastaFileName):
|
|
118 self._fastaFileName = fastaFileName
|
|
119
|
|
120 def setConfigFileName(self, configFileName):
|
|
121 self._configFileName = configFileName
|
|
122
|
|
123 def setAddWickerCode(self, addWickerCode):
|
|
124 self._addWickerCode = addWickerCode
|
|
125
|
|
126 def setReverseComp(self, reverseComp):
|
|
127 self._reverseComp = reverseComp
|
|
128
|
|
129 def setDoClean(self, doClean):
|
|
130 self._doClean = doClean
|
|
131
|
|
132 def setVerbosity(self, verbosity):
|
|
133 self._verbosity = verbosity
|
|
134
|
|
135 def setProjectName(self, projectName):
|
|
136 self._projectName = projectName
|
|
137
|
|
138 def _checkOptions(self):
|
|
139 if self._fastaFileName == "":
|
|
140 self._logAndRaise("ERROR: Missing input fasta file name")
|
|
141
|
|
142 def _logAndRaise(self, errorMsg):
|
|
143 self._log.error(errorMsg)
|
|
144 raise Exception(errorMsg)
|
|
145
|
|
146 def run(self):
|
|
147 LoggerFactory.setLevel(self._log, self._verbosity)
|
|
148 if self._configFileName:
|
|
149 self._checkConfig()
|
|
150 self._checkOptions()
|
|
151 self._log.info("START TEclassifier PASTEC Edition")
|
|
152 self._log.debug("Fasta file name: %s" % self._fastaFileName)
|
|
153 nbSeq = FastaUtils.dbSize(self._fastaFileName)
|
|
154 self._log.debug("Total number of sequences: %i)" % nbSeq)
|
|
155
|
|
156 #TODO: add step => avoid to re-launch DetectTEFeatures, if error with PASTEC (e.g. wrong bank format)
|
|
157 #step 1
|
|
158 iDF = DetectTEFeatures(self._fastaFileName, self._projectName, self._configFileName, self._doClean, self._verbosity)
|
|
159 iDF.run()
|
|
160
|
|
161 #step 2
|
|
162 iLP = LaunchPASTEC(configFileName = self._configFileName, inputFileName = self._fastaFileName, projectName = self._projectName, verbose = self._verbosity)
|
|
163 iLP.run()
|
|
164
|
|
165 classifFileName = "%s.classif" % self._projectName
|
|
166
|
|
167 iSP = StatPastec(classifFileName)
|
|
168 iSP.run()
|
|
169
|
|
170 if self._reverseComp:
|
|
171 self._log.info("Reverse complement...")
|
|
172 iRevComplAccording2Classif = ReverseComplementAccordingToClassif()
|
|
173 iRevComplAccording2Classif.setFastaFile(self._fastaFileName)
|
|
174 iRevComplAccording2Classif.setClassifFile(classifFileName)
|
|
175 iRevComplAccording2Classif.run()
|
|
176 tmpFastaFileName = "%s_negStrandReversed.fa" % os.path.splitext(self._fastaFileName)[0]
|
|
177 else:
|
|
178 tmpFastaFileName = self._fastaFileName
|
|
179
|
|
180 if self._addWickerCode:
|
|
181 self._log.info("Rename headers according to Wicker's code...")
|
|
182 iRHC = RenameHeaderClassif(classifFileName, tmpFastaFileName, self._projectName)
|
|
183 iRHC.setOutputFileName("")
|
|
184 iRHC.run()
|
|
185 if self._doClean:
|
|
186 os.remove(tmpFastaFileName)
|
|
187
|
|
188 self._log.info("END TEclassifier PASTEC Edition")
|
|
189
|
|
190 if __name__ == "__main__":
|
|
191 iLaunch = TEclassifierPE()
|
|
192 iLaunch.setAttributesFromCmdLine()
|
|
193 iLaunch.run() |