comparison commons/core/parsing/BlatToGff.py @ 36:44d5973c188c

Uploaded
author m-zytnicki
date Tue, 30 Apr 2013 15:02:29 -0400
parents 769e306b7933
children
comparison
equal deleted inserted replaced
35:d94018ca4ada 36:44d5973c188c
1 # Copyright INRA (Institut National de la Recherche Agronomique)
2 # http://www.inra.fr
3 # http://urgi.versailles.inra.fr
4 #
5 # This software is governed by the CeCILL license under French law and
6 # abiding by the rules of distribution of free software. You can use,
7 # modify and/ or redistribute the software under the terms of the CeCILL
8 # license as circulated by CEA, CNRS and INRIA at the following URL
9 # "http://www.cecill.info".
10 #
11 # As a counterpart to the access to the source code and rights to copy,
12 # modify and redistribute granted by the license, users are provided only
13 # with a limited warranty and the software's author, the holder of the
14 # economic rights, and the successive licensors have only limited
15 # liability.
16 #
17 # In this respect, the user's attention is drawn to the risks associated
18 # with loading, using, modifying and/or developing or reproducing the
19 # software by the user in light of its specific status of free software,
20 # that may mean that it is complicated to manipulate, and that also
21 # therefore means that it is reserved for developers and experienced
22 # professionals having in-depth computer knowledge. Users are therefore
23 # encouraged to load and test the software's suitability as regards their
24 # requirements in conditions enabling the security of their systems and/or
25 # data to be ensured and, more generally, to use and operate it in the
26 # same conditions as regards security.
27 #
28 # The fact that you are presently reading this means that you have had
29 # knowledge of the CeCILL license and that you accept its terms.
30
31 import optparse
32 import os
33 from commons.core.parsing.BlatParser import BlatParser
34
35 class BlatToGff(object):
36
37
38 def __init__(self):
39 pass
40
41 def setAttributesFromCmdLine(self):
42 help = '\
43 \nThis Script Launch BlatToGff.\n\n\
44 Example 1: python BlatToGff.py -i blatResultsFile.tab -o outputFile.gff3\n\n'
45 parser = optparse.OptionParser(usage= help, version="CovertSamToFastq.py v1.0")
46 parser.add_option( '-i', '--input', dest='inputBLAT', help='Blat Input File Name [Format: tabular]', default= None )
47 parser.add_option( '-o', '--output', dest='output', help='Output File Name [Format: GFF3]', default= None )
48 parser.add_option( '-n', '--methodname', dest='methodName', help='Method name in col. 3 [Default: None]', default= None )
49 ( options, args ) = parser.parse_args()
50 self._options = options
51
52 def checkOptions(self):
53 if self._options.inputBLAT == '':
54 raise Exception("ERROR: No Blat file specified for -i !")
55 elif not os.path.exists(self._options.inputBLAT):
56 raise Exception("ERROR: Blat Input File doesn't exist !")
57 else:
58 self._inputFileBlat = self._options.inputBLAT
59
60 if self._options.output == '':
61 raise Exception("ERROR: No Output file specified for -o !")
62 else:
63 self._outputFileGFF = self._options.output
64
65 self._methodName = self._options.methodName
66
67 def run(self):
68 self.checkOptions()
69 self._createGFFOutputFile()
70 BLATFile = open(self._inputFileBlat, 'r')
71
72 headerBlatLine = BLATFile.readline()
73 headerBlatLine = BLATFile.readline()
74 headerBlatLine = BLATFile.readline()
75 headerBlatLine = BLATFile.readline()
76 headerBlatLine = BLATFile.readline()
77 blatLine = BLATFile.readline()
78 numberLine = 6
79 while blatLine != '':
80 gffLine = self.convertBlatObjectToGffLine(blatLine, numberLine)
81 self._printGFFLinesToOutputFile(gffLine)
82 blatLine = BLATFile.readline()
83 numberLine = numberLine + 1
84
85 def convertBlatObjectToGffLine(self, blatLine, numberLine):
86 iBlatHit = BlatParser()
87 iBlatHit.setAttributesFromString(blatLine, numberLine)
88 col1 = iBlatHit.getTName()
89 col2 = 'BlatToGff'
90 if self._methodName == '' or self._methodName == None:
91 col3 = 'BES'
92 else:
93 col3 = '%s:BES' % self._methodName
94 col4 = iBlatHit.getTStart()
95 col5 = iBlatHit.getTEnd()
96 col6 = '.'
97 col7 = '+'
98 col8 = '.'
99 col9 = 'ID=%s;Name=%s;bes_start=%s;bes_end=%s;bes_size=%s' % (iBlatHit.getQName(), iBlatHit.getQName(), iBlatHit.getTStart(), iBlatHit.getTEnd(), iBlatHit.getTSize())
100 gffLine = '%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n' % (col1, col2, col3, col4, col5, col6, col7, col8, col9)
101 return gffLine
102
103 def _createGFFOutputFile(self):
104 GFFfile = open(self._outputFileGFF, 'w')
105 GFFfile.write("##gff-version 3\n")
106 GFFfile.close()
107
108 def _printGFFLinesToOutputFile(self, line):
109 GFFfile = open(self._outputFileGFF, 'a')
110 GFFfile.write(line)
111 GFFfile.close()
112
113 if __name__ == '__main__':
114 iBlatToGff = BlatToGff()
115 iBlatToGff.setAttributesFromCmdLine()
116 iBlatToGff.run()