Architecture

Architecture is a chromatin-contact graph stored as two tables: the CRE Loci are the vertices (row i = vertex i) and a sorted edge table holds src, tgt and the edge columns. Loops make edges, Hi-C contacts weigh them, and graph algorithms run through the graph backend.

  1. Two tables
  2. The pipeline
  3. From loops to weighted edges
  4. Sharing a pixel
  5. Genes and hubs
  6. The graph backend seam
  7. Drawing a region
  8. Tables out, storing and combining
  9. Costs

Two tables

Part What it holds
A.loci the CRE Loci, genome-sorted; vertex i is row i
A.src, A.tgt int32 row numbers, src < tgt, one entry per edge
A.ep edge columns: w (Hi-C weight), d (distance), n (observed / expected), … — float arrays aligned to the edges
A.vp vertex columns: annot, gene, strength, component, … — arrays aligned to the Loci rows
import genomeblocks as gb

cre = gb.Loci.make("peaks.bed")
A = gb.Architecture.make(cre, "loops.bedpe", r=100)
# -> [INFO] 4 loops | 4 mapped (100.0%) | loci=6, links=4 (1 trans)
A.loci is cre
# -> True
A.src, A.tgt
# -> (array([0, 1, 5, 0], dtype=int32), array([2, 4, 6, 5], dtype=int32))
A.blocks
# -> {'chr1': (0, 2), 'chr2': (2, 3), 'trans': (3, 4)}
A.degree, A.n_loci, A.n_links, A.n_trans
# -> (array([2, 1, 1, 0, 1, 2, 1]), 6, 4, 1)

Because vertex ids are row numbers, every vertex column, annotation and signal cube built from the same Loci lines up with the graph by position: there is no uid → vertex dictionary. A[uid] and A[uid1, uid2] still work through loci.uids for lookups by name.

EDGES (src < tgt), SORTED: | cis chr1 | cis chr2 | … | cis chrX | trans | chr1 chr2 chr3 … chrX trans A.cis = edges[: trans_start] A.chrom('chr2') = edges[lo:hi] A.trans views: two offsets, no copy AS A MATRIX cis blocks on the diagonal, a few trans dots off it A.neighbors(row) one CSR adjacency over all edges: trans partners included A.graph(backend=) the graph backend's object, built from the arrays on demand A.save(path) parquet: vertices + edges tables, readable from R / polars
Views are offsets, not copies. Edges are kept sorted so each chromosome's cis edges form one contiguous block, in genome order, and every trans edge sits in a final block. A.chrom("chr2"), A.cis and A.trans are slices of the same arrays; they share the vertex columns, and edits through a view write through to the full graph.
  • Canonical order. The constructor sorts edges by (block, src, tgt), where a cis edge’s block is its chromosome’s natural rank and every trans edge comes after; blocks finds the block boundaries once with diff.
  • Views. chrom(c), cis, trans, region(...), subgraph(rows=, vp=, values=) and A & B return an Architecture over the same Loci with a slice (or a selection) of the edge arrays.
  • Neighbours come from one CSR adjacency over all edges (built on first use), so a CRE’s trans partners are always included; neighbors(x) returns them with the edge columns as a DataFrame.
A.chrom("chr1").src, A.cis.n_links, A.trans.src, A.trans.tgt
# -> (array([0, 1], dtype=int32), 3, array([0], dtype=int32), array([5], dtype=int32))
A.neighbors(0)
# ->    row                uid chrom    cis    w
# -> 0    2  chr1:4900-5100(+)  chr1   True  0.0
# -> 1    5    chr2:500-600(+)  chr2  False  0.0

The pipeline

READS STEP WRITES Loci + BEDPE / Pairs make() vertices · edge table .mcool at 5 kb .add_mcool() ep.w (Hi-C count) CRE positions .normalize() ep.d · ep.n (O/E) Genes .annotate() vp.annot · vp.gene ep.n .strength() vp.strength vp.strength .prime_hubs() hub CREs → genes
Six chained calls, each writing a column. Edge columns (ep.w, ep.d, ep.n) and vertex columns (vp.annot, vp.gene, vp.strength) are plain arrays in two dicts. Each step reads what the previous one wrote, so the steps run in this order; a step whose input column is missing raises and names the call that writes it.

From loops to weighted edges

