.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/gallery/sequence/blosum_dendrogram.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_gallery_sequence_blosum_dendrogram.py: Dendrogram of the BLOSUM62 matrix ================================= In this example a dendrogram is created, that displays the similarity of amino acids in the *BLOSUM62* substitution matrix. The amino acids are clustered with the UPGMA method. .. GENERATED FROM PYTHON SOURCE LINES 9-24 .. code-block:: Python # Code source: Patrick Kunzmann # License: BSD 3 clause import numpy as np import matplotlib.pyplot as plt import biotite.sequence as seq import biotite.sequence.align as align import biotite.sequence.phylo as phylo import biotite.sequence.graphics as graphics # Obtain BLOSUM62 matrix = align.SubstitutionMatrix.std_protein_matrix() print(matrix) .. rst-class:: sphx-glr-script-out .. code-block:: none A C D E F G H I K L M N P Q R S T V W Y B Z X * A 4 0 -2 -1 -2 0 -2 -1 -1 -1 -1 -2 -1 -1 -1 1 0 0 -3 -2 -2 -1 0 -4 C 0 9 -3 -4 -2 -3 -3 -1 -3 -1 -1 -3 -3 -3 -3 -1 -1 -1 -2 -2 -3 -3 -2 -4 D -2 -3 6 2 -3 -1 -1 -3 -1 -4 -3 1 -1 0 -2 0 -1 -3 -4 -3 4 1 -1 -4 E -1 -4 2 5 -3 -2 0 -3 1 -3 -2 0 -1 2 0 0 -1 -2 -3 -2 1 4 -1 -4 F -2 -2 -3 -3 6 -3 -1 0 -3 0 0 -3 -4 -3 -3 -2 -2 -1 1 3 -3 -3 -1 -4 G 0 -3 -1 -2 -3 6 -2 -4 -2 -4 -3 0 -2 -2 -2 0 -2 -3 -2 -3 -1 -2 -1 -4 H -2 -3 -1 0 -1 -2 8 -3 -1 -3 -2 1 -2 0 0 -1 -2 -3 -2 2 0 0 -1 -4 I -1 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1 -1 -2 -1 -1 0 0 -1 -2 -1 -1 -1 -1 -4 * -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 1 .. GENERATED FROM PYTHON SOURCE LINES 25-29 The original *BLOSUM62* contains symbols for ambiguous amino acids and the stop signal. As these are not actual amino acids, a new substitution matrix is created, where these symbols are are removed. .. GENERATED FROM PYTHON SOURCE LINES 29-39 .. code-block:: Python # Matrix should not contain ambiguous symbols or stop signal matrix = align.SubstitutionMatrix( seq.Alphabet(matrix.get_alphabet1().get_symbols()[:-4]), seq.Alphabet(matrix.get_alphabet2().get_symbols()[:-4]), matrix.score_matrix()[:-4, :-4] ) similarities = matrix.score_matrix() print(matrix) .. rst-class:: sphx-glr-script-out .. code-block:: none A C D E F G H I K L M N P Q R S T V W Y A 4 0 -2 -1 -2 0 -2 -1 -1 -1 -1 -2 -1 -1 -1 1 0 0 -3 -2 C 0 9 -3 -4 -2 -3 -3 -1 -3 -1 -1 -3 -3 -3 -3 -1 -1 -1 -2 -2 D -2 -3 6 2 -3 -1 -1 -3 -1 -4 -3 1 -1 0 -2 0 -1 -3 -4 -3 E -1 -4 2 5 -3 -2 0 -3 1 -3 -2 0 -1 2 0 0 -1 -2 -3 -2 F -2 -2 -3 -3 6 -3 -1 0 -3 0 0 -3 -4 -3 -3 -2 -2 -1 1 3 G 0 -3 -1 -2 -3 6 -2 -4 -2 -4 -3 0 -2 -2 -2 0 -2 -3 -2 -3 H -2 -3 -1 0 -1 -2 8 -3 -1 -3 -2 1 -2 0 0 -1 -2 -3 -2 2 I -1 -1 -3 -3 0 -4 -3 4 -3 2 1 -3 -3 -3 -3 -2 -1 3 -3 -1 K -1 -3 -1 1 -3 -2 -1 -3 5 -2 -1 0 -1 1 2 0 -1 -2 -3 -2 L -1 -1 -4 -3 0 -4 -3 2 -2 4 2 -3 -3 -2 -2 -2 -1 1 -2 -1 M -1 -1 -3 -2 0 -3 -2 1 -1 2 5 -2 -2 0 -1 -1 -1 1 -1 -1 N -2 -3 1 0 -3 0 1 -3 0 -3 -2 6 -2 0 0 1 0 -3 -4 -2 P -1 -3 -1 -1 -4 -2 -2 -3 -1 -3 -2 -2 7 -1 -2 -1 -1 -2 -4 -3 Q -1 -3 0 2 -3 -2 0 -3 1 -2 0 0 -1 5 1 0 -1 -2 -2 -1 R -1 -3 -2 0 -3 -2 0 -3 2 -2 -1 0 -2 1 5 -1 -1 -3 -3 -2 S 1 -1 0 0 -2 0 -1 -2 0 -2 -1 1 -1 0 -1 4 1 -2 -3 -2 T 0 -1 -1 -1 -2 -2 -2 -1 -1 -1 -1 0 -1 -1 -1 1 5 0 -2 -2 V 0 -1 -3 -2 -1 -3 -3 3 -2 1 1 -3 -2 -2 -3 -2 0 4 -3 -1 W -3 -2 -4 -3 1 -2 -2 -3 -3 -2 -1 -4 -4 -2 -3 -3 -2 -3 11 2 Y -2 -2 -3 -2 3 -3 2 -1 -2 -1 -1 -2 -3 -1 -2 -2 -2 -1 2 7 .. GENERATED FROM PYTHON SOURCE LINES 40-47 Now a function must be defined, that converts the similarity depicted by a substitution matrix into a distance required by the UPGMA method. In this case, the distance is defined as the difference between the similarity of the two symbols and the average maximum similarity of the symbols to themselves. Finally the obtained (phylogenetic) tree is plotted as dendrogram. .. GENERATED FROM PYTHON SOURCE LINES 47-69 .. code-block:: Python def get_distance(similarities, i, j): s_max = (similarities[i,i] + similarities[j,j]) / 2 return s_max - similarities[i,j] distances = np.zeros(similarities.shape) for i in range(distances.shape[0]): for j in range(distances.shape[1]): distances[i,j] = get_distance(similarities, i, j) tree = phylo.upgma(distances) fig = plt.figure(figsize=(8.0, 5.0)) ax = fig.add_subplot(111) # Use the 3-letter amino acid code aa label labels = [seq.ProteinSequence.convert_letter_1to3(letter).capitalize() for letter in matrix.get_alphabet1()] graphics.plot_dendrogram( ax, tree, orientation="top", labels=labels ) ax.set_ylabel("Distance") # Add grid for clearer distance perception ax.yaxis.grid(color="lightgray") plt.show() .. image-sg:: /examples/gallery/sequence/images/sphx_glr_blosum_dendrogram_001.png :alt: blosum dendrogram :srcset: /examples/gallery/sequence/images/sphx_glr_blosum_dendrogram_001.png :class: sphx-glr-single-img .. _sphx_glr_download_examples_gallery_sequence_blosum_dendrogram.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: blosum_dendrogram.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: blosum_dendrogram.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_