-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsensus.py
54 lines (49 loc) · 1.18 KB
/
consensus.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
#fasta
seqs={}
with open('rosalind_cons.txt', 'r') as f1:
for line in f1:
if ">" in line:
name = line.strip()
seqs[name]=[]
else:
seqs[name].append(line.strip())
for name,seq in seqs.items():
seqs[name] = ''.join(seqs[name])
j = len(seqs[name])
#matrix
A = []
C = []
G = []
T = []
for i in range(0, j):
A.append(0)
C.append(0)
G.append(0)
T.append(0)
for i in range(0, j):
for seq in seqs.values():
if seq[i] == 'A':
A[i] += 1
elif seq[i] == 'C':
C[i] += 1
elif seq[i] == 'G':
G[i] += 1
else:
T[i] += 1
#consensus seq
consensus=[]
for i in range(0, j):
m = max(A[i], C[i], G[i], T[i])
if m == A[i]:
consensus.append('A')
elif m == C[i]:
consensus.append('C')
elif m == G[i]:
consensus.append('G')
else:
consensus.append('T')
print(''.join(consensus))
print('A:', ' '.join(str(n) for n in A))
print('C:', ' '.join(str(n) for n in C))
print('G:', ' '.join(str(n) for n in G))
print('T:', ' '.join(str(n) for n in T))