-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAmbiguityConsensus.py
executable file
·142 lines (117 loc) · 4.56 KB
/
AmbiguityConsensus.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/python
import optparse
from Bio import AlignIO
from Bio.Align import AlignInfo
from collections import defaultdict
################################# Command line options
desc='Generate the consensus of an alignment.'
parser = optparse.OptionParser(description=desc, version='%prog version 0.1 - 31-03-2015 - Author: FCicconardi')
parser.add_option('-i', '--alignment', dest='aln', help='Input alignment in FASTA. Mandatory opt.', action='store', metavar='FILE')
parser.add_option('-l', '--locus', dest='locus', help='Locus. Mandatory opt.', action='store', metavar='<ARG>')
parser.add_option('-t', '--consensus-threshold', dest='thld', help='Set consensus threshold value. The higher the more is sensitive [0-1]. Default <0.6>.', action='store', metavar='<ARG>', type='float', default=0.4)
parser.add_option('-o', '--out', dest='out', help='Name of output fasta file. Mandatory opt.', action='store', metavar='FILE')
(opts, args) = parser.parse_args()
mandatories = ['aln','out']
for m in mandatories:
if not opts.__dict__[m]:
print "\nWARNING! output file is not specified\n"
parser.print_help()
exit(-1)
############################## Reading files and parameters
alignment = AlignIO.read(open(opts.aln), 'fasta')
print "Alignment length %i" % alignment.get_alignment_length()
summary_align = AlignInfo.SummaryInfo(alignment)
consensus = str(summary_align.dumb_consensus(threshold=float(opts.thld), ambiguous='-', require_multiple = 2)).replace('!','-')
consensus=''
for i in range(0,alignment.get_alignment_length()):
pos=''
amb=[]
for record in alignment:
pos+=record.seq[i].replace('!','-')
pA=float(pos.count('A'))/len(pos)
pT=float(pos.count('T'))/len(pos)
pC=float(pos.count('C'))/len(pos)
pG=float(pos.count('G'))/len(pos)
pGap=float(pos.count('-'))/len(pos)
#print pos
#print i, 'A', pA
#print i, 'T', pT
#print i, 'C', pC
#print i, 'G', pG
#print i, '-', pGap
if pA > opts.thld:
#print i, 'A', pA
amb.append('A')
if pT > opts.thld:
#print i, 'T', pT
amb.append('T')
if pC > opts.thld:
#print i, 'C', pC
amb.append('C')
if pG > opts.thld:
#print i, 'G', pG
amb.append('G')
if pGap > opts.thld:
#print i, '-', pGap
amb.append('-')
if len(amb) == 0 or len(amb) == 5:
consensus+='N'
elif len(amb) == 1:
consensus+=amb[0]
elif len(amb) == 2:
if 'A' in amb and 'G' in amb:
consensus+='R'
elif 'C' in amb and 'T' in amb:
consensus+='Y'
elif 'G' in amb and 'C' in amb:
consensus+='S'
elif 'A' in amb and 'T' in amb:
consensus+='W'
elif 'T' in amb and 'G' in amb:
consensus+='K'
elif 'A' in amb and 'C' in amb:
consensus+='M'
elif 'A' in amb and '-' in amb:
consensus+='A'
elif 'T' in amb and '-' in amb:
consensus+='T'
elif 'C' in amb and '-' in amb:
consensus+='C'
elif 'G' in amb and '-' in amb:
consensus+='G'
elif len(amb) == 3:
if 'C' in amb and 'G' in amb and 'T' in amb:
consensus+='B'
elif 'A' in amb and 'G' in amb and 'T' in amb:
consensus+='D'
elif 'A' in amb and 'C' in amb and 'T' in amb:
consensus+='H'
elif 'A' in amb and 'C' in amb and 'G' in amb:
consensus+='V'
elif 'A' in amb and 'T' in amb and '-' in amb:
consensus+='W'
elif 'A' in amb and 'C' in amb and '-' in amb:
consensus+='M'
elif 'A' in amb and 'G' in amb and '-' in amb:
consensus+='R'
elif 'T' in amb and 'C' in amb and '-' in amb:
consensus+='Y'
elif 'T' in amb and 'G' in amb and '-' in amb:
consensus+='K'
elif 'C' in amb and 'G' in amb and '-' in amb:
consensus+='S'
elif len(amb) == 4:
if '-' not in amb:
consensus+='N'
elif 'A' in amb and 'T' in amb and 'C' in amb and '-' in amb:
consensus+='H'
elif 'A' in amb and 'C' in amb and 'G' in amb and '-' in amb:
consensus+='V'
elif 'A' in amb and 'T' in amb and 'G' in amb and '-' in amb:
consensus+='D'
elif 'T' in amb and 'C' in amb and 'G' in amb and '-' in amb:
consensus+='B'
out_fasta=open(opts.out, 'w')
print >> out_fasta, '>'+opts.locus+'_consensus threshold '+str(opts.thld)
print >> out_fasta, consensus
#print opts.thld