twitcher 0.1.8

Find template switch mutations in genomic data
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
use std::{
    fmt::Display,
    mem,
    num::TryFromIntError,
    ops::{Add, AddAssign, Sub, SubAssign},
    sync::LazyLock,
};

use bstr::ByteSlice;
use rust_htslib::bam::{self, ext::BamRecordExtensions};
use serde::{Deserialize, Serialize};

use crate::common::contig::ContigName;

#[derive(Clone, Debug, PartialEq, Eq, Hash, Deserialize, Serialize)]
pub struct GenomePosition {
    contig: ContigName,
    // zero-based
    position: usize,
}

#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Hash, Deserialize, Serialize)]
pub struct GenomeRegion {
    start: GenomePosition,
    len: Option<usize>,
}

impl GenomePosition {
    /// Create a position from a 0-based coordinate
    pub fn new_0(contig: ContigName, position: usize) -> Self {
        Self { contig, position }
    }

    /// Create a position from a 1-based coordinate
    pub fn new_1(contig: ContigName, position: usize) -> Self {
        Self {
            contig,
            position: position - 1,
        }
    }

    pub fn contig(&self) -> &ContigName {
        &self.contig
    }

    pub fn position_0(&self) -> usize {
        self.position
    }

    pub fn abs_diff(&self, other: &Self) -> usize {
        self.position.abs_diff(other.position)
    }
}

impl PartialOrd for GenomePosition {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        self.contig
            .eq(&other.contig)
            .then_some(self.position.cmp(&other.position))
    }
}

impl AddAssign<usize> for GenomePosition {
    fn add_assign(&mut self, rhs: usize) {
        self.position += rhs;
    }
}

impl Add<usize> for GenomePosition {
    type Output = GenomePosition;

    fn add(mut self, rhs: usize) -> Self::Output {
        self += rhs;
        self
    }
}

impl SubAssign<usize> for GenomePosition {
    fn sub_assign(&mut self, rhs: usize) {
        self.position -= rhs;
    }
}

impl Sub<usize> for GenomePosition {
    type Output = GenomePosition;

    fn sub(mut self, rhs: usize) -> Self::Output {
        self -= rhs;
        self
    }
}

#[allow(dead_code)]
impl GenomeRegion {
    pub fn new_bounded(start: GenomePosition, len: usize) -> Self {
        Self {
            start,
            len: Some(len),
        }
    }

    pub fn new_unbounded(start: GenomePosition) -> Self {
        Self { start, len: None }
    }

    pub fn contig(&self) -> &ContigName {
        self.start.contig()
    }

    pub fn from_incl_incl(
        start: GenomePosition,
        end: Option<GenomePosition>,
    ) -> anyhow::Result<Self> {
        Self::from_incl_excl(start, end.map(|end| end + 1))
    }

    pub fn from_incl_excl(
        start: GenomePosition,
        end: Option<GenomePosition>,
    ) -> anyhow::Result<Self> {
        if let Some(end) = &end {
            if start.contig() != end.contig() {
                anyhow::bail!("Cannot create GenomeRegion spanning multiple contigs");
            }
            if start.position_0() > end.position_0() {
                anyhow::bail!("Cannot create GenomeRegion with start after end");
            }
        }
        let len = end.map(|end| end.position_0() - start.position_0());
        Ok(Self { start, len })
    }

    pub fn start(&self) -> &GenomePosition {
        &self.start
    }

    pub fn len(&self) -> Option<usize> {
        self.len
    }

    pub fn is_bounded(&self) -> bool {
        self.len.is_some()
    }

    pub fn end_excl(&self) -> Option<GenomePosition> {
        self.len.map(|l| self.start.clone() + l)
    }

    pub fn contains_pos(&self, pos: &GenomePosition) -> bool {
        if self.start.contig != pos.contig {
            return false;
        }

        if pos.position < self.start.position {
            return false;
        }

        match self.end_excl() {
            Some(end) => *pos < end,
            None => true,
        }
    }

    pub fn contains(&self, other: &GenomeRegion) -> bool {
        if self.start.contig() != other.contig() {
            return false;
        }

        self.start() <= other.start()
            && match (self.end_excl(), other.end_excl()) {
                (None, None | Some(_)) => true,
                (Some(_), None) => false,
                (Some(this), Some(other)) => this >= other,
            }
    }

