-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathshowContactmap.py.deprecated
More file actions
374 lines (307 loc) · 11.4 KB
/
showContactmap.py.deprecated
File metadata and controls
374 lines (307 loc) · 11.4 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
import os
import pickle
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from mirnylib.genome import Genome
from mirnylib.h5dict import h5dict
from mirnylib.numutils import coarsegrain, completeIC, zoomArray
from mirnylib.plotting import nicePlot
from mirnylib.systemutils import setExceptionHook
from openmmlib import contactmaps, polymerutils
from openmmlib.contactmapManager import averageContacts
# matplotlib.use("Agg")
setExceptionHook()
import mirnylib.plotting
filename = "/net/levsha/share/nezar/ctcf_sites/GM12878.ctcf_narrowPeak.loj.encodeMotif.rad21.txt"
SEPARATION = 400
LIFETIME = 200
class simulator(object):
def __init__(self, i, forw, rev, blocks, steps):
import pyximport
pyximport.install()
import numpy as np
from smcTranslocatorDirectional import smcTranslocator
N = len(forw)
birthArray = np.zeros(N, dtype=np.double) + 0.1
deathArray = np.zeros(N, dtype=np.double) + 1.0 / LIFETIME
stallArrayLeft = forw
stallArrayRight = rev
stallDeathArray = np.zeros(N, dtype=np.double) + 1 / LIFETIME
pauseArray = np.zeros(N, dtype=np.double)
smcNum = N // SEPARATION
myDeathArray = deathArray
SMCTran = smcTranslocator(
birthArray,
myDeathArray,
stallArrayLeft,
stallArrayRight,
pauseArray,
stallDeathArray,
smcNum,
)
self.SMCTran = SMCTran
self.steps = steps
self.blocks = blocks
self.i = 0
def next(self):
self.i += 1
if self.i == self.blocks:
raise StopIteration
self.SMCTran.steps(self.steps)
conts = self.SMCTran.getSMCs()
if self.i % 1000 == 500:
print(self.i, conts[0][0])
return np.array(conts) // 20
def getForwBacv(mu=3):
df = pd.read_csv(filename, sep="\t")
df = df.loc[(~pd.isnull(df["summitDist"]))]
mychr = 14
df = df.loc[df["chrom"] == "chr{0}".format(mychr)]
start = df["start"].values
end = df["end"].values
strand = df["summitDist"].values > 0
strength = df["fc"]
mid_1k = (start + end) // 1200
M = mid_1k.max() + 1
forw = np.bincount(mid_1k[strand], weights=(strength[strand] / 20), minlength=M)
rev = np.bincount(mid_1k[~strand], weights=(strength[~strand] / 20), minlength=M)
low = 60000
high = 75000
lowMon = low * 1000 // 600
highMon = high * 1000 // 600
forw = forw[lowMon:highMon]
rev = rev[lowMon:highMon]
def logistic(x, mu=3):
x[x == 0] = -99999999
return 1 / (1 + np.exp(-(x - mu)))
forw = logistic(forw, mu)
rev = logistic(rev, mu)
return forw, rev
# uncommend this to just display a simulated heatmap.
# hm = averageContacts(simulator, range(30), 1500, classInitArgs=[forw, rev, 5000, 150], bucketNum = 20, nproc=30)
# exit()
# print(hm.shape)
class contactCalculator:
def __init__(self, filenames, cutoff, coarsegrainBy, method):
self.filenames = filenames
self.cutoff = cutoff
self.coarsegrain = coarsegrainBy
self.method = method
def next(self):
if len(self.filenames) == 0:
raise StopIteration
data = polymerutils.load(self.filenames.pop())
contacts = self.method(data, cutoff=self.cutoff) // self.coarsegrain
return contacts
def getCmap(prefix="", radius=6):
"""
This is a function to calculate a simulated Hi-C contact map from one or several folders with conformations, defined by "prefix".
"""
n = 20 # number of processes to use = number of cores
coarsegrainBy = 5 # how many monomers per pixel in a heatmap
print(os.getcwd())
folders = [i for i in os.listdir(".") if i.startswith(prefix)]
foldes = [i for i in folders if os.path.exists(i)]
print(folders)
files = sum([polymerutils.scanBlocks(i)["files"] for i in folders], [])
filegroups = [files[i::n] for i in range(n)]
data = polymerutils.load(files[0])
N = len(data)
method = contactmaps.findMethod(data, radius)
cmapN = int(np.ceil(N / coarsegrainBy))
cmap = averageContacts(
contactCalculator,
filegroups,
cmapN,
classInitArgs=[radius, coarsegrainBy, method],
nproc=n,
bucketNum=60,
useFmap=True,
)
pickle.dump(cmap, open("cmaps/cmap{0}_r={1}.pkl".format(prefix, radius), "wb"))
def getAllCmaps():
"""This functions iterates over different contact radii and over different contactmap names,
and calculates contact maps. Right now set only for one contact map.
"""
for radius in [8]:
for prefix in [
# "lessCTCF","lessLifetime","moreLifetime","moreSeparation","steps=500",
# "flagship_try","flagshipLessCTCF","flagshipMoreCtcf","flagship_cellType"
# "flagshipMod_", "flagshipModLessCtcf", "flagshipModMoreCtcf"
# "flagshipBoundaryStallLifetime100Mu3","flagshipBoundaryStallLifetime200Mu3",
# "flagshipBoundaryStallLifetime300Mu3",
# "flagshipLifetime100Mu3","flagshipLifetime200Mu3",
"flagshipLifetime300Mu3"
# ,"flagshipLifetime300Mu2","flagshipLifetime300Mu4",
]:
print(prefix, radius)
getCmap(prefix, radius)
exit()
# getAllCmaps()
def pivotHeatmap(heatmap, diags=20):
N = len(heatmap)
newdata = np.zeros((diags, 2 * N), dtype=float)
for i in range(diags):
diag = np.diagonal(heatmap, i)
pad = N - len(diag)
newdiag = np.zeros(2 * len(diag), dtype=float)
newdiag[::2] = diag
newdiag[1::2] = diag
if pad == 0:
newdata[i] = newdiag
else:
newdata[i][pad:-pad] = newdiag
return newdata
def showCmap():
"""Shows Hi-C data together with the simulated data. Hi-C data created by hiclib is needed for that,
but you can replace the line mydict=h5dict()... and the following line with your own data loading code."""
low = 60000
high = 75000
lowMon = low * 1000 // 600
highMon = high * 1000 // 600
low20 = low // 10
high20 = high // 10
# here Hi-C data is loaded for display purposes only..... replace it with your own code if your data is in a different format
mydict = h5dict(
"/home/magus/HiC2011/Erez2014/hg19/GM12878_inSitu-all-combined-10k_HighRes.byChr",
"r",
)
hicdata = mydict.get_dataset("13 13")[low20:high20, low20:high20]
hicdata = completeIC(hicdata)
curshape = hicdata.shape
newshape = (1000 * (high - low)) // (600 * 5)
print(hicdata.shape, newshape)
hicdata = zoomArray(hicdata, (newshape, newshape))
hicdata = np.clip(hicdata, 0, np.percentile(hicdata, 99.99))
hicdata /= np.mean(np.sum(hicdata, axis=1))
# hicdata = hm / np.mean(np.sum(hm, axis=1))
for fname in os.listdir("cmaps"):
cmap = pickle.load(open(os.path.join("cmaps", fname), "rb"))
# arr = coarsegrain(cmap, 2)
arr = cmap
if arr.shape[0] != hicdata.shape[0]:
continue
print(arr.shape)
arr = arr / np.mean(np.sum(arr, axis=1))
ran = np.arange(len(arr))
mask = ran[:, None] > ran[None, :]
arr[mask] = hicdata[mask]
logarr = np.log(arr + 0.0001)
# noinspection PyTypeChecker
plt.imshow(
logarr,
vmax=np.percentile(logarr, 99.99),
vmin=np.percentile(logarr, 10),
extent=[low, high, high, low],
interpolation="none",
)
plt.savefig(os.path.join("heatmaps", fname + ".png"))
plt.savefig(os.path.join("heatmaps", fname + ".pdf"))
plt.show()
plt.clf()
# getCmap()
# showCmap()
# plt.show()
def showCmapNew():
"""Saves a bunch of heatmaps at high resolutions."""
plt.figure(figsize=(8, 8))
low = 60000
high = 75000
lowMon = low * 1000 // 600
highMon = high * 1000 // 600
low20 = low // 10
high20 = high // 10
mydict = h5dict(
"/home/magus/HiC2011/Erez2014/hg19/GM12878_inSitu-all-combined-10k_HighRes.byChr",
"r",
)
hicdata = mydict.get_dataset("13 13")[low20:high20, low20:high20]
hicdata = completeIC(hicdata)
curshape = hicdata.shape
resolutionMon = 5
newshape = (1000 * (high - low)) // (600 * resolutionMon)
print(hicdata.shape, newshape)
hicdata = zoomArray(hicdata, (newshape, newshape))
hicdata = np.clip(hicdata, 0, np.percentile(hicdata, 99.99))
hicdata /= np.mean(np.sum(hicdata, axis=1))
# hicdata = hm / np.mean(np.sum(hm, axis=1))
# for fname in os.listdir("cmaps"):
for fname in ["cmapflagshipLifetime300Mu3_r=8.pkl"]:
if ("r=8" not in fname) or ("Lifetime" not in fname):
print("not going", fname)
continue
try:
mu = float(fname.split("_r=")[0].split("Mu")[1])
except:
continue
forw, rev = getForwBacv(mu)
cmap = pickle.load(open(os.path.join("cmaps", fname), "rb"))
# arr = coarsegrain(cmap, 2)
arr = cmap
if arr.shape[0] != hicdata.shape[0]:
continue
arr = arr / np.mean(np.sum(arr, axis=1))
hicdata *= 1.5
diags = 1000
print(arr.shape)
ax = plt.subplot(211)
turned = pivotHeatmap(arr, diags)[::-1] * 3
turned2 = pivotHeatmap(hicdata, diags)
turned = np.concatenate([turned, turned2], axis=0)
myextent = [
low,
high,
-(high - low) * diags / len(arr),
(high - low) * diags / len(arr),
]
plt.imshow(
np.log(turned + 0.0001),
aspect=0.5,
cmap="fall",
vmax=-4,
vmin=-8,
extent=myextent,
interpolation="none",
)
# plt.colorbar()
# plt.ylim([-(high - low) * diags/ len(arr) , (high - low) * diags/ len(arr) ])
# nicePlot(show=False)
plt.subplot(413, sharex=ax)
xaxis = np.arange(len(forw) // 20) * 12 + 60000
forwcg = coarsegrain(forw, 20)
revcg = coarsegrain(rev, 20)
plt.vlines(xaxis[forwcg > 0], 0, forwcg[forwcg > 0], color="blue")
plt.vlines(xaxis[revcg > 0], 0, revcg[revcg > 0], color="green")
# plt.scatter(xaxis[forwcg>0], forwcg[forwcg>0], label = "forward CTCF")
# plt.scatter(xaxis[revcg > 0],revcg[revcg>0], label = "reverse CTCF")
plt.xlim([60000, 75000])
plt.title(fname)
plt.legend()
plt.show()
continue
# nicePlot(show=False)
# plt.subplot(414, sharex = ax)
# plt.plot(xaxis, data)
# plt.show()
# arr = arr / np.mean(np.sum(arr, axis=1))
# ran = np.arange(len(arr))
# mask = ran[:,None] > ran[None,:]
# arr[mask] = hicdata[mask]
# logarr = np.log(arr + 0.0001)
# noinspection PyTypeChecker
# plt.imshow(logarr, vmax = np.percentile(logarr, 99.9), extent = [low, high, high, low], interpolation = "none")
for st in range(60000, 75000, 1000):
for size in [2000, 3000, 5000]:
end = st + size
if end > 75000:
continue
plt.xlim([st, end])
plt.savefig(os.path.join("heatmaps", "{0}_st={1}_end={2}_r=2.png".format(fname, st, end)))
plt.savefig(os.path.join("heatmaps", "{0}_st={1}_end={2}_r=2.pdf".format(fname, st, end)))
plt.clf()
plt.show()
# getCmap()
showCmapNew()
# plt.show()