From e2e9c56d6e257a5d9f1285b07ffe56a002454125 Mon Sep 17 00:00:00 2001 From: Jim Procter Date: Fri, 14 Jun 2013 15:06:42 +0100 Subject: [PATCH] JAL-1316 JAL-1313 newick IO test harness --- test/jalview/io/NewickFileTests.java | 159 ++++++++++++++++++++++++++++++++++ 1 file changed, 159 insertions(+) create mode 100644 test/jalview/io/NewickFileTests.java diff --git a/test/jalview/io/NewickFileTests.java b/test/jalview/io/NewickFileTests.java new file mode 100644 index 0000000..2ee3623 --- /dev/null +++ b/test/jalview/io/NewickFileTests.java @@ -0,0 +1,159 @@ +/** + * + */ +package jalview.io; + +import static org.junit.Assert.*; + +import java.util.Arrays; +import java.util.Collection; +import java.util.Iterator; +import java.util.Vector; + +import jalview.analysis.NJTree; +import jalview.analysis.SequenceIdMatcher; +import jalview.datamodel.SequenceI; +import jalview.datamodel.SequenceNode; + +import org.jmol.util.ArrayUtil; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameters; + +/** + * @author jimp + * + */ +@RunWith(Parameterized.class) +public class NewickFileTests +{ + + @Parameters + public static Collection data() + { + return Arrays + .asList(new Object[][] + { + + new String[] + { + "Simple uniref50 newick", + "(((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);" }, + new String[] + { + "Tree with quotes", + "('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);" }, + new String[] + { + "Tree with double escaped comma in node", + "('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);" } }); + }; + + String name, testTree; + + public NewickFileTests(String _name, String _testTree) + { + this.name = _name; + this.testTree = _testTree; + } + + /** + * @throws java.lang.Exception + */ + @BeforeClass + public static void setUpBeforeClass() throws Exception + { + } + + @Test + public void testTreeIO() throws Exception + { + String stage = "Init", treename = " '" + name + "' :"; + try + { + stage = "Parsing testTree " + treename; + System.out.println(treename + "\n" + testTree); + NewickFile nf = new NewickFile(testTree, FormatAdapter.PASTE); + nf.parse(); + assertTrue(stage + "Invalid Tree '" + nf.getWarningMessage() + "'", + nf.isValid()); + SequenceNode tree = nf.getTree(); + assertTrue(stage + "Null Tree", tree != null); + stage = "Creating newick file from testTree " + treename; + String gentree = new NewickFile(tree).print(nf.HasBootstrap(), + nf.HasDistances()); + assertTrue(stage + "Empty string generated", gentree != null + && gentree.trim().length() > 0); + stage = "Parsing regenerated testTree " + treename; + NewickFile nf_regen = new NewickFile(gentree, FormatAdapter.PASTE); + nf_regen.parse(); + assertTrue( + stage + "Newick file is invalid ('" + + nf_regen.getWarningMessage() + "')", + nf_regen.isValid()); + SequenceNode tree_regen = nf.getTree(); + assertTrue(stage + "Null Tree", tree_regen != null); + stage = "Compare original and generated tree" + treename; + + Vector oseqs, nseqs; + oseqs = new NJTree(new SequenceI[0], nf).findLeaves(nf.getTree(), + new Vector()); + assertTrue(stage + "No nodes in original tree.", oseqs.size() > 0); + SequenceI[] olsqs = new SequenceI[oseqs.size()]; + for (int i = 0, iSize = oseqs.size(); i < iSize; i++) + { + olsqs[i] = (SequenceI) ((SequenceNode) oseqs.get(i)).element(); + } + nseqs = (Vector) new NJTree(new SequenceI[0], nf_regen).findLeaves( + nf_regen.getTree(), new Vector()); + assertTrue(stage + "No nodes in regerated tree.", nseqs.size() > 0); + SequenceI[] nsqs = new SequenceI[nseqs.size()]; + for (int i = 0, iSize = nseqs.size(); i < iSize; i++) + { + nsqs[i] = (SequenceI) ((SequenceNode) nseqs.get(i)).element(); + } + assertTrue(stage + " Different number of leaves (original " + + olsqs.length + " and regen " + nsqs.length + ")", + olsqs.length == nsqs.length); + SequenceIdMatcher omatcher = new SequenceIdMatcher(olsqs), nmatcher = new SequenceIdMatcher( + nsqs); + + SequenceI[] osmatches = omatcher.findIdMatch(nsqs); + SequenceI[] nsmatches = nmatcher.findIdMatch(olsqs); + String warns = ""; + for (int i = 0, iSize = nseqs.size(); i < iSize; i++) + { + if (nsmatches[i] == null) + { + warns += "\noriginal sequence ID '" + olsqs[i].getName() + + "' wasn't found in regenerated set."; + } + if (osmatches[i] == null) + { + warns += "\nregenerated sequence ID '" + nsqs[i].getName() + + "' wasn't found in original set."; + } + } + + if (warns.length() > 0) + { + fail(stage + warns); + } + } catch (Exception x) + { + throw (new Exception(stage + "Exception raised", x)); + } + } + + /** + * @throws java.lang.Exception + */ + @AfterClass + public static void tearDownAfterClass() throws Exception + { + } + +} -- 1.7.10.2