Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
9c1fea1
search: key the fragment-intensity cache by peptidoform, cache intens…
ypriverol Nov 25, 2026
346adb8
search: recalibrate the merged charge queues of a chunk in parallel
ypriverol Nov 25, 2026
0c9423c
dia: sort the re-rank order on the thread pool
ypriverol Nov 25, 2026
8acfa67
dia: pick the pass-2 nominee rows with one parallel sort
ypriverol Nov 25, 2026
6b28b4e
dia: FxHash maps in the co-elution and re-rank passes, parallel trans…
ypriverol Nov 25, 2026
7fdace7
dia: train the RT refit folds together and predict the rows in parallel
ypriverol Nov 25, 2026
ca5aacf
dia: FxHash map for the best pass-1 row of each precursor
ypriverol Nov 25, 2026
833360b
search: merge the charge-search units of a chunk in parallel
ypriverol Nov 25, 2026
a3bd756
dia: parallel peptidoform lookup when reading the PIN, FxHash maps
ypriverol Nov 25, 2026
1a26795
search: copy the kept PSMs' peptides out of the candidate store in pa…
ypriverol Nov 25, 2026
11c15db
output: classify the rescored PSMs on the thread pool
ypriverol Nov 25, 2026
aa51f69
dia: build the pass-1 row keys while parsing, FxHash nominee map
ypriverol Nov 25, 2026
43c40d9
dia: check and split each PIN block's lines in parallel
ypriverol Nov 25, 2026
50f2dde
rescore: compute the line search's row terms on the thread pool
ypriverol Nov 25, 2026
7f3afbc
scoring: pad GBDT batches to 8 rows, not 32
ypriverol Nov 25, 2026
59f3259
model-train: split histogram features over enough tasks for every thread
ypriverol Nov 25, 2026
f9a192c
output: build the protein-inference peptide rows in parallel
ypriverol Nov 25, 2026
b0a59a2
dia: start the precursor learned score as soon as pass 2 has its table
ypriverol Nov 25, 2026
fceed46
rescore: name the line-search breakpoint and row-term types
ypriverol Nov 25, 2026
ca8d6df
dia: precursor-level q-values in dia/qvalue.rs, keeping dia.rs under …
ypriverol Nov 25, 2026
9b0d056
dia: classwise_q_values re-exported for the tests only
ypriverol Nov 25, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
399 changes: 51 additions & 348 deletions crates/andes/src/bin/andes/dia.rs

Large diffs are not rendered by default.

67 changes: 60 additions & 7 deletions crates/andes/src/bin/andes/dia/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use std::io::Read;
use std::path::Path;
use std::sync::mpsc::sync_channel;

use rayon::prelude::*;

/// Bytes read per block.
const BLOCK_BYTES: usize = 64 << 20;
/// Lines per parallel work unit of a block.
Expand All @@ -23,6 +25,40 @@ fn push_lines<'a>(text: &'a str, out: &mut Vec<&'a str>) {
}
}

/// Pieces a block's lines are split into on the thread pool.
const SPLIT_PIECES: usize = 64;

/// The lines of `bytes` as [`push_lines`] gives them, `None` when `bytes` is not UTF-8. The
/// bytes are cut after newlines into pieces that are checked and split in parallel; each piece
/// but the last ends with a newline, so the pieces' lines, in order, are the lines of the whole.
fn block_lines(bytes: &[u8]) -> Option<Vec<&str>> {
let n = bytes.len();
let mut cuts = vec![0usize];
for k in 1..SPLIT_PIECES {
let from = (k * n / SPLIT_PIECES).max(*cuts.last().unwrap_or(&0));
match bytes[from..].iter().position(|&b| b == b'\n') {
Some(p) => cuts.push(from + p + 1),
None => break,
}
}
cuts.push(n);
cuts.dedup();
let pieces: Vec<Option<Vec<&str>>> = cuts
.par_windows(2)
.map(|w| {
let text = std::str::from_utf8(&bytes[w[0]..w[1]]).ok()?;
let mut lines = Vec::new();
push_lines(text, &mut lines);
Some(lines)
})
.collect();
let mut lines = Vec::with_capacity(pieces.iter().flatten().map(Vec::len).sum());
for piece in pieces {
lines.extend(piece?);
}
Some(lines)
}