1 · MAKE anchor 1 ± r anchor 2 ± r loop a b c a b c edges a–c, b–c (never within one anchor) 2 · ADD_MCOOL 12 bin i bin j a, b → bin i · c → bin j edges a–c, b–c share pixel (i, j) ep.w = 12 / 2 = 6 each 3 · NORMALIZE w (log) distance d (log) fit: E(d) = C / d^α cis: ep.n = w / E(d) trans: d = ∞, E = mean trans w
Loops make edges, Hi-C weighs them, distance normalises them. make takes every CRE within ± r of each loop anchor's midpoint and links every CRE under one anchor to every CRE under the other. add_mcool maps each CRE to its cooler bin and gives each edge the pixel count of its bin pair, divided among the edges that share that pixel. normalize divides by the expected contact at that distance.
  • make(loci, bedpe, r=2500, dmax=1e9, trans=True) reads the loops with as_pairs (a BEDPE path, a Pairs or a frame), re-codes the anchors onto the CREs’ Genome, drops cis loops longer than dmax (trans loops stay unless trans=False), builds one Loci of widened anchor midpoints per side, and asks the intervals backend for overlap_pairs(anchors, loci). The two hit lists are crossed per loop (_cross, a repeat-based group product), self-pairs are dropped, and unique on lo · n + hi keys makes each CRE pair one edge. Every interval backend gives the same edge list.
  • add_mcool(mcool, resolution=, weights=None, diagonal=False) maps every CRE to the cooler bin of its start from the bin offsets, forms one (bin1, bin2) key per edge, and reads the pixel table one chromosome block at a time (rows of the lower bin), so memory stays bounded and trans pixels are found in the same pass. A pixel’s count is split across the edges in it (evenly, or in proportion to weights[i] · weights[j]), so dense clusters of CREs do not multiply one contact. Edges with both CREs in one bin, and edges without a pixel, keep w = 0. See Sharing a pixel.
  • normalize(source="w", name="n") fits w ≈ C · d^−α on cis edges with positive distance and weight and writes ep.d and ep.n = w / E(d). The fit needs at least three such edges at two distinct distances; without them normalize warns (a UserWarning naming the usable cis-edge count), A.fit holds alpha = NaN and the cis n is 0. Trans edges have no distance: ep.d = inf and their expectation is the mean trans weight.
  • prune() removes co-located cis edges (d = 0); trans edges stay.
A.ep["w"][:] = [5.0, 3.0, 2.0, 1.0]
A.normalize()
# -> [INFO] Power-law fit: alpha=0.397, C=1.006e+02 on 3 cis edges; 1 trans edges use the mean trans weight → ep.n
A.ep["d"], A.ep["n"].round(3)
# -> (array([4000., 9000., 4500.,   inf]), array([1.341, 1.11 , 0.562, 1.   ]))

Sharing a pixel

A pixel is the finest thing a cooler measures, and CREs are smaller than a bin, so add_mcool has to decide which bin a CRE belongs to and who gets a pixel that several edges fall in. Both choices shape CRE strengths, and with them the super-hubs. The effect is largest when the CRE set changes: one sample’s graph against a graph on the CREs of several samples.

WHICH BIN A CRE GOES TO bin i − 1 bin i bin i + 1 bin i + 2 a b start → bin i (add_mcool) centre → bin i + 1 a: start and centre in bin i · b: starts in bin i, centre in bin i + 1, so its pixels are read from bin i's row
A CRE is binned by its first base. b crosses the boundary into bin i + 1 and its centre lies there, but its edges read row i of the map.

Binning. Start binning is cheap and unambiguous, but on ATAC-seq CREs (median ~950 bp) 23% cross a 5 kb boundary and 11% have their centre in another bin. Binning by centre would put 89–90% of counted edges on a pixel FitHiChIP called as a loop, against 82–83% now. It is not the default: it fixes neither problem below, and on its own it moves about a quarter of the super-hubs.

