whittle 1.0.0

Coordinate-consistent trimming of long-read uBAM and tagged FASTQ
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
//! Reconstruction of BAM output records: decoding, the kept windows of a read,
//! and the rebuilt record of each window.

use super::*;

/// Converts a raw record to a `RecordBuf` on the render worker without routing
/// sequence, quality and every aux value through the generic SAM trait
/// iterators: bases are unpacked through a byte-pair table and aux arrays are
/// decoded in bulk (`decode_data`), which reduces conversion overhead on long
/// reads.
///
/// BAM's `CG:B:I` overflow representation of a CIGAR longer than 65535
/// operations is not expanded: the workflows accept unaligned records only,
/// whose CIGAR is empty.
pub(crate) fn decode_raw_record(src: &bam::Record) -> std::io::Result<RecordBuf> {
    let mut dst = RecordBuf::default();
    *dst.name_mut() = src.name().map(Into::into);
    *dst.flags_mut() = src.flags();
    *dst.reference_sequence_id_mut() = src.reference_sequence_id().transpose()?;
    *dst.alignment_start_mut() = src.alignment_start().transpose()?;
    *dst.mapping_quality_mut() = src.mapping_quality();

    let cigar = dst.cigar_mut().as_mut();
    cigar.clear();
    for result in src.cigar().iter() {
        cigar.push(result?);
    }

    *dst.mate_reference_sequence_id_mut() = src.mate_reference_sequence_id().transpose()?;
    *dst.mate_alignment_start_mut() = src.mate_alignment_start().transpose()?;
    *dst.template_length_mut() = src.template_length();
    let sequence = src.sequence();
    *dst.sequence_mut() = unpack_bases(sequence.as_bytes(), sequence.len()).into();
    *dst.quality_scores_mut() = src.quality_scores().into();
    *dst.data_mut() = decode_data(src.data().as_bytes())?;
    Ok(dst)
}

/// Unpacks the first `len` bases of a 4-bit packed sequence into their SAM
/// base letters (`=ACMGRSVTWYHKDBN`), one table lookup per byte.
fn unpack_bases(packed: &[u8], len: usize) -> Vec<u8> {
    const PAIRS: [[u8; 2]; 256] = {
        const BASES: &[u8; 16] = b"=ACMGRSVTWYHKDBN";
        let mut pairs = [[0; 2]; 256];
        let mut i = 0;
        while i < 256 {
            pairs[i] = [BASES[i >> 4], BASES[i & 0x0f]];
            i += 1;
        }
        pairs
    };
    let mut bases: Vec<u8> = packed.iter().flat_map(|&b| PAIRS[usize::from(b)]).collect();
    bases.truncate(len);
    bases
}

/// Decodes a raw aux block into owned values, as the noodles record decoder
/// does, with arrays converted in bulk. A duplicated tag or a malformed field
/// (`next_field`) is refused.
fn decode_data(aux: &[u8]) -> io::Result<noodles_sam::alignment::record_buf::Data> {
    fn values<const N: usize, T>(bytes: &[u8], f: impl Fn([u8; N]) -> T) -> Vec<T> {
        bytes.as_chunks::<N>().0.iter().map(|&c| f(c)).collect()
    }

    let mut data = noodles_sam::alignment::record_buf::Data::default();
    let mut pos = 0;
    while pos < aux.len() {
        let field = next_field(aux, pos)?;
        pos = field.bytes.end;
        let raw = &aux[field.bytes.start + 3..field.bytes.end];
        let value = match field.ty {
            b'A' => Value::Character(raw[0]),
            b'c' => Value::Int8(raw[0] as i8),
            b'C' => Value::UInt8(raw[0]),
            b's' => Value::Int16(i16::from_le_bytes([raw[0], raw[1]])),
            b'S' => Value::UInt16(u16::from_le_bytes([raw[0], raw[1]])),
            b'i' => Value::Int32(i32::from_le_bytes([raw[0], raw[1], raw[2], raw[3]])),
            b'I' => Value::UInt32(u32::from_le_bytes([raw[0], raw[1], raw[2], raw[3]])),
            b'f' => Value::Float(f32::from_le_bytes([raw[0], raw[1], raw[2], raw[3]])),
            b'Z' => Value::String(raw[..raw.len() - 1].into()),
            b'H' => Value::Hex(raw[..raw.len() - 1].into()),
            // `next_field` accepts only the array subtypes below; the elements
            // follow the subtype and the 4-byte count.
            _ => Value::Array(match raw[0] {
                b'c' => Array::Int8(raw[5..].iter().map(|&b| b as i8).collect()),
                b'C' => Array::UInt8(raw[5..].to_vec()),
                b's' => Array::Int16(values(&raw[5..], i16::from_le_bytes)),
                b'S' => Array::UInt16(values(&raw[5..], u16::from_le_bytes)),
                b'i' => Array::Int32(values(&raw[5..], i32::from_le_bytes)),
                b'I' => Array::UInt32(values(&raw[5..], u32::from_le_bytes)),
                _ => Array::Float(values(&raw[5..], f32::from_le_bytes)),
            }),
        };
        let tag = Tag::new(field.tag[0], field.tag[1]);
        if data.insert(tag, value).is_some() {
            return Err(io::Error::new(
                io::ErrorKind::InvalidData,
                format!("duplicate tag: {tag:?}"),
            ));
        }
    }
    Ok(data)
}

