annotate commons/core/parsing/GffParser.py @ 44:5f796c5c579f

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