granges 0.2.2

A Rust library and command line tool for genomic range operations.
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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
//! [`LeftGroupedJoin`], [`JoinData`], and [`JoinDataIterator`] types for overlaps.
//!
#![allow(clippy::all)]

use std::collections::HashSet;

use crate::{
    traits::{GenericRange, IndexedDataContainer, JoinDataOperations},
    Position,
};

/// This is a generic range used just in join logic, to avoid
/// having to handle bringing range types into [`LeftGroupedJoin`],
/// which clog up the API a bit.
/// These can represent indexed and empty ranges.
#[derive(Clone, Debug, PartialEq)]
pub struct RangeTuple((Position, Position, Option<usize>));

impl GenericRange for RangeTuple {
    fn start(&self) -> Position {
        self.0 .0
    }
    fn end(&self) -> Position {
        self.0 .1
    }
    fn index(&self) -> Option<usize> {
        self.0 .2
    }
}

/// This is a special "reduced" range that stores indices
/// to multiple data elements.
#[derive(Clone, Debug, PartialEq)]
pub struct RangeReduced((Position, Position, HashSet<Option<usize>>));

impl RangeReduced {
    pub fn indices(&self) -> &HashSet<Option<usize>> {
        &self.0 .2
    }
}

/// Create a vector of "reduced" or flattened ranges, that stack the indices of each range.
///
///
/// This first finds every range position, sorts, and dedups these. Then,
/// it iterates through each range of adjacent positions. Each of these
/// new ranges is guaranteed to be covered by >= 1 range.
pub fn reduce_ranges<R: GenericRange>(ranges: &Vec<R>) -> Vec<RangeReduced> {
    let mut range_ends: Vec<Position> = ranges
        .iter()
        .flat_map(|range| vec![range.start(), range.end()].into_iter())
        .collect();
    range_ends.sort_unstable();
    range_ends.dedup();

    let mut ranges_reduced = Vec::new();
    for range in range_ends.windows(2) {
        let mut indices = HashSet::new();
        if let [start, end] = range {
            for range in ranges {
                if range.start() < *end && range.end() > *start {
                    indices.insert(range.index());
                }
            }
            if !indices.is_empty() {
                ranges_reduced.push(RangeReduced((*start, *end, indices)));
            }
        }
    }
    ranges_reduced
}

impl GenericRange for RangeReduced {
    fn start(&self) -> Position {
        self.0 .0
    }
    fn end(&self) -> Position {
        self.0 .1
    }
    // Note: [`RangeReduced`] do not have valid indices,
    // so this returns [`None`]. (They do have a [`Vec<Option<usize>>`]
    // of indices, but this has to be accessed through the type.)
    fn index(&self) -> Option<usize> {
        None
    }
}

/// [`LeftGroupedJoin`] contains information about the right ranges
/// and their degree of overlap with a focal left range. This information
/// is designed to facilitate downstream statistical sumamries of the
/// corresponding data in overlapping ranges.
#[derive(Clone, Debug, PartialEq)]
pub struct LeftGroupedJoin {
    /// The left range.
    pub left: RangeTuple,

    /// A `Vec` of the right overlapping ranges (unsorted).
    // NOTE: previously lengths, overlap width, and their data
    // indices were stored. For just one extra u32 we can store
    // all the data in the original structure.
    pub rights: Vec<RangeTuple>,
}

impl LeftGroupedJoin {
    /// Create a new [`LeftGroupedJoin`].
    pub fn new<R: GenericRange>(left_range: &R) -> Self {
        Self {
            left: RangeTuple(left_range.as_tuple()),
            rights: Vec::new(),
        }
    }
    /// Add a right (overlapping) range to this [`LeftGroupedJoin`].
    ///
    // Note: in principle, this can be called on *non-overlapping* right ranges too,
    // for a full-outer join.
    pub fn add_right<R: GenericRange>(&mut self, right: &R) {
        self.rights.push(RangeTuple(right.as_tuple()))
    }

