use std::sync::Arc;
use noodles::bam;
use noodles::sam::alignment::record::Cigar as _;
use noodles::sam::alignment::record::cigar::op::Kind;
use pyo3::exceptions::{PyIndexError, PyTypeError, PyValueError};
use pyo3::prelude::*;
use pyo3::pyclass::CompareOp;
use pyo3::sync::PyOnceLock;
use pyo3::types::{IntoPyDict, PyDict, PyList, PyString, PyTuple, PyType};
use super::builder::Bridged;
use super::{Int, bridge, to_python};
use noodles::sam::alignment::record::Flags;
use crate::footprint::Footprint;
use crate::overlap::{AgreementStrategy, DisagreementStrategy, Observation};
use crate::pileup::{
EntryKind, MISSING_BASE_QUALITY, NONE, Template, TemplateName, TemplateRead, name_of,
number_templates, templates,
};
use crate::template::{entry_from_five_prime, from_five_prime};
use crate::{DEFAULT_MIN_BASE_QUALITY, PileupEntry};
const ABSENT: i64 = i64::MIN;
const FIELDS: [&str; 6] = [
"alignment",
"query_position",
"query_position_or_next",
"pileup_type",
"insertion_offset",
"insertion_length",
];
const PILEUP_FIELDS: [&str; 4] = [
"reference_name",
"reference_pos",
"pileups",
"min_base_quality",
];
static PILEUP_READ_TYPES: PyOnceLock<[Py<PyAny>; 4]> = PyOnceLock::new();
#[derive(Clone)]
pub(crate) struct Held {
record: Arc<Bridged>,
kind: EntryKind,
query_position: i64,
query_position_or_next: i64,
insertion_offset: i64,
insertion_length: i64,
position: Option<i64>,
five_prime_distance: Option<usize>,
}
impl Held {
pub(crate) fn of(entry: &PileupEntry<'_, super::builder::PyRecord>, position: i64) -> Self {
let present = |value: Option<usize>| value.map_or(ABSENT, |value| value as i64);
Self {
record: Arc::clone(entry.source_record().shared()),
kind: entry.kind(),
query_position: present(entry.query_position()),
query_position_or_next: present(entry.query_position_or_next()),
insertion_offset: present(entry.insertion_offset()),
insertion_length: entry.insertion_length() as i64,
position: Some(position),
five_prime_distance: entry.five_prime_distance(),
}
}
fn located(
record: &Arc<Bridged>,
query_length: u32,
position: i64,
(kind, offset, length): (EntryKind, u32, u32),
) -> Self {
let five_prime_distance = entry_from_five_prime(
kind,
record.record.flags().is_reverse_complemented(),
query_length as usize,
(offset != NONE).then_some(offset as usize),
);
let offset = if offset == NONE {
ABSENT
} else {
i64::from(offset)
};
Self {
record: Arc::clone(record),
kind,
query_position: if kind == EntryKind::Base {
offset
} else {
ABSENT
},
query_position_or_next: if matches!(kind, EntryKind::Base | EntryKind::Deletion) {
offset
} else {
ABSENT
},
insertion_offset: if kind == EntryKind::Insertion {
offset
} else {
ABSENT
},
insertion_length: if kind == EntryKind::Insertion {
i64::from(length)
} else {
0
},
position: Some(position),
five_prime_distance,
}
}
fn by_hand(
record: Arc<Bridged>,
kind: EntryKind,
[
query_position,
query_position_or_next,
insertion_offset,
insertion_length,
]: [i64; 4],
) -> Self {
let bam = &record.record;
let five_prime_distance = bam
.cigar()
.read_length()
.ok()
.filter(|_| kind == EntryKind::Base)
.and_then(|length| {
let reverse = bam.flags().is_reverse_complemented();
from_five_prime(reverse, length, usize::try_from(query_position).ok()?)
});
Self {
record,
kind,
query_position,
query_position_or_next,
insertion_offset,
insertion_length,
position: None,
five_prime_distance,
}
}
fn bam(&self) -> &bam::Record {
&self.record.record
}
fn base(&self) -> PyResult<Option<u8>> {
let sequence = self.bam().sequence();
if self.query_position == ABSENT || sequence.is_empty() {
return Ok(None);
}
let index = index(self.query_position, sequence.len())
.ok_or_else(|| PyIndexError::new_err("string index out of range"))?;
Ok(sequence.get(index))
}
fn quality(&self) -> PyResult<Option<u8>> {
let offset = self.query_position_or_next;
if offset == ABSENT {
return Ok(None);
}
let record = self.bam();
let qualities = record.quality_scores().as_bytes();
if qualities.is_empty() {
return Ok((offset < record.sequence().len() as i64).then_some(MISSING_BASE_QUALITY));
}
let index = index(offset, qualities.len())
.ok_or_else(|| PyIndexError::new_err("array index out of range"))?;
Ok(Some(qualities[index]))
}
fn passes(&self, floor: i64) -> PyResult<bool> {
Ok(self
.quality()?
.is_some_and(|quality| i64::from(quality) >= floor))
}
fn inserted_bases(&self) -> Option<String> {
let sequence = self.bam().sequence();
if self.insertion_offset == ABSENT || sequence.is_empty() {
return None;
}
let (start, end) = slice(
self.insertion_offset,
self.insertion_offset.saturating_add(self.insertion_length),
sequence.len(),
);
Some(
(start..end)
.filter_map(|index| sequence.get(index).map(char::from))
.collect(),
)
}
fn inserted_qualities(&self) -> Option<Vec<u8>> {
if self.insertion_offset == ABSENT {
return None;
}
let record = self.bam();
let qualities = record.quality_scores().as_bytes();
if qualities.is_empty() {
let stored = record.sequence().len();
let length = usize::try_from(self.insertion_length).unwrap_or(0);
return (stored > 0).then(|| vec![MISSING_BASE_QUALITY; length]);
}
let (start, end) = slice(
self.insertion_offset,
self.insertion_offset.saturating_add(self.insertion_length),
qualities.len(),
);
Some(qualities[start..end].to_vec())
}
fn template_end_distance(&self) -> crate::Result<Option<usize>> {
let position = self.position.or_else(|| self.position_of_base());
let Some(position) = position.and_then(|position| usize::try_from(position).ok()) else {
return Ok(None);
};
let ends = self.record.derived.ends(self.bam())?;
Ok(ends.and_then(|ends| ends.distance(position)))
}
fn is_fr_pair(&self) -> crate::Result<bool> {
self.record.derived.is_fr_pair(self.bam())
}
fn position_of_base(&self) -> Option<i64> {
if self.kind != EntryKind::Base || self.query_position < 0 {
return None;
}
let record = self.bam();
let mut reference = usize::from(record.alignment_start()?.ok()?) as i64 - 1;
let mut query = 0;
for op in record.cigar().iter() {
let op = op.ok()?;
let length = op.len() as i64;
match op.kind() {
Kind::Match | Kind::SequenceMatch | Kind::SequenceMismatch => {
if (query..query + length).contains(&self.query_position) {
return Some(reference + self.query_position - query);
}
reference += length;
query += length;
}
Kind::Insertion | Kind::SoftClip => query += length,
Kind::Deletion | Kind::Skip => reference += length,
Kind::HardClip | Kind::Pad => {}
}
}
None
}
fn template(&self) -> TemplateName<'_> {
TemplateName::of(
self.bam(),
&self.record.derived,
Arc::as_ptr(&self.record) as usize,
)
}
fn observation(&self) -> PyResult<Observation> {
Ok(Observation {
kind: self.kind,
base: self.base()?,
quality: self.quality()?,
})
}
fn fields<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyTuple>> {
let optional = |value: i64| (value != ABSENT).then_some(value);
(
self.record.object.clone_ref(py),
optional(self.query_position),
optional(self.query_position_or_next),
pileup_read_type(py, self.kind)?,
optional(self.insertion_offset),
self.insertion_length,
)
.into_pyobject(py)
}
}
impl TemplateRead for Held {
fn flags(&self) -> Flags {
self.bam().flags()
}
fn five_prime_distance(&self) -> Option<usize> {
self.five_prime_distance
}
fn template_end_distance(&self) -> crate::Result<Option<usize>> {
Held::template_end_distance(self)
}
fn is_fr_pair(&self) -> crate::Result<bool> {
Held::is_fr_pair(self)
}
}
fn as_tuple<'py>(
py: Python<'py>,
value: &Bound<'py, PyAny>,
) -> PyResult<Option<Bound<'py, PyTuple>>> {
if let Ok(read) = value.cast::<PileupRead>() {
return read.get().held.fields(py).map(Some);
}
Ok(value.cast::<PyTuple>().ok().cloned())
}
fn bridged(alignment: &Bound<'_, PyAny>) -> PyResult<Arc<Bridged>> {
let mut record = bam::Record::default();
bridge::read(alignment, &mut record)?;
Ok(Arc::new(Bridged::new(alignment.clone().unbind(), record)))
}
fn reads_of<'a>(
py: Python<'_>,
entries: impl IntoIterator<Item = &'a Held>,
) -> PyResult<Py<PyTuple>> {
let reads = entries
.into_iter()
.map(|held| Py::new(py, PileupRead { held: held.clone() }))
.collect::<PyResult<Vec<_>>>()?;
Ok(PyTuple::new(py, reads)?.unbind())
}
fn index(position: i64, length: usize) -> Option<usize> {
let length = length as i64;
let position = if position < 0 {
position + length
} else {
position
};
(0..length).contains(&position).then_some(position as usize)
}
fn slice(start: i64, stop: i64, length: usize) -> (usize, usize) {
let bound = |value: i64| {
let value = if value < 0 {
value + length as i64
} else {
value
};
value.clamp(0, length as i64) as usize
};
let (start, stop) = (bound(start), bound(stop));
(start, stop.max(start))
}
fn kind_of(name: &str) -> PyResult<EntryKind> {
Ok(match name {
"base" => EntryKind::Base,
"deletion" => EntryKind::Deletion,
"insertion" => EntryKind::Insertion,
"skip" => EntryKind::Skip,
_ => {
return Err(PyValueError::new_err(format!(
"'{name}' is not a valid PileupReadType"
)));
}
})
}
fn pileup_read_type(py: Python<'_>, kind: EntryKind) -> PyResult<Bound<'_, PyAny>> {
let members = PILEUP_READ_TYPES.get_or_try_init(py, || -> PyResult<_> {
let types = py.import("streampile._pileup")?.getattr("PileupReadType")?;
let member = |name: &str| types.getattr(name).map(Bound::unbind);
Ok([
member("base")?,
member("deletion")?,
member("insertion")?,
member("skip")?,
])
})?;
let index = match kind {
EntryKind::Base => 0,
EntryKind::Deletion => 1,
EntryKind::Insertion => 2,
EntryKind::Skip => 3,
};
Ok(members[index].bind(py).clone())
}
fn optional(value: Option<i64>) -> i64 {
value.unwrap_or(ABSENT)
}
#[pyclass(module = "streampile", name = "PileupRead", frozen)]
pub(crate) struct PileupRead {
held: Held,
}
#[pymethods]
impl PileupRead {
#[new]
#[pyo3(signature = (
alignment,
query_position,
query_position_or_next,
pileup_type,
insertion_offset = None,
insertion_length = 0,
))]
fn new(
alignment: &Bound<'_, PyAny>,
query_position: Option<i64>,
query_position_or_next: Option<i64>,
pileup_type: &str,
insertion_offset: Option<i64>,
insertion_length: i64,
) -> PyResult<Self> {
Ok(Self {
held: Held::by_hand(
bridged(alignment)?,
kind_of(pileup_type)?,
[
optional(query_position),
optional(query_position_or_next),
optional(insertion_offset),
insertion_length,
],
),
})
}
#[classattr]
#[pyo3(name = "_fields")]
fn field_names(py: Python<'_>) -> PyResult<Bound<'_, PyTuple>> {
PyTuple::new(py, FIELDS)
}
#[classattr]
fn __match_args__(py: Python<'_>) -> PyResult<Bound<'_, PyTuple>> {
PyTuple::new(py, FIELDS)
}
#[classattr]
#[pyo3(name = "_field_defaults")]
fn field_defaults(py: Python<'_>) -> PyResult<Bound<'_, PyDict>> {
let defaults = PyDict::new(py);
defaults.set_item("insertion_offset", py.None())?;
defaults.set_item("insertion_length", 0)?;
Ok(defaults)
}
#[classmethod]
#[pyo3(name = "_make")]
fn make<'py>(
cls: &Bound<'py, PyType>,
iterable: &Bound<'py, PyAny>,
) -> PyResult<Bound<'py, PyAny>> {
let fields = iterable.try_iter()?.collect::<PyResult<Vec<_>>>()?;
cls.call1(PyTuple::new(cls.py(), fields)?)
}
#[getter]
fn alignment(&self, py: Python<'_>) -> Py<PyAny> {
self.held.record.object.clone_ref(py)
}
#[getter]
fn query_position(&self) -> Option<i64> {
(self.held.query_position != ABSENT).then_some(self.held.query_position)
}
#[getter]
fn query_position_or_next(&self) -> Option<i64> {
(self.held.query_position_or_next != ABSENT).then_some(self.held.query_position_or_next)
}
#[getter]
fn pileup_type<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> {
pileup_read_type(py, self.held.kind)
}
#[getter]
fn insertion_offset(&self) -> Option<i64> {
(self.held.insertion_offset != ABSENT).then_some(self.held.insertion_offset)
}
#[getter]
fn insertion_length(&self) -> i64 {
self.held.insertion_length
}
#[getter]
fn base(&self) -> PyResult<Option<char>> {
Ok(self.held.base()?.map(char::from))
}
#[getter]
fn qual(&self) -> PyResult<Option<u8>> {
self.held.quality()
}
#[getter]
fn is_del(&self) -> bool {
self.held.kind == EntryKind::Deletion
}
#[getter]
fn is_no_call(&self) -> PyResult<bool> {
Ok(self.held.base()? == Some(b'N'))
}
#[getter]
fn is_ins(&self) -> bool {
self.held.kind == EntryKind::Insertion
}
#[getter]
fn is_refskip(&self) -> bool {
self.held.kind == EntryKind::Skip
}
#[getter]
fn inserted_bases(&self) -> Option<String> {
self.held.inserted_bases()
}
#[getter]
fn inserted_qualities<'py>(&self, py: Python<'py>) -> PyResult<Option<Bound<'py, PyList>>> {
self.held
.inserted_qualities()
.map(|qualities| PyList::new(py, qualities))
.transpose()
}
#[getter]
fn five_prime_distance(&self) -> Option<usize> {
self.held.five_prime_distance()
}
#[getter]
fn template_end_distance(&self) -> PyResult<Option<usize>> {
self.held.template_end_distance().map_err(to_python)
}
#[getter]
fn is_fr_pair(&self) -> PyResult<bool> {
self.held.is_fr_pair().map_err(to_python)
}
#[pyo3(name = "_asdict")]
fn asdict<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyDict>> {
let fields = self.held.fields(py)?;
let dict = PyDict::new(py);
for (name, value) in FIELDS.iter().zip(fields.iter()) {
dict.set_item(name, value)?;
}
Ok(dict)
}
#[pyo3(signature = (**changes))]
#[pyo3(name = "_replace")]
fn replace(&self, py: Python<'_>, changes: Option<&Bound<'_, PyDict>>) -> PyResult<Self> {
let fields = self.asdict(py)?;
let mut moved = false;
if let Some(changes) = changes {
for (name, value) in changes {
if !FIELDS.contains(&name.extract::<&str>()?) {
return Err(PyValueError::new_err(format!(
"Got unexpected field names: [{}]",
name.repr()?
)));
}
moved |= name.eq("alignment")?;
fields.set_item(name, value)?;
}
}
let field = |name: &str| fields.as_any().get_item(name);
let record = if moved {
bridged(&field("alignment")?)?
} else {
Arc::clone(&self.held.record)
};
let mut held = Held::by_hand(
record,
kind_of(&field("pileup_type")?.extract::<String>()?)?,
[
optional(field("query_position")?.extract()?),
optional(field("query_position_or_next")?.extract()?),
optional(field("insertion_offset")?.extract()?),
field("insertion_length")?.extract()?,
],
);
if !moved {
held.position = self.held.position;
let placed =
|held: &Held| (held.kind, held.query_position, held.query_position_or_next);
if placed(&held) == placed(&self.held) {
held.five_prime_distance = self.held.five_prime_distance;
}
}
Ok(Self { held })
}
#[pyo3(signature = (**changes))]
fn __replace__(&self, py: Python<'_>, changes: Option<&Bound<'_, PyDict>>) -> PyResult<Self> {
self.replace(py, changes)
}
fn count<'py>(
&self,
py: Python<'py>,
value: &Bound<'py, PyAny>,
) -> PyResult<Bound<'py, PyAny>> {
self.held.fields(py)?.call_method1("count", (value,))
}
#[pyo3(signature = (value, *bounds))]
fn index<'py>(
&self,
py: Python<'py>,
value: &Bound<'py, PyAny>,
bounds: &Bound<'py, PyTuple>,
) -> PyResult<Bound<'py, PyAny>> {
let arguments: Vec<_> = std::iter::once(value.clone()).chain(bounds).collect();
let arguments = PyTuple::new(py, arguments)?;
self.held.fields(py)?.call_method1("index", arguments)
}
fn __contains__(&self, py: Python<'_>, value: &Bound<'_, PyAny>) -> PyResult<bool> {
self.held.fields(py)?.contains(value)
}
fn __add__<'py>(&self, py: Python<'py>, other: &Bound<'py, PyAny>) -> PyResult<Py<PyAny>> {
let Some(other) = as_tuple(py, other)? else {
return Ok(py.NotImplemented());
};
Ok(self.held.fields(py)?.add(other)?.unbind())
}
fn __radd__<'py>(&self, py: Python<'py>, other: &Bound<'py, PyAny>) -> PyResult<Py<PyAny>> {
let Some(other) = as_tuple(py, other)? else {
return Ok(py.NotImplemented());
};
Ok(other.add(self.held.fields(py)?)?.unbind())
}
fn __mul__<'py>(&self, py: Python<'py>, times: &Bound<'py, PyAny>) -> PyResult<Py<PyAny>> {
match self.held.fields(py)?.as_any().mul(times) {
Ok(repeated) => Ok(repeated.unbind()),
Err(_) => Ok(py.NotImplemented()),
}
}
fn __rmul__<'py>(&self, py: Python<'py>, times: &Bound<'py, PyAny>) -> PyResult<Py<PyAny>> {
self.__mul__(py, times)
}
fn __getnewargs__<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyTuple>> {
self.held.fields(py)
}
fn __reduce__<'py>(
slf: &Bound<'py, Self>,
) -> PyResult<(Bound<'py, PyType>, Bound<'py, PyTuple>)> {
Ok((slf.get_type(), slf.get().held.fields(slf.py())?))
}
fn __copy__(slf: Bound<'_, Self>) -> Bound<'_, Self> {
slf
}
fn __deepcopy__(&self, py: Python<'_>, memo: &Bound<'_, PyAny>) -> PyResult<Self> {
let alignment = py
.import("copy")?
.call_method1("deepcopy", (self.held.record.object.bind(py), memo))?;
let record = Bridged::new(alignment.unbind(), self.held.record.record.clone());
Ok(Self {
held: Held {
record: Arc::new(record),
..self.held.clone()
},
})
}
#[allow(clippy::unused_self)]
fn __len__(&self) -> usize {
FIELDS.len()
}
fn __getitem__<'py>(
&self,
py: Python<'py>,
index: &Bound<'py, PyAny>,
) -> PyResult<Bound<'py, PyAny>> {
self.held.fields(py)?.as_any().get_item(index)
}
fn __iter__<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> {
Ok(self.held.fields(py)?.as_any().try_iter()?.into_any())
}
fn __hash__(&self, py: Python<'_>) -> PyResult<isize> {
self.held.fields(py)?.hash()
}
fn __richcmp__<'py>(
&self,
py: Python<'py>,
other: &Bound<'py, PyAny>,
op: CompareOp,
) -> PyResult<Py<PyAny>> {
let Some(other) = as_tuple(py, other)? else {
return Ok(py.NotImplemented());
};
Ok(self.held.fields(py)?.rich_compare(other, op)?.unbind())
}
fn __repr__(&self, py: Python<'_>) -> PyResult<String> {
let fields = self.held.fields(py)?;
let mut parts = Vec::with_capacity(FIELDS.len());
for (name, value) in FIELDS.iter().zip(fields.iter()) {
parts.push(format!("{name}={}", value.repr()?));
}
Ok(format!("PileupRead({})", parts.join(", ")))
}
}
#[pyclass(module = "streampile", name = "Pileup", frozen, weakref)]
pub(crate) struct Pileup {
reference_name: Py<PyString>,
reference_pos: i64,
min_base_quality: i64,
entries: Vec<Held>,
pileups: PyOnceLock<Py<PyTuple>>,
}
impl Pileup {
pub(crate) fn of(
reference_name: Py<PyString>,
reference_pos: i64,
min_base_quality: i64,
entries: Vec<Held>,
) -> Self {
Self {
reference_name,
reference_pos,
min_base_quality,
entries,
pileups: PyOnceLock::new(),
}
}
pub(crate) fn is_at(&self, py: Python<'_>, contig: &str, position: i64) -> bool {
self.reference_pos == position
&& self
.reference_name
.bind(py)
.to_cow()
.is_ok_and(|name| name == contig)
}
pub(crate) fn locus(&self, py: Python<'_>) -> String {
format!("{}:{}", self.reference_name.bind(py), self.reference_pos)
}
fn pileups_of<'py>(&self, py: Python<'py>) -> PyResult<&Bound<'py, PyTuple>> {
let pileups = self
.pileups
.get_or_try_init(py, || reads_of(py, &self.entries))?;
Ok(pileups.bind(py))
}
fn fields<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyTuple>> {
(
self.reference_name.clone_ref(py),
self.reference_pos,
self.pileups_of(py)?.clone(),
self.min_base_quality,
)
.into_pyobject(py)
}
fn passing(&self) -> impl Iterator<Item = PyResult<(&Held, bool)>> {
let floor = self.min_base_quality;
self.entries
.iter()
.map(move |held| Ok((held, held.passes(floor)?)))
}
}
#[pymethods]
impl Pileup {
#[new]
#[pyo3(signature = (
reference_name,
reference_pos,
pileups,
min_base_quality = Int::from(i64::from(DEFAULT_MIN_BASE_QUALITY)),
))]
#[pyo3(text_signature = "(reference_name, reference_pos, pileups, min_base_quality=13)")]
fn new(
py: Python<'_>,
reference_name: Bound<'_, PyString>,
reference_pos: i64,
pileups: &Bound<'_, PyAny>,
min_base_quality: Int,
) -> PyResult<Self> {
let min_base_quality = i64::from(min_base_quality.of("min_base_quality", u8::MAX)?);
let pileups = PyTuple::new(py, pileups.try_iter()?.collect::<PyResult<Vec<_>>>()?)?;
let entries = pileups
.iter()
.map(|read| {
read.cast::<PileupRead>()
.map(|read| Held {
position: Some(reference_pos),
..read.get().held.clone()
})
.map_err(|_| PyTypeError::new_err("every entry of a Pileup is a PileupRead"))
})
.collect::<PyResult<Vec<_>>>()?;
let pileup = Self::of(
reference_name.unbind(),
reference_pos,
min_base_quality,
entries,
);
let _ = pileup.pileups.set(py, pileups.unbind());
Ok(pileup)
}
#[classmethod]
#[pyo3(signature = (
alignments,
contig,
pos,
min_base_quality = Int::from(i64::from(DEFAULT_MIN_BASE_QUALITY)),
))]
#[pyo3(text_signature = "(alignments, contig, pos, min_base_quality=13)")]
fn from_alignments(
_cls: &Bound<'_, PyType>,
alignments: &Bound<'_, PyAny>,
contig: Bound<'_, PyString>,
pos: i64,
min_base_quality: Int,
) -> PyResult<Self> {
let min_base_quality = i64::from(min_base_quality.of("min_base_quality", u8::MAX)?);
let mut entries = Vec::new();
let mut footprint = Footprint::default();
for alignment in alignments.try_iter()? {
let alignment = alignment?;
if !alignment.getattr("reference_name")?.eq(&contig)? {
continue;
}
let mut record = bam::Record::default();
bridge::read(&alignment, &mut record)?;
let flags = record.flags();
let (Some(Ok(_)), Some(Ok(start))) =
(record.reference_sequence_id(), record.alignment_start())
else {
continue;
};
if flags.is_unmapped() {
continue;
}
let start = usize::from(start) as i64 - 1;
let placed = footprint
.fill(start, record.cigar().as_bytes(), record.sequence().len())
.map_err(|source| {
to_python(crate::Error::InvalidRecord {
name: crate::pileup::record_name(&record),
source,
})
})?;
if !placed {
continue;
}
let query_length = footprint.query_length;
let record = Arc::new(Bridged::new(alignment.unbind(), record));
footprint.entries_at(pos, |kind, offset, length| {
entries.push(Held::located(
&record,
query_length,
pos,
(kind, offset, length),
));
});
}
Ok(Self::of(contig.unbind(), pos, min_base_quality, entries))
}
#[getter]
fn reference_name(&self, py: Python<'_>) -> Py<PyString> {
self.reference_name.clone_ref(py)
}
#[getter]
fn reference_pos(&self) -> i64 {
self.reference_pos
}
#[getter]
fn pileups<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyTuple>> {
self.pileups_of(py).cloned()
}
#[getter]
fn min_base_quality(&self) -> i64 {
self.min_base_quality
}
#[getter]
fn unfiltered_depth(&self) -> usize {
self.entries
.iter()
.filter(|held| held.kind != EntryKind::Insertion)
.count()
}
#[getter]
fn filtered_depth(&self) -> PyResult<usize> {
let mut depth = 0;
for passing in self.passing() {
let (held, passes) = passing?;
depth += usize::from(passes && held.kind != EntryKind::Insertion);
}
Ok(depth)
}
#[getter]
fn bases(&self) -> PyResult<Vec<char>> {
let mut bases = Vec::with_capacity(self.entries.len());
for passing in self.passing() {
let (held, passes) = passing?;
if passes
&& held.kind == EntryKind::Base
&& let Some(base) = held.base()?
{
bases.push(char::from(base));
}
}
Ok(bases)
}
#[getter]
fn qualities<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyList>> {
let floor = self.min_base_quality;
let mut qualities = Vec::with_capacity(self.entries.len());
for held in &self.entries {
if held.kind == EntryKind::Base
&& let Some(quality) = held.quality()?
&& i64::from(quality) >= floor
{
qualities.push(quality);
}
}
PyList::new(py, qualities)
}
#[pyo3(signature = (*, agreement = "consensus", disagreement = "consensus"))]
fn templates(&self, agreement: &str, disagreement: &str) -> PyResult<Vec<PileupTemplate>> {
let agreement: AgreementStrategy = agreement.parse().map_err(to_python)?;
let disagreement: DisagreementStrategy = disagreement.parse().map_err(to_python)?;
let numbers = number_templates(&self.entries, |held| {
(held.kind != EntryKind::Insertion).then(|| held.template())
});
let mut entries = Vec::with_capacity(self.entries.len());
for (held, number) in self.entries.iter().zip(numbers) {
if let Some(number) = number {
entries.push((number, held.clone(), held.observation()?));
}
}
let floor = self.min_base_quality;
Ok(templates(entries, agreement, disagreement, floor)
.into_iter()
.map(|template| PileupTemplate {
template,
pileups: PyOnceLock::new(),
})
.collect())
}
#[classattr]
fn __match_args__(py: Python<'_>) -> PyResult<Bound<'_, PyTuple>> {
PyTuple::new(py, PILEUP_FIELDS)
}
#[classattr]
fn __dataclass_fields__(py: Python<'_>) -> PyResult<Bound<'_, PyAny>> {
let dataclasses = py.import("dataclasses")?;
let builtins = py.import("builtins")?;
let floor = dataclasses.getattr("field")?.call(
(),
Some(&[("default", DEFAULT_MIN_BASE_QUALITY)].into_py_dict(py)?),
)?;
let fields = PyList::new(
py,
[
(PILEUP_FIELDS[0], builtins.getattr("str")?)
.into_pyobject(py)?
.into_any(),
(PILEUP_FIELDS[1], builtins.getattr("int")?)
.into_pyobject(py)?
.into_any(),
(PILEUP_FIELDS[2], builtins.getattr("tuple")?)
.into_pyobject(py)?
.into_any(),
(PILEUP_FIELDS[3], builtins.getattr("int")?, floor)
.into_pyobject(py)?
.into_any(),
],
)?;
dataclasses
.call_method1("make_dataclass", ("Pileup", fields))?
.getattr("__dataclass_fields__")
}
#[pyo3(signature = (**changes))]
fn __replace__<'py>(
slf: &Bound<'py, Self>,
changes: Option<&Bound<'py, PyDict>>,
) -> PyResult<Bound<'py, PyAny>> {
let py = slf.py();
let fields = PyDict::new(py);
for (name, value) in PILEUP_FIELDS.iter().zip(slf.get().fields(py)?.iter()) {
fields.set_item(name, value)?;
}
if let Some(changes) = changes {
fields.update(changes.as_mapping())?;
}
slf.get_type().call((), Some(&fields))
}
fn __reduce__<'py>(
slf: &Bound<'py, Self>,
) -> PyResult<(Bound<'py, PyType>, Bound<'py, PyTuple>)> {
Ok((slf.get_type(), slf.get().fields(slf.py())?))
}
fn __copy__(slf: Bound<'_, Self>) -> Bound<'_, Self> {
slf
}
fn __deepcopy__(&self, py: Python<'_>, memo: &Bound<'_, PyAny>) -> PyResult<Self> {
let pileups = py
.import("copy")?
.call_method1("deepcopy", (self.pileups_of(py)?, memo))?;
Self::new(
py,
self.reference_name.bind(py).clone(),
self.reference_pos,
&pileups,
Int::from(self.min_base_quality),
)
}
fn __eq__(&self, py: Python<'_>, other: &Bound<'_, PyAny>) -> PyResult<Py<PyAny>> {
let Ok(other) = other.cast::<Pileup>() else {
return Ok(py.NotImplemented());
};
let equal = self.fields(py)?.eq(other.get().fields(py)?)?;
Ok(equal.into_pyobject(py)?.to_owned().into_any().unbind())
}
fn __hash__(&self, py: Python<'_>) -> PyResult<isize> {
self.fields(py)?.hash()
}
fn __repr__(&self, py: Python<'_>) -> PyResult<String> {
Ok(format!(
"Pileup(reference_name={}, reference_pos={}, pileups={}, min_base_quality={})",
self.reference_name.bind(py).repr()?,
self.reference_pos,
self.pileups_of(py)?.repr()?,
self.min_base_quality,
))
}
}
#[pyclass(module = "streampile", name = "PileupTemplate", frozen)]
pub(crate) struct PileupTemplate {
template: Template<Held>,
pileups: PyOnceLock<Py<PyTuple>>,
}
#[pymethods]
impl PileupTemplate {
#[getter]
fn query_name(&self) -> String {
String::from_utf8_lossy(name_of(self.template.first().bam())).into_owned()
}
#[getter]
fn reads<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyTuple>> {
let reads = self
.pileups
.get_or_try_init(py, || reads_of(py, self.template.entries()))?;
Ok(reads.bind(py).clone())
}
#[getter]
fn pileup_type<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> {
pileup_read_type(py, self.template.called().kind)
}
#[getter]
fn base(&self) -> Option<char> {
self.template.called().base.map(char::from)
}
#[getter]
fn qual(&self) -> Option<u8> {
self.template.called().quality
}
#[getter]
fn is_del(&self) -> bool {
self.template.called().kind == EntryKind::Deletion
}
#[getter]
fn is_refskip(&self) -> bool {
self.template.called().kind == EntryKind::Skip
}
#[getter]
fn is_no_call(&self) -> bool {
self.template.called().base == Some(b'N')
}
#[getter]
fn is_reverse(&self) -> bool {
self.template.is_reverse()
}
#[getter]
fn five_prime_distance(&self) -> PyResult<Option<usize>> {
self.template.five_prime_distance().map_err(to_python)
}
#[getter]
fn template_end_distance(&self) -> PyResult<Option<usize>> {
self.template.template_end_distance().map_err(to_python)
}
fn __repr__(&self, py: Python<'_>) -> PyResult<String> {
Ok(format!(
"PileupTemplate(query_name={}, pileup_type={}, base={}, qual={}, reads={})",
self.query_name().into_pyobject(py)?.repr()?,
self.pileup_type(py)?.repr()?,
self.base().into_pyobject(py)?.repr()?,
self.qual().into_pyobject(py)?.repr()?,
self.reads(py)?.repr()?,
))
}
}