JAL-3438 spotless for 2.11.2.0
[jalview.git] / test / jalview / io / NewickFileTests.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
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.
11  *  
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.
16  * 
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.
20  */
21 package jalview.io;
22
23 import static org.testng.ConversionUtils.wrapDataProvider;
24
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;
30
31 import java.util.Arrays;
32 import java.util.Collection;
33 import java.util.Vector;
34
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;
42
43 /**
44  * @author jimp
45  * 
46  */
47 public class NewickFileTests
48 {
49
50   @BeforeClass(alwaysRun = true)
51   public void setUpJvOptionPane()
52   {
53     JvOptionPane.setInteractiveMode(false);
54     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
55   }
56
57   @Factory
58   public static Object[] factoryData()
59   {
60     return wrapDataProvider(NewickFileTests.class, data());
61   }
62
63   @Parameters
64   public static Collection data()
65   {
66     return Arrays.asList(new Object[][] {
67
68         new String[]
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);" },
71         new String[]
72         { "Tree with quotes",
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);" },
74         new String[]
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);" } });
77   };
78
79   String name, testTree;
80
81   public NewickFileTests(String _name, String _testTree)
82   {
83     this.name = _name;
84     this.testTree = _testTree;
85   }
86
87   /**
88    * @throws java.lang.Exception
89    */
90   @BeforeClass(alwaysRun = true)
91   public static void setUpBeforeClass() throws Exception
92   {
93   }
94
95   @Test(groups = { "Functional" })
96   public void testTreeIO() throws Exception
97   {
98     String stage = "Init", treename = " '" + name + "' :";
99     try
100     {
101       stage = "Parsing testTree " + treename;
102       System.out.println(treename + "\n" + testTree);
103       NewickFile nf = new NewickFile(testTree, DataSourceType.PASTE);
104       nf.parse();
105       AssertJUnit.assertTrue(
106               stage + "Invalid Tree '" + nf.getWarningMessage() + "'",
107               nf.isValid());
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(),
112               nf.HasDistances());
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);
117       nf_regen.parse();
118       AssertJUnit.assertTrue(
119               stage + "Newick file is invalid ('"
120                       + nf_regen.getWarningMessage() + "')",
121               nf_regen.isValid());
122       SequenceNode tree_regen = nf.getTree();
123       AssertJUnit.assertTrue(stage + "Null Tree", tree_regen != null);
124       stage = "Compare original and generated tree" + treename;
125
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.",
130               oseqs.size() > 0);
131       SequenceI[] olsqs = new SequenceI[oseqs.size()];
132       for (int i = 0, iSize = oseqs.size(); i < iSize; i++)
133       {
134         olsqs[i] = (SequenceI) oseqs.get(i).element();
135       }
136       nseqs = new TreeModel(new SequenceI[0], null, nf_regen)
137               .findLeaves(nf_regen.getTree());
138       AssertJUnit.assertTrue(stage + "No nodes in regerated tree.",
139               nseqs.size() > 0);
140       SequenceI[] nsqs = new SequenceI[nseqs.size()];
141       for (int i = 0, iSize = nseqs.size(); i < iSize; i++)
142       {
143         nsqs[i] = (SequenceI) nseqs.get(i).element();
144       }
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);
151
152       SequenceI[] osmatches = omatcher.findIdMatch(nsqs);
153       SequenceI[] nsmatches = nmatcher.findIdMatch(olsqs);
154       String warns = "";
155       for (int i = 0, iSize = nseqs.size(); i < iSize; i++)
156       {
157         if (nsmatches[i] == null)
158         {
159           warns += "\noriginal sequence ID '" + olsqs[i].getName()
160                   + "' wasn't found in regenerated set.";
161         }
162         if (osmatches[i] == null)
163         {
164           warns += "\nregenerated sequence ID '" + nsqs[i].getName()
165                   + "' wasn't found in original set.";
166         }
167       }
168
169       if (warns.length() > 0)
170       {
171         Assert.fail(stage + warns);
172       }
173     } catch (Exception x)
174     {
175       throw (new Exception(stage + "Exception raised", x));
176     }
177   }
178
179   /**
180    * @throws java.lang.Exception
181    */
182   @AfterClass(alwaysRun = true)
183   public static void tearDownAfterClass() throws Exception
184   {
185   }
186
187 }