Repository navigation
Expand file tree
/
Copy pathvisualizations.py
More file actions
149 lines (131 loc) · 5.09 KB
/
Copy pathvisualizations.py
File metadata and controls
149 lines (131 loc) · 5.09 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import itertools,math
import numpy as np
from scipy.stats import binom_test
try:
from pybedtools import BedTool
except:
print("Pybedtools not imported")
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
def plot_styler():
ax = plt.subplot(111)
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.yaxis.set_ticks_position('left')
ax.xaxis.set_ticks_position('bottom')
return
def barplot_gen(strand1,strand2,name1,name2,output):
"""
This should be an option for the user if he wants to generate vizualizations too.
"""
plot_styler()
plt.bar(range(1,3),[strand1,strand2],align="center")
plt.xticks(range(1,3),[name1,name2])
plt.ylabel("Occurrences")
plt.xlabel("Strand Orientation")
plt.tight_layout()
plt.savefig(output)
plt.close()
return
def barplot_pair_lists_gen(x_tickL,List1,List2,name1,name2,x_label,title_legend,output):
"""
This should be an option for the user if he wants to generate vizualizations too.
"""
plot_styler()
plt.bar(range(1,len(List1)*3+1,3),List1,label=name1,align="center")
plt.bar(range(2,len(List2)*3+1,3),List2,label=name2,align="center")
plt.xticks(range(1,len(List1)*3+1,3),x_tickL,rotation=90,fontsize=9)
plt.ylabel("Occurrences")
plt.xlabel(x_label)
plt.legend(frameon=False,title=title_legend)
plt.tight_layout()
plt.savefig(output)
plt.close()
return
def barplot_single_gen(List1,List1_names,y_label,x_label,output):
"""
This should be an option for the user if he wants to generate vizualizations too.
"""
plot_styler()
plt.bar(range(1,len(List1)*1+1,1),List1,align="center")
plt.xticks(range(1,len(List1)*3+1),[List1_names[k] for k in range(0,len(List1_names)*1,3)],rotation=90)
plt.ylabel(y_label)
plt.xlabel(x_label)
plt.tight_layout()
plt.savefig(output)
plt.close()
return
def heatmap_gen(DataLL,DataLL_control,BinsL,output):
try:
import seaborn as sns
except:
print("seaborn not imported")
try:
import pandas as pd
except:
print("pandas not imported")
for k in range(len(DataLL)):
if DataLL[k]==[]:
DataLL[k]={};
for k in range(len(DataLL_control)):
if DataLL_control[k]==[]:
DataLL_control[k]={};
all_cons = [max(k.keys()) for k in DataLL if k!={}]
if all_cons==[]:
return
else:
max_cons = max(all_cons)
RatioLL=[]
for i in range(len(DataLL)):
RatioL=[];
for k in range(1,max_cons+1):
if k in DataLL_control[i].keys():
if float(DataLL_control[i][k])!=0 and float(DataLL[i][k])!=0:
RatioL.append(math.log10(DataLL[i][k]/float(DataLL_control[i][k])))
else:
RatioL.append(np.nan)
else:
RatioL.append(np.nan)
RatioLL.append(RatioL)
df = pd.DataFrame(np.array(RatioLL),index=BinsL)
mask = df.isnull()
sns.heatmap(df,cbar_kws={'label': 'log(Enrichment)'}, mask=mask)
plt.xlabel("Consecutive occurrences")
plt.ylabel("Distance bins")
plt.tight_layout()
plt.savefig(output)
plt.close()
return
def distribution_gen(occsL,occsL_control,output):
"""
This function calculates the distance of consecutive patterns and plots it for both the real data and the controls.
"""
from collections import Counter
plot_styler()
Distances_consecutiveL = [occsL[k+1]-occsL[k] for k in range(len(occsL)-1)]
Distances_consecutive_controlL = [occsL_control[k+1]-occsL_control[k] for k in range(len(occsL_control)-1)]
Distances_consecutiveD = Counter(Distances_consecutiveL).most_common()
Distances_consecutive_controlD = Counter(Distances_consecutive_controlL).most_common()
plt.plot([k[0] for k in Distances_consecutiveD],[m[1] for m in Distances_consecutiveD],"o",markersize=2,label="Observed")
plt.plot([k[0] for k in Distances_consecutive_controlD],[m[1] for m in Distances_consecutive_controlD],"o",markersize=2,label="Expected")
plt.xlabel("Distance of consecutive")
plt.ylabel("Occurrences")
plt.legend(frameon=False)
plt.savefig(output)
plt.close()
return
def distnace_distribution_gen(same_strandL_distance,opposite_strandL_distance,name1,name2,min_dist,max_dist,output):
try:
import seaborn as sns
except:
print("seaborn not imported")
plot_styler()
plt.hist(same_strandL_distance,50,histtype='step',label=name1)
plt.hist(opposite_strandL_distance,50,histtype='step',label=name2)
plt.xlabel("Distance")
plt.ylabel("Occurrences")
plt.xlim(min_dist,max_dist)
plt.legend(frameon=False,title="Orientation")
plt.savefig(output)
plt.close()