annotate TEisotools-1.0/commons/core/seq/AlignedBioseqDB.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 sys
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
33 from commons.core.seq.BioseqDB import BioseqDB
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
34 from commons.core.seq.Bioseq import Bioseq
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
35 from commons.core.coord.Align import Align
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
36 from commons.core.coord.Range import Range
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
37 from commons.core.stat.Stat import Stat
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
38 from math import log
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
39
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
40
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
41 ## Multiple Sequence Alignment Representation
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
42 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
43 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
44 class AlignedBioseqDB( BioseqDB ):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
45
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
46 def __init__( self, name="" ):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
47 BioseqDB.__init__( self, name )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
48 seqLength = self.getLength()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
49 if self.getSize() > 1:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
50 for bs in self.db[1:]:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
51 if bs.getLength() != seqLength:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
52 print "ERROR: aligned sequences have different length"
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
53
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
54
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
55 ## Get length of the alignment
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
56 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
57 # @return length
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
58 # @warning name before migration was 'length'
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
59 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
60 def getLength( self ):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
61 length = 0
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
62 if self.db != []:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
63 length = self.db[0].getLength()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
64 return length
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
65
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
66
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
67 ## Get the true length of a given sequence (without gaps)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
68 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
69 # @param header string header of the sequence to analyze
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
70 # @return length integer
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
71 # @warning name before migration was 'true_length'
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
72 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
73 def getSeqLengthWithoutGaps( self, header ):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
74 bs = self.fetch( header )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
75 count = 0
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
76 for pos in xrange(0,len(bs.sequence)):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
77 if bs.sequence[pos] != "-":
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
78 count += 1
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
79 return count
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
80
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
81 def cleanMSA( self ):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
82 #TODO: Refactoring
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
83 """clean the MSA"""
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
84 i2del = []
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
85
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
86 # for each sequence in the MSA
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
87 for seqi in xrange(0,self.getSize()):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
88 if seqi in i2del:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
89 continue
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
90 #define it as the reference
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
91 ref = self.db[seqi].sequence
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
92 refHeader = self.db[seqi].header
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
93 # for each following sequence
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
94 for seq_next in xrange(seqi+1,self.getSize()):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
95 if seq_next in i2del:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
96 continue
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
97 keep = 0
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
98 # for each position along the MSA
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
99 for posx in xrange(0,self.getLength()):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
100 seq = self.db[seq_next].sequence
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
101 if seq[posx] != '-' and ref[posx] != '-':
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
102 keep = 1
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
103 break
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
104 seqHeader = self.db[seq_next].header
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
105 # if there is at least one gap between the ref seq and the other seq
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
106 # keep track of the shortest by recording it in "i2del"
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
107 if keep == 0:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
108
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
109 if self.getSeqLengthWithoutGaps(refHeader) < self.getSeqLengthWithoutGaps(seqHeader):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
110 if seqi not in i2del:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
111 i2del.append( seqi )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
112 else:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
113 if seq_next not in i2del:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
114 i2del.append( seq_next )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
115
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
116 # delete from the MSA each seq present in the list "i2del"
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
117 for i in reversed(sorted(set(i2del))):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
118 del self.db[i]
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
119
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
120 self.idx = {}
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
121 count = 0
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
122 for i in self.db:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
123 self.idx[i.header] = count
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
124 count += 1
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
125
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
126 ## Record the occurrences of symbols (A, T, G, C, N, -, ...) at each site
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
127 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
128 # @return: list of dico whose keys are symbols and values are their occurrences
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
129 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
130 def getListOccPerSite( self ):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
131 lOccPerSite = [] # list of dictionaries, one per position on the sequence
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
132 n = 0 # nb of sequences parsed from the input file
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
133 firstSeq = True
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
134
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
135 # for each sequence in the bank
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
136 for bs in self.db:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
137 if bs.sequence == None:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
138 break
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
139 n += 1
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
140
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
141 # if it is the first to be parsed, create a dico at each site
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
142 if firstSeq:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
143 for i in xrange(0,len(bs.sequence)):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
144 lOccPerSite.append( {} )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
145 firstSeq = False
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
146
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
147 # for each site, add its nucleotide
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
148 for i in xrange(0,len(bs.sequence)):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
149 nuc = bs.sequence[i].upper()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
150 if lOccPerSite[i].has_key( nuc ):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
151 lOccPerSite[i][nuc] += 1
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
152 else:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
153 lOccPerSite[i][nuc] = 1
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
154
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
155 return lOccPerSite
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
156
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
157 #TODO: review minNbNt !!! It should be at least 2 nucleotides to build a consensus...
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
158 ## Make a consensus from the MSA
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
159 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
160 # @param minNbNt: minimum nb of nucleotides to edit a consensus
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
161 # @param minPropNt: minimum proportion for the major nucleotide to be used, otherwise add 'N' (default=0.0)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
162 # @param verbose: level of information sent to stdout (default=0/1)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
163 # @return: consensus
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
164 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
165 def getConsensus( self, minNbNt, minPropNt=0.0, verbose=0 , isHeaderSAtannot=False):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
166
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
167 maxPropN = 0.40 # discard consensus if more than 40% of N's
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
168
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
169 nbInSeq = self.getSize()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
170 if verbose > 0:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
171 print "nb of aligned sequences: %i" % ( nbInSeq ); sys.stdout.flush()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
172 if nbInSeq < 2:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
173 print "ERROR: can't make a consensus with less than 2 sequences"
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
174 sys.exit(1)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
175 if minNbNt >= nbInSeq:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
176 minNbNt = nbInSeq - 1
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
177 print "minNbNt=%i" % ( minNbNt )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
178 if minPropNt >= 1.0:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
179 print "ERROR: minPropNt=%.2f should be a proportion (below 1.0)" % ( minPropNt )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
180 sys.exit(1)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
181
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
182 lOccPerSite = self.getListOccPerSite()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
183 nbSites = len(lOccPerSite)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
184 if verbose > 0:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
185 print "nb of sites: %i" % ( nbSites ); sys.stdout.flush()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
186
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
187 seqConsensus = ""
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
188
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
189 # for each site (i.e. each column of the MSA)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
190 nbRmvColumns = 0
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
191 countSites = 0
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
192 for dNt2Occ in lOccPerSite:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
193 countSites += 1
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
194 if verbose > 1:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
195 print "site %s / %i" % ( str(countSites).zfill( len(str(nbSites)) ),
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
196 nbSites )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
197 sys.stdout.flush()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
198 occMaxNt = 0 # occurrences of the predominant nucleotide at this site
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
199 lBestNt = []
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
200 nbNt = 0 # total nb of A, T, G and C (no gap)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
201
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
202 # for each distinct symbol at this site (A, T, G, C, N, -,...)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
203 for j in dNt2Occ.keys():
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
204 if j != "-":
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
205 nbNt += dNt2Occ[j]
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
206 if verbose > 1:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
207 print "%s: %i" % ( j, dNt2Occ[j] )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
208 if dNt2Occ[j] > occMaxNt:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
209 occMaxNt = dNt2Occ[j]
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
210 lBestNt = [ j ]
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
211 elif dNt2Occ[j] == occMaxNt:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
212 lBestNt.append( j )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
213 if nbNt == 0: # some MSA programs can remove some sequences (e.g. Muscle after Recon) or when using Refalign (non-alignable TE fragments put together via a refseq)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
214 nbRmvColumns += 1
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
215
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
216 if len( lBestNt ) >= 1:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
217 bestNt = lBestNt[0]
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
218
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
219 # if the predominant nucleotide occurs in less than x% of the sequences, put a "N"
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
220 if minPropNt > 0.0 and nbNt != 0 and float(occMaxNt)/float(nbNt) < minPropNt:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
221 bestNt = "N"
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
222
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
223 if int(nbNt) >= int(minNbNt):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
224 seqConsensus += bestNt
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
225 if verbose > 1:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
226 print "-> %s" % ( bestNt )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
227
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
228 if nbRmvColumns:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
229 if nbRmvColumns == 1:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
230 print "WARNING: 1 site was removed (%.2f%%)" % (nbRmvColumns / float(nbSites) * 100)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
231 else:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
232 print "WARNING: %i sites were removed (%.2f%%)" % ( nbRmvColumns, nbRmvColumns / float(nbSites) * 100 )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
233 sys.stdout.flush()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
234 if seqConsensus == "":
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
235 print "WARNING: no consensus can be built (no sequence left)"
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
236 return
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
237
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
238 propN = seqConsensus.count("N") / float(len(seqConsensus))
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
239 if propN >= maxPropN:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
240 print "WARNING: no consensus can be built (%i%% of N's >= %i%%)" % ( propN * 100, maxPropN * 100 )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
241 return
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
242 elif propN >= maxPropN * 0.5:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
243 print "WARNING: %i%% of N's" % ( propN * 100 )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
244
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
245 consensus = Bioseq()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
246 consensus.sequence = seqConsensus
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
247 if isHeaderSAtannot:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
248 header = self.db[0].header
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
249 pyramid = header.split("Gr")[1].split("Cl")[0]
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
250 pile = header.split("Cl")[1].split(" ")[0]
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
251 consensus.header = "consensus=%s length=%i nbAlign=%i pile=%s pyramid=%s" % (self.name, len(seqConsensus), self.getSize(), pile, pyramid)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
252 else:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
253 consensus.header = "consensus=%s length=%i nbAlign=%i" % ( self.name, len(seqConsensus), self.getSize() )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
254
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
255 if verbose > 0:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
256
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
257 statEntropy = self.getEntropy( verbose - 1 )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
258 print "entropy: %s" % ( statEntropy.stringQuantiles() )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
259 sys.stdout.flush()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
260
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
261 return consensus
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
262
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
263
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
264 ## Get the entropy of the whole multiple alignment (only for A, T, G and C)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
265 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
266 # @param verbose level of verbosity
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
267 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
268 # @return statistics about the entropy of the MSA
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
269 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
270 def getEntropy( self, verbose=0 ):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
271
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
272 stats = Stat()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
273
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
274 # get the occurrences of symbols at each site
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
275 lOccPerSite = self.getListOccPerSite()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
276
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
277 countSite = 0
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
278
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
279 # for each site
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
280 for dSymbol2Occ in lOccPerSite:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
281 countSite += 1
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
282
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
283 # count the number of nucleotides (A, T, G and C, doesn't count gap '-')
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
284 nbNt = 0
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
285 dATGC2Occ = {}
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
286 for base in ["A","T","G","C"]:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
287 dATGC2Occ[ base ] = 0.0
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
288 for nt in dSymbol2Occ.keys():
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
289 if nt != "-":
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
290 nbNt += dSymbol2Occ[ nt ]
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
291 checkedNt = self.getATGCNFromIUPAC( nt )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
292 if checkedNt in ["A","T","G","C"] and dSymbol2Occ.has_key( checkedNt ):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
293 dATGC2Occ[ checkedNt ] += 1 * dSymbol2Occ[ checkedNt ]
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
294 else: # for 'N'
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
295 if dSymbol2Occ.has_key( checkedNt ):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
296 dATGC2Occ[ "A" ] += 0.25 * dSymbol2Occ[ checkedNt ]
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
297 dATGC2Occ[ "T" ] += 0.25 * dSymbol2Occ[ checkedNt ]
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
298 dATGC2Occ[ "G" ] += 0.25 * dSymbol2Occ[ checkedNt ]
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
299 dATGC2Occ[ "C" ] += 0.25 * dSymbol2Occ[ checkedNt ]
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
300 if verbose > 2:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
301 for base in dATGC2Occ.keys():
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
302 print "%s: %i" % ( base, dATGC2Occ[ base ] )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
303
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
304 # compute the entropy for the site
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
305 entropySite = 0.0
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
306 for nt in dATGC2Occ.keys():
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
307 entropySite += self.computeEntropy( dATGC2Occ[ nt ], nbNt )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
308 if verbose > 1:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
309 print "site %i (%i nt): entropy = %.3f" % ( countSite, nbNt, entropySite )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
310 stats.add( entropySite )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
311
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
312 return stats
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
313
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
314
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
315 ## Get A, T, G, C or N from an IUPAC letter
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
316 # IUPAC = ['A','T','G','C','U','R','Y','M','K','W','S','B','D','H','V','N']
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
317 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
318 # @return A, T, G, C or N
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
319 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
320 def getATGCNFromIUPAC( self, nt ):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
321 iBs = Bioseq()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
322 return iBs.getATGCNFromIUPAC( nt )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
323
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
324
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
325 ## Compute the entropy based on the occurrences of a certain nucleotide and the total number of nucleotides
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
326 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
327 def computeEntropy( self, nbOcc, nbNt ):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
328 if nbOcc == 0.0:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
329 return 0.0
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
330 else:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
331 freq = nbOcc / float(nbNt)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
332 return - freq * log(freq) / log(2)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
333
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
334
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
335 ## Save the multiple alignment as a matrix with '0' if gap, '1' otherwise
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
336 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
337 def saveAsBinaryMatrix( self, outFile ):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
338 outFileHandler = open( outFile, "w" )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
339 for bs in self.db:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
340 string = "%s" % ( bs.header )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
341 for nt in bs.sequence:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
342 if nt != "-":
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
343 string += "\t%i" % ( 1 )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
344 else:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
345 string += "\t%i" % ( 0 )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
346 outFileHandler.write( "%s\n" % ( string ) )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
347 outFileHandler.close()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
348
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
349
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
350 ## Return a list of Align instances corresponding to the aligned regions (without gaps)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
351 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
352 # @param query string header of the sequence considered as query
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
353 # @param subject string header of the sequence considered as subject
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
354 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
355 def getAlignList( self, query, subject ):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
356 lAligns = []
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
357 alignQ = self.fetch( query ).sequence
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
358 alignS = self.fetch( subject ).sequence
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
359 createNewAlign = True
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
360 indexAlign = 0
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
361 indexQ = 0
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
362 indexS = 0
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
363 while indexAlign < len(alignQ):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
364 if alignQ[ indexAlign ] != "-" and alignS[ indexAlign ] != "-":
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
365 indexQ += 1
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
366 indexS += 1
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
367 if createNewAlign:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
368 iAlign = Align( Range( query, indexQ, indexQ ),
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
369 Range( subject, indexS, indexS ),
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
370 0,
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
371 int( alignQ[ indexAlign ] == alignS[ indexAlign ] ),
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
372 int( alignQ[ indexAlign ] == alignS[ indexAlign ] ) )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
373 lAligns.append( iAlign )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
374 createNewAlign = False
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
375 else:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
376 lAligns[-1].range_query.end += 1
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
377 lAligns[-1].range_subject.end += 1
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
378 lAligns[-1].score += int( alignQ[ indexAlign ] == alignS[ indexAlign ] )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
379 lAligns[-1].identity += int( alignQ[ indexAlign ] == alignS[ indexAlign ] )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
380 else:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
381 if not createNewAlign:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
382 lAligns[-1].identity = 100 * lAligns[-1].identity / lAligns[-1].getLengthOnQuery()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
383 createNewAlign = True
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
384 if alignQ[ indexAlign ] != "-":
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
385 indexQ += 1
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
386 elif alignS[ indexAlign ] != "-":
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
387 indexS += 1
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
388 indexAlign += 1
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
389 if not createNewAlign:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
390 lAligns[-1].identity = 100 * lAligns[-1].identity / lAligns[-1].getLengthOnQuery()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
391 return lAligns
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
392
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
393
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
394 def removeGaps(self):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
395 for iBs in self.db:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
396 iBs.removeSymbol( "-" )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
397
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
398 ## Compute mean per cent identity for MSA.
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
399 # First sequence in MSA is considered as reference sequence.
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
400 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
401 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
402 def computeMeanPcentIdentity(self):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
403 seqRef = self.db[0]
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
404 sumPcentIdentity = 0
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
405
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
406 for seq in self.db[1:]:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
407 pcentIdentity = self._computePcentIdentityBetweenSeqRefAndCurrentSeq(seqRef, seq)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
408 sumPcentIdentity = sumPcentIdentity + pcentIdentity
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
409
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
410 nbSeq = len(self.db[1:])
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
411 meanPcentIdentity = round (sumPcentIdentity/nbSeq)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
412
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
413 return meanPcentIdentity
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
414
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
415 def _computePcentIdentityBetweenSeqRefAndCurrentSeq(self, seqRef, seq):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
416 indexOnSeqRef = 0
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
417 sumIdentity = 0
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
418 for nuclSeq in seq.sequence:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
419 nuclRef = seqRef.sequence[indexOnSeqRef]
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
420
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
421 if nuclRef != "-" and nuclRef == nuclSeq:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
422 sumIdentity = sumIdentity + 1
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
423 indexOnSeqRef = indexOnSeqRef + 1
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
424
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
425 return float(sumIdentity) / float(seqRef.getLength()) * 100
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
426
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
427
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
428
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
429
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
430
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
431
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
432
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
433
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
434
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
435
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
436
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
437
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
438
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
439
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
440