annotate commons/core/parsing/FastaParser.py @ 42:2abc37c27164

Deleted selected files
author m-zytnicki
date Wed, 18 Sep 2013 07:45:59 -0400
parents 44d5973c188c
children 169d364ddd91
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
36
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
1 #
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
2 # Copyright INRA-URGI 2009-2010
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
3 #
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
4 # This software is governed by the CeCILL license under French law and
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
5 # abiding by the rules of distribution of free software. You can use,
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
6 # modify and/ or redistribute the software under the terms of the CeCILL
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
7 # license as circulated by CEA, CNRS and INRIA at the following URL
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
8 # "http://www.cecill.info".
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
9 #
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
10 # As a counterpart to the access to the source code and rights to copy,
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
11 # modify and redistribute granted by the license, users are provided only
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
12 # with a limited warranty and the software's author, the holder of the
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
13 # economic rights, and the successive licensors have only limited
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
14 # liability.
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
15 #
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
16 # In this respect, the user's attention is drawn to the risks associated
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
17 # with loading, using, modifying and/or developing or reproducing the
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
18 # software by the user in light of its specific status of free software,
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
19 # that may mean that it is complicated to manipulate, and that also
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
20 # therefore means that it is reserved for developers and experienced
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
21 # professionals having in-depth computer knowledge. Users are therefore
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
22 # encouraged to load and test the software's suitability as regards their
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
23 # requirements in conditions enabling the security of their systems and/or
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
24 # data to be ensured and, more generally, to use and operate it in the
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
25 # same conditions as regards security.
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
26 #
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
27 # The fact that you are presently reading this means that you have had
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
28 # knowledge of the CeCILL license and that you accept its terms.
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
29 #
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
30 import sys
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
31 from commons.core.parsing.SequenceListParser import SequenceListParser
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
32 from SMART.Java.Python.structure.Sequence import Sequence
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
33 from SMART.Java.Python.misc.UnlimitedProgress import UnlimitedProgress
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
34
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
35 class FastaParser(SequenceListParser):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
36 """A class that reads a list of sequences in FASTA"""
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
37
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
38 def __init__(self, fileName, verbosity = 0):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
39 super(FastaParser, self).__init__(fileName, verbosity)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
40 self.tags = {}
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
41
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
42
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
43 def getTags(self):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
44 return self.tags
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
45
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
46
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
47 def getFileFormats():
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
48 return ["fasta", "mfa", "fas"]
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
49 getFileFormats = staticmethod(getFileFormats)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
50
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
51
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
52 def getInfos(self):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
53 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
54 Get some generic information about the sequences
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
55 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
56 self.nbSequences = 0
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
57 self.size = 0
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
58 self.reset()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
59 progress = UnlimitedProgress(100000, "Reading input file", self.verbosity - 9)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
60 for line in self.handle:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
61 line = line.strip()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
62 if line == "":
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
63 continue
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
64 if line[0] == ">":
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
65 self.nbSequences += 1
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
66 else:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
67 self.size += len(line)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
68 progress.inc()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
69 progress.done()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
70 self.reset()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
71
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
72
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
73 def parseOne(self):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
74 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
75 Parse only one element in the file
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
76 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
77 name = None
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
78 string = ""
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
79
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
80 if self.currentLine != None:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
81 if self.currentLine[0] != ">":
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
82 raise Exception("First line is weird: %s" % (self.currentLine))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
83 name = self.currentLine[1:].split()[0].replace("|", "_").replace(".", "_")
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
84 self.currentLine = None
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
85
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
86 for line in self.handle:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
87 line = line.strip()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
88 if line == "":
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
89 pass
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
90 elif line[0] == ">":
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
91 if name == None:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
92 name = line[1:].split()[0].replace("|", "_").replace(".", "_")
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
93 else:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
94 self.currentLine = line
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
95 return Sequence(name, string)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
96 else:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
97 string += line
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
98
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
99 if name == None:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
100 return None
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
101 return Sequence(name, string)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
102
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
103
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
104 def setTags(self):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
105 mark = self.handle.tell()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
106 thisTag = mark
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
107
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
108 line = self.handle.readline()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
109 while line != "":
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
110 if line[0] == ">":
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
111 line = line.strip()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
112 self.tags[line[1:].split()[0]] = thisTag
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
113 thisTag = self.handle.tell()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
114 line = self.handle.readline()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
115
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
116 self.handle.seek(mark)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
117
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
118
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
119 def getSubSequence(self, chromosome, start, end, direction, name = None):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
120 if not self.tags:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
121 self.setTags()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
122
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
123 if chromosome not in self.tags:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
124 raise Exception("Cannot find " + chromosome)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
125
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
126 if name == None:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
127 name = "%s:%d-%d (%d)" % (chromosome, start, end, direction)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
128 sequence = Sequence(name)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
129
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
130 # switch from 0-based to 1-based coordinates
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
131 start -= 1
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
132 end -= 1
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
133
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
134 self.handle.seek(self.tags[chromosome])
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
135 line = self.handle.readline().strip()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
136 if line != ">" + chromosome:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
137 raise Exception("Arrived in a wrong place (got %s)" % (line))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
138
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
139 position1 = self.handle.tell()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
140 line = self.handle.readline().strip()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
141 position2 = self.handle.tell()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
142 size = len(line)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
143 address = position1 + ((start - (start % size)) / size) * (position2 - position1);
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
144
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
145 count = max(0, start - (start % size));
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
146 self.handle.seek(address)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
147
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
148 newSequence = ""
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
149 for line in self.handle:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
150 line = line.strip()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
151
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
152 if line[0] == ">":
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
153 break
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
154
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
155 subStart = start - count
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
156 if subStart < 0:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
157 subStart = 0
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
158 subEnd = end - count
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
159 subSize = subEnd - subStart + 1
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
160 if subSize + subStart > len(line):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
161 subSize = len(line) - subStart
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
162 if subEnd < 0:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
163 break
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
164 if subStart <= len(line):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
165 newSequence += line[subStart:subStart+subSize]
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
166 count += len(line)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
167
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
168 if newSequence == "":
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
169 raise Exception("Error, sequence %s is empty" % (name))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
170 sequence.sequence = newSequence
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
171 if direction == -1:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
172 sequence.reverseComplement()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
173 return sequence