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
use bstr::ByteVec;
use std::{cell::RefCell, collections::HashSet, rc::Rc};
use toml_pretty_deser::Visitor;
use super::super::{PartialRegionDefinition, RegionDefinition, extract_regions};
use crate::transformations::prelude::*;
/// Extract regions by coordinates
/// that is by (segment|source, 0-based start, length)
/// defined triplets, joined with (possibly empty) separator.
#[derive(Clone, JsonSchema)]
#[tpd]
#[derive(Debug)]
#[expect(clippy::struct_field_names, reason = "Step != the actual data")]
pub struct Regions {
#[tpd(nested)]
pub regions: Vec<RegionDefinition>, //validated to be non_empty in transformations::validate_regions
pub out_label: TagLabel,
#[tpd(skip)]
#[schemars(skip)]
pub output_tag_type: TagValueType,
}
impl VerifyIn<PartialConfig> for PartialRegions {
fn verify(
&mut self,
parent: &PartialConfig,
_options: &VerifyOptions,
) -> std::result::Result<(), ValidationFailure>
where
Self: Sized + toml_pretty_deser::Visitor,
{
if let Some(regions) = self.regions.value.as_mut() {
for region in regions.iter_mut() {
if let Some(region_def) = region.value.as_mut() {
region_def.source.validate_segment(parent);
if region_def.can_concrete() {
region.state = TomlValueState::Ok;
}
} // cov:excl-line
}
if regions.iter().all(TomlValue::is_ok) {
self.regions.state = TomlValueState::Ok;
}
} // cov:excl-line
self.regions.verify(|regions| {
if regions.is_empty() {
Err(ValidationFailure::new(
"Must contain at least one region definition",
None,
))
} else {
Ok(())
}
});
Ok(())
}
}
impl TagUser for PartialTaggedVariant<PartialRegions> {
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() {
let mut used_tags = vec![];
let mut seen = HashSet::new();
let mut all_location = true;
let mut any_tags = false;
let all_segments = if let Some(regions) = inner.regions.as_mut() {
let mut all_segments = true;
for tv_region in regions.iter_mut() {
let region = tv_region.as_ref().expect("Parent was ok?");
let source = region
.source
.as_ref()
.expect("parent was ok")
.as_ref_post()
.expect("Not PostVerify");
if !matches!(source, crate::config::ResolvedSourceNoAll::Segment(_)) {
all_segments = false;
}
if let Some(source_tags) = source.get_tags() {
any_tags = true;
let toml_source =
Rc::new(RefCell::new((&mut tv_region.state, &mut tv_region.help)));
for entry in source_tags {
if seen.insert(entry.0.clone()) {
//only add unseen tags
if let Some(provided_tag_types) = tags_available.get(&entry.0) {
if !matches!(
provided_tag_types.tag_type,
TagValueType::Location
) {
all_location = false;
}
} else {
all_location = false;
}
used_tags.push(Some(UsedTag {
name: entry.0,
accepted_tag_types: entry.1,
toml_source: toml_source.clone(),
further_help: None,
}));
}
}
}
}
all_segments
} else {
false
};
let output_tag_type = if (any_tags && all_location) || all_segments {
TagValueType::Location
} else {
TagValueType::String
};
inner.output_tag_type = Some(output_tag_type);
Some(TagUsageInfo {
declared_tag: inner.out_label.to_declared_tag(output_tag_type),
used_tags,
..Default::default()
})
} else {
None // cov:excl-line
}
}
}
impl Step for Regions {
// fn uses_tags(
// &self,
// tags_available: &IndexMap<TagLabel, TagMetadata>,
// ) -> Option<Vec<(String, &[TagValueType])>> {
// let mut tags = Vec::new();
// let mut seen = HashSet::new();
// let mut all_location = true;
// let mut any_tags = false;
// for region in &self.regions {
// if let Some(source_tags) = region.source.get_tags() {
// any_tags = true;
// for entry in source_tags {
// if seen.insert(entry.0.clone()) {
// //only add unseen tags
// if let Some(provided_tag_types) = tags_available.get(&entry.0) {
// if !matches!(provided_tag_types.tag_type, TagValueType::Location) {
// all_location = false;
// }
// } else {
// all_location = false;
// }
// tags.push(entry);
// }
// }
// }
// }
// let all_segments = self
// .regions
// .iter()
// .all(|x| matches!(x.source, crate::config::ResolvedSourceNoAll::Segment(_)));
// if (any_tags && all_location) || all_segments {
// self.output_tag_type
// .set(TagValueType::Location)
// .expect("can't have been set yet");
// } else {
// self.output_tag_type
// .set(TagValueType::String)
// .expect("can't have been set yet");
// }
//
// if tags.is_empty() { None } else { Some(tags) }
// }
fn apply(
&self,
mut block: FastQBlocksCombined,
_input_info: &InputInfo,
_demultiplex_info: &OptDemultiplex,
) -> anyhow::Result<(FastQBlocksCombined, bool)> {
if matches!(self.output_tag_type, TagValueType::Location) {
let mut col = LocationColumn::new();
for ii in 0..block.len() {
let extracted = extract_regions(ii, &block, &self.regions);
if extracted.iter().any(Option::is_none) {
//if any region could not be extracted, we store Missing
col.push_none();
continue;
}
//all segments -> Location.
let mut entries: Vec<(Option<HitRegionView>, Vec<u8>)> = Vec::new();
for (seq, opt_coords) in extracted.into_iter().flatten() {
// eats Nones.
if let Some(coords) = opt_coords {
entries.push((
Some(HitRegionView {
segment_index: coords.segment_index,
start: coords.start,
len: coords.length,
}),
seq.to_vec(),
));
} else if !seq.is_empty() {
//mutants false positive
// cov:excl-start
unreachable!();
// cov:excl-stop
} // cov:excl-line
}
if entries.is_empty() {
col.push_none();
} else {
let refs: Vec<(Option<HitRegionView>, &[u8])> = entries
.iter()
.map(|(loc, seq)| (loc.clone(), seq.as_slice()))
.collect::<Vec<_>>();
col.push_many(&refs);
}
}
block
.tags
.insert(self.out_label.clone(), TagColumn::Location(col));
} else {
let mut out = Vec::with_capacity(block.segments[0].len());
for ii in 0..block.len() {
let mut h = BString::default();
let extracted = extract_regions(ii, &block, &self.regions);
for (seq, _segment_index) in extracted.into_iter().flatten() {
h.push_str(&seq);
}
out.push(Some(h));
}
block
.tags
.insert(self.out_label.clone(), TagColumn::String(out));
}
Ok((block, true))
}
}