annotate commons/core/seq/Bioseq.py @ 30:5677346472b5

Deleted selected files
author m-zytnicki
date Mon, 29 Apr 2013 03:45:52 -0400
parents 94ab73e8a190
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 # Copyright INRA (Institut National de la Recherche Agronomique)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
2 # http://www.inra.fr
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
3 # http://urgi.versailles.inra.fr
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
4 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
5 # This software is governed by the CeCILL license under French law and
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
6 # abiding by the rules of distribution of free software. You can use,
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
7 # modify and/ or redistribute the software under the terms of the CeCILL
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
8 # license as circulated by CEA, CNRS and INRIA at the following URL
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
9 # "http://www.cecill.info".
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
10 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
11 # As a counterpart to the access to the source code and rights to copy,
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
12 # modify and redistribute granted by the license, users are provided only
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
13 # with a limited warranty and the software's author, the holder of the
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
14 # economic rights, and the successive licensors have only limited
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
15 # liability.
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
16 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
17 # In this respect, the user's attention is drawn to the risks associated
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
18 # with loading, using, modifying and/or developing or reproducing the
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
19 # software by the user in light of its specific status of free software,
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
20 # that may mean that it is complicated to manipulate, and that also
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
21 # therefore means that it is reserved for developers and experienced
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
22 # professionals having in-depth computer knowledge. Users are therefore
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
23 # encouraged to load and test the software's suitability as regards their
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
24 # requirements in conditions enabling the security of their systems and/or
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
25 # data to be ensured and, more generally, to use and operate it in the
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
26 # same conditions as regards security.
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
27 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
28 # The fact that you are presently reading this means that you have had
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
29 # knowledge of the CeCILL license and that you accept its terms.
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
30
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
31
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
32 import sys
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
33 import string
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
34 import re
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
35 import random
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
36 import cStringIO
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
37 from commons.core.coord.Map import Map
18
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
38 from commons.core.checker.RepetException import RepetException
6
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
39
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
40 DNA_ALPHABET_WITH_N = set( ['A','T','G','C','N'] )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
41 IUPAC = set(['A','T','G','C','U','R','Y','M','K','W','S','B','D','H','V','N'])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
42
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
43
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
44 ## Record a sequence with its header
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
45 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
46 class Bioseq( object ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
47
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
48 header = ""
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
49 sequence = ""
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
50
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
51 ## constructor
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
52 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
53 # @param name the header of sequence
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
54 # @param seq sequence (DNA, RNA, protein)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
55 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
56 def __init__( self, name="", seq="" ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
57 self.header = name
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
58 self.sequence = seq
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
59
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
60
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
61 ## Equal operator
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
62 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
63 def __eq__( self, o ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
64 if self.header==o.header and self.sequence==o.sequence:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
65 return True
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 ## overload __repr__
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
70 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
71 def __repr__( self ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
72 return "%s;%s" % ( self.header, self.sequence )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
73
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
74
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
75 ## set attribute header
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
76 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
77 # @param header a string
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
78 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
79 def setHeader( self, header ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
80 self.header = header
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
81
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
82
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
83 ## get attribute header
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
84 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
85 # @return header
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
86 def getHeader(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
87 return self.header
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
88
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
89
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
90 ## set attribute sequence
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
91 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
92 # @param sequence a string
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
93 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
94 def setSequence( self, sequence ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
95 self.sequence = sequence
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 getSequence(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
99 return self.sequence
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
100
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
101 ## reset
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
102 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
103 def reset( self ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
104 self.setHeader( "" )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
105 self.setSequence( "" )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
106
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
107
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
108 ## Test if bioseq is empty
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
109 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
110 def isEmpty( self ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
111 return self.header == "" and self.sequence == ""
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
112
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
113
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
114 ## Reverse the sequence
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
115 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
116 def reverse( self ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
117 tmp = self.sequence
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
118 self.sequence = tmp[::-1]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
119
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
120
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
121 ## Turn the sequence into its complement
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
122 # Force upper case letters
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
123 # @warning: old name in pyRepet.Bioseq realComplement
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
124 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
125 def complement( self ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
126 complement = ""
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
127 self.upCase()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
128 for i in xrange(0,len(self.sequence),1):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
129 if self.sequence[i] == "A":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
130 complement += "T"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
131 elif self.sequence[i] == "T":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
132 complement += "A"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
133 elif self.sequence[i] == "C":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
134 complement += "G"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
135 elif self.sequence[i] == "G":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
136 complement += "C"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
137 elif self.sequence[i] == "M":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
138 complement += "K"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
139 elif self.sequence[i] == "R":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
140 complement += "Y"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
141 elif self.sequence[i] == "W":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
142 complement += "W"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
143 elif self.sequence[i] == "S":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
144 complement += "S"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
145 elif self.sequence[i] == "Y":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
146 complement += "R"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
147 elif self.sequence[i] == "K":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
148 complement += "M"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
149 elif self.sequence[i] == "V":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
150 complement += "B"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
151 elif self.sequence[i] == "H":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
152 complement += "D"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
153 elif self.sequence[i] == "D":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
154 complement += "H"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
155 elif self.sequence[i] == "B":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
156 complement += "V"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
157 elif self.sequence[i] == "N":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
158 complement += "N"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
159 elif self.sequence[i] == "-":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
160 complement += "-"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
161 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
162 print "WARNING: unknown symbol '%s', replacing it by N" % ( self.sequence[i] )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
163 complement += "N"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
164 self.sequence = complement
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
165
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
166
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
167 ## Reverse and complement the sequence
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
168 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
169 # Force upper case letters
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
170 # @warning: old name in pyRepet.Bioseq : complement
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
171 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
172 def reverseComplement( self ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
173 self.reverse()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
174 self.complement()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
175
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
176
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
177 ## Remove gap in the sequence
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
178 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
179 def cleanGap(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
180 self.sequence = self.sequence.replace("-","")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
181
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
182
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
183 ## Copy current Bioseq Instance
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
184 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
185 # @return: a Bioseq instance, a copy of current sequence.
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
186 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
187 def copyBioseqInstance(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
188 seq = Bioseq()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
189 seq.sequence = self.sequence
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
190 seq.header = self.header
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
191 return seq
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
192
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
193
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
194 ## Add phase information after the name of sequence in header
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
195 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
196 # @param phase integer representing phase (1, 2, 3, -1, -2, -3)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
197 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
198 def setFrameInfoOnHeader(self, phase):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
199 if " " in self.header:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
200 name, desc = self.header.split(" ", 1)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
201 name = name + "_" + str(phase)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
202 self.header = name + " " + desc
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
203 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
204 self.header = self.header + "_" + str(phase)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
205
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
206
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
207 ## Fill Bioseq attributes with fasta file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
208 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
209 # @param faFileHandler file handler of a fasta file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
210 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
211 def read( self, faFileHandler ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
212 line = faFileHandler.readline()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
213 if line == "":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
214 self.header = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
215 self.sequence = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
216 return
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
217 while line == "\n":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
218 line = faFileHandler.readline()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
219 if line[0] == '>':
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
220 self.header = string.rstrip(line[1:])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
221 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
222 print "error, line is",string.rstrip(line)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
223 return
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
224 line = " "
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
225 seq = cStringIO.StringIO()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
226 while line:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
227 prev_pos = faFileHandler.tell()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
228 line = faFileHandler.readline()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
229 if line == "":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
230 break
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
231 if line[0] == '>':
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
232 faFileHandler.seek( prev_pos )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
233 break
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
234 seq.write( string.rstrip(line) )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
235 self.sequence = seq.getvalue()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
236
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
237
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
238 ## Create a subsequence with a modified header
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
239 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
240 # @param s integer start a required subsequence
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
241 # @param e integer end a required subsequence
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
242 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
243 # @return a Bioseq instance, a subsequence of current sequence
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
244 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
245 def subseq( self, s, e=0 ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
246 if e == 0 :
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
247 e=len( self.sequence )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
248 if s > e :
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
249 print "error: start must be < or = to end"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
250 return
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
251 if s <= 0 :
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
252 print "error: start must be > 0"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
253 return
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
254 sub = Bioseq()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
255 sub.header = self.header + " fragment " + str(s) + ".." + str(e)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
256 sub.sequence = self.sequence[(s-1):e]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
257 return sub
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
258
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
259
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
260 ## Get the nucleotide or aminoacid at the given position
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
261 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
262 # @param pos integer nucleotide or aminoacid position
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
263 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
264 # @return a string
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
265 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
266 def getNtFromPosition(self, pos):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
267 result = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
268 if not (pos < 1 or pos > self.getLength()):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
269 result = self.sequence[pos - 1]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
270 return result
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
271
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
272
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
273 ## Print in stdout the Bioseq in fasta format with 60 characters lines
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
274 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
275 # @param l length of required sequence default is whole sequence
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
276 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
277 def view(self,l=0):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
278 print '>'+self.header
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
279 i=0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
280 if(l==0):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
281 l=len(self.sequence)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
282 seq=self.sequence[0:l]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
283
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
284 while i<len(seq):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
285 print seq[i:i+60]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
286 i=i+60
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
287
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
288
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
289 ## Get length of sequence
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
290 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
291 # @param avoidN boolean don't count 'N' nucleotides
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
292 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
293 # @return length of current sequence
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
294 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
295 def getLength( self, countN = True ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
296 if countN:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
297 return len(self.sequence)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
298 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
299 return len(self.sequence) - self.countNt('N')
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
300
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
301
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
302 ## Return the proportion of a specific character
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
303 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
304 # @param nt character that we want to count
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
305 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
306 def propNt( self, nt ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
307 return self.countNt( nt ) / float( self.getLength() )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
308
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
309
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
310 ## Count occurrence of specific character
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
311 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
312 # @param nt character that we want to count
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
313 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
314 # @return nb of occurrences
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
315 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
316 def countNt( self, nt ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
317 return self.sequence.count( nt )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
318
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
319
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
320 ## Count occurrence of each nucleotide in current seq
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
321 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
322 # @return a dict, keys are nucleotides, values are nb of occurrences
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
323 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
324 def countAllNt( self ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
325 dNt2Count = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
326 for nt in ["A","T","G","C","N"]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
327 dNt2Count[ nt ] = self.countNt( nt )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
328 return dNt2Count
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
329
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
330
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
331 ## Return a dict with the number of occurrences for each combination of ATGC of specified size and number of word found
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
332 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
333 # @param size integer required length word
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
334 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
335 def occ_word( self, size ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
336 occ = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
337 if size == 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
338 return occ,0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
339 nbword = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
340 srch = re.compile('[^ATGC]+')
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
341 wordlist = self._createWordList( size )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
342 for i in wordlist:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
343 occ[i] = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
344 lenseq = len(self.sequence)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
345 i = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
346 while i < lenseq-size+1:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
347 word = self.sequence[i:i+size].upper()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
348 m = srch.search(word)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
349 if m == None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
350 occ[word] = occ[word]+1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
351 nbword = nbword + 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
352 i = i + 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
353 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
354 i = i + m.end(0)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
355 return occ, nbword
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
356
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
357
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
358 ## Return a dictionary with the frequency of occurs for each combination of ATGC of specified size
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
359 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
360 # @param size integer required length word
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
361 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
362 def freq_word( self, size ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
363 dOcc, nbWords = self.occ_word( size )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
364 freq = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
365 for word in dOcc.keys():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
366 freq[word] = float(dOcc[word]) / nbWords
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
367 return freq
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
368
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
369
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
370 ## Find ORF in each phase
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
371 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
372 # @return: a dict, keys are phases, values are stop codon positions.
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
373 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
374 def findORF (self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
375 orf = {0:[],1:[],2:[]}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
376 length = len (self.sequence)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
377 for i in xrange(0,length):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
378 triplet = self.sequence[i:i+3]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
379 if ( triplet == "TAA" or triplet == "TAG" or triplet == "TGA"):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
380 phase = i % 3
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
381 orf[phase].append(i)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
382 return orf
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
383
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
384
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
385 ## Convert the sequence into upper case
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
386 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
387 def upCase( self ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
388 self.sequence = self.sequence.upper()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
389
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
390
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
391 ## Convert the sequence into lower case
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
392 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
393 def lowCase( self ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
394 self.sequence = self.sequence.lower()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
395
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
396
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
397 ## Extract the cluster of the fragment (output from Grouper)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
398 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
399 # @return cluster id (string)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
400 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
401 def getClusterID( self ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
402 data = self.header.split()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
403 return data[0].split("Cl")[1]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
404
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
405
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
406 ## Extract the group of the sequence (output from Grouper)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
407 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
408 # @return group id (string)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
409 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
410 def getGroupID( self ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
411 data = self.header.split()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
412 return data[0].split("Gr")[1].split("Cl")[0]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
413
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
414
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
415 ## Get the header of the full sequence (output from Grouper)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
416 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
417 # @example 'Dmel_Grouper_3091_Malign_3:LARD' from '>MbS1566Gr81Cl81 Dmel_Grouper_3091_Malign_3:LARD {Fragment} 1..5203'
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
418 # @return header (string)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
419 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
420 def getHeaderFullSeq( self ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
421 data = self.header.split()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
422 return data[1]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
423
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
424
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
425 ## Get the strand of the fragment (output from Grouper)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
426 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
427 # @return: strand (+ or -)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
428 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
429 def getFragStrand( self ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
430 data = self.header.split()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
431 coord = data[3].split("..")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
432 if int(coord[0]) < int(coord[-1]):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
433 return "+"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
434 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
435 return "-"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
436
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
437
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
438 ## Get A, T, G, C or N from an IUPAC letter
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
439 # IUPAC = ['A','T','G','C','U','R','Y','M','K','W','S','B','D','H','V','N']
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
440 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
441 # @return A, T, G, C or N
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
442 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
443 def getATGCNFromIUPAC( self, nt ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
444 subset = ["A","T","G","C","N"]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
445
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
446 if nt in subset:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
447 return nt
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
448 elif nt == "U":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
449 return "T"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
450 elif nt == "R":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
451 return random.choice( "AG" )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
452 elif nt == "Y":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
453 return random.choice( "CT" )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
454 elif nt == "M":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
455 return random.choice( "CA" )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
456 elif nt == "K":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
457 return random.choice( "TG" )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
458 elif nt == "W":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
459 return random.choice( "TA" )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
460 elif nt == "S":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
461 return random.choice( "CG" )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
462 elif nt == "B":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
463 return random.choice( "CTG" )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
464 elif nt == "D":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
465 return random.choice( "ATG" )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
466 elif nt == "H":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
467 return random.choice( "ATC" )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
468 elif nt == "V":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
469 return random.choice( "ACG" )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
470 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
471 return "N"
18
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
472
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
473 ## Get nucleotide from an IUPAC letter and a nucleotide
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
474 # Works only for IUPAC code with two possibilities ['R','Y','M','K','W','S']
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
475 # Examples:
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
476 # Y and C returns T
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
477 # Y and T returns C
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
478 # B and C throws RepetException
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
479 #
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
480 # @return A, T, G, C
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
481 #
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
482 def getATGCNFromIUPACandATGCN(self, IUPACCode, nt):
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
483 if IUPACCode == "R":
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
484 possibleNt = set(["A", "G"])
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
485 if nt not in possibleNt:
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
486 raise RepetException("IUPAC code '%s' and nucleotide '%s' are not compatible" % (IUPACCode, nt))
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
487 return (possibleNt - set(nt)).pop()
6
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
488
18
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
489 elif IUPACCode == "Y":
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
490 possibleNt = set(["C", "T"])
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
491 if nt not in possibleNt:
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
492 raise RepetException("IUPAC code '%s' and nucleotide '%s' are not compatible" % (IUPACCode, nt))
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
493 return (possibleNt - set(nt)).pop()
6
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
494
18
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
495 elif IUPACCode == "M":
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
496 possibleNt = set(["A", "C"])
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
497 if nt not in possibleNt:
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
498 raise RepetException("IUPAC code '%s' and nucleotide '%s' are not compatible" % (IUPACCode, nt))
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
499 return (possibleNt - set(nt)).pop()
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
500
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
501 elif IUPACCode == "K":
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
502 possibleNt = set(["T", "G"])
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
503 if nt not in possibleNt:
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
504 raise RepetException("IUPAC code '%s' and nucleotide '%s' are not compatible" % (IUPACCode, nt))
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
505 return (possibleNt - set(nt)).pop()
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
506
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
507 elif IUPACCode == "W":
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
508 possibleNt = set(["A", "T"])
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
509 if nt not in possibleNt:
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
510 raise RepetException("IUPAC code '%s' and nucleotide '%s' are not compatible" % (IUPACCode, nt))
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
511 return (possibleNt - set(nt)).pop()
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
512
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
513 elif IUPACCode == "S":
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
514 possibleNt = set(["C", "G"])
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
515 if nt not in possibleNt:
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
516 raise RepetException("IUPAC code '%s' and nucleotide '%s' are not compatible" % (IUPACCode, nt))
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
517 return (possibleNt - set(nt)).pop()
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
518
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
519 else:
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
520 raise RepetException("Can't retrieve the third nucleotide from IUPAC code '%s' and nucleotide '%s'" % (IUPACCode, nt))
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
521
6
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
522 def getSeqWithOnlyATGCN( self ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
523 newSeq = ""
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
524 for nt in self.sequence:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
525 newSeq += self.getATGCNFromIUPAC( nt )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
526 return newSeq
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
527
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
528
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
529 ## Replace any symbol not in (A,T,G,C,N) by another nucleotide it represents
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
530 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
531 def partialIUPAC( self ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
532 self.sequence = self.getSeqWithOnlyATGCN()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
533
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
534
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
535 ## Remove non Unix end-of-line symbols, if any
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
536 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
537 def checkEOF( self ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
538 symbol = "\r" # corresponds to '^M' from Windows
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
539 if symbol in self.sequence:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
540 print "WARNING: Windows EOF removed in '%s'" % ( self.header )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
541 sys.stdout.flush()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
542 newSeq = self.sequence.replace( symbol, "" )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
543 self.sequence = newSeq
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
544
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
545
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
546 ## Write Bioseq instance into a fasta file handler
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
547 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
548 # @param faFileHandler file handler of a fasta file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
549 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
550 def write( self, faFileHandler ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
551 faFileHandler.write( ">%s\n" % ( self.header ) )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
552 self.writeSeqInFasta( faFileHandler )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
553
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
554
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
555 ## Write only the sequence of Bioseq instance into a fasta file handler
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
556 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
557 # @param faFileHandler file handler of a fasta file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
558 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
559 def writeSeqInFasta( self, faFileHandler ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
560 i = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
561 while i < self.getLength():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
562 faFileHandler.write( "%s\n" % ( self.sequence[i:i+60] ) )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
563 i += 60
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
564
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
565
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
566 ## Append Bioseq instance to a fasta file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
567 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
568 # @param faFile name of a fasta file as a string
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
569 # @param mode 'write' or 'append'
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
570 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
571 def save( self, faFile, mode="a" ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
572 faFileHandler = open( faFile, mode )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
573 self.write( faFileHandler )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
574 faFileHandler.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
575
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
576
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
577 ## Append Bioseq instance to a fasta file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
578 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
579 # @param faFile name of a fasta file as a string
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
580 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
581 def appendBioseqInFile( self, faFile ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
582 self.save( faFile, "a" )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
583
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
584
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
585 ## Write Bioseq instance into a fasta file handler
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
586 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
587 # @param faFileHandler file handler on a file with writing right
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
588 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
589 def writeABioseqInAFastaFile( self, faFileHandler ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
590 self.write( faFileHandler )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
591
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
592
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
593 ## Write Bioseq instance with other header into a fasta file handler
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
594 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
595 # @param faFileHandler file handler on a file with writing right
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
596 # @param otherHeader a string representing a new header (without the > and the \n)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
597 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
598 def writeWithOtherHeader( self, faFileHandler, otherHeader ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
599 self.header = otherHeader
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
600 self.write( faFileHandler )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
601
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
602
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
603 ## Append Bioseq header and Bioseq sequence in a fasta file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
604 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
605 # @param faFileHandler file handler on a file with writing right
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
606 # @param otherHeader a string representing a new header (without the > and the \n)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
607 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
608 def writeABioseqInAFastaFileWithOtherHeader( self, faFileHandler, otherHeader ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
609 self.writeWithOtherHeader( faFileHandler, otherHeader )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
610
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
611
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
612 ## get the list of Maps corresponding to seq without gap
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
613 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
614 # @warning This method was called getMap() in pyRepet.Bioseq
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
615 # @return a list of Map object
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
616 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
617 def getLMapWhithoutGap( self ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
618 lMaps = []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
619 countSite = 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
620 countSubseq = 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
621 inGap = False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
622 startMap = -1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
623 endMap = -1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
624
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
625 # initialize with the first site
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
626 if self.sequence[0] == "-":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
627 inGap = True
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
628 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
629 startMap = countSite
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
630
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
631 # for each remaining site
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
632 for site in self.sequence[1:]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
633 countSite += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
634
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
635 # if it is a gap
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
636 if site == "-":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
637
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
638 # if this is the beginning of a gap, record the previous subsequence
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
639 if inGap == False:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
640 inGap = True
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
641 endMap = countSite - 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
642 lMaps.append( Map( "%s_subSeq%i" % (self.header,countSubseq), self.header, startMap, endMap ) )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
643 countSubseq += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
644
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
645 # if it is NOT a gap
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
646 if site != "-":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
647
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
648 # if it is the end of a gap, begin the next subsequence
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
649 if inGap == True:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
650 inGap = False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
651 startMap = countSite
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
652
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
653 # if it is the last site
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
654 if countSite == self.getLength():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
655 endMap = countSite
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
656 lMaps.append( Map( "%s_subSeq%i" % (self.header,countSubseq), self.header, startMap, endMap ) )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
657
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
658 return lMaps
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
659
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
660
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
661 ## get the percentage of GC
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
662 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
663 # @return a percentage
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
664 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
665 def getGCpercentage( self ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
666 tmpSeq = self.getSeqWithOnlyATGCN()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
667 nbGC = tmpSeq.count( "G" ) + tmpSeq.count( "C" )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
668 return 100 * nbGC / float( self.getLength() )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
669
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
670 ## get the percentage of GC of a sequence without counting N in sequence length
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
671 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
672 # @return a percentage
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
673 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
674 def getGCpercentageInSequenceWithoutCountNInLength(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
675 tmpSeq = self.getSeqWithOnlyATGCN()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
676 nbGC = tmpSeq.count( "G" ) + tmpSeq.count( "C" )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
677 return 100 * nbGC / float( self.getLength() - self.countNt("N") )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
678
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
679 ## get the 5 prime subsequence of a given length at the given position
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
680 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
681 # @param position integer
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
682 # @param flankLength integer subsequence length
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
683 # @return a sequence string
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
684 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
685 def get5PrimeFlank(self, position, flankLength):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
686 if(position == 1):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
687 return ""
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
688 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
689 startOfFlank = 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
690 endOfFlank = position -1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
691
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
692 if((position - flankLength) > 0):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
693 startOfFlank = position - flankLength
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
694 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
695 startOfFlank = 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
696
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
697 return self.subseq(startOfFlank, endOfFlank).sequence
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
698
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
699
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
700 ## get the 3 prime subsequence of a given length at the given position
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
701 # In the case of indels, the polymorphism length can be specified
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
702 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
703 # @param position integer
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
704 # @param flankLength integer subsequence length
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
705 # @param polymLength integer polymorphism length
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
706 # @return a sequence string
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
707 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
708 def get3PrimeFlank(self, position, flankLength, polymLength = 1):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
709 if((position + polymLength) > len( self.sequence )):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
710 return ""
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
711 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
712 startOfFlank = position + polymLength
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
713
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
714 if((position+polymLength+flankLength) > len( self.sequence )):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
715 endOfFlank = len( self.sequence )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
716 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
717 endOfFlank = position+polymLength+flankLength-1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
718
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
719 return self.subseq(startOfFlank, endOfFlank).sequence
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
720
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
721
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
722 def _createWordList(self,size,l=['A','T','G','C']):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
723 if size == 1 :
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
724 return l
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
725 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
726 l2 = []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
727 for i in l:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
728 for j in ['A','T','G','C']:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
729 l2.append( i + j )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
730 return self._createWordList(size-1,l2)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
731
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
732
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
733 def removeSymbol( self, symbol ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
734 tmp = self.sequence.replace( symbol, "" )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
735 self.sequence = tmp