File: test_dataio_phylip_reader_chars.py

package info (click to toggle)
python-dendropy 4.2.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 68,392 kB
  • ctags: 3,947
  • sloc: python: 41,840; xml: 1,400; makefile: 15
file content (310 lines) | stat: -rw-r--r-- 12,996 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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# !/usr/bin/env python

##############################################################################
##  DendroPy Phylogenetic Computing Library.
##
##  Copyright 2010-2015 Jeet Sukumaran and Mark T. Holder.
##  All rights reserved.
##
##  See "LICENSE.rst" for terms and conditions of usage.
##
##  If you use this work or any portion thereof in published work,
##  please cite it as:
##
##     Sukumaran, J. and M. T. Holder. 2010. DendroPy: a Python library
##     for phylogenetic computing. Bioinformatics 26: 1569-1571.
##
##############################################################################

"""
Tests for general PHYLIP character matrix reading.
"""

import unittest
import dendropy
import collections
from dendropy.utility import error
from dendropy.test.support import dendropytest
from dendropy.test.support import pathmap
from dendropy.test.support import standard_file_test_chars
from dendropy.test.support import compare_and_validate
from dendropy.dataio import phylipreader
from dendropy.utility import messaging
_LOG = messaging.get_logger(__name__)

class PhylipCharactersReaderDnaTestCase(
        standard_file_test_chars.DnaTestChecker,
        dendropytest.ExtendedTestCase):

    @classmethod
    def setUpClass(cls):
        cls.build()

    def test_basic_phylip(self):
        src_filenames = [
                "standard-test-chars-dna.relaxed.phylip",
                ]
        for src_idx, src_filename in enumerate(src_filenames):
            # print(src_idx, src_filename)
            src_path = pathmap.char_source_path(src_filename)
            self.verify_get_from(
                    matrix_type=dendropy.DnaCharacterMatrix,
                    src_filepath=src_path,
                    schema="phylip",
                    factory_kwargs={},
                    check_taxon_annotations=False,
                    check_matrix_annotations=False,
                    check_sequence_annotations=False,
                    check_column_annotations=False,
                    check_cell_annotations=False)

class PhylipCharactersReaderRnaTestCase(
        standard_file_test_chars.RnaTestChecker,
        dendropytest.ExtendedTestCase):

    @classmethod
    def setUpClass(cls):
        cls.build()

    def test_basic_phylip(self):
        src_filenames = [
                "standard-test-chars-rna.relaxed.phylip",
                ]
        for src_idx, src_filename in enumerate(src_filenames):
            # print(src_idx, src_filename)
            src_path = pathmap.char_source_path(src_filename)
            self.verify_get_from(
                    matrix_type=dendropy.RnaCharacterMatrix,
                    src_filepath=src_path,
                    schema="phylip",
                    factory_kwargs={},
                    check_taxon_annotations=False,
                    check_matrix_annotations=False,
                    check_sequence_annotations=False,
                    check_column_annotations=False,
                    check_cell_annotations=False)

class PhylipCharactersReaderProteinTestCase(
        standard_file_test_chars.ProteinTestChecker,
        dendropytest.ExtendedTestCase):

    @classmethod
    def setUpClass(cls):
        cls.build()

    def test_basic_phylip(self):
        src_filenames = [
                "standard-test-chars-protein.relaxed.phylip",
                ]
        for src_idx, src_filename in enumerate(src_filenames):
            # print(src_idx, src_filename)
            src_path = pathmap.char_source_path(src_filename)
            self.verify_get_from(
                    matrix_type=dendropy.ProteinCharacterMatrix,
                    src_filepath=src_path,
                    schema="phylip",
                    factory_kwargs={},
                    check_taxon_annotations=False,
                    check_matrix_annotations=False,
                    check_sequence_annotations=False,
                    check_column_annotations=False,
                    check_cell_annotations=False)

class PhylipStandardCharacters01234TestCase(
        standard_file_test_chars.Standard01234TestChecker,
        dendropytest.ExtendedTestCase):

    @classmethod
    def setUpClass(cls):
        cls.build(state_alphabet_fundamental_symbols="0123456789-")

    def test_basic_phylip(self):
        src_filenames = [
                "standard-test-chars-generic.relaxed.phylip",
                ]
        for src_idx, src_filename in enumerate(src_filenames):
            # print(src_idx, src_filename)
            src_path = pathmap.char_source_path(src_filename)
            self.verify_get_from(
                    matrix_type=dendropy.StandardCharacterMatrix,
                    src_filepath=src_path,
                    schema="phylip",
                    factory_kwargs={},
                    check_taxon_annotations=False,
                    check_matrix_annotations=False,
                    check_sequence_annotations=False,
                    check_column_annotations=False,
                    check_cell_annotations=False)

