236e354a3d2efce168f4d78b8c113f3f27a16a27
[jalview.git] / test / jalview / analysis / AverageDistanceEngineTest.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.analysis;
22
23 import java.io.File;
24 import java.io.FileInputStream;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.util.ArrayList;
28 import java.util.List;
29 import java.util.Map;
30
31 import org.junit.Assert;
32 import org.testng.annotations.BeforeClass;
33 import org.testng.annotations.BeforeMethod;
34 import org.testng.annotations.Test;
35
36 import jalview.bin.Cache;
37 import jalview.bin.Console;
38 import jalview.datamodel.AlignmentAnnotation;
39 import jalview.datamodel.AlignmentI;
40 import jalview.datamodel.BinaryNode;
41 import jalview.datamodel.ContactMatrixI;
42 import jalview.datamodel.SequenceI;
43 import jalview.gui.AlignFrame;
44 import jalview.gui.JvOptionPane;
45 import jalview.io.DataSourceType;
46 import jalview.io.FastaFile;
47 import jalview.io.FileLoader;
48 import jalview.io.FormatAdapter;
49 import jalview.util.Platform;
50 import jalview.ws.datamodel.alphafold.PAEContactMatrix;
51
52 public class AverageDistanceEngineTest
53 {
54
55   @BeforeClass(alwaysRun = true)
56   public void setUpJvOptionPane()
57   {
58     JvOptionPane.setInteractiveMode(false);
59     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
60   }
61
62   @BeforeMethod(alwaysRun = true)
63   public void loadProperties()
64   {
65     Cache.loadProperties("test/jalview/bin/TestProps.jvprops");
66   }
67
68   @Test(groups = { "Functional" })
69   public void testUPGMAEngine() throws Exception
70   {
71     AlignFrame af = new FileLoader(false).LoadFileWaitTillLoaded(
72             "examples/test_fab41.result/sample.a3m", DataSourceType.FILE);
73     AlignmentI seqs = af.getViewport().getAlignment();
74     SequenceI target = seqs.getSequenceAt(0);
75     File testPAE = new File(
76             "examples/test_fab41.result/test_fab41_predicted_aligned_error_v1.json");
77     List<Object> pae_obj = (List<Object>) Platform
78             .parseJSON(new FileInputStream(testPAE));
79     if (pae_obj == null)
80     {
81       Assert.fail("JSON PAE file did not parse properly.");
82     }
83     ContactMatrixI matrix = new PAEContactMatrix(target,
84             (Map<String, Object>) pae_obj.get(0));
85     AlignmentAnnotation aa = target.addContactList(matrix);
86     System.out.println("Matrix has max=" + matrix.getMax() + " and min="
87             + matrix.getMin());
88     long start = System.currentTimeMillis();
89     AverageDistanceEngine clusterer = new AverageDistanceEngine(
90             af.getViewport(), null, matrix, false);
91     System.out.println("built a tree in "
92             + (System.currentTimeMillis() - start) * 0.001 + " seconds.");
93     StringBuffer sb = new StringBuffer();
94     System.out.println("Newick string\n"
95             + new jalview.io.NewickFile(clusterer.getTopNode(), true, true)
96                     .print());
97
98     double height = clusterer.findHeight(clusterer.getTopNode());
99     // compute height fraction to cut
100     // PAE matrixes are absolute measure in angstrom, so
101     // cluster all regions within threshold (e.g. 2A) - if height above
102     // threshold. Otherwise all nodes are in one cluster
103     double thr = .2;
104     List<BinaryNode> groups;
105     if (height > thr)
106     {
107       float cut = (float) (thr / height);
108       System.out.println("Threshold " + cut + " for height=" + height);
109       groups = clusterer.groupNodes(cut);
110     }
111     else
112     {
113       groups = new ArrayList<BinaryNode>();
114       groups.add(clusterer.getTopNode());
115     }
116     int n = 1;
117     for (BinaryNode root : groups)
118     {
119       System.out.println("Cluster " + n++);
120       for (BinaryNode leaf : clusterer.findLeaves(root))
121       {
122         System.out.print(" " + leaf.getName());
123       }
124       System.out.println("\\");
125     }
126   }
127
128 }