Skip to content

Commit 053ee1c

Browse files
committed
deprecate and clean up polymerutils
1 parent c4730e1 commit 053ee1c

File tree

3 files changed

+38
-45
lines changed

3 files changed

+38
-45
lines changed

polychrom/legacy/polymerutils.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from polychrom.polymerutils import * # noqa: F403

polychrom/polymer_analyses.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
------------------------------
1212
1313
The main function calculating contacts is: :py:func:`polychrom.polymer_analyses.calculate_contacts`
14-
Right now it is a simple wrapper around scipy.cKDTree.
14+
Right now it is a simple wrapper around scipy.KDTree.
1515
1616
Another function :py:func:`polychrom.polymer_analyses.smart_contacts` was added recently to help build contact maps
1717
with a large contact radius. It randomly sub-samples the monomers; by default selecting N/cutoff monomers. It then
@@ -37,7 +37,7 @@
3737

3838
import numpy as np
3939
import pandas as pd
40-
from scipy.spatial import cKDTree
40+
from scipy.spatial import KDTree
4141
from scipy.ndimage import gaussian_filter1d
4242

4343
try:
@@ -66,7 +66,7 @@ def calculate_contacts(data, cutoff=1.7):
6666
if np.isnan(data).any():
6767
raise RuntimeError("Data contains NANs")
6868

69-
tree = cKDTree(data)
69+
tree = KDTree(data)
7070
pairs = tree.query_pairs(cutoff, output_type="ndarray")
7171
return pairs
7272

