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.SequenceIdMatcher;
26 import jalview.analysis.TreeModel;
27 import jalview.datamodel.SequenceI;
28 import jalview.datamodel.SequenceNode;
29 import jalview.gui.JvOptionPane;
31 import java.util.Arrays;
32 import java.util.Collection;
33 import java.util.Vector;
35 import org.junit.runners.Parameterized.Parameters;
36 import org.testng.Assert;
37 import org.testng.AssertJUnit;
38 import org.testng.annotations.AfterClass;
39 import org.testng.annotations.BeforeClass;
40 import org.testng.annotations.Factory;
41 import org.testng.annotations.Test;
47 public class NewickFileTests
50 @BeforeClass(alwaysRun = true)
51 public void setUpJvOptionPane()
53 JvOptionPane.setInteractiveMode(false);
54 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
58 public static Object[] factoryData()
60 return wrapDataProvider(NewickFileTests.class, data());
64 public static Collection data()
66 return Arrays.asList(new Object[][] {
69 { "Simple uniref50 newick",
70 "(((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);" },
73 "('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);" },
75 { "Tree with double escaped comma in node",
76 "('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);" } });
79 String name, testTree;
81 public NewickFileTests(String _name, String _testTree)
84 this.testTree = _testTree;
88 * @throws java.lang.Exception
90 @BeforeClass(alwaysRun = true)
91 public static void setUpBeforeClass() throws Exception
95 @Test(groups = { "Functional" })
96 public void testTreeIO() throws Exception
98 String stage = "Init", treename = " '" + name + "' :";
101 stage = "Parsing testTree " + treename;
102 System.out.println(treename + "\n" + testTree);
103 NewickFile nf = new NewickFile(testTree, DataSourceType.PASTE);
105 AssertJUnit.assertTrue(
106 stage + "Invalid Tree '" + nf.getWarningMessage() + "'",
108 SequenceNode tree = nf.getTree();
109 AssertJUnit.assertTrue(stage + "Null Tree", tree != null);
110 stage = "Creating newick file from testTree " + treename;
111 String gentree = new NewickFile(tree).print(nf.HasBootstrap(),
113 AssertJUnit.assertTrue(stage + "Empty string generated",
114 gentree != null && gentree.trim().length() > 0);
115 stage = "Parsing regenerated testTree " + treename;
116 NewickFile nf_regen = new NewickFile(gentree, DataSourceType.PASTE);
118 AssertJUnit.assertTrue(
119 stage + "Newick file is invalid ('"
120 + nf_regen.getWarningMessage() + "')",
122 SequenceNode tree_regen = nf.getTree();
123 AssertJUnit.assertTrue(stage + "Null Tree", tree_regen != null);
124 stage = "Compare original and generated tree" + treename;
126 Vector<SequenceNode> oseqs, nseqs;
127 oseqs = new TreeModel(new SequenceI[0], null, nf)
128 .findLeaves(nf.getTree());
129 AssertJUnit.assertTrue(stage + "No nodes in original tree.",
131 SequenceI[] olsqs = new SequenceI[oseqs.size()];
132 for (int i = 0, iSize = oseqs.size(); i < iSize; i++)
134 olsqs[i] = (SequenceI) oseqs.get(i).element();
136 nseqs = new TreeModel(new SequenceI[0], null, nf_regen)
137 .findLeaves(nf_regen.getTree());
138 AssertJUnit.assertTrue(stage + "No nodes in regerated tree.",
140 SequenceI[] nsqs = new SequenceI[nseqs.size()];
141 for (int i = 0, iSize = nseqs.size(); i < iSize; i++)
143 nsqs[i] = (SequenceI) nseqs.get(i).element();
145 AssertJUnit.assertTrue(
146 stage + " Different number of leaves (original "
147 + olsqs.length + " and regen " + nsqs.length + ")",
148 olsqs.length == nsqs.length);
149 SequenceIdMatcher omatcher = new SequenceIdMatcher(olsqs),
150 nmatcher = new SequenceIdMatcher(nsqs);
152 SequenceI[] osmatches = omatcher.findIdMatch(nsqs);
153 SequenceI[] nsmatches = nmatcher.findIdMatch(olsqs);
155 for (int i = 0, iSize = nseqs.size(); i < iSize; i++)
157 if (nsmatches[i] == null)
159 warns += "\noriginal sequence ID '" + olsqs[i].getName()
160 + "' wasn't found in regenerated set.";
162 if (osmatches[i] == null)
164 warns += "\nregenerated sequence ID '" + nsqs[i].getName()
165 + "' wasn't found in original set.";
169 if (warns.length() > 0)
171 Assert.fail(stage + warns);
173 } catch (Exception x)
175 throw (new Exception(stage + "Exception raised", x));
180 * @throws java.lang.Exception
182 @AfterClass(alwaysRun = true)
183 public static void tearDownAfterClass() throws Exception