ligase 0.3.0

Type-safe, reproducible computational biology pipelines (core engine)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
//! Reading a vendored resistance algorithm.
//!
//! The file read here is a `.ruleset`, derived from Stanford's ASI2 XML by
//! `scripts/vendor_hivdb_algorithm.py` and committed beside it. Both are in
//! `crates/ligase-core/rulesets/`, and the test suite re-derives one from
//! the other on every run, so this reader can be strict about a small
//! format instead of lenient about a large one.
//!
//! Strict means every line is understood or the file is refused. That is
//! [D21](https://ligase.neoforgelabs.tech/project/decisions/)'s rule applied
//! to a ruleset: a reader that skipped what it did not recognise would
//! silently drop a drug, and a dropped drug reads as "susceptible" — a false
//! negative in the direction that matters.

use std::collections::BTreeMap;
use std::fmt;
use std::path::Path;

/// The format version this reader understands, as written on a
/// `.ruleset`'s first line.
const FORMAT: u32 = 1;

/// Every algorithm shipped inside the engine, newest first.
///
/// Embedded rather than read from disk, so an installed wheel carries its
/// own rules and a run cannot depend on where a file was unpacked. The
/// XML each was derived from is in `crates/ligase-core/rulesets/`, pinned
/// by SHA-256; these are the derived form, and the test suite re-derives
/// them from the XML on every run.
pub const VENDORED: &[(&str, &str)] = &[
    (
        "HIVDB_10.2",
        include_str!("../../rulesets/HIVDB_10.2.ruleset"),
    ),
    (
        "HIVDB_9.8",
        include_str!("../../rulesets/HIVDB_9.8.ruleset"),
    ),
];

/// The algorithm used when a pipeline does not name one.
///
/// 9.8 rather than the newer 10.2, because it is the version the
/// published results in `tests/test_validation.py` were produced with,
/// and a default that disagreed with the suite's own reference answers
/// would make every validation run an argument about algorithm drift.
/// A pipeline that wants today's science names `HIVDB_10.2`.
pub const DEFAULT: &str = "HIVDB_9.8";

/// What every vendored algorithm is, by name, version, date and content.
///
/// This is what a `hivdb` step records as its tool version, so it is what
/// puts the rules into the cache key. It names what the engine *ships*,
/// not what a step chose: a methods paragraph renders a tool version
/// after "ran with", and a step that named 9.8 must not appear to have
/// run with 10.2. Which algorithm a step used is its `algorithm`
/// parameter, reported beside this.
///
/// The digest is of the bytes, not of the name: upstream publishes
/// corrections under near-identical names —
/// `HIVDB_10.0.xml` and `HIVDB_10.0-1.xml` both exist — and a key that
/// moved only with the name would serve a result computed under rules
/// that have since changed.
///
/// Every vendored algorithm is named, not only the one in use, because the
/// probe runs before any step body has said which it wants. That is
/// over-conservative: editing 10.2 reruns a pipeline pinned to 9.8. It is the
/// direction [D20](https://ligase.neoforgelabs.tech/project/decisions/)
/// already commits to — a cache that is too cold costs time, one that is too
/// warm is wrong.
///
/// # Errors
///
/// If any embedded ruleset does not parse, which would mean the engine
/// was built from a broken derivation.
pub fn fingerprint() -> Result<String, String> {
    let mut parts = Vec::with_capacity(VENDORED.len());
    for (name, text) in VENDORED.iter().rev() {
        let ruleset = Ruleset::parse(text).map_err(|error| format!("{name}: {error}"))?;
        parts.push(format!(
            "{} {} blake3:{}",
            ruleset.version,
            ruleset.date,
            blake3::hash(text.as_bytes()).to_hex()
        ));
    }
    // "as shipped" rather than a bare list of versions, because a methods
    // paragraph renders this after "ran with" -- and "ran with HIVdb 10.2"
    // would be a false statement about a step that named 9.8. Which one a
    // step used is its `algorithm` parameter, reported beside this.
    Ok(format!("HIVdb rules as shipped: {}", parts.join("; ")))
}

/// The amino acids a rule may name at one position.
///
/// A bitset rather than a sorted string: matching a rule against an
/// observation is an intersection test, done once per rule position per
/// drug per sample, and the whole alphabet fits in a `u32`.
///
/// Bits 0–25 are `A`–`Z`. Bit 26 is a deletion and bit 27 an insertion,
/// which ASI writes as lowercase `d` and `i`. Bit 28 is a stop codon,
/// which Stanford writes `*`.
///
/// A stop codon can be observed but is never named by a rule, so it can
/// never satisfy one. That is the right answer rather than a gap: a stop
/// in the middle of protease is a defective virus, not a resistant one.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord)]
pub struct Acids(u32);

/// Where the deletion bit sits.
const DELETION: u32 = 26;

