611f99e4ed0dde0fcdae5c8f215fb12f164b3c3f
[jalview.git] / src / jalview / ws / ebi / HmmerJSONProcessor.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
8 import java.io.IOException;
9
10 import org.json.simple.JSONArray;
11 import org.json.simple.JSONObject;
12 import org.json.simple.parser.JSONParser;
13
14 public class HmmerJSONProcessor
15 {
16   AlignmentI resultAl;
17
18   public HmmerJSONProcessor(AlignmentI searchResult)
19   {
20     resultAl = searchResult;
21   }
22
23   public void parseFrom(FileParse jsonsource) throws IOException,
24           OutOfMemoryError
25   {
26     JSONParser hmmerResultParser = new JSONParser();
27     Object jsonResults = null;
28     try
29     {
30       jsonResults = hmmerResultParser.parse(jsonsource.getReader());
31     } catch (Exception p)
32     {
33       throw new IOException("While parsing from " + jsonsource.getInFile(),
34               p);
35     }
36     if (jsonResults == null)
37     {
38       throw new IOException("No data at" + jsonsource.getInFile());
39     }
40     if (!(jsonResults instanceof JSONObject))
41     {
42       throw new IOException("Unexpected JSON model at "
43               + jsonsource.getInFile());
44     }
45     try
46     {
47       JSONObject hmmsearchr = (JSONObject) ((JSONObject) jsonResults)
48               .get("results");
49       // now process the hits
50       addStatistics((JSONObject) hmmsearchr.get("stats"));
51       JSONArray jsonArray = (JSONArray) hmmsearchr.get("hits");
52       long p = 1;
53       for (Object hit : jsonArray)
54       {
55         JSONObject hmmhit = (JSONObject) hit;
56         addHit(hmmhit, p++);
57       }
58     } catch (ClassCastException q)
59     {
60       throw new IOException("Unexpected JSON model content at "
61               + jsonsource.getInFile(), q);
62     }
63   }
64
65   /**
66    * 
67    * @param object
68    *          - actually a JSONObject key value set of search statistics.
69    */
70   public void addStatistics(JSONObject stats)
71   {
72     for (Object stat : stats.keySet())
73     {
74       String key = (String) stat;
75       Object val = stats.get(key);
76       resultAl.setProperty(key, "" + val);
77     }
78   }
79
80   //
81   // mapping of keys to types of property on sequence
82   //
83   public void addHit(JSONObject hmmrhit, long p)
84   {
85     String sname = (String) hmmrhit.get("name");
86     SequenceI[] hits = resultAl.findSequenceMatch(sname);
87     if (hits == null)
88     {
89       System.err.println("No seq for " + sname);
90     }
91     double pvalue = (Double) hmmrhit.get("pvalue");
92
93     double evalue = Double.valueOf("" + hmmrhit.get("evalue"));
94     for (Object domainhit : ((JSONArray) hmmrhit.get("domains")))
95     {
96       JSONObject dhit=(JSONObject) domainhit;
97       // dhit.get(key)
98
99       // alihmmfrom,alihmmto alimodel
100       // alisqfrom,alisqto,aliaseq
101       // alippline
102       // 
103       for (SequenceI hitseq : hits)
104       {
105         // match alisqfrom,alisqto,seq
106         // overlay ppline as seqannotation
107         // ievalue
108         // cevalue
109         // 
110
111         AlignmentAnnotation pval = new AlignmentAnnotation("p-value",
112                 "hmmer3 pvalue", pvalue);
113         AlignmentAnnotation eval = new AlignmentAnnotation("e-value",
114                 "hmmer3 evalue", evalue);
115         hitseq.addAlignmentAnnotation(pval);
116         hitseq.addAlignmentAnnotation(eval);
117         
118
119       }
120     }
121   }
122 }