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