class PhylipCharactersContinuousTestCase(
        standard_file_test_chars.ContinuousTestChecker,
        dendropytest.ExtendedTestCase):

    @classmethod
    def setUpClass(cls):
        cls.build()

    def test_basic_nexus(self):
        src_filenames = [
                ("standard-test-chars-continuous.relaxed.phylip", {}),
                ("standard-test-chars-continuous.interleaved.phylip", {"interleaved": True}),
                ]
        for src_idx, (src_filename, kwargs) in enumerate(src_filenames):
            # print(src_idx, src_filename)
            src_path = pathmap.char_source_path(src_filename)
            self.verify_get_from(
                    matrix_type=dendropy.ContinuousCharacterMatrix,
                    src_filepath=src_path,
                    schema="phylip",
                    factory_kwargs=kwargs,
                    check_taxon_annotations=False,
                    check_matrix_annotations=False,
                    check_sequence_annotations=False,
                    check_column_annotations=False,
                    check_cell_annotations=False)

class PhylipVariantsTestCases(dendropytest.ExtendedTestCase):

    @classmethod
    def setUpClass(cls):
        cls.expected_seqs = collections.OrderedDict()
        cls.expected_seqs["Turkey"]     = "AAGCTNGGGCATTTCAGGGTGAGCCCGGGCAATACAGGGTAT"
        cls.expected_seqs["Salmo gair"] = "AAGCCTTGGCAGTGCAGGGTGAGCCGTGGCCGGGCACGGTAT"
        cls.expected_seqs["H. Sapiens"] = "ACCGGTTGGCCGTTCAGGGTACAGGTTGGCCGTTCAGGGTAA"
        cls.expected_seqs["Chimp"]      = "AAACCCTTGCCGTTACGCTTAAACCGAGGCCGGGACACTCAT"
        cls.expected_seqs["Gorilla"]    = "AAACCCTTGCCGGTACGCTTAAACCATTGCCGGTACGCTTAA"

    def test_strict_sequential(self):
        s = """\
  5    42
Turkey    AAGCTNGGGC ATTTCAGGGT
GAGCCCGGGC AATACAGGGT AT
Salmo gairAAGCCTTGGC AGTGCAGGGT
GAGCCGTGGC CGGGCACGGT AT
H. SapiensACCGGTTGGC CGTTCAGGGT
ACAGGTTGGC CGTTCAGGGT AA
Chimp     AAACCCTTGC CGTTACGCTT
AAACCGAGGC CGGGACACTC AT
Gorilla   AAACCCTTGC CGGTACGCTT
AAACCATTGC CGGTACGCTT AA
        """
        char_matrix = dendropy.DnaCharacterMatrix.get_from_string(
                s,
                "phylip",
                strict=True)
        self.assertEqual(len(char_matrix), len(self.expected_seqs))
        self.assertEqual(len(char_matrix.taxon_namespace), len(self.expected_seqs))
        for taxon, expected_taxon in zip(char_matrix, self.expected_seqs):
            self.assertEqual(taxon.label, expected_taxon)
            self.assertEqual(char_matrix[taxon].symbols_as_string(), self.expected_seqs[expected_taxon])

    def test_strict_interleaved(self):
        s = """\
5    42
Turkey    AAGCTNGGGC ATTTCAGGGT
Salmo gairAAGCCTTGGC AGTGCAGGGT
H. SapiensACCGGTTGGC CGTTCAGGGT
Chimp     AAACCCTTGC CGTTACGCTT
Gorilla   AAACCCTTGC CGGTACGCTT

GAGCCCGGGC AATACAGGGT AT
GAGCCGTGGC CGGGCACGGT AT
ACAGGTTGGC CGTTCAGGGT AA
AAACCGAGGC CGGGACACTC AT
AAACCATTGC CGGTACGCTT AA
        """
        char_matrix = dendropy.DnaCharacterMatrix.get_from_string(
                s,
                "phylip",
                interleaved=True,
                strict=True)
        self.assertEqual(len(char_matrix), len(self.expected_seqs))
        self.assertEqual(len(char_matrix.taxon_namespace), len(self.expected_seqs))
        for taxon, expected_taxon in zip(char_matrix, self.expected_seqs):
            self.assertEqual(taxon.label, expected_taxon)
            self.assertEqual(char_matrix[taxon].symbols_as_string(), self.expected_seqs[expected_taxon])

    def test_strict_interleaved_with_bad_chars(self):
        s = """\
5    42
Turkey    AAGCTNGGGC ATTTCA3828GGGT
Salmo gairAAGCCTTGGC AGTGCA3828GGGT
H. SapiensACCGGTTGGC CGTTCA3828GGGT
Chimp     AAACCCTTGC CGTTAC3828GCTT
Gorilla   AAACCCTTGC CGGTAC3828GCTT

GAGCCCGGGC AATACAGGGT AT
GAGCCGTGGC CGGGCACGGT AT
ACAGGTTGGC CGTTCAGGGT AA
AAACCGAGGC CGGGACACTC AT
AAACCATTGC CGGTACGCTT AA
        """
        char_matrix = dendropy.DnaCharacterMatrix.get_from_string(
                s,
                "phylip",
                interleaved=True,
                strict=True,
                ignore_invalid_chars=True)
        self.assertEqual(len(char_matrix), len(self.expected_seqs))
        self.assertEqual(len(char_matrix.taxon_namespace), len(self.expected_seqs))
        for taxon, expected_taxon in zip(char_matrix, self.expected_seqs):
            self.assertEqual(taxon.label, expected_taxon)
            self.assertEqual(char_matrix[taxon].symbols_as_string(), self.expected_seqs[expected_taxon])

