annotate commons/core/parsing/MapperParser.py @ 58:5f5c9b74c2dd

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