annotate TEisotools-1.0/commons/core/seq/BioseqUtils.py @ 6:20ec0d14798e draft

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