Last active
October 25, 2017 15:55
-
-
Save mtholder/a832dc3421859eda6de563c4bebfebbf to your computer and use it in GitHub Desktop.
takes 6 (non-negative) distances as arguments (in the order [d12, d13, d14, d23, d24, d34]) and returns tree (with branch lengths if the four-point condition is met).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| from __future__ import print_function | |
| import sys | |
| try: | |
| farg = [float(i) for i in sys.argv[1:]] | |
| for i in farg: | |
| assert i >= 0.0 | |
| d_12, d_13, d_14, d_23, d_24, d_34 = farg | |
| except: | |
| m = 'd_12, d_13, d_14, d_23, d_24, d_34' | |
| sys.exit('Expecting 6 (non-negative) distances in the order:\n {}\n'.format(m)) | |
| bs_12 = d_12 + d_34 | |
| bs_13 = d_13 + d_24 | |
| bs_14 = d_14 + d_23 | |
| pref_s = min(bs_12, bs_13, bs_14) | |
| if bs_12 == pref_s: | |
| tree = '(1, 2, (3, 4))' | |
| sep = min(bs_13, bs_14) - bs_12 | |
| exp_0 = abs(bs_13 - bs_14) | |
| elif bs_13 == pref_s: | |
| tree = '(1, 3, (2, 4))' | |
| sep = min(bs_12, bs_14) - bs_13 | |
| exp_0 = abs(bs_12 - bs_14) | |
| else: | |
| assert bs_14 == pref_s | |
| tree = '(1, 4, (2, 3))' | |
| sep = min(bs_12, bs_13) - bs_14 | |
| exp_0 = abs(bs_12 - bs_13) | |
| print("The preferred tree is: {}".format(tree)) | |
| print("It is preferred by a margin of {}".format(sep)) | |
| print("The difference in the 2 larged sums is expected to be 0\nIt was calculated to be {}".format(exp_0)) | |
| ROUND_TOL = 1.0e-8 | |
| if exp_0 <= ROUND_TOL: | |
| internal = sep / 2.0 | |
| if bs_12 == pref_s: | |
| treet = '(1:{fir}, 2:{sec}, (3:{thi}, 4:{fou}):{internal});' | |
| diff_fir_sec = d_13 - d_23 | |
| sec = (d_12 - diff_fir_sec) / 2.0 | |
| fir = d_12 - sec | |
| diff_thi_fou = d_13 - d_14 | |
| fou = (d_34 - diff_thi_fou) / 2.0 | |
| thi = d_34 - fou | |
| elif bs_13 == pref_s: | |
| treet = '(1:{fir}, 3:{thi}, (2:{sec}, 4:{fou}):{internal});' | |
| diff_fir_thi = d_12 - d_23 | |
| thi = (d_13 - diff_fir_thi) / 2.0 | |
| fir = d_13 - thi | |
| diff_sec_fou = d_12 - d_14 | |
| fou = (d_24 - diff_sec_fou) / 2.0 | |
| sec = d_24 - fou | |
| else: | |
| treet = '(1:{fir}, 4:{fou}, (2:{sec}, 3:{thi}):{internal});' | |
| diff_fir_fou = d_12 - d_24 | |
| fou = (d_14 - diff_fir_fou) / 2.0 | |
| fir = d_14 - fou | |
| diff_sec_thi = d_12 - d_13 | |
| thi = (d_23 - diff_sec_thi) / 2.0 | |
| sec = d_23 - thi | |
| template = "With branch lengths:\n {}".format(treet) | |
| print(template.format(fir=fir, sec=sec, internal=internal, thi=thi, fou=fou)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment