-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnullmodelsTDA.py
170 lines (160 loc) · 8.64 KB
/
nullmodelsTDA.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
160
161
162
163
164
165
166
167
168
169
170
import csv
import glob
import sys
import numpy as np
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import seaborn as sns
#need to read in persistence diagrams
def readPersisFile(filename):
persisdiag = []
with open(filename,'rt') as f:
freader = csv.reader(f,delimiter='\t')
for line in freader:
if int(line[0]) != 1: continue
persisdiag.append((float(line[1]),float(line[2])))
return persisdiag
#plot birth times
def plotBirthTimes(origpersis,randperms,edgeperms,distperms,filename):
origtimes,randtimes,edgetimes,disttimes = [],[],[],[]
for chrnum in range(13,23):
origtimes.extend([x[0] for celltype in origpersis[chrnum] for x in celltype[1]])
if chrnum in randperms:
randtimes.extend([x[0] for perm in randperms[chrnum] for x in perm[1]])
else:
print 'no random permutation data for chr',chrnum
if chrnum in edgeperms:
edgetimes.extend([x[0] for perm in edgeperms[chrnum] for x in perm[1]])
else:
print 'no edge permutation data for chr',chrnum
if chrnum in distperms:
disttimes.extend([x[0] for perm in distperms[chrnum] for x in perm[1]])
else:
print 'no distance permutation data for chr',chrnum
print len(origtimes),len(randtimes),len(edgetimes),len(disttimes)
fig,ax = plt.subplots()
sns.distplot(randtimes, label="Random permutation", ax=ax, kde=True,kde_kws={'linewidth': 2}, norm_hist=True,color='#ffa600',hist_kws={"edgecolor": "none","alpha":0.1})#"histtype": "step","alpha":1,"linewidth":2})
sns.distplot(edgetimes, label="Edge permutation", ax=ax, kde=True,kde_kws={'linewidth': 2},norm_hist=True,color='#7a5195',hist_kws={"edgecolor": "none","alpha":0.1})#"histtype": "step","alpha":1,"linewidth":2})#
sns.distplot(disttimes, label="Linear dependence", ax=ax, kde=True,kde_kws={'linewidth': 2},norm_hist=True,color='#ef5675',hist_kws={"edgecolor": "none","alpha":0.1})#"histtype": "step","alpha":1,"linewidth":2})#
sns.distplot(origtimes, label="Hi-C", ax=ax, kde=True,kde_kws={'linewidth': 2}, norm_hist=True,color='#003f5c',hist_kws={"edgecolor": "none","alpha":0.1})#"histtype": "step","alpha":1,"linewidth":2})#
ax.set_xlim([0,1])
ax.set_yticklabels([])
plt.xlabel('Loop birth time')
ax.legend(frameon=False,loc='upper left')
plt.savefig(filename,bbox_inches='tight')
print 'saved birth times histogram to',filename
#plot lifespans
def plotLifeSpans(origpersis,randperms,edgeperms,distperms,filename):
origtimes,randtimes,edgetimes,disttimes = [],[],[],[]
for chrnum in range(13,23):
origtimes.extend([x[1]-x[0] for celltype in origpersis[chrnum] for x in celltype[1]])
if chrnum in randperms:
randtimes.extend([x[1]-x[0] for perm in randperms[chrnum] for x in perm[1]])
else:
print 'no random permutation data for chr',chrnum
if chrnum in edgeperms:
edgetimes.extend([x[1]-x[0] for perm in edgeperms[chrnum] for x in perm[1]])
else:
print 'no edge permutation data for chr',chrnum
if chrnum in distperms:
disttimes.extend([x[1]-x[0] for perm in distperms[chrnum] for x in perm[1]])
else:
print 'no distance permutation data for chr',chrnum
print len(origtimes),len(randtimes),len(edgetimes),len(disttimes)
fig,ax = plt.subplots()
sns.distplot(randtimes, label="Random permutation", ax=ax, kde=True,kde_kws={'linewidth': 2},norm_hist=True,color = '#ffa600',hist_kws={"edgecolor": "none","alpha":0.1})#"histtype": "step","alpha":1,"linewidth":2})#
sns.distplot(edgetimes, label="Edge permutation", ax=ax, kde=True,kde_kws={'linewidth': 2},norm_hist=True,color='#7a5195',hist_kws={"edgecolor": "none","alpha":0.1})#{"histtype": "step","alpha":1,"linewidth":2})#
sns.distplot(disttimes, label="Linear dependence", ax=ax, kde=True,kde_kws={'linewidth': 2},norm_hist=True,color='#ef5675',hist_kws={"edgecolor": "none","alpha":0.1})#{"histtype": "step","alpha":1,"linewidth":2})#
sns.distplot(origtimes, label="Hi-C", ax=ax, kde=True,kde_kws={'linewidth': 2},norm_hist=True,color='#003f5c',hist_kws={"edgecolor": "none","alpha":0.1})#{"histtype": "step","alpha":1,"linewidth":2})#
ax.set_xlim([0,0.6])
ax.set_yticklabels([])
plt.xlabel('Loop life span')
ax.legend(frameon=False,loc='upper right')
plt.savefig(filename,bbox_inches='tight')
print 'saved life spans histogram to',filename
#plot numbers of loops (ratio w/ original)
def plotLoopNumbers(origpersis,randperms,edgeperms,distperms,filename):
randtimes,edgetimes,disttimes = [],[],[]
for chrnum in range(13,23):
origloopnums = {}
for celltypes in origpersis[chrnum]:
origloopnums[celltypes[0]] = len(celltypes[1])
#origtimes.extend([x[1]-x[0] for celltype in origpersis[chrnum] for x in celltype])
if chrnum in randperms:
for perm in randperms[chrnum]:
celltype = perm[0]
randtimes.extend([float(len(perm[1]))/origloopnums[celltype]])
else:
print 'no random permutation data for chr',chrnum
if chrnum in edgeperms:
for perm in edgeperms[chrnum]:
celltype = perm[0]
edgetimes.extend([float(len(perm[1]))/origloopnums[celltype]])
#edgetimes.extend([x[1]-x[0] for perm in edgeperms[chrnum] for x in perm])
else:
print 'no edge permutation data for chr',chrnum
if chrnum in distperms:
for perm in distperms[chrnum]:
celltype = perm[0]
disttimes.extend([float(len(perm[1]))/origloopnums[celltype]])
#disttimes.extend([x[1]-x[0] for perm in distperms[chrnum] for x in perm])
else:
print 'no distance permutation data for chr',chrnum
print len(randtimes),len(edgetimes),len(disttimes)
fig,ax = plt.subplots()
sns.distplot(randtimes, label="Random permutation", ax=ax, kde=False,norm_hist=True,color='#ffa600',hist_kws={"edgecolor": "none","alpha":0.75})
sns.distplot(edgetimes, label="Edge permutation", ax=ax, kde=False,norm_hist=True,color='#7a5195',hist_kws={"edgecolor": "none","alpha":0.75})
sns.distplot(disttimes, label="Linear dependence", ax=ax, kde=False,norm_hist=True,color='#ef5675',hist_kws={"edgecolor": "none","alpha":0.75})
#sns.distplot(origtimes, label="Hi-C", ax=ax, kde=False,norm_hist=True)
#ax.set_xlim([0,1])
ax.set_yticklabels([])
plt.xticks(np.arange(0,np.ceil(max(edgetimes)),step=1))
plt.xlabel('Ratio of loop count in model to corresponding Hi-C')
ax.legend(frameon=False,loc='upper right')
plt.savefig(filename,bbox_inches='tight')
print 'saved loop counts histogram to',filename
fileseed = sys.argv[1]
outputloc = sys.argv[2]
origpersis = {}
distperms = {}
randperms = {}
edgeperms = {}
for chrnum in range(13,23):
origfilenames = glob.glob(fileseed+str(chrnum)+'_persisdiagram.txt')
for origfile in origfilenames:
celltype = origfile.split('_')[1]
if chrnum in origpersis:
origpersis[chrnum].append((celltype,readPersisFile(origfile)))
else:
origpersis[chrnum] = [(celltype,readPersisFile(origfile))]
dpfilenames = glob.glob(fileseed+str(chrnum)+'_distperm*_persisdiagram.txt')
#print dpfilenames
if len(dpfilenames) > 0:
for dpfile in dpfilenames:
celltype = dpfile.split('_')[1]
if chrnum in distperms:
distperms[chrnum].append((celltype,readPersisFile(dpfile)))
else:
distperms[chrnum] = [(celltype,readPersisFile(dpfile))]
rpfilenames = glob.glob(fileseed+str(chrnum)+'_randperm*_persisdiagram.txt')
#print rpfilenames
if len(rpfilenames) > 0:
for rpfile in rpfilenames:
celltype = rpfile.split('_')[1]
if chrnum in randperms:
randperms[chrnum].append((celltype,readPersisFile(rpfile)))
else:
randperms[chrnum] = [(celltype,readPersisFile(rpfile))]
epfilenames = glob.glob(fileseed+str(chrnum)+'_edgeperm*_persisdiagram.txt')
#print epfilenames
if len(epfilenames) > 0:
for epfile in epfilenames:
celltype = epfile.split('_')[1]
if chrnum in edgeperms:
edgeperms[chrnum].append((celltype,readPersisFile(epfile)))
else:
edgeperms[chrnum] = [(celltype,readPersisFile(epfile))]
plotBirthTimes(origpersis,randperms,edgeperms,distperms,outputloc+'_birthtimes.pdf')
plotLifeSpans(origpersis,randperms,edgeperms,distperms, outputloc+'_lifespans.pdf')
plotLoopNumbers(origpersis,randperms,edgeperms,distperms,outputloc+'_loopcounts.pdf')