File: calculate_UPGMA_cluster.rst

package info (click to toggle)
python-cogent 2024.5.7a1%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 74,600 kB
  • sloc: python: 92,479; makefile: 117; sh: 16
file content (60 lines) | stat: -rw-r--r-- 1,329 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
.. jupyter-execute::
    :hide-code:

    import set_working_directory

Make a UPGMA cluster
====================

.. sectionauthor:: Catherine Lozupone

An example of how to calculate the pairwise distances for a set of sequences.

.. note:: UPGMA should not be used for phylogenetic reconstruction.

.. jupyter-execute::

    from cogent3 import load_aligned_seqs
    from cogent3.cluster.UPGMA import upgma
    from cogent3.evolve import distance

Import a substitution model (or create your own)

.. jupyter-execute::

    from cogent3.evolve.models import HKY85

Load the alignment.

.. jupyter-execute::

    al = load_aligned_seqs("data/test.paml")

Create a pairwise distances object calculator for the alignment, providing a substitution model instance.

.. jupyter-execute::

    d = distance.EstimateDistances(al, submodel=HKY85())
    d.run(show_progress=False)

Now use this matrix to build a UPGMA cluster.

.. jupyter-execute::

    mycluster = upgma(d.get_pairwise_distances())
    print(mycluster.ascii_art())

We demonstrate saving this UPGMA cluster to a file.

.. jupyter-execute::

    mycluster.write("test_upgma.tree")

..  We don't actually want to keep that file now, so I'm importing the ``os`` module to delete it.

.. jupyter-execute::
    :hide-code:

    import os

    os.remove("test_upgma.tree")