deepbiop_utils/
lib.rs

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
//! **deepbiop-utils** is a collection of utility functions.

pub mod blat;
pub mod interval;
pub mod io;
pub mod strategy;
pub mod sv;

#[cfg(feature = "python")]
pub mod python;

use colored::Colorize;
use pyo3::prelude::*;

use pyo3_stub_gen::derive::*;

#[gen_stub_pyfunction(module = "deepbiop.utils")]
#[pyfunction]
#[pyo3(signature = (sequence, targets, text_width=None))]
pub fn highlight_targets(
    sequence: &str,
    targets: Vec<(usize, usize)>,
    text_width: Option<usize>,
) -> String {
    let mut highlighted = String::new();
    let mut last_end = 0;
    for (start, end) in targets {
        highlighted.push_str(&sequence[last_end..start]);
        highlighted.push_str(&sequence[start..end].magenta().bold().to_string());
        last_end = end;
    }
    highlighted.push_str(&sequence[last_end..]);
    let text_width = text_width.unwrap_or(80);
    let chunked = textwrap::wrap(&highlighted, text_width);
    chunked.join("\n")
}

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

    #[test]
    fn test_highlight() {
        let sequence = "ATGCACTGACTGACATGCACTGACTGAC";

        let hstr = highlight_targets(sequence, vec![(0, 3), (10, 13)], None);

        println!("{}", hstr);
    }
}