comparison TEisotools-1.1.a/TEiso/Cuffcompare.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
33 import os
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 Cuffcompare(object):
40
41 def __init__(self, reference = "", transcripts = "", outprefix ="" , workingDir = "", verbosity = 3):
42 self._reference = reference
43 self._transcripts = transcripts
44 self._outprefix = outprefix
45 self._output_Dir = workingDir
46 self._verbosity = verbosity
47
48 def setAttributesFromCmdLine(self):
49 description = "Cuffcompare provides classification, reference annotation mapping and various statistics for Cufflinks transfrags.\n"
50 usage = " Cuffcompare -r <reference> -i <transcripts> -o <outprefix>\n"
51 parser = RepetOptionParser(description = description, usage = usage)
52 parser.add_option( '-r', '--reference', dest='reference', help='a set of known mRNAs to use as a reference for assessing the accuracy of mRNAs or gene models given in <input.gtf>' )
53 parser.add_option( '-i', '--transcripts', dest='transcripts', help='input transfrags' )
54 parser.add_option( '-o', '--outprefix', dest='outprefix', help='write all output files with out prefix', default = "cuffcompare")
55 options, args = parser.parse_args()
56 self.setAttributesFromOptions(options)
57
58 def setAttributesFromOptions(self, options):
59 self._reference = options.reference
60 self._transcripts = options.transcripts
61 self._outprefix = options.outprefix
62
63 def checkExecutables(self):
64 if not CheckerUtils.isExecutableInUserPath("cuffcompare"):
65 raise Exception("ERROR: cuffcompare must be in your path")
66
67 def checkOptions(self):
68 if self._transcripts != "":
69 if not FileUtils.isRessourceExists(self._transcripts):
70 raise Exception("ERROR: input file %s does not exist!" % self._transcripts)
71 else:
72 raise Exception("ERROR: No specified -i option!")
73
74 if self._reference != "":
75 if not FileUtils.isRessourceExists(self._reference):
76 raise Exception("ERROR: reference file %s does not exist!" % self._reference)
77 else:
78 raise Exception("ERROR: No specified -r option!")
79
80 def getCuffcompareCmd(self, reference, transcripts, outprefix):
81 cmd = 'cuffcompare -R -C -r %s %s -o %s' % (reference, transcripts, outprefix)
82 ##print cmd
83 return cmd
84
85 def run(self):
86 self.checkExecutables()
87 self.checkOptions()
88 try:
89 workingDir = self._output_Dir
90 if os.path.exists(workingDir):
91 raise Exception("ERROR: %s already exists." % workingDir)
92 cmd_cuffcompare = self.getCuffcompareCmd(self._reference, self._transcripts, self._outprefix)
93 ## hide output of subprocess: stdout = index_dir_stderr
94 fstdout = open( "cuffcompare.log" , 'w' )
95 process = subprocess.Popen(cmd_cuffcompare, shell = True, stdout = fstdout, stderr=subprocess.STDOUT)
96 returncode = process.wait()
97 fstdout.close()
98 # get stderr, allowing for case where it's very large
99 fstdout = open("cuffcompare.log", 'rb' )
100 stderr = ''
101 buffsize = 1048576
102 try:
103 while True:
104 stderr += fstdout.read( buffsize )
105 if not stderr or len( stderr ) % buffsize != 0:
106 break
107 except OverflowError:
108 pass
109 fstdout.close()
110 if returncode != 0:
111 raise Exception, stderr
112
113 if not os.path.exists(workingDir):
114 os.mkdir(workingDir)
115 ##os.system("mv cuffcompare.log %s/cuffcompare.log " % workingDir)
116 os.system("mv cuffcompare.* %s" % workingDir)
117 except Exception:
118 raise Exception("ERROR in %s " % cmd_cuffcompare)
119
120
121 if __name__ == "__main__":
122 iLaunch = Cuffcompare()
123 iLaunch.setAttributesFromCmdLine()
124 iLaunch.run()
125