/// Where the insertion bit sits.
const INSERTION: u32 = 27;

/// Where the stop-codon bit sits.
const STOP: u32 = 28;

impl Acids {
    /// An empty set.
    #[must_use]
    pub const fn none() -> Self {
        Self(0)
    }

    /// The set holding one amino acid, a deletion (`d`), an insertion
    /// (`i`) or a stop codon (`*`).
    ///
    /// # Errors
    ///
    /// If the character is not an uppercase letter, `d` or `i`.
    pub fn of(acid: char) -> Result<Self, String> {
        match acid {
            'A'..='Z' => Ok(Self(1 << (acid as u32 - 'A' as u32))),
            'd' => Ok(Self(1 << DELETION)),
            'i' => Ok(Self(1 << INSERTION)),
            '*' => Ok(Self(1 << STOP)),
            other => Err(format!("not an amino acid: {other:?}")),
        }
    }

    /// Every amino acid in a string such as `KE`, `215FY` or `69i`.
    ///
    /// # Errors
    ///
    /// If any character is not one this set can hold.
    pub fn parse(acids: &str) -> Result<Self, String> {
        let mut set = Self::none();
        for acid in acids.chars() {
            set = set.with(Self::of(acid)?);
        }
        if set == Self::none() {
            return Err("no amino acids".to_owned());
        }
        Ok(set)
    }

    /// This set together with another.
    #[must_use]
    pub const fn with(self, other: Self) -> Self {
        Self(self.0 | other.0)
    }

    /// Whether the two sets share an amino acid.
    ///
    /// This is what "the rule matches" means. A mixture is a set with more
    /// than one member, and a rule is satisfied when *any* of the amino
    /// acids present is one it names — which is how Stanford treats an
    /// ambiguous position, and the only safe direction: taking the
    /// majority would drop minority resistance, which is the reason
    /// anyone sequences deeply.
    #[must_use]
    pub const fn meets(self, other: Self) -> bool {
        self.0 & other.0 != 0
    }

    /// Whether the set holds nothing.
    #[must_use]
    pub const fn is_empty(self) -> bool {
        self.0 == 0
    }
}

impl fmt::Display for Acids {
    /// The amino acids in alphabetical order, deletions and insertions
    /// last, as ASI writes them.
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        for bit in 0..26 {
            if self.0 & (1 << bit) != 0 {
                write!(formatter, "{}", char::from(b'A' + bit))?;
            }
        }
        if self.0 & (1 << DELETION) != 0 {
            write!(formatter, "d")?;
        }
        if self.0 & (1 << INSERTION) != 0 {
            write!(formatter, "i")?;
        }
        if self.0 & (1 << STOP) != 0 {
            write!(formatter, "*")?;
        }
        Ok(())
    }
}

/// One position of a protein, and the amino acids that satisfy a rule
/// there.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Residue {
    /// The position in the protein, counting from one.
    pub position: u32,
    /// The amino acids the rule names.
    pub acids: Acids,
}

/// One alternative within a scoring group.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Item {
    /// Every residue that must be present. More than one is the rule
    /// language's `AND`.
    pub residues: Vec<Residue>,
    /// What this contributes when it matches. Negative scores are real —
    /// `184V => -10` for zidovudine — and are never clamped.
    pub score: i32,
}

/// One drug, and how its score is reached.
#[derive(Clone, Debug)]
pub struct Drug {
    /// The abbreviation the algorithm uses, e.g. `3TC` or `ATV/r`.
    pub abbreviation: String,
    /// The full name, e.g. `lamivudine`.
    pub name: String,
    /// The drug class, e.g. `NRTI`.
    pub class: String,
    /// The gene the class is scored against, e.g. `RT`.
    pub gene: String,
    /// The scoring groups. The drug's score is the sum over groups of the
    /// largest matching item in each, which is what `SCORE FROM` with
    /// `MAX` means. A group with nothing matching contributes nothing.
    pub groups: Vec<Vec<Item>>,
}

/// One rung of the resistance scale.
#[derive(Clone, Debug)]
pub struct Level {
    /// Its order, 1 (susceptible) to 5 (high-level resistance).
    pub order: u8,
    /// Susceptible, Intermediate or Resistant — the coarse three-way
    /// reading of the five levels.
    pub sir: String,
    /// The wording the algorithm gives, e.g. `High-Level Resistance`.
    pub name: String,
}

/// A vendored resistance algorithm, ready to interpret against.
#[derive(Clone, Debug)]
pub struct Ruleset {
    /// The algorithm's name, e.g. `HIVDB`.
    pub algorithm: String,
    /// Its version, e.g. `9.8`.
    pub version: String,
    /// The date the algorithm carries, e.g. `2025-01-05`.
    pub date: String,
    /// Every drug, in the order the algorithm declares them.
    pub drugs: Vec<Drug>,
    levels: Vec<Level>,
    default_level: u8,
    ranges: Vec<(Option<i32>, Option<i32>, u8)>,
}

