From: jprocter Date: Mon, 11 Feb 2013 16:09:42 +0000 (+0000) Subject: upgrade to latest paradise services model using JSon response. X-Git-Tag: Jalview_2_9~221^2^2~8^2~15^2~2 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=9d75efeeb9a6e51bd328f56e376dca4e007b635c;hp=e1d776602ceedd69cae590bcf85836d7b32c5993;p=jalview.git upgrade to latest paradise services model using JSon response. --- diff --git a/.classpath b/.classpath index 3ec9da3..f1bb31c 100644 --- a/.classpath +++ b/.classpath @@ -44,8 +44,9 @@ - + + diff --git a/.gitignore b/.gitignore index 3407192..9841761 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,5 @@ /dist /classes .externalToolBuilders/Jalview Release indices [Builder].launch +/.DS_Store +/.com.apple.timemachine.supported diff --git a/THIRDPARTYLIBS b/THIRDPARTYLIBS index bedd23a..57aad05 100644 --- a/THIRDPARTYLIBS +++ b/THIRDPARTYLIBS @@ -33,7 +33,7 @@ vamsas-client.jar wsdl4j.jar xercesImpl.jar xml-apis.jar - +json_simple-1.1.jar : Apache 2.0 license : downloaded from https://code.google.com/p/json-simple/downloads/list (will move to 1.1.1 version when jalview is mavenised and osgi-ised) Additional dependencies examples/javascript/deployJava.js : http://java.com/js/deployJava.js diff --git a/src/jalview/ext/paradise/Annotate3D.java b/src/jalview/ext/paradise/Annotate3D.java index b0adb45..114de42 100644 --- a/src/jalview/ext/paradise/Annotate3D.java +++ b/src/jalview/ext/paradise/Annotate3D.java @@ -2,33 +2,186 @@ package jalview.ext.paradise; import jalview.ws.HttpClientUtils; +import java.io.IOException; +import java.io.InputStreamReader; import java.io.Reader; +import java.io.StringReader; +import java.net.URL; import java.util.ArrayList; +import java.util.Collection; +import java.util.Iterator; import java.util.List; import org.apache.http.NameValuePair; import org.apache.http.message.BasicNameValuePair; +import org.json.simple.JSONArray; +import org.json.simple.JSONObject; +import org.json.simple.JSONStreamAware; +import org.json.simple.parser.ContentHandler; +import org.json.simple.parser.ParseException; +/** + * simple methods for calling the various paradise RNA tools + * + * @author jimp + * + * History: v1.0 revised from original due to refactoring of + * paradise-ubmc.u-strasbg.fr/webservices/annotate3d to + * http://arn-ibmc.in2p3.fr/api/compute/2d?tool=rnaview + */ public class Annotate3D { + private static String twoDtoolsURL = "http://arn-ibmc.in2p3.fr/api/compute/2d"; + private static ContentHandler createContentHandler() + { + ContentHandler ch = new ContentHandler() { + + @Override + public void startJSON() throws ParseException, IOException + { + // TODO Auto-generated method stub + + } + + @Override + public void endJSON() throws ParseException, IOException + { + // TODO Auto-generated method stub + + } + + @Override + public boolean startObject() throws ParseException, IOException + { + // TODO Auto-generated method stub + return false; + } + + @Override + public boolean endObject() throws ParseException, IOException + { + // TODO Auto-generated method stub + return false; + } + + @Override + public boolean startObjectEntry(String key) throws ParseException, + IOException + { + // TODO Auto-generated method stub + return false; + } - public static Reader getRNAMLForPDBFileAsString(String pdbfile) + @Override + public boolean endObjectEntry() throws ParseException, IOException + { + // TODO Auto-generated method stub + return false; + } + + @Override + public boolean startArray() throws ParseException, IOException + { + // TODO Auto-generated method stub + return false; + } + + @Override + public boolean endArray() throws ParseException, IOException + { + // TODO Auto-generated method stub + return false; + } + + @Override + public boolean primitive(Object value) throws ParseException, + IOException + { + // TODO Auto-generated method stub + return false; + } + + }; + return ch; + } + public static Iterator getRNAMLForPDBFileAsString(String pdbfile) throws Exception { List vals = new ArrayList(); + vals.add(new BasicNameValuePair("tool", "rnaview")); vals.add(new BasicNameValuePair("data", pdbfile)); - return HttpClientUtils.doHttpUrlPost( - "http://paradise-ibmc.u-strasbg.fr/webservices/annotate3d", - vals); + vals.add(new BasicNameValuePair("output", "rnaml")); + return processJsonResponseFor(HttpClientUtils.doHttpUrlPost(twoDtoolsURL, vals)); + } + public static Iterator processJsonResponseFor(Reader respons) throws Exception + { + org.json.simple.parser.JSONParser jp = new org.json.simple.parser.JSONParser(); + try { + final JSONArray responses = (JSONArray) jp.parse(respons); + final Iterator rvals = responses.iterator(); + return new Iterator() + { + @Override + public boolean hasNext() + { + return rvals.hasNext(); + } + @Override + public Reader next() + { + JSONObject val=(JSONObject) rvals.next(); + + Object sval = null; + try { + sval = val.get("2D"); + } catch (Exception x) {x.printStackTrace();}; + if (sval==null) + { + System.err.println("DEVELOPER WARNING: Annotate3d didn't return a '2D' tag in its response. Consider checking output of server. Response was :"+val.toString()); + + sval = ""; + } + return new StringReader((sval instanceof JSONObject) ? ((JSONObject)sval).toString():sval.toString()); + + }@Override + public void remove() + { + throw new Error("Remove: Not implemented"); + + }@Override + protected Object clone() throws CloneNotSupportedException + { + throw new CloneNotSupportedException("Clone: Not implemented"); + }@Override + public boolean equals(Object obj) + { + return super.equals(obj); + }@Override + protected void finalize() throws Throwable + { + while (rvals.hasNext()) + { + rvals.next(); + } + super.finalize(); + } + }; + } catch (Exception foo) + { + throw new Exception("Couldn't parse response from Annotate3d server.",foo); + } + + } - public static Reader getRNAMLForPDBId(String pdbid) throws Exception + public static Iterator getRNAMLForPDBId(String pdbid) throws Exception { List vals = new ArrayList(); + vals.add(new BasicNameValuePair("tool", "rnaview")); vals.add(new BasicNameValuePair("pdbid", pdbid)); - return HttpClientUtils.doHttpUrlPost( - "http://paradise-ibmc.u-strasbg.fr/webservices/annotate3d", - vals); + vals.add(new BasicNameValuePair("format", "rnaml")); + java.net.URL geturl = new URL(twoDtoolsURL+"?tool=rnaview&output=rnaml&pdbid="+pdbid); + return processJsonResponseFor(new InputStreamReader(geturl.openStream())); } } diff --git a/src/jalview/ws/jws1/Annotate3D.java b/src/jalview/ws/jws1/Annotate3D.java index a30a9a1..e90b2d2 100644 --- a/src/jalview/ws/jws1/Annotate3D.java +++ b/src/jalview/ws/jws1/Annotate3D.java @@ -31,6 +31,7 @@ import java.io.Reader; import java.net.MalformedURLException; import java.net.URL; import java.net.URLEncoder; +import java.util.Iterator; public class Annotate3D { @@ -76,12 +77,21 @@ public class Annotate3D sb.append(cbuff[i]); } } - - FileParse fp = new InputStreamParser( - jalview.ext.paradise.Annotate3D.getRNAMLForPDBFileAsString(sb - .toString()), source.getDataName()); - AlignmentI nal = new FormatAdapter().readFromFile(fp, "RNAML"); - return nal; + Iterator r = jalview.ext.paradise.Annotate3D + .getRNAMLForPDBFileAsString(sb.toString()); + AlignmentI al=null; + while (r.hasNext()) + { + FileParse fp = new InputStreamParser(r.next(), source.getDataName()); + AlignmentI nal = new FormatAdapter().readFromFile(fp, "RNAML"); + if (al==null) + { + al = nal; + } else { + al.append(nal); + } + } + return al; } catch (Throwable x) { if (x instanceof IOException) diff --git a/test/jalview/ext/paradise/TestAnnotate3D.java b/test/jalview/ext/paradise/TestAnnotate3D.java index 1ed46f4..702d9ad 100644 --- a/test/jalview/ext/paradise/TestAnnotate3D.java +++ b/test/jalview/ext/paradise/TestAnnotate3D.java @@ -10,6 +10,8 @@ import jalview.io.FormatAdapter; import java.io.BufferedReader; import java.io.File; +import java.io.Reader; +import java.util.Iterator; import org.junit.Assert; import org.junit.Test; @@ -24,13 +26,17 @@ public class TestAnnotate3D @Test public void testIdVsContent() throws Exception { - BufferedReader id = (BufferedReader) Annotate3D + Iterator ids = Annotate3D .getRNAMLForPDBId("2GIS"); - assertTrue("Didn't retrieve 2GIS by id.", id != null); - BufferedReader file = (BufferedReader) Annotate3D + assertTrue("Didn't retrieve 2GIS by id.", ids != null); + Iterator files = Annotate3D .getRNAMLForPDBFileAsString(FileUtil.readFileToString(new File( "examples/2GIS.pdb"))); - assertTrue("Didn't retrieve using examples/2GIS.pdb.", file != null); + assertTrue("Didn't retrieve using examples/2GIS.pdb.", files != null); + int i=0; + while (ids.hasNext() && files.hasNext()) + { + BufferedReader file=new BufferedReader(files.next()), id=new BufferedReader(ids.next()); String iline, fline; do { @@ -40,12 +46,13 @@ public class TestAnnotate3D System.out.println(iline); if (fline != null) System.out.println(fline); - - assertTrue("Results differ for ID and file upload based retrieval", + // next assert fails for latest RNAview - because the XMLID entries change between file and ID based RNAML generation. + assertTrue("Results differ for ID and file upload based retrieval (chain entry "+(++i)+")", ((iline == fline && iline == null) || (iline != null && fline != null && iline.equals(fline)))); } while (iline != null); + } } /** @@ -62,39 +69,46 @@ public class TestAnnotate3D StringBuffer sb = new StringBuffer(); // Comment - should add new FileParse constructor like new FileParse(Reader // ..). for direct reading - BufferedReader br = new BufferedReader( - Annotate3D.getRNAMLForPDBFileAsString(FileUtil - .readFileToString(new File("examples/2GIS.pdb")))); - String line; - while ((line = br.readLine()) != null) + Iterator readers = Annotate3D + .getRNAMLForPDBFileAsString(FileUtil.readFileToString(new File( + "examples/2GIS.pdb"))); + int r=0; + while (readers.hasNext()) { - sb.append(line + "\n"); - } - assertTrue("No data returned by Annotate3D", sb.length() > 0); - AlignmentI al = new FormatAdapter().readFile(sb.toString(), - FormatAdapter.PASTE, "RNAML"); + System.out.println("Testing RNAML input number "+(++r)); + BufferedReader br = new BufferedReader(readers.next()); + String line; + while ((line = br.readLine()) != null) + { + sb.append(line + "\n"); + } + assertTrue("No data returned by Annotate3D", sb.length() > 0); + AlignmentI al = new FormatAdapter().readFile(sb.toString(), + FormatAdapter.PASTE, "RNAML"); - assertTrue("No alignment returned.", al != null); - assertTrue("No sequences in returned alignment.", al.getHeight() > 0); - for (SequenceI sq : al.getSequences()) - { + assertTrue("No alignment returned.", al != null); + assertTrue("No sequences in returned alignment.", al.getHeight() > 0); + for (SequenceI sq : al.getSequences()) { - SequenceI struseq = null; - String sq_ = new String(sq.getSequence()).toLowerCase(); - for (SequenceI _struseq : pdbf.getSeqsAsArray()) { - if (new String(_struseq.getSequence()).toLowerCase().equals(sq_)) + SequenceI struseq = null; + String sq_ = new String(sq.getSequence()).toLowerCase(); + for (SequenceI _struseq : pdbf.getSeqsAsArray()) { - struseq = _struseq; - break; + if (new String(_struseq.getSequence()).toLowerCase() + .equals(sq_)) + { + struseq = _struseq; + break; + } + } + if (struseq == null) + { + Assert.fail("Couldn't find this sequence in original input:\n" + + new FastaFile().print(new SequenceI[] + { sq }) + "\n\nOriginal input:\n" + + new FastaFile().print(pdbf.getSeqsAsArray()) + "\n"); } - } - if (struseq == null) - { - Assert.fail("Couldn't find this sequence in original input:\n" - + new FastaFile().print(new SequenceI[] - { sq }) + "\n\nOriginal input:\n" - + new FastaFile().print(pdbf.getSeqsAsArray()) + "\n"); } } }