6 import static org.junit.Assert.*;
8 import java.util.Arrays;
9 import java.util.Collection;
10 import java.util.Iterator;
11 import java.util.Vector;
13 import jalview.analysis.NJTree;
14 import jalview.analysis.SequenceIdMatcher;
15 import jalview.datamodel.SequenceI;
16 import jalview.datamodel.SequenceNode;
18 import org.jmol.util.ArrayUtil;
19 import org.junit.AfterClass;
20 import org.junit.BeforeClass;
21 import org.junit.Test;
22 import org.junit.runner.RunWith;
23 import org.junit.runners.Parameterized;
24 import org.junit.runners.Parameterized.Parameters;
30 @RunWith(Parameterized.class)
31 public class NewickFileTests
35 public static Collection data()
38 .asList(new Object[][]
43 "Simple uniref50 newick",
44 "(((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);" },
48 "('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);" },
51 "Tree with double escaped comma in node",
52 "('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);" } });
55 String name, testTree;
57 public NewickFileTests(String _name, String _testTree)
60 this.testTree = _testTree;
64 * @throws java.lang.Exception
67 public static void setUpBeforeClass() throws Exception
72 public void testTreeIO() throws Exception
74 String stage = "Init", treename = " '" + name + "' :";
77 stage = "Parsing testTree " + treename;
78 System.out.println(treename + "\n" + testTree);
79 NewickFile nf = new NewickFile(testTree, FormatAdapter.PASTE);
81 assertTrue(stage + "Invalid Tree '" + nf.getWarningMessage() + "'",
83 SequenceNode tree = nf.getTree();
84 assertTrue(stage + "Null Tree", tree != null);
85 stage = "Creating newick file from testTree " + treename;
86 String gentree = new NewickFile(tree).print(nf.HasBootstrap(),
88 assertTrue(stage + "Empty string generated", gentree != null
89 && gentree.trim().length() > 0);
90 stage = "Parsing regenerated testTree " + treename;
91 NewickFile nf_regen = new NewickFile(gentree, FormatAdapter.PASTE);
94 stage + "Newick file is invalid ('"
95 + nf_regen.getWarningMessage() + "')",
97 SequenceNode tree_regen = nf.getTree();
98 assertTrue(stage + "Null Tree", tree_regen != null);
99 stage = "Compare original and generated tree" + treename;
102 oseqs = new NJTree(new SequenceI[0], nf).findLeaves(nf.getTree(),
104 assertTrue(stage + "No nodes in original tree.", oseqs.size() > 0);
105 SequenceI[] olsqs = new SequenceI[oseqs.size()];
106 for (int i = 0, iSize = oseqs.size(); i < iSize; i++)
108 olsqs[i] = (SequenceI) ((SequenceNode) oseqs.get(i)).element();
110 nseqs = (Vector) new NJTree(new SequenceI[0], nf_regen).findLeaves(
111 nf_regen.getTree(), new Vector());
112 assertTrue(stage + "No nodes in regerated tree.", nseqs.size() > 0);
113 SequenceI[] nsqs = new SequenceI[nseqs.size()];
114 for (int i = 0, iSize = nseqs.size(); i < iSize; i++)
116 nsqs[i] = (SequenceI) ((SequenceNode) nseqs.get(i)).element();
118 assertTrue(stage + " Different number of leaves (original "
119 + olsqs.length + " and regen " + nsqs.length + ")",
120 olsqs.length == nsqs.length);
121 SequenceIdMatcher omatcher = new SequenceIdMatcher(olsqs), nmatcher = new SequenceIdMatcher(
124 SequenceI[] osmatches = omatcher.findIdMatch(nsqs);
125 SequenceI[] nsmatches = nmatcher.findIdMatch(olsqs);
127 for (int i = 0, iSize = nseqs.size(); i < iSize; i++)
129 if (nsmatches[i] == null)
131 warns += "\noriginal sequence ID '" + olsqs[i].getName()
132 + "' wasn't found in regenerated set.";
134 if (osmatches[i] == null)
136 warns += "\nregenerated sequence ID '" + nsqs[i].getName()
137 + "' wasn't found in original set.";
141 if (warns.length() > 0)
145 } catch (Exception x)
147 throw (new Exception(stage + "Exception raised", x));
152 * @throws java.lang.Exception
155 public static void tearDownAfterClass() throws Exception