annotate commons/core/parsing/FastaParser.py @ 14:c79b9ae3f65f

Deleted selected files
author m-zytnicki
date Fri, 19 Apr 2013 10:13:11 -0400
parents 769e306b7933
children 94ab73e8a190
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 commons.core.parsing.SequenceListParser import SequenceListParser
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
32 from SMART.Java.Python.structure.Sequence import Sequence
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
33 from SMART.Java.Python.misc.UnlimitedProgress import UnlimitedProgress
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
34
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
35 class FastaParser(SequenceListParser):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
36 """A class that reads a list of sequences in FASTA"""
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
37
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
38 def __init__(self, fileName, verbosity = 0):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
39 super(FastaParser, self).__init__(fileName, verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
40 self.tags = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
41
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
42
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
43 def getTags(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
44 return self.tags
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
45
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
46
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
47 def getFileFormats():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
48 return ["fasta", "mfa", "fas"]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
49 getFileFormats = staticmethod(getFileFormats)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
50
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
51
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
52 def getInfos(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
53 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
54 Get some generic information about the sequences
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
55 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
56 self.nbSequences = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
57 self.size = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
58 self.reset()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
59 progress = UnlimitedProgress(100000, "Reading input file", self.verbosity - 9)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
60 for line in self.handle:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
61 line = line.strip()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
62 if line == "":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
63 continue
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
64 if line[0] == ">":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
65 self.nbSequences += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
66 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
67 self.size += len(line)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
68 progress.inc()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
69 progress.done()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
70 self.reset()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
71
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
72
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
73 def parseOne(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
74 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
75 Parse only one element in the file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
76 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
77 name = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
78 string = ""
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
79
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
80 if self.currentLine != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
81 if self.currentLine[0] != ">":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
82 raise Exception("First line is weird: %s" % (self.currentLine))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
83 name = self.currentLine[1:].split()[0]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
84 self.currentLine = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
85
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
86 for line in self.handle:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
87 line = line.strip()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
88 if line == "":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
89 pass
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
90 elif line[0] == ">":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
91 if name == None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
92 name = line[1:].split()[0]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
93 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
94 self.currentLine = line
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
95 return Sequence(name, string)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
96 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
97 string += line
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
98
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
99 if name == None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
100 return None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
101 return Sequence(name, string)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
102
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
103
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
104 def setTags(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
105 mark = self.handle.tell()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
106 thisTag = mark
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
107
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
108 line = self.handle.readline()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
109 while line != "":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
110 if line[0] == ">":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
111 line = line.strip()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
112 self.tags[line[1:].split()[0]] = thisTag
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
113 thisTag = self.handle.tell()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
114 line = self.handle.readline()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
115
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
116 self.handle.seek(mark)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
117
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
118
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
119 def getSubSequence(self, chromosome, start, end, direction, name = None):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
120 if not self.tags:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
121 self.setTags()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
122
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
123 if chromosome not in self.tags:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
124 raise Exception("Cannot find " + chromosome)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
125
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
126 if name == None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
127 name = "%s:%d-%d (%d)" % (chromosome, start, end, direction)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
128 sequence = Sequence(name)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
129
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
130 # switch from 0-based to 1-based coordinates
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
131 start -= 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
132 end -= 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
133
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
134 self.handle.seek(self.tags[chromosome])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
135 line = self.handle.readline().strip()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
136 if line != ">" + chromosome:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
137 raise Exception("Arrived in a wrong place (got %s)" % (line))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
138
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
139 position1 = self.handle.tell()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
140 line = self.handle.readline().strip()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
141 position2 = self.handle.tell()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
142 size = len(line)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
143 address = position1 + ((start - (start % size)) / size) * (position2 - position1);
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
144
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
145 count = max(0, start - (start % size));
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
146 self.handle.seek(address)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
147
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
148 newSequence = ""
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
149 for line in self.handle:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
150 line = line.strip()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
151
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
152 if line[0] == ">":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
153 break
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
154
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
155 subStart = start - count
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
156 if subStart < 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
157 subStart = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
158 subEnd = end - count
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
159 subSize = subEnd - subStart + 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
160 if subSize + subStart > len(line):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
161 subSize = len(line) - subStart
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
162 if subEnd < 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
163 break
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
164 if subStart <= len(line):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
165 newSequence += line[subStart:subStart+subSize]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
166 count += len(line)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
167
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
168 if newSequence == "":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
169 raise Exception("Error, sequence %s is empty" % (name))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
170 sequence.sequence = newSequence
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
171 if direction == -1:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
172 sequence.reverseComplement()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
173 return sequence