annotate commons/core/seq/AlignedBioseqDB.py @ 14:c79b9ae3f65f

Deleted selected files
author m-zytnicki
date Fri, 19 Apr 2013 10:13:11 -0400
parents 769e306b7933
children 94ab73e8a190
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1 # Copyright INRA (Institut National de la Recherche Agronomique)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
2 # http://www.inra.fr
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
3 # http://urgi.versailles.inra.fr
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
4 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
5 # This software is governed by the CeCILL license under French law and
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
6 # abiding by the rules of distribution of free software. You can use,
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
7 # modify and/ or redistribute the software under the terms of the CeCILL
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
8 # license as circulated by CEA, CNRS and INRIA at the following URL
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
9 # "http://www.cecill.info".
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
10 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
11 # As a counterpart to the access to the source code and rights to copy,
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
12 # modify and redistribute granted by the license, users are provided only
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
13 # with a limited warranty and the software's author, the holder of the
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
14 # economic rights, and the successive licensors have only limited
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
15 # liability.
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
16 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
17 # In this respect, the user's attention is drawn to the risks associated
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
18 # with loading, using, modifying and/or developing or reproducing the
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
19 # software by the user in light of its specific status of free software,
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
20 # that may mean that it is complicated to manipulate, and that also
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
21 # therefore means that it is reserved for developers and experienced
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
22 # professionals having in-depth computer knowledge. Users are therefore
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
23 # encouraged to load and test the software's suitability as regards their
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
24 # requirements in conditions enabling the security of their systems and/or
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
25 # data to be ensured and, more generally, to use and operate it in the
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
26 # same conditions as regards security.
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
27 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
28 # The fact that you are presently reading this means that you have had
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
29 # knowledge of the CeCILL license and that you accept its terms.
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
30
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
31
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
32 import sys
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
33 from commons.core.seq.BioseqDB import BioseqDB
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 from commons.core.coord.Align import Align
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
36 from commons.core.coord.Range import Range
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
37 from commons.core.stat.Stat import Stat
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
38 from math import log
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
39
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
40
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
41 ## Multiple Sequence Alignment Representation
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
42 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
43 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
44 class AlignedBioseqDB( BioseqDB ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
45
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
46 def __init__( self, name="" ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
47 BioseqDB.__init__( self, name )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
48 seqLength = self.getLength()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
49 if self.getSize() > 1:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
50 for bs in self.db[1:]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
51 if bs.getLength() != seqLength:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
52 print "ERROR: aligned sequences have different length"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
53
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
54
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
55 ## Get length of the alignment
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
56 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
57 # @return length
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
58 # @warning name before migration was 'length'
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
59 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
60 def getLength( self ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
61 length = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
62 if self.db != []:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
63 length = self.db[0].getLength()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
64 return length
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
65
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
66
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
67 ## Get the true length of a given sequence (without gaps)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
68 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
69 # @param header string header of the sequence to analyze
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
70 # @return length integer
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
71 # @warning name before migration was 'true_length'
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
72 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
73 def getSeqLengthWithoutGaps( self, header ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
74 bs = self.fetch( header )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
75 count = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
76 for pos in xrange(0,len(bs.sequence)):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
77 if bs.sequence[pos] != "-":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
78 count += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
79 return count
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
80
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
81
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
82 ## Record the occurrences of symbols (A, T, G, C, N, -, ...) at each site
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
83 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
84 # @return: list of dico whose keys are symbols and values are their occurrences
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
85 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
86 def getListOccPerSite( self ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
87 lOccPerSite = [] # list of dictionaries, one per position on the sequence
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
88 n = 0 # nb of sequences parsed from the input file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
89 firstSeq = True
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
90
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
91 # for each sequence in the bank
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
92 for bs in self.db:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
93 if bs.sequence == None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
94 break
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
95 n += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
96
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
97 # if it is the first to be parsed, create a dico at each site
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
98 if firstSeq:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
99 for i in xrange(0,len(bs.sequence)):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
100 lOccPerSite.append( {} )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
101 firstSeq = False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
102
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
103 # for each site, add its nucleotide
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
104 for i in xrange(0,len(bs.sequence)):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
105 nuc = bs.sequence[i].upper()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
106 if lOccPerSite[i].has_key( nuc ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
107 lOccPerSite[i][nuc] += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
108 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
109 lOccPerSite[i][nuc] = 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
110
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
111 return lOccPerSite
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
112
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
113 #TODO: review minNbNt !!! It should be at least 2 nucleotides to build a consensus...
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
114 ## Make a consensus from the MSA
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
115 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
116 # @param minNbNt: minimum nb of nucleotides to edit a consensus
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
117 # @param minPropNt: minimum proportion for the major nucleotide to be used, otherwise add 'N' (default=0.0)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
118 # @param verbose: level of information sent to stdout (default=0/1)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
119 # @return: consensus
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
120 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
121 def getConsensus( self, minNbNt, minPropNt=0.0, verbose=0 , isHeaderSAtannot=False):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
122
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
123 maxPropN = 0.40 # discard consensus if more than 40% of N's
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
124
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
125 nbInSeq = self.getSize()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
126 if verbose > 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
127 print "nb of aligned sequences: %i" % ( nbInSeq ); sys.stdout.flush()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
128 if nbInSeq < 2:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
129 print "ERROR: can't make a consensus with less than 2 sequences"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
130 sys.exit(1)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
131 if minNbNt >= nbInSeq:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
132 minNbNt = nbInSeq - 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
133 print "minNbNt=%i" % ( minNbNt )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
134 if minPropNt >= 1.0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
135 print "ERROR: minPropNt=%.2f should be a proportion (below 1.0)" % ( minPropNt )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
136 sys.exit(1)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
137
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
138 lOccPerSite = self.getListOccPerSite()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
139 nbSites = len(lOccPerSite)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
140 if verbose > 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
141 print "nb of sites: %i" % ( nbSites ); sys.stdout.flush()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
142
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
143 seqConsensus = ""
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
144
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
145 # for each site (i.e. each column of the MSA)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
146 nbRmvColumns = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
147 countSites = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
148 for dNt2Occ in lOccPerSite:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
149 countSites += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
150 if verbose > 1:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
151 print "site %s / %i" % ( str(countSites).zfill( len(str(nbSites)) ),
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
152 nbSites )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
153 sys.stdout.flush()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
154 occMaxNt = 0 # occurrences of the predominant nucleotide at this site
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
155 lBestNt = []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
156 nbNt = 0 # total nb of A, T, G and C (no gap)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
157
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
158 # for each distinct symbol at this site (A, T, G, C, N, -,...)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
159 for j in dNt2Occ.keys():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
160 if j != "-":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
161 nbNt += dNt2Occ[j]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
162 if verbose > 1:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
163 print "%s: %i" % ( j, dNt2Occ[j] )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
164 if dNt2Occ[j] > occMaxNt:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
165 occMaxNt = dNt2Occ[j]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
166 lBestNt = [ j ]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
167 elif dNt2Occ[j] == occMaxNt:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
168 lBestNt.append( j )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
169 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)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
170 nbRmvColumns += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
171
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
172 if len( lBestNt ) >= 1:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
173 bestNt = lBestNt[0]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
174
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
175 # if the predominant nucleotide occurs in less than x% of the sequences, put a "N"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
176 if minPropNt > 0.0 and nbNt != 0 and float(occMaxNt)/float(nbNt) < minPropNt:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
177 bestNt = "N"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
178
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
179 if int(nbNt) >= int(minNbNt):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
180 seqConsensus += bestNt
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
181 if verbose > 1:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
182 print "-> %s" % ( bestNt )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
183
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
184 if nbRmvColumns:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
185 if nbRmvColumns == 1:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
186 print "WARNING: 1 site was removed (%.2f%%)" % (nbRmvColumns / float(nbSites) * 100)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
187 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
188 print "WARNING: %i sites were removed (%.2f%%)" % ( nbRmvColumns, nbRmvColumns / float(nbSites) * 100 )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
189 sys.stdout.flush()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
190 if seqConsensus == "":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
191 print "WARNING: no consensus can be built (no sequence left)"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
192 return
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
193
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
194 propN = seqConsensus.count("N") / float(len(seqConsensus))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
195 if propN >= maxPropN:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
196 print "WARNING: no consensus can be built (%i%% of N's >= %i%%)" % ( propN * 100, maxPropN * 100 )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
197 return
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
198 elif propN >= maxPropN * 0.5:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
199 print "WARNING: %i%% of N's" % ( propN * 100 )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
200
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
201 consensus = Bioseq()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
202 consensus.sequence = seqConsensus
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
203 if isHeaderSAtannot:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
204 header = self.db[0].header
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
205 pyramid = header.split("Gr")[1].split("Cl")[0]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
206 pile = header.split("Cl")[1].split(" ")[0]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
207 consensus.header = "consensus=%s length=%i nbAlign=%i pile=%s pyramid=%s" % (self.name, len(seqConsensus), self.getSize(), pile, pyramid)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
208 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
209 consensus.header = "consensus=%s length=%i nbAlign=%i" % ( self.name, len(seqConsensus), self.getSize() )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
210
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
211 if verbose > 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
212
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
213 statEntropy = self.getEntropy( verbose - 1 )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
214 print "entropy: %s" % ( statEntropy.stringQuantiles() )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
215 sys.stdout.flush()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
216
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
217 return consensus
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
218
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
219
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
220 ## Get the entropy of the whole multiple alignment (only for A, T, G and C)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
221 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
222 # @param verbose level of verbosity
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
223 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
224 # @return statistics about the entropy of the MSA
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
225 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
226 def getEntropy( self, verbose=0 ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
227
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
228 stats = Stat()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
229
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
230 # get the occurrences of symbols at each site
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
231 lOccPerSite = self.getListOccPerSite()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
232
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
233 countSite = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
234
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
235 # for each site
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
236 for dSymbol2Occ in lOccPerSite:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
237 countSite += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
238
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
239 # count the number of nucleotides (A, T, G and C, doesn't count gap '-')
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
240 nbNt = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
241 dATGC2Occ = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
242 for base in ["A","T","G","C"]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
243 dATGC2Occ[ base ] = 0.0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
244 for nt in dSymbol2Occ.keys():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
245 if nt != "-":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
246 nbNt += dSymbol2Occ[ nt ]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
247 checkedNt = self.getATGCNFromIUPAC( nt )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
248 if checkedNt in ["A","T","G","C"] and dSymbol2Occ.has_key( checkedNt ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
249 dATGC2Occ[ checkedNt ] += 1 * dSymbol2Occ[ checkedNt ]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
250 else: # for 'N'
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
251 if dSymbol2Occ.has_key( checkedNt ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
252 dATGC2Occ[ "A" ] += 0.25 * dSymbol2Occ[ checkedNt ]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
253 dATGC2Occ[ "T" ] += 0.25 * dSymbol2Occ[ checkedNt ]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
254 dATGC2Occ[ "G" ] += 0.25 * dSymbol2Occ[ checkedNt ]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
255 dATGC2Occ[ "C" ] += 0.25 * dSymbol2Occ[ checkedNt ]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
256 if verbose > 2:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
257 for base in dATGC2Occ.keys():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
258 print "%s: %i" % ( base, dATGC2Occ[ base ] )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
259
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
260 # compute the entropy for the site
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
261 entropySite = 0.0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
262 for nt in dATGC2Occ.keys():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
263 entropySite += self.computeEntropy( dATGC2Occ[ nt ], nbNt )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
264 if verbose > 1:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
265 print "site %i (%i nt): entropy = %.3f" % ( countSite, nbNt, entropySite )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
266 stats.add( entropySite )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
267
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
268 return stats
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
269
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
270
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
271 ## Get A, T, G, C or N from an IUPAC letter
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
272 # IUPAC = ['A','T','G','C','U','R','Y','M','K','W','S','B','D','H','V','N']
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
273 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
274 # @return A, T, G, C or N
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
275 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
276 def getATGCNFromIUPAC( self, nt ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
277 iBs = Bioseq()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
278 return iBs.getATGCNFromIUPAC( nt )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
279
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
280
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
281 ## Compute the entropy based on the occurrences of a certain nucleotide and the total number of nucleotides
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
282 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
283 def computeEntropy( self, nbOcc, nbNt ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
284 if nbOcc == 0.0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
285 return 0.0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
286 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
287 freq = nbOcc / float(nbNt)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
288 return - freq * log(freq) / log(2)
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 ## Save the multiple alignment as a matrix with '0' if gap, '1' otherwise
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
292 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
293 def saveAsBinaryMatrix( self, outFile ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
294 outFileHandler = open( outFile, "w" )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
295 for bs in self.db:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
296 string = "%s" % ( bs.header )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
297 for nt in bs.sequence:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
298 if nt != "-":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
299 string += "\t%i" % ( 1 )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
300 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
301 string += "\t%i" % ( 0 )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
302 outFileHandler.write( "%s\n" % ( string ) )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
303 outFileHandler.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
304
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
305
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
306 ## Return a list of Align instances corresponding to the aligned regions (without gaps)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
307 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
308 # @param query string header of the sequence considered as query
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
309 # @param subject string header of the sequence considered as subject
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
310 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
311 def getAlignList( self, query, subject ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
312 lAligns = []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
313 alignQ = self.fetch( query ).sequence
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
314 alignS = self.fetch( subject ).sequence
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
315 createNewAlign = True
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
316 indexAlign = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
317 indexQ = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
318 indexS = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
319 while indexAlign < len(alignQ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
320 if alignQ[ indexAlign ] != "-" and alignS[ indexAlign ] != "-":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
321 indexQ += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
322 indexS += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
323 if createNewAlign:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
324 iAlign = Align( Range( query, indexQ, indexQ ),
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
325 Range( subject, indexS, indexS ),
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
326 0,
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
327 int( alignQ[ indexAlign ] == alignS[ indexAlign ] ),
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
328 int( alignQ[ indexAlign ] == alignS[ indexAlign ] ) )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
329 lAligns.append( iAlign )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
330 createNewAlign = False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
331 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
332 lAligns[-1].range_query.end += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
333 lAligns[-1].range_subject.end += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
334 lAligns[-1].score += int( alignQ[ indexAlign ] == alignS[ indexAlign ] )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
335 lAligns[-1].identity += int( alignQ[ indexAlign ] == alignS[ indexAlign ] )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
336 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
337 if not createNewAlign:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
338 lAligns[-1].identity = 100 * lAligns[-1].identity / lAligns[-1].getLengthOnQuery()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
339 createNewAlign = True
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
340 if alignQ[ indexAlign ] != "-":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
341 indexQ += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
342 elif alignS[ indexAlign ] != "-":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
343 indexS += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
344 indexAlign += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
345 if not createNewAlign:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
346 lAligns[-1].identity = 100 * lAligns[-1].identity / lAligns[-1].getLengthOnQuery()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
347 return lAligns
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
348
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
349
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
350 def removeGaps(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
351 for iBs in self.db:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
352 iBs.removeSymbol( "-" )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
353
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
354 ## Compute mean per cent identity for MSA.
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
355 # First sequence in MSA is considered as reference sequence.
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
356 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
357 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
358 def computeMeanPcentIdentity(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
359 seqRef = self.db[0]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
360 sumPcentIdentity = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
361
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
362 for seq in self.db[1:]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
363 pcentIdentity = self._computePcentIdentityBetweenSeqRefAndCurrentSeq(seqRef, seq)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
364 sumPcentIdentity = sumPcentIdentity + pcentIdentity
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
365
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
366 nbSeq = len(self.db[1:])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
367 meanPcentIdentity = round (sumPcentIdentity/nbSeq)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
368
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
369 return meanPcentIdentity
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
370
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
371 def _computePcentIdentityBetweenSeqRefAndCurrentSeq(self, seqRef, seq):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
372 indexOnSeqRef = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
373 sumIdentity = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
374 for nuclSeq in seq.sequence:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
375 nuclRef = seqRef.sequence[indexOnSeqRef]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
376
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
377 if nuclRef != "-" and nuclRef == nuclSeq:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
378 sumIdentity = sumIdentity + 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
379 indexOnSeqRef = indexOnSeqRef + 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
380
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
381 return float(sumIdentity) / float(seqRef.getLength()) * 100
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
382
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
383
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
384
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
385
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
386
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
387
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
388
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
389
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
390
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
391
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
392
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
393
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
394
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
395
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
396