/// One output window of a read: bases `[start, end)`, segment `idx` (0-based)
/// of `total`.
#[derive(Debug, Clone, Copy)]
pub(crate) struct Window {
    /// First base of the window, inclusive.
    pub start: usize,
    /// End of the window, exclusive.
    pub end: usize,
    /// 0-based segment index.
    pub idx: usize,
    /// Number of segments produced from the read.
    pub total: usize,
}

/// Computes the edit that builds output record `window` of `src`: SEQ/QUAL
/// cut to the window, `MM`/`ML`/`MN` rebuilt, `sa` coverage runs re-encoded,
/// stale signal-space tags rewritten or dropped, and the name updated for
/// splits and PacBio interval crops. `build_record` applies it to the raw
/// record: aux tags are copied in source order with the rewritten ones
/// replaced in place, per-base arrays cut to the window, removed ones skipped
/// and added ones appended. A `Malformed` block is removed. An untrimmed,
/// unsplit record with an `Absent` or `Consistent` block and nothing to remove
/// yields `None`: its output is its input.
///
/// `remove` names the tags `--remove-tag` drops. Removal
/// is applied last, to the rewritten tag set, so a removed tag whittle
/// maintains (`MM`, the move table, a per-base array) is absent from the
/// output rather than left stale.
pub(super) fn window_edit<'a>(
    src: &RecordBuf,
    window: Window,
    mod_block: ModBlock,
    indexed: Option<&IndexedMods>,
    moves: Option<&MoveIndex<'_>>,
    remove: &'a TagRemoval,
) -> Option<RecordEdit<'a>> {
    let Window {
        start, end, total, ..
    } = window;
    let seq = src.sequence().as_ref();
    let qual = src.quality_scores().as_ref();
    let orig_len = seq.len();
    let trimmed = start != 0 || end != orig_len;
    let split = total > 1;
    if !trimmed
        && !split
        && remove.is_empty()
        && matches!(mod_block, ModBlock::Absent | ModBlock::Consistent)
    {
        return None;
    }

    let platform = platform(src);
    let name = (trimmed || split).then(|| {
        let name: &[u8] = src.name().map(AsRef::as_ref).unwrap_or_default();
        let coords = query_span(src).map(|(qs0, _)| window_coords(qs0, start, end));
        segment_name(platform, name, window, coords)
    });

    // Tags with dedicated handling: `Some` replaces the source value in place,
    // or is appended when the source lacks the tag; `None` removes it.
    let mut updates: TagUpdates = Vec::new();
    match mod_block {
        ModBlock::Absent => {},
        ModBlock::Malformed => updates.extend(MOD_TAGS.map(|t| (t, None))),
        ModBlock::Consistent | ModBlock::MissingMn => {
            if let Some((mm, ml)) = mod_tags(src) {
                let (mm, ml) = rebuild_mods(mm, ml, seq, start, end, indexed);
                updates.push((Tag::BASE_MODIFICATIONS, Some(Value::String(mm.into()))));
                updates.push((
                    Tag::BASE_MODIFICATION_PROBABILITIES,
                    ml.map(|ml| Value::Array(Array::UInt8(ml))),
                ));
                updates.push((
                    Tag::BASE_MODIFICATION_SEQUENCE_LENGTH,
                    Some(Value::Int32((end - start) as i32)),
                ));
            }
        },
    }
    updates.extend(window_tag_updates(src, qual, window, platform, moves));
    // The `sa` coverage runs are re-encoded for the window; runs that do not
    // cover the read leave the tag unchanged.
    let sa = Tag::new(RLE_COVERAGE_TAG[0], RLE_COVERAGE_TAG[1]);
    if trimmed
        && let Some(value) = src.data().get(&sa)
        && let Some(sliced) = windowed_value(RLE_COVERAGE_TAG, value, orig_len, start, end)
    {
        updates.push((sa, Some(sliced)));
    }

    Some(RecordEdit {
        start,
        end,
        name,
        updates,
        remove,
        reason: None,
    })
}

