7c068cbfda5e90f9bc6e8ff1789df2c8f7f3d6c7
[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.List;
8 import java.util.Map;
9
10 import org.junit.Assert;
11 import org.testng.annotations.BeforeClass;
12 import org.testng.annotations.BeforeMethod;
13 import org.testng.annotations.Test;
14
15 import jalview.bin.Cache;
16 import jalview.bin.Console;
17 import jalview.datamodel.AlignmentAnnotation;
18 import jalview.datamodel.AlignmentI;
19 import jalview.datamodel.BinaryNode;
20 import jalview.datamodel.ContactMatrixI;
21 import jalview.datamodel.SequenceI;
22 import jalview.gui.AlignFrame;
23 import jalview.gui.JvOptionPane;
24 import jalview.io.DataSourceType;
25 import jalview.io.FastaFile;
26 import jalview.io.FileLoader;
27 import jalview.io.FormatAdapter;
28 import jalview.util.Platform;
29 import jalview.ws.datamodel.alphafold.PAEContactMatrix;
30
31 public class AverageDistanceEngineTest
32 {
33
34     @BeforeClass(alwaysRun = true)
35     public void setUpJvOptionPane()
36     {
37       JvOptionPane.setInteractiveMode(false);
38       JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
39     }
40
41     @BeforeMethod(alwaysRun = true)
42     public void loadProperties()
43     {
44       Cache.loadProperties("test/jalview/bin/TestProps.jvprops");
45     }
46     @Test
47     public void testUPGMAEngine() throws Exception
48     {
49       AlignFrame af = new FileLoader(false).LoadFileWaitTillLoaded("examples/test_fab41.result/sample.a3m",DataSourceType.FILE);
50       AlignmentI seqs = af.getViewport().getAlignment();
51       SequenceI target = seqs.getSequenceAt(0);
52       File testPAE = new File("examples/test_fab41.result/test_fab41_predicted_aligned_error_v1.json");
53       List<Object> pae_obj = (List<Object>) Platform.parseJSON(new FileInputStream(testPAE));
54       if (pae_obj == null)
55       {
56         Assert.fail("JSON PAE file did not parse properly.");
57       }
58       ContactMatrixI matrix = new PAEContactMatrix(target,
59               (Map<String, Object>) pae_obj.get(0));
60       AlignmentAnnotation aa = target.addContactList(matrix);
61       long start = System.currentTimeMillis();
62       AverageDistanceEngine clusterer = new AverageDistanceEngine(af.getViewport(), null, matrix);
63       System.out.println("built a tree in "+(System.currentTimeMillis()-start)*0.001+" seconds.");
64       StringBuffer sb = new StringBuffer(); 
65       System.out.println("Newick string\n"+      new jalview.io.NewickFile(clusterer.getTopNode(),true,true).print());
66       
67       clusterer.findHeight(clusterer.getTopNode());
68       List<BinaryNode> groups = clusterer.groupNodes(0.8f);
69       int n=1;
70       for (BinaryNode root:groups)
71       {
72         System.out.println("Cluster "+n++);
73         for (BinaryNode leaf:clusterer.findLeaves(root))
74         {
75           System.out.print(" "+leaf.getName());
76         }
77         System.out.println("\\");
78       }
79       
80     }
81
82 }