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