1 · EQUAL SPLIT bin i bin j 6 6 a b c pixel (i, j) = 12, two edges each CRE of bin i: 12 / 2 2 · UNION GRAPH: DILUTION bin i bin j 6 6 a b c b: a peak in another sample only own graph a–c = 12 · union 6 3 · PEAK MASK (weights=) bin i bin j 12 0 a b c weights a = 1, b = 0: a–c 12, b–c 0 own graph unchanged · 12 handed out once peak in this sample no peak in this sample
The equal split, its dilution in a union graph, and the peak mask. Every CRE pair of two bins is usually an edge, so the equal split gives each CRE the pixel count divided by the CREs in its own bin. A CRE with no peak in the sample still counts, so in a union graph it takes half of its neighbour's reads; weights= set to the sample's peak mask hands the pixel to the edges between CREs with a peak.

The equal split. Within one graph the equal split conserves contact exactly: the edge weights sum to the counts of the pixels they touch. But the denominator is the number of edges the skeleton puts in the pixel, so it depends on the CRE set. On H3K27ac HiChIP of naive CD4, Th17 and Treg cells, a union graph (the CREs with a peak in any of the three states, the loops of all three) shares 3.5–4.3% of each state’s contact with edges that touch a CRE closed in that state. Those edges land on a few CREs: 42 of the 43 super-hubs Th17 loses between its own graph and the union have such a CRE in their 5 kb bin and keep 0.54× of their strength (median). weights= with the state’s peak mask removes that loss and leaves each state’s own graph bit for bit as it was, since every CRE of an own graph has a peak.

A LOOP BETWEEN ADJACENT BINS anchor 1 ± r = bin i anchor 2 ± r = bin i + 1 loop x y z x–y: both start in bin i → pixel (i, i), the bin's self-contact x–z, y–z: → pixel (i, i + 1), the called loop diagonal=False (default): x–y gets 0; a loop never has both anchors in one bin CONTACT MAP (5 kb) x–y loop diagonal: self-contact, ~100× a loop pixel (H3K27ac HiChIP, 5 kb)
The self-contact pixel. A loop between adjacent bins has the two bins as anchor windows; a CRE crossing the boundary sits under both, so start binning puts both CREs of an edge in one bin and the edge reads the bin's self-contact.

The diagonal. About 9% of FitHiChIP loops at 5 kb join adjacent bins. The edges they produce between two CREs of one bin are 0.2–0.3% of the T-cell edges but carry ~10% of the contact (median self-contact 1,100–1,800 reads against 15 for a counted pixel), and 17–22% of the super-hubs take more than half of their strength from them. A union graph adds the other states’ adjacent-bin loops, which lifts the knee for everyone else. diagonal=False (the default) gives these edges 0.

RULE CONSERVES CONTACT NO DILUTION BY CLOSED CRES NO SELF- CONTACT HUBS LOST N / TH17 / TREG TRADE-OFF equal split diagonal=True (2.0.1) yes no no 76 / 43 / 171 closed CREs in a bin take equal shares no split full count per edge no yes no 0 / 3 / 2 +19–27% contact; partner CREs inflate each pixel once per CRE a node rule no yes no 68 / 51 / 0 +9–13%; one bin's CREs counted twice centre binning bin of the CRE centre yes no no 30 / 31 / 49 more edges on their loop pixel; dilutes ATAC-weighted weights=accessibility yes partly no 31 / 25 / 18 closed CREs keep a residual share peak mask weights=mask, diagonal=True yes yes no 35 / 0 / 121 self-contacts still lift the knee peak mask, no diagonal weights=mask (default diag.) yes yes yes 8 / 0 / 7 every graph's hubs move 14–29%
Seven rules on the T-cell graphs. Super-hubs = slope-1 knee on CRE strength; "lost" = own-graph super-hubs that are not super-hubs of the union graph (naive / Th17 / Treg). Conservation = summed edge weight over the summed counts of the pixels touched.
rule add_mcool call super-hubs lost, N / Th17 / Treg contact conserved own graphs vs 2.0.1
equal split (2.0.1) diagonal=True 76 / 43 / 171 yes same
no split — 0 / 3 / 2 +19–27% 24–32% of super-hubs move
each pixel once per CRE — 68 / 51 / 0 +9–13% 15–37% move
centre binning — 30 / 31 / 49 yes 21–26% move
ATAC-weighted weights=atac, diagonal=True 31 / 25 / 18 yes 5–17% move
peak mask weights=mask, diagonal=True 35 / 0 / 121 yes same
peak mask, no diagonal weights=mask 8 / 0 / 7 yes 14–29% move

