annotate TEisotools-1.1.a/commons/core/seq/BioseqUtils.py @ 15:255c852351c5 draft

Uploaded
author urgi-team
date Thu, 21 Jul 2016 07:36:44 -0400
parents feef9a0db09d
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
13
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
1 # Copyright INRA (Institut National de la Recherche Agronomique)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
2 # http://www.inra.fr
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
3 # http://urgi.versailles.inra.fr
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
4 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
5 # This software is governed by the CeCILL license under French law and
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
6 # abiding by the rules of distribution of free software. You can use,
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
7 # modify and/ or redistribute the software under the terms of the CeCILL
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
8 # license as circulated by CEA, CNRS and INRIA at the following URL
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
9 # "http://www.cecill.info".
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
10 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
11 # As a counterpart to the access to the source code and rights to copy,
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
12 # modify and redistribute granted by the license, users are provided only
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
13 # with a limited warranty and the software's author, the holder of the
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
14 # economic rights, and the successive licensors have only limited
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
15 # liability.
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
16 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
17 # In this respect, the user's attention is drawn to the risks associated
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
18 # with loading, using, modifying and/or developing or reproducing the
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
19 # software by the user in light of its specific status of free software,
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
20 # that may mean that it is complicated to manipulate, and that also
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
21 # therefore means that it is reserved for developers and experienced
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
22 # professionals having in-depth computer knowledge. Users are therefore
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
23 # encouraged to load and test the software's suitability as regards their
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
24 # requirements in conditions enabling the security of their systems and/or
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
25 # data to be ensured and, more generally, to use and operate it in the
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
26 # same conditions as regards security.
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
27 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
28 # The fact that you are presently reading this means that you have had
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
29 # knowledge of the CeCILL license and that you accept its terms.
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
30
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
31
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
32 import math
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
33 import re
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
34 from commons.core.seq.Bioseq import Bioseq
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
35
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
36 ## Static methods for sequences manipulation
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
37 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
38 class BioseqUtils(object):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
39
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
40 ## Translate a nucleotide sequence
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
41 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
42 # @param bioSeqInstanceToTranslate a bioseq instance to translate
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
43 # @param phase a integer : 1 (default), 2 or 3
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
44 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
45 def translateSequence(bioSeqInstanceToTranslate, phase=1):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
46 pep = ""
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
47 #length = math.floor((len(self.sequence)-phase-1)/3)*3
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
48 length = int( math.floor( ( len(bioSeqInstanceToTranslate.sequence )-( phase-1 ) )/3 )*3 )
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
49 #We need capital letters !
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
50 bioSeqInstanceToTranslate.upCase()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
51 sequence = bioSeqInstanceToTranslate.sequence
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
52 for i in xrange(phase-1,length,3):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
53 if (sequence[i:i+3] == "TTT" or sequence[i:i+3] == "TTC"):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
54 pep = pep + "F"
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
55 elif ( sequence[i:i+3] == "TTA" or sequence[i:i+3] == "TTG" ):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
56 pep = pep + "L"
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
57 elif ( sequence[i:i+2] == "CT" ):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
58 pep = pep + "L"
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
59 elif ( sequence[i:i+3] == "ATT" or sequence[i:i+3] == "ATC" or sequence[i:i+3] == "ATA" ):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
60 pep = pep + "I"
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
61 elif ( sequence[i:i+3] == "ATG" ):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
62 pep = pep + "M"
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
63 elif ( sequence[i:i+2] == "GT" ):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
64 pep = pep + "V"
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
65 elif ( sequence[i:i+2] == "TC" ) :
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
66 pep = pep + "S"
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
67 elif ( sequence[i:i+2] == "CC" ) :
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
68 pep = pep + "P"
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
69 elif ( sequence[i:i+2] == "AC" ) :
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
70 pep = pep + "T"
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
71 elif ( sequence[i:i+2] == "GC" ) :
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
72 pep = pep + "A"
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
73 elif ( sequence[i:i+3] == "TAT" or sequence[i:i+3] == "TAC" ) :
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
74 pep = pep + "Y"
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
75 elif ( sequence[i:i+3] == "TAA" or sequence[i:i+3] == "TAG" ) :
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
76 pep = pep + "*"
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
77 elif ( sequence[i:i+3] == "CAT" or sequence[i:i+3] == "CAC" ) :
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
78 pep = pep + "H"
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
79 elif ( sequence[i:i+3] == "CAA" or sequence[i:i+3] == "CAG" ) :
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
80 pep = pep + "Q"
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
81 elif ( sequence[i:i+3] == "AAT" or sequence[i:i+3] == "AAC" ) :
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
82 pep = pep + "N"
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
83 elif ( sequence[i:i+3] == "AAA" or sequence[i:i+3] == "AAG" ) :
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
84 pep = pep + "K"
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
85 elif ( sequence[i:i+3] == "GAT" or sequence[i:i+3] == "GAC" ) :
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
86 pep = pep + "D"
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
87 elif ( sequence[i:i+3] == "GAA" or sequence[i:i+3] == "GAG" ) :
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
88 pep = pep + "E"
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
89 elif ( sequence[i:i+3] == "TGT" or sequence[i:i+3] == "TGC" ) :
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
90 pep = pep + "C"
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
91 elif ( sequence[i:i+3] == "TGA" ) :
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
92 pep = pep + "*"
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
93 elif ( sequence[i:i+3] == "TGG" ) :
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
94 pep = pep + "W"
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
95 elif ( sequence[i:i+2] == "CG" ) :
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
96 pep = pep + "R"
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
97 elif ( sequence[i:i+3] == "AGT" or sequence[i:i+3] == "AGC" ) :
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
98 pep = pep + "S"
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
99 elif ( sequence[i:i+3] == "AGA" or sequence[i:i+3] == "AGG" ) :
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
100 pep = pep + "R"
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
101 elif ( sequence[i:i+2] == "GG" ):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
102 pep = pep + "G"
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
103 #We don't know the amino acid because we don't have the nucleotide
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
104 #R Purine (A or G)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
105 #Y Pyrimidine (C, T, or U)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
106 #M C or A
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
107 #K T, U, or G
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
108 #W T, U, or A
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
109 #S C or G
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
110 #B C, T, U, or G (not A)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
111 #D A, T, U, or G (not C)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
112 #H A, T, U, or C (not G)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
113 #V A, C, or G (not T, not U)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
114 #N Unknown nucleotide
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
115 elif ( re.search("N|R|Y|M|K|W|S|B|D|H|V", sequence[i:i+3])):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
116 pep = pep + "X"
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
117 bioSeqInstanceToTranslate.sequence = pep
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
118
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
119 translateSequence = staticmethod(translateSequence)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
120
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
121 ## Add the frame info in header
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
122 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
123 # @param bioSeqInstance a bioseq instance to translate
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
124 # @param phase a integer : 1 , 2 or 3
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
125 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
126 def setFrameInfoOnHeader(bioSeqInstance, phase):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
127 if " " in bioSeqInstance.header:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
128 name, desc = bioSeqInstance.header.split(" ", 1)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
129 name = name + "_" + str(phase)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
130 bioSeqInstance.header = name + " " + desc
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
131 else:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
132 bioSeqInstance.header = bioSeqInstance.header + "_" + str(phase)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
133
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
134 setFrameInfoOnHeader = staticmethod(setFrameInfoOnHeader)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
135
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
136 ## Translate a nucleotide sequence for all frames (positives and negatives)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
137 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
138 # @param bioSeqInstanceToTranslate a bioseq instance to translate
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
139 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
140 def translateInAllFrame( bioSeqInstanceToTranslate ):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
141 positives = BioseqUtils._translateInPositiveFrames( bioSeqInstanceToTranslate )
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
142 negatives = BioseqUtils._translateInNegativeFrames( bioSeqInstanceToTranslate )
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
143 listAll6Frames = []
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
144 listAll6Frames.extend(positives)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
145 listAll6Frames.extend(negatives)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
146 return listAll6Frames
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
147
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
148 translateInAllFrame = staticmethod(translateInAllFrame)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
149
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
150 ## Replace the stop codons by X in sequence
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
151 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
152 # @param bioSeqInstance a bioseq instance
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
153 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
154 def replaceStopCodonsByX( bioSeqInstance ):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
155 bioSeqInstance.sequence = bioSeqInstance.sequence.replace ("*", "X")
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
156
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
157 replaceStopCodonsByX = staticmethod(replaceStopCodonsByX)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
158
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
159 ## Translate in a list all the frames of all the bioseq of bioseq list
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
160 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
161 # @param bioseqList a list of bioseq instances
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
162 # @return a list of translated bioseq instances
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
163 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
164 def translateBioseqListInAllFrames( bioseqList ):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
165 bioseqListInAllFrames = []
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
166 for bioseq in bioseqList :
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
167 bioseqListInAllFrames.extend(BioseqUtils.translateInAllFrame(bioseq))
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
168 return bioseqListInAllFrames
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
169
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
170 translateBioseqListInAllFrames = staticmethod( translateBioseqListInAllFrames )
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
171
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
172 ## Replace the stop codons by X for each sequence of a bioseq list
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
173 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
174 # @param lBioseqWithStops a list of bioseq instances
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
175 # @return a list of bioseq instances
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
176 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
177 def replaceStopCodonsByXInBioseqList ( lBioseqWithStops ):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
178 bioseqListWithStopsreplaced = []
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
179 for bioseq in lBioseqWithStops:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
180 BioseqUtils.replaceStopCodonsByX(bioseq)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
181 bioseqListWithStopsreplaced.append(bioseq)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
182 return bioseqListWithStopsreplaced
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
183
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
184 replaceStopCodonsByXInBioseqList = staticmethod( replaceStopCodonsByXInBioseqList )
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
185
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
186 ## Write a list of bioseq instances in a fasta file (60 characters per line)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
187 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
188 # @param lBioseq a list of bioseq instances
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
189 # @param fileName string
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
190 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
191 def writeBioseqListIntoFastaFile( lBioseq, fileName ):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
192 fout = open(fileName, "w")
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
193 for bioseq in lBioseq:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
194 bioseq.write(fout)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
195 fout.close()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
196
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
197 writeBioseqListIntoFastaFile = staticmethod( writeBioseqListIntoFastaFile )
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
198
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
199 ## read in a fasta file and create a list of bioseq instances
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
200 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
201 # @param fileName string
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
202 # @return a list of bioseq
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
203 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
204 def extractBioseqListFromFastaFile( fileName ):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
205 file = open( fileName )
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
206 lBioseq = []
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
207 currentHeader = ""
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
208 while currentHeader != None:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
209 bioseq = Bioseq()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
210 bioseq.read(file)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
211 currentHeader = bioseq.header
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
212 if currentHeader != None:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
213 lBioseq.append(bioseq)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
214 return lBioseq
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
215
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
216 extractBioseqListFromFastaFile = staticmethod( extractBioseqListFromFastaFile )
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
217
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
218 ## Give the length of a sequence search by name
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
219 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
220 # @param lBioseq a list of bioseq instances
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
221 # @param seqName string
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
222 # @return an integer
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
223 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
224 def getSeqLengthWithSeqName( lBioseq, seqName ):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
225 length = 0
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
226 for bioseq in lBioseq:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
227 if bioseq.header == seqName:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
228 length = bioseq.getLength()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
229 break
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
230 return length
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
231
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
232 getSeqLengthWithSeqName = staticmethod( getSeqLengthWithSeqName )
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
233
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
234 def _translateInPositiveFrames( bioSeqInstanceToTranslate ):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
235 seq1 = bioSeqInstanceToTranslate.copyBioseqInstance()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
236 BioseqUtils.setFrameInfoOnHeader(seq1, 1)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
237 BioseqUtils.translateSequence(seq1, 1)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
238 seq2 = bioSeqInstanceToTranslate.copyBioseqInstance()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
239 BioseqUtils.setFrameInfoOnHeader(seq2, 2)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
240 BioseqUtils.translateSequence(seq2, 2)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
241 seq3 = bioSeqInstanceToTranslate.copyBioseqInstance()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
242 BioseqUtils.setFrameInfoOnHeader(seq3, 3)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
243 BioseqUtils.translateSequence(seq3, 3)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
244 return [seq1, seq2, seq3]
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
245
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
246 _translateInPositiveFrames = staticmethod( _translateInPositiveFrames )
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
247
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
248 def _translateInNegativeFrames(bioSeqInstanceToTranslate):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
249 seq4 = bioSeqInstanceToTranslate.copyBioseqInstance()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
250 seq4.reverseComplement()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
251 BioseqUtils.setFrameInfoOnHeader(seq4, 4)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
252 BioseqUtils.translateSequence(seq4, 1)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
253 seq5 = bioSeqInstanceToTranslate.copyBioseqInstance()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
254 seq5.reverseComplement()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
255 BioseqUtils.setFrameInfoOnHeader(seq5, 5)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
256 BioseqUtils.translateSequence(seq5, 2)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
257 seq6 = bioSeqInstanceToTranslate.copyBioseqInstance()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
258 seq6.reverseComplement()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
259 BioseqUtils.setFrameInfoOnHeader(seq6, 6)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
260 BioseqUtils.translateSequence(seq6, 3)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
261 return [seq4, seq5, seq6]
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
262
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
263 _translateInNegativeFrames = staticmethod( _translateInNegativeFrames )
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
264
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
265
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
266 ## Return a dictionary which keys are sequence headers and values sequence lengths.
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
267 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
268 def getLengthPerSeqFromFile( inFile ):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
269 dHeader2Length = {}
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
270 inFileHandler = open( inFile, "r" )
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
271 while True:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
272 iBs = Bioseq()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
273 iBs.read( inFileHandler )
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
274 if iBs.sequence == None:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
275 break
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
276 dHeader2Length[ iBs.header ] = iBs.getLength()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
277 inFileHandler.close()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
278 return dHeader2Length
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
279
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
280 getLengthPerSeqFromFile = staticmethod( getLengthPerSeqFromFile )
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
281
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
282
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
283 ## Return the list of Bioseq instances, these being sorted in decreasing length
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
284 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
285 def getBioseqListSortedByDecreasingLength( lBioseqs ):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
286 return sorted( lBioseqs, key=lambda iBs: ( iBs.getLength() ), reverse=True )
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
287
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
288 getBioseqListSortedByDecreasingLength = staticmethod( getBioseqListSortedByDecreasingLength )
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
289
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
290
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
291 ## Return the list of Bioseq instances, these being sorted in decreasing length (without gaps)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
292 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
293 def getBioseqListSortedByDecreasingLengthWithoutGaps( lBioseqs ):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
294 return sorted( lBioseqs, key=lambda iBs: ( len(iBs.sequence.replace("-","")) ), reverse=True )
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
295
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
296 getBioseqListSortedByDecreasingLengthWithoutGaps = staticmethod( getBioseqListSortedByDecreasingLengthWithoutGaps )