-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrosalind_cons.py
51 lines (43 loc) · 1.33 KB
/
rosalind_cons.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
# -*- coding: utf-8 -*-
"""
Created on Mon Jan 16 14:12:11 2023
@author: ivanp
"""
import pandas as pd
filename = "rosalind_cons.txt"
def read_fasta(filename):
dic = dict()
with open(filename,"r") as _f:
line = _f.readline().rstrip()
key = line
value = ""
while line:
line = _f.readline().rstrip()
if line.startswith(">"):
dic[key]=value
key = line
value = ""
else:
value = value + line
else:
dic[key]=value
return dic
def count_nucs(row,cols):
cross = "".join(row.values)
return (cross.count(cols[0]),cross.count(cols[1]),cross.count(cols[2]),cross.count(cols[3]))
dic_fasta = read_fasta(filename)
#split every element into a list
dic_fasta = {k:list(v) for k,v in dic_fasta.items()}
#create a daframe and consensus matrix
df = pd.DataFrame(dic_fasta)
_columns = ["A","T","C","G"]
con_mat = df.apply(count_nucs,axis=1,result_type="expand",cols=_columns)
con_mat.columns = _columns
#find the consensus - the largest element
cons = "".join(con_mat.idxmax(axis=1))
#print out the matrix
con_mat = con_mat.T
print(cons)
for _,row in con_mat.iterrows():
values = " ".join(row.astype(str))
print(f"{row.name}: {values}")