The two rules that remove the dilution without double counting are the ones add_mcool offers. The peak mask needs the sample’s peaks, which only the caller has, so weights=None stays the default. The self-contact is never a loop pixel, so diagonal=False is the default; diagonal=True gives the 2.0.1 weights. In the T-cell graphs the two together turn 76 / 43 / 171 lost super-hubs into 8 / 0 / 7, and the IKZF2 promoter in Treg (two CREs in one bin, 2,180 counts each in the union) clears the union cut-off (2,012). The script and these numbers are in benchmarks/bench_mcool_split.py and benchmarks/results/mcool_split.json. The script also runs the comparison on a seeded synthetic pair of samples: with the equal split ~255 own CREs are weaker in the union, and the ~360 that share a bin with a CRE closed in the sample keep 0.62–0.67× of their own-graph strength; with the peak mask 1–2 are weaker and those ~360 keep 1.11–1.21×.

Genes and hubs

ANNOTATE · GENE ASSIGNMENT 3.1 1.2 0.9 2.4 1.7 1.1 MYC PVT1 e1 → MYC e2 → PVT1 e3 → MYC e4 → PVT1 each enhancer takes the gene of its highest-O/E promoter neighbour PRIME_HUBS · SLOPE-1 KNEE knee: tangent slope = 1 on the [0, 1]-scaled curve hubs CREs ranked by node strength (Σ incident O/E) hub CREs → their vp.gene → prime genes
Contacts assign genes; strength finds hubs. annotate labels each CRE with its region class (see Genes). A promoter CRE takes its nearest gene. Every other CRE takes the gene of the promoter it contacts most strongly, by the edge column key (O/E by default), cis or trans. strength sums each vertex's incident O/E. prime_hubs ranks CREs by that strength, cuts at the point where the scaled curve's slope reaches 1, and returns the hubs' genes.
  • annotate(genes) is genes.labels(A.loci) plus genes.nearest_tss on the promoter rows; the gene assignment is vectorised over the edge arrays: one lexsort by (vertex, −weight) and the first row per vertex wins. Use a different name= per weight key (key="n_CM", name="gene_CM") to keep several assignments side by side.
  • strength(key) is two bincounts over src and tgt.
  • elbow(key) / prime_hubs use se.knee, the same slope-1 rule on the smoothed, 0–1-scaled ranked curve that call_se uses for super-enhancers.
  • support(genes, r=5000) finds the linked CREs within r of every gene’s TSS in one overlap_pairs call, as {gene_name: [uid, ...]} (or row arrays with rows=True).
genes = gb.Genes.make("genes.gtf")
A.annotate(genes, verbose=False).strength(verbose=False)
A.vp["annot"][:3], A.vp["gene"]
# -> (array(['Promoter-TSS', 'Promoter-TSS', '3UTR'], dtype=object),
#     array(['GENE_A', 'GENE_A', 'GENE_A', 'GENE_B', 'GENE_B', 'GENE_A', ''], dtype=object))
A.support(genes, r=5000)
# -> {'GENE_A': ['chr1:900-1100(+)', 'chr1:1900-2100(-)', 'chr1:4900-5100(+)'], 'GENE_B': ['chr1:10900-11100(-)']}

The chr2 CRE in row 5 is Intergenic and gets GENE_A through its trans edge to the GENE_A promoter: assignments follow every edge, whatever the block.

The graph backend seam

Nothing in the tables depends on a graph library. When an algorithm needs a graph object, backends.graph.native(A, backend) builds one from the arrays (vertex i = Loci row i, every edge, edge columns as edge attributes, vprops copied on request) for the engine in use:

Backend Object Default?
graph-tool graph_tool.Graph (built once, cached; edge columns re-synced on every A.graph()) first choice when installed (conda)
scipy symmetric CSR adjacency, scipy.sparse.csgraph the pip default
igraph igraph.Graph on request
networkx networkx.Graph (parallel edges merged, their numeric columns summed, as in the adjacency) on request

graph-tool and scipy are the only two in the automatic list, because they give identical answers; components renumbers every engine’s labels by the first row of each component (rows without links get -1), and pagerank returns one array per row for every engine, so results do not depend on the engine. The scipy PageRank is a power iteration on the column-stochastic matrix with dangling mass spread uniformly, matching the others to 1e-6.

