nx_sequence.nx
buildroot/runtime/nx_sequence.nx
about
nx_sequence.nx -- foundational DNA / RNA sequence algebra primitive.
license_tier: INDEPENDENT_REDERIVE
genealogy_id: international-research-sources/biology/dna_2bit_encoding_field_standard
G0.1 of NISHI_GENOMICS_SUBSTRATE_ROADMAP.md. Encoding +
reverse-complement + k-mer enumeration + canonical k-mer. Built
bits-up against existing nx_syscalls; no other genomics dependency.
2-bit packed DNA encoding:
A = 00, C = 01, G = 10, T = 11
Complement = code XOR 0b11 (A<->T, C<->G).
4 bases per byte, packed big-endian within byte:
byte bits 7-6 = base position 0 within byte
byte bits 5-4 = base position 1
byte bits 3-2 = base position 2
byte bits 1-0 = base position 3
Bytes ordered first-to-last. A trailing partial byte zero-pads
the low bits (so "ACG" = 0x18 = 00 01 10 00 in one byte).
k-mer encoding (k <= 32):
Single i64 holding the 2*k low bits. Base position 0 of the k-mer
sits in the highest 2 bits of the 2*k-bit window. This means
lexicographic ordering of bases (A < C < G < T) matches arithmetic
ordering of the packed i64, so canonical k-mer = min(fwd, rc) is
a simple arithmetic min.
Why 2-bit:
- Minimum storage for an alphabet of 4 (human reference ~750 MB
uncompressed FASTA -> ~190 MB packed)
- Reverse-complement of a packed code is a single XOR
- SIMD-friendly: 16 bases per 32-bit word, 32 bases per i64
- k-mer hashing fits naturally
N / ambiguity handling:
Out of scope for G0.1. See nx_sequence_ambig.nx (G0.5) for the
IUPAC sidecar bitset. In G0.1, callers must filter Ns upstream
or the encoder returns -1 (refused) on any non-ACGT input.
API:
dependencies 2 imports · 13 importers
diagram shows first 10 each side; +0 more imports, +3 more importers in the complete lists below.
imports: nx_syscalls.nxnx_const.nx
imported by: nx_align_match_stranded_test.nxnx_align_match_test.nxnx_align_minimizer.nxnx_align_minimizer_test.nxnx_align_score.nxnx_align_score_test.nxnx_pileup.nxnx_pileup_test.nxnx_sam.nxnx_sequence_ambig.nxnx_sequence_ambig_test.nxnx_sequence_kat_test.nxnx_vcf.nx
structs
| none |
consts
| none |
functions
| 95 | func dna_encode_base(c: i64) -> i64 |
| 110 | func dna_decode_base(code: i64) -> i64 |
| 120 | func dna_complement_code(code: i64) -> i64 |
| 129 | func dna_get_base(bases: *u8, pos: i64) -> i64 |
| 139 | func dna_set_base(bases: *u8, pos: i64, code: i64) -> i64 |
| 156 | func dna_pack(ascii: *u8, len: i64, out_bases: *u8) -> i64 |
| 170 | func dna_unpack(bases: *u8, len: i64, out: *u8) -> i64 |
| 186 | func dna_revcomp(in_bases: *u8, len: i64, out_bases: *u8) -> i64 |
| 203 | func dna_kmer_at(bases: *u8, pos: i64, k: i64) -> i64 |
| 219 | func dna_kmer_revcomp(kmer: i64, k: i64) -> i64 |
| 239 | func dna_kmer_canonical(kmer: i64, k: i64) -> i64 |