Design
Scope
A reader for the mitoLEAF™ human mitochondrial phylogeny, the "phm" XML of nested <Node> elements. parse builds a haplogroup Tree; path, motif, children and node walk it; haplotype accumulates a haplogroup's motifs from the root with back mutations applied; distance compares a haplogroup with an observed haplotype; and assign reports the nearest haplogroups. It is pure computation on a string the caller has read: no capability, no file or network access.
Not in scope:
- A classifier.
assignis a nearest-neighbour report by symmetric difference: no range awareness, no weighting by how informative a position is, none of EMPOP™'s or HaploGrep™'s phylogenetic reasoning. Limitations spells this out. - Phylogenetic alignment of indels, which neither this deck nor its dependency does.
- Bundled tree data. mitoLEAF's data is MPL-2.0 and not redistributed;
testdata/tree.xmlis a seven-haplogroup synthetic fixture. - Forensic validation. The deck has not been through the validation any jurisdiction requires for casework.
Decisions
- One nomenclature implementation. Haplotypes are
lineage.MtHaplotypevalues from@deckyard/forensicgenetics, and gains are parsed with itslineage.parseDifference, so rCRS notation has one implementation. Onlylineage.jis imported. - Back mutations parsed here.
lineage.parseDifferencerejects both back-mutation spellings, soparseMotifItemclassifies a token itself: a leading-(-5899.1) is the loss of an inserted base; a base and digits only (A263) is a reverting substitution; anything else is a gain (263G,A263G,315.1C,8281-, IUPAC codes).8281-is a deletion gained,-8281.1an insertion lost, andA263Ga redundantly spelled gain, not a reversion. Positions are 1 to 16569, insertion indexes 1 to 9999. - Reverting an insertion removes the highest index present at that position; a reversion with nothing to revert does nothing.
- Lenient structure, strict identity.
Id/HGattributes and the tag name are read case-insensitively; comments and wrapper elements are skipped. The first<Node>is the root. A duplicate haplogroup or a second top-level node (whichassigncould never reach) is an error. - Motifs compiled once, errors deferred.
parsecompiles every node's motif intoTree.ops, sohaplotype,distanceandassignnever parse a token again. A malformed token does not failparse; it throws its original error when a call folds that node, so one bad motif deep in a release does not make the whole tree unreadable. Tree.opsholds plain strings. One space-joined string of operations per haplogroup, rather than a list or a struct of the deck's own, because the interpreter checks values crossing the module boundary and nested lists made every call measurably slower.fromNodesis the supported constructor and derivesops; a hand-builtTreewithout them falls back to compiling motifs on the fly.distanceagrees withlineage.mtDistance. It compares states keyed by position directly instead of building a haplotype, and refuses an observation with two designations at one position, the rule of forensicgenetics'mtHaplotype. Two tests compare it withmtDistanceand withassignover every haplogroup, since forensicgenetics does not know about this deck and nothing upstream guards the contract.assignin one pass. A preorder walk with an explicit stack applies each node's changes to one shared state, keeps a running distance, and rolls the changes back on the way out. Ties go to the node reached first, so ancestors win.- Explicit stacks, never recursion over the
Tree. Passing the tree into a recursive helper copied it per call:parsetook 2 min 14 s andassign56 s on the 6409-node tree before both moved to explicit stacks. - Ranges are not compared. Phylogeny haplotypes span the whole genome (
WHOLE_GENOME); observations usually cover the control region. Checking that is the caller's, withlineage.mtRangesAgree.
Errors
Failures of the deck are Error{kind: "mitoleaf"}: malformed XML, no <Node>, a duplicate or second top-level haplogroup, an unknown haplogroup, a malformed back mutation, two designations at one position in an observation. Kind "lineage" passes through unchanged from the dependency for an invalid gain, and kind "limit" from xml.decode for a document past its nesting or node caps. assign on a tree without nodes fails with a runtime error.
Security and limits
- The input is a string the caller has read; the deck performs no I/O.
- XML is decoded by
xml.decode, under its nesting and node caps; a reached cap passes on as kind"limit", any other decode failure is reported as malformed XML (kind"mitoleaf"). - No deck-level size cap: the published tree is about 550 KB and 6409 haplogroups.
Testing and validation
- 43 tests on the synthetic seven-haplogroup tree, which exercises an insertion, an IUPAC ambiguity, a deletion and both back-mutation spellings. The tree is inlined in the tests so an installed deck runs them without
testdata/; a test keeps it byte-equal totestdata/tree.xml. - Agreement with
lineage.mtDistanceand betweenassignanddistanceis tested over every haplogroup. - Timings were taken on the published 6409-haplogroup tree with Jennifer 0.27.0:
parseabout 0.7 s,assignabout 0.4 s, and every call passing theTreeabout 5.5 ms. On 0.28.0parseandassignare about twice as fast and the per-call cost is gone (figures). - The tests need
vendor/populated (tools/vendor-deps.sh, network), which fetches the released forensicgenetics through the registry. - Not validated against EMPOP or HaploGrep assignments.
Known limitations and open questions
- On Jennifer 0.27.0 every call from an importing program that passes a
Treecosts about 5.5 ms on the published tree before doing any work, because the interpreter rebuilds values holding a module's own structs at the boundary. A loop callingdistanceper haplogroup takes about a minute;assignscores the whole tree in one call. On 0.28.0 (jennifer's M29.14) the cost is gone:hasandpathtake under 0.1 ms a call,distanceandhaplotypeabout 1.2 ms, and thedistanceloop over every haplogroup 10 s instead of 84 s on a tree of the same size. - Code reachable with the
Treein its call chain must not use a callback builtin such aslists.sortBy: it made everyhaplotypeanddistancecall copy the whole tree. The deck sorts plain keys instead (jennifer's M30.8 would lift this). Tree.opsis derived from the motifs and not checked against them; editingNode.motifwithoutfromNodesleaves results on the old motif.- The deck does not record which mitoLEAF release a tree came from; the phm file's leading comment states it.
Dependencies on the interpreter
- Floor
>=0.27.0(strings.replicatearrived in 0.27.0), the default binary (nojennifer-tinyengine declared). xml.decode;@deckyard/forensicgenetics>=0.1.0through[decks].- Performance depends on
jennifer's M29.14 (cheaper calls into another module, in 0.28.0) and M30.8 (borrow through named callbacks).