class PhylipContinuousVariantsTestCases(dendropytest.ExtendedTestCase):

    @classmethod
    def setUpClass(cls):
        cls.expected_seqs = collections.OrderedDict()
        cls.expected_seqs["Turkey"]     = [-231.6391 ,  972.4189  ,  626.6717  ,  -328.6811 ,  -213.5738 ,  464.3897  ,  -91.3483  ,  349.8176  ,  333.4800  ,  521.4970  ]
        cls.expected_seqs["Salmo gair"] = [ 104.4199 ,  669.7402  ,  -68.6082  ,  975.4302  ,  -874.4510 ,  -191.3305 ,  -179.8437 ,  655.5611  ,  -657.4532 ,  -563.7863 ]
        cls.expected_seqs["H. Sapiens"] = [-613.2947 ,  -600.7053 ,  -700.5140 ,  438.6092  ,  615.5268  ,  640.7933  ,  503.8948  ,  -159.7922 ,  866.8036  ,  274.0275  ]
        cls.expected_seqs["Chimp"]      = [-654.7695 ,  103.3806  ,  -971.8866 ,  853.9164  ,  653.5797  ,  823.6672  ,  -476.6859 ,  325.9331  ,  456.0902  ,  -399.7095 ]
        cls.expected_seqs["Gorilla"]    = [-762.4904 ,  808.3665  ,  522.5775  ,  250.6523  ,  -287.9786 ,  -995.4612 ,  571.9263  ,  -793.3975 ,  -42.7027  ,  186.8869  ]

    def test_strict_sequential(self):
        s = """\
  5    10
Turkey    -231.6391 972.4189  626.6717  -328.6811 -213.5738 464.3897  -91.3483  349.8176  333.4800  521.4970
Salmo gair 104.4199 669.7402  -68.6082  975.4302  -874.4510 -191.3305 -179.8437 655.5611  -657.4532 -563.7863
H. Sapiens-613.2947 -600.7053 -700.5140 438.6092  615.5268  640.7933  503.8948  -159.7922 866.8036  274.0275
Chimp     -654.7695 103.3806  -971.8866 853.9164  653.5797  823.6672  -476.6859 325.9331  456.0902  -399.7095
Gorilla   -762.4904 808.3665  522.5775  250.6523  -287.9786 -995.4612 571.9263  -793.3975 -42.7027  186.8869
        """
        char_matrix = dendropy.ContinuousCharacterMatrix.get_from_string(
                s,
                "phylip",
                strict=True)
        self.assertEqual(len(char_matrix), len(self.expected_seqs))
        self.assertEqual(len(char_matrix.taxon_namespace), len(self.expected_seqs))
        for taxon, expected_taxon in zip(char_matrix, self.expected_seqs):
            self.assertEqual(taxon.label, expected_taxon)
            self.assertEqual(char_matrix[taxon].values(), self.expected_seqs[expected_taxon])

    def test_strict_interleaved(self):
        s = """\
  5    10
Turkey    -231.6391 972.4189  626.6717  -328.6811
Salmo gair 104.4199 669.7402  -68.6082  975.4302
H. Sapiens-613.2947 -600.7053 -700.5140 438.6092
Chimp     -654.7695 103.3806  -971.8866 853.9164
Gorilla   -762.4904 808.3665  522.5775  250.6523
 -213.5738 464.3897  -91.3483  349.8176  333.4800  521.4970
 -874.4510 -191.3305 -179.8437 655.5611  -657.4532 -563.7863
 615.5268  640.7933  503.8948  -159.7922 866.8036  274.0275
 653.5797  823.6672  -476.6859 325.9331  456.0902  -399.7095
 -287.9786 -995.4612 571.9263  -793.3975 -42.7027  186.8869
        """
        char_matrix = dendropy.ContinuousCharacterMatrix.get_from_string(
                s,
                "phylip",
                interleaved=True,
                strict=True)
        self.assertEqual(len(char_matrix), len(self.expected_seqs))
        self.assertEqual(len(char_matrix.taxon_namespace), len(self.expected_seqs))
        for taxon, expected_taxon in zip(char_matrix, self.expected_seqs):
            self.assertEqual(taxon.label, expected_taxon)
            self.assertEqual(char_matrix[taxon].values(), self.expected_seqs[expected_taxon])

if __name__ == "__main__":
    unittest.main()