streampile 0.2.0

Forward-only pileups streamed from coordinate-sorted BAM records
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
//! Copies a pysam `AlignedSegment` into a BAM record.
//!
//! An `AlignedSegment` holds its record as htslib's `bam1_t`, behind the `_delegate` pointer that
//! pysam's `libcalignedsegment.pxd` declares first in the object, after the Python object header
//! and Cython's vtable pointer, and just before its `header`. Reading the record from there takes
//! one copy, where reading it through the object's attributes takes a dozen Python calls. The
//! layout is checked once, on a record made from known SAM text: the word after `_delegate` must
//! be the record's header before `_delegate` is followed, and the record read from it must then
//! match the record read through attributes. If either differs, records are read through
//! attributes.

use std::cell::RefCell;
use std::ffi::c_void;
use std::mem::size_of;
use std::sync::atomic::{AtomicBool, Ordering};

use noodles::bam;
use pyo3::exceptions::PyValueError;
use pyo3::prelude::*;
use pyo3::sync::PyOnceLock;
use pyo3::types::{PyString, PyType};

use super::to_python;
use crate::Error;
use crate::auxiliary;
use crate::error::invalid_data;

const DELEGATE_OFFSET: usize = size_of::<pyo3::ffi::PyObject>() + size_of::<*const c_void>();

const MOST_CIGAR_OPERATORS: usize = 0xFFFF;

const SEQUENCE_CODES: &[u8; 16] = b"=ACMGRSVTWYHKDBN";

static VERIFIED: AtomicBool = AtomicBool::new(false);
static DIRECT: AtomicBool = AtomicBool::new(false);

static ALIGNED_SEGMENT: PyOnceLock<Py<PyType>> = PyOnceLock::new();

thread_local! {
    static STAGED: RefCell<Vec<u8>> = const { RefCell::new(Vec::new()) };
}

/// htslib's `bam1_core_t`, as htslib 1.10 and later lay it out.
#[repr(C)]
struct Core {
    pos: i64,
    tid: i32,
    bin: u16,
    qual: u8,
    l_extranul: u8,
    flag: u16,
    l_qname: u16,
    n_cigar: u32,
    l_qseq: i32,
    mtid: i32,
    mpos: i64,
    isize: i64,
}

/// htslib's `bam1_t`, up to the fields read here.
#[repr(C)]
struct Delegate {
    core: Core,
    id: u64,
    data: *const u8,
    l_data: i32,
}

/// The fixed-length fields of a BAM record, before its variable-length ones.
struct Head {
    tid: i32,
    pos: i64,
    mapq: u8,
    bin: u16,
    flag: u16,
    l_seq: usize,
    mtid: i32,
    mpos: i64,
    tlen: i64,
}

/// Reads a pysam `AlignedSegment`, or any object with its attributes, into a BAM record.
///
/// The object must not change while this runs, which holding the GIL and a reference to it
/// ensures; nothing of it is kept, written, or freed.
pub(crate) fn read(object: &Bound<'_, PyAny>, record: &mut bam::Record) -> PyResult<()> {
    STAGED.with_borrow_mut(|staged| {
        staged.clear();
        if DIRECT.load(Ordering::Relaxed) && is_aligned_segment(object)? {
            // SAFETY: the object is an `AlignedSegment`, whose layout was checked at import.
            unsafe { stage_delegate(object, staged) }?;
        } else {
            stage_attributes(object, staged)?;
        }
        bam::io::Reader::from(&staged[..])
            .read_record(record)
            .map_err(|error| invalid(&name_of(object), error))?;
        Ok(())
    })
}

/// Whether records are read from htslib's `bam1_t`, and with `enabled`, sets it first: records
/// are read from `bam1_t` only when its layout was found as expected.
#[pyfunction]
#[pyo3(signature = (enabled = None))]
pub(crate) fn direct_bridge(enabled: Option<bool>) -> bool {
    if let Some(enabled) = enabled {
        DIRECT.store(
            enabled && VERIFIED.load(Ordering::Relaxed),
            Ordering::Relaxed,
        );
    }
    DIRECT.load(Ordering::Relaxed)
}

