JAL-1950 represent duplicate sequences by their representative in the hit alignment
[jalview.git] / test / jalview / ws / ebi / HmmerJSONProcessTest.java
1 package jalview.ws.ebi;
2
3 import jalview.datamodel.AlignmentAnnotation;
4 import jalview.datamodel.AlignmentI;
5 import jalview.datamodel.SequenceI;
6 import jalview.io.FileParse;
7 import jalview.io.FormatAdapter;
8
9 import java.io.File;
10
11 import org.json.simple.JSONObject;
12 import org.json.simple.parser.JSONParser;
13 import org.testng.Assert;
14 import org.testng.annotations.Test;
15
16 public class HmmerJSONProcessTest {
17   public static File alignmentFragFile = new File(
18           "examples/testdata/hmmer3/alignment_frag.fa.gz");
19
20   private AlignmentI getSearchResultFragmentAlignment() throws Exception
21   {
22     AlignmentI alf = new FormatAdapter().readFile(
23             alignmentFragFile.getAbsolutePath(), FormatAdapter.FILE,
24             "FASTA");
25
26     return alf;
27   }
28
29   public static File alignmentResultFile = new File(
30           "examples/testdata/hmmer3/alignment_res.fa.gz");
31
32   private AlignmentI getSearchResultAlignment() throws Exception
33   {
34     AlignmentI alf = new FormatAdapter().readFile(
35             alignmentResultFile.getAbsolutePath(), FormatAdapter.FILE,
36             "FASTA");
37
38     return alf;
39   }
40
41   public static String hitTestFile = "examples/testdata/hmmer3/hit_fragment.json.gz",
42           hmmerResultFile = "examples/testdata/hmmer3/hmmeresult.json.gz";
43
44
45   @Test(groups = { "Functional" })
46   public void parseHitTest() throws Exception
47   {
48
49     Assert.assertTrue(new File(hitTestFile).exists(),
50             "Can't find test data.\n"
51             + hitTestFile);
52     JSONParser jp = new JSONParser();
53     // read JSON in same way - via fileparse
54     Object hitfragment = jp.parse(new FileParse(hitTestFile,
55             FormatAdapter.FILE).getReader());
56     Assert.assertTrue((hitfragment instanceof JSONObject),
57             "Didn't find a JSON object map in " + hitTestFile);
58     AlignmentI searchResult = getSearchResultFragmentAlignment();
59
60     Assert.assertTrue(searchResult != null && searchResult.getHeight() > 0,
61             "Didn't read search result alignment from " + alignmentFragFile);
62
63     HmmerJSONProcessor hjsp = new HmmerJSONProcessor(searchResult);
64     hjsp.addHit((JSONObject) hitfragment, 1);
65     // check that
66     // scores, posterior probabilities and stuff exist.
67   }
68
69   @Test(groups = { "Functional" })
70   public void parseJsonResultTest() throws Exception
71   {
72
73     Assert.assertTrue(new File(hmmerResultFile).exists(),
74             "Can't find test data.\n" + hmmerResultFile);
75
76     AlignmentI searchResult = getSearchResultAlignment();
77
78     Assert.assertTrue(searchResult != null && searchResult.getHeight() > 0,
79             "Didn't read search result alignment from " + alignmentFragFile);
80
81     HmmerJSONProcessor hjsp = new HmmerJSONProcessor(searchResult);
82     hjsp.parseFrom(new FileParse(hmmerResultFile, FormatAdapter.FILE));
83     AlignmentAnnotation[] aa = searchResult.getSequenceAt(5)
84             .getAnnotation();
85     Assert.assertNotNull(aa);
86     Assert.assertEquals(aa.length, 3,
87             "didn't get expected set of annotation.\n");
88     // DPTSERWFHGHLSGKEAEKLLTeKGKHGSFLVRESQSHPGDFVLSVRTgddkgesndgKSKVTHVMIR-CQELKYDVGGGERFDSLTDLVEHYKKNPmvet
89     // LGTVLQLKQP
90     // 5789*****************9799***********************999998888888********.99**************************9999
91     // 899999*999
92     // AlignmentAnnotation
93     // 101 == 8
94     String seq = "tLGT";
95     SequenceI s5 = searchResult.getSequenceAt(5);
96     Assert.assertEquals(
97             s5.getSubSequence(s5.findIndex(225), s5.findIndex(229))
98                     .getSequenceAsString(),
99             seq);
100     int pos = s5.findIndex(226);
101     for (AlignmentAnnotation an : aa)
102     {
103       if (an.label.startsWith("Posterior"))
104       {
105         Assert.assertEquals(an.annotations[pos].value, 8f);
106
107       }
108     }
109     ;
110     // check that
111     // scores, posterior probabilities and stuff exist.
112   }
113   // Groovy test
114   // def al = Jalview.getAlignFrames()[0].getViewport().getAlignment()
115   // def jproc = new jalview.ws.ebi.HmmerJSONProcessor(al)
116   // jproc.parseFrom(new
117   // jalview.io.FileParse("examples/testdata/hmmer3/hmmeresult.json.gz","File"))
118   // jproc.updateView(Jalview.getAlignFrames()[0].getViewport())
119
120 }