Mercurial > repos > yufei-luo > s_mart
comparison SMART/Java/Python/cleaning/GffCleaner.py @ 6:769e306b7933
Change the repository level.
| author | yufei-luo |
|---|---|
| date | Fri, 18 Jan 2013 04:54:14 -0500 |
| parents | |
| children | e57682cd6997 |
comparison
equal
deleted
inserted
replaced
| 5:ea3082881bf8 | 6:769e306b7933 |
|---|---|
| 1 #! /usr/bin/env python | |
| 2 # | |
| 3 # Copyright INRA-URGI 2009-2010 | |
| 4 # | |
| 5 # This software is governed by the CeCILL license under French law and | |
| 6 # abiding by the rules of distribution of free software. You can use, | |
| 7 # modify and/ or redistribute the software under the terms of the CeCILL | |
| 8 # license as circulated by CEA, CNRS and INRIA at the following URL | |
| 9 # "http://www.cecill.info". | |
| 10 # | |
| 11 # As a counterpart to the access to the source code and rights to copy, | |
| 12 # modify and redistribute granted by the license, users are provided only | |
| 13 # with a limited warranty and the software's author, the holder of the | |
| 14 # economic rights, and the successive licensors have only limited | |
| 15 # liability. | |
| 16 # | |
| 17 # In this respect, the user's attention is drawn to the risks associated | |
| 18 # with loading, using, modifying and/or developing or reproducing the | |
| 19 # software by the user in light of its specific status of free software, | |
| 20 # that may mean that it is complicated to manipulate, and that also | |
| 21 # therefore means that it is reserved for developers and experienced | |
| 22 # professionals having in-depth computer knowledge. Users are therefore | |
| 23 # encouraged to load and test the software's suitability as regards their | |
| 24 # requirements in conditions enabling the security of their systems and/or | |
| 25 # data to be ensured and, more generally, to use and operate it in the | |
| 26 # same conditions as regards security. | |
| 27 # | |
| 28 # The fact that you are presently reading this means that you have had | |
| 29 # knowledge of the CeCILL license and that you accept its terms. | |
| 30 # | |
| 31 """ | |
| 32 Clean a GFF file (as given by NCBI or TAIR) and outputs a GFF3 file. | |
| 33 """ | |
| 34 | |
| 35 from SMART.Java.Python.cleaning.TranscriptListCleaner import TranscriptListCleaner | |
| 36 from SMART.Java.Python.misc.Progress import Progress | |
| 37 from SMART.Java.Python.misc.UnlimitedProgress import UnlimitedProgress | |
| 38 | |
| 39 count = {} | |
| 40 | |
| 41 class ParsedLine(object): | |
| 42 def __init__(self, line, cpt): | |
| 43 self.line = line | |
| 44 self.cpt = cpt | |
| 45 self.parse() | |
| 46 | |
| 47 def parse(self): | |
| 48 self.line = self.line.strip() | |
| 49 self.splittedLine = self.line.split(None, 8) | |
| 50 if len(self.splittedLine) < 9: | |
| 51 raise Exception("Line '%s' has less than 9 fields. Exiting..." % (self.line)) | |
| 52 self.type = self.splittedLine[2] | |
| 53 self.parseOptions() | |
| 54 self.getId() | |
| 55 self.getParents() | |
| 56 | |
| 57 def parseOptions(self): | |
| 58 self.parsedOptions = {} | |
| 59 for option in self.splittedLine[8].split(";"): | |
| 60 option = option.strip() | |
| 61 if option == "": continue | |
| 62 posSpace = option.find(" ") | |
| 63 posEqual = option.find("=") | |
| 64 if posEqual != -1 and (posEqual < posSpace or posSpace == -1): | |
| 65 key, value = option.split("=", 1) | |
| 66 elif posSpace != -1: | |
| 67 key, value = option.split(None, 1) | |
| 68 else: | |
| 69 key = "ID" | |
| 70 value = option | |
| 71 self.parsedOptions[key.strip()] = value.strip(" \"") | |
| 72 | |
| 73 def getId(self): | |
| 74 for key in self.parsedOptions: | |
| 75 if key.lower() == "id": | |
| 76 self.id = self.parsedOptions[key] | |
| 77 return | |
| 78 if "Parent" in self.parsedOptions: | |
| 79 parent = self.parsedOptions["Parent"].split(",")[0] | |
| 80 if parent not in count: | |
| 81 count[parent] = {} | |
| 82 if self.type not in count[parent]: | |
| 83 count[parent][self.type] = 0 | |
| 84 count[parent][self.type] += 1 | |
| 85 self.id = "%s-%s-%d" % (parent, self.type, count[parent][self.type]) | |
| 86 else: | |
| 87 self.id = "smart%d" % (self.cpt) | |
| 88 self.parsedOptions["ID"] = self.id | |
| 89 | |
| 90 def getParents(self): | |
| 91 for key in self.parsedOptions: | |
| 92 if key.lower() in ("parent", "derives_from"): | |
| 93 self.parents = self.parsedOptions[key].split(",") | |
| 94 return | |
| 95 self.parents = None | |
| 96 | |
| 97 def removeParent(self): | |
| 98 for key in self.parsedOptions.keys(): | |
| 99 if key.lower() in ("parent", "derives_from"): | |
| 100 del self.parsedOptions[key] | |
| 101 | |
| 102 def export(self): | |
| 103 self.splittedLine[8] = ";".join(["%s=%s" % (key, value) for key, value in self.parsedOptions.iteritems()]) | |
| 104 return "%s\n" % ("\t".join(self.splittedLine)) | |
| 105 | |
| 106 | |
| 107 class GffCleaner(TranscriptListCleaner): | |
| 108 | |
| 109 def __init__(self, verbosity = 1): | |
| 110 super(GffCleaner, self).__init__(verbosity) | |
| 111 self.lines = {} | |
| 112 self.acceptedTypes = ["mRNA", "transcript", "exon"] | |
| 113 self.parents = [] | |
| 114 self.children = {} | |
| 115 | |
| 116 def getFileFormats(): | |
| 117 return ["gff", "gff2", "gff3"] | |
| 118 getFileFormats = staticmethod(getFileFormats) | |
| 119 | |
| 120 def setAcceptedTypes(self, types): | |
| 121 self.acceptedTypes = types | |
| 122 | |
| 123 def parse(self): | |
| 124 progress = UnlimitedProgress(100000, "Reading input file", self.verbosity) | |
| 125 for cpt, line in enumerate(self.inputHandle): | |
| 126 if not line or line[0] == "#": continue | |
| 127 if line[0] == ">": break | |
| 128 parsedLine = ParsedLine(line, cpt) | |
| 129 if self.acceptedTypes == None or parsedLine.type in self.acceptedTypes: | |
| 130 self.lines[parsedLine.id] = parsedLine | |
| 131 progress.inc() | |
| 132 progress.done() | |
| 133 | |
| 134 def sort(self): | |
| 135 progress = Progress(len(self.lines.keys()), "Sorting file", self.verbosity) | |
| 136 for line in self.lines.values(): | |
| 137 parentFound = False | |
| 138 if line.parents: | |
| 139 for parent in line.parents: | |
| 140 if parent in self.lines: | |
| 141 parentFound = True | |
| 142 if parent in self.children: | |
| 143 self.children[parent].append(line) | |
| 144 else: | |
| 145 self.children[parent] = [line] | |
| 146 if not parentFound: | |
| 147 line.removeParent() | |
| 148 self.parents.append(line) | |
| 149 progress.inc() | |
| 150 progress.done() | |
| 151 | |
| 152 def write(self): | |
| 153 progress = Progress(len(self.parents), "Writing output file", self.verbosity) | |
| 154 for line in self.parents: | |
| 155 self.writeLine(line) | |
| 156 progress.inc() | |
| 157 progress.done() | |
| 158 | |
| 159 def writeLine(self, line): | |
| 160 self.outputHandle.write(line.export()) | |
| 161 if line.id in self.children: | |
| 162 for child in self.children[line.id]: | |
| 163 self.writeLine(child) | |
| 164 | |
| 165 def _clean(self): | |
| 166 self.parse() | |
| 167 self.sort() | |
| 168 self.write() |
