@@ -1558,24 +1558,14 @@ def _run_one_phylogeny(
15581558 for i in range (0 , len (seq ), line_len ):
15591559 out .write (seq [i : i + line_len ] + "\n " )
15601560 for sid , seq in part1_seqs .items ():
1561- # Tract FASTA headers include coverage suffix like
1562- # "<safe_sample_id>_<ref1_label>_tract_HC_<cov>%". Recover the base
1563- # sample ID so that phylogeny tract IDs can be mapped back to the
1564- # per-genome records by stripping the coverage part.
1565- base_id = sid
1566- cov_suffix = f"_{ ref1_label } _tract_HC_"
1567- if cov_suffix in base_id :
1568- base_id = base_id .split (cov_suffix , 1 )[0 ]
1569- header = _safe_fasta_id (base_id ) + f"_{ ref1_label } _tracts"
1561+ # Use tract FASTA headers (which already encode sample, clade and coverage)
1562+ # but sanitize them for downstream tools (e.g. remove '%' characters).
1563+ header = _safe_fasta_id (sid )
15701564 out .write (f">{ header } \n " )
15711565 for i in range (0 , len (seq ), line_len ):
15721566 out .write (seq [i : i + line_len ] + "\n " )
15731567 for sid , seq in part2_seqs .items ():
1574- base_id = sid
1575- cov_suffix = f"_{ ref2_label } _tract_HC_"
1576- if cov_suffix in base_id :
1577- base_id = base_id .split (cov_suffix , 1 )[0 ]
1578- header = _safe_fasta_id (base_id ) + f"_{ ref2_label } _tracts"
1568+ header = _safe_fasta_id (sid )
15791569 out .write (f">{ header } \n " )
15801570 for i in range (0 , len (seq ), line_len ):
15811571 out .write (seq [i : i + line_len ] + "\n " )
@@ -1620,9 +1610,11 @@ def _outbreak_label_from_header(h: str) -> str:
16201610 return "Ib"
16211611 return "other"
16221612
1623- ref_ids = [hid for hid in aln_dict .keys () if not hid .endswith (f"_{ ref1_label } _tracts" ) and not hid .endswith (f"_{ ref2_label } _tracts" )]
1624- tract1_ids = [hid for hid in aln_dict .keys () if hid .endswith (f"_{ ref1_label } _tracts" )]
1625- tract2_ids = [hid for hid in aln_dict .keys () if hid .endswith (f"_{ ref2_label } _tracts" )]
1613+ # Partition alignment IDs into references vs tract sequences for ref1/ref2.
1614+ tract1_ids = [hid for hid in aln_dict .keys () if f"_{ ref1_label } _tract_HC_" in hid ]
1615+ tract2_ids = [hid for hid in aln_dict .keys () if f"_{ ref2_label } _tract_HC_" in hid ]
1616+ tract_ids = set (tract1_ids ) | set (tract2_ids )
1617+ ref_ids = [hid for hid in aln_dict .keys () if hid not in tract_ids ]
16261618
16271619 ref_outbreak = {rid : _outbreak_label_from_header (rid ) for rid in ref_ids }
16281620
@@ -1657,8 +1649,10 @@ def _nearest_outbreak(seq_id: str) -> str:
16571649 args ._phylogeny_ancestors = {}
16581650
16591651 def _sample_key_from_tract (hid : str , label : str ) -> str :
1660- suffix = f"_{ label } _tracts"
1661- return hid [: - len (suffix )] if hid .endswith (suffix ) else hid
1652+ # Tract IDs contain coverage like "<safe_id>_<label>_tract_HC_<cov>".
1653+ # Recover the base sample ID by stripping the coverage suffix.
1654+ cov_suffix = f"_{ label } _tract_HC_"
1655+ return hid .split (cov_suffix , 1 )[0 ] if cov_suffix in hid else hid
16621656
16631657 for hid in tract1_ids :
16641658 key = _sample_key_from_tract (hid , ref1_label )
@@ -1799,8 +1793,8 @@ def _nearest_outbreak_tree(seq_id: str) -> Optional[str]:
17991793 return best_label
18001794
18011795 def _sample_key_from_tract (hid : str , label : str ) -> str :
1802- suffix = f"_{ label } _tracts "
1803- return hid [: - len ( suffix ) ] if hid . endswith ( suffix ) else hid
1796+ cov_suffix = f"_{ label } _tract_HC_ "
1797+ return hid . split ( cov_suffix , 1 )[ 0 ] if cov_suffix in hid else hid
18041798
18051799 for hid in tract1_ids :
18061800 key = _sample_key_from_tract (hid , ref1_label )
0 commit comments