impl Ruleset {
    /// How a methods section or a cache key names this algorithm.
    ///
    /// `HIVDB 9.8 (2025-01-05)` — the name, the version and the date the
    /// algorithm itself carries, so that two rulesets claiming one version
    /// but holding different rules cannot be confused for each other by
    /// anyone reading a report.
    #[must_use]
    pub fn describe(&self) -> String {
        format!("{} {} ({})", self.algorithm, self.version, self.date)
    }

    /// Every gene any drug is scored against, in declaration order.
    #[must_use]
    pub fn genes(&self) -> Vec<&str> {
        let mut genes: Vec<&str> = Vec::new();
        for drug in &self.drugs {
            if !genes.contains(&drug.gene.as_str()) {
                genes.push(&drug.gene);
            }
        }
        genes
    }

    /// The level a score falls in.
    ///
    /// # Errors
    ///
    /// If no range covers the score. A ruleset whose ranges leave a gap is
    /// refused when it is read, so this cannot happen for a ruleset that
    /// loaded; it is an error rather than a default because answering
    /// "susceptible" for a score nothing described would be a guess in the
    /// unsafe direction.
    pub fn level(&self, score: i32) -> Result<&Level, String> {
        let order = self
            .ranges
            .iter()
            .find(|(low, high, _)| {
                low.is_none_or(|bound| score >= bound) && high.is_none_or(|b| score <= b)
            })
            .map_or(self.default_level, |(_, _, order)| *order);
        self.levels
            .iter()
            .find(|level| level.order == order)
            .ok_or_else(|| format!("no level {order}"))
    }

    /// One of the algorithms shipped inside the engine.
    ///
    /// # Errors
    ///
    /// If no vendored algorithm has that name, or it does not parse. The
    /// error names every algorithm there is, because a typo in a
    /// pipeline's `algorithm=` is the likely cause and a list is the
    /// answer to it.
    pub fn vendored(name: &str) -> Result<Self, String> {
        let (_, text) = VENDORED
            .iter()
            .find(|(vendored, _)| *vendored == name)
            .ok_or_else(|| {
                format!(
                    "no vendored algorithm called {name:?}; this engine ships {}",
                    VENDORED
                        .iter()
                        .map(|(vendored, _)| *vendored)
                        .collect::<Vec<_>>()
                        .join(" and ")
                )
            })?;
        Self::parse(text).map_err(|error| format!("{name}: {error}"))
    }

    /// Read a `.ruleset` from disk.
    ///
    /// # Errors
    ///
    /// If the file cannot be read, is not a `.ruleset` of a format version
    /// this engine understands, or holds a line this reader does not
    /// recognise.
    pub fn read(path: &Path) -> Result<Self, String> {
        let text = std::fs::read_to_string(path)
            .map_err(|error| format!("{}: {error}", path.display()))?;
        Self::parse(&text).map_err(|error| format!("{}: {error}", path.display()))
    }