/// The per-read state the decoded BAM workflows share.
pub(super) struct PreparedRead<'a> {
    /// The record's bases.
    pub(super) seq: &'a [u8],
    /// The record's per-base qualities.
    pub(super) qual: &'a [u8],
    /// The state of the record's modification block.
    pub(super) mod_block: ModBlock,
    /// The original-coordinate interval retained by barcode restriction, `None`
    /// without an adapter source or when the record carries no verified `bi`.
    pub(super) barcode: Option<(usize, usize)>,
}

/// Runs the per-read guards and bookkeeping shared by the decoded workflows:
/// refuses aligned reads and legacy mod tags, requires full per-base quality,
/// classifies the modification block, counting a malformed one, counts a
/// malformed per-base tag, and resolves the barcode window from the verified
/// `bi` spans, counting an unusable or unverified `bi`.
pub(super) fn prepare_read<'a>(
    rec: &'a RecordBuf,
    cfg: &Config,
    counters: &Counters,
) -> anyhow::Result<PreparedRead<'a>> {
    crate::io::bam::ensure_trimmable(rec)?;
    let seq = rec.sequence().as_ref();
    let qual = rec.quality_scores().as_ref();
    if qual.len() != seq.len() || crate::io::bam::quality_absent(qual) {
        anyhow::bail!(
            "read {}: BAM record SEQ length {} != QUAL length {} \
             (records without per-base quality are not supported)",
            crate::io::bam::display_name(rec.name().map(AsRef::as_ref)),
            seq.len(),
            qual.len()
        );
    }
    let mod_block = inspect_mod_block(rec, seq.len());
    if mod_block == ModBlock::Malformed {
        counters.malformed_mod_reads.fetch_add(1, Ordering::Relaxed);
    }
    if has_malformed_perbase_tag(rec, seq.len()) {
        counters.malformed_tag_reads.fetch_add(1, Ordering::Relaxed);
    }
    let barcode = match cfg.adapters.as_ref() {
        Some(adapters) => match barcode_window(rec, seq.len()) {
            BarcodeSpan::Spans { front, rear } => {
                let (window, unverified) = verified_barcode_window(rec, seq, adapters, front, rear);
                if unverified {
                    counters
                        .barcode_tag_unverified_reads
                        .fetch_add(1, Ordering::Relaxed);
                }
                window
            },
            BarcodeSpan::Absent => None,
            BarcodeSpan::Malformed => {
                counters
                    .barcode_tag_malformed_reads
                    .fetch_add(1, Ordering::Relaxed);
                None
            },
        },
        None => None,
    };
    Ok(PreparedRead {
        seq,
        qual,
        mod_block,
        barcode,
    })
}

