2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
23 import static org.testng.ConversionUtils.wrapDataProvider;
25 import jalview.analysis.NJTree;
26 import jalview.analysis.SequenceIdMatcher;
27 import jalview.datamodel.SequenceI;
28 import jalview.datamodel.SequenceNode;
30 import java.util.Arrays;
31 import java.util.Collection;
32 import java.util.Vector;
34 import org.junit.runners.Parameterized.Parameters;
35 import org.testng.Assert;
36 import org.testng.AssertJUnit;
37 import org.testng.annotations.AfterClass;
38 import org.testng.annotations.BeforeClass;
39 import org.testng.annotations.Factory;
40 import org.testng.annotations.Test;
46 public class NewickFileTests
50 public static Object[] factoryData()
52 return wrapDataProvider(NewickFileTests.class, data());
56 public static Collection data()
59 .asList(new Object[][] {
62 "Simple uniref50 newick",
63 "(((FER_BRANA:128.0,FER3_RAPSA:128.0):50.75,FER_CAPAA:178.75):121.94443,(Q93Z60_ARATH:271.45456,((O80429_MAIZE:183.0,FER1_MAIZE:183.0):30.5,((Q7XA98_TRIPR:90.0,FER1_PEA:90.0):83.32143,(((FER2_ARATH:64.0,FER1_ARATH:64.0):94.375,(FER1_SPIOL:124.5,FER1_MESCR:124.5):33.875):6.4166718,((Q93XJ9_SOLTU:33.5,FER1_SOLLC:33.5):49.0,FER_CAPAN:82.5):82.29167):8.529755):40.178574):57.95456):29.239868);" },
66 "('Syn_PROSU-1_IIh_3d(CA4)|CK_Syn_PROSU-1_1907':1.0638313,'Syn_MINOS11_5.3_3d(CA4)|CK_Syn_MINOS11_750':1.063831);" },
68 "Tree with double escaped comma in node",
69 "('Syn_PROSU-1_IIh_3d(CA4)|CK_Syn_PROSU-1_1907':1.0638313,'Syn_MINOS11_5.3_3d(CA4)''|CK_Syn_MINOS11_750':1.063831);" } });
72 String name, testTree;
74 public NewickFileTests(String _name, String _testTree)
77 this.testTree = _testTree;
81 * @throws java.lang.Exception
83 @BeforeClass(alwaysRun = true)
84 public static void setUpBeforeClass() throws Exception
88 @Test(groups = { "Functional" })
89 public void testTreeIO() throws Exception
91 String stage = "Init", treename = " '" + name + "' :";
94 stage = "Parsing testTree " + treename;
95 System.out.println(treename + "\n" + testTree);
96 NewickFile nf = new NewickFile(testTree, FormatAdapter.PASTE);
98 AssertJUnit.assertTrue(
99 stage + "Invalid Tree '" + nf.getWarningMessage() + "'",
101 SequenceNode tree = nf.getTree();
102 AssertJUnit.assertTrue(stage + "Null Tree", tree != null);
103 stage = "Creating newick file from testTree " + treename;
104 String gentree = new NewickFile(tree).print(nf.HasBootstrap(),
106 AssertJUnit.assertTrue(stage + "Empty string generated",
107 gentree != null && gentree.trim().length() > 0);
108 stage = "Parsing regenerated testTree " + treename;
109 NewickFile nf_regen = new NewickFile(gentree, FormatAdapter.PASTE);
111 AssertJUnit.assertTrue(
112 stage + "Newick file is invalid ('"
113 + nf_regen.getWarningMessage() + "')",
115 SequenceNode tree_regen = nf.getTree();
116 AssertJUnit.assertTrue(stage + "Null Tree", tree_regen != null);
117 stage = "Compare original and generated tree" + treename;
120 oseqs = new NJTree(new SequenceI[0], nf).findLeaves(nf.getTree(),
122 AssertJUnit.assertTrue(stage + "No nodes in original tree.",
124 SequenceI[] olsqs = new SequenceI[oseqs.size()];
125 for (int i = 0, iSize = oseqs.size(); i < iSize; i++)
127 olsqs[i] = (SequenceI) ((SequenceNode) oseqs.get(i)).element();
129 nseqs = new NJTree(new SequenceI[0], nf_regen).findLeaves(
130 nf_regen.getTree(), new Vector());
131 AssertJUnit.assertTrue(stage + "No nodes in regerated tree.",
133 SequenceI[] nsqs = new SequenceI[nseqs.size()];
134 for (int i = 0, iSize = nseqs.size(); i < iSize; i++)
136 nsqs[i] = (SequenceI) ((SequenceNode) nseqs.get(i)).element();
138 AssertJUnit.assertTrue(stage
139 + " Different number of leaves (original " + olsqs.length
140 + " and regen " + nsqs.length + ")",
141 olsqs.length == nsqs.length);
142 SequenceIdMatcher omatcher = new SequenceIdMatcher(olsqs), nmatcher = new SequenceIdMatcher(
145 SequenceI[] osmatches = omatcher.findIdMatch(nsqs);
146 SequenceI[] nsmatches = nmatcher.findIdMatch(olsqs);
148 for (int i = 0, iSize = nseqs.size(); i < iSize; i++)
150 if (nsmatches[i] == null)
152 warns += "\noriginal sequence ID '" + olsqs[i].getName()
153 + "' wasn't found in regenerated set.";
155 if (osmatches[i] == null)
157 warns += "\nregenerated sequence ID '" + nsqs[i].getName()
158 + "' wasn't found in original set.";
162 if (warns.length() > 0)
164 Assert.fail(stage + warns);
166 } catch (Exception x)
168 throw (new Exception(stage + "Exception raised", x));
173 * @throws java.lang.Exception
176 public static void tearDownAfterClass() throws Exception