annotate TEisotools-1.0/commons/core/parsing/GtfParser.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 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
2 # Copyright INRA-URGI 2009-2010
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
3 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
4 # This software is governed by the CeCILL license under French law and
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
5 # abiding by the rules of distribution of free software. You can use,
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
6 # modify and/ or redistribute the software under the terms of the CeCILL
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
7 # license as circulated by CEA, CNRS and INRIA at the following URL
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
8 # "http://www.cecill.info".
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
9 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
10 # As a counterpart to the access to the source code and rights to copy,
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
11 # modify and redistribute granted by the license, users are provided only
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
12 # with a limited warranty and the software's author, the holder of the
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
13 # economic rights, and the successive licensors have only limited
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
14 # liability.
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
15 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
16 # In this respect, the user's attention is drawn to the risks associated
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
17 # with loading, using, modifying and/or developing or reproducing the
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
18 # software by the user in light of its specific status of free software,
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
19 # that may mean that it is complicated to manipulate, and that also
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
20 # therefore means that it is reserved for developers and experienced
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
21 # professionals having in-depth computer knowledge. Users are therefore
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
22 # encouraged to load and test the software's suitability as regards their
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
23 # requirements in conditions enabling the security of their systems and/or
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
24 # data to be ensured and, more generally, to use and operate it in the
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
25 # same conditions as regards security.
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
26 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
27 # The fact that you are presently reading this means that you have had
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
28 # knowledge of the CeCILL license and that you accept its terms.
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
29 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
30 import re
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
31 import sys
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
32 from SMART.Java.Python.structure.Interval import Interval
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
33 from SMART.Java.Python.structure.Transcript import Transcript
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
34 from commons.core.parsing.TranscriptListParser import TranscriptListParser
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
35
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
36
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
37 class GtfParser(TranscriptListParser):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
38 """A class that parses a GTF file and create a transcript list"""
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
39
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
40
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
41 def __init__(self, fileName, verbosity = 0, assemblyTools=False):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
42 super(GtfParser, self).__init__(fileName, verbosity)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
43 self._assemblyTools=assemblyTools
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
44
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
45
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
46 def __del__(self):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
47 super(GtfParser, self).__del__()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
48
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
49
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
50 def getFileFormats():
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
51 return ["gtf", "gtf2"]
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
52 getFileFormats = staticmethod(getFileFormats)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
53
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
54
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
55 def skipFirstLines(self):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
56 pass
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
57
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
58
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
59 def parseLine(self, line):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
60 if line[0] == "#":
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
61 return None
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
62 m = re.search(r"^\s*(\S+)\s+(\S+)\s+(\S+)\s+(\d+)\s+(\d+)\s+(\S+)\s+([+-.])\s+(\S+)\s+(\S.*)$", line)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
63 if m == None:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
64 raise Exception("\nLine %d '%s' does not have a GTF format\n" % (self.currentLineNb, line))
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
65 if(self._assemblyTools==False):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
66 interval = Interval()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
67 interval.setChromosome(m.group(1))
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
68 interval.setName("unnamed transcript")
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
69 interval.setStart(min(int(m.group(4)), int(m.group(5))))
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
70 interval.setEnd(max(int(m.group(4)), int(m.group(5))))
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
71 if m.group(7) == ".":
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
72 interval.setDirection("+")
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
73 else:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
74 interval.setDirection(m.group(7))
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
75 if m.group(6).isdigit():
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
76 interval.setTagValue("score", m.group(6))
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
77 type = m.group(3)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
78
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
79 if type not in ("transcript", "exon"):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
80 return None
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
81
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
82 remainings = m.group(9).split(";")
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
83 for remaining in remainings:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
84 remaining = remaining.strip()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
85 if remaining == "":
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
86 continue
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
87 parts = remaining.split(" ", 1)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
88 field = parts[0].strip()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
89 value = " ".join(parts[1:]).strip(" \"")
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
90 if field == "transcript_id":
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
91 interval.setTagValue("ID", value)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
92 elif field == "gene_name":
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
93 interval.setName(value)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
94 elif field == "transcript_name":
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
95 interval.setName(value)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
96 elif field == "exon_number":
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
97 continue
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
98 else:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
99 try:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
100 intValue = int(value)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
101 interval.setTagValue(field, intValue)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
102 except ValueError:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
103 interval.setTagValue(field, value)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
104
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
105 self.currentTranscriptAddress = self.previousTranscriptAddress
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
106 if self.currentTranscript == None or interval.getTagValue("ID") != self.currentTranscript.getTagValue("ID"):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
107 transcript = self.currentTranscript
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
108 self.currentTranscript = Transcript()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
109 self.currentTranscript.copy(interval)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
110 self.currentTranscript.setTagValue("feature", "transcript")
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
111 self.previousTranscriptAddress = self.currentAddress
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
112 return transcript
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
113 if type == "exon":
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
114 self.currentTranscript.addExon(interval)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
115 return None
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
116 else:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
117 if m.group(7) != ".":
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
118 interval = Interval()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
119 interval.setChromosome(m.group(1))
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
120 interval.setName("unnamed transcript")
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
121 interval.setStart(min(int(m.group(4)), int(m.group(5))))
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
122 interval.setEnd(max(int(m.group(4)), int(m.group(5))))
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
123 if m.group(7) == ".":
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
124 interval.setDirection("+")
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
125 else:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
126 interval.setDirection(m.group(7))
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
127 if m.group(6).isdigit():
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
128 interval.setTagValue("score", m.group(6))
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
129 type = m.group(3)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
130
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
131 if type not in ("transcript", "exon"):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
132 return None
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
133
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
134 remainings = m.group(9).split(";")
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
135 for remaining in remainings:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
136 remaining = remaining.strip()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
137 if remaining == "":
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
138 continue
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
139 parts = remaining.split(" ", 1)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
140 field = parts[0].strip()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
141 value = " ".join(parts[1:]).strip(" \"")
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
142 if field == "transcript_id":
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
143 interval.setTagValue("ID", value)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
144 elif field == "gene_name":
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
145 interval.setName(value)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
146 elif field == "transcript_name":
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
147 interval.setName(value)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
148 elif field == "exon_number":
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
149 continue
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
150 else:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
151 try:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
152 intValue = int(value)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
153 interval.setTagValue(field, intValue)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
154 except ValueError:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
155 interval.setTagValue(field, value)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
156
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
157 self.currentTranscriptAddress = self.previousTranscriptAddress
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
158 if self.currentTranscript == None or interval.getTagValue("ID") != self.currentTranscript.getTagValue("ID"):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
159 transcript = self.currentTranscript
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
160 self.currentTranscript = Transcript()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
161 self.currentTranscript.copy(interval)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
162 self.currentTranscript.setTagValue("feature", "transcript")
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
163 self.previousTranscriptAddress = self.currentAddress
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
164 return transcript
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
165 if type == "exon":
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
166 self.currentTranscript.addExon(interval)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
167 return None