File: test_AlignInfo.py

package info (click to toggle)
python-biopython 1.85%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 126,372 kB
  • sloc: xml: 1,047,995; python: 332,722; ansic: 16,944; sql: 1,208; makefile: 140; sh: 81
file content (225 lines) | stat: -rw-r--r-- 9,322 bytes parent folder | download
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# Copyright 2016 by Peter Cock.  All rights reserved.
# This code is part of the Biopython distribution and governed by its
# license.  Please see the LICENSE file that should have been included
# as part of this package.

"""Bio.Align.AlignInfo related tests."""
import math
import unittest

from Bio import AlignIO
from Bio import BiopythonDeprecationWarning
from Bio.Align import MultipleSeqAlignment
from Bio.Align.AlignInfo import SummaryInfo
from Bio.Data import IUPACData
from Bio.motifs import Motif
from Bio.Seq import Seq
from Bio.SeqRecord import SeqRecord


class AlignInfoTests(unittest.TestCase):
    """Test basic usage."""

    def assertAlmostEqualList(self, list1, list2, **kwargs):
        self.assertEqual(len(list1), len(list2))
        for v1, v2 in zip(list1, list2):
            self.assertAlmostEqual(v1, v2, **kwargs)

    def test_nucleotides(self):
        filename = "GFF/multi.fna"
        fmt = "fasta"
        msa = AlignIO.read(filename, fmt)
        summary = SummaryInfo(msa)
        alignment = msa.alignment
        motif = Motif("ACGT", alignment)

        with self.assertWarns(BiopythonDeprecationWarning):
            c = summary.dumb_consensus(threshold=0.1, ambiguous="N")
        # dumb_consensus uses ambiguous if multiple letters have the same score
        self.assertEqual(c, "ANGNCCCC")
        c = motif.counts.calculate_consensus(identity=0.1)
        # Instead, EMBOSS uses the first letter it encounters
        self.assertEqual(c, "AaGcCCCC")
        with self.assertWarns(BiopythonDeprecationWarning):
            c = summary.dumb_consensus(ambiguous="N")
        self.assertEqual(c, "NNNNNNNN")
        c = motif.counts.calculate_consensus(identity=0.7)
        self.assertEqual(c, "NNNNNNNN")

        with self.assertWarns(BiopythonDeprecationWarning):
            c = summary.gap_consensus(ambiguous="N")
        self.assertEqual(c, "NNNNNNNN")

        expected = {"A": 0.25, "G": 0.25, "T": 0.25, "C": 0.25}

        with self.assertWarns(BiopythonDeprecationWarning):
            m = summary.pos_specific_score_matrix(chars_to_ignore=["-"], axis_seq=c)

        counts = motif.counts

        for i in range(alignment.length):
            for letter in "ACGT":
                self.assertAlmostEqual(counts[letter][i], m[i][letter])

        self.assertEqual(
            str(m),
            """    A   C   G   T
N  2.0 0.0 1.0 0.0
N  1.0 1.0 1.0 0.0
N  1.0 0.0 2.0 0.0
N  0.0 1.0 1.0 1.0
N  1.0 2.0 0.0 0.0
N  0.0 2.0 1.0 0.0
N  1.0 2.0 0.0 0.0
N  0.0 2.0 1.0 0.0
""",
        )

        # provide the frequencies and chars to ignore explicitly.
        with self.assertWarns(BiopythonDeprecationWarning):
            ic = summary.information_content(
                e_freq_table=expected, chars_to_ignore=["-"]
            )
        self.assertAlmostEqual(ic, 7.32029999423075)
        ic = sum(motif.relative_entropy)
        self.assertAlmostEqual(ic, 7.32029999423075)

    def test_proteins(self):
        letters = IUPACData.protein_letters
        a = MultipleSeqAlignment(
            [
                SeqRecord(Seq("MHQAIFIYQIGYP*LKSGYIQSIRSPEYDNW-"), id="ID001"),
                SeqRecord(Seq("MH--IFIYQIGYAYLKSGYIQSIRSPEY-NW*"), id="ID002"),
                SeqRecord(Seq("MHQAIFIYQIGYPYLKSGYIQSIRSPEYDNW*"), id="ID003"),
            ]
        )
        self.assertEqual(32, a.get_alignment_length())

        s = SummaryInfo(a)

        alignment = a.alignment
        motif = Motif(letters + "*", alignment)
        counts = motif.counts

        with self.assertWarns(BiopythonDeprecationWarning):
            dumb_consensus = s.dumb_consensus()
        self.assertEqual(dumb_consensus, "MHQAIFIYQIGYXXLKSGYIQSIRSPEYDNW*")
        consensus = counts.calculate_consensus(identity=0.7)
        self.assertEqual(consensus, dumb_consensus)

        with self.assertWarns(BiopythonDeprecationWarning):
            c = s.gap_consensus(ambiguous="X")
        self.assertEqual(c, "MHXXIFIYQIGYXXLKSGYIQSIRSPEYXNWX")

        with self.assertWarns(BiopythonDeprecationWarning):
            m = s.pos_specific_score_matrix(chars_to_ignore=["-", "*"], axis_seq=c)
        j = 0
        all_letters = s._get_all_letters()
        for i in range(alignment.length):
            for letter in letters:
                count = counts[letter][i]
                if letter in all_letters:
                    self.assertAlmostEqual(count, m[j][letter])
                else:
                    self.assertAlmostEqual(count, 0.0)
            j += 1
        self.assertEqual(
            str(m),
            """    A   D   E   F   G   H   I   K   L   M   N   P   Q   R   S   W   Y
M  0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 3.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
H  0.0 0.0 0.0 0.0 0.0 3.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
X  0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 2.0 0.0 0.0 0.0 0.0
X  2.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
I  0.0 0.0 0.0 0.0 0.0 0.0 3.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0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 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
Q  0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 3.0 0.0 0.0 0.0 0.0
S  0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 3.0 0.0 0.0
I  0.0 0.0 0.0 0.0 0.0 0.0 3.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
R  0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 3.0 0.0 0.0 0.0
S  0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 3.0 0.0 0.0
P  0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 3.0 0.0 0.0 0.0 0.0 0.0
E  0.0 0.0 3.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
Y  0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 3.0
X  0.0 2.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
N  0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 3.0 0.0 0.0 0.0 0.0 0.0 0.0
W  0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 3.0 0.0
X  0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
""",
        )

        base_freq = 1.0 / len(letters)
        e_freq_table = {letter: base_freq for letter in letters}
        with self.assertWarns(BiopythonDeprecationWarning):
            ic = s.information_content(
                e_freq_table=e_freq_table, chars_to_ignore=["-", "*"]
            )
        self.assertAlmostEqual(ic, 133.061475107)
        motif = Motif(letters, alignment)
        ic = sum(motif.relative_entropy)
        self.assertAlmostEqual(ic, 133.061475107)

    def test_pseudo_count(self):
        # use example from
        # http://biologie.univ-mrs.fr/upload/p202/01.4.PSSM_theory.pdf
        msa = MultipleSeqAlignment(
            [
                SeqRecord(Seq("AACCACGTTTAA"), id="ID001"),
                SeqRecord(Seq("CACCACGTGGGT"), id="ID002"),
                SeqRecord(Seq("CACCACGTTCGC"), id="ID003"),
                SeqRecord(Seq("GCGCACGTGGGG"), id="ID004"),
                SeqRecord(Seq("TCGCACGTTGTG"), id="ID005"),
                SeqRecord(Seq("TGGCACGTGTTT"), id="ID006"),
                SeqRecord(Seq("TGACACGTGGGA"), id="ID007"),
                SeqRecord(Seq("TTACACGTGCGC"), id="ID008"),
            ]
        )

        summary = SummaryInfo(msa)
        expected = {"A": 0.325, "G": 0.175, "T": 0.325, "C": 0.175}
        with self.assertWarns(BiopythonDeprecationWarning):
            ic = summary.information_content(
                e_freq_table=expected, log_base=math.exp(1), pseudo_count=1
            )
        self.assertAlmostEqual(ic, 7.546369561463767)
        ic_vector = [
            0.11112361,
            0.08677812,
            0.35598044,
            1.29445419,
            0.80272907,
            1.29445419,
            1.29445419,
            0.80272907,
            0.60929642,
            0.39157892,
            0.46539767,
            0.03739368,
        ]
        self.assertAlmostEqualList(summary.ic_vector, ic_vector)
        # One more time, now using a new-style Alignment object:
        alignment = msa.alignment
        motif = Motif("ACGT", alignment)
        motif.background = expected
        motif.pseudocounts = expected
        self.assertAlmostEqualList(motif.relative_entropy * math.log(2), ic_vector)
        ic = sum(ic_vector)
        self.assertAlmostEqual(ic, 7.546369561463767)


if __name__ == "__main__":
    runner = unittest.TextTestRunner(verbosity=2)
    unittest.main(testRunner=runner)