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