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