JAL-4134 add tree distance engine test to functional
[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     @Test(groups={"Functional"})
48     public void testUPGMAEngine() throws Exception
49     {
50       AlignFrame af = new FileLoader(false).LoadFileWaitTillLoaded("examples/test_fab41.result/sample.a3m",DataSourceType.FILE);
51       AlignmentI seqs = af.getViewport().getAlignment();
52       SequenceI target = seqs.getSequenceAt(0);
53       File testPAE = new File("examples/test_fab41.result/test_fab41_predicted_aligned_error_v1.json");
54       List<Object> pae_obj = (List<Object>) Platform.parseJSON(new FileInputStream(testPAE));
55       if (pae_obj == null)
56       {
57         Assert.fail("JSON PAE file did not parse properly.");
58       }
59       ContactMatrixI matrix = new PAEContactMatrix(target,
60               (Map<String, Object>) pae_obj.get(0));
61       AlignmentAnnotation aa = target.addContactList(matrix);
62       System.out.println("Matrix has max="+matrix.getMax()+" and min="+matrix.getMin());
63       long start = System.currentTimeMillis();
64       AverageDistanceEngine clusterer = new AverageDistanceEngine(af.getViewport(), null, matrix);
65       System.out.println("built a tree in "+(System.currentTimeMillis()-start)*0.001+" seconds.");
66       StringBuffer sb = new StringBuffer(); 
67       System.out.println("Newick string\n"+      new jalview.io.NewickFile(clusterer.getTopNode(),true,true).print());
68       
69       double height = clusterer.findHeight(clusterer.getTopNode());
70       // compute height fraction to cut 
71       // PAE matrixes are absolute measure in angstrom, so 
72       // cluster all regions within threshold (e.g. 2A) - if height above threshold. Otherwise all nodes are in one cluster
73       double thr=.2;
74       List<BinaryNode> groups;
75       if (height>thr)
76       {
77         float cut = (float) (thr/height);
78         System.out.println("Threshold "+cut+" for height="+height);
79         groups = clusterer.groupNodes(cut);
80       } else{
81         groups=new ArrayList<BinaryNode>();
82         groups.add(clusterer.getTopNode());
83       }
84       int n=1;
85       for (BinaryNode root:groups)
86       {
87         System.out.println("Cluster "+n++);
88         for (BinaryNode leaf:clusterer.findLeaves(root))
89         {
90           System.out.print(" "+leaf.getName());
91         }
92         System.out.println("\\");
93       }
94       
95     }
96
97 }