    /// Sort the right ranges, for faster downstream processing.
    pub fn sort_ranges(&mut self) {
        self.rights.sort_by(|a, b| {
            a.start()
                .cmp(&b.start())
                .then_with(|| a.end().cmp(&b.end()))
                .then_with(|| a.index().cmp(&b.index()))
        });
    }

    /// "Reduce" the ranges into a minimum set, with all
    /// their indices gathered in a [`Vec<Option<usize>>`].
    /// This returns a [`Vec<RangeReduced>`].
    pub fn reduce_ranges(&mut self) -> Vec<RangeReduced> {
        // we need to trim these by the left range to get the
        // proper overlaps within this left range
        let rights: Vec<_> = self
            .rights
            .iter()
            .map(|range| {
                let (start, end) = range.overlap_range(&self.left).unwrap();
                RangeTuple((start, end, range.index()))
            })
            .collect();
        reduce_ranges(&rights)
    }

    /// Return whether this left range has any [`LeftGroupedJoin`].
    pub fn has_overlaps(&self) -> bool {
        !self.overlaps().is_empty()
    }

    /// Retrieve the number of right overlaps.
    pub fn num_overlaps(&self) -> usize {
        self.overlaps().len()
    }

    /// Retrieve the right overlaps.
    pub fn overlaps(&self) -> Vec<Position> {
        self.rights
            .iter()
            .map(|r| r.overlap_width(&self.left))
            .collect()
    }

    /// Get the left index.
    pub fn left_index(&self) -> Option<usize> {
        self.left.index()
    }

    /// Get the right indices.
    pub fn right_indices(&self) -> Vec<Option<usize>> {
        self.rights.iter().map(|r| r.index()).collect()
    }
}

/// [`JoinData`] contains a [`Vec<LeftGroupedJoin>`] of all overlap joins,
/// and owns the left data container from the join. It stores a reference
/// to the right data container.
#[derive(Clone, Debug)]
pub struct JoinData<'a, DL, DR> {
    pub joins: Vec<LeftGroupedJoin>,
    pub left_data: DL,
    pub right_data: &'a DR,
}

impl<'a, DL, DR> JoinData<'a, DL, DR> {
    /// Create a new [`JoinData`].
    pub fn new(left_data: DL, right_data: &'a DR) -> Self {
        let joins = Vec::new();
        JoinData {
            joins,
            left_data,
            right_data,
        }
    }

    /// Push the [`LeftGroupedJoin`] to joins.
    pub fn push(&mut self, join: LeftGroupedJoin) {
        self.joins.push(join)
    }

    /// Get the total number of joins.
    pub fn len(&self) -> usize {
        self.joins.len()
    }

    /// Return whether the [`JoinData`] object is empty (contains no ranges).
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Create an iterator over the joins.
    pub fn iter(&'a self) -> JoinDataIterator<'a, DL, DR> {
        JoinDataIterator {
            inner: self.joins.iter(),
            left_data: Some(&self.left_data),
            right_data: Some(self.right_data),
        }
    }
}

pub struct CombinedJoinData<DL, DR> {
    pub join: LeftGroupedJoin, // Information on the join
    pub left_data: DL,         // The left data element
    pub right_data: Vec<DR>,   // The right data elements
}

impl<DL, DR> JoinDataOperations<DL, DR> for CombinedJoinData<DL, DR> {
    type LeftDataElementType = DL;
    type RightDataElementType = DR;
    fn join(&self) -> &LeftGroupedJoin {
        &self.join
    }
    fn left_data(&self) -> Option<&Self::LeftDataElementType> {
        Some(&self.left_data)
    }
    fn right_data(&self) -> Option<&Vec<Self::RightDataElementType>> {
        Some(&self.right_data)
    }
}

impl<'a, DL, DR> JoinData<'a, DL, DR>
where
    DL: IndexedDataContainer + 'a,
    DR: IndexedDataContainer + 'a,
{
    /// Apply `func` to each element, putting the results into the returned
    /// `Vec<U>`.
    pub fn map<F, V>(self, func: F) -> Vec<V>
    where
        F: Fn(
            CombinedJoinData<
                <DL as IndexedDataContainer>::OwnedItem,
                <DR as IndexedDataContainer>::OwnedItem,
            >,
        ) -> V,
    {
        self.joins
            .into_iter()
            .map(|join| {
                let left_data = self.left_data.get_owned(join.left_index().unwrap());
                let right_data = join
                    .right_indices()
                    .iter()
                    .map(|idx| self.right_data.get_owned(idx.unwrap()))
                    .collect();

                func(CombinedJoinData {
                    join,
                    left_data,
                    right_data,
                })
            })
            .collect()
    }
}

/// [`JoinDataLeftEmpty`] contains a [`Vec<LeftGroupedJoin>`] of all overlap joins,
/// and stores a reference to the right data container.
#[derive(Clone, Debug)]
pub struct JoinDataLeftEmpty<'a, DR> {
    pub joins: Vec<LeftGroupedJoin>,
    pub right_data: &'a DR,
}

