-
Notifications
You must be signed in to change notification settings - Fork 3
/
consensus-seq.py
121 lines (86 loc) · 2.48 KB
/
consensus-seq.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
#!/usr/bin/env python3
from collections import defaultdict
import re
import textwrap
import argparse
def remove(stringOrlist, list):
emptyList = []
for i in stringOrlist:
if i not in list:
emptyList.append(i)
else:
pass
outString = "".join(emptyList)
return outString
def mostCommon(Dict):
for i in sorted(Dict.keys()):
highest = 0
for j in Dict:
aa = j
count = Dict[j]
if count > highest:
highest = count
consensus = aa
if count == highest:
if aa != "-":
consensus = aa
return consensus
def fasta(fasta_file):
count = 0
seq = ''
header = ''
Dict = defaultdict(lambda: defaultdict(lambda: 'EMPTY'))
for i in fasta_file:
i = i.rstrip()
if re.match(r'^>', i):
count += 1
if count % 1000000 == 0:
print(count)
if len(seq) > 0:
Dict[header] = seq
header = i[1:]
# header = header.split(" ")[0]
seq = ''
else:
header = i[1:]
# header = header.split(" ")[0]
seq = ''
else:
seq += i
Dict[header] = seq
# print(count)
return Dict
parser = argparse.ArgumentParser(
prog="consensus-seq.py",
formatter_class=argparse.RawDescriptionHelpFormatter,
description=textwrap.dedent('''
Script for generating a representative consensus sequence from an alignment
contact: [email protected]
'''))
parser.add_argument('-i', help='alignment file')
args = parser.parse_args()
aln = open(args.i)
aln = fasta(aln)
firstheader = list(aln.keys())[0]
seqLength = len(aln[firstheader])
clusterLength = len(aln.keys())
checkDict = defaultdict(list)
for i in aln.keys():
checkDict[len(aln[i])].append(i)
if len(checkDict.keys()) > 1:
print("Alignment file not formatted correctly. All sequences must be the same length.")
raise SystemExit
Dict = defaultdict(lambda: defaultdict(list))
for j in aln.keys():
seq = aln[j]
for k in range(0, seqLength):
Dict[k][seq[k]].append(1)
Dict2 = defaultdict(lambda: defaultdict(lambda: 'EMPTY'))
for j in Dict.keys():
for k in Dict[j]:
Dict2[j][k] = sum(Dict[j][k])
consensus = ""
for j in range(0, seqLength):
residue = mostCommon(Dict2[j])
consensus += residue
print(consensus)