annotate SMART/Java/Python/structure/Sequence.py @ 18:94ab73e8a190

Uploaded
author m-zytnicki
date Mon, 29 Apr 2013 03:20:15 -0400
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 import re
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
32 from commons.core.seq.Bioseq import Bioseq
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
33
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
34 reverseComplementString = {
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
35 "A": "T",
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
36 "C": "G",
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
37 "G": "C",
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
38 "T": "A",
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
39 "U": "A",
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
40 "M": "K",
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
41 "R": "Y",
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
42 "W": "W",
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
43 "S": "S",
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
44 "Y": "R",
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
45 "K": "M",
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
46 "V": "B",
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
47 "H": "D",
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
48 "D": "H",
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
49 "B": "V",
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
50 "N": "N",
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
51 "a": "t",
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
52 "c": "g",
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
53 "g": "c",
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
54 "t": "a",
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
55 "u": "a",
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
56 "m": "k",
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
57 "r": "y",
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
58 "w": "w",
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
59 "s": "s",
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
60 "y": "r",
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
61 "k": "m",
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
62 "v": "b",
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
63 "h": "d",
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
64 "d": "h",
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
65 "b": "v",
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
66 "n": "n"
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 class Sequence(Bioseq):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
70 """A class that codes for a sequence"""
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
71
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
72 def __init__(self, name = "", sequence = ""):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
73 super(Sequence, self).__init__(name, sequence)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
74 self.name = self.header
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
75 self.quality = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
76 self.chunkedSequence = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
77 self.chunkedQuality = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
78 self.integerQuality = False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
79
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
80 def setName(self, name=""):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
81 super(Sequence, self).setHeader(name)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
82
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
83 def getName(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
84 return self.getHeader()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
85
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
86 def setSequence(self, seq=""):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
87 super(Sequence, self).setSequence(seq)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
88
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
89 def setQuality(self, quality):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
90 if quality == None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
91 self.quality = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
92 return
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
93 if " " in quality:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
94 self.quality = quality.split()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
95 self.integerQuality = True
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
96 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
97 self.quality = list(quality)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
98
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
99 def getQuality(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
100 if self.quality == None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
101 return None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
102 if self.integerQuality:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
103 return " ".join(self.quality)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
104 return "".join(self.quality)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
105
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
106 def getSize(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
107 return len(self.getSequence())
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
108
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
109
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
110 def copy(self, sequence):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
111 self.setName(sequence.getName())
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
112 self.setSequence(sequence.getSequence())
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
113 self.setQuality(sequence.getQuality())
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
114 self.chunkedSequence = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
115 self.chunkedQuality = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
116
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
117
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
118 def chunkSequence(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
119 self.chunkedSequence = []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
120 for i in range (0, self.getSize() / 60 + 1):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
121 self.chunkedSequence.append(self.getSequence()[i * 60 : min(self.getSize(), (i+1) * 60)])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
122 if self.quality != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
123 self.chunkedQuality = []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
124 for i in range (0, self.getSize() / 60 + 1):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
125 self.chunkedQuality.append(self.quality[i * 60 : min(self.getSize(), (i+1) * 60)])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
126
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
127 def concatenate(self, seq):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
128 sequence = self.getSequence()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
129 sequence += seq.getSequence()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
130 self.setSequence(sequence)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
131 if self.quality != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
132 sep = " " if self.integerQuality else ""
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
133 self.setQuality(self.getQuality() + sep + seq.getQuality())
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
134 self.chunkedSequence = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
135 self.chunkedQuality = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
136
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
137
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
138 def printFasta(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
139 if self.chunkedSequence == None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
140 self.chunkSequence()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
141 return ">%s\n%s\n" % (self.getHeader(), "\n".join(self.chunkedSequence))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
142
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
143
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
144 def printFastq(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
145 if self.chunkedSequence == None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
146 self.chunkSequence()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
147 return "@%s\n%s\n+%s\n%s\n" % (self.getHeader(), self.getSequence(), self.getHeader(), self.getQuality())
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
148
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
149
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
150 def reverseComplement(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
151 seq = ""
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
152 self.chunkedSequence = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
153 self.chunkedQuality = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
154 for i in range(0, self.getSize()):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
155 char = self.getSequence()[i:i+1]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
156 if char not in reverseComplementString:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
157 sys.exit("Cannot understand character %s from string %s" % (char, self.getSequence()))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
158 seq = "%s%s" % (reverseComplementString[char], seq)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
159 self.setSequence(seq)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
160 if self.quality != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
161 self.quality = self.quality[::-1]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
162
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
163
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
164 def containsAmbiguousNucleotides(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
165 m = re.search("[^ACGTUacgtu]", self.getSequence())
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
166 if m != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
167 return True
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
168 return False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
169
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
170
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
171 def shrinkToFirstNucleotides(self, nbNucleotides):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
172 self.chunkedSequence = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
173 self.chunkedQuality = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
174 self.setSequence(self.getSequence()[0:nbNucleotides])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
175 if self.quality != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
176 self.quality = self.quality[0:nbNucleotides]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
177
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
178
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
179 def shrinkToLastNucleotides(self, nbNucleotides):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
180 self.chunkedSequence = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
181 self.chunkedQuality = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
182 self.setSequence(self.getSequence()[-nbNucleotides:])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
183 if self.quality != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
184 self.quality = self.quality[-nbNucleotides:]