impl<'a, DR> JoinDataLeftEmpty<'a, DR> {
    /// Create a new [`JoinData`].
    pub fn new(right_data: &'a DR) -> Self {
        let joins = Vec::new();
        JoinDataLeftEmpty { joins, right_data }
    }

    /// Push the [`LeftGroupedJoin`] to joins.
    pub fn push(&mut self, join: LeftGroupedJoin) {
        self.joins.push(join)
    }

    /// Get the total number of joins.
    pub fn len(&self) -> usize {
        self.joins.len()
    }

    /// Return whether the [`JoinData`] object is empty (contains no ranges).
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Create an iterator over the joins.
    pub fn iter(&'a self) -> JoinDataIterator<'a, (), DR> {
        JoinDataIterator {
            inner: self.joins.iter(),
            left_data: None,
            right_data: Some(self.right_data),
        }
    }
}

pub struct CombinedJoinDataLeftEmpty<DR> {
    pub join: LeftGroupedJoin, // Information on the join
    pub right_data: Vec<DR>,   // The right data element
}

impl<DR> JoinDataOperations<(), DR> for CombinedJoinDataLeftEmpty<DR> {
    type LeftDataElementType = ();
    type RightDataElementType = DR;
    fn join(&self) -> &LeftGroupedJoin {
        &self.join
    }
    fn left_data(&self) -> Option<&Self::LeftDataElementType> {
        None
    }
    fn right_data(&self) -> Option<&Vec<Self::RightDataElementType>> {
        Some(&self.right_data)
    }
}

impl<'a, DR> JoinDataLeftEmpty<'a, DR>
where
    DR: IndexedDataContainer + 'a,
{
    /// Apply `func` to each element, putting the results into the returned
    /// `Vec<U>`.
    pub fn map<F, V>(self, func: F) -> Vec<V>
    where
        F: Fn(CombinedJoinDataLeftEmpty<<DR as IndexedDataContainer>::OwnedItem>) -> V,
    {
        // TODO/OPTIMIZE: would consuming here (and analagous funcs) be better/faster?
        // Would require a bit of a redesign.
        self.joins
            .into_iter()
            .map(|join| {
                let right_indices = join.right_indices();
                let right_data = right_indices
                    .iter()
                    .map(|idx| self.right_data.get_owned(idx.unwrap()))
                    .collect();

                func(CombinedJoinDataLeftEmpty { join, right_data })
            })
            .collect()
    }
}

/// [`JoinDataRightEmpty`] contains a [`Vec<LeftGroupedJoin>`] of all overlap joins,
/// and owns the left data.
#[derive(Clone, Debug)]
pub struct JoinDataRightEmpty<DR> {
    pub joins: Vec<LeftGroupedJoin>,
    pub left_data: DR,
}

impl<'a, DL> JoinDataRightEmpty<DL> {
    /// Create a new [`JoinData`].
    pub fn new(left_data: DL) -> Self {
        let joins = Vec::new();
        JoinDataRightEmpty { joins, left_data }
    }

    /// Push the [`LeftGroupedJoin`] to joins.
    pub fn push(&mut self, join: LeftGroupedJoin) {
        self.joins.push(join)
    }

    /// Get the total number of joins.
    pub fn len(&self) -> usize {
        self.joins.len()
    }

    /// Return whether the [`JoinData`] object is empty (contains no ranges).
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Create an iterator over the joins.
    pub fn iter(&'a self) -> JoinDataIterator<'a, DL, ()> {
        JoinDataIterator {
            inner: self.joins.iter(),
            left_data: Some(&self.left_data),
            right_data: None,
        }
    }
}

pub struct CombinedJoinDataRightEmpty<DL> {
    pub join: LeftGroupedJoin, // Information on the join
    pub left_data: DL,         // The right data element
}

impl<DL> JoinDataOperations<DL, ()> for CombinedJoinDataRightEmpty<DL> {
    type LeftDataElementType = DL;
    type RightDataElementType = ();
    fn join(&self) -> &LeftGroupedJoin {
        &self.join
    }
    fn left_data(&self) -> Option<&Self::LeftDataElementType> {
        Some(&self.left_data)
    }
    fn right_data(&self) -> Option<&Vec<Self::RightDataElementType>> {
        None
    }
}

impl<'a, DL> JoinDataRightEmpty<DL>
where
    DL: IndexedDataContainer,
{
    /// Apply `func` to each element, putting the results into the returned
    /// `Vec<U>`.
    pub fn map<F, V>(self, func: F) -> Vec<V>
    where
        F: Fn(CombinedJoinDataRightEmpty<<DL as IndexedDataContainer>::OwnedItem>) -> V,
    {
        self.joins
            .into_iter()
            .map(|join| {
                let left_data = self.left_data.get_owned(join.left_index().unwrap());

                func(CombinedJoinDataRightEmpty { join, left_data })
            })
            .collect()
    }
}

/// [`JoinDataBothEmpty`] contains a [`Vec<LeftGroupedJoin>`] of all overlap joins
/// without any owned or references to data containers.
#[derive(Clone, Debug)]
pub struct JoinDataBothEmpty {
    pub joins: Vec<LeftGroupedJoin>,
}

impl JoinDataBothEmpty {
    /// Create a new [`JoinData`].
    pub fn new() -> Self {
        let joins = Vec::new();
        JoinDataBothEmpty { joins }
    }

    /// Push the [`LeftGroupedJoin`] to joins.
    pub fn push(&mut self, join: LeftGroupedJoin) {
        self.joins.push(join)
    }

    /// Get the total number of joins.
    pub fn len(&self) -> usize {
        self.joins.len()
    }

    /// Return whether the [`JoinData`] object is empty (contains no ranges).
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Create an iterator over the joins.
    pub fn iter(&self) -> JoinDataIterator<'_, (), ()> {
        JoinDataIterator {
            inner: self.joins.iter(),
            left_data: None,
            right_data: None,
        }
    }
}

pub struct CombinedJoinDataBothEmpty {
    pub join: LeftGroupedJoin,
}

impl JoinDataOperations<(), ()> for CombinedJoinDataBothEmpty {
    type LeftDataElementType = ();
    type RightDataElementType = ();
    fn join(&self) -> &LeftGroupedJoin {
        &self.join
    }
    fn left_data(&self) -> Option<&Self::LeftDataElementType> {
        None
    }
    fn right_data(&self) -> Option<&Vec<Self::RightDataElementType>> {
        None
    }
}

impl JoinDataBothEmpty {
    /// Apply `func` to each element, putting the results into the returned
    /// `Vec<U>`.
    pub fn map<F, V>(self, func: F) -> Vec<V>
    where
        F: Fn(CombinedJoinDataBothEmpty) -> V,
    {
        self.joins
            .into_iter()
            .map(|join| func(CombinedJoinDataBothEmpty { join }))
            .collect()
    }
}