A.components()
# -> array([ 0,  1,  0, -1,  1,  0,  0])
for b in ("scipy", "igraph", "networkx"):
    print(b, A.components(backend=b).tolist(), A.pagerank("w", backend=b).round(4).tolist())
# -> scipy [0, 1, 0, -1, 1, 0, 0] [0.2178, 0.1626, 0.1786, 0.0244, 0.1626, 0.1466, 0.1074]
# -> igraph [0, 1, 0, -1, 1, 0, 0] [0.2178, 0.1626, 0.1786, 0.0244, 0.1626, 0.1466, 0.1074]
# -> networkx [0, 1, 0, -1, 1, 0, 0] [0.2178, 0.1626, 0.1786, 0.0244, 0.1626, 0.1466, 0.1074]
type(A.graph()).__name__                    # scipy here: graph-tool is not installed
# -> 'csr_matrix'
A.components(backend="graph-tool")                  # without graph-tool
# -> ImportError: the 'graph-tool' graph backend is not installed: conda install -c conda-forge graph-tool

to_graph_tool, to_networkx, to_igraph, to_scipy(weight) and to_anndata() (vertices as obs, one sparse adjacency per edge column in obsp, the layout scanpy’s graph tools read) are the exports; backends.graph.layout gives 2-D positions (the engine’s force-directed layout, scipy’s spectral layout, a circle, or genomic position) for drawing.

Drawing a region

A.draw(region, layout="spring" | "circular" | "genomic", backend=None, merge_distance=None, vertex_size_by=, vertex_color=, edge_width_by="w") (architecture_draw.draw) takes the linked CREs of a region from loci.overlap_rows, optionally merges CREs closer than merge_distance into one node, sums parallel edges, asks backends.graph.layout_edges for positions, and draws nodes and edges with plain matplotlib. The genomic layout puts nodes on a line at their position and edges as arcs, a browser-like view. No graph library is needed for the circular and genomic layouts; the spring layout uses whichever graph backend is in use.

ax = A.draw("chr1:0-12 kb", layout="genomic")
ax.get_title().splitlines()[-1]
# -> '4 nodes, 2 edges'
ax = A.draw(("chr1", 0, 12_000), merge_distance=1500, vertex_size_by="strength", vertex_color="annot", backend="networkx")
ax.get_title().splitlines()[-1]
# -> '3 nodes (4 CREs merged at 1,500 bp), 2 edges'

Tables out, storing and combining

  • As a table an Architecture is its edge table: shape, columns, head(), describe(), to_pandas() / edges_frame() (src, tgt, uids, chromosomes, cis, edge columns), to_polars, to_arrow and the TableMixin protocols. vertices_frame() is the vertex side with every vp column; block_counts() is the chromosome × chromosome edge count.
  • from_edges(loci, src, tgt, **ep), from_frame(loci, df), from_scipy(loci, M) build a graph from arrays, an edge table (rows or uids) or a sparse matrix.
  • save(dir) / load(dir) write vertices.parquet (the Loci plus vp: columns), edges.parquet (src, tgt, ep: columns) and meta.json, readable from pandas, polars or R.
  • G | H, G & H combine graphs on the same Loci by edge key; copy() is deep.
B = gb.Architecture.from_frame(cre, A.edges_frame())
list(B) == list(A)
# -> True
A.save("arch"); back = gb.Architecture.load("arch")
list(back) == list(A), list(back.vp)
# -> (True, ['annot', 'gene', 'strength', 'component', 'pagerank'])
import polars as pl
pl.DataFrame(A).shape
# -> (4, 10)

Costs

  • make: two overlap_pairs (anchors against CREs), one group cross product, one unique; no Python loop over loops.
  • add_mcool: one pass over the pixel table, chromosome block by block, with a searchsorted per block.
  • normalize, strength, annotate: a curve_fit on the cis edges, bincounts, one lexsort over the edges.
  • Views: two offsets, no copy; the adjacency is one argsort of 2 · n_links entries, built once.
  • Graph algorithms: the engine’s cost plus one array copy in and out; the graph-tool object is cached.

Measured numbers are on the Benchmarks page.


Copyright © 2024–2026 Umut Berkay Altintas. MIT Licensed.

This site uses Just the Docs, a documentation theme for Jekyll.