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

Uploaded
author urgi-team
date Wed, 20 Jul 2016 05:00:24 -0400
parents
children
comparison
equal deleted inserted replaced
5:4093a2fb58be 6:20ec0d14798e
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, sys
34 from commons.core.checker.CheckerUtils import CheckerUtils
35 from commons.core.utils.RepetOptionParser import RepetOptionParser
36 from commons.core.utils.FileUtils import FileUtils
37 import subprocess
38
39 class Cufflinks(object):
40
41 def __init__(self, input_mapped = "", input_transcripts = "", workingDir = "", verbosity = 3):
42 self._input_mapped = input_mapped
43 self._transcripts = input_transcripts
44 self._output_dir = workingDir
45 self._verbosity = verbosity
46
47 def setAttributesFromCmdLine(self):
48 description = "It accepts aligned RNA-Seq reads and assembles the alignments into a parsimonious set of transcripts."
49 usage = "Cufflinks.py -i <hits.bam> -g <transcripts.gtf> -o <output-dir>\n"
50 parser = RepetOptionParser(description = description, usage = usage)
51 parser.add_option( '-i', '--input_mapped', dest='input_mapped', help='aligned RNA-Seq reads' )
52 parser.add_option( '-g' , '--input_transcripts', dest='input_transcripts', help='GTF/GFF with known transcripts' , default="" )
53 parser.add_option( '-o', '--output_dir', dest='output_dir', help='write all output files to this directory', default = "")
54 options, args = parser.parse_args()
55 self.setAttributesFromOptions(options)
56
57 def setAttributesFromOptions(self, options):
58 self._input_mapped = options.input_mapped
59 self._transcripts = options.input_transcripts
60 self._output_dir = options.output_dir
61
62 def checkExecutables(self):
63 if not CheckerUtils.isExecutableInUserPath("cufflinks"):
64 raise Exception("ERROR: cufflinks must be in your path")
65
66 def checkOptions(self):
67 if self._input_mapped != "":
68 if not FileUtils.isRessourceExists(self._input_mapped):
69 raise Exception("ERROR: reference file %s does not exist!" % self._input_mapped)
70 else:
71 raise Exception("ERROR: No specified -i option!")
72
73 if self._transcripts != "" :
74 if not FileUtils.isRessourceExists(self._input_mapped):
75 raise Exception("ERROR: reference file %s does not exist!" % self._transcripts)
76
77
78 def getCufflinksCmd(self, mapped, transcripts, output_dir ):
79 if self._transcripts != "" :
80 cmd = 'cufflinks %s -g %s -o %s' % (mapped, transcripts , output_dir)
81 else:
82 cmd = 'cufflinks %s -o %s' % (mapped , output_dir)
83 # print cmd
84 return cmd
85
86 def run(self):
87 self.checkExecutables()
88 self.checkOptions()
89 try:
90 workingDir = self._output_dir
91 if os.path.exists(workingDir):
92 print "ERROR: %s already exists." % workingDir
93 sys.exit(1)
94 raise Exception("ERROR: %s already exists." % workingDir)
95 cmd_cufflinks = self.getCufflinksCmd(self._input_mapped, self._transcripts, self._output_dir)
96 ## hide output of subprocess: stdout = index_dir_stderr
97 fstdout = open( "cufflinks.log" , 'w' )
98 process = subprocess.Popen(cmd_cufflinks, shell = True, stdout = fstdout, stderr=subprocess.STDOUT)
99 returncode = process.wait()
100 fstdout.close()
101 # get stderr, allowing for case where it's very large
102 fstdout = open("cufflinks.log", 'rb' )
103 stderr = ''
104 buffsize = 1048576
105 try:
106 while True:
107 stderr += fstdout.read( buffsize )
108 if not stderr or len( stderr ) % buffsize != 0:
109 break
110 except OverflowError:
111 pass
112 fstdout.close()
113 if returncode != 0:
114 raise Exception, stderr
115 os.system("mv cufflinks.log %s/cufflinks.log " % workingDir)
116 except Exception:
117 raise Exception("ERROR in %s " % cmd_cufflinks)
118
119
120 if __name__ == "__main__":
121 iLaunch = Cufflinks()
122 iLaunch.setAttributesFromCmdLine()
123 iLaunch.run()
124