-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ecoli_Cake.py
159 lines (139 loc) · 5.05 KB
/
Ecoli_Cake.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
from itertools import permutations
import argparse
import sys
import os
from pyteomics.fasta import read
from scipy.stats import chisquare
import math
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.ticker import FuncFormatter
import numpy as np
from scipy import stats
import matplotlib.pyplot as mpl
from statsmodels.stats.multitest import multipletests
plt.style.use('seaborn')
plt.rcParams["font.family"] = "Times New Roman"
parser = argparse.ArgumentParser(description='Use to find kmers')
parser.add_argument('-modifa',
type=str,
help='modifying_fasta',
required=True
)
parser.add_argument('-reffa',
type=str,
help='reference_fasta',
required=True
)
parser.add_argument('-save_way',
type=str,
help='path_of_save',
required=True
)
parser.add_argument('-image_save',
type=str,
help='image_save_path',
required=True
)
args = parser.parse_args()
ref_path = args.reffa
mpl.rcParams['figure.figsize'] = [12, 8]
print('Analizing of motive abundance of E.coli ...\n')
genome = read(args.reffa)
syntheny_bloks = read(args.modifa)
alphablet = ['A', 'T', 'G' ,'C']
new_genome = ''
for line in genome:
new_genome += line.sequence
def create_lib(r):
return set([''.join(i) for i in permutations(alphablet * r, r=r)])
all_ends = ''
for line in syntheny_bloks:
all_ends += line.sequence
print('Waiting...\n')
lable_k = []
all_sb_friq = []
end_sb_friq = []
sort_sb_friq = []
sort_end_sb_friq = []
all_p_values = []
sort_p_values = []
sort_lable_k = []
cake = ''
dict_of_motiv = {'GATC': ['GATC'], 'CANCATC': [],'AACN4CTTT': [], 'RTACN4GTG': [], 'GAGACC': ['GAGACC']}
for kmer in create_lib(1):
dict_of_motiv['CANCATC'].append('CA{}CATC'.format(kmer))
for kmer in create_lib(4):
dict_of_motiv['AACN4CTTT'].append('AAC{}CTTT'.format(kmer))
dict_of_motiv['RTACN4GTG'].append('ATAC{}GTG'.format(kmer))
dict_of_motiv['RTACN4GTG'].append('GTAC{}GTG'.format(kmer))
for key in dict_of_motiv:
lable_k.append(key)
mid_count = 0
end_count = 0
print('Counting {} ...'.format(key))
for motiv in dict_of_motiv[key]:
mid_count += new_genome.count(motiv)
end_count += all_ends.count(motiv)
len_mot = len(motiv)
mid = mid_count * 100/(len(new_genome) - len_mot + 1)
end = end_count * 100/(len(all_ends) - len_mot + 1)
all_sb_friq.append(mid_count * 100/(len(new_genome) - len_mot + 1))
end_sb_friq.append(end_count * 100/(len(all_ends) - len_mot + 1))
cake += 'Frequency in ends {} \t{}\n'.format(motiv, end_sb_friq)
cake += 'Frequency in genome {} \t{}\n'.format(motiv, all_sb_friq)
cake += 'Ratio \t{}\n'.format(end/mid)
pvalue = stats.chi2_contingency([
[len(all_ends), len(new_genome)],
[end_count, mid_count]
])[1]
print('P-value = {}\n'.format(pvalue))
cake += 'P-value for {} \t{}\n'.format(motiv, str(math.log1p(pvalue)))
all_p_values.append(pvalue)
#correction for multiple comparisons
all_p_values = multipletests(all_p_values, method='fdr_bh')[1]
all_p_values = list(np.array(all_p_values))
sort_p_values = list(np.sort(np.array(all_p_values)))
for sornum in sort_p_values:
for num in range(len(all_p_values)):
if sornum == all_p_values[num]:
if all_sb_friq[num] not in sort_sb_friq:
sort_sb_friq.append(all_sb_friq[num])
sort_end_sb_friq.append(end_sb_friq[num])
sort_lable_k.append(lable_k[num])
savefile = open(args.save_way, 'w')
savefile.write(cake)
savefile.close()
print('Text file is complete!\n༼ つ ◕_◕ ༽つ\n')
print('Now please wait a picture\n(ノ◕ヮ◕)ノ*:・゚✧\n')
x = np.arange(len(sort_lable_k))
width = 0.35
fig, ax = plt.subplots()
rects1 = ax.bar(x - width/2, sort_sb_friq, width, label='K-mer in the middle of SB, %')
rects2 = ax.bar(x + width/2, sort_end_sb_friq, width, label='K-mer in the ends of SB, %')
ax.set_ylabel('Frequency, %')
ax.set_xlabel('K-mers')
ax.set_title('Escherichia coli')
ax.set_xticks(x)
ax.set_xticklabels(sort_lable_k, rotation='vertical')
ax.legend(loc='center', bbox_to_anchor=(1.15, 0.5))
def autolabel(rects):
for rect in range(len(rects)):
if rects1[rect].get_height() > rects2[rect].get_height():
height = rects1[rect].get_height()
ax.annotate('log(P-value)=\n{}'.format(round(np.log(sort_p_values[rect]), 3)),
xy=(rects1[rect].get_x() + rects1[rect].get_width() / 2, height),
xytext=(0, 1),
textcoords="offset points",
ha='center', va='bottom')
else:
height = rects2[rect].get_height()
ax.annotate('log(P-value)=\n{}'.format(round(np.log(sort_p_values[rect]), 3)),
xy=(rects1[rect].get_x() + rects1[rect].get_width() / 2, height),
xytext=(0, 1),
textcoords="offset points",
ha='center', va='bottom')
autolabel(rects1)
fig.tight_layout()
plt.savefig(args.image_save, format='pdf', dpi=300)
print('Analyze of Escherichia coli was done!\n')