annotate TEisotools-1.1.a/commons/core/parsing/GffParser.py @ 13:feef9a0db09d draft

Uploaded
author urgi-team
date Wed, 20 Jul 2016 09:04:42 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
13
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
1 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
2 # Copyright INRA-URGI 2009-2010
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
3 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
4 # This software is governed by the CeCILL license under French law and
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
5 # abiding by the rules of distribution of free software. You can use,
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
6 # modify and/ or redistribute the software under the terms of the CeCILL
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
7 # license as circulated by CEA, CNRS and INRIA at the following URL
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
8 # "http://www.cecill.info".
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
9 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
10 # As a counterpart to the access to the source code and rights to copy,
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
11 # modify and redistribute granted by the license, users are provided only
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
12 # with a limited warranty and the software's author, the holder of the
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
13 # economic rights, and the successive licensors have only limited
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
14 # liability.
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
15 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
16 # In this respect, the user's attention is drawn to the risks associated
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
17 # with loading, using, modifying and/or developing or reproducing the
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
18 # software by the user in light of its specific status of free software,
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
19 # that may mean that it is complicated to manipulate, and that also
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
20 # therefore means that it is reserved for developers and experienced
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
21 # professionals having in-depth computer knowledge. Users are therefore
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
22 # encouraged to load and test the software's suitability as regards their
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
23 # requirements in conditions enabling the security of their systems and/or
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
24 # data to be ensured and, more generally, to use and operate it in the
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
25 # same conditions as regards security.
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
26 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
27 # The fact that you are presently reading this means that you have had
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
28 # knowledge of the CeCILL license and that you accept its terms.
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
29 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
30 import re
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
31 import sys
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
32 from SMART.Java.Python.structure.Interval import Interval
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
33 from SMART.Java.Python.structure.Transcript import Transcript
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
34 from commons.core.parsing.TranscriptListParser import TranscriptListParser
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
35
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
36
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
37 class GffParser(TranscriptListParser):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
38 """A class that parses a GFF file and create a transcript list"""
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
39
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
40
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
41 def __init__(self, fileName, verbosity = 0):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
42 super(GffParser, self).__init__(fileName, verbosity)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
43
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
44
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
45 def __del__(self):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
46 super(GffParser, self).__del__()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
47
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
48
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
49 def getFileFormats():
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
50 return ["gff", "gff2", "gff3"]
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
51 getFileFormats = staticmethod(getFileFormats)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
52
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
53
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
54 def skipFirstLines(self):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
55 pass
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
56
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
57
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
58 def getInfos(self):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
59 self.chromosomes = set()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
60 self.nbTranscripts = 0
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
61 self.size = 0
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
62 self.reset()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
63 if self.verbosity >= 10:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
64 print "Getting information on %s." % (self.fileName)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
65 self.reset()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
66 for line in self.handle:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
67 line = line.strip()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
68 if line == "" or line[0] == "#":
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
69 continue
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
70 parts = line.split("\t")
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
71 if len(parts) != 9:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
72 raise Exception("Error! Line '%s' has %d tab-separated fields instead of 9!" % (line, len(parts)))
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
73 self.chromosomes.add(parts[0])
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
74 if parts[8].find("Parent") == -1:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
75 self.nbTranscripts += 1
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
76 else:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
77 self.size += max(int(parts[3]), int(parts[4])) - min(int(parts[3]), int(parts[4])) + 1
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
78 if self.verbosity >= 10 and self.nbTranscripts % 100000 == 0:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
79 sys.stdout.write(" %d transcripts read\r" % (self.nbTranscripts))
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
80 sys.stdout.flush()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
81 self.reset()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
82 if self.verbosity >= 10:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
83 print " %d transcripts read" % (self.nbTranscripts)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
84 print "Done."
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
85
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
86
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
87 def parseLine(self, line):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
88 if not line or line[0] == "#":
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
89 return None
feef9a0db09d 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)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
91 if m == None:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
92 raise Exception("\nLine %d '%s' does not have a GFF format\n" % (self.currentLineNb, line))
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
93 interval = Interval()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
94 interval.setChromosome(m.group(1))
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
95 interval.setName("unnamed transcript")
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
96 interval.setStart(min(int(m.group(4)), int(m.group(5))))
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
97 interval.setEnd(max(int(m.group(4)), int(m.group(5))))
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
98 if m.group(7) == ".":
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
99 interval.setDirection("+")
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
100 else:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
101 interval.setDirection(m.group(7))
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
102 interval.setTagValue("feature", m.group(3))
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
103 if m.group(6).isdigit():
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
104 interval.setTagValue("score", m.group(6))
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
105
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
106 remainings = m.group(9).split(";")
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
107 for remaining in remainings:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
108 remaining = remaining.strip()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
109 if remaining == "":
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
110 continue
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
111 posSpace = remaining.find(" ")
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
112 posEqual = remaining.find("=")
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
113 if posEqual != -1 and (posEqual < posSpace or posSpace == -1):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
114 parts = remaining.split("=")
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
115 else:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
116 parts = remaining.split()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
117 field = parts[0].strip()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
118 value = " ".join(parts[1:]).strip(" \"")
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
119 if field in ("Name", "name", "Sequence", "TE", "SAT"):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
120 interval.setName(value)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
121 else:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
122 try:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
123 intValue = int(value)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
124 interval.setTagValue(field, intValue)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
125 except ValueError:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
126 interval.setTagValue(field, value)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
127
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
128 self.currentTranscriptAddress = self.previousTranscriptAddress
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
129 if "Parent" in interval.getTagNames():
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
130 if self.currentTranscript == None:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
131 raise Exception("GFF file does not start with a transcript! First line is '%s'." % (line.strip()))
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
132 if interval.getTagValue("Parent") != self.currentTranscript.getTagValue("ID"):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
133 raise Exception("Exon '%s' is not right after its transcript in GFF file!" % (interval))
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
134 self.currentTranscript.addExon(interval)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
135 if interval.name == None:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
136 interval.name = self.currentTranscript.name
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
137 return None
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
138
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
139 if interval is not None and interval.name.startswith("unnamed"):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
140 if "ID" in interval.getTagNames():
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
141 interval.name = interval.getTagValue("ID")
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
142 else:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
143 interval.name = "unnamed transcript %s" % (self.currentLineNb)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
144
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
145 transcript = self.currentTranscript
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
146 self.currentTranscript = Transcript()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
147 self.currentTranscript.copy(interval)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
148 self.previousTranscriptAddress = self.currentAddress
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
149 return transcript