Skip to content

Commit b62293f

Browse files
authored
Merge pull request #4 from mtortora/coolify
Implemented the contact_maps.coolify method
2 parents 0621278 + 02b3827 commit b62293f

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

polykit/analysis/contact_maps.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,14 @@
4343
"""
4444

4545
import ctypes
46+
import cooler
4647
import multiprocessing as mp
4748
import random
4849
import warnings
4950
from contextlib import closing
5051

5152
import numpy as np
53+
import pandas as pd
5254

5355
from . import polymer_analyses
5456

@@ -585,3 +587,49 @@ def monomerResolutionContactMapSubchains(
585587
uniqueContacts=True,
586588
nproc=n,
587589
)
590+
591+
592+
def coolify(contact_matrix,
593+
cool_uri,
594+
chrom_dict={},
595+
binsize=2500,
596+
chunksize=10000000):
597+
"""
598+
Save a simulated contact map to .cool, and return corresponding Cooler
599+
600+
Parameters
601+
----------
602+
contact_matrix : NxN int array
603+
Simulated contact map (in dense numpy.ndarray format)
604+
cool_uri : str
605+
Name of .cool file to be created (excluding extension)
606+
chrom_dict : dict
607+
Dictionary of chromosome lengths, in the form {'chr1':size1, ...}
608+
If unspecified, assumes the map spans a single chromosome
609+
binsize : int
610+
Map resolution in bp (i.e., genomic size of each simulated monomer)
611+
chunksize : int
612+
Number of pixels handled by each worker process
613+
614+
Returns
615+
-------
616+
Associated cooler.Cooler object
617+
"""
618+
619+
nbins = contact_matrix.shape[0]
620+
621+
chrom_dict = chrom_dict if chrom_dict else {'chr_sim': binsize*nbins}
622+
chrom_sizes = pd.Series(chrom_dict, name='length', dtype='int64')
623+
624+
assert binsize*nbins == sum(chrom_sizes), "Chromosome sizes do not match map dimensions"
625+
626+
bins = cooler.binnify(chrom_sizes, binsize)
627+
bins['weight'] = np.ones(nbins) * np.sqrt(2/nbins)
628+
629+
pixels = cooler.create.ArrayLoader(bins, contact_matrix, chunksize=chunksize)
630+
631+
cool_uri = f"{cool_uri}.{binsize}.cool"
632+
cooler.create_cooler(cool_uri, bins, pixels, ordered=True)
633+
634+
return cooler.Cooler(cool_uri)
635+

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ scipy>=0.16
33
pandas>=0.19
44
matplotlib>=2.0
55
rowan
6+
cooler
67
joblib
78
pyside2
89
ipython

0 commit comments

Comments
 (0)