Back to index

python-biopython  1.60
Public Member Functions | Public Attributes | Private Member Functions
test_Nexus.NexusTest1 Class Reference

List of all members.

Public Member Functions

def setUp
def tearDown
def test_WriteToFileName
def test_NexusTest1
def test_TreeTest1
def test_internal_node_labels

Public Attributes

 testfile_dir
 handle

Private Member Functions

def _get_flat_nodes

Detailed Description

Definition at line 10 of file test_Nexus.py.


Member Function Documentation

def test_Nexus.NexusTest1._get_flat_nodes (   self,
  tree 
) [private]

Definition at line 385 of file test_Nexus.py.

00385 
00386     def _get_flat_nodes(self, tree):
00387         cur_nodes = [tree.node(tree.root)]
00388         nodedata = []
00389         while len(cur_nodes) > 0:
00390             new_nodes = []
00391             for cur_node in cur_nodes:
00392                 nodedata.append((cur_node.data.taxon,
00393                     cur_node.data.branchlength, cur_node.data.support,
00394                     cur_node.data.comment))
00395                 new_nodes.extend([tree.node(nid) for nid in
00396                     cur_node.get_succ()])
00397             cur_nodes = new_nodes
00398         return nodedata

Here is the caller graph for this function:

Definition at line 11 of file test_Nexus.py.

00011 
00012     def setUp(self):
00013         self.testfile_dir = "Nexus"
00014         self.handle = open(os.path.join(self.testfile_dir, 
00015             "test_Nexus_input.nex"))

Definition at line 16 of file test_Nexus.py.

00016 
00017     def tearDown(self):
00018         self.handle.close()

Handle text labels on internal nodes.

Definition at line 357 of file test_Nexus.py.

00357 
00358     def test_internal_node_labels(self):
00359         """Handle text labels on internal nodes.
00360         """
00361         ts1b = "(Cephalotaxus:125.000000,(Taxus:100.000000,Torreya:100.000000)"\
00362                "TT1:25.000000)Taxaceae:90.000000;"
00363         tree = Trees.Tree(ts1b)
00364         assert self._get_flat_nodes(tree) == [('Taxaceae', 90.0, None, None),
00365                 ('Cephalotaxus', 125.0, None, None), ('TT1', 25.0, None, None),
00366                 ('Taxus', 100.0, None, None), ('Torreya', 100.0, None, None)]
00367 
00368         ts1c = "(Cephalotaxus:125.000000,(Taxus:100.000000,Torreya:100.000000)"\
00369                 "25.000000)90.000000;"
00370         tree = Trees.Tree(ts1c)
00371         assert self._get_flat_nodes(tree) == [(None, 90.0, None, None),
00372                 ('Cephalotaxus', 125.0, None, None), (None, 25.0, None, None),
00373                 ('Taxus', 100.0, None, None), ('Torreya', 100.0, None, None)]
00374 
00375         ts2 = "(((t9:0.385832, (t8:0.445135,t4:0.41401)C:0.024032)B:0.041436,"\
00376           "t6:0.392496)A:0.0291131, t2:0.497673, ((t0:0.301171,"\
00377           "t7:0.482152)E:0.0268148, ((t5:0.0984167,t3:0.488578)G:0.0349662,"\
00378           "t1:0.130208)F:0.0318288)D:0.0273876);"
00379         tree = Trees.Tree(ts2)
00380 
00381         large_ex_handle = open(os.path.join(self.testfile_dir,
00382             "int_node_labels.nwk"))
00383         tree = Trees.Tree(large_ex_handle.read())
00384         large_ex_handle.close()

Here is the call graph for this function:

Test Nexus module

Definition at line 29 of file test_Nexus.py.

