Skip to content

Commit 7e4a410

Browse files
authored
Merge pull request #7 from hadirah87/master
FRiP function and test
2 parents 666474f + c144c45 commit 7e4a410

2 files changed

Lines changed: 62 additions & 0 deletions

File tree

looplib/looptools.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,51 @@ def get_n_leafs(idx, children):
156156
else:
157157
return sum([get_n_leafs(child, children)
158158
for child in children[idx]])
159+
160+
161+
162+
def expand_boundary_positions(boundary_positions, offsets=[0]):
163+
"""
164+
Expand boundary_positions by offsets.
165+
166+
Args:
167+
boundary_positions : np.array
168+
List of boundary locations.
169+
offsets : np.array (optional)
170+
List of window sizes. Defaults to [0], the positon of the boundaries.
171+
For symmetric windows around boundaries, use np.arange(-window_size, (window_size) + 1)
172+
173+
Returns:
174+
np.ndarray: Array containing peak positions.
175+
"""
176+
expanded_positions = np.array([])
177+
178+
for offset in offsets:
179+
inds_to_add = [boundary + offset for boundary in boundary_positions]
180+
expanded_positions = np.hstack((expanded_positions, inds_to_add))
181+
182+
return expanded_positions.astype(int)
183+
184+
185+
186+
187+
def FRiP(number_of_sites, lef_positions, boundary_positions):
188+
"""
189+
Calculate the Fraction of Reads in Peaks (FRiP).
190+
191+
FRiP is calculated as the sum of reads falling into peak positions
192+
divided by the total number of LEF positions.
193+
194+
Args:
195+
number_of_sites (int): The total number of sites.
196+
extruders_positions (list): List of LEF positions.
197+
peak_positions (list): List of peak positions.
198+
199+
Returns:
200+
float: The Fraction of Reads in Peaks (FRiP).
201+
"""
202+
203+
hist, bin_edges = np.histogram(lef_positions, np.arange(number_of_sites + 1))
204+
return np.sum(hist[boundary_positions]) / len(lef_positions)
205+
206+

looplib/test.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from looplib.looptools import *
2+
3+
# Tests for FRiP
4+
lattice_length = 5
5+
boundary_list = [1, 4]
6+
7+
lef_A = [1, 1, 4, 1]
8+
assert FRiP(lattice_length, lef_A, boundary_list) == 1
9+
10+
lef_B = [3, 0, 2, 3]
11+
assert FRiP(lattice_length, lef_B, boundary_list) == 0
12+
13+
lef_C = [1, 1, 3, 3]
14+
assert FRiP(lattice_length, lef_C, boundary_list) == 0.5

0 commit comments

Comments
 (0)