annotate commons/core/parsing/BlatToGff.py @ 58:5f5c9b74c2dd

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