annotate TEisotools-1.0/TEiso/Cufflinks.py @ 6:20ec0d14798e draft

Uploaded
author urgi-team
date Wed, 20 Jul 2016 05:00:24 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
1 #!/usr/bin/env python
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
2
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
3 # Copyright INRA (Institut National de la Recherche Agronomique)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
4 # http://www.inra.fr
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
5 # http://urgi.versailles.inra.fr
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
6 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
7 # This software is governed by the CeCILL license under French law and
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
8 # abiding by the rules of distribution of free software. You can use,
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
9 # modify and/ or redistribute the software under the terms of the CeCILL
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
10 # license as circulated by CEA, CNRS and INRIA at the following URL
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
11 # "http://www.cecill.info".
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
12 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
13 # As a counterpart to the access to the source code and rights to copy,
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
14 # modify and redistribute granted by the license, users are provided only
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
15 # with a limited warranty and the software's author, the holder of the
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
16 # economic rights, and the successive licensors have only limited
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
17 # liability.
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
18 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
19 # In this respect, the user's attention is drawn to the risks associated
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
20 # with loading, using, modifying and/or developing or reproducing the
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
21 # software by the user in light of its specific status of free software,
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
22 # that may mean that it is complicated to manipulate, and that also
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
23 # therefore means that it is reserved for developers and experienced
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
24 # professionals having in-depth computer knowledge. Users are therefore
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
25 # encouraged to load and test the software's suitability as regards their
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
26 # requirements in conditions enabling the security of their systems and/or
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
27 # data to be ensured and, more generally, to use and operate it in the
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
28 # same conditions as regards security.
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
29 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
30 # The fact that you are presently reading this means that you have had
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
31 # knowledge of the CeCILL license and that you accept its terms.
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
32
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
33 import os, sys
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
34 from commons.core.checker.CheckerUtils import CheckerUtils
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
35 from commons.core.utils.RepetOptionParser import RepetOptionParser
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
36 from commons.core.utils.FileUtils import FileUtils
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
37 import subprocess
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
38
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
39 class Cufflinks(object):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
40
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
41 def __init__(self, input_mapped = "", input_transcripts = "", workingDir = "", verbosity = 3):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
42 self._input_mapped = input_mapped
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
43 self._transcripts = input_transcripts
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
44 self._output_dir = workingDir
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
45 self._verbosity = verbosity
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
46
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
47 def setAttributesFromCmdLine(self):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
48 description = "It accepts aligned RNA-Seq reads and assembles the alignments into a parsimonious set of transcripts."
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
49 usage = "Cufflinks.py -i <hits.bam> -g <transcripts.gtf> -o <output-dir>\n"
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
50 parser = RepetOptionParser(description = description, usage = usage)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
51 parser.add_option( '-i', '--input_mapped', dest='input_mapped', help='aligned RNA-Seq reads' )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
52 parser.add_option( '-g' , '--input_transcripts', dest='input_transcripts', help='GTF/GFF with known transcripts' , default="" )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
53 parser.add_option( '-o', '--output_dir', dest='output_dir', help='write all output files to this directory', default = "")
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
54 options, args = parser.parse_args()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
55 self.setAttributesFromOptions(options)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
56
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
57 def setAttributesFromOptions(self, options):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
58 self._input_mapped = options.input_mapped
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
59 self._transcripts = options.input_transcripts
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
60 self._output_dir = options.output_dir
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
61
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
62 def checkExecutables(self):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
63 if not CheckerUtils.isExecutableInUserPath("cufflinks"):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
64 raise Exception("ERROR: cufflinks must be in your path")
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
65
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
66 def checkOptions(self):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
67 if self._input_mapped != "":
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
68 if not FileUtils.isRessourceExists(self._input_mapped):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
69 raise Exception("ERROR: reference file %s does not exist!" % self._input_mapped)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
70 else:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
71 raise Exception("ERROR: No specified -i option!")
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
72
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
73 if self._transcripts != "" :
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
74 if not FileUtils.isRessourceExists(self._input_mapped):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
75 raise Exception("ERROR: reference file %s does not exist!" % self._transcripts)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
76
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
77
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
78 def getCufflinksCmd(self, mapped, transcripts, output_dir ):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
79 if self._transcripts != "" :
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
80 cmd = 'cufflinks %s -g %s -o %s' % (mapped, transcripts , output_dir)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
81 else:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
82 cmd = 'cufflinks %s -o %s' % (mapped , output_dir)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
83 # print cmd
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
84 return cmd
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
85
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
86 def run(self):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
87 self.checkExecutables()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
88 self.checkOptions()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
89 try:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
90 workingDir = self._output_dir
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
91 if os.path.exists(workingDir):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
92 print "ERROR: %s already exists." % workingDir
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
93 sys.exit(1)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
94 raise Exception("ERROR: %s already exists." % workingDir)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
95 cmd_cufflinks = self.getCufflinksCmd(self._input_mapped, self._transcripts, self._output_dir)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
96 ## hide output of subprocess: stdout = index_dir_stderr
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
97 fstdout = open( "cufflinks.log" , 'w' )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
98 process = subprocess.Popen(cmd_cufflinks, shell = True, stdout = fstdout, stderr=subprocess.STDOUT)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
99 returncode = process.wait()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
100 fstdout.close()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
101 # get stderr, allowing for case where it's very large
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
102 fstdout = open("cufflinks.log", 'rb' )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
103 stderr = ''
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
104 buffsize = 1048576
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
105 try:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
106 while True:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
107 stderr += fstdout.read( buffsize )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
108 if not stderr or len( stderr ) % buffsize != 0:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
109 break
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
110 except OverflowError:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
111 pass
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
112 fstdout.close()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
113 if returncode != 0:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
114 raise Exception, stderr
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
115 os.system("mv cufflinks.log %s/cufflinks.log " % workingDir)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
116 except Exception:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
117 raise Exception("ERROR in %s " % cmd_cufflinks)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
118
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
119
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
120 if __name__ == "__main__":
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
121 iLaunch = Cufflinks()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
122 iLaunch.setAttributesFromCmdLine()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
123 iLaunch.run()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
124