1 package jalview.ws.ebi;
3 import jalview.datamodel.AlignmentAnnotation;
4 import jalview.datamodel.AlignmentI;
5 import jalview.datamodel.Annotation;
6 import jalview.datamodel.SequenceFeature;
7 import jalview.datamodel.SequenceI;
8 import jalview.io.FileParse;
10 import java.io.IOException;
12 import org.json.simple.JSONArray;
13 import org.json.simple.JSONObject;
14 import org.json.simple.parser.JSONParser;
16 public class HmmerJSONProcessor
20 public HmmerJSONProcessor(AlignmentI searchResult)
22 resultAl = searchResult;
25 public void parseFrom(FileParse jsonsource) throws IOException,
28 JSONParser hmmerResultParser = new JSONParser();
29 Object jsonResults = null;
32 jsonResults = hmmerResultParser.parse(jsonsource.getReader());
35 throw new IOException("While parsing from " + jsonsource.getInFile(),
38 if (jsonResults == null)
40 throw new IOException("No data at" + jsonsource.getInFile());
42 if (!(jsonResults instanceof JSONObject))
44 throw new IOException("Unexpected JSON model at "
45 + jsonsource.getInFile());
49 JSONObject hmmsearchr = (JSONObject) ((JSONObject) jsonResults)
51 // now process the hits
52 addStatistics((JSONObject) hmmsearchr.get("stats"));
53 JSONArray jsonArray = (JSONArray) hmmsearchr.get("hits");
55 for (Object hit : jsonArray)
57 JSONObject hmmhit = (JSONObject) hit;
60 } catch (ClassCastException q)
62 throw new IOException("Unexpected JSON model content at "
63 + jsonsource.getInFile(), q);
70 * - actually a JSONObject key value set of search statistics.
72 public void addStatistics(JSONObject stats)
74 for (Object stat : stats.keySet())
76 String key = (String) stat;
77 Object val = stats.get(key);
78 resultAl.setProperty(key, "" + val);
82 // encodings for JSON keys
84 * score becomes sequence associated AlignmentAnnotation
86 private String[] score = { "aliId", "ali_IdCount", "bitscore", "ievalue",
87 "aliSim", "aliSimCount", "aliL", "aliSim", "ievalue", "cevalue" };
90 * attrib becomes numeric or binary attribute for sequence with respect to
93 private String[] attrib = { "bias", "oasc", "is_included", "is_reported" };
96 * name of the hmmsearch query
98 private String[] label = { "alihmmname" // (query label?)},
102 * integer attributes for each
104 private String[] ipos = { "alihmmfrom", "alihmmto" }, pos_l = {
105 "alimline", "alimodel", "alirfline" };
108 * positional quantitative annotation encoded as strings.
110 private String[] pos_nscore = { "alippline" };
113 // mapping of keys to types of property on sequence
115 public void addHit(JSONObject hmmrhit, long p)
117 String sname = (String) hmmrhit.get("name");
118 SequenceI[] hits = resultAl.findSequenceMatch(sname);
121 System.err.println("No seq for " + sname);
123 double pvalue = (Double) hmmrhit.get("pvalue");
125 double evalue = Double.valueOf("" + hmmrhit.get("evalue"));
126 for (Object domainhit : ((JSONArray) hmmrhit.get("domains")))
128 JSONObject dhit = (JSONObject) domainhit;
131 // alihmmfrom,alihmmto alimodel
132 long alihmmfrom = (long) dhit.get("alihmmfrom"), alihmmto = (long) dhit
133 .get("alihmmto"), alisqfrom = (long) dhit.get("alisqfrom"), alisqto = (long) dhit
136 // alisqfrom,alisqto,aliaseq
139 String aliaseq = (String) dhit.get("aliaseq"), alimodel = (String) dhit
140 .get("alimodel"), ppline = (String) dhit.get("alippline");
143 for (SequenceI hitseq : hits)
145 // match alisqfrom,alisqto,seq
146 if (hitseq.getStart() == alisqfrom && hitseq.getEnd() == alisqto)
148 found++; // annotated a sequence
149 AlignmentAnnotation alipp = parsePosteriorProb(ppline);
150 AlignmentAnnotation pval = new AlignmentAnnotation("p-value",
151 "hmmer3 pvalue", pvalue);
152 AlignmentAnnotation eval = new AlignmentAnnotation("e-value",
153 "hmmer3 evalue", evalue);
154 pval.setCalcId("HMMER3");
155 eval.setCalcId("HMMER3");
156 alipp.setCalcId("HMMER3");
157 hitseq.addAlignmentAnnotation(pval);
158 hitseq.addAlignmentAnnotation(eval);
159 alipp.createSequenceMapping(hitseq, hitseq.getStart(), false);
160 hitseq.addAlignmentAnnotation(alipp);
161 hitseq.addSequenceFeature(new SequenceFeature(
162 "Pfam Domain Architecture", (hmmrhit.get("archindex"))
164 + (String) hmmrhit.get("arch"), 0, 0, Integer
165 .valueOf((String) hmmrhit.get("archScore")),
167 alipp.setScore(Double.valueOf("" + dhit.get("bitscore")));
168 alipp.adjustForAlignment();
169 resultAl.addAnnotation(pval);
170 resultAl.addAnnotation(eval);
171 resultAl.addAnnotation(alipp);
172 alipp.validateRangeAndDisplay();
177 System.err.println("Warn - no match for json hit " + sname + "/"
178 + alisqfrom + "-" + alisqto);
182 System.err.println("Warn - multiple matches for json hit " + sname
183 + "/" + alisqfrom + "-" + alisqto);
188 private AlignmentAnnotation parsePosteriorProb(String ppline)
190 Annotation[] ae = new Annotation[ppline.length()];
192 for (int i = 0, iSize = ppline.length(); i < iSize; i++)
194 char pp = ppline.charAt(i);
197 ae[spos++] = new Annotation(10f);
201 if (pp >= '0' && pp <= '9')
203 ae[spos++] = new Annotation(Integer.valueOf("" + pp));
207 AlignmentAnnotation pprob = new AlignmentAnnotation(
208 "Posterior Probability",
209 "Likelihood of HMM fit at each hit position.", ae);
210 pprob.graph = pprob.BAR_GRAPH;
211 pprob.visible = false;