/// Reads `path`: `on_header` gets its first line, then `on_block` gets the following non-empty
/// lines block by block, in file order, with the index of the block's first line among them.
/// Lines end as [`std::io::BufRead::lines`] ends them; a file that is not UTF-8 is an error.
Expand Down Expand Up @@ -74,18 +110,18 @@ pub(crate) fn read_blocks(
});
let mut header: Option<_> = Some(on_header);
let mut first = 0usize;
let not_utf8 = || err(&"stream did not contain valid UTF-8");
for block in rx {
let block = block.map_err(|e| err(&e))?;
let text = std::str::from_utf8(&block)
.map_err(|_| err(&"stream did not contain valid UTF-8"))?;
let mut body = text;
let mut body: &[u8] = &block;
if let Some(h) = header.take() {
let (line, rest) = text.split_once('\n').unwrap_or((text, ""));
let end = body.iter().position(|&b| b == b'\n');
let line = std::str::from_utf8(&body[..end.unwrap_or(body.len())])
.map_err(|_| not_utf8())?;
body = end.map_or(&[][..], |e| &body[e + 1..]);
h(line.strip_suffix('\r').unwrap_or(line))?;
body = rest;
}
let mut lines: Vec<&str> = Vec::new();
push_lines(body, &mut lines);
let lines = block_lines(body).ok_or_else(not_utf8)?;
on_block(first, &lines)?;
first += lines.len();
}
Expand Down Expand Up @@ -145,6 +181,23 @@ mod tests {
assert_eq!(rows, body);
}

#[test]
fn parallel_split_gives_the_lines_of_the_whole_block() {
let text: String = (0..5000)
.map(|i| match i % 7 {
0 => "\n".to_string(),
1 => format!("r{i}\tx\r\n"),
_ => format!("row {i}\t{}\n", "y".repeat(i % 13)),
})
.chain(["tail without newline".to_string()])
.collect();
let mut want = Vec::new();
push_lines(&text, &mut want);
assert_eq!(block_lines(text.as_bytes()), Some(want));
assert_eq!(block_lines(b""), Some(Vec::new()));
assert_eq!(block_lines(b"a\n\xff\nb\n"), None);
}

#[test]
fn an_empty_file_is_an_error() {
let dir = tempfile::tempdir().unwrap();
Expand Down
68 changes: 54 additions & 14 deletions crates/andes/src/bin/andes/dia/pass2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,59 @@ impl Pass2 {
}
}

/// The row of the highest pool LLR per (peptidoform, charge, run) among the rows above
/// `NOMINATE_MIN_LLR`, the first such row on ties, as sorted row indices. Without a NaN LLR the
/// rows are ranked by one parallel sort; with one, they are walked in order (a NaN never
/// replaces the best row and is replaced by none).
fn best_rows(
table: &PinTable,
llr: &[f64],
run_of: &(impl Fn(usize) -> usize + Sync),
) -> Vec<usize> {
let eligible = |i: usize| {
let r = &table.rows[i];
!(r.spec == NONE || r.form == NONE || r.charge == 0 || llr[i] <= NOMINATE_MIN_LLR)
};
let key = |i: usize| {
let r = &table.rows[i];
(r.form, r.charge, run_of(r.spec as usize))
};
let mut want: Vec<usize> = if llr.par_iter().any(|v| v.is_nan()) {
let mut best: HashMap<(u32, u8, usize), usize> = HashMap::new();
for i in (0..table.rows.len()).filter(|&i| eligible(i)) {
let e = best.entry(key(i)).or_insert(i);
if llr[i] > llr[*e] {
*e = i;
}
}
best.into_values().collect()
} else {
let mut rows: Vec<(u32, u8, usize, usize)> = (0..table.rows.len())
.into_par_iter()
.filter(|&i| eligible(i))
.map(|i| {
let (form, charge, run) = key(i);
(form, charge, run, i)
})
.collect();
rows.par_sort_unstable_by(|a, b| {
(a.0, a.1, a.2)
.cmp(&(b.0, b.1, b.2))
.then(
llr[b.3]
.partial_cmp(&llr[a.3])
.unwrap_or(std::cmp::Ordering::Equal),
)
.then(a.3.cmp(&b.3))
});
rows.chunk_by(|a, b| (a.0, a.1, a.2) == (b.0, b.1, b.2))
.map(|g| g[0].3)
.collect()
};
want.par_sort_unstable();
want
}

