/bio-hi-c-analysis-matrix-operations
Balance, normalize, and transform Hi-C contact matrices using cooler and cooltools. Apply iterative correction (ICE), compute expected values, and generate observed/expected matrices. Use when normalizing or transforming Hi-C matrices.
$ npx -y skills add FreedomIntelligence/OpenClaw-Medical-Skills --skill bio-hi-c-analysis-matrix-operations --agent claude-codeHow it fires
How this skill gets triggered: by you, by Claude, or both.
- Fires itselfAuto-invocation. Claude auto-loads it when your prompt matches the work.Auto-invocation is when the right skill fires by itself at the right moment, driven by a FLOW.md router and a hook, instead of you invoking it by name. It is the difference between a skill being installed and a skill actually getting used.Read the full definition →
- You can call itInvoke it directly when you want it.
- Slash command
/bio-hi-c-analysis-matrix-operations
Context preview
The summary Claude sees to decide when to auto-load this skill.
Balance, normalize, and transform Hi-C contact matrices using cooler and cooltools. Apply iterative correction (ICE), compute expected values, and generate observed/expected matrices. Use when normalizing or transforming Hi-C matrices.
SKILL.md
bio-hi-c-analysis-matrix-operations.SKILL.mdname: bio-hi-c-analysis-matrix-operations
description: Balance, normalize, and transform Hi-C contact matrices using cooler and cooltools. Apply iterative correction (ICE), compute expected values, and generate observed/expected matrices. Use when normalizing or transforming Hi-C matrices.
tool_type: python
primary_tool: cooltools
Version Compatibility
Reference examples tested with: cooler 0.9+, cooltools 0.6+, numpy 1.26+, pandas 2.2+, scipy 1.12+
Before using code patterns, verify installed versions match. If versions differ:
- Python: `pip show <package>` then `help(module.function)` to check signatures
- CLI: `<tool> --version` then `<tool> --help` to confirm flags
If code throws ImportError, AttributeError, or TypeError, introspect the installed package and adapt the example to match the actual API rather than retrying.
Hi-C Matrix Operations
**"Normalize my Hi-C contact matrix"** → Apply iterative correction (ICE/KR balancing), compute distance-decay expected values, and generate observed/expected ratio matrices.
- Python: `cooler.balance_cooler(clr)`, `cooltools.expected_cis(clr)`
Balance, normalize, and transform contact matrices.
Required Imports
import cooler
import cooltools
import numpy as np
import pandas as pd
Matrix Balancing (ICE)
# Balance a cooler file (iterative correction)
cooler.balance_cooler('matrix.cool', store=True, cis_only=True)
# The balanced weights are stored in the 'weight' column
clr = cooler.Cooler('matrix.cool')
weights = clr.bins()['weight'][:]
print(f'Balanced weights range: {weights.min():.4f} - {weights.max():.4f}')Balance with CLI
# Balance using cooler CLI
cooler balance matrix.cool --cis-only --force
# Check balance status
cooler info matrix.cool | grep "weight"
Access Balanced vs Raw Matrix
clr = cooler.Cooler('matrix.cool')
# Balanced (normalized) matrix
balanced = clr.matrix(balance=True).fetch('chr1')
# Raw (count) matrix
raw = clr.matrix(balance=False).fetch('chr1')
print(f'Raw sum: {raw.sum():.0f}')
print(f'Balanced sum: {np.nansum(balanced):.4f}')Compute Expected Values
import cooltools
clr = cooler.Cooler('matrix.cool')
# Compute expected (average by distance)
expected = cooltools.expected_cis(clr, ignore_diags=2)
print(expected.head())
# Columns: region1, region2, dist, n_valid, count.sum, balanced.sum, balanced.avgObserved/Expected Matrix
**Goal:** Remove the distance-dependent decay from a contact matrix so that enriched interactions (loops, compartments) stand out above the background.
**Approach:** Compute the average contact frequency at each genomic distance (expected), then divide each observed pixel by its distance-matched expected value to produce an O/E ratio matrix.
import cooltools
clr = cooler.Cooler('matrix.cool')
# Compute expected
expected = cooltools.expected_cis(clr, ignore_diags=2)
# Get O/E matrix for a region
def get_oe_matrix(clr, region, expected_df):
matrix = clr.matrix(balance=True).fetch(region)
n = matrix.shape[0]
# Get expected values for this chromosome
chrom = region.split(':')[0]
exp_chr = expected_df[expected_df['region1'] == chrom]
exp_values = exp_chr.set_index('dist')['balanced.avg']
# Create expected matrix
expected_matrix = np.zeros_like(matrix)
for i in range(n):
for j in range(n):
dist = abs(i - j)
if dist in exp_values.index:
expected_matrix[i, j] = exp_values[dist]
# Compute O/E
oe = matrix / expected_matrix
oe[expected_matrix == 0] = np.nan
return oe
oe_matrix = get_oe_matrix(clr, 'chr1', expected)Using cooltools for O/E
import cooltools
clr = cooler.Cooler('matrix.cool')
# Compute expected
expected = cooltools.expected_cis(clr, ignore_diags=2)
# Get O/E normalized matrix
# cooltools provides this through the snipping module
from cooltools.lib import snip
# For a specific region pair
region1 = ('chr1', 50000000, 60000000)
region2 = ('chr1', 50000000, 60000000)
# Snippet
snippet = snip.snip_pileup(
clr.matrix(balance=True),
region1,
region2,
exp_func=None, # Add expected function for O/E
)Log Transform
# Log2 transform of O/E matrix
log_oe = np.log2(oe_matrix)
log_oe[np.isinf(log_oe)] = np.nan
print(f'Log2(O/E) range: {np.nanmin(log_oe):.2f} to {np.nanmax(log_oe):.2f}')Distance Decay Normalization
def distance_normalize(matrix, decay_func=None):
'''Normalize by expected distance decay'''
n = matrix.shape[0]
normalized = np.zeros_like(matrix)
for diag in range(n):
diag_values = np.diag(matrix, diag)
expected = np.nanmean(diag_values) if decay_func is None else decay_func(diag)
if expected > 0:
for i in range(n - diag):
normalized[i, i + diag] = matrix[i, i + diag] / expected
normalized[i + diag, i] = matrix[i + diag, i] / expected
return normalizedAggregate Multiple Replicates
# Sum matrices from multiple replicates
files = ['rep1.cool', 'rep2.cool', 'rep3.cool']
matrices = []
for f in files:
clr = cooler.Cooler(f)
m = clr.matrix(balance=False).fetch('chr1')
matrices.append(m)
# Sum raw matrices
summed = np.sum(matrices, axis=0)
# Then balance the summed resultSmooth Matrix
from scipy.ndimage import uniform_filter
# Apply smoothing
smoothed = uniform_filter(matrix, size=3, mode='constant')
# Gaussian smoothing
from scipy.ndimage import gaussian_filter
smoothed_gauss = gaussian_filter(matrix, sigma=1)
Downsample/Coarsen Matrix
def coarsen_matrix(matrix, factor):
'''Coarsen matrix by summing bins'''
n = matrix.shape[0]
new_n = n // factor
coarse = np.zeros((new_n, new_n))
for i in range(new_n):
for j in range(new_n):
coarse[i, j] = np.nansum(matrix[
i*factoRead more
name: bio-hi-c-analysis-matrix-operations description: Balance, normalize, and transform Hi-C contact matrices using cooler and cooltools. Apply iterative correction (ICE), compute expected values, and generate observed/expected matrices. Use when normalizing or transforming Hi-C matrices. tool_type: python primary_tool: cooltools
Version Compatibility
Reference examples tested with: cooler 0.9+, cooltools 0.6+, numpy 1.26+, pandas 2.2+, scipy 1.12+
Before using code patterns, verify installed versions match. If versions differ:
- Python: `pip show <package>` then `help(module.function)` to check signatures
- CLI: `<tool> --version` then `<tool> --help` to confirm flags
If code throws ImportError, AttributeError, or TypeError, introspect the installed package and adapt the example to match the actual API rather than retrying.
Hi-C Matrix Operations
**"Normalize my Hi-C contact matrix"** → Apply iterative correction (ICE/KR balancing), compute distance-decay expected values, and generate observed/expected ratio matrices.
- Python: `cooler.balance_cooler(clr)`, `cooltools.expected_cis(clr)`
Balance, normalize, and transform contact matrices.
Required Imports
import cooler import cooltools import numpy as np import pandas as pd
Matrix Balancing (ICE)
# Balance a cooler file (iterative correction)
cooler.balance_cooler('matrix.cool', store=True, cis_only=True)
# The balanced weights are stored in the 'weight' column
clr = cooler.Cooler('matrix.cool')
weights = clr.bins()['weight'][:]
print(f'Balanced weights range: {weights.min():.4f} - {weights.max():.4f}')Balance with CLI
# Balance using cooler CLI cooler balance matrix.cool --cis-only --force # Check balance status cooler info matrix.cool | grep "weight"
Access Balanced vs Raw Matrix
clr = cooler.Cooler('matrix.cool')
# Balanced (normalized) matrix
balanced = clr.matrix(balance=True).fetch('chr1')
# Raw (count) matrix
raw = clr.matrix(balance=False).fetch('chr1')
print(f'Raw sum: {raw.sum():.0f}')
print(f'Balanced sum: {np.nansum(balanced):.4f}')Compute Expected Values
import cooltools
clr = cooler.Cooler('matrix.cool')
# Compute expected (average by distance)
expected = cooltools.expected_cis(clr, ignore_diags=2)
print(expected.head())
# Columns: region1, region2, dist, n_valid, count.sum, balanced.sum, balanced.avgObserved/Expected Matrix
**Goal:** Remove the distance-dependent decay from a contact matrix so that enriched interactions (loops, compartments) stand out above the background.
**Approach:** Compute the average contact frequency at each genomic distance (expected), then divide each observed pixel by its distance-matched expected value to produce an O/E ratio matrix.
import cooltools
clr = cooler.Cooler('matrix.cool')
# Compute expected
expected = cooltools.expected_cis(clr, ignore_diags=2)
# Get O/E matrix for a region
def get_oe_matrix(clr, region, expected_df):
matrix = clr.matrix(balance=True).fetch(region)
n = matrix.shape[0]
# Get expected values for this chromosome
chrom = region.split(':')[0]
exp_chr = expected_df[expected_df['region1'] == chrom]
exp_values = exp_chr.set_index('dist')['balanced.avg']
# Create expected matrix
expected_matrix = np.zeros_like(matrix)
for i in range(n):
for j in range(n):
dist = abs(i - j)
if dist in exp_values.index:
expected_matrix[i, j] = exp_values[dist]
# Compute O/E
oe = matrix / expected_matrix
oe[expected_matrix == 0] = np.nan
return oe
oe_matrix = get_oe_matrix(clr, 'chr1', expected)Using cooltools for O/E
import cooltools
clr = cooler.Cooler('matrix.cool')
# Compute expected
expected = cooltools.expected_cis(clr, ignore_diags=2)
# Get O/E normalized matrix
# cooltools provides this through the snipping module
from cooltools.lib import snip
# For a specific region pair
region1 = ('chr1', 50000000, 60000000)
region2 = ('chr1', 50000000, 60000000)
# Snippet
snippet = snip.snip_pileup(
clr.matrix(balance=True),
region1,
region2,
exp_func=None, # Add expected function for O/E
)Log Transform
# Log2 transform of O/E matrix
log_oe = np.log2(oe_matrix)
log_oe[np.isinf(log_oe)] = np.nan
print(f'Log2(O/E) range: {np.nanmin(log_oe):.2f} to {np.nanmax(log_oe):.2f}')Distance Decay Normalization
def distance_normalize(matrix, decay_func=None):
'''Normalize by expected distance decay'''
n = matrix.shape[0]
normalized = np.zeros_like(matrix)
for diag in range(n):
diag_values = np.diag(matrix, diag)
expected = np.nanmean(diag_values) if decay_func is None else decay_func(diag)
if expected > 0:
for i in range(n - diag):
normalized[i, i + diag] = matrix[i, i + diag] / expected
normalized[i + diag, i] = matrix[i + diag, i] / expected
return normalizedAggregate Multiple Replicates
# Sum matrices from multiple replicates
files = ['rep1.cool', 'rep2.cool', 'rep3.cool']
matrices = []
for f in files:
clr = cooler.Cooler(f)
m = clr.matrix(balance=False).fetch('chr1')
matrices.append(m)
# Sum raw matrices
summed = np.sum(matrices, axis=0)
# Then balance the summed resultSmooth Matrix
from scipy.ndimage import uniform_filter # Apply smoothing smoothed = uniform_filter(matrix, size=3, mode='constant') # Gaussian smoothing from scipy.ndimage import gaussian_filter smoothed_gauss = gaussian_filter(matrix, sigma=1)
Downsample/Coarsen Matrix
def coarsen_matrix(matrix, factor):
'''Coarsen matrix by summing bins'''
n = matrix.shape[0]
new_n = n // factor
coarse = np.zeros((new_n, new_n))
for i in range(new_n):
for j in range(new_n):
coarse[i, j] = np.nansum(matrix[
i*factoThe largest open-source medical AI skill library for OpenClaw.
Other skills on openclaw-medical-skills.
adaptyv
Cloud laboratory platform for automated protein testing and validation. Use when designing proteins and needing experimental validation including binding…
adhd-daily-planner
Time-blind friendly planning, executive function support, and daily structure for ADHD brains. Specializes in realistic time estimation, dopamine-aware task…
aeon
This skill should be used for time series machine learning tasks including classification, regression, clustering, forecasting, anomaly detection,…
agent-browser
Browse the web for any task — research topics, read articles, interact with web apps, fill forms, take screenshots, extract data, and test web pages. Use…

