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()
67 .asList(new Object[][] {
70 "Simple uniref50 newick",
71 "(((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);" },
74 "('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);" },
76 "Tree with double escaped comma in node",
77 "('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);" } });
80 String name, testTree;
82 public NewickFileTests(String _name, String _testTree)
85 this.testTree = _testTree;
89 * @throws java.lang.Exception
91 @BeforeClass(alwaysRun = true)
92 public static void setUpBeforeClass() throws Exception
96 @Test(groups = { "Functional" })
97 public void testTreeIO() throws Exception
99 String stage = "Init", treename = " '" + name + "' :";
102 stage = "Parsing testTree " + treename;
103 System.out.println(treename + "\n" + testTree);
104 NewickFile nf = new NewickFile(testTree, DataSourceType.PASTE);
106 AssertJUnit.assertTrue(
107 stage + "Invalid Tree '" + nf.getWarningMessage() + "'",
109 SequenceNode tree = nf.getTree();
110 AssertJUnit.assertTrue(stage + "Null Tree", tree != null);
111 stage = "Creating newick file from testTree " + treename;
112 String gentree = new NewickFile(tree).print(nf.HasBootstrap(),
114 AssertJUnit.assertTrue(stage + "Empty string generated",
115 gentree != null && gentree.trim().length() > 0);
116 stage = "Parsing regenerated testTree " + treename;
117 NewickFile nf_regen = new NewickFile(gentree, DataSourceType.PASTE);
119 AssertJUnit.assertTrue(
120 stage + "Newick file is invalid ('"
121 + nf_regen.getWarningMessage() + "')",
123 SequenceNode tree_regen = nf.getTree();
124 AssertJUnit.assertTrue(stage + "Null Tree", tree_regen != null);
125 stage = "Compare original and generated tree" + treename;
127 Vector<SequenceNode> oseqs, nseqs;
128 oseqs = new TreeModel(new SequenceI[0], null, nf).findLeaves(nf
130 AssertJUnit.assertTrue(stage + "No nodes in original tree.",
132 SequenceI[] olsqs = new SequenceI[oseqs.size()];
133 for (int i = 0, iSize = oseqs.size(); i < iSize; i++)
135 olsqs[i] = (SequenceI) oseqs.get(i).element();
137 nseqs = new TreeModel(new SequenceI[0], null, nf_regen)
140 AssertJUnit.assertTrue(stage + "No nodes in regerated tree.",
142 SequenceI[] nsqs = new SequenceI[nseqs.size()];
143 for (int i = 0, iSize = nseqs.size(); i < iSize; i++)
145 nsqs[i] = (SequenceI) nseqs.get(i).element();
147 AssertJUnit.assertTrue(stage
148 + " Different number of leaves (original " + olsqs.length
149 + " and regen " + nsqs.length + ")",
150 olsqs.length == nsqs.length);
151 SequenceIdMatcher omatcher = new SequenceIdMatcher(olsqs), nmatcher = new SequenceIdMatcher(
154 SequenceI[] osmatches = omatcher.findIdMatch(nsqs);
155 SequenceI[] nsmatches = nmatcher.findIdMatch(olsqs);
157 for (int i = 0, iSize = nseqs.size(); i < iSize; i++)
159 if (nsmatches[i] == null)
161 warns += "\noriginal sequence ID '" + olsqs[i].getName()
162 + "' wasn't found in regenerated set.";
164 if (osmatches[i] == null)
166 warns += "\nregenerated sequence ID '" + nsqs[i].getName()
167 + "' wasn't found in original set.";
171 if (warns.length() > 0)
173 Assert.fail(stage + warns);
175 } catch (Exception x)
177 throw (new Exception(stage + "Exception raised", x));
182 * @throws java.lang.Exception
184 @AfterClass(alwaysRun = true)
185 public static void tearDownAfterClass() throws Exception