util.py
4.3 KB
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
import numpy as np
import matplotlib
matplotlib.use('Agg')
from pylab import rcParams
rcParams['figure.figsize'] = 19, 20
from matplotlib import pyplot as plt
from matplotlib.pyplot import cm
import os
import pandas as pd
from plotnine import *
from functools import partial
from concurrent.futures import ThreadPoolExecutor
'''
Files checking
'''
def check_dir(path):
if not os.path.exists(path):
os.makedirs(path)
def check_dir_file(file_name):
path = os.path.dirname(file_name)
if path != "":
check_dir(path)
'''
Import data
'''
def import_ncRNA(path):
file_order = ["CB.txt","ORF.txt","KMER3.txt","KMER4.txt","KMER5.txt"]
df_raw = []
with ThreadPoolExecutor(max_workers=5) as tp:
for x in file_order:
df_raw.append(tp.submit(pd.read_csv,path+x,sep=",",header=None))
df = df_raw[0].result()
for i in range(1,len(file_order)):
tmp = df_raw[i].result()
df = df.merge(tmp,on=0)
df = df.fillna(0)
data = df.iloc[:,1:].values
data_names = df.iloc[:,0].values
return (data,data_names)
'''
Plot function
'''
def plot_repartition(data,m,n,name,labels_names):
repartition = data[1]
dico = []
N = np.sum(repartition)
for i in range(m):
value_classes = np.sum(repartition,axis=0)
for j in range(n):
idx = i*n+j
value_sum = np.sum(repartition[idx,:])
if value_sum > 0:
for k in range(len(labels_names)):
tmp = {"id": idx, "x":i, "y":j, "label": labels_names[k], "percentage in cluster": repartition[idx,k]/value_sum, "percentage in class": repartition[idx,k]/value_classes[k],"total percentage": repartition[idx,k]/N}
dico.append(tmp)
else:
dico.append({"id": idx, "x":i, "y":j, "label": "empty", "percentage in cluster": 1.0,"percentage in class":0.0,"total percentage":0.0})
df = pd.DataFrame(dico)
p = ggplot(df,aes(fill="label",y="percentage in cluster",x="1",alpha = "total percentage"))
p += geom_bar(stat="identity")
p += facet_grid("x ~ y ")
p += scale_x_discrete(name = "", limits = "1",labels=[""])
p += xlab("")
p += ylab("")
check_dir_file(name)
p.save(name+".png",width=6, height=7)
def combination(vector_values):
nb_values = [len(x) for x in vector_values]
nb_var = len(vector_values)
idx_values = [0] * nb_var
while(idx_values[0] < nb_values[0]):
yield [vector_values[i][idx_values[i]] for i in range(nb_var)]
if idx_values[-1] < nb_values[-1]:
idx_values[-1] += 1
for i in range(nb_var-2,-1,-1):
if idx_values[i] < nb_values[i] and idx_values[i+1] == nb_values[i+1]:
idx_values[i] += 1
else:
break
for i in range(1,nb_var):
idx_values[i] = 0 if idx_values[i] == nb_values[i] else idx_values[i]
NUC_ORDER = ["A","C","T","G"]
def plot_weights(units,m,n,name):
features = [
["Position "+x for x in NUC_ORDER]+["Composition "+x for x in NUC_ORDER] + ["GC composition"],
["ORF length", "ORF coverage", "All ORF coverage mean", "All ORF coverage std", "Frame bias", "ORF frequency", "Position start codon mean", "Position start codon std", "Position end codon mean", "Position end codon std"]
]
features_names = ["Codon_Bias", "ORF", "KMER3", "KMER4", "KMER5"]
features_pos = [np.arange(len(features[0]))]
for i in range(1,len(features)):
idx = features_pos[i-1][-1]+1
features_pos.append(np.arange(idx,idx+len(features[i])))
for l in range(len(features)):
dico = []
for i in range(m):
for j in range(n):
idx = i*n + j
for k in range(len(features[l])):
dico.append({"x":i,"y":j,"x2": k, "y2": units[idx,features_pos[l][k]],"Name":features[l][k]})
df = pd.DataFrame(dico)
p = ggplot(df,aes(x="x2",y="y2"))
p += geom_line()
p += geom_point(aes(color="Name"),size=2.0)
p += facet_grid("x ~ y ")
p += xlab("")
p += ylab("")
p += guides(color=guide_legend(override_aes={"size":4}))
p += theme(
legend_text=element_text(size=10),
legend_title=element_text(size=12),
axis_text_x = element_blank(),
legend_position="top"
)
p.save(name+features_names[l]+".png",width=15, height=10)
def plot_density(y, proba,name):
dico_raw = []
for i in range(y.shape[0]):
dico_raw.append({"label":str(y[i]),"Proba":proba[i,0],"Type":"Coding"})
dico_raw.append({"label":str(y[i]),"Proba":proba[i,1],"Type":"Noncoding"})
df = pd.DataFrame(dico_raw)
p = ggplot(df,aes(color="label",fill="label",x="Proba",group="label"))
p += geom_histogram()
p += facet_wrap("~Type")
check_dir_file(name)
p.save(name+".png",width=6, height=7)