use crate::transformations::prelude::*;
#[derive(Clone, JsonSchema)]
#[tpd]
#[derive(Debug)]
pub struct Truncate {
n: usize,
#[schemars(with = "String")]
#[tpd(adapt_in_verify(String))]
segment: SegmentIndex,
#[tpd(alias = "if_label")]
if_tag: Option<ConditionalTagLabel>,
}
impl VerifyIn<PartialConfig> for PartialTruncate {
fn verify(
&mut self,
parent: &PartialConfig,
_options: &VerifyOptions,
) -> std::result::Result<(), ValidationFailure>
where
Self: Sized + toml_pretty_deser::Visitor,
{
self.segment.validate_segment(parent);
Ok(())
}
}
impl TagUser for PartialTaggedVariant<PartialTruncate> {
fn get_tag_usage(
&mut self,
_tags_available: &IndexMap<TagLabel, TagMetadata>,
_segment_order: &[String],
) -> Option<TagUsageInfo<'_>> {
if let Some(inner) = self.toml_value.value.as_mut() {
Some(TagUsageInfo {
used_tags: vec![inner.if_tag.to_used_tag(&[])],
must_see_all_tags: true,
..Default::default()
})
} else {
None }
}
}
impl Step for Truncate {
fn apply(
&self,
mut block: FastQBlocksCombined,
_input_info: &InputInfo,
_demultiplex_info: &OptDemultiplex,
) -> anyhow::Result<(FastQBlocksCombined, bool)> {
let condition = self
.if_tag
.as_ref()
.map(|tag| get_bool_vec_from_tag(&block, tag));
block.apply_in_place(
self.segment,
|read| read.max_len(self.n),
condition.as_deref(),
);
block.filter_tag_locations_beyond_read_length(self.segment);
Ok((block, true))
}
}