00029 
00030     def test_NexusTest1(self):
00031         """Test Nexus module"""
00032         # check data of main nexus file
00033         n=Nexus.Nexus(self.handle)
00034         self.assertEqual(os.path.normpath(n.filename),
00035                          os.path.normpath("Nexus/test_Nexus_input.nex"))
00036         self.assertEqual(n.ntax, 9)
00037         self.assertEqual(n.nchar, 48)
00038         self.assertEqual(n.datatype, "dna")
00039         self.assertEqual(n.interleave, True)
00040         self.assertEqual(n.missing, "?")
00041         self.assertEqual(n.gap, "-")
00042         self.assertEqual(n.taxlabels, ["t1",
00043                                        "t2 the name",
00044                                        "isn'that [a] strange name?",
00045                                        "one should be punished, for (that)!",
00046                                        "t5",
00047                                        "t6",
00048                                        "t7",
00049                                        "t8",
00050                                        "t9"])
00051         self.assertEqual(n.charlabels, {0: 'a',
00052                                         1: 'b',
00053                                         2: 'c',
00054                                         4: 'f',
00055                                         9: 'A',
00056                                         10: 'B',
00057                                         22: 'x',
00058                                         23: "y",
00059                                         29: "1,2,3 can't decide for a name?!",
00060                                         47: "final"})
00061         self.assertEqual(n.charsets, {
00062             "big":        [0, 2, 4, 6],
00063             "bigchunk":   [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],
00064             "byname":     [0, 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],
00065             "c1":         [0, 1, 2, 3, 4, 5, 6, 7],
00066             "c2":         [8, 9, 10, 11, 12, 13, 14, 15],
00067             "c3":         [16, 17, 18, 19, 20, 21, 22, 23],
00068             "firsthalf":  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23],
00069             "mix":        [0, 1, 4, 7, 10, 13, 16, 19, 22, 25, 28, 31, 34, 37, 40, 43, 46],
00070             "mux":        [0, 1, 4, 7, 8, 10, 13, 16, 17, 18, 19, 20, 21, 22, 23, 25, 28, 31, 34, 37, 40, 43, 46],
00071             "pos1":       [0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45],
00072             "pos2":       [1, 4, 7, 10, 13, 16, 19, 22, 25, 28, 31, 34, 37, 40, 43, 46],
00073             "pos3":       [2, 5, 8, 11, 14, 17, 20, 23, 26, 29, 32, 35, 38, 41, 44, 47],
00074             "secondhalf": [24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47],
00075                                      })
00076         self.assertEqual(n.taxsets, {
00077             "normal":    ["isn'that [a] strange name?",
00078                           'one should be punished, for (that)!',
00079                           't1',
00080                           't5',
00081                           't6',
00082                           't8'],
00083             "reference": ["isn'that [a] strange name?",
00084                           'one should be punished, for (that)!',
00085                           't1',
00086                           't2 the name',
00087                           't5',
00088                           't6'],
00089             "tbyname1": ["isn'that [a] strange name?",
00090                           'one should be punished, for (that)!',
00091                           't1',
00092                           't2 the name',
00093                           't5',
00094                           't6'],
00095             "tbyname2": ["isn'that [a] strange name?",
00096                           'one should be punished, for (that)!',
00097                           't2 the name',
00098                           't5',
00099                           't6',
00100                           't7'],
00101             "tbyname3": ['t1',
00102                           't2 the name'],
00103                                     })
00104         self.assertEqual(len(n.charpartitions), 2)
00105         self.assertTrue('codons' in n.charpartitions)
00106         self.assertTrue('part' in n.charpartitions)
00107         self.assertEqual(n.charpartitions['codons'],
00108             {'a': [0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45],
00109              'b': [1, 4, 7, 10, 13, 16, 19, 22, 25, 28, 31, 34, 37, 40, 43, 46],
00110              'c': [2, 5, 8, 11, 14, 17, 20, 23, 26, 29, 32, 35, 38, 41, 44, 47],
00111             })
00112         self.assertEqual(n.charpartitions['part'],
00113             {"one":   [0, 1, 2, 3, 4, 5, 6, 7],
00114              "three": [16, 17, 18, 19, 20, 21, 22, 23],
00115              "two":   [8, 9, 10, 11, 12, 13, 14, 15],
00116             })
00117         self.assertEqual(n.taxpartitions.keys(), ['taxpart'])
00118         self.assertEqual(n.taxpartitions['taxpart'], 
00119             {"badnames":  ["isn'that [a] strange name?",
00120                            'one should be punished, for (that)!',
00121                            't2 the name'],
00122              "goodnames": ['t1', 't5', 't6', 't7', 't8', 't9'],
00123             })
00124 
00125         # now we check excluding characters, deleting taxa,
00126         # and exporting adjusted sets
00127         f1=tempfile.NamedTemporaryFile("w+")
00128         n.write_nexus_data(f1,
00129                            delete=['t1','t7'],
00130                            exclude=n.invert(n.charsets['big']))
00131         f1.seek(0)
00132         nf1=Nexus.Nexus(f1)
00133         self.assertEqual(os.path.normpath(nf1.filename),
00134                          os.path.normpath(f1.name))
00135         self.assertEqual(nf1.ntax, 7)
00136         self.assertEqual(nf1.nchar, 4)
00137         self.assertEqual(nf1.datatype, "dna")
00138         self.assertEqual(nf1.interleave, False)
00139         self.assertEqual(nf1.missing, "?")
00140         self.assertEqual(nf1.gap, "-")
00141         self.assertEqual(nf1.taxlabels, ["t2 the name",
00142                                          "isn'that [a] strange name?",
00143                                          "one should be punished, for (that)!",
00144                                          "t5",
00145                                          "t6",
00146                                          "t8",
00147                                          "t9"])
00148         self.assertEqual(nf1.charlabels, {0: 'a', 1: 'c', 2: 'f'})
00149         self.assertEqual(nf1.charsets, {'big':       [0, 1, 2, 3],
00150                                         'bigchunk':  [1, 2, 3],
00151                                         'byname':    [0, 2, 3],
00152                                         'c1':        [0, 1, 2, 3],
00153                                         'firsthalf': [0, 1, 2, 3],
00154                                         'mix':       [0, 2],
00155                                         'mux':       [0, 2],
00156                                         'pos1':      [0, 3],
00157                                         'pos2':      [2],
00158                                         'pos3':      [1],
00159                                        })
00160         self.assertEqual(nf1.taxsets, {
00161             'normal':    ["isn'that [a] strange name?",
00162                           'one should be punished, for (that)!',
00163                           't5',
00164                           't6',
00165                           't8'],
00166             'reference': ["isn'that [a] strange name?",
00167                           'one should be punished, for (that)!',
00168                           't2 the name',
00169                           't5',
00170                           't6'],
00171             'tbyname1':  ["isn'that [a] strange name?",
00172                           'one should be punished, for (that)!',
00173                           't2 the name',
00174                           't5',
00175                           't6'],
00176             'tbyname2':  ["isn'that [a] strange name?",
00177                           'one should be punished, for (that)!',
00178                           't2 the name',
00179                           't5',
00180                           't6'],
00181             'tbyname3': ['t2 the name'],
00182                                        })
00183         self.assertEqual(len(nf1.charpartitions), 2)
00184         self.assertTrue('codons' in nf1.charpartitions)
00185         self.assertTrue('part' in nf1.charpartitions)
00186         self.assertEqual(nf1.charpartitions['codons'], {'a': [0, 3],
00187                                                         'b': [2],
00188                                                         'c': [1]})
00189         self.assertEqual(nf1.charpartitions['part'], {'one': [0, 1, 2, 3]})
00190 
00191         self.assertEqual(nf1.taxpartitions.keys(), ['taxpart'])
00192         self.assertEqual(nf1.taxpartitions['taxpart'],
00193             {"badnames":  ["isn'that [a] strange name?",
00194                            'one should be punished, for (that)!',
00195                            't2 the name'],
00196              "goodnames": ['t5', 't6', 't8', 't9'],
00197             })
00198 
00199         f2=tempfile.NamedTemporaryFile("w+")
00200         n.write_nexus_data(f2,
00201                            delete=['t2_the_name'],
00202                            exclude=range(3,40,4))
00203         f2.seek(0)
00204         nf2=Nexus.Nexus(f2)
00205         self.assertEqual(os.path.normpath(nf2.filename),
00206                          os.path.normpath(f2.name))
00207         self.assertEqual(nf2.ntax, 9)
00208         self.assertEqual(nf2.nchar, 38)
00209         self.assertEqual(nf2.datatype, "dna")
00210         self.assertEqual(nf2.interleave, False)
00211         self.assertEqual(nf2.missing, "?")
00212         self.assertEqual(nf2.gap, "-")
00213         self.assertEqual(nf2.taxlabels, ["t1", 
00214                                          "t2 the name", 
00215                                          "isn'that [a] strange name?", 
00216                                          "one should be punished, for (that)!", 
00217                                          "t5", 
00218                                          "t6", 
00219                                          "t7", 
00220                                          "t8", 
00221                                          "t9"])
00222         self.assertEqual(nf2.charlabels, {0: "a",
00223                                           1: "b",
00224                                           2: "c",
00225                                           3: "f",
00226                                           7: "A",
00227                                           8: "B",
00228                                           17: "x",
00229                                           22: "1,2,3 can't decide for a name?!",
00230                                           37: "final"})
00231         self.assertEqual(nf2.charsets,
00232             {"big":        [0, 2, 3, 5],
00233              "bigchunk":   [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],
00234              "byname":     [0, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22],
00235              "c1":         [0, 1, 2, 3, 4, 5],
00236              "c2":         [6, 7, 8, 9, 10, 11],
00237              "c3":         [12, 13, 14, 15, 16, 17],
00238              "firsthalf":  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17],
00239              "mix":        [0, 1, 3, 8, 10, 12, 17, 19, 21, 26, 28, 30, 33, 36],
00240              "mux":        [0, 1, 3, 6, 8, 10, 12, 13, 14, 15, 16, 17, 19, 21, 26, 28, 30, 33, 36],
00241              "pos1":       [0, 5, 7, 9, 14, 16, 18, 23, 25, 27, 32, 35],
00242              "pos2":       [1, 3, 8, 10, 12, 17, 19, 21, 26, 28, 30, 33, 36],
00243              "pos3":       [2, 4, 6, 11, 13, 15, 20, 22, 24, 29, 31, 34, 37],
00244              "secondhalf": [18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37],
00245             })
00246 
00247         self.assertEqual(nf2.taxsets,
00248             {"normal":    ["isn'that [a] strange name?",
00249                            'one should be punished, for (that)!',
00250                            't1',
00251                            't5',
00252                            't6',
00253                            't8'],
00254              "reference": ["isn'that [a] strange name?",
00255                            'one should be punished, for (that)!',
00256                            't1',
00257                            't2 the name',
00258                            't5',
00259                            't6'],
00260              "tbyname1":  ["isn'that [a] strange name?",
00261                            'one should be punished, for (that)!',
00262                            't1',
00263                            't2 the name',
00264                            't5',
00265                            't6'],
00266              "tbyname2":  ["isn'that [a] strange name?",
00267                            'one should be punished, for (that)!',
00268                            't2 the name',
00269                            't5',
00270                            't6',
00271                            't7'],
00272              "tbyname3":  ['t1',
00273                            't2 the name']})
00274         self.assertEqual(len(nf2.charpartitions), 2)
00275         self.assertTrue('codons' in nf2.charpartitions)
00276         self.assertTrue('part' in nf2.charpartitions)
00277         self.assertEqual(nf2.charpartitions['codons'],
00278             {"a": [0, 5, 7, 9, 14, 16, 18, 23, 25, 27, 32, 35],
00279              "b": [1, 3, 8, 10, 12, 17, 19, 21, 26, 28, 30, 33, 36],
00280              "c": [2, 4, 6, 11, 13, 15, 20, 22, 24, 29, 31, 34, 37],
00281             })
00282         self.assertEqual(nf2.charpartitions['part'],
00283             {"one":   [0, 1, 2, 3, 4, 5],
00284              "three": [12, 13, 14, 15, 16, 17],
00285              "two":   [6, 7, 8, 9, 10, 11],
00286             })
00287         self.assertEqual(nf2.taxpartitions.keys(), ['taxpart'])
00288         self.assertEqual(nf2.taxpartitions['taxpart'],
00289             {"badnames":  ["isn'that [a] strange name?",
00290                            'one should be punished, for (that)!',
00291                            't2 the name'],
00292              "goodnames": ['t1', 't5', 't6', 't7', 't8', 't9'],
00293             })
00294         # check the stepmatrix
00295         self.assertEqual(n.weighted_stepmatrix(name='matrix_test'),
00296         """\
00297 usertype matrix_test stepmatrix=5
00298         A        C        G        T        -
00299 [A]     .       2.40     2.57     2.43     2.43     
00300 [C]    2.40      .       2.28     2.12     2.14     
00301 [G]    2.57     2.28      .       2.31     2.31     
00302 [T]    2.43     2.12     2.31      .       2.14     
00303 [-]    2.43     2.14     2.31     2.14      .       
00304 ;
00305 """)
00306 

