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
|
# !/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 NEXUS character matrix reading.
"""
import unittest
import dendropy
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 nexusreader
from dendropy.utility import messaging
_LOG = messaging.get_logger(__name__)
class NexusCharactersReaderDnaTestCase(
standard_file_test_chars.DnaTestChecker,
dendropytest.ExtendedTestCase):
@classmethod
def setUpClass(cls):
cls.build()
def test_basic_nexus(self):
src_filenames = [
"standard-test-chars-dna.simple.nexus",
"standard-test-chars-dna.basic.nexus",
"standard-test-chars-dna.interleaved.nexus",
"standard-test-chars-dna.matchchar.nexus",
"standard-test-chars-dna.multi.nexus",
]
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="nexus",
factory_kwargs={},
check_taxon_annotations=False,
check_matrix_annotations=False,
check_sequence_annotations=False,
check_column_annotations=False,
check_cell_annotations=False)
class NexusCharactersReaderRnaTestCase(
standard_file_test_chars.RnaTestChecker,
dendropytest.ExtendedTestCase):
@classmethod
def setUpClass(cls):
cls.build()
def test_basic_nexus(self):
src_filenames = [
"standard-test-chars-rna.simple.nexus",
"standard-test-chars-rna.basic.nexus",
"standard-test-chars-rna.interleaved.nexus",
"standard-test-chars-rna.matchchar.nexus",
"standard-test-chars-rna.multi.nexus",
]
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="nexus",
factory_kwargs={},
check_taxon_annotations=False,
check_matrix_annotations=False,
check_sequence_annotations=False,
check_column_annotations=False,
check_cell_annotations=False)
class NexusCharactersReaderProteinTestCase(
standard_file_test_chars.ProteinTestChecker,
dendropytest.ExtendedTestCase):
@classmethod
def setUpClass(cls):
cls.build()
def test_basic_nexus(self):
src_filenames = [
"standard-test-chars-protein.simple.nexus",
"standard-test-chars-protein.basic.nexus",
"standard-test-chars-protein.interleaved.nexus",
"standard-test-chars-protein.matchchar.nexus",
"standard-test-chars-protein.multi.nexus",
]
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="nexus",
factory_kwargs={},
check_taxon_annotations=False,
check_matrix_annotations=False,
check_sequence_annotations=False,
check_column_annotations=False,
check_cell_annotations=False)
class NexusCharactersContinuousTestCase(
standard_file_test_chars.ContinuousTestChecker,
dendropytest.ExtendedTestCase):
@classmethod
def setUpClass(cls):
cls.build()
def test_basic_nexus(self):
src_filenames = [
"standard-test-chars-continuous.mesquite.nexus",
"standard-test-chars-continuous.mesquite.interleaved.nexus",
]
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.ContinuousCharacterMatrix,
src_filepath=src_path,
schema="nexus",
factory_kwargs={},
check_taxon_annotations=False,
check_matrix_annotations=False,
check_sequence_annotations=False,
check_column_annotations=False,
check_cell_annotations=False)
class NexusStandardCharacters01234TestCase(
standard_file_test_chars.Standard01234TestChecker,
dendropytest.ExtendedTestCase):
@classmethod
def setUpClass(cls):
cls.build()
def test_basic_nexus(self):
src_filenames = [
"standard-test-chars-generic.simple.nexus",
"standard-test-chars-generic.basic.nexus",
"standard-test-chars-generic.dotted.nexus",
"standard-test-chars-generic.interleaved.nexus",
]
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="nexus",
factory_kwargs={},
check_taxon_annotations=False,
check_matrix_annotations=False,
check_sequence_annotations=False,
check_column_annotations=False,
check_cell_annotations=False)
class NexusTooManyTaxaTest(
dendropytest.ExtendedTestCase):
def testTooManyTaxaNonInterleaved(self):
data_str = """\
#NEXUS
BEGIN TAXA;
DIMENSIONS NTAX=2;
TAXLABELS AAA BBB ;
END;
BEGIN CHARACTERS;
DIMENSIONS NCHAR=8;
FORMAT DATATYPE=DNA GAP=- MISSING=? MATCHCHAR=.;
MATRIX
AAA ACGTACGT
BBB ACGTACGT
CCC ACGTACGT
;
END;
"""
self.assertRaises(nexusreader.NexusReader.TooManyTaxaError,
dendropy.DnaCharacterMatrix.get_from_string,
data_str,
'nexus')
class NexusCharsSubsetsTest(
compare_and_validate.Comparator,
dendropytest.ExtendedTestCase):
def verify_subsets(self, src_filename, expected_sets):
"""
``src_filename`` -- name of file containing full data and charsets
statement
``expected_sets`` -- dictionary with keys = label of charset, and values
= name of file with subset of characters correspond
to the charset.
"""
src_data = dendropy.DnaCharacterMatrix.get_from_path(
pathmap.char_source_path(src_filename),
'nexus')
state_alphabet = src_data.default_state_alphabet
self.assertEqual(len(src_data.character_subsets), len(expected_sets))
for label, expected_data_file in expected_sets.items():
_LOG.debug(label)
self.assertTrue(label in src_data.character_subsets)
result_subset = src_data.export_character_subset(label)
expected_subset = dendropy.DnaCharacterMatrix.get_from_path(
pathmap.char_source_path(expected_data_file),
'nexus')
# confirm subset is correct
self.compare_distinct_char_matrix(
result_subset,
expected_subset,
taxon_namespace_scoped=False,
)
# mutate new and confirm that old remains unchanged
e1_symbols = src_data[0].symbols_as_string()
r1 = result_subset[0]
dummy_state = state_alphabet["A"]
for idx in range(len(r1)):
r1[idx].value = dummy_state
self.assertEqual(e1_symbols, src_data[0].symbols_as_string())
# mutate old and confirm that new remains unchanged
r2_symbols = result_subset[1].symbols_as_string()
e2 = src_data[1]
dummy_state = state_alphabet["A"]
for idx in range(len(e2)):
e2[idx].value = dummy_state
self.assertEqual(r2_symbols, result_subset[1].symbols_as_string())
def testNonInterleaved(self):
"""
Charsets here go through all forms of position specification.
"""
expected_sets = {
"coding" : "primates.chars.subsets-coding.nexus",
"noncoding" : "primates.chars.subsets-noncoding.nexus",
"1stpos" : "primates.chars.subsets-1stpos.nexus",
"2ndpos" : "primates.chars.subsets-2ndpos.nexus",
"3rdpos" : "primates.chars.subsets-3rdpos.nexus",
}
self.verify_subsets('primates.chars.subsets-all.nexus', expected_sets)
def testInterleaved(self):
"""
A bug in DendroPy resulted in the block immediately following an
interleaved character matrix DATA or CHARACTERS block being skipped.
This tests for it by ensuring that the ASSUMPTIONS block following an
interleaved CHARACTERS block is parsed. A better test would approach
the issue more directly, by checking to see if block parsing left the
stream reader in the correct position.
"""
expected_sets = {
"c1" : "interleaved-charsets-c1.nex",
"c2" : "interleaved-charsets-c2.nex",
"c3" : "interleaved-charsets-c3.nex",
}
self.verify_subsets('interleaved-charsets-all.nex', expected_sets)
if __name__ == "__main__":
unittest.main()
|