-
Notifications
You must be signed in to change notification settings - Fork 0
/
CaMS-Vis.py
72 lines (60 loc) · 2.22 KB
/
CaMS-Vis.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
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
def compliment(str):
return str.replace('A', 't').replace('T', 'a').replace('C', 'g').replace('G', 'c').upper()
def data_input(vcf):
f = pd.read_table(vcf)
f["ALT"] = f["ALT"].str[0]
h = 1
for i in f["ALT"]:
if h == (len(f["ALT"])-1):
break
if f["ALT"][h] != "<":
f["ALT"][h] = f["REF"][h-1] + f["ALT"][h] + f["REF"][h+1]
h += 1
global g
g = f[f["ALT"]!= "<"]
g.drop(g.columns.difference(['REF','ALT']), 1, inplace=True)
def mut_catalog(data):
reference = g["REF"].tolist()
substitution = g["ALT"].tolist()
basic_mutation_type = ['CA','CG','CT','TA','TC','TG']
types = []
codon_type = []
for i in range(len(reference)):
if reference[i] == 'A' or reference[i] == 'G':
reference[i] = compliment(reference[i])
substitution[i] = compliment(substitution[i])
types.append(reference[i]+substitution[i][1])
codon_type.append(substitution[i][0]+'X'+substitution[i][2])
d = {'Reference':reference, 'Substitution':substitution, 'Type':types, 'Codon':codon_type}
df1 = pd.DataFrame(data=d)
N_rows = len(set(codon_type))
d = {'CA':np.zeros(N_rows),'CT':np.zeros(N_rows),'CG':np.zeros(N_rows),'TA':np.zeros(N_rows),'TC':np.zeros(N_rows),'TG':np.zeros(N_rows)}
global df2
df2 = pd.DataFrame(data=d)
df2.index = set(codon_type)
for typ in df2.columns:
for cod in df2.index:
lis=[]
h=0
for sub in df1["Codon"]:
if sub==cod and df1["Type"][h]==typ:
lis.append(sub)
h+=1
df2[typ][cod] = len(lis)
df2.rename(columns={"CA":"C>A", "CT":"C>T", "CG":"C>G", "TA":"T>A", "TC":"T>C", "TG":"T>G"})
def mutsig_plt():
axes = df2.plot.bar(rot=90, subplots=True, legend=None, ylim=(0,1000),
layout=(1,6), figsize=(20,3), sharey=True,
fontsize=10)
plt.tight_layout()
plt.savefig('mut_sig.png')
def main(vcf):
data_input(vcf)
mut_catalog(g)
mutsig_plt()
if __name__ == "__main__":
vcf = input("Enter your bcf in text file format:")
main(vcf)