Test Tree module.

Definition at line 307 of file test_Nexus.py.

00307 
00308     def test_TreeTest1(self):
00309         """Test Tree module."""
00310         n=Nexus.Nexus(self.handle)
00311         t3=n.trees[2]
00312         t2=n.trees[2]
00313         t3.root_with_outgroup(['t1','t5'])
00314         self.assertEqual(str(t3), "tree tree1 = (((((('one should be punished, for (that)!','isn''that [a] strange name?'),'t2 the name'),t8,t9),t6),t7),(t5,t1));")
00315         self.assertEqual(t3.is_monophyletic(['t8','t9','t6','t7']), -1)
00316         self.assertEqual(t3.is_monophyletic(['t1','t5']), 13)
00317         t3.split(parent_id=t3.search_taxon('t9'))
00318         stdout = sys.stdout
00319         try:
00320             sys.stdout = cStringIO.StringIO()
00321             t3.display()
00322             if sys.version_info[0] == 3:
00323                 output = sys.stdout.getvalue()
00324             else:
00325                 sys.stdout.reset()
00326                 output = sys.stdout.read()
00327         finally:
00328             sys.stdout = stdout 
00329         expected = """\
00330   #                            taxon            prev            succ    brlen blen (sum)  support              comment
00331   1    'isn''that [a] strange name?'               2              []   100.00     119.84    10.00                    -
00332   2                                -               4          [3, 1]     0.40      19.84     0.30                    -
00333   3 'one should be punished, for (that)!'               2              []     0.50      20.34        -                    -
00334   4                                -               6          [2, 5]     4.00      19.44     3.00                    -
00335   5                    't2 the name'               4              []     0.30      19.74        -                    -
00336   6                                -               9       [4, 7, 8]     2.00      15.44     1.00                    -
00337   7                               t8               6              []     1.20      16.64        -                    -
00338   8                               t9               6        [17, 18]     3.40      18.84        -                    -
00339   9                                -              11         [6, 10]     0.44      13.44    33.00                    -
00340  10                               t6               9              []     1.00      14.44        -                    -
00341  11                                -              16         [9, 12]    13.00      13.00    12.00                    -
00342  12                               t7              11              []    99.90     112.90        -                    -
00343  13                                -              16        [14, 15]     0.00       0.00     0.00                    -
00344  14                               t5              13              []    99.00      99.00        -                    -
00345  15                               t1              13              []     0.98       0.98        -                    -
00346  16                                -            None        [11, 13]     0.00       0.00        -                    -
00347  17                              t90               8              []     1.00      19.84        -                    -
00348  18                              t91               8              []     1.00      19.84        -                    -
00349 
00350 Root:  16
00351 """
00352         self.assertEqual(len(output.split("\n")), len(expected.split("\n")))
00353         for l1, l2 in zip(output.split("\n"), expected.split("\n")):
00354             self.assertEqual(l1, l2)
00355         self.assertEqual(output, expected)
00356         self.assertEqual(t3.is_compatible(t2,threshold=0.3), [])

Test writing to a given filename.

Definition at line 19 of file test_Nexus.py.

00019 
00020     def test_WriteToFileName(self):
00021         """Test writing to a given filename."""
00022         filename = "Nexus/test_temp.nex"
00023         if os.path.isfile(filename):
00024             os.remove(filename)
00025         n = Nexus.Nexus(self.handle)
00026         n.write_nexus_data(filename)
00027         self.assertTrue(os.path.isfile(filename))
00028         os.remove(filename)


Member Data Documentation

Definition at line 13 of file test_Nexus.py.

Definition at line 12 of file test_Nexus.py.


The documentation for this class was generated from the following file: