annotate TEisotools-1.1.a/TEiso/Tophat.py @ 15:255c852351c5 draft

Uploaded
author urgi-team
date Thu, 21 Jul 2016 07:36:44 -0400
parents feef9a0db09d
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
13
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
1 #!/usr/bin/env python
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
2
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
3 # Copyright INRA (Institut National de la Recherche Agronomique)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
4 # http://www.inra.fr
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
5 # http://urgi.versailles.inra.fr
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
6 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
7 # This software is governed by the CeCILL license under French law and
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
8 # abiding by the rules of distribution of free software. You can use,
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
9 # modify and/ or redistribute the software under the terms of the CeCILL
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
10 # license as circulated by CEA, CNRS and INRIA at the following URL
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
11 # "http://www.cecill.info".
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
12 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
13 # As a counterpart to the access to the source code and rights to copy,
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
14 # modify and redistribute granted by the license, users are provided only
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
15 # with a limited warranty and the software's author, the holder of the
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
16 # economic rights, and the successive licensors have only limited
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
17 # liability.
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
18 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
19 # In this respect, the user's attention is drawn to the risks associated
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
20 # with loading, using, modifying and/or developing or reproducing the
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
21 # software by the user in light of its specific status of free software,
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
22 # that may mean that it is complicated to manipulate, and that also
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
23 # therefore means that it is reserved for developers and experienced
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
24 # professionals having in-depth computer knowledge. Users are therefore
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
25 # encouraged to load and test the software's suitability as regards their
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
26 # requirements in conditions enabling the security of their systems and/or
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
27 # data to be ensured and, more generally, to use and operate it in the
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
28 # same conditions as regards security.
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
29 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
30 # The fact that you are presently reading this means that you have had
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
31 # knowledge of the CeCILL license and that you accept its terms.
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
32
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
33 import os, glob
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
34 import subprocess
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
35 import time
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
36 from commons.core.checker.CheckerUtils import CheckerUtils
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
37 from commons.core.utils.RepetOptionParser import RepetOptionParser
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
38 from commons.core.LoggerFactory import LoggerFactory
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
39 from commons.core.utils.FileUtils import FileUtils
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
40
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
41 LOG_DEPTH = "repet.RNAseq_pipe"
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
42
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
43
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
44 class Tophat(object):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
45
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
46 def __init__(self, workingDir = "", index_genome = "", single_paired = "", single_read = "", left_read ="", right_read = "", verbosity = 3):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
47 #self._transcripts = input_transcripts
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
48 self._outputDir = workingDir
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
49 self._bowtie_index = index_genome
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
50 self._type = single_paired
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
51 self._single_read = single_read
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
52 self._left_read = left_read
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
53 self._right_read = right_read
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
54 self._verbosity = verbosity
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
55 self._log = LoggerFactory.createLogger("%s.%s" % (LOG_DEPTH, self.__class__.__name__), self._verbosity)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
56 def setAttributesFromCmdLine(self):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
57 description = "TopHat maps short sequences from spliced transcripts to whole genomes..\n"
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
58 usage = "if reads are single:/n Tophat.py -G <GTF/GFF with known transcripts> -b <bowtie_index> -t single -r <single_read> \n"
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
59 usage +="if reads are paired:/n Tophat.py -G <GTF/GFF with known transcripts> -b <bowtie_index> -t paired -r1 <reads_left> -r2 <reads_right>\n"
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
60 parser = RepetOptionParser(description = description, usage = usage)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
61 # parser.add_option( '-G', '--input_transcripts', dest='input_transcripts', help='GTF/GFF with known transcripts', default = "")
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
62 parser.add_option( '-o', '--outputDir', dest='outputDir', help='write all output files to this directory', default = "")
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
63 parser.add_option( '-b', '--index_genome', dest='index_genome', help='Indexing reference genome', default = "")
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
64 parser.add_option( '-e', '--single_paired', dest='single_paired', help='types of input reads', default = "paired")
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
65 parser.add_option( '-s', '--single_read', dest = 'single_read', help='a single input read', default = "" )
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
66 parser.add_option( '-l', '--left_read', dest='left_read', help='left reads', default = "" )
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
67 parser.add_option( '-r', '--right_read', dest='right_read', help='right reads', default = "" )
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
68 options, args = parser.parse_args()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
69 self.setAttributesFromOptions(options)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
70
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
71 def setAttributesFromOptions(self, options):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
72 ## self._transcripts = options.input_transcripts
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
73 self._outputDir = options.outputDir
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
74 self._bowtie_index = options.index_genome
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
75 self._type = options.single_paired
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
76 self._single_read = options.single_read
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
77 self._left_read = options.left_read
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
78 self._right_read = options.right_read
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
79
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
80
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
81 def checkExecutables(self):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
82
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
83 if not CheckerUtils.isExecutableInUserPath("tophat2"):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
84 raise Exception("ERROR: tophat must be in your path")
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
85
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
86 def checkOptions(self):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
87 if self._bowtie_index == "":
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
88 raise Exception("ERROR: No specified -b option!")
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
89
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
90 ## if self._transcripts != "":
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
91 ## if not FileUtils.isRessourceExists(self._transcripts):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
92 ## raise Exception("ERROR: %s does not exist!" % self._transcripts)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
93
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
94 if self._type == "paired":
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
95 for f in self._left_read:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
96 if not FileUtils.isRessourceExists(f):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
97 raise Exception("ERROR: %s does not exist!" % f)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
98 for f in self._right_read:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
99 if not FileUtils.isRessourceExists(f):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
100 raise Exception("ERROR: %s does not exist!" % f)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
101 elif self._type == "single":
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
102 for f in self._single_read:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
103 if not FileUtils.isRessourceExists(f):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
104 raise Exception("ERROR: %s does not exist!" % f)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
105 else:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
106 raise Exception("ERROR: No specified -t option!")
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
107
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
108 def getTophatCmd_single(self, out_tophat, BowtiePrefix, single_read):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
109 cmd = "tophat2 -p 8 -o %s %s %s" % (out_tophat, BowtiePrefix, ",".join(single_read))
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
110 return cmd
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
111
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
112 def getTophatCmd_paired(self, out_tophat, BowtiePrefix, left_read, right_read):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
113 ####sur SGE comme saruman
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
114 #cmd = "echo " + "'tophat -p 8 -o %s ../%s %s %s'" % (out_tophat, prefix, ",".join(left_Read), ",".join(right_Read))+ "|qsub -V -cwd -pe multithread 8"
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
115 cmd = "tophat2 -p 8 -o %s %s %s %s" % (out_tophat, BowtiePrefix, ",".join(left_read), ",".join(right_read))
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
116 #print cmd
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
117 return cmd
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
118
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
119 def run(self):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
120 self.checkExecutables()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
121 self.checkOptions()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
122 try:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
123 if os.path.exists(self._outputDir):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
124 raise Exception("ERROR: %s already exists." % self._outputDir)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
125 if self._type == "single":
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
126 cmd_tophat = self.getTophatCmd_single(self._outputDir, self._bowtie_index, self._single_read)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
127 if self._type == "paired":
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
128 cmd_tophat = self.getTophatCmd_paired(self._outputDir, self._bowtie_index, self._left_read, self._right_read)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
129 #print cmd_tophat
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
130 ## hide output of subprocess: stdout = index_dir_stderr
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
131 fstdout = open( "tophat.log" , 'w' )
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
132 process = subprocess.Popen(cmd_tophat, shell = True, stdout = fstdout, stderr=subprocess.STDOUT)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
133 returncode = process.wait()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
134 fstdout.close()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
135 # get stderr, allowing for case where it's very large
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
136 fstdout = open("tophat.log", 'rb' )
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
137 stderr = ''
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
138 buffsize = 1048576
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
139 try:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
140 while True:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
141 stderr += fstdout.read( buffsize )
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
142 if not stderr or len( stderr ) % buffsize != 0:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
143 break
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
144 except OverflowError:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
145 pass
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
146 fstdout.close()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
147 if returncode != 0:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
148 raise Exception, stderr
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
149
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
150 os.system("mv tophat.log %s/tophat.log " % self._outputDir)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
151 except Exception:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
152 raise Exception("ERROR in %s " % cmd_tophat)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
153
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
154
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
155
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
156
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
157 if __name__ == "__main__":
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
158 iLaunch = Tophat()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
159 iLaunch.setAttributesFromCmdLine()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
160 iLaunch.run()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
161