Mercurial > repos > yufei-luo > s_mart
comparison commons/core/parsing/BedParser.py @ 6:769e306b7933
Change the repository level.
| author | yufei-luo |
|---|---|
| date | Fri, 18 Jan 2013 04:54:14 -0500 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 5:ea3082881bf8 | 6:769e306b7933 |
|---|---|
| 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 commons.core.parsing.TranscriptListParser import TranscriptListParser | |
| 34 from SMART.Java.Python.structure.Transcript import Transcript | |
| 35 | |
| 36 | |
| 37 class BedParser(TranscriptListParser): | |
| 38 """A class that parses a BED file and create a transcript list""" | |
| 39 | |
| 40 | |
| 41 def __init__(self, fileName, verbosity = 0): | |
| 42 self.title = None | |
| 43 TranscriptListParser.__init__(self, fileName, verbosity) | |
| 44 | |
| 45 | |
| 46 # def __del__(self): | |
| 47 # super(BedParser, self).__del__() | |
| 48 | |
| 49 | |
| 50 def getFileFormats(): | |
| 51 return ["bed"] | |
| 52 getFileFormats = staticmethod(getFileFormats) | |
| 53 | |
| 54 | |
| 55 def skipFirstLines(self): | |
| 56 mark = self.handle.tell() | |
| 57 line = self.handle.readline() | |
| 58 line = line.strip() | |
| 59 m = re.search(r"^\s*track\s+name\s*=\s*(\S+)\s+", line) | |
| 60 if m != None: | |
| 61 self.title = m.group(1) | |
| 62 self.currentLineNb += 1 | |
| 63 else: | |
| 64 self.handle.seek(mark) | |
| 65 return | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 def parseLine(self, line): | |
| 71 m = re.search(r"^\s*(\S+)\t+(\d+)\t+(\d+)\s*$", line) | |
| 72 if m != None: | |
| 73 transcript = Transcript() | |
| 74 transcript.setChromosome(m.group(1)) | |
| 75 transcript.setStart(min(int(m.group(2)), int(m.group(3))-1)) | |
| 76 transcript.setEnd(max(int(m.group(2)), int(m.group(3))-1)) | |
| 77 transcript.setName("Unnamed") | |
| 78 transcript.setDirection(1) | |
| 79 return transcript | |
| 80 | |
| 81 m = re.search(r"^\s*(\S+)\t+(\d+)\t+(\d+)\t+([^\t]+)\s*$", line) | |
| 82 if m != None: | |
| 83 transcript = Transcript() | |
| 84 transcript.setChromosome(m.group(1)) | |
| 85 transcript.setStart(min(int(m.group(2)), int(m.group(3))-1)) | |
| 86 transcript.setEnd(max(int(m.group(2)), int(m.group(3))-1)) | |
| 87 transcript.setName(m.group(4)) | |
| 88 transcript.setDirection(1) | |
| 89 return transcript | |
| 90 | |
| 91 m = re.search(r"^\s*(\S+)\t+(\d+)\t+(\d+)\t+([^\t]+)\t+\d+\.?\d*\s*$", line) | |
| 92 if m != None: | |
| 93 transcript = Transcript() | |
| 94 transcript.setChromosome(m.group(1)) | |
| 95 transcript.setStart(min(int(m.group(2)), int(m.group(3))-1)) | |
| 96 transcript.setEnd(max(int(m.group(2)), int(m.group(3))-1)) | |
| 97 transcript.setName(m.group(4)) | |
| 98 transcript.setDirection(1) | |
| 99 return transcript | |
| 100 | |
| 101 m = re.search(r"^\s*(\S+)\t+(\d+)\t+(\d+)\t+([^\t]+)\t+\d+\t+([+-])\t+\d+\t+\d+\t+0\t+(\d+)\t+(\S+)\t+(\S+)\s*$", line) | |
| 102 if m == None: | |
| 103 raise Exception("\nLine %d '%s' does not has a BED format." % (self.currentLineNb, line)) | |
| 104 transcript = Transcript() | |
| 105 transcript.setChromosome(m.group(1)) | |
| 106 transcript.setStart(min(int(m.group(2)), int(m.group(3))-1)) | |
| 107 transcript.setEnd(max(int(m.group(2)), int(m.group(3))-1)) | |
| 108 transcript.setName(m.group(4)) | |
| 109 transcript.setDirection(m.group(5)) | |
| 110 nbExons = int(m.group(6)) | |
| 111 sizes = m.group(7).split(",") | |
| 112 starts = m.group(8).split(",") | |
| 113 | |
| 114 # check for comment in name | |
| 115 m = re.search(r"^([^\(]*)\((\S+)\)$", transcript.getName()) | |
| 116 if m != None: | |
| 117 transcript.setName(m.group(1)) | |
| 118 transcript.setTagValues(m.group(2), ";", "=") | |
| 119 | |
| 120 # check for nb occurrences in name | |
| 121 m = re.search(r"(.*)-(\d+)$", transcript.getName()) | |
| 122 if m != None: | |
| 123 transcript.setName(m.group(1)) | |
| 124 transcript.setOccurrence(int(m.group(2))) | |
| 125 | |
| 126 for i in range(nbExons): | |
| 127 exon = Interval(transcript) | |
| 128 exon.setStart(int(starts[i])+transcript.getStart()) | |
| 129 exon.setEnd(transcript.getStart()+int(starts[i])+int(sizes[i])-1) | |
| 130 exon.setSize(int(sizes[i])) | |
| 131 transcript.addExon(exon) | |
| 132 | |
| 133 if transcript.exons[0].getStart() != transcript.getStart(): | |
| 134 sys.exit("There is something wrong with the start of transcript line '%s': transcript starts at %d whereas first exon starts at %d" % (line.strip(), transcript.start, transcript.exons[0].start)) | |
| 135 if transcript.exons[-1].getEnd() != transcript.getEnd(): | |
| 136 sys.exit("There is something wrong with the end of transcript line '%s': transcript ends at %d whereas last exon ends at %d" % (line.strip(), transcript.end, transcript.exons[-1].end)) | |
| 137 | |
| 138 return transcript | |
| 139 |
