From ef1720abdb25245b472e38195e0451dff606234a Mon Sep 17 00:00:00 2001 From: Joel Hellewell Date: Thu, 20 Jul 2023 09:33:02 +0100 Subject: [PATCH] rewrote function to combine genetic lists to take references and not destroy previous lists --- src/gen_list.rs | 89 ++++++++++++++++++++++++++++++------------------- src/main.rs | 20 ++++------- 2 files changed, 61 insertions(+), 48 deletions(-) diff --git a/src/gen_list.rs b/src/gen_list.rs index 69e2c3a..e01280f 100644 --- a/src/gen_list.rs +++ b/src/gen_list.rs @@ -41,45 +41,66 @@ pub fn create_list(refseq: &[char], seq: &[char]) -> Vec { } // Combines two vectors of Mutations into a single vector -pub fn combine_lists(seq1: &mut Vec, seq2: &mut Vec) -> Vec { +pub fn combine_lists(seq1: Option<&Vec>, seq2: Option<&Vec>) -> Vec { let mut out: Vec = Vec::new(); - - seq1.reverse(); - seq2.reverse(); - - let mut j = seq2.pop(); - let mut i = seq1.pop(); - - while i.is_some() | j.is_some() { - if j.is_none() { - out.push(i.unwrap()); - i = seq1.pop(); - } else if i.is_none() { - out.push(j.unwrap()); - j = seq2.pop(); - } else { - let i0 = i.unwrap().0; - let j0 = j.unwrap().0; - - match i0.cmp(&j0) { - Ordering::Equal => { - // This should call another function to handle the calculation - out.push(Mutation(i0, 5.0, 5.0, 5.0, 5.0)); - i = seq1.pop(); - j = seq2.pop(); - } - Ordering::Less => { - out.push(i.unwrap()); - i = seq1.pop(); - } - Ordering::Greater => { - out.push(j.unwrap()); - j = seq2.pop(); - } + let seq1 = seq1.unwrap(); + let seq2 = seq2.unwrap(); + + // Index in each vector of mutations + let mut s1_i = seq1.len() - 1; + let mut s2_i = seq2.len() - 1; + + // Mutation at each index + let mut s1_node = seq1.get(s1_i); + let mut s2_node = seq2.get(s2_i); + + // Location of mutation in sequence + let mut s1_loc = s1_node.unwrap().0; + let mut s2_loc = s2_node.unwrap().0; + + while (s1_i < 0) | (s2_i > 0) { + match s1_loc.cmp(&s2_loc) { + Ordering::Equal => { + out.push(Mutation(s1_loc, 5.0, 5.0, 5.0, 5.0)); + + s1_i -= 1; + s1_node = seq1.get(s1_i); + s1_loc = s1_node.unwrap().0; + + s2_i -= 1; + s2_node = seq2.get(s2_i); + s2_loc = s2_node.unwrap().0; + }, + Ordering::Greater => { + out.push(*s1_node.unwrap()); + s1_i -= 1; + s1_node = seq1.get(s1_i); + s1_loc = s1_node.unwrap().0; + }, + Ordering::Less => { + out.push(*s2_node.unwrap()); + s2_i -= 1; + s2_node = seq2.get(s2_i); + s2_loc = s2_node.unwrap().0; } } } + // Push last entries + match s1_loc.cmp(&s2_loc) { + Ordering::Equal => { + out.push(Mutation(s1_loc, 5.0, 5.0, 5.0, 5.0));}, + Ordering::Greater => { + out.push(*s1_node.unwrap()); + out.push(*s2_node.unwrap());}, + Ordering::Less => { + out.push(*s2_node.unwrap()); + out.push(*s1_node.unwrap()); + }, + } + + out.reverse(); + out } diff --git a/src/main.rs b/src/main.rs index 7a85f37..d991aca 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,22 +17,14 @@ fn main() { let filename = "listeria0.aln"; let mut ll = create_genetic_data(filename); - println!("{:?}", ll.likelihood_lists.len()); + // println!("{:?}", ll.likelihood_lists.get_mut()); + + let combo = combine_lists(ll.likelihood_lists.get(0), ll.likelihood_lists.get(1)); + // println!("seq1: {:?}",ll.likelihood_lists); + println!("combined seq: {:?}", combo); - // let record2 = reader.next().unwrap().unwrap(); - // let seq2: Vec = record2.seq().iter().map(|l| *l as char).collect(); - - // let record3 = reader.next().unwrap().unwrap(); - // let seq3: Vec = record3.seq().iter().map(|l| *l as char).collect(); - - // let mut out: Vec = create_list(&seq_vec, &seq2); - // let mut out2: Vec = create_list(&seq_vec, &seq3); - - // let mut lists: Vec<&mut Vec> = vec![&mut out, &mut out2]; - - // let combined_out = combine_lists(&mut out, &mut out2); - // println!("combined seq: {:?}", combined_out[0..25].to_vec()); + // println!("{:?}", ll.likelihood_lists.get(0).unwrap().get(0)); // let tr = phylo2vec_quad(vec![0, 1, 0]); // let tr2 = phylo2vec_lin(vec![0, 0, 2, 3], false);