annotate TEisotools-1.0/TEiso/CufflinksGTFToBed.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 from commons.core.LoggerFactory import LoggerFactory
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
34 from commons.core.utils.RepetOptionParser import RepetOptionParser
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
35 from commons.core.checker.RepetException import RepetException
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
36 from commons.core.utils.FileUtils import FileUtils
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
37 from commons.core.parsing.GtfParser import GtfParser
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
38 import os
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
39 LOG_NAME = "TEiso"
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
40
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
41 class CufflinksGTFToBed(object):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
42
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
43 def __init__(self, inputFile = "", outputFile = "", verbosity = 3):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
44 self._inputFile = inputFile
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
45 self._outputFile = outputFile
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
46 self._verbosity = verbosity
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
47 self._log = LoggerFactory.createLogger("%s.%s" % (LOG_NAME, self.__class__.__name__), self._verbosity)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
48
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
49 def setAttributesFromCmdLine(self):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
50 self._toolVersion = "1.0"
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
51 description = "CufflinksGTFToBed version %s" % self._toolVersion
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
52 epilog = "\n parses a GTF file of Cufflinks and create a bed file. \n"
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
53 epilog += "example: CufflinksGTFToBed.py -i <inputFile> -o <outputFile>\n"
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
54 parser = RepetOptionParser(description = description, epilog = epilog, version = self._toolVersion)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
55 parser.add_option("-i", "--inputFile", dest = "inputFile", action = "store", type = "string", help = "Input GTF File name (transcript.gtf of Cufflinks).", default = "")
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
56 parser.add_option("-o", "--outputFile", dest = "outputFile", action = "store", type = "string", help = "output Bed File name", default = "")
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
57 parser.add_option("-v", "--verbosity", dest = "verbosity", action = "store", type = "int", help = "Verbosity [optional] [default: 3]",default = 3)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
58 options = parser.parse_args()[0]
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
59 self._setAttributesFromOptions(options)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
60
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
61 def _setAttributesFromOptions(self, options):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
62 self._inputFile = options.inputFile
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
63 self._outputFile = options.outputFile
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
64 self._verbosity = options.verbosity
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
65
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
66 def _logAndRaise(self, errorMsg):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
67 self._log.error(errorMsg)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
68 raise RepetException(errorMsg)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
69
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
70 def checkoption(self):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
71 if self._outputFile == "":
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
72 #self._log.info("Missing output file destination")
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
73 self._outputFile = "%s.bed" % os.path.splitext(self._inputFile)[0]
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
74 else:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
75 if FileUtils.isRessourceExists(self._outputFile):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
76 self._log.info("Output file '%s' already exists!" % self._outputFile)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
77
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
78 if self._inputFile == "":
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
79 self._log.info("Missing input file")
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
80
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
81 def getTranscriptToBed (self, inputFile ,outputFile):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
82 try:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
83 filewrite=open(outputFile, "w")
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
84 gtfParser = GtfParser(self._inputFile, assemblyTools=True)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
85 for transcript in gtfParser.getIterator():
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
86 if(transcript.getDirection()==1):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
87 strand="+"
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
88 else:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
89 strand="-"
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
90 filewrite.write("%s\t%s\t%s\t%s\t%s\t%s\t%.3f\n" % (transcript.getChromosome(),transcript.getStart(),
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
91 transcript.getEnd(), transcript.getTagValue("ID"), transcript.getTagValue("gene_id"),
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
92 strand,float(transcript.getTagValue("FPKM")) ))
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
93 filewrite.close()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
94 except:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
95 raise Exception("Couldn't open %s for writing" % outputFile)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
96
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
97 def run(self):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
98 self.checkoption()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
99 self.getTranscriptToBed(self._inputFile, self._outputFile)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
100
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
101
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
102 if __name__== "__main__":
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
103 iTranscriptToBed = CufflinksGTFToBed()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
104 iTranscriptToBed.setAttributesFromCmdLine()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
105 iTranscriptToBed.run()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
106
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
107