    /// Read a `.ruleset` from text.
    ///
    /// # Errors
    ///
    /// If any line is not one this reader recognises, or a declaration a
    /// later line depends on is missing.
    #[allow(clippy::too_many_lines)]
    pub fn parse(text: &str) -> Result<Self, String> {
        let mut lines = text.lines();
        let first = lines.next().unwrap_or_default();
        let format = first
            .strip_prefix("#ligase-hivdb-ruleset ")
            .ok_or("not a ruleset: no `#ligase-hivdb-ruleset` on the first line")?;
        if format.trim() != FORMAT.to_string() {
            return Err(format!(
                "ruleset format {}, but this engine reads {FORMAT}",
                format.trim()
            ));
        }

        let mut algorithm = None;
        let mut levels = Vec::new();
        let mut default_level = None;
        let mut ranges = Vec::new();
        let mut gene_of_class: BTreeMap<String, String> = BTreeMap::new();
        let mut class_of_drug: BTreeMap<String, String> = BTreeMap::new();
        let mut drugs: Vec<Drug> = Vec::new();

        for (number, line) in lines.enumerate() {
            if line.starts_with('#') || line.trim().is_empty() {
                continue;
            }
            let at = |what: &str| format!("line {}: {what}", number + 2);
            let fields: Vec<&str> = line.split('\t').collect();
            let number_at = |index: usize, what: &str| -> Result<i32, String> {
                fields
                    .get(index)
                    .ok_or_else(|| at(&format!("no {what}")))?
                    .parse::<i32>()
                    .map_err(|_| at(&format!("{what} is not a number")))
            };
            let field = |index: usize, what: &str| -> Result<String, String> {
                fields
                    .get(index)
                    .filter(|value| !value.is_empty())
                    .map(|value| (*value).to_owned())
                    .ok_or_else(|| at(&format!("no {what}")))
            };

            match *fields.first().unwrap_or(&"") {
                "algorithm" => {
                    algorithm = Some((
                        field(1, "algorithm name")?,
                        field(2, "version")?,
                        field(3, "date")?,
                    ));
                }
                "level" => levels.push(Level {
                    order: u8::try_from(number_at(1, "level order")?)
                        .map_err(|_| at("level order is out of range"))?,
                    sir: field(2, "SIR")?,
                    name: field(3, "level name")?,
                }),
                "default" => {
                    default_level = Some(
                        u8::try_from(number_at(1, "default level")?)
                            .map_err(|_| at("default level is out of range"))?,
                    );
                }
                "range" => {
                    let bound = |index: usize, what: &str| -> Result<Option<i32>, String> {
                        match field(index, what)?.as_str() {
                            "*" => Ok(None),
                            value => value
                                .parse::<i32>()
                                .map(Some)
                                .map_err(|_| at(&format!("{what} is not a number"))),
                        }
                    };
                    ranges.push((
                        bound(1, "range lower bound")?,
                        bound(2, "range upper bound")?,
                        u8::try_from(number_at(3, "range level")?)
                            .map_err(|_| at("range level is out of range"))?,
                    ));
                }
                "gene" => {
                    let gene = field(1, "gene name")?;
                    for class in field(2, "drug classes")?.split(',') {
                        gene_of_class.insert(class.to_owned(), gene.clone());
                    }
                }
                "class" => {
                    let class = field(1, "class name")?;
                    for drug in field(2, "drugs")?.split(',') {
                        class_of_drug.insert(drug.to_owned(), class.clone());
                    }
                }
                "drug" => {
                    let abbreviation = field(1, "drug abbreviation")?;
                    let class = field(3, "drug class")?;
                    let gene = gene_of_class
                        .get(&class)
                        .ok_or_else(|| at(&format!("no gene declares the class {class}")))?
                        .clone();
                    drugs.push(Drug {
                        abbreviation,
                        name: field(2, "drug name")?,
                        class,
                        gene,
                        groups: Vec::new(),
                    });
                }
                "rule" => {
                    let abbreviation = field(1, "drug abbreviation")?;
                    let group = usize::try_from(number_at(2, "group")?)
                        .map_err(|_| at("group is out of range"))?;
                    let score = number_at(3, "score")?;
                    let residues = field(4, "residues")?
                        .split('+')
                        .map(parse_residue)
                        .collect::<Result<Vec<_>, _>>()
                        .map_err(|error| at(&error))?;

                    let drug = drugs
                        .iter_mut()
                        .find(|drug| drug.abbreviation == abbreviation)
                        .ok_or_else(|| at(&format!("rule for undeclared drug {abbreviation}")))?;
                    if group > drug.groups.len() {
                        return Err(at(&format!(
                            "group {group} follows group {}, leaving a gap",
                            drug.groups.len().saturating_sub(1)
                        )));
                    }
                    if group == drug.groups.len() {
                        drug.groups.push(Vec::new());
                    }
                    drug.groups[group].push(Item { residues, score });
                }
                other => return Err(at(&format!("unknown record {other:?}"))),
            }
        }

        let (algorithm, version, date) = algorithm.ok_or("no `algorithm` record")?;
        if levels.is_empty() {
            return Err("no `level` records".to_owned());
        }
        if ranges.is_empty() {
            return Err("no `range` records".to_owned());
        }
        if drugs.is_empty() {
            return Err("no `drug` records".to_owned());
        }
        let default_level = default_level.ok_or("no `default` record")?;
        for (_, _, order) in &ranges {
            if !levels.iter().any(|level| level.order == *order) {
                return Err(format!("a range names level {order}, which is not defined"));
            }
        }
        if !levels.iter().any(|level| level.order == default_level) {
            return Err(format!("the default level {default_level} is not defined"));
        }

        Ok(Self {
            algorithm,
            version,
            date,
            drugs,
            levels,
            default_level,
            ranges,
        })
    }
}

/// Read one residue, e.g. `215FY`, `67d` or `69i`.
fn parse_residue(residue: &str) -> Result<Residue, String> {
    let split = residue
        .find(|character: char| !character.is_ascii_digit())
        .ok_or_else(|| format!("residue {residue:?} names no amino acid"))?;
    if split == 0 {
        return Err(format!("residue {residue:?} has no position"));
    }
    let (position, acids) = residue.split_at(split);
    Ok(Residue {
        position: position
            .parse()
            .map_err(|_| format!("residue {residue:?} has no position"))?,
        acids: Acids::parse(acids).map_err(|error| format!("residue {residue:?}: {error}"))?,
    })
}