/// Runs the per-read guards and the trim on a decoded record, filters each
/// produced segment and calls `render` with every window and the record's
/// modification block: `None` for a survivor, or the reason for a rejected
/// segment (a read that produced no segment is one rejected full window).
/// Rejected windows are rendered only while a rejected output is open.
/// Counts a dropped undo blob once the survivors are known. Shared by the BAM
/// and FASTQ output paths.
pub(super) fn render_windows(
    rec: &RecordBuf,
    cfg: &Config,
    counters: &Counters,
    render: impl FnMut(Window, ModBlock, Option<&IndexedMods>, Option<Reason>) -> anyhow::Result<()>,
) -> anyhow::Result<()> {
    let PreparedRead {
        seq,
        qual,
        mod_block,
        barcode,
    } = prepare_read(rec, cfg, counters)?;
    let _read = crate::workflow::read_span(rec.name().map(|n| n.as_ref()).unwrap_or(b"<unnamed>"));
    let _read = _read.enter();
    let produced = trim::apply(seq, qual, &cfg.trim, cfg.adapters.as_ref(), barcode);
    let indexed =
        if matches!(mod_block, ModBlock::Consistent | ModBlock::MissingMn) && produced.len() > 1 {
            mod_tags(rec).map(|(mm, ml)| IndexedMods::new(mods::parse(mm, ml.unwrap_or(&[])), seq))
        } else {
            None
        };
    let mut survivors: Vec<(usize, usize)> = Vec::new();
    // Both callbacks render, so the closure is shared through a cell.
    let render = std::cell::RefCell::new(render);
    process_read_segments(
        &produced,
        seq,
        qual,
        &cfg.filter,
        counters,
        |idx, total, start, end| {
            survivors.push((start, end));
            render.borrow_mut()(
                Window {
                    start,
                    end,
                    idx,
                    total,
                },
                mod_block,
                indexed.as_ref(),
                None,
            )
        },
        |rejection| {
            if !counters.wants_rejects() {
                return Ok(());
            }
            let (window, reason) = match rejection {
                Rejection::Segment {
                    idx,
                    total,
                    start,
                    end,
                    reason,
                } => (
                    Window {
                        start,
                        end,
                        idx,
                        total,
                    },
                    Reason::Dropped(reason),
                ),
                Rejection::Whole => (
                    Window {
                        start: 0,
                        end: seq.len(),
                        idx: 0,
                        total: 1,
                    },
                    Reason::TrimmedToNothing,
                ),
            };
            render.borrow_mut()(window, mod_block, indexed.as_ref(), Some(reason))
        },
    )?;
    count_dropped_read_tags(counters, rec, seq.len(), &survivors);
    Ok(())
}

/// Renders one record for BAM output: every surviving window is built from
/// the raw record `raw` with the edit its decoded form `rec` determines and
/// handed to `emit`, and every rejected window is sent to the rejected output.
/// Shared by the sequential and parallel drivers. `emit` receives `None` for
/// a window whose output record is the input record.
pub(super) fn render_bam_read(
    header: &sam::Header,
    raw: &bam::Record,
    rec: &RecordBuf,
    cfg: &Config,
    counters: &Counters,
    mut emit: impl FnMut(Option<Vec<u8>>) -> io::Result<()>,
) -> anyhow::Result<()> {
    let direction = if cfg.update_moves {
        signal_reversed(header, rec)
    } else {
        None
    };
    // The move table is indexed on the first window that needs it.
    let moves: std::cell::OnceCell<Option<MoveIndex<'_>>> = std::cell::OnceCell::new();
    let seq = rec.sequence().as_ref();
    render_windows(rec, cfg, counters, |window, mod_block, indexed, reason| {
        let partial = window.start != 0 || window.end != seq.len();
        if cfg.update_moves
            && direction.is_none()
            && partial
            && rec.data().get(&Tag::new(b'm', b'v')).is_some()
        {
            anyhow::bail!(
                "read {}: --update-moves requires a DNA or RNA basecall_model in the @RG description",
                crate::io::bam::display_name(rec.name().map(AsRef::as_ref))
            );
        }
        let moves = if partial {
            moves
                .get_or_init(|| direction.and_then(|reverse| MoveIndex::new(rec, reverse)))
                .as_ref()
        } else {
            None
        };
        let edit = window_edit(rec, window, mod_block, indexed, moves, &cfg.remove_tags);
        match (edit, reason) {
            (None, None) => Ok(emit(None)?),
            (Some(edit), None) => Ok(emit(Some(build_record(raw, edit)?))?),
            (edit, Some(reason)) => {
                let mut edit =
                    edit.unwrap_or_else(|| RecordEdit::unchanged(seq.len(), &cfg.remove_tags));
                edit.reason = Some(reason);
                counters.reject(RejectItem::Bam(build_record(raw, edit)?))
            },
        }
    })
}