NJ tree
The Neighbour-Joining method uses genetic distances to build a phylogenetic tree. piqtree provides only piq_jc_distances for this. cogent3 includes many more methods. The results of either can be used to build a tree. For divergent sequences we will use Lake's paralinear measure as it accomodates divergent sequence compositions.
In [1]:
Copied!
import cogent3
from piqtree import download_dataset
aln_path = download_dataset("example.phy.gz", dest_dir="data")
aln = cogent3.load_aligned_seqs(aln_path, moltype="dna", format_name="phylip")
import cogent3
from piqtree import download_dataset
aln_path = download_dataset("example.phy.gz", dest_dir="data")
aln = cogent3.load_aligned_seqs(aln_path, moltype="dna", format_name="phylip")
Getting a paralinear distance matrix¶
This can be obtained directly from the alignment object itself.
In [2]:
Copied!
dists = aln.distance_matrix(calc="paralinear")
dists
dists = aln.distance_matrix(calc="paralinear")
dists
Out[2]:
| names | LngfishAu | LngfishSA | LngfishAf | Frog | Turtle | Sphenodon | Lizard | Crocodile | Bird | Human | Seal | Cow | Whale | Mouse | Rat | Platypus | Opossum |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| LngfishAu | 0.0000 | 0.2974 | 0.2943 | 0.3090 | 0.3806 | 0.4356 | 0.4347 | 0.4429 | 0.4254 | 0.3956 | 0.3618 | 0.3885 | 0.3811 | 0.3950 | 0.3947 | 0.4123 | 0.3964 |
| LngfishSA | 0.2974 | 0.0000 | 0.2419 | 0.3630 | 0.4093 | 0.4380 | 0.4330 | 0.4362 | 0.4011 | 0.4291 | 0.4046 | 0.4214 | 0.4224 | 0.4116 | 0.4432 | 0.4184 | 0.3763 |
| LngfishAf | 0.2943 | 0.2419 | 0.0000 | 0.3619 | 0.4013 | 0.4595 | 0.4710 | 0.4734 | 0.4374 | 0.4558 | 0.4220 | 0.4275 | 0.4234 | 0.4324 | 0.4617 | 0.4332 | 0.3975 |
| Frog | 0.3090 | 0.3630 | 0.3619 | 0.0000 | 0.3704 | 0.4578 | 0.4300 | 0.4455 | 0.4133 | 0.4038 | 0.3788 | 0.3770 | 0.3922 | 0.4081 | 0.4110 | 0.3956 | 0.3751 |
| Turtle | 0.3806 | 0.4093 | 0.4013 | 0.3704 | 0.0000 | 0.3480 | 0.3542 | 0.3578 | 0.3225 | 0.3630 | 0.3425 | 0.3389 | 0.3429 | 0.3448 | 0.3649 | 0.3609 | 0.3473 |
| Sphenodon | 0.4356 | 0.4380 | 0.4595 | 0.4578 | 0.3480 | 0.0000 | 0.3845 | 0.4014 | 0.3759 | 0.4009 | 0.3991 | 0.3924 | 0.3944 | 0.3923 | 0.4210 | 0.4176 | 0.3793 |
| Lizard | 0.4347 | 0.4330 | 0.4710 | 0.4300 | 0.3542 | 0.3845 | 0.0000 | 0.4243 | 0.3799 | 0.4058 | 0.3947 | 0.4036 | 0.4209 | 0.3985 | 0.4337 | 0.4157 | 0.3886 |
| Crocodile | 0.4429 | 0.4362 | 0.4734 | 0.4455 | 0.3578 | 0.4014 | 0.4243 | 0.0000 | 0.3303 | 0.4406 | 0.4344 | 0.4318 | 0.4566 | 0.4733 | 0.4688 | 0.4349 | 0.4524 |
| Bird | 0.4254 | 0.4011 | 0.4374 | 0.4133 | 0.3225 | 0.3759 | 0.3799 | 0.3303 | 0.0000 | 0.3976 | 0.3835 | 0.3949 | 0.3931 | 0.4210 | 0.4166 | 0.3960 | 0.3802 |
| Human | 0.3956 | 0.4291 | 0.4558 | 0.4038 | 0.3630 | 0.4009 | 0.4058 | 0.4406 | 0.3976 | 0.0000 | 0.2138 | 0.2120 | 0.2246 | 0.2562 | 0.2618 | 0.3142 | 0.2899 |
| Seal | 0.3618 | 0.4046 | 0.4220 | 0.3788 | 0.3425 | 0.3991 | 0.3947 | 0.4344 | 0.3835 | 0.2138 | 0.0000 | 0.1616 | 0.1751 | 0.2093 | 0.2280 | 0.2663 | 0.2467 |
| Cow | 0.3885 | 0.4214 | 0.4275 | 0.3770 | 0.3389 | 0.3924 | 0.4036 | 0.4318 | 0.3949 | 0.2120 | 0.1616 | 0.0000 | 0.1529 | 0.2317 | 0.2354 | 0.2765 | 0.2434 |
| Whale | 0.3811 | 0.4224 | 0.4234 | 0.3922 | 0.3429 | 0.3944 | 0.4209 | 0.4566 | 0.3931 | 0.2246 | 0.1751 | 0.1529 | 0.0000 | 0.2357 | 0.2533 | 0.2723 | 0.2506 |
| Mouse | 0.3950 | 0.4116 | 0.4324 | 0.4081 | 0.3448 | 0.3923 | 0.3985 | 0.4733 | 0.4210 | 0.2562 | 0.2093 | 0.2317 | 0.2357 | 0.0000 | 0.1262 | 0.2887 | 0.2476 |
| Rat | 0.3947 | 0.4432 | 0.4617 | 0.4110 | 0.3649 | 0.4210 | 0.4337 | 0.4688 | 0.4166 | 0.2618 | 0.2280 | 0.2354 | 0.2533 | 0.1262 | 0.0000 | 0.3003 | 0.2767 |
| Platypus | 0.4123 | 0.4184 | 0.4332 | 0.3956 | 0.3609 | 0.4176 | 0.4157 | 0.4349 | 0.3960 | 0.3142 | 0.2663 | 0.2765 | 0.2723 | 0.2887 | 0.3003 | 0.0000 | 0.2428 |
| Opossum | 0.3964 | 0.3763 | 0.3975 | 0.3751 | 0.3473 | 0.3793 | 0.3886 | 0.4524 | 0.3802 | 0.2899 | 0.2467 | 0.2434 | 0.2506 | 0.2476 | 0.2767 | 0.2428 | 0.0000 |
Get help on the piq_nj_tree app.
In [3]:
Copied!
cogent3.app_help("piq_nj_tree")
cogent3.app_help("piq_nj_tree")
Overview
--------
Construct a neighbour joining tree from a pairwise distance matrix.
Options for making the app
--------------------------
piq_nj_tree_app = get_app('piq_nj_tree', *args, **kwargs)
Parameters
----------
pairwise_distances : DistanceMatrix
Pairwise distances to construct neighbour joining tree from.
allow_negative : bool, optional
Whether to allow negative branch lengths in the output.
Coerces to 0 if not allowed, by default False.
Returns
-------
PhyloNode
The neighbour joining tree.
See Also
--------
jc_distances : construction of pairwise JC distance matrix from alignment.
Input type
----------
DistanceMatrix
Output type
-----------
PhyloNode
Make an app and apply it to the distance matrix.
In [4]:
Copied!
nj = cogent3.get_app("piq_nj_tree")
tree = nj(dists)
nj = cogent3.get_app("piq_nj_tree")
tree = nj(dists)
Warning Branch lengths can be negative in the piqtree NJ tree. This manifests as branches going backwards!
In [5]:
Copied!
tree.get_figure().show()
tree.get_figure().show()
Note To write the tree to a file, use the
write()method.
Combining the piqtree dist and nj apps¶
We can combine the piq_jc_distances and piq_nj_tree apps to build a tree from an alignment in one step.
In [6]:
Copied!
jc = cogent3.get_app("piq_jc_distances")
nj = cogent3.get_app("piq_nj_tree")
app = jc + nj
tree = app(aln)
tree.get_figure().show()
jc = cogent3.get_app("piq_jc_distances")
nj = cogent3.get_app("piq_nj_tree")
app = jc + nj
tree = app(aln)
tree.get_figure().show()