    pub fn intersection(&self, other: &Self) -> Option<Self> {
        if self.contig() != other.contig() {
            return None;
        }

        let intersection_start = self.start.position_0().max(other.start.position_0());
        let intersection_end = match (self.end_excl(), other.end_excl()) {
            (None, None) => None,
            (None, Some(end)) | (Some(end), None) => Some(end.position_0()),
            (Some(self_end), Some(other_end)) => {
                Some(self_end.position_0().min(other_end.position_0()))
            }
        };

        let intersection_len = match intersection_end {
            Some(end) if end > intersection_start => Some(end - intersection_start),
            Some(_) => return None, // early return, no overlap
            None => None,           // unbounded
        };

        Some(Self {
            start: GenomePosition {
                contig: self.start.contig.clone(),
                position: intersection_start,
            },
            len: intersection_len,
        })
    }

    pub fn merge(&self, other: &Self) -> Option<Self> {
        if self.contig() != other.contig() {
            return None;
        }

        let (mut this, mut other) = (self, other);
        if self > other {
            mem::swap(&mut this, &mut other);
        }

        if let Some(this_end) = this.end_excl() {
            if &this_end < other.start() {
                None
            } else {
                let end = other.end_excl().map(|other_end| {
                    if this_end >= other_end {
                        this_end
                    } else {
                        other_end
                    }
                });
                Some(GenomeRegion::from_incl_excl(this.start().clone(), end).ok()?)
            }
        } else {
            Some(this.clone())
        }
    }

    pub fn parse(s: &[u8]) -> anyhow::Result<Self> {
        let Some(result) = parse_region_pos_fmt(s).or_else(|| parse_region_bed_fmt(s)) else {
            anyhow::bail!("Cannot parse the region '{}'", s.as_bstr());
        };
        Ok(result)
    }
}

static REGION_REGEX_POS_FORMAT: LazyLock<regex::bytes::Regex> =
    LazyLock::new(|| regex::bytes::Regex::new(r"^(\w+)(?::(\d+)-(\d+)?)?$").unwrap());

fn parse_region_pos_fmt(s: &[u8]) -> Option<GenomeRegion> {
    let captures = REGION_REGEX_POS_FORMAT.captures(s)?;
    let chr = captures.get(1).unwrap();
    let pos_1_incl = captures.get(2).and_then(extract_match).unwrap_or(1);
    let end_1_incl = if let Some(end) = captures.get(3) {
        extract_match(end)
    } else {
        None
    };

    let contig = ContigName::new(chr.as_bytes());
    let start_incl = GenomePosition::new_1(contig.clone(), pos_1_incl);
    let end_incl = end_1_incl.map(|end| GenomePosition::new_1(contig.clone(), end));
    GenomeRegion::from_incl_incl(start_incl, end_incl).ok()
}

fn extract_match(m: regex::bytes::Match) -> Option<usize> {
    let pos_1_incl: usize = m.as_bytes().to_str().ok()?.parse().ok()?;
    Some(pos_1_incl)
}

static REGION_REGEX_BED_FORMAT: LazyLock<regex::bytes::Regex> =
    LazyLock::new(|| regex::bytes::Regex::new(r"^(\w+)\t(\d+)\t(\d+)$").unwrap());

fn parse_region_bed_fmt(s: &[u8]) -> Option<GenomeRegion> {
    let captures = REGION_REGEX_BED_FORMAT.captures(s)?;
    let chr = captures.get(1).unwrap();
    let pos_0_incl: usize = extract_match(captures.get(2)?)?;
    let end_0_excl = extract_match(captures.get(3)?)?;

    let contig = ContigName::new(chr.as_bytes());
    let start_incl = GenomePosition::new_0(contig.clone(), pos_0_incl);
    let end_excl = GenomePosition::new_0(contig.clone(), end_0_excl);
    GenomeRegion::from_incl_excl(start_incl, Some(end_excl)).ok()
}

impl Display for GenomePosition {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}:{}", self.contig.as_ref(), self.position_0() + 1)
    }
}

impl Display for GenomeRegion {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        if let Some(len) = self.len() {
            let end = self.start.position_0() + len - 1;
            write!(f, "{}-{}", self.start(), end + 1)
        } else {
            write!(f, "{}-", self.start())
        }
    }
}

impl TryFrom<&rust_htslib::bcf::Record> for GenomeRegion {
    type Error = TryFromIntError;

    fn try_from(rec: &rust_htslib::bcf::Record) -> Result<Self, Self::Error> {
        let chr = rec.header().rid2name(rec.rid().unwrap()).unwrap();
        let pos = rec.pos();
        let gpos = GenomePosition::new_0(chr.into(), usize::try_from(pos)?);
        Ok(GenomeRegion::new_bounded(
            gpos,
            usize::try_from(rec.end() - pos)?,
        ))
    }
}

impl GenomeRegion {
    pub fn try_from_bam_record(
        rec: &rust_htslib::bam::Record,
        chr: ContigName,
    ) -> anyhow::Result<Self> {
        let pos = rec.pos();
        let gpos = GenomePosition::new_0(chr, pos.try_into()?);
        let len = rec.reference_end() - pos;
        Ok(GenomeRegion::new_bounded(gpos, usize::try_from(len)?))
    }
}

impl<'a> TryFrom<&'a GenomeRegion> for bam::FetchDefinition<'a> {
    type Error = anyhow::Error;

    fn try_from(value: &'a GenomeRegion) -> Result<Self, Self::Error> {
        Ok(match (value.start().position_0(), value.end_excl()) {
            (0, None) => bam::FetchDefinition::String(value.contig().as_ref()),
            (_, None) => anyhow::bail!(
                "A region like {value} cannot be used for fetching from bam files. Use regions that either have an end coordinate, or include the entire sequence (e.g. chr1, chr1:100-200)"
            ),
            (n, Some(end)) => bam::FetchDefinition::RegionString(
                value.contig().as_ref(),
                i64::try_from(n)?,
                i64::try_from(end.position_0())?,
            ),
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn pos(contig: &str, p: usize) -> GenomePosition {
        GenomePosition::new_0(ContigName::from(contig), p)
    }

    fn region(contig: &str, start: usize, len: usize) -> GenomeRegion {
        GenomeRegion::new_bounded(pos(contig, start), len)
    }

    // --- parse ---

    #[test]
    fn parse_pos_format_range() {
        // "chr1:100-200" is 1-based inclusive → 0-based [99, 200), len = 101
        let r = GenomeRegion::parse(b"chr1:100-200").unwrap();
        assert_eq!(r.start().position_0(), 99);
        assert_eq!(r.len(), Some(101));
    }

    #[test]
    fn parse_pos_format_whole_contig() {
        let r = GenomeRegion::parse(b"chr1").unwrap();
        assert_eq!(r.start().position_0(), 0);
        assert!(!r.is_bounded());
    }

    #[test]
    fn parse_pos_format_open_ended() {
        // "chr1:5-" means from position 5 to end (0-based start = 4)
        let r = GenomeRegion::parse(b"chr1:5-").unwrap();
        assert_eq!(r.start().position_0(), 4);
        assert!(!r.is_bounded());
    }

    #[test]
    fn parse_bed_format_multi_char_contig() {
        // BED format: 0-based half-open, tab-separated
        let r = GenomeRegion::parse(b"chr1\t99\t200").unwrap();
        assert_eq!(r.start().position_0(), 99);
        assert_eq!(r.len(), Some(101));
    }

    #[test]
    fn parse_invalid_returns_error() {
        assert!(GenomeRegion::parse(b"!!!invalid!!!").is_err());
    }

    // --- merge ---

    #[test]
    fn merge_overlapping_other_extends_further() {
        // [10, 20) merged with [15, 30) => [10, 30)
        let r1 = region("chr1", 10, 10);
        let r2 = region("chr1", 15, 15);
        let m = r1.merge(&r2).unwrap();
        assert_eq!(m.start().position_0(), 10);
        assert_eq!(m.len(), Some(20));
    }

    #[test]
    fn merge_overlapping_this_extends_further() {
        // [10, 35) merged with [15, 25) => [10, 35)
        let r1 = region("chr1", 10, 25);
        let r2 = region("chr1", 15, 10);
        let m = r1.merge(&r2).unwrap();
        assert_eq!(m.start().position_0(), 10);
        assert_eq!(m.len(), Some(25));
    }

    #[test]
    fn merge_adjacent_regions() {
        // [10, 20) and [20, 30) share a boundary — should merge
        let r1 = region("chr1", 10, 10);
        let r2 = region("chr1", 20, 10);
        let m = r1.merge(&r2).unwrap();
        assert_eq!(m.start().position_0(), 10);
        assert_eq!(m.len(), Some(20));
    }

    #[test]
    fn merge_no_overlap_returns_none() {
        // [10, 15) and [20, 30) — gap in between
        let r1 = region("chr1", 10, 5);
        let r2 = region("chr1", 20, 10);
        assert!(r1.merge(&r2).is_none());
    }

    #[test]
    fn merge_different_contigs_returns_none() {
        let r1 = region("chr1", 10, 10);
        let r2 = region("chr2", 10, 10);
        assert!(r1.merge(&r2).is_none());
    }

    #[test]
    fn merge_is_commutative() {
        let r1 = region("chr1", 10, 10);
        let r2 = region("chr1", 15, 20);
        assert_eq!(r1.merge(&r2), r2.merge(&r1));
    }
}