/// The best pool row of every (run, peptidoform, charge) with `llr` above the nomination
/// threshold. `llr` is the pool `dia_llr` of each PIN row of `table`.
pub(crate) fn nominate(
Expand All @@ -204,20 +257,7 @@ pub(crate) fn nominate(
table.forms.len()
));
}
let mut best: HashMap<(u32, u8, usize), usize> = HashMap::new();
for (i, r) in table.rows.iter().enumerate() {
if r.spec == NONE || r.form == NONE || r.charge == 0 || llr[i] <= NOMINATE_MIN_LLR {
continue;
}
let e = best
.entry((r.form, r.charge, run_of(r.spec as usize)))
.or_insert(i);
if llr[i] > llr[*e] {
*e = i;
}
}
let mut want: Vec<usize> = best.into_values().collect();
want.sort_unstable();
let want = best_rows(table, llr, &run_of);
predicted.keep_only(want.iter().map(|&i| (table.rows[i].form, table.rows[i].charge)));
crate::memory::release_free_memory();
let predicted = &*predicted;
Expand Down
36 changes: 22 additions & 14 deletions crates/andes/src/bin/andes/dia/pin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
//! label and score. Lines are parsed in parallel blocks; peptidoforms are numbered in the order
//! they first appear.

use std::collections::HashMap;
use std::path::Path;

use model::Spectrum;
use onnx_infer::predictors::form::{parse_peptide, Form, Mass};
use rayon::prelude::*;
use rustc_hash::FxHashMap;

use crate::dia::rt::{locate, row_charge, spectrum_bases, PinRow, PinTable, NONE};

Expand All @@ -19,8 +19,8 @@ pub(crate) fn read_pin(
let layout: std::cell::OnceCell<PinLayout> = std::cell::OnceCell::new();
let bases = spectrum_bases(spectra);
let mut forms: Vec<(Form, Mass)> = Vec::new();
let mut form_ix: HashMap<Form, u32> = HashMap::new();
let mut by_peptide: HashMap<String, u32> = HashMap::new();
let mut form_ix: FxHashMap<Form, u32> = FxHashMap::default();
let mut by_peptide: FxHashMap<String, u32> = FxHashMap::default();
let mut rows = Vec::new();
let mut unlocated = 0usize;
crate::dia::blocks::read_blocks(
Expand All @@ -42,10 +42,14 @@ pub(crate) fn read_pin(
for r in parsed {
block.push(r?);
}
let known: Vec<bool> = block
.par_iter()
.map(|r| by_peptide.contains_key(r.peptide))
.collect();
let mut fresh: Vec<&str> = Vec::new();
let mut seen: std::collections::HashSet<&str> = std::collections::HashSet::new();
for r in &block {
if !by_peptide.contains_key(r.peptide) && seen.insert(r.peptide) {
let mut seen: rustc_hash::FxHashSet<&str> = rustc_hash::FxHashSet::default();
for (r, _) in block.iter().zip(&known).filter(|(_, &k)| !k) {
if seen.insert(r.peptide) {
fresh.push(r.peptide);
}
}
Expand All @@ -60,13 +64,17 @@ pub(crate) fn read_pin(
});
by_peptide.insert((*p).to_string(), f);
}
rows.reserve(block.len());
for r in block {
unlocated += usize::from(r.row.spec == NONE);
let mut row = r.row;
row.form = by_peptide.get(r.peptide).copied().unwrap_or(NONE);
rows.push(row);
}
unlocated += block.iter().filter(|r| r.row.spec == NONE).count();
let by_peptide = &by_peptide;
let located: Vec<PinRow> = block
.into_par_iter()
.map(|r| {
let mut row = r.row;
row.form = by_peptide.get(r.peptide).copied().unwrap_or(NONE);
row
})
.collect();
rows.extend(located);
Ok(())
},
)?;
Expand Down Expand Up @@ -161,7 +169,7 @@ impl PinLayout {
pin: &Path,
row: usize,
line: &'a str,
bases: &HashMap<String, u32>,
bases: &FxHashMap<String, u32>,
spectra: &[Spectrum],
) -> Result<ParsedRow<'a>, String> {
let x: Vec<&str> = line.split('\t').collect();
Expand Down
Loading
Loading