Mercurial > repos > yufei-luo > s_mart
comparison commons/core/parsing/MapperParser.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 sys | |
| 31 from SMART.Java.Python.structure.Mapping import Mapping | |
| 32 | |
| 33 | |
| 34 class MapperParser(object): | |
| 35 """An interface that parses the output of a generic mapper""" | |
| 36 | |
| 37 def __init__(self, fileName, verbosity = 0): | |
| 38 super(MapperParser, self).__init__() | |
| 39 self.verbosity = verbosity | |
| 40 self.nbMappings = None | |
| 41 self.chromosomes = None | |
| 42 self.size = None | |
| 43 self.currentMapping = Mapping() | |
| 44 self.handle = open(fileName) | |
| 45 self.currentLineNb = 0 | |
| 46 self.skipFirstLines() | |
| 47 self.fileName = fileName | |
| 48 self.startingPoint = self.handle.tell() | |
| 49 | |
| 50 | |
| 51 def __del__(self): | |
| 52 self.handle.close() | |
| 53 | |
| 54 | |
| 55 def reset(self): | |
| 56 self.handle.seek(self.startingPoint) | |
| 57 self.currentLineNb = 0 | |
| 58 | |
| 59 | |
| 60 def getNextMapping(self): | |
| 61 for line in self.handle: | |
| 62 mapping = self.parseLine(line) | |
| 63 self.currentLineNb += 1 | |
| 64 if mapping != None: | |
| 65 return mapping | |
| 66 return False | |
| 67 | |
| 68 | |
| 69 def getIterator(self): | |
| 70 self.reset() | |
| 71 mapping = self.getNextMapping() | |
| 72 while mapping: | |
| 73 yield mapping | |
| 74 mapping = self.getNextMapping() | |
| 75 | |
| 76 | |
| 77 def getInfos(self): | |
| 78 self.chromosomes = set() | |
| 79 self.nbMappings = 0 | |
| 80 self.size = 0 | |
| 81 self.reset() | |
| 82 if self.verbosity >= 10: | |
| 83 print "Getting information." | |
| 84 for mapping in self.getIterator(): | |
| 85 transcript = mapping.getTranscript() | |
| 86 self.chromosomes.add(transcript.getChromosome()) | |
| 87 self.nbMappings += 1 | |
| 88 self.size += transcript.getSize() | |
| 89 if self.verbosity >= 10 and self.nbMappings % 100000 == 0: | |
| 90 sys.stdout.write(" %d mappings read\r" % (self.nbMappings)) | |
| 91 sys.stdout.flush() | |
| 92 self.reset() | |
| 93 if self.verbosity >= 10: | |
| 94 print " %d mappings read" % (self.nbMappings) | |
| 95 print "Done." | |
| 96 | |
| 97 | |
| 98 def getNbMappings(self): | |
| 99 if self.nbMappings != None: | |
| 100 return self.nbMappings | |
| 101 self.getInfos() | |
| 102 return self.nbMappings | |
| 103 | |
| 104 | |
| 105 def getNbItems(self): | |
| 106 return self.getNbMappings() | |
| 107 | |
| 108 | |
| 109 def getChromosomes(self): | |
| 110 if self.chromosomes != None: | |
| 111 return self.chromosomes | |
| 112 self.getInfos() | |
| 113 return self.chromosomes | |
| 114 | |
| 115 | |
| 116 def getSize(self): | |
| 117 if self.size != None: | |
| 118 return self.size | |
| 119 self.getInfos() | |
| 120 return self.size | |
| 121 | |
| 122 | |
| 123 def getNbNucleotides(self): | |
| 124 return self.getSize() | |
| 125 | |
| 126 | |
| 127 def setDefaultTagValue(self, name, value): | |
| 128 for mapping in self.getIterator(): | |
| 129 mapping.setTagValue(name, value) |
