hi-c-analysis/compartment-analysis/SKILL.md
Detects A/B chromatin compartments from balanced Hi-C contact matrices via eigenvector decomposition of the distance-normalized, Pearson-correlated cis matrix with cooltools (eigs_cis), then orients (phases) the compartment eigenvector against a GC or gene-density track so the active (A) sign is not arbitrary. Covers the eigenvector-is-a-choice problem (per-arm view_df to remove the centromere gradient; picking the eigenvector by max correlation with activity, not by eigenvalue), GC phasing with bioframe.frac_gc, resolution choice (100kb-1Mb), saddle plots and saddle_strength for compartmentalization strength, the cohesin-loss-strengthens-compartments result, subcompartments (SNIPER/Calder/dcHiC), and cross-condition compartment switching. Use when calling A/B compartments, computing E1/eigenvectors, phasing the eigenvector, building saddle plots, choosing a compartment resolution, quantifying compartment strength, or comparing compartmentalization across conditions.
npx skillsauth add GPTomics/bioSkills bio-hi-c-analysis-compartment-analysisInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
Reference examples tested with: cooler 0.10+, cooltools 0.7+, bioframe 0.7+
Before using code patterns, verify installed versions match. If versions differ:
pip show <package> then help(module.function) to check signaturescooltools had a major API shift around 0.5 -> 0.7+ (functions standardized on view_df/viewframe arguments; eigs_cis, expected_cis, saddle signatures changed). The cooler MUST be balanced before any compartment analysis: clr.matrix(balance=True) requires a stored weight column. A .mcool is multi-resolution -- pass a single-resolution URI (file.mcool::/resolutions/100000), not the bare .mcool. The phasing_track MUST share the cooler's exact binning or phasing silently no-ops. If code throws ImportError, AttributeError, or TypeError, introspect the installed package and adapt the example to match the actual API rather than retrying.
"Which regions of my genome are in the active vs inactive compartment?" -> Distance-normalize the cis matrix, take an eigenvector of its Pearson correlation matrix, then orient it by GC/gene density so positive = A (active) -- but verify the kept eigenvector is the compartment one, not an arm gradient.
cooltools.eigs_cis(clr, gc_track, view_df=arms, n_eigs=3, sort_metric='pearsonr')The two most damaging beginner assumptions are "E1 = compartments" and "positive E1 = active." Both are false out of the box, and both fail silently -- the pipeline runs, returns a track, and is wrong.
E1 is not guaranteed to be the compartment track. On a whole-chromosome O/E correlation matrix the largest eigenvalue very often belongs to a smooth p-arm-vs-q-arm or centromere-to-telomere GRADIENT, not the plaid A/B checkerboard -- the real compartment signal then lands in E2 or E3. cooltools' own docs concede the first eigenvector "occasionally describes chromosomal arms or translocation blowouts." The compartment eigenvector is the one with the largest |correlation| to an activity track (GC, gene density, H3K27ac), not the one with the largest eigenvalue. The structural fix removes the gradient at the source: run eigs_cis per chromosome ARM (a view_df split at centromeres, from bioframe.make_chromarms), so the arm gradient is never in the within-arm matrix. Set sort_metric='pearsonr' so the returned eigenvectors are ordered by GC correlation, not eigenvalue -- otherwise the arm gradient is reported as "E1." A monotonic "compartment track" with no sign flips across a chromosome is the failure signature of a captured arm gradient.
The sign is arbitrary until phased. Eigenvectors are defined up to sign; the positive lobe is meaningless and can differ per chromosome AND per sample. The eigenvector MUST be oriented with an external active-chromatin track via the phasing_track argument so A = positive. GC content is the field default (it needs no extra assay and tracks compartment A; Lieberman-Aiden 2009 Science 326:289) -- compute it with bioframe.frac_gc at the compartment resolution, exactly matching the cooler's binning. Wrong/weak phasing flips A<->B silently, and every downstream saddle, switch call, and differential result inverts with no error. This is a classic source of irreproducible compartment papers.
Compartments are an equilibrium phenomenon decoupled from TADs/loops. Compartments = microphase separation of A/B chromatin states (cohesin-independent; survive cohesin loss, Schwarzer 2017 Nature 551:51, and CTCF loss, Nora 2017 Cell 169:930). TADs/loops = ATP-driven loop extrusion stalled at CTCF. Removing cohesin reinforces compartments while erasing TADs (Schwarzer 2017 reports reinforced compartmentalization on Nipbl loss; Rao 2017 Cell 171:305 eliminates all loop domains with compartments retained) -- loop extrusion actively mixes chromatin across compartment boundaries, so removing the extruder lets microphase separation run to completion (Nuebler 2018 PNAS 115:E6697). A preserved-or-stronger saddle after a cohesin/Nipbl/RAD21 perturbation is the EXPECTED result, not a bug; compartment-strength and TAD-strength are antagonistic. If a CTCF/cohesin perturbation makes compartments vanish, suspect a phasing artifact, not biology.
| Output | Tool / call | What it is | When |
|--------|-------------|-----------|------|
| A/B eigenvector (E1) | cooltools.eigs_cis (cis, per-arm) | leading GC-phased eigenvector of the cis O/E correlation matrix; sign = A/B | standard A/B call, single map, per chromosome arm |
| Genome-wide A/B | cooltools.eigs_trans | eigenvector of inter-chromosomal blocks; immune to the cis arm-gradient | whole-genome A/B consensus with deep trans coverage |
| Compartment strength | cooltools.saddle + saddle_strength | (AA+BB)/(AB+BA) corner ratio of the saddle | comparing compartmentalization across conditions |
| 5-6 subcompartments | SNIPER (Xiong & Ma 2019 Nat Commun 10:5069) | autoencoder imputes inter-chr contacts -> MLP classifies A1/A2/B1/B2/B3 at 100kb | deep inter-chr data; Rao-style subcompartments |
| Continuous compartment rank | Calder (Liu 2021 Nat Commun 12:2439) | intra-chr divisive hierarchical clustering -> 0-1 multi-scale rank | cross-cell-line repositioning; modest coverage |
| Differential compartments | dcHiC (Chakraborty 2022 Nat Commun 13:6827) | quantile-normalized scores + multivariate Mahalanobis distance + significance; solves cross-sample sign flips | >=2 samples, "which bins switch A<->B" |
| Single-cell compartment | scA/B (Tan 2018 Dip-C Science) | CpG/activity proxy per locus; do NOT eigendecompose one sparse cell | scHi-C, ~20-50k contacts/cell |
| Scenario | Recommended | Why |
|----------|-------------|-----|
| Matrix not yet balanced | cooler balance first (-> matrix-operations) | unbalanced -> O/E is all-NaN; meaningless eigenvector |
| Standard A/B call, one map | eigs_cis per arm at 100kb-1Mb, phase by GC | compartments are chromosome-scale; arms remove the centromere gradient |
| E1 looks monotonic / no sign flips | inspect E2/E3, pick by max |corr| with GC; or split by arm | E1 captured an arm/translocation gradient, not compartments |
| Sign of A/B seems inverted | confirm phasing_track is at the cooler's binning | weak/mismatched phasing flips A<->B silently |
| Want compartment STRENGTH | saddle + saddle_strength, fixed extent across samples | a single eigenvector does not quantify strength |
| Want 5-6 subcompartments | SNIPER or Calder, NOT more eigenvectors | subcompartments need inter-chr ML or hierarchical clustering, not n_eigs |
| Two+ conditions, compartment shift | -> hic-differential (dcHiC) | replicate-aware, sign-coherent across the cohort; hand-diffing eigenvectors flips signs |
| Single-cell Hi-C | scA/B (Dip-C), not a per-cell eigenvector | one sparse cell is too noisy to eigendecompose |
| Annotate switched bins with marks | -> chip-seq/chromatin-state-segmentation, chip-seq/peak-annotation | overlay ChromHMM/histone state on compartment calls |
| Render the eigenvector/saddle | -> hic-visualization; export bigWig -> genome-intervals/bigwig-tracks | track/heatmap conventions live there |
Goal: Assign each genomic bin to the active (A) or inactive (B) compartment with a non-arbitrary sign, avoiding the centromere arm-gradient artifact.
Approach: Build a per-arm view_df (split at centromeres) so the arm gradient never enters the matrix; compute a GC-content phasing track at the cooler's exact binning; run eigs_cis with the GC track and sort_metric='pearsonr' so eigenvectors are ordered by GC correlation; then take the GC-correlated eigenvector as the compartment track.
import cooler
import cooltools
import bioframe
clr = cooler.Cooler('matrix.mcool::/resolutions/100000') # 100kb: compartments are coarse-scale
chromsizes = clr.chromsizes
cens = bioframe.fetch_centromeres('hg38')
arms = bioframe.make_chromarms(chromsizes, cens) # per-arm view removes the centromere gradient
arms = arms[arms.chrom.isin(clr.chromnames)].reset_index(drop=True)
genome = bioframe.load_fasta('hg38.fa') # FASTA index (.fai) must exist
bins = clr.bins()[:][['chrom', 'start', 'end']]
gc = bioframe.frac_gc(bins, genome) # phasing track at the cooler's exact binning
eigvals, eigvecs = cooltools.eigs_cis(clr, gc, view_df=arms, n_eigs=3, sort_metric='pearsonr')
eigvecs['compartment'] = ['A' if e > 0 else 'B' for e in eigvecs['E1']] # GC-phased: positive E1 = A
After phasing, sanity-check that E1 correlates with gc['GC'] (sign and magnitude). If the strongest correlation is in E2/E3, that component -- not E1 -- is the compartment track; re-derive the call from it.
Goal: Quantify how strongly the genome demixes into A and B with a single comparable number across conditions.
Approach: Compute the distance-decay expected; pass the cooler, the expected, and the phased E1 eigenvector to saddle, which digitizes E1 into quantile groups internally (via qrange) and aggregates O/E into a 2D table of same-vs-cross-compartment interactions; then read saddle_strength (the (AA+BB)/(AB+BA) corner ratio) at one fixed extent, applied identically to every sample compared.
N_GROUPS = 38 # quantile groups for digitizing E1; ~30-50 is conventional (cooltools tutorial)
Q_LO, Q_HI = 0.025, 0.975 # trim the extreme 2.5% tails before digitizing to resist outlier bins
expected = cooltools.expected_cis(clr, view_df=arms) # has the 'balanced.avg' column saddle needs
track = eigvecs[['chrom', 'start', 'end', 'E1']] # the SAME phased E1 used for the A/B call
interaction_sum, interaction_count = cooltools.saddle(
clr, expected, track, 'cis', n_bins=N_GROUPS, qrange=(Q_LO, Q_HI), view_df=arms
)
strength = cooltools.api.saddle.saddle_strength(interaction_sum, interaction_count) # 1D array; lives in cooltools.api.saddle, not top level
EXTENT = N_GROUPS // 5 # read strength at the top/bottom ~20% of bins; pick one extent, use it everywhere
score = strength[EXTENT]
saddle_strength returns an ARRAY (cumulative corner ratio over increasing extent), not a scalar -- there is no canonical single number, so choose an extent and apply it identically across compared samples. Mismatched n_bins, resolution, qrange, or extent make strengths incomparable. Remember: a preserved-or-higher strength after cohesin/Nipbl/RAD21 loss is the expected result.
Goal: Find bins that switch A<->B between two conditions without being fooled by per-sample sign flips.
Approach: Do NOT independently phase two eigenvectors and diff them bin-by-bin -- a weak per-chromosome GC correlation can flip the sign in one sample only, manufacturing fake "switches." Use dcHiC, which computes eigenvectors on quantile-normalized scores in a shared framework (sign-coherent across the cohort) and reports a multivariate significance per bin. Route this to hic-differential.
# Hand-diffing is only safe when you have CONFIRMED both eigenvectors are sign-coherent (same arms phased
# to the same GC track with strong correlation). Otherwise use dcHiC -- see hic-differential.
import pandas as pd
merged = eig1.merge(eig2, on=['chrom', 'start', 'end'], suffixes=('_1', '_2'))
merged['switch'] = (merged['E1_1'] > 0) != (merged['E1_2'] > 0) # only meaningful if both are phased coherently
Trigger: eigs_cis run per whole chromosome (no per-arm view_df) and/or sort_metric=None. Mechanism: the largest eigenvalue belongs to the smooth p-vs-q arm / centromere gradient, not the A/B checkerboard. Symptom: a monotonic "compartment track" across a chromosome with no sign flips; weak correlation of E1 with GC. Fix: run per chromosome arm (bioframe.make_chromarms); set sort_metric='pearsonr'; pick the eigenvector with the largest |corr| to GC.
Trigger: eigs_cis called with phasing_track=None. Mechanism: the sign of an eigenvector is mathematically arbitrary. Symptom: active euchromatin lands in "B"; A/B inverted relative to GC; per-chromosome sign inconsistency. Fix: pass a GC (or gene-density / H3K27ac) phasing_track so positive E1 = A.
Trigger: GC/activity track computed at a different resolution than the cooler. Mechanism: cooltools aligns the track to the cooler bins; a mismatch yields garbage correlations or a silent no-op. Symptom: phasing has no effect, or signs are random. Fix: compute the track on clr.bins() at the exact compartment resolution.
Trigger: eigs_cis at 5-25kb. Mechanism: compartments are a 100kb-1Mb feature; fine bins are sparse and dominated by TAD/loop structure and noise. Symptom: a noisy, jagged E1 that does not correlate with GC. Fix: call at 100kb-1Mb (250kb common; up to 1Mb for shallow data).
Trigger: raising n_eigs to "get A1/A2/B1/B2/B3". Mechanism: Rao's 6 subcompartments came from clustering inter-chromosomal patterns in a 4.9-billion-contact map, not from extra eigenvectors. Symptom: higher eigenvectors are noise, not finer biology. Fix: use SNIPER (inter-chr ML) or Calder (hierarchical); for differential use dcHiC.
Trigger: subtracting/comparing two per-sample eigenvectors bin-by-bin. Mechanism: a weak GC correlation can flip the sign in one sample only. Symptom: spurious "compartment switches" concentrated on whole chromosomes/arms. Fix: use dcHiC (sign-coherent quantile-normalized framework) -- see hic-differential.
Trigger: different n_bins, qrange, resolution, or extent between compared saddles. Mechanism: saddle_strength is an extent-dependent array, not an absolute scalar. Symptom: strength differences that track the settings, not the biology. Fix: fix n_bins, qrange, resolution, and the corner extent; apply identically to all samples.
| Threshold | Source | Rationale |
|-----------|--------|-----------|
| Compartment resolution 100kb-1Mb (250kb typical) | compartment scale (Lieberman-Aiden 2009) | finer bins mix in TAD/loop structure and sparsity noise; A/B is chromosome-scale |
| Run per chromosome ARM | eigenvector-selection (cooltools docs; Mirny lab) | removes the centromere/arm gradient that otherwise hijacks E1 |
| sort_metric='pearsonr' | cooltools default-mismatch | default sorts by eigenvalue, so the arm gradient is reported as E1; pearsonr sorts by GC correlation |
| n_eigs>=3 and inspect eigenvalues | eigenvector-selection | n_eigs=1 hides the arm-vs-compartment problem; which component is biology is then undeterminable |
| clip_percentile=99.9 (cooler eigs_cis default) | outlier suppression | dense cis_eig defaults clip_percentile=0; the entry points differ -- do not assume |
| Saddle quantile groups ~30-50; trim 2.5% tails | cooltools tutorial | enough groups to resolve the saddle; tail trim resists outlier bins |
| Saddle strength at a fixed extent (e.g. top/bottom ~20%) | saddle_strength is an array | no canonical scalar; one extent, applied identically across samples |
| Subcompartments require deep inter-chr data | Rao 2014 (4.9B contacts) | shallow maps + extra eigenvectors give noise, not subcompartments |
| Error / symptom | Cause | Solution |
|-----------------|-------|----------|
| clr.matrix(balance=True) / O/E all NaN | cooler not balanced | run cooler balance / cooler.balance_cooler first (-> matrix-operations) |
| Empty / wrong-resolution result on .mcool | bare .mcool passed | use file.mcool::/resolutions/<bp> URI |
| A/B compartments inverted | eigenvector sign unphased or weak phasing | pass a GC/gene-density phasing_track at the cooler's binning |
| E1 monotonic, no sign flips | whole-chromosome run captured the arm gradient | run per arm (make_chromarms); pick by max |corr| with GC |
| frac_gc / empty eigenvector on some chroms | chrom naming mismatch (chr1 vs 1) across cooler/FASTA/centromeres | harmonize names; subset the view to clr.chromnames |
| saddle KeyError on balanced.avg | wrong/absent expected table | pass cooltools.expected_cis(clr, view_df=...) output and contact_type='cis' |
| AttributeError on a cooltools function | pre-0.7 vs 0.7+ API change | help(cooltools.eigs_cis); update to the viewframe signature |
tools
--- name: bio-phasing-imputation-foundations description: Frames the phasing/imputation pipeline before any tool runs: phasing and imputation are one Li-Stephens copying HMM (recombination is the transition, mutation the emission, the genetic map and Ne set the rates), imputation's honest output is a dosage with a self-estimated quality (INFO/R2/DR2) not a hard genotype, and the stages are ordered and each fails silently (QC, align build and strand to the panel, phase, impute per chromosome, fil
tools
Chooses the enrichment generation before any tool runs, mapping the input shape to a method class - a pre-selected gene list plus a background to over-representation analysis (ORA, hypergeometric), a ranked statistic for all genes to gene set enrichment (GSEA), a signed signaling topology to pathway-topology (SPIA) - then making the null explicit (competitive vs self-contained, gene vs subject sampling) and running a trustworthiness checklist (testable-gene universe, FDR, redundancy collapse, leading-edge check, version reporting). Covers why every clusterProfiler GSEA is the inter-gene-correlation-uncorrected competitive null, why the background not the gene list decides ORA significance, and why no method is universally best. Use when deciding ORA vs GSEA vs topology, which gene-set DB, whether a result is trustworthy, or which null a tool computes. For ORA see go-enrichment, GSEA see gsea, databases kegg-pathways/reactome-pathways/wikipathways; the ranking comes from differential-expression/de-results.
testing
End-to-end GWAS workflow from VCF to association results. Covers PLINK QC, population structure correction, and association testing for case-control or quantitative traits. Use when running genome-wide association studies.
development
Orchestrates the full path from differential expression results to redundancy-collapsed functional enrichment: choose ORA vs GSEA, convert gene IDs per method, run enrichGO/enrichKEGG/enrichPathway/enrichWP or gseGO/gseKEGG (clusterProfiler, ReactomePA, rWikiPathways), and visualize. Routes the ORA-vs-GSEA generation fork and the null/universe/reproducibility theory to pathway-analysis/enrichment-foundations. Use when a DESeq2/edgeR/limma result must become enriched GO terms, KEGG/Reactome/WikiPathways pathways, or a GSEA leading edge; when deciding whether a ranking exists for all genes (GSEA, named decreasing vector) or only a pre-selected list (ORA plus a defensible background universe); or when assembling DE-to-pathway end to end. The DE list and ranking statistic come from differential-expression/de-results; per-method nuance lives in the pathway-analysis skills.