/// An iterator over the [`LeftGroupedJoin`] types that represent
/// information about overlaps right ranges have with a particular left range.
///
/// This also contains references to the left and right data containers, for
/// better ergonomics in downstream data processing.
pub struct JoinDataIterator<'a, DL, DR> {
    inner: std::slice::Iter<'a, LeftGroupedJoin>,
    pub left_data: Option<&'a DL>,
    pub right_data: Option<&'a DR>,
}

impl<'a, DL, DR> Iterator for JoinDataIterator<'a, DL, DR> {
    type Item = &'a LeftGroupedJoin;

    fn next(&mut self) -> Option<Self::Item> {
        self.inner.next()
    }
}

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

    #[test]
    fn test_join_data_new() {
        let left_data = vec![1, 2];
        let right_data = vec![4, 8];
        let mut jd = JoinData::new(left_data, &right_data);
        assert_eq!(jd.len(), 0);

        let left = RangeIndexed::new(0, 10, 1);
        let mut join = LeftGroupedJoin::new(&left);
        let right = RangeIndexed::new(8, 10, 1);
        join.add_right(&right);
        jd.push(join);
        assert_eq!(jd.len(), 1);
    }

    #[test]
    fn test_single_range_indexed() {
        let ranges = vec![RangeIndexed {
            start: 1,
            end: 5,
            index: 0,
        }];
        let reduced = reduce_ranges(&ranges);

        assert_eq!(reduced.len(), 1);
        assert_eq!(reduced[0].0 .0, 1);
        assert_eq!(reduced[0].0 .1, 5);
        assert!(reduced[0].0 .2.contains(&Some(0)));
    }

    #[test]
    fn test_overlapping_ranges_indexed() {
        let ranges = vec![
            RangeIndexed {
                start: 1,
                end: 4,
                index: 0,
            },
            RangeIndexed {
                start: 3,
                end: 6,
                index: 1,
            },
        ];
        let reduced = reduce_ranges(&ranges);

        assert_eq!(reduced.len(), 3);

        let mut iter = reduced.iter();
        let first_range = iter.next().unwrap();
        assert_eq!(first_range.start(), 1);
        assert_eq!(first_range.end(), 3);
        assert_eq!(first_range.indices().len(), 1);
        assert!(first_range.indices().contains(&Some(0)));

        let second_range = iter.next().unwrap();
        assert_eq!(second_range.start(), 3);
        assert_eq!(second_range.end(), 4);
        assert_eq!(second_range.indices().len(), 2);
        assert!(second_range.indices().contains(&Some(0)));
        assert!(second_range.indices().contains(&Some(1)));

        let third_range = iter.next().unwrap();
        assert_eq!(third_range.start(), 4);
        assert_eq!(third_range.end(), 6);
        assert_eq!(third_range.indices().len(), 1);
        assert!(third_range.indices().contains(&Some(1)));
    }

    #[test]
    fn test_non_overlapping_ranges_indexed() {
        let ranges = vec![
            RangeIndexed {
                start: 1,
                end: 3,
                index: 0,
            },
            RangeIndexed {
                start: 4,
                end: 6,
                index: 1,
            },
        ];
        let reduced = reduce_ranges(&ranges);

        assert_eq!(reduced.len(), 2);

        let mut iter = reduced.iter();
        let first_range = iter.next().unwrap();
        assert_eq!(first_range.start(), 1);
        assert_eq!(first_range.end(), 3);
        assert_eq!(first_range.indices().len(), 1);
        assert!(first_range.indices().contains(&Some(0)));

        let second_range = iter.next().unwrap();
        assert_eq!(second_range.start(), 4);
        assert_eq!(second_range.end(), 6);
        assert_eq!(second_range.indices().len(), 1);
        assert!(second_range.indices().contains(&Some(1)));
    }
}