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
/// Command line interface for fibertools-rs.
pub mod cli;

pub mod extract;

use rust_htslib::{bam, bam::ext::BamRecordExtensions};
use std::fmt::Display;

/// get positions on the complimented sequence in the cigar record
pub fn positions_on_complimented_sequence(
    record: &bam::Record,
    input_positions: &[i64],
) -> Vec<i64> {
    // reverse positions if needed
    let positions: Vec<i64> = if record.is_reverse() {
        let seq_len = i64::try_from(record.seq_len()).unwrap();
        input_positions.iter().rev().map(|p| seq_len - p).collect()
    } else {
        input_positions.to_vec()
    };
    positions
}

/// search a sorted array for insertions positions of another sorted array
/// returned index i satisfies
/// left
/// a[i-1] < v <= a[i]
/// right
/// a[i-1] <= v < a[i]
/// https://numpy.org/doc/stable/reference/generated/numpy.searchsorted.html
/// ```
/// use fibertools_rs::*;
/// let a = vec![1, 2, 3, 5, 6, 7, 8, 9, 10];
/// let v = vec![0, 1, 3, 4, 11, 11];
/// let indexes = search_sorted(&a, &v);
/// assert_eq!(indexes, vec![0, 0, 2, 3, 9, 9]);
/// ```
pub fn search_sorted<T>(a: &Vec<T>, v: &Vec<T>) -> Vec<usize>
where
    T: Ord,
    T: Display,
{
    let mut indexes = Vec::with_capacity(v.len());
    let mut a_idx = 0;
    for cur_v in v {
        while a_idx < a.len() {
            // check starting condition
            if a_idx == 0 && *cur_v <= a[a_idx] {
                indexes.push(0);
                break;
            } else if a_idx == 0 {
                a_idx += 1;
            }
            // end condition
            if a_idx == a.len() - 1 && *cur_v > a[a_idx] {
                indexes.push(a_idx + 1);
                break;
            }
            // middle of the array
            else if (a[a_idx - 1] < *cur_v) && (*cur_v <= a[a_idx]) {
                indexes.push(a_idx);
                break;
            }
            a_idx += 1;
        }
        //println!("{} {}\t{} {}", a_idx, v_idx, a[a_idx], v[v_idx]);
    }
    indexes
}

///```
/// use rust_htslib::{bam, bam::Read};
/// use fibertools_rs::*;
/// use log;
/// use env_logger::{Builder, Target};;
/// Builder::new().target(Target::Stderr).filter(None, log::LevelFilter::Debug).init();
/// let mut bam = bam::Reader::from_path(&".test/aligned.bam").unwrap();
/// for record in bam.records() {
///     let record = record.unwrap();
///     let seq_len = i64::try_from(record.seq_len()).unwrap();
///     let positions: Vec<i64> = (0..seq_len).collect();
///     liftover_closest(&record, &positions);    
/// }
///```
pub fn liftover_closest(record: &bam::Record, positions: &Vec<i64>) -> Vec<i64> {
    // find the shared positions in the reference
    let mut ref_positions = vec![];
    let (_q_pos, r_pos): (Vec<i64>, Vec<i64>) = record
        .aligned_pairs()
        .map(|[q_pos, r_pos]| (q_pos, r_pos))
        .unzip();
    let ref_idxs = search_sorted(&r_pos, positions);
    for mut idx in ref_idxs {
        // if we map past the end of the reference take the last reference position
        if idx == r_pos.len() {
            idx -= 1;
        }
        ref_positions.push(r_pos[idx]);
    }
    ref_positions
}

/// liftover positions using the cigar string
pub fn liftover_exact(record: &bam::Record, positions: &Vec<i64>) -> Vec<i64> {
    // find the shared positions in the reference
    let mut ref_positions = vec![];
    let mut cur_pos = 0;
    for [q_pos, r_pos] in record.aligned_pairs() {
        while cur_pos < positions.len() && positions[cur_pos] <= q_pos {
            if positions[cur_pos] == q_pos {
                log::trace!("Found position: q_pos:{}, r_pos:{}", q_pos, r_pos);
                ref_positions.push(r_pos);
            }
            cur_pos += 1;
        }
        if cur_pos == positions.len() {
            break;
        }
    }
    ref_positions
}

pub fn get_closest_reference_positions(positions: &[i64], record: &bam::Record) -> Vec<i64> {
    let positions = positions_on_complimented_sequence(record, positions);
    // get the reference positions
    liftover_closest(record, &positions)
}

pub fn get_closest_reference_range(
    starts: &[i64],
    lengths: &[i64],
    record: &bam::Record,
) -> Vec<(i64, i64)> {
    let starts = get_closest_reference_positions(starts, record);
    let mol_ends: Vec<i64> = starts
        .iter()
        .zip(lengths.iter())
        .map(|(start, length)| start + length)
        .collect();
    let ends = get_closest_reference_positions(&mol_ends, record);

    starts
        .iter()
        .zip(ends.iter())
        .filter(|(&start, &end)| end - start > 0) // filter out zero length ranges, basically means there is no liftover
        .map(|(&start, &end)| (start, end - start))
        .collect()
}