Skip to main content

microBioRust_seqmetrics/
hamming.rs

1#[allow(unused_imports)]
2
3//function to calculate the hamming distance between 2 sequences
4pub fn hamming_distance(seq1: &str, seq2: &str) -> usize {
5    //sequences must be of equal length, i.e. a sequence alignment
6    assert_eq!(
7        seq1.len(),
8        seq2.len(),
9        "Sequences must be of equal length for Hamming distance"
10    );
11    seq1.as_bytes()
12        .iter()
13        .zip(seq2.as_bytes())
14        .filter(|(a, b)| a != b)
15        .count()
16}
17
18pub async fn hamming_matrix(sequences: &Vec<String>) -> Result<Vec<Vec<usize>>, anyhow::Error> {
19    assert!(
20        sequences.windows(2).all(|w| w[0].len() == w[1].len()),
21        "all sequences must be the same length, i.e an alignment"
22    );
23    println!("all sequences are the same length, proceeding");
24    let n = sequences.len();
25    let mut distances = vec![vec![0usize; n]; n];
26    for i in 0..n {
27        for j in i + 1..n {
28            let d = hamming_distance(&sequences[i], &sequences[j]);
29            distances[i][j] = d;
30            distances[j][i] = d;
31        }
32    }
33    Ok(distances)
34}