Mercurial > repos > yufei-luo > s_mart
comparison commons/tools/LaunchBlaster.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 BLASTER | |
40 # | |
41 class LaunchBlaster(object): | |
42 | |
43 def __init__(self, queryFileName = "", subjectFileName = "", evalue = 1e-300, identity = 90, length = 100, doAllByall = False, type = "ncbi", nbCPU = 1, program="blastn",extraParams="", doClean = False, verbosity = 0): | |
44 self._queryFileName = queryFileName | |
45 self.setSubjectFileName(subjectFileName) | |
46 self._eValue = evalue | |
47 self._identity = identity | |
48 self._length = length | |
49 self._doAllByall = doAllByall | |
50 self._blastType = type | |
51 self._program = program | |
52 self._extraParams = extraParams | |
53 self._nbCPU = nbCPU | |
54 self._doClean = doClean | |
55 self._verbosity = verbosity | |
56 self._log = LoggerFactory.createLogger("%s.%s" % (LOG_DEPTH, self.__class__.__name__), self._verbosity) | |
57 | |
58 def setAttributesFromCmdLine(self): | |
59 description = "Launch Blaster." | |
60 epilog = "\nExample 1: launch without verbosity and keep temporary files.\n" | |
61 epilog += "\t$ python LaunchBlaster.py -q query.fa -v 0" | |
62 epilog += "\n\t" | |
63 epilog += "\nExample 2: launch with verbosity to have errors (level 1) and basic information (level 2), and delete temporary files.\n" | |
64 epilog += "\t$ python LaunchBlaster.py -q query.fa -s nr.fa -c -v 2" | |
65 parser = RepetOptionParser(description = description, epilog = epilog) | |
66 parser.add_option("-q", "--query", dest = "query", action = "store", type = "string", help = "query fasta file name [compulsory] [format: fasta]", default = "") | |
67 parser.add_option("-s", "--subject", dest = "subject", action = "store", type = "string", help = "subject fasta file name [default: query] [format: fasta]", default = "") | |
68 parser.add_option("-e", "--evalue", dest = "evalue", action = "store", type = "string", help = "Blast e-value [default: 1e-300]", default = "1e-300") | |
69 parser.add_option("-d", "--id", dest = "identity", action = "store", type = "int", help = "Blast identity [default: 90]", default = 90) | |
70 parser.add_option("-l", "--length", dest = "length", action = "store", type = "int", help = "Minimal hit length [default: 100]", default = 100) | |
71 parser.add_option("-a", "--aba", dest = "doAllByall", action = "store_true", help = "all-by-all Blast [default: False]", default = False) | |
72 parser.add_option("-t", "--type", dest = "type", action = "store", type = "string", help = "Blast type [ncbi, wu, blastplus] [default: ncbi]", default = "ncbi") | |
73 parser.add_option("-u", "--program", dest = "program", action = "store", type = "string", help = "Blast program type [blastn, blastx, blastx] [default: blastn]", default = "blastn") | |
74 parser.add_option("-x", "--extraParams",dest = "extraParams", action = "store", type = "string", help = "Additional blast program parameters[default: '']", default = "") | |
75 parser.add_option("-n", "--ncpu", dest = "cpu", action = "store", type = "int", help = "Number of CPUs to use [default: 1]", default = 1) | |
76 parser.add_option("-c", "--clean", dest = "doClean", action = "store_true", help = "clean temporary files [default: False]", default = False) | |
77 parser.add_option("-v", "--verbosity", dest = "verbosity", action = "store", type = "int", help = "verbosity [default: 1]", default = 1) | |
78 options = parser.parse_args()[0] | |
79 self._setAttributesFromOptions(options) | |
80 | |
81 def _setAttributesFromOptions(self, options): | |
82 self.setQueryFileName(options.query) | |
83 self.setSubjectFileName(options.subject) | |
84 self.setEvalue(options.evalue) | |
85 self.setIdentity(options.identity) | |
86 self.setLength(options.length) | |
87 self.setDoAllByall(options.doAllByall) | |
88 self.setType(options.type) | |
89 self.setProgram(options.program) | |
90 self.setExtraParams(options.extraParams) | |
91 self.setCPU(options.cpu) | |
92 self.setDoClean(options.doClean) | |
93 self.setVerbosity(options.verbosity) | |
94 | |
95 | |
96 def setQueryFileName(self, queryFileName): | |
97 self._queryFileName = queryFileName | |
98 | |
99 def setSubjectFileName(self, subjectFileName): | |
100 if subjectFileName == "": | |
101 self._subjectFileName = self._queryFileName | |
102 else: | |
103 self._subjectFileName = subjectFileName | |
104 | |
105 def setEvalue(self, evalue): | |
106 self._eValue = evalue | |
107 | |
108 def setIdentity(self, identity): | |
109 self._identity = identity | |
110 | |
111 def setLength(self, length): | |
112 self._length = length | |
113 | |
114 def setDoAllByall(self, doAllByall): | |
115 self._doAllByall = doAllByall | |
116 | |
117 def setType(self, blastType): | |
118 self._blastType = blastType | |
119 | |
120 def setProgram(self, program): | |
121 self._program = program | |
122 | |
123 def setExtraParams(self, extraParams): | |
124 self._extraParams = extraParams | |
125 | |
126 def setCPU(self, cpu): | |
127 self._nbCPU = cpu | |
128 | |
129 def setDoClean(self, doClean): | |
130 self._doClean = doClean | |
131 | |
132 def setVerbosity(self, verbosity): | |
133 self._verbosity = verbosity | |
134 | |
135 def _checkOptions(self): | |
136 if self._queryFileName == "": | |
137 self._logAndRaise("ERROR: Missing input fasta file name") | |
138 | |
139 lBlastType = ["ncbi", "wu", "blastplus"] | |
140 if self._blastType.lower() not in lBlastType: | |
141 self._logAndRaise("ERROR: unknown Blast type '%s' - correct values are %s" % (self._blastType, lBlastType)) | |
142 | |
143 def _logAndRaise(self, errorMsg): | |
144 self._log.error(errorMsg) | |
145 raise Exception(errorMsg) | |
146 | |
147 def _getBlasterCmd(self): | |
148 lArgs = [] | |
149 lArgs.append("-n %s" % self._program) | |
150 lArgs.append("-q %s" % self._queryFileName) | |
151 lArgs.append("-s %s" % self._subjectFileName) | |
152 lArgs.append("-B %s" % self._queryFileName) | |
153 if self._doAllByall: | |
154 lArgs.append("-a") | |
155 lArgs.append("-E %s" % self._eValue) | |
156 lArgs.append("-L %s" % self._length) | |
157 lArgs.append("-I %s" % self._identity) | |
158 if self._blastType == "ncbi": | |
159 lArgs.append("-N") | |
160 lArgs.append("-p '-a %s %s'" % (self._nbCPU, self._extraParams)) | |
161 elif self._blastType == "wu": | |
162 lArgs.append("-W") | |
163 lArgs.append("-p '-cpus=%s %s'" % (self._nbCPU, self._extraParams)) | |
164 elif self._blastType == "blastplus": | |
165 lArgs.append("-X") | |
166 lArgs.append("-p '-num_threads %s %s'" % (self._nbCPU, self._extraParams)) | |
167 # TODO: check the check option at the beginning of step 2 to allow to launch megablast for blast and blast+ | |
168 # elif config.get(sectionName, "blast") == "mega": | |
169 # lArgs.append("-N") | |
170 # lArgs.append("-n megablast") | |
171 # elif config.get(sectionName, "blast") == "megablastplus": | |
172 # lArgs.append("-X") | |
173 # lArgs.append("-n megablast") | |
174 if self._doClean: | |
175 lArgs.append("-c") | |
176 lArgs.append("-v %i" % (self._verbosity - 1)) | |
177 return self._getSystemCommand("blaster", lArgs) | |
178 | |
179 def _getSystemCommand(self, prg, lArgs): | |
180 systemCmd = prg | |
181 for arg in lArgs: | |
182 systemCmd += " " + arg | |
183 return systemCmd | |
184 | |
185 def run(self): | |
186 LoggerFactory.setLevel(self._log, self._verbosity) | |
187 self._checkOptions() | |
188 self._log.info("START LaunchBlaster") | |
189 self._log.debug("Query file name: %s" % self._queryFileName) | |
190 self._log.debug("Subject file name: %s" % self._subjectFileName) | |
191 if self._doClean: | |
192 self._log.warning("Files will be cleaned") | |
193 cmd = self._getBlasterCmd() | |
194 process = subprocess.Popen(cmd, shell = True) | |
195 self._log.debug("Running : %s" % cmd) | |
196 process.communicate() | |
197 if process.returncode != 0: | |
198 self._logAndRaise("ERROR when launching '%s'" % cmd) | |
199 self._log.info("END LaunchBlaster") | |
200 | |
201 if __name__ == "__main__": | |
202 iLaunch = LaunchBlaster() | |
203 iLaunch.setAttributesFromCmdLine() | |
204 iLaunch.run() |