@@ -573,7 +573,7 @@ def calculate_cistrans(data, chains, chain_id=0, cutoff=5, pbc_box=False, box_si
573573
chain_end = chains[chain_id][1]
574574

575575
# all contact pairs available in the scaled data
576-
tree = cKDTree(data_scaled, boxsize=box_size)
576+
tree = KDTree(data_scaled, boxsize=box_size)
577577
pairs = tree.query_pairs(cutoff, output_type="ndarray")
578578

579579
# total number of contacts of the marked chain:
@@ -582,7 +582,7 @@ def calculate_cistrans(data, chains, chain_id=0, cutoff=5, pbc_box=False, box_si
582582
all_signal = len(pairs[pairs < chain_end]) - len(pairs[pairs < chain_start])
583583

584584
# contact pairs of the marked chain with itself
585-
tree = cKDTree(data[chain_start:chain_end], boxsize=None)
585+
tree = KDTree(data[chain_start:chain_end], boxsize=None)
586586
pairs = tree.query_pairs(cutoff, output_type="ndarray")
587587

588588
# doubled number of contacts of the marked chain with itself (i.e. cis signal)
@@ -593,3 +593,12 @@ def calculate_cistrans(data, chains, chain_id=0, cutoff=5, pbc_box=False, box_si
593593
trans_signal = all_signal - cis_signal
594594

595595
return cis_signal, trans_signal
596+
597+
598+
def rotation_matrix(rotate):
599+
"""Calculates rotation matrix based on three rotation angles"""
600+
tx, ty, tz = rotate
601+
Rx = np.array([[1, 0, 0], [0, np.cos(tx), -np.sin(tx)], [0, np.sin(tx), np.cos(tx)]])
602+
Ry = np.array([[np.cos(ty), 0, -np.sin(ty)], [0, 1, 0], [np.sin(ty), 0, np.cos(ty)]])
603+
Rz = np.array([[np.cos(tz), -np.sin(tz), 0], [np.sin(tz), np.cos(tz), 0], [0, 0, 1]])
604+
return np.dot(Rx, np.dot(Ry, Rz))

polychrom/polymerutils.py

Lines changed: 23 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,11 @@
2424
xyz = data["pos"]
2525
"""
2626

27-
2827
from __future__ import absolute_import, division, print_function, unicode_literals
2928

3029
import glob
31-
import io
3230
import os
31+
import warnings
3332

3433
import numpy as np
3534

@@ -39,15 +38,9 @@
3938

4039

4140
def load(filename):
42-
"""Universal load function for any type of data file It always returns just XYZ
43-
positions - use fetch_block or hdf5_format.load_URI for loading the whole metadata
44-
45-
Accepted file types
46-
-------------------
47-
48-
New-style URIs (HDF5 based storage)
49-
50-
Joblib and text files were deprecated.
41+
"""
42+
A function to load a single conformation from a URI. Deprecated.
43+
Use load_URI from hdf5_format instead.
5144
5245
Parameters
5346
----------
@@ -56,23 +49,18 @@ def load(filename):
5649
filename to load or a URI
5750
5851
"""
52+
warnings.warn("polymerutils.load is deprecated. Use hdf5_format.load_URI instead.", DeprecationWarning)
53+
5954
if "::" in filename:
6055
return hdf5_format.load_URI(filename)["pos"]
61-
62-
raise ValueError("Only URIs are supported in this version of polychrom")
63-
6456

57+
raise ValueError("Only URIs are supported in this version of polychrom")
6558

6659

6760
def fetch_block(folder, ind, full_output=False):
6861
"""
69-
A more generic function to fetch block number "ind" from a trajectory in a folder
70-
71-
72-
This function is useful both if you want to load both "old style" trajectories (block1.dat),
73-
and "new style" trajectories ("blocks_1-50.h5")
74-
75-
It will be used in files "show"
62+
A function to fetch a single block from a folder with a new-style trajectory.
63+
Old-style trajectores are deprecated.
7664
7765
Parameters
7866
----------
@@ -92,12 +80,14 @@ def fetch_block(folder, ind, full_output=False):
9280
9381
if full_output==True, then dict with data and metadata; XYZ is under key "pos"
9482
"""
83+
warnings.warn(
84+
"fetch_block is deprecated. Use hdf5_format.list_uris followed by hdf5_format.load_URI instead.",
85+
DeprecationWarning,
86+
)
87+
9588
blocksh5 = glob.glob(os.path.join(folder, "blocks*.h5"))
96-
blocksdat = glob.glob(os.path.join(folder, "block*.dat"))
9789
ind = int(ind)
98-
if (len(blocksh5) > 0) and (len(blocksdat) > 0):
99-
raise ValueError("both .h5 and .dat files found in folder - exiting")
100-
if (len(blocksh5) == 0) and (len(blocksdat) == 0):
90+
if len(blocksh5) == 0:
10191
raise ValueError("no blocks found")
10292

10393
if len(blocksh5) > 0:
@@ -113,21 +103,17 @@ def fetch_block(folder, ind, full_output=False):
113103
block = load_URI(blocksh5[pos] + f"::{ind}")
114104
if not full_output:
115105
return block["pos"]
106+
return block
116107

117-
if len(blocksdat) > 0:
118-
return load(os.path.join(folder, f"block{ind}.dat"))
119108
raise ValueError(f"Cannot find the block {ind} in the folder {folder}")
120109

121110

122111
def save(data, filename, mode="txt", pdbGroups=None):
123112
"""
124-
Basically unchanged polymerutils.save function from openmm-polymer
125-
126-
127-
128-
It is also very useful for saving files to PDB format to make them compatible
129-
with nglview, pymol_show and others
113+
A legacy function, currently only kept for compatibility with PDB saving that is rarely used.
130114
"""
115+
warnings.warn("polymerutils.save is deprecated. Will be moved to legacy", DeprecationWarning)
116+
131117
data = np.asarray(data, dtype=np.float32)
132118

133119
if mode == "pdb":
@@ -175,13 +161,10 @@ def add(st, n):
175161
filename.write("C {0} {1} {2}".format(*i))
176162

177163
else:
178-
raise ValueError("Unknown mode : %s, use or pdb" % mode)
164+
raise ValueError(f"Unknown mode {mode}. Only 'pdb' and 'pyxyz' are supported.")
179165

180166

181167
def rotation_matrix(rotate):
182-
"""Calculates rotation matrix based on three rotation angles"""
183-
tx, ty, tz = rotate
184-
Rx = np.array([[1, 0, 0], [0, np.cos(tx), -np.sin(tx)], [0, np.sin(tx), np.cos(tx)]])
185-
Ry = np.array([[np.cos(ty), 0, -np.sin(ty)], [0, 1, 0], [np.sin(ty), 0, np.cos(ty)]])
186-
Rz = np.array([[np.cos(tz), -np.sin(tz), 0], [np.sin(tz), np.cos(tz), 0], [0, 0, 1]])
187-
return np.dot(Rx, np.dot(Ry, Rz))
168+
warnings.warn("rotation_matrix will be moved to polymer_analyses", DeprecationWarning)
169+
from polychrom.polymer_analyses import rotation_matrix as rm
170+
return rm(rotate)

0 commit comments

Comments
 (0)