annotate commons/core/seq/BioseqUtils.py @ 58:5f5c9b74c2dd

Uploaded
author m-zytnicki
date Fri, 07 Feb 2014 11:53:36 -0500
parents 769e306b7933
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1 # 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 math
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
33 import re
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
34 from commons.core.seq.Bioseq import Bioseq
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
35
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
36 ## Static methods for sequences manipulation
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
37 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
38 class BioseqUtils(object):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
39
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
40 ## Translate a nucleotide sequence
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
41 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
42 # @param bioSeqInstanceToTranslate a bioseq instance to translate
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
43 # @param phase a integer : 1 (default), 2 or 3
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
44 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
45 def translateSequence(bioSeqInstanceToTranslate, phase=1):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
46 pep = ""
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
47 #length = math.floor((len(self.sequence)-phase-1)/3)*3
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
48 length = int( math.floor( ( len(bioSeqInstanceToTranslate.sequence )-( phase-1 ) )/3 )*3 )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
49 #We need capital letters !
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
50 bioSeqInstanceToTranslate.upCase()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
51 sequence = bioSeqInstanceToTranslate.sequence
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
52 for i in xrange(phase-1,length,3):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
53 if (sequence[i:i+3] == "TTT" or sequence[i:i+3] == "TTC"):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
54 pep = pep + "F"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
55 elif ( sequence[i:i+3] == "TTA" or sequence[i:i+3] == "TTG" ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
56 pep = pep + "L"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
57 elif ( sequence[i:i+2] == "CT" ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
58 pep = pep + "L"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
59 elif ( sequence[i:i+3] == "ATT" or sequence[i:i+3] == "ATC" or sequence[i:i+3] == "ATA" ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
60 pep = pep + "I"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
61 elif ( sequence[i:i+3] == "ATG" ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
62 pep = pep + "M"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
63 elif ( sequence[i:i+2] == "GT" ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
64 pep = pep + "V"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
65 elif ( sequence[i:i+2] == "TC" ) :
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
66 pep = pep + "S"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
67 elif ( sequence[i:i+2] == "CC" ) :
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
68 pep = pep + "P"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
69 elif ( sequence[i:i+2] == "AC" ) :
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
70 pep = pep + "T"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
71 elif ( sequence[i:i+2] == "GC" ) :
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
72 pep = pep + "A"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
73 elif ( sequence[i:i+3] == "TAT" or sequence[i:i+3] == "TAC" ) :
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
74 pep = pep + "Y"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
75 elif ( sequence[i:i+3] == "TAA" or sequence[i:i+3] == "TAG" ) :
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
76 pep = pep + "*"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
77 elif ( sequence[i:i+3] == "CAT" or sequence[i:i+3] == "CAC" ) :
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
78 pep = pep + "H"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
79 elif ( sequence[i:i+3] == "CAA" or sequence[i:i+3] == "CAG" ) :
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
80 pep = pep + "Q"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
81 elif ( sequence[i:i+3] == "AAT" or sequence[i:i+3] == "AAC" ) :
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
82 pep = pep + "N"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
83 elif ( sequence[i:i+3] == "AAA" or sequence[i:i+3] == "AAG" ) :
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
84 pep = pep + "K"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
85 elif ( sequence[i:i+3] == "GAT" or sequence[i:i+3] == "GAC" ) :
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
86 pep = pep + "D"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
87 elif ( sequence[i:i+3] == "GAA" or sequence[i:i+3] == "GAG" ) :
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
88 pep = pep + "E"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
89 elif ( sequence[i:i+3] == "TGT" or sequence[i:i+3] == "TGC" ) :
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
90 pep = pep + "C"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
91 elif ( sequence[i:i+3] == "TGA" ) :
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
92 pep = pep + "*"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
93 elif ( sequence[i:i+3] == "TGG" ) :
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
94 pep = pep + "W"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
95 elif ( sequence[i:i+2] == "CG" ) :
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
96 pep = pep + "R"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
97 elif ( sequence[i:i+3] == "AGT" or sequence[i:i+3] == "AGC" ) :
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
98 pep = pep + "S"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
99 elif ( sequence[i:i+3] == "AGA" or sequence[i:i+3] == "AGG" ) :
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
100 pep = pep + "R"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
101 elif ( sequence[i:i+2] == "GG" ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
102 pep = pep + "G"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
103 #We don't know the amino acid because we don't have the nucleotide
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
104 #R Purine (A or G)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
105 #Y Pyrimidine (C, T, or U)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
106 #M C or A
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
107 #K T, U, or G
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
108 #W T, U, or A
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
109 #S C or G
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
110 #B C, T, U, or G (not A)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
111 #D A, T, U, or G (not C)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
112 #H A, T, U, or C (not G)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
113 #V A, C, or G (not T, not U)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
114 #N Unknown nucleotide
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
115 elif ( re.search("N|R|Y|M|K|W|S|B|D|H|V", sequence[i:i+3])):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
116 pep = pep + "X"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
117 bioSeqInstanceToTranslate.sequence = pep
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
118
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
119 translateSequence = staticmethod(translateSequence)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
120
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
121 ## Add the frame info in header
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
122 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
123 # @param bioSeqInstance a bioseq instance to translate
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
124 # @param phase a integer : 1 , 2 or 3
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
125 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
126 def setFrameInfoOnHeader(bioSeqInstance, phase):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
127 if " " in bioSeqInstance.header:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
128 name, desc = bioSeqInstance.header.split(" ", 1)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
129 name = name + "_" + str(phase)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
130 bioSeqInstance.header = name + " " + desc
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
131 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
132 bioSeqInstance.header = bioSeqInstance.header + "_" + str(phase)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
133
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
134 setFrameInfoOnHeader = staticmethod(setFrameInfoOnHeader)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
135
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
136 ## Translate a nucleotide sequence for all frames (positives and negatives)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
137 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
138 # @param bioSeqInstanceToTranslate a bioseq instance to translate
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
139 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
140 def translateInAllFrame( bioSeqInstanceToTranslate ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
141 positives = BioseqUtils._translateInPositiveFrames( bioSeqInstanceToTranslate )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
142 negatives = BioseqUtils._translateInNegativeFrames( bioSeqInstanceToTranslate )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
143 listAll6Frames = []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
144 listAll6Frames.extend(positives)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
145 listAll6Frames.extend(negatives)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
146 return listAll6Frames
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
147
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
148 translateInAllFrame = staticmethod(translateInAllFrame)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
149
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
150 ## Replace the stop codons by X in sequence
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
151 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
152 # @param bioSeqInstance a bioseq instance
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
153 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
154 def replaceStopCodonsByX( bioSeqInstance ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
155 bioSeqInstance.sequence = bioSeqInstance.sequence.replace ("*", "X")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
156
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
157 replaceStopCodonsByX = staticmethod(replaceStopCodonsByX)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
158
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
159 ## Translate in a list all the frames of all the bioseq of bioseq list
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
160 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
161 # @param bioseqList a list of bioseq instances
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
162 # @return a list of translated bioseq instances
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
163 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
164 def translateBioseqListInAllFrames( bioseqList ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
165 bioseqListInAllFrames = []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
166 for bioseq in bioseqList :
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
167 bioseqListInAllFrames.extend(BioseqUtils.translateInAllFrame(bioseq))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
168 return bioseqListInAllFrames
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
169
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
170 translateBioseqListInAllFrames = staticmethod( translateBioseqListInAllFrames )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
171
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
172 ## Replace the stop codons by X for each sequence of a bioseq list
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
173 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
174 # @param lBioseqWithStops a list of bioseq instances
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
175 # @return a list of bioseq instances
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
176 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
177 def replaceStopCodonsByXInBioseqList ( lBioseqWithStops ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
178 bioseqListWithStopsreplaced = []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
179 for bioseq in lBioseqWithStops:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
180 BioseqUtils.replaceStopCodonsByX(bioseq)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
181 bioseqListWithStopsreplaced.append(bioseq)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
182 return bioseqListWithStopsreplaced
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
183
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
184 replaceStopCodonsByXInBioseqList = staticmethod( replaceStopCodonsByXInBioseqList )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
185
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
186 ## Write a list of bioseq instances in a fasta file (60 characters per line)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
187 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
188 # @param lBioseq a list of bioseq instances
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
189 # @param fileName string
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
190 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
191 def writeBioseqListIntoFastaFile( lBioseq, fileName ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
192 fout = open(fileName, "w")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
193 for bioseq in lBioseq:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
194 bioseq.write(fout)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
195 fout.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
196
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
197 writeBioseqListIntoFastaFile = staticmethod( writeBioseqListIntoFastaFile )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
198
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
199 ## read in a fasta file and create a list of bioseq instances
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
200 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
201 # @param fileName string
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
202 # @return a list of bioseq
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
203 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
204 def extractBioseqListFromFastaFile( fileName ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
205 file = open( fileName )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
206 lBioseq = []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
207 currentHeader = ""
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
208 while currentHeader != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
209 bioseq = Bioseq()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
210 bioseq.read(file)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
211 currentHeader = bioseq.header
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
212 if currentHeader != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
213 lBioseq.append(bioseq)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
214 return lBioseq
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
215
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
216 extractBioseqListFromFastaFile = staticmethod( extractBioseqListFromFastaFile )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
217
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
218 ## Give the length of a sequence search by name
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
219 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
220 # @param lBioseq a list of bioseq instances
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
221 # @param seqName string
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
222 # @return an integer
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
223 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
224 def getSeqLengthWithSeqName( lBioseq, seqName ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
225 length = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
226 for bioseq in lBioseq:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
227 if bioseq.header == seqName:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
228 length = bioseq.getLength()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
229 break
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
230 return length
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
231
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
232 getSeqLengthWithSeqName = staticmethod( getSeqLengthWithSeqName )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
233
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
234 def _translateInPositiveFrames( bioSeqInstanceToTranslate ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
235 seq1 = bioSeqInstanceToTranslate.copyBioseqInstance()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
236 BioseqUtils.setFrameInfoOnHeader(seq1, 1)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
237 BioseqUtils.translateSequence(seq1, 1)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
238 seq2 = bioSeqInstanceToTranslate.copyBioseqInstance()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
239 BioseqUtils.setFrameInfoOnHeader(seq2, 2)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
240 BioseqUtils.translateSequence(seq2, 2)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
241 seq3 = bioSeqInstanceToTranslate.copyBioseqInstance()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
242 BioseqUtils.setFrameInfoOnHeader(seq3, 3)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
243 BioseqUtils.translateSequence(seq3, 3)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
244 return [seq1, seq2, seq3]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
245
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
246 _translateInPositiveFrames = staticmethod( _translateInPositiveFrames )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
247
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
248 def _translateInNegativeFrames(bioSeqInstanceToTranslate):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
249 seq4 = bioSeqInstanceToTranslate.copyBioseqInstance()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
250 seq4.reverseComplement()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
251 BioseqUtils.setFrameInfoOnHeader(seq4, 4)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
252 BioseqUtils.translateSequence(seq4, 1)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
253 seq5 = bioSeqInstanceToTranslate.copyBioseqInstance()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
254 seq5.reverseComplement()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
255 BioseqUtils.setFrameInfoOnHeader(seq5, 5)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
256 BioseqUtils.translateSequence(seq5, 2)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
257 seq6 = bioSeqInstanceToTranslate.copyBioseqInstance()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
258 seq6.reverseComplement()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
259 BioseqUtils.setFrameInfoOnHeader(seq6, 6)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
260 BioseqUtils.translateSequence(seq6, 3)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
261 return [seq4, seq5, seq6]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
262
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
263 _translateInNegativeFrames = staticmethod( _translateInNegativeFrames )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
264
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
265
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
266 ## Return a dictionary which keys are sequence headers and values sequence lengths.
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
267 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
268 def getLengthPerSeqFromFile( inFile ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
269 dHeader2Length = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
270 inFileHandler = open( inFile, "r" )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
271 while True:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
272 iBs = Bioseq()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
273 iBs.read( inFileHandler )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
274 if iBs.sequence == None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
275 break
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
276 dHeader2Length[ iBs.header ] = iBs.getLength()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
277 inFileHandler.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
278 return dHeader2Length
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
279
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
280 getLengthPerSeqFromFile = staticmethod( getLengthPerSeqFromFile )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
281
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
282
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
283 ## Return the list of Bioseq instances, these being sorted in decreasing length
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
284 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
285 def getBioseqListSortedByDecreasingLength( lBioseqs ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
286 return sorted( lBioseqs, key=lambda iBs: ( iBs.getLength() ), reverse=True )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
287
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
288 getBioseqListSortedByDecreasingLength = staticmethod( getBioseqListSortedByDecreasingLength )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
289
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
290
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
291 ## Return the list of Bioseq instances, these being sorted in decreasing length (without gaps)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
292 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
293 def getBioseqListSortedByDecreasingLengthWithoutGaps( lBioseqs ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
294 return sorted( lBioseqs, key=lambda iBs: ( len(iBs.sequence.replace("-","")) ), reverse=True )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
295
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
296 getBioseqListSortedByDecreasingLengthWithoutGaps = staticmethod( getBioseqListSortedByDecreasingLengthWithoutGaps )