Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: marginal reconstruction needs use the most likely state, not the… #305

Merged
merged 1 commit into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 9 additions & 22 deletions packages/treetime/src/commands/ancestral/marginal_sparse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -414,10 +414,12 @@ pub fn ancestral_reconstruction_marginal_sparse(
return;
}

dbg!(&node.payload.name);
let seq: Seq = (0..n_partitions)
.flat_map(|si| {
let PartitionLikelihood { alphabet, .. } = &partitions[si];
let node_seq = &node.payload.sparse_partitions[si].seq;
let node_profile = &node.payload.sparse_partitions[si].profile;

let mut seq = if node.is_root {
node_seq.sequence.clone()
Expand All @@ -433,11 +435,6 @@ pub fn ancestral_reconstruction_marginal_sparse(
seq[m.pos()] = m.qry();
}

// Implant most likely state of variable sites
for (&pos, states) in &node.payload.sparse_partitions[si].seq.fitch.variable {
seq[pos] = states.get_one();
}

// Implant indels
for indel in &edge.indels {
if indel.deletion {
Expand All @@ -456,8 +453,10 @@ pub fn ancestral_reconstruction_marginal_sparse(
seq[r.0..r.1].fill(ambig_char);
}

for (pos, states) in &node_seq.fitch.variable {
seq[*pos] = states.get_one();
// change variable sites to their most likely state
for (pos, states) in &node_profile.variable {
dbg!(pos, &states);
seq[*pos] = alphabet.char(states.dis.argmax().unwrap());
}

node.payload.sparse_partitions[si].seq.sequence = seq.clone();
Expand Down Expand Up @@ -501,16 +500,10 @@ mod tests {

let aln = read_many_fasta_str(
indoc! {r#"
>root
ACAGCCATGTATTG--
>AB
ACATCCCTGTA-TG--
>A
ACATCGCCNNA--GAC
>B
GCATCCCTGTA-NG--
>CD
CCGGCCATGTATTG--
>C
CCGGCGATGTRTTG--
>D
Expand All @@ -522,11 +515,11 @@ mod tests {
let expected = read_many_fasta_str(
indoc! {r#"
>root
ACAGCCATGTATTG--
TCGGCGCTGTATTG--
>AB
ACATCCCTGTA-TG--
ACATCGCTGTA-TG--
>CD
CCGGCCATGTATTG--
TCGGCGGTGTATTG--
"#},
&NUC_ALPHABET,
)?
Expand Down Expand Up @@ -570,16 +563,10 @@ mod tests {

let aln = read_many_fasta_str(
indoc! {r#"
>root
ACAGCCATGTATTG--
>AB
ACATCCCTGTA-TG--
>A
ACATCGCCNNA--GAC
>B
GCATCCCTGTA-NG--
>CD
CCGGCCATGTATTG--
>C
CCGGCGATGTRTTG--
>D
Expand Down
5 changes: 1 addition & 4 deletions test_scripts/ancestral_sparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,11 +224,8 @@ def ancestral_sparse(graph: Graph) -> float:
return logLH

def tests():
aln = {"root":"ACAGCCATGTATTG--",
"AB":"ACATCCCTGTA-TG--",
"A":"ACATCGCCNNA--GAC",
aln = {"A":"ACATCGCCNNA--GAC",
"B":"GCATCCCTGTA-NG--",
"CD":"CCGGCCATGTATTG--",
"C":"CCGGCGATGTRTTG--",
"D":"TCGGCCGTGTRTTG--"}

Expand Down