4d30b3b32e01cb4179d7a371219766b2e235ed55
[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   // encodings for JSON keys
81   /**
82    * score becomes sequence associated AlignmentAnnotation
83    */
84   private String[] score = { "aliId", "ali_IdCount", "bitscore", "ievalue",
85       "aliSim", "aliSimCount", "aliL", "aliSim", "ievalue", "cevalue" };
86
87   /**
88    * attrib becomes numeric or binary attribute for sequence with respect to
89    * this hmmsearch run
90    */
91   private String[] attrib = { "bias", "oasc", "is_included", "is_reported" };
92
93   /**
94    * name of the hmmsearch query
95    */
96   private String[] label = { "alihmmname" // (query label?)},
97   };
98
99   /**
100    * integer attributes for each
101    */
102   private String[] ipos = { "alihmmfrom", "alihmmto" }, pos_l = {
103       "alimline", "alimodel", "alirfline" };
104
105   /**
106    * positional quantitative annotation encoded as strings.
107    */
108   private String[] pos_nscore = { "alippline" };
109
110   //
111   // mapping of keys to types of property on sequence
112   //
113   public void addHit(JSONObject hmmrhit, long p)
114   {
115     String sname = (String) hmmrhit.get("name");
116     SequenceI[] hits = resultAl.findSequenceMatch(sname);
117     if (hits == null)
118     {
119       System.err.println("No seq for " + sname);
120     }
121     double pvalue = (Double) hmmrhit.get("pvalue");
122
123     double evalue = Double.valueOf("" + hmmrhit.get("evalue"));
124     for (Object domainhit : ((JSONArray) hmmrhit.get("domains")))
125     {
126       JSONObject dhit=(JSONObject) domainhit;
127       // dhit.get(key)
128
129       // alihmmfrom,alihmmto alimodel
130       // alisqfrom,alisqto,aliaseq
131       // alippline
132       // 
133       for (SequenceI hitseq : hits)
134       {
135         // match alisqfrom,alisqto,seq
136         // overlay ppline as seqannotation
137         // ievalue
138         // cevalue
139         // 
140
141         AlignmentAnnotation pval = new AlignmentAnnotation("p-value",
142                 "hmmer3 pvalue", pvalue);
143         AlignmentAnnotation eval = new AlignmentAnnotation("e-value",
144                 "hmmer3 evalue", evalue);
145         hitseq.addAlignmentAnnotation(pval);
146         hitseq.addAlignmentAnnotation(eval);
147         
148
149       }
150     }
151   }
152 }