nx_pileup.nx
buildroot/runtime/nx_pileup.nx
about
nx_pileup.nx -- CIGAR walking primitives for variant-calling pileup.
license_tier: INDEPENDENT_REDERIVE
genealogy_id: international-research-sources/sam-spec-v1.4.6 + li-2009-samtools-pileup
G2.1a of NISHI_GENOMICS_SUBSTRATE_ROADMAP.md. The math foundation
for variant calling: given an aligned read's CIGAR string and its
alignment ref-start, compute "what query base contributes to a
specific reference position?" Aggregating this across many reads
at a single ref position IS the pileup, and the pileup IS the
input to germline + somatic variant callers.
CIGAR walking model (SAM-spec consumption flags from nx_cigar):
For each op in alignment order:
- if consumes_ref: advance ref-position counter by op_len
- if consumes_query: advance query-position counter by op_len
Until ref-position counter reaches the target. The resulting
query-position counter is the read offset of the base that
aligns to the target ref position (or in_gap=1 if the target
falls inside a D / N op).
Why this primitive matters:
- Variant calling: aggregate per-position base counts -> SNVs
- Indel detection: I/D ops at specific ref positions
- Coverage depth: count reads spanning a position
- Quality recalibration (BQSR): per-position quality stats
- Pileup visualisation (IGV / samtools tview)
API:
cigar_query_pos_at_ref(
cigar_codes, cigar_lens, n_groups,
r_start, target_r,
out_q_pos, out_in_gap
) -> i64
r_start : reference start position of this alignment (the
SAM POS field minus 1 if interpreting SAM 1-indexed;
substrate uses 0-indexed throughout)
target_r : the reference position we want the read-base for
out_q_pos : query offset (0-indexed into read sequence)
dependencies 5 imports · 3 importers
imports: nx_syscalls.nxnx_const.nxnx_cigar.nxnx_sequence.nxnx_sequence_ambig.nx
imported by: nx_pileup_test.nxnx_variant.nxnx_variant_test.nx
structs
| none |
consts
| 88 | const NX_PILEUP_FLAG_BASE: i64 = 0 // normal ACGT base contributed |
| 89 | const NX_PILEUP_FLAG_N: i64 = 1 // ambiguous base (N) contributed |
| 90 | const NX_PILEUP_FLAG_DEL: i64 = 2 // position in deletion gap, no base |
| 91 | const NX_PILEUP_FLAG_OUT: i64 = 3 // target_r outside alignment region |
| 193 | const NX_PILEUP_COUNT_N: i64 = 4 |
| 194 | const NX_PILEUP_COUNT_DEL: i64 = 5 |
| 195 | const NX_PILEUP_COUNT_LEN: i64 = 6 |
functions
| 95 | func cigar_query_pos_at_ref(cigar_codes: *u8, cigar_lens: *i64, n_groups: i64, |
| 154 | func pileup_contribute_one_read( |
| 217 | func pileup_insertion_after_ref(cigar_codes: *u8, cigar_lens: *i64, n_groups: i64, |
| 285 | func ins_event_to_hash(read_bases: *u8, q_pos: i64, ins_len: i64) -> i64 |
| 304 | func ins_event_decode_hash(hash: i64, out_packed: *i64) -> i64 called by 1: main |
| 328 | func pileup_deletion_after_ref(cigar_codes: *u8, cigar_lens: *i64, n_groups: i64, |
| 383 | func indel_count_insertions_by_length(ins_lens: *i64, n_reads: i64, called by 1: main |
| 407 | func pileup_aggregate_counts(base_codes: *i64, flags: *i64, n_reads: i64, called by 1: main |