From: Charles Ofoegbu Date: Mon, 20 Oct 2014 09:34:25 +0000 (+0100) Subject: Implementation of support for Intermine webservice as a source of sequence data X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=92ec29327ccfb5d84ffcf60936bb813d557ced51;hp=c9a26406e764133ae48a29aa0fc33908aa6b6ae0;p=jalview.git Implementation of support for Intermine webservice as a source of sequence data --- diff --git a/src/jalview/ws/HttpClientUtils.java b/src/jalview/ws/HttpClientUtils.java index 229fa4e..4b6129f 100644 --- a/src/jalview/ws/HttpClientUtils.java +++ b/src/jalview/ws/HttpClientUtils.java @@ -1,19 +1,19 @@ /* * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2) * Copyright (C) 2014 The Jalview Authors - * + * * This file is part of Jalview. - * + * * Jalview is free software: you can redistribute it and/or - * modify it under the terms of the GNU General Public License + * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation, either version 3 * of the License, or (at your option) any later version. - * - * Jalview is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty - * of MERCHANTABILITY or FITNESS FOR A PARTICULAR + * + * Jalview is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR * PURPOSE. See the GNU General Public License for more details. - * + * * You should have received a copy of the GNU General Public License * along with Jalview. If not, see . * The Jalview Authors are detailed in the 'AUTHORS' file. @@ -33,6 +33,7 @@ import org.apache.http.NameValuePair; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; +import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.mime.HttpMultipartMode; import org.apache.http.entity.mime.MultipartEntity; @@ -43,16 +44,16 @@ import org.apache.http.impl.client.DefaultHttpClient; /** * Helpful procedures for working with services via HTTPClient - * + * * @author jimp - * + * */ public class HttpClientUtils { /** * do a minimal HTTP post with URL-Encoded parameters passed in the Query * string - * + * * @param postUrl * @param vals * @return Reader containing content, if any, or null if no entity returned. @@ -83,9 +84,38 @@ public class HttpClientUtils } } + public static String doHttpUrlGet(String getUrl /* , HttpParams params */) + throws ClientProtocolException, IOException + { + HttpClient httpclient = new DefaultHttpClient(); + HttpGet httpget = new HttpGet(getUrl); + // httpget.setParams(params); + HttpResponse response = httpclient.execute(httpget); + HttpEntity resEntity = response.getEntity(); + + + if (resEntity != null) + { + BufferedReader r = new BufferedReader(new InputStreamReader( + resEntity.getContent())); + + String aux = ""; + StringBuilder responseBuilder = new StringBuilder(); + while ((aux = r.readLine()) != null) + { + responseBuilder.append(aux); + } + return responseBuilder.toString(); + } + else + { + return null; + } + } + public static BufferedReader doHttpMpartFilePost(String postUrl, List vals, String fparm, File file, String mtype) - throws ClientProtocolException, IOException + throws ClientProtocolException, IOException { HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(postUrl); diff --git a/src/jalview/ws/dbsources/Intermine.java b/src/jalview/ws/dbsources/Intermine.java index 65ce5c9..e738adf 100644 --- a/src/jalview/ws/dbsources/Intermine.java +++ b/src/jalview/ws/dbsources/Intermine.java @@ -1,13 +1,21 @@ package jalview.ws.dbsources; +import jalview.datamodel.Alignment; import jalview.datamodel.AlignmentI; import jalview.datamodel.DBRefSource; -import jalview.ws.seqfetcher.DbSourceProxy; +import jalview.io.FormatAdapter; +import jalview.io.IdentifyFile; +import jalview.ws.intermine.IntermineFetchClient; import jalview.ws.seqfetcher.DbSourceProxyImpl; +import java.util.Iterator; + +import org.json.simple.JSONArray; +import org.json.simple.JSONObject; + import com.stevesoft.pat.Regex; -public class Intermine extends DbSourceProxyImpl implements DbSourceProxy +public class Intermine extends DbSourceProxyImpl // implements DbSourceProxy { @Override @@ -25,77 +33,83 @@ public class Intermine extends DbSourceProxyImpl implements DbSourceProxy @Override public String getDbVersion() { - // TODO Auto-generated method stub - return null; + return "0"; } @Override public String getAccessionSeparator() { - // TODO Auto-generated method stub return null; } @Override public Regex getAccessionValidator() { - // TODO Auto-generated method stub return null; } - // @Override - // public Hashtable getDbSourceProperties() - // { - // // TODO Auto-generated method stub - // return null; - // } @Override public String getTestQuery() { // TODO Auto-generated method stub - return null; + return "http://www.flymine.org/query/service/jbrowse/7227/features/2L?start=100000&end=100200&reference=true"; } @Override public boolean isValidReference(String accession) { - // TODO Auto-generated method stub return false; } @Override - public AlignmentI getSequenceRecords(String queries) throws Exception + public AlignmentI getSequenceRecords(String query) throws Exception { - // TODO Auto-generated method stub - return null; + startQuery(); + String jsonString = IntermineFetchClient.fetchData(query); + + Object sequenceString = ""; + System.out.println("Found json: " + jsonString); + org.json.simple.parser.JSONParser jsonParser = new org.json.simple.parser.JSONParser(); + try + { + JSONObject jobj = (JSONObject) jsonParser.parse(jsonString); + JSONArray responses = (JSONArray) jobj.get("features"); + for (Iterator rvals = responses.iterator(); rvals + .hasNext();) + { + JSONObject feature = rvals.next(); + Object seq = feature.get("seq"); + System.out.println("Sequence : " + seq.toString()); + } + } catch (Exception e) + { + e.printStackTrace(); + } + stopQuery(); + return parseResult(sequenceString.toString()); } @Override - public boolean queryInProgress() + protected Alignment parseResult(String result) throws Exception { - // TODO Auto-generated method stub - return false; + Alignment sequences = null; + String format = new IdentifyFile().Identify(result, "Paste"); + if (FormatAdapter.isValidFormat(format)) + { + sequences = new FormatAdapter().readFile(result, "Paste", + format); + } + return sequences; } - @Override - public StringBuffer getRawRecords() - { - // TODO Auto-generated method stub - return null; - } - @Override - public boolean isA(Object dbsourceproperty) - { - // TODO Auto-generated method stub - return false; - } + + @Override public int getTier() { - // TODO Auto-generated method stub return 0; } diff --git a/src/jalview/ws/intermine/IntermineFetchClient.java b/src/jalview/ws/intermine/IntermineFetchClient.java new file mode 100644 index 0000000..535959f --- /dev/null +++ b/src/jalview/ws/intermine/IntermineFetchClient.java @@ -0,0 +1,110 @@ +package jalview.ws.intermine; + +import jalview.ws.HttpClientUtils; + +import java.io.IOException; + +import javax.ws.rs.core.MultivaluedMap; + +import org.apache.http.client.ClientProtocolException; + +import com.sun.jersey.api.client.Client; + +public class IntermineFetchClient +{ + + private static final Client client = Client.create(); + + public static enum IntermineDB + { + YeastMine("Yeast Mine", "http://yeastmine.yeastgenome.org/yeastmine"), FlyMine( + "Fly Mine", "http://www.flymine.org/query"), FlyMine_Beta( + "Fly Mine Beta", + "http://beta.flymine.org/beta"), MouseMine("Mouse Mine", + "http://www.mousemine.org/mousemine"), modMine( + "Mod Mine", + "http://intermine.modencode.org/modminetest"), RatMine( + "Rat Mine", "http://ratmine.mcw.edu/ratmine"); + + private final String name; + private final String Url; + + IntermineDB(String name, String Url) + { + this.Url = Url; + this.name = name; + } + + public String getName() + { + return this.name; + } + + public String getURL() + { + return this.Url; + } + } + + public static enum IntermineMethod + { + GET_VERSION("/service/version"), GET_RELEASE("/service/version/release"), GET_FASTA_LIST( + "/service/list/results/fasta"), GET_FASTA_QUERY( + "/query/results/fasta"); + + private final String target; + + IntermineMethod(String target) + { + this.target = target; + } + + public String getTarget() + { + return target; + } + + } + + public static String[] getSupportedDBs() + { + String[] supportedDbs = new String[IntermineDB.values().length]; + int count = 0; + for (IntermineDB db : IntermineDB.values()) + { + supportedDbs[count++] = db.getName(); + } + return supportedDbs; + + } + + public static String fetchData(IntermineDB Database, + IntermineMethod method, MultivaluedMap params) + { + String response = ""; + if (params.isEmpty()) + { + + response = client.resource(Database.getURL() + method.getTarget()) + .accept("text/plain") + .get(String.class); + + } + else + { + response = client.resource(Database.getURL() + method.getTarget()) + .queryParams(params) + .get(String.class); + } + return response; + } + + public static String fetchData(String url) + throws ClientProtocolException, IOException + { + + return HttpClientUtils.doHttpUrlGet(url);// client.resource(url).get(String.class); + } + + +}