/// Checks pysam's layout against a record made from known SAM text, read through its
/// attributes, and reads records from `bam1_t` from now on only if both agree.
pub(crate) fn verify(py: Python<'_>) -> PyResult<()> {
    const HEADER: &str = "@HD\tVN:1.6\tSO:coordinate\n@SQ\tSN:chr1\tLN:1000\n";
    const LINE: &str = "pair/1\t99\tchr1\t11\t42\t3S4M1I2M2D1M\t=\t20\t30\tACGTACGTACG\t\
        ABCDEFGHIJK\tMC:Z:10M\tXI:i:-7\tXB:B:s,1,-2";
    let pysam = py.import("pysam")?;
    let header = pysam
        .getattr("AlignmentHeader")?
        .call_method1("from_text", (HEADER,))?;
    let segment = pysam
        .getattr("AlignedSegment")?
        .call_method1("fromstring", (LINE, header))?;
    let basic_size: usize = segment.get_type().getattr("__basicsize__")?.extract()?;
    let mut through_attributes = bam::Record::default();
    STAGED.with_borrow_mut(|staged| -> PyResult<()> {
        staged.clear();
        stage_attributes(&segment, staged)?;
        bam::io::Reader::from(&staged[..])
            .read_record(&mut through_attributes)
            .map_err(|error| invalid(&name_of(&segment), error))?;
        Ok(())
    })?;
    let header_at = DELEGATE_OFFSET + size_of::<*const c_void>();
    let laid_out = basic_size >= header_at + size_of::<*const c_void>() && {
        // SAFETY: the object is at least `basic_size` bytes long, so the word read lies within it,
        // and it is only compared, never followed.
        let word = unsafe {
            segment
                .as_ptr()
                .byte_add(header_at)
                .cast::<*const c_void>()
                .read_unaligned()
        };
        word == segment.getattr("header")?.as_ptr().cast::<c_void>()
    };
    let mut direct = bam::Record::default();
    let agrees = laid_out
        && STAGED.with_borrow_mut(|staged| {
            staged.clear();
            // SAFETY: `header` lies where pysam declares it, right after `_delegate`, so the word
            // before it is `_delegate`, which is checked for null; what it points to is compared
            // with the attributes below.
            unsafe { stage_delegate(&segment, staged) }.is_ok()
                && bam::io::Reader::from(&staged[..])
                    .read_record(&mut direct)
                    .is_ok()
        })
        && same_record(&direct, &through_attributes)
        && direct.data().as_bytes() == b"MCZ10M\0";
    VERIFIED.store(agrees, Ordering::Relaxed);
    DIRECT.store(agrees, Ordering::Relaxed);
    Ok(())
}

fn same_record(a: &bam::Record, b: &bam::Record) -> bool {
    a.reference_sequence_id().transpose().ok() == b.reference_sequence_id().transpose().ok()
        && a.alignment_start().transpose().ok() == b.alignment_start().transpose().ok()
        && a.mapping_quality() == b.mapping_quality()
        && a.flags() == b.flags()
        && a.mate_reference_sequence_id().transpose().ok()
            == b.mate_reference_sequence_id().transpose().ok()
        && a.mate_alignment_start().transpose().ok() == b.mate_alignment_start().transpose().ok()
        && a.template_length() == b.template_length()
        && a.name() == b.name()
        && a.cigar().as_bytes() == b.cigar().as_bytes()
        && a.sequence().as_bytes() == b.sequence().as_bytes()
        && a.quality_scores().as_bytes() == b.quality_scores().as_bytes()
        && a.name().is_some_and(|name| name == "pair/1")
}

fn is_aligned_segment(object: &Bound<'_, PyAny>) -> PyResult<bool> {
    let py = object.py();
    let aligned_segment = ALIGNED_SEGMENT.get_or_try_init(py, || {
        py.import("pysam")?
            .getattr("AlignedSegment")?
            .cast_into::<PyType>()
            .map(Bound::unbind)
            .map_err(PyErr::from)
    })?;
    let kind = object.get_type();
    Ok(kind.is(aligned_segment.bind(py)) || kind.is_subclass(aligned_segment.bind(py))?)
}

