Mercurial > repos > urgi-team > teiso
comparison TEisotools-1.1.a/TEiso/Bowtie_build.py @ 13:feef9a0db09d draft
Uploaded
author | urgi-team |
---|---|
date | Wed, 20 Jul 2016 09:04:42 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
12:22b0494ec883 | 13:feef9a0db09d |
---|---|
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 import os | |
33 from commons.core.checker.CheckerUtils import CheckerUtils | |
34 from commons.core.utils.RepetOptionParser import RepetOptionParser | |
35 from commons.core.utils.FileUtils import FileUtils | |
36 import subprocess | |
37 | |
38 class Bowtie_build(object): | |
39 | |
40 def __init__(self, input_reference = "", output_prefix= "", workingDir = "", verbosity = 3): | |
41 | |
42 self._inputRef = input_reference | |
43 self._outPrefix = output_prefix | |
44 self._outputDir = workingDir | |
45 self._verbosity = verbosity | |
46 | |
47 def setAttributesFromCmdLine(self): | |
48 description = "Create index for refrence file with bowtie-build.\n" | |
49 usage = "Bowtie_build -i <reference_in> -o <ebwt_outfile_base>\n" | |
50 print usage | |
51 parser = RepetOptionParser(description = description, usage = usage) | |
52 parser.add_option( '-i', '--input_reference', dest='input_reference', help='comma-separated list of files with ref sequences' ) | |
53 parser.add_option( '-p', '--output_prefix', dest='output_prefix', help=' write Ebwt data to files with this dir/basename' ) | |
54 parser.add_option( '-o', '--outputDir', dest='outputDir', help='result directory name', default = "") | |
55 options, args = parser.parse_args() | |
56 self.setAttributesFromOptions(options) | |
57 | |
58 def setAttributesFromOptions(self, options): | |
59 self._inputRef = options.input_reference | |
60 self._outPrefix = options.output_prefix | |
61 self._outputDir = options.outputDir | |
62 | |
63 def checkExecutables(self): | |
64 if not CheckerUtils.isExecutableInUserPath("bowtie2-build"): | |
65 raise Exception("ERROR: bowtie2-build must be in your path") | |
66 | |
67 def checkOptions(self): | |
68 if self._inputRef != "": | |
69 if not FileUtils.isRessourceExists(self._inputRef): | |
70 raise Exception("ERROR: reference file %s does not exist!" % self._inputRef) | |
71 else: | |
72 raise Exception("ERROR: No specified -i option!") | |
73 | |
74 if self._outPrefix == "": | |
75 raise Exception("ERROR: No specified -o option!") | |
76 | |
77 | |
78 def getBowtie2buildCmd(self, inputRef, outPrefix ): | |
79 cmd = 'bowtie2-build %s %s' % (inputRef, self._outPrefix ) | |
80 #print cmd | |
81 return cmd | |
82 | |
83 def run(self): | |
84 | |
85 self.checkExecutables() | |
86 self.checkOptions() | |
87 try: | |
88 workingDir = self._outputDir | |
89 if os.path.exists(workingDir): | |
90 raise Exception("ERROR: %s already exists." % workingDir) | |
91 os.mkdir(workingDir) | |
92 os.chdir(workingDir) | |
93 os.symlink("%s" % self._inputRef,"%s.fa" % self._outPrefix) | |
94 cmd_bowtie = self.getBowtie2buildCmd(self._inputRef, self._outPrefix) | |
95 ## hide output of subprocess: stdout = index_dir_stderr | |
96 fstdout = open( "bowtie2_build.log" , 'w' ) | |
97 process = subprocess.Popen(cmd_bowtie, shell = True, stdout = fstdout, stderr=subprocess.STDOUT) | |
98 returncode = process.wait() | |
99 fstdout.close() | |
100 # get stderr, allowing for case where it's very large | |
101 fstdout = open("bowtie2_build.log", 'rb' ) | |
102 stderr = '' | |
103 buffsize = 1048576 | |
104 try: | |
105 while True: | |
106 stderr += fstdout.read( buffsize ) | |
107 if not stderr or len( stderr ) % buffsize != 0: | |
108 break | |
109 except OverflowError: | |
110 pass | |
111 fstdout.close() | |
112 if returncode != 0: | |
113 raise Exception, stderr | |
114 except Exception: | |
115 raise Exception("ERROR in %s " % cmd_bowtie) | |
116 | |
117 | |
118 if __name__ == "__main__": | |
119 print "heloo" | |
120 iLaunch = Bowtie_build() | |
121 iLaunch.setAttributesFromCmdLine() | |
122 iLaunch.run() | |
123 |