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