The program FAN-C also has a utitlity for random subsampling of a matrix: fanc downsample / fanc hic --downsample
For example, via the CLI:
#!/bin/bash
fanc downsample --help
# 2023-10-10 15:21:17,645 INFO FAN-C version: 0.9.27
# *** fanc downsample is deprecated. Please use fanc hic --downsample instead! ***
# usage: fanc downsample [-h] [-tmp] hic n output
#
# Downsample contacts from a Hic object.
#
# positional arguments:
# hic Hic object to be downsampled.
# n Sample size or reference Hi-C object. If sample size is < 1,will be interpreted as a fraction of valid pairs.
# output Downsampled Hic output.
#
# options:
# -h, --help show this help message and exit
# -tmp, --work-in-tmp Work in temporary directory
Or, for example, via the Python API:
#!/usr/bin/env python3
import sys
import os
import fanc
import numpy as np
def calculate_contact_sum(hic_file):
hic = fanc.load(hic_file)
contact_sum = 0
for value in hic.edge_data(hic._default_score_field, lazy=True):
if np.isfinite(value):
contact_sum += value
return contact_sum, hic
file_1 = "path/to/file/sample_1.hic"
file_2 = "path/to/file/sample_2.hic"
# Works with .cool files as well
sum_1, hic_1 = calculate_contact_sum(file_1)
sum_2, hic_2 = calculate_contact_sum(file_2)
sums = {'file_1': sum_1, 'file_2': sum_2}
min_key = min(sums, key=sums.get)
min_value = sums[min_key]
hic_1.downsample(
min_value,
file_name = "path/to/outfile_1.hic" # or .cool
)
hic_2.downsample(
min_value,
file_name = "path/to/outfile_2.hic" # or .cool
)
• 0 views
•
link