annotate TEisotools-1.0/commons/core/parsing/GffParser.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 GffParser(TranscriptListParser):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
38 """A class that parses a GFF 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):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
42 super(GffParser, self).__init__(fileName, verbosity)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
43
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
44
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
45 def __del__(self):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
46 super(GffParser, self).__del__()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
47
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
48
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
49 def getFileFormats():
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
50 return ["gff", "gff2", "gff3"]
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
51 getFileFormats = staticmethod(getFileFormats)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
52
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
53
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
54 def skipFirstLines(self):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
55 pass
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
56
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
57
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
58 def getInfos(self):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
59 self.chromosomes = set()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
60 self.nbTranscripts = 0
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
61 self.size = 0
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
62 self.reset()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
63 if self.verbosity >= 10:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
64 print "Getting information on %s." % (self.fileName)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
65 self.reset()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
66 for line in self.handle:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
67 line = line.strip()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
68 if line == "" or line[0] == "#":
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
69 continue
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
70 parts = line.split("\t")
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
71 if len(parts) != 9:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
72 raise Exception("Error! Line '%s' has %d tab-separated fields instead of 9!" % (line, len(parts)))
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
73 self.chromosomes.add(parts[0])
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
74 if parts[8].find("Parent") == -1:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
75 self.nbTranscripts += 1
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
76 else:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
77 self.size += max(int(parts[3]), int(parts[4])) - min(int(parts[3]), int(parts[4])) + 1
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
78 if self.verbosity >= 10 and self.nbTranscripts % 100000 == 0:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
79 sys.stdout.write(" %d transcripts read\r" % (self.nbTranscripts))
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
80 sys.stdout.flush()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
81 self.reset()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
82 if self.verbosity >= 10:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
83 print " %d transcripts read" % (self.nbTranscripts)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
84 print "Done."
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
85
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
86
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
87 def parseLine(self, line):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
88 if not line or line[0] == "#":
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
89 return None
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
90 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
91 if m == None:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
92 raise Exception("\nLine %d '%s' does not have a GFF format\n" % (self.currentLineNb, line))
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
93 interval = Interval()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
94 interval.setChromosome(m.group(1))
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
95 interval.setName("unnamed transcript")
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
96 interval.setStart(min(int(m.group(4)), int(m.group(5))))
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
97 interval.setEnd(max(int(m.group(4)), int(m.group(5))))
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
98 if m.group(7) == ".":
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
99 interval.setDirection("+")
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
100 else:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
101 interval.setDirection(m.group(7))
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
102 interval.setTagValue("feature", m.group(3))
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
103 if m.group(6).isdigit():
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
104 interval.setTagValue("score", m.group(6))
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
105
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
106 remainings = m.group(9).split(";")
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
107 for remaining in remainings:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
108 remaining = remaining.strip()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
109 if remaining == "":
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
110 continue
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
111 posSpace = remaining.find(" ")
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
112 posEqual = remaining.find("=")
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
113 if posEqual != -1 and (posEqual < posSpace or posSpace == -1):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
114 parts = remaining.split("=")
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
115 else:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
116 parts = remaining.split()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
117 field = parts[0].strip()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
118 value = " ".join(parts[1:]).strip(" \"")
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
119 if field in ("Name", "name", "Sequence", "TE", "SAT"):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
120 interval.setName(value)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
121 else:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
122 try:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
123 intValue = int(value)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
124 interval.setTagValue(field, intValue)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
125 except ValueError:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
126 interval.setTagValue(field, value)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
127
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
128 self.currentTranscriptAddress = self.previousTranscriptAddress
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
129 if "Parent" in interval.getTagNames():
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
130 if self.currentTranscript == None:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
131 raise Exception("GFF file does not start with a transcript! First line is '%s'." % (line.strip()))
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
132 if interval.getTagValue("Parent") != self.currentTranscript.getTagValue("ID"):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
133 raise Exception("Exon '%s' is not right after its transcript in GFF file!" % (interval))
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
134 self.currentTranscript.addExon(interval)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
135 if interval.name == None:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
136 interval.name = self.currentTranscript.name
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
137 return None
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
138
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
139 if interval is not None and interval.name.startswith("unnamed"):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
140 if "ID" in interval.getTagNames():
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
141 interval.name = interval.getTagValue("ID")
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
142 else:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
143 interval.name = "unnamed transcript %s" % (self.currentLineNb)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
144
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
145 transcript = self.currentTranscript
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
146 self.currentTranscript = Transcript()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
147 self.currentTranscript.copy(interval)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
148 self.previousTranscriptAddress = self.currentAddress
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
149 return transcript