Skip to content

Commit

Permalink
rewrote function to combine genetic lists to take references and not …
Browse files Browse the repository at this point in the history
…destroy previous lists
  • Loading branch information
jhellewell14 committed Jul 20, 2023
1 parent 1a7d92f commit ef1720a
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 48 deletions.
89 changes: 55 additions & 34 deletions src/gen_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,45 +41,66 @@ pub fn create_list(refseq: &[char], seq: &[char]) -> Vec<Mutation> {
}

// Combines two vectors of Mutations into a single vector
pub fn combine_lists(seq1: &mut Vec<Mutation>, seq2: &mut Vec<Mutation>) -> Vec<Mutation> {
pub fn combine_lists(seq1: Option<&Vec<Mutation>>, seq2: Option<&Vec<Mutation>>) -> Vec<Mutation> {
let mut out: Vec<Mutation> = 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
}

Expand Down
20 changes: 6 additions & 14 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<char> = record2.seq().iter().map(|l| *l as char).collect();

// let record3 = reader.next().unwrap().unwrap();
// let seq3: Vec<char> = record3.seq().iter().map(|l| *l as char).collect();

// let mut out: Vec<Mutation> = create_list(&seq_vec, &seq2);
// let mut out2: Vec<Mutation> = create_list(&seq_vec, &seq3);

// let mut lists: Vec<&mut Vec<Mutation>> = 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);

Expand Down

0 comments on commit ef1720a

Please sign in to comment.