/// The 0-based start of a pysam `AlignedSegment`, or of any object with its attributes, read
/// without copying its record: -1 for a record with none.
pub(crate) fn reference_start(object: &Bound<'_, PyAny>) -> PyResult<i64> {
    if DIRECT.load(Ordering::Relaxed) && is_aligned_segment(object)? {
        // SAFETY: the object is an `AlignedSegment`, whose layout was checked at import.
        if let Some(delegate) = unsafe { delegate(object) } {
            return Ok(delegate.core.pos);
        }
    }
    object.getattr("reference_start")?.extract()
}

/// The `bam1_t` of an `AlignedSegment`, or `None` for a null `_delegate`.
///
/// # Safety
///
/// `object` must be a pysam `AlignedSegment` laid out as [`verify`] checks.
unsafe fn delegate<'a>(object: &'a Bound<'_, PyAny>) -> Option<&'a Delegate> {
    // SAFETY: the caller guarantees `_delegate` is at this offset of the object.
    let delegate = unsafe {
        object
            .as_ptr()
            .cast::<*const Delegate>()
            .byte_add(DELEGATE_OFFSET)
            .read()
    };
    // SAFETY: a non-null `_delegate` points at the `bam1_t` the object owns, which lives as long
    // as the object.
    unsafe { delegate.as_ref() }
}

/// Stages the record of an `AlignedSegment` from its `bam1_t`, keeping, of its tags, only the
/// `MC` tag a pileup reads, or all of them when they cannot be read.
///
/// # Safety
///
/// `object` must be a pysam `AlignedSegment` laid out as [`verify`] checks.
unsafe fn stage_delegate(object: &Bound<'_, PyAny>, staged: &mut Vec<u8>) -> PyResult<()> {
    let invalid = || PyValueError::new_err("An AlignedSegment holds no valid record.");
    // SAFETY: the caller guarantees the layout.
    let delegate = unsafe { delegate(object) }.ok_or_else(invalid)?;
    let core = &delegate.core;
    let length = usize::try_from(delegate.l_data).map_err(|_| invalid())?;
    let data = if length == 0 || delegate.data.is_null() {
        &[][..]
    } else {
        // SAFETY: htslib keeps `l_data` bytes of the record at `data`.
        unsafe { std::slice::from_raw_parts(delegate.data, length) }
    };
    let l_qname = usize::from(core.l_qname);
    let l_seq = usize::try_from(core.l_qseq).map_err(|_| invalid())?;
    let operators = core.n_cigar as usize;
    let name_end = l_qname
        .checked_sub(usize::from(core.l_extranul))
        .ok_or_else(invalid)?;
    let cigar_end = l_qname + operators * 4;
    let sequence_end = cigar_end + l_seq.div_ceil(2);
    let qualities_end = sequence_end + l_seq;
    if qualities_end > data.len() {
        return Err(invalid());
    }
    let name = if name_end == 0 {
        b"*\0"
    } else {
        &data[..name_end]
    };
    let head = Head {
        tid: core.tid,
        pos: core.pos,
        mapq: core.qual,
        bin: core.bin,
        flag: core.flag,
        l_seq,
        mtid: core.mtid,
        mpos: core.mpos,
        tlen: core.isize,
    };
    let cigar = &data[l_qname..cigar_end];
    let ops = cigar
        .as_chunks::<4>()
        .0
        .iter()
        .map(|op| u32::from_ne_bytes(*op));
    let aux = &data[qualities_end..];
    let mut mate_cigar = Vec::new();
    let aux = match auxiliary::find_field(aux, *b"MC") {
        Ok(Some(field)) => {
            mate_cigar.extend_from_slice(b"MC");
            mate_cigar.extend_from_slice(&aux[field.start as usize..field.end as usize]);
            &mate_cigar[..]
        }
        Ok(None) => &[],
        Err(_) => aux,
    };
    stage(
        staged,
        &head,
        name,
        ops,
        operators,
        |staged| staged.extend_from_slice(&data[cigar_end..qualities_end]),
        aux,
    )
}

