--- /dev/null
+package jalview.ws.ebi;
+
+import jalview.datamodel.AlignmentAnnotation;
+import jalview.datamodel.AlignmentI;
+import jalview.datamodel.SequenceI;
+import jalview.io.FileParse;
+
+import java.io.IOException;
+
+import org.json.simple.JSONArray;
+import org.json.simple.JSONObject;
+import org.json.simple.parser.JSONParser;
+
+public class HmmerJSONProcessor
+{
+ AlignmentI resultAl;
+
+ public HmmerJSONProcessor(AlignmentI searchResult)
+ {
+ resultAl = searchResult;
+ }
+
+ public void parseFrom(FileParse jsonsource) throws IOException,
+ OutOfMemoryError
+ {
+ JSONParser hmmerResultParser = new JSONParser();
+ Object jsonResults = null;
+ try
+ {
+ jsonResults = hmmerResultParser.parse(jsonsource.getReader());
+ } catch (Exception p)
+ {
+ throw new IOException("While parsing from " + jsonsource.getInFile(),
+ p);
+ }
+ if (jsonResults == null)
+ {
+ throw new IOException("No data at" + jsonsource.getInFile());
+ }
+ if (!(jsonResults instanceof JSONObject))
+ {
+ throw new IOException("Unexpected JSON model at "
+ + jsonsource.getInFile());
+ }
+ try
+ {
+ JSONObject hmmsearchr = (JSONObject) ((JSONObject) jsonResults)
+ .get("results");
+ // now process the hits
+ addStatistics((JSONObject) hmmsearchr.get("stats"));
+ JSONArray jsonArray = (JSONArray) hmmsearchr.get("hits");
+ long p = 1;
+ for (Object hit : jsonArray)
+ {
+ JSONObject hmmhit = (JSONObject) hit;
+ addHit(hmmhit, p++);
+ }
+ } catch (ClassCastException q)
+ {
+ throw new IOException("Unexpected JSON model content at "
+ + jsonsource.getInFile(), q);
+ }
+ }
+
+ /**
+ *
+ * @param object
+ * - actually a JSONObject key value set of search statistics.
+ */
+ public void addStatistics(JSONObject stats)
+ {
+ for (Object stat : stats.keySet())
+ {
+ String key = (String) stat;
+ Object val = stats.get(key);
+ resultAl.setProperty(key, "" + val);
+ }
+ }
+
+ //
+ // mapping of keys to types of property on sequence
+ //
+ public void addHit(JSONObject hmmrhit, long p)
+ {
+ String sname = (String) hmmrhit.get("name");
+ SequenceI[] hits = resultAl.findSequenceMatch(sname);
+ if (hits == null)
+ {
+ System.err.println("No seq for " + sname);
+ }
+ double pvalue = (Double) hmmrhit.get("pvalue");
+
+ double evalue = Double.valueOf("" + hmmrhit.get("evalue"));
+ for (Object domainhit : ((JSONArray) hmmrhit.get("domains")))
+ {
+ JSONObject dhit=(JSONObject) domainhit;
+ // dhit.get(key)
+
+ // alihmmfrom,alihmmto alimodel
+ // alisqfrom,alisqto,aliaseq
+ // alippline
+ //
+ for (SequenceI hitseq : hits)
+ {
+ // match alisqfrom,alisqto,seq
+ // overlay ppline as seqannotation
+ // ievalue
+ // cevalue
+ //
+
+ AlignmentAnnotation pval = new AlignmentAnnotation("p-value",
+ "hmmer3 pvalue", pvalue);
+ AlignmentAnnotation eval = new AlignmentAnnotation("e-value",
+ "hmmer3 evalue", evalue);
+ hitseq.addAlignmentAnnotation(pval);
+ hitseq.addAlignmentAnnotation(eval);
+
+
+ }
+ }
+ }
+}
--- /dev/null
+package jalview.ws.ebi;
+
+import jalview.datamodel.AlignmentI;
+import jalview.io.FileParse;
+import jalview.io.FormatAdapter;
+
+import java.io.File;
+
+import org.json.simple.JSONObject;
+import org.json.simple.parser.JSONParser;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+public class HmmerJSONProcessTest {
+ public static File alignmentFragFile = new File(
+ "examples/testdata/hmmer3/alignment_frag.fa.gz");
+
+ private AlignmentI getSearchResultFragmentAlignment() throws Exception
+ {
+ AlignmentI alf = new FormatAdapter().readFile(
+ alignmentFragFile.getAbsolutePath(), FormatAdapter.FILE,
+ "FASTA");
+
+ return alf;
+ }
+
+ public static File alignmentResultFile = new File(
+ "examples/testdata/hmmer3/alignment_res.fa.gz");
+
+ private AlignmentI getSearchResultAlignment() throws Exception
+ {
+ AlignmentI alf = new FormatAdapter().readFile(
+ alignmentResultFile.getAbsolutePath(), FormatAdapter.FILE,
+ "FASTA");
+
+ return alf;
+ }
+
+ public static String hitTestFile = "examples/testdata/hmmer3/hit_fragment.json.gz",
+ hmmerResultFile = "examples/testdata/hmmer3/hmmeresult.json.gz";
+
+
+ @Test(groups = { "Functional" })
+ public void parseHitTest() throws Exception
+ {
+
+ Assert.assertTrue(new File(hitTestFile).exists(),
+ "Can't find test data.\n"
+ + hitTestFile);
+ JSONParser jp = new JSONParser();
+ // read JSON in same way - via fileparse
+ Object hitfragment = jp.parse(new FileParse(hitTestFile,
+ FormatAdapter.FILE).getReader());
+ Assert.assertTrue((hitfragment instanceof JSONObject),
+ "Didn't find a JSON object map in " + hitTestFile);
+ AlignmentI searchResult = getSearchResultFragmentAlignment();
+
+ Assert.assertTrue(searchResult != null && searchResult.getHeight() > 0,
+ "Didn't read search result alignment from " + alignmentFragFile);
+
+ HmmerJSONProcessor hjsp = new HmmerJSONProcessor(searchResult);
+ hjsp.addHit((JSONObject) hitfragment, 1);
+ // check that
+ // scores, posterior probabilities and stuff exist.
+ }
+
+ @Test(groups = { "Functional" })
+ public void parseJsonResultTest() throws Exception
+ {
+
+ Assert.assertTrue(new File(hmmerResultFile).exists(),
+ "Can't find test data.\n" + hmmerResultFile);
+
+ AlignmentI searchResult = getSearchResultAlignment();
+
+ Assert.assertTrue(searchResult != null && searchResult.getHeight() > 0,
+ "Didn't read search result alignment from " + alignmentFragFile);
+
+ HmmerJSONProcessor hjsp = new HmmerJSONProcessor(searchResult);
+ hjsp.parseFrom(new FileParse(hmmerResultFile, FormatAdapter.FILE));
+ // check that
+ // scores, posterior probabilities and stuff exist.
+ }
+ // Groovy test
+ // def al = Jalview.getAlignFrames()[0].getViewport().getAlignment()
+ // new jalview.ws.ebi.HmmerJSONProcessor(al).parseFrom(new
+ // jalview.io.FileParse("examples/testdata/hmmer3/hmmeresult.json.gz","File"))
+
+}