Skip to content

Commit

Permalink
fix branch lengths in conversion scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
ktmeaton committed Apr 27, 2021
1 parent 8fba8a6 commit c309726
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 18 deletions.
2 changes: 1 addition & 1 deletion workflow/rules/phylogeny.smk
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,6 @@ rule beast:
cut -f 1,21,22 {input.tsv} > {output.latlon};
cp {input.aln} {output.aln};
cp {input.divtree} {output.divtree};
cp {input.timetree} {output.timetree};
python3 {scripts_dir}/nexus2newick.py {input.timetree} {output.timetree};
cp {input.constant_sites} {output.constant_sites};
"""
18 changes: 17 additions & 1 deletion workflow/scripts/newick2nexus.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,20 @@
tree_output = sys.argv[2]

tree = Phylo.read(tree_input, "newick")
Phylo.write(tree, tree_output, "nexus")

# Get minimum branch length for output precision
min_branch_length = 1e10

for c in tree.find_clades():
# Remove comments
c.comment = None
if c.branch_length and c.branch_length < min_branch_length:
min_branch_length = c.branch_length

# This is a terrible way to get sig digits
sig_dig = 5
# If we're using scientific notation, it's really small
if "e-" in str(min_branch_length):
sig_dig = int(str(min_branch_length).split("e-")[-1]) + 2

Phylo.write(tree, tree_output, "nexus", format_branch_length="%1.{}f".format(sig_dig))
14 changes: 0 additions & 14 deletions workflow/scripts/nexus2beast.py

This file was deleted.

17 changes: 15 additions & 2 deletions workflow/scripts/nexus2newick.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,21 @@

tree = Phylo.read(tree_input, "nexus")

# Remove comments
# Get minimum branch length for output precision
min_branch_length = 1e10

for c in tree.find_clades():
# Remove comments
c.comment = None
if c.branch_length and c.branch_length < min_branch_length:
min_branch_length = c.branch_length

# This is a terrible way to get sig digits
sig_dig = 5
# If we're using scientific notation, it's really small
if "e-" in str(min_branch_length):
sig_dig = int(str(min_branch_length).split("e-")[-1]) + 2

Phylo.write(tree, tree_output, "newick")
Phylo.write(
tree, tree_output, "newick", format_branch_length="%1.{}f".format(sig_dig),
)

0 comments on commit c309726

Please sign in to comment.