/// Stages the record of any object with the attributes of an `AlignedSegment`, keeping, of its
/// tags, only the `MC` tag a pileup reads.
fn stage_attributes(object: &Bound<'_, PyAny>, staged: &mut Vec<u8>) -> PyResult<()> {
    let name: Option<String> = object.getattr("query_name")?.extract()?;
    let cigar: Option<Vec<(u32, u32)>> = object.getattr("cigartuples")?.extract()?;
    let sequence: Option<String> = object.getattr("query_sequence")?.extract()?;
    let qualities: Option<Vec<u8>> = object.getattr("query_qualities")?.extract()?;
    let sequence = sequence.unwrap_or_default();
    let head = Head {
        tid: object.getattr("reference_id")?.extract()?,
        pos: object.getattr("reference_start")?.extract()?,
        mapq: object.getattr("mapping_quality")?.extract()?,
        bin: 4680,
        flag: object.getattr("flag")?.extract()?,
        l_seq: sequence.len(),
        mtid: object.getattr("next_reference_id")?.extract()?,
        mpos: object.getattr("next_reference_start")?.extract()?,
        tlen: object.getattr("template_length")?.extract()?,
    };
    let mut aux = Vec::new();
    if object.call_method1("has_tag", ("MC",))?.is_truthy()? {
        let (value, kind): (Bound<'_, PyAny>, String) =
            object.call_method1("get_tag", ("MC", true))?.extract()?;
        aux.extend_from_slice(b"MC");
        stage_value(&value, kind.as_bytes(), &mut aux)?;
    }
    let mut name = name.unwrap_or_else(|| "*".to_owned()).into_bytes();
    name.push(0);
    let cigar = cigar.unwrap_or_default();
    let operators = cigar.len();
    let ops = cigar.into_iter().map(|(kind, length)| length << 4 | kind);
    let quality_length = head.l_seq;
    stage(
        staged,
        &head,
        &name,
        ops,
        operators,
        |staged| {
            let bases = sequence.as_bytes();
            for pair in bases.chunks(2) {
                let high = nibble(pair[0]) << 4;
                staged.push(high | pair.get(1).map_or(0, |&base| nibble(base)));
            }
            match qualities.as_deref() {
                Some(qualities) if qualities.len() == quality_length => {
                    staged.extend_from_slice(qualities);
                }
                _ => staged.extend(std::iter::repeat_n(0xFF, quality_length)),
            }
        },
        &aux,
    )
}

/// Stages a tag's type and value as BAM stores them, from the value and the type pysam reads.
fn stage_value(value: &Bound<'_, PyAny>, kind: &[u8], staged: &mut Vec<u8>) -> PyResult<()> {
    match kind {
        [b'Z' | b'H'] => {
            staged.extend_from_slice(kind);
            staged.extend_from_slice(value.cast::<PyString>()?.to_str()?.as_bytes());
            staged.push(0);
        }
        [b'A'] => {
            staged.extend_from_slice(kind);
            let text = value.cast::<PyString>()?.to_str()?;
            staged.push(text.bytes().next().unwrap_or(b' '));
        }
        [b'B', subtype] => {
            staged.extend_from_slice(kind);
            let length = u32::try_from(value.len()?)
                .map_err(|_| PyValueError::new_err("An array tag is too long."))?;
            staged.extend_from_slice(&length.to_le_bytes());
            for element in value.try_iter()? {
                stage_number(&element?, *subtype, staged)?;
            }
        }
        [number] => {
            staged.push(*number);
            stage_number(value, *number, staged)?;
        }
        _ => {
            return Err(PyValueError::new_err(format!(
                "A tag has the unknown type {}.",
                String::from_utf8_lossy(kind)
            )));
        }
    }
    Ok(())
}

/// Stages a number of one of BAM's numeric types, little-endian.
fn stage_number(value: &Bound<'_, PyAny>, kind: u8, staged: &mut Vec<u8>) -> PyResult<()> {
    match kind {
        b'c' => staged.extend_from_slice(&value.extract::<i8>()?.to_le_bytes()),
        b'C' => staged.extend_from_slice(&value.extract::<u8>()?.to_le_bytes()),
        b's' => staged.extend_from_slice(&value.extract::<i16>()?.to_le_bytes()),
        b'S' => staged.extend_from_slice(&value.extract::<u16>()?.to_le_bytes()),
        b'i' => staged.extend_from_slice(&value.extract::<i32>()?.to_le_bytes()),
        b'I' => staged.extend_from_slice(&value.extract::<u32>()?.to_le_bytes()),
        b'f' => staged.extend_from_slice(&value.extract::<f32>()?.to_le_bytes()),
        b'd' => staged.extend_from_slice(&value.extract::<f64>()?.to_le_bytes()),
        other => {
            return Err(PyValueError::new_err(format!(
                "A tag has the unknown type {}.",
                char::from(other)
            )));
        }
    }
    Ok(())
}

/// The error of a read that cannot be staged as a BAM record, as the crate words it.
fn invalid(name: &str, problem: impl std::fmt::Display) -> PyErr {
    to_python(Error::InvalidRecord {
        name: name.to_owned(),
        source: invalid_data(format!("{problem}")),
    })
}

/// The name of an object with the attributes of an `AlignedSegment`, `*` without one.
fn name_of(object: &Bound<'_, PyAny>) -> String {
    object
        .getattr("query_name")
        .and_then(|name| name.extract::<Option<String>>())
        .ok()
        .flatten()
        .unwrap_or_else(|| "*".to_owned())
}

fn nibble(base: u8) -> u8 {
    let base = base.to_ascii_uppercase();
    SEQUENCE_CODES
        .iter()
        .position(|&code| code == base)
        .map_or(15, |code| code as u8)
}

/// Stages a record as a BAM reader reads it, block size first, moving a CIGAR of more operators
/// than BAM holds to a `CG` tag, as htslib writes it.
fn stage(
    staged: &mut Vec<u8>,
    head: &Head,
    name: &[u8],
    ops: impl Iterator<Item = u32> + Clone,
    operators: usize,
    sequence_and_qualities: impl FnOnce(&mut Vec<u8>),
    aux: &[u8],
) -> PyResult<()> {
    let read_name = String::from_utf8_lossy(name.strip_suffix(b"\0").unwrap_or(name));
    let too_long = |field: &str| invalid(&read_name, format!("its {field} is too long"));
    let narrow = |value: i64, field: &str| i32::try_from(value).map_err(|_| too_long(field));
    let l_read_name = u8::try_from(name.len()).map_err(|_| too_long("name"))?;
    let long_cigar = operators > MOST_CIGAR_OPERATORS;
    staged.extend_from_slice(&[0; 4]);
    staged.extend_from_slice(&head.tid.to_le_bytes());
    staged.extend_from_slice(&narrow(head.pos, "position")?.to_le_bytes());
    staged.push(l_read_name);
    staged.push(head.mapq);
    staged.extend_from_slice(&head.bin.to_le_bytes());
    let stored_operators = if long_cigar { 2 } else { operators as u16 };
    staged.extend_from_slice(&stored_operators.to_le_bytes());
    staged.extend_from_slice(&head.flag.to_le_bytes());
    let l_seq = u32::try_from(head.l_seq).map_err(|_| too_long("sequence"))?;
    staged.extend_from_slice(&l_seq.to_le_bytes());
    staged.extend_from_slice(&head.mtid.to_le_bytes());
    staged.extend_from_slice(&narrow(head.mpos, "mate position")?.to_le_bytes());
    staged.extend_from_slice(&narrow(head.tlen, "template length")?.to_le_bytes());
    staged.extend_from_slice(name);
    if long_cigar {
        let span: u32 = ops
            .clone()
            .filter(|op| matches!(op & 0xF, 0 | 2 | 3 | 7 | 8))
            .map(|op| op >> 4)
            .sum();
        staged.extend_from_slice(&(l_seq << 4 | 4).to_le_bytes());
        staged.extend_from_slice(&(span << 4 | 3).to_le_bytes());
    } else {
        for op in ops.clone() {
            staged.extend_from_slice(&op.to_le_bytes());
        }
    }
    sequence_and_qualities(staged);
    staged.extend_from_slice(aux);
    if long_cigar {
        staged.extend_from_slice(b"CGBI");
        staged.extend_from_slice(&(operators as u32).to_le_bytes());
        for op in ops {
            staged.extend_from_slice(&op.to_le_bytes());
        }
    }
    let block_size = u32::try_from(staged.len() - 4).map_err(|_| too_long("record"))?;
    staged[..4].copy_from_slice(&block_size.to_le_bytes());
    Ok(())
}