1 package jalview.ext.ensembl;
3 import jalview.datamodel.AlignmentI;
4 import jalview.datamodel.DBRefEntry;
5 import jalview.util.DBRefUtils;
7 import java.io.BufferedReader;
8 import java.io.IOException;
9 import java.net.MalformedURLException;
11 import java.util.ArrayList;
12 import java.util.Iterator;
13 import java.util.List;
15 import org.json.simple.JSONArray;
16 import org.json.simple.JSONObject;
17 import org.json.simple.parser.JSONParser;
18 import org.json.simple.parser.ParseException;
20 public class EnsemblXref extends EnsemblRestClient
24 public String getDbName()
26 return "ENSEMBL (xref)";
30 public AlignmentI getSequenceRecords(String queries) throws Exception
36 protected URL getUrl(List<String> ids) throws MalformedURLException
38 // TODO Auto-generated method stub
43 protected boolean useGetRequest()
49 protected String getRequestMimeType(boolean multipleIds)
51 return "application/json";
55 protected String getResponseMimeType()
57 return "application/json";
61 * Calls the Ensembl xrefs REST endpoint and retrieves any cross-references
62 * ("primary_id") for the given identifier (Ensembl accession id) and database
63 * name. The "dbname" returned by Ensembl is canonicalised to Jalview's
64 * standard version, and a DBRefEntry constructed.
70 public List<DBRefEntry> getCrossReferences(String identifier,
73 List<DBRefEntry> result = new ArrayList<DBRefEntry>();
74 List<String> ids = new ArrayList<String>();
77 BufferedReader br = null;
80 for (String db : database)
82 URL url = getUrl(identifier, db);
85 br = getHttpResponse(url, ids);
87 for (DBRefEntry xref : parseResponse(br))
89 if (!result.contains(xref))
96 } catch (IOException e)
106 } catch (IOException e)
117 * Parses "primary_id" and "dbname" values from the JSON response and returns
118 * a list of DBRefEntry constructed.
122 * @throws IOException
124 protected List<DBRefEntry> parseResponse(BufferedReader br)
127 JSONParser jp = new JSONParser();
128 List<DBRefEntry> result = new ArrayList<DBRefEntry>();
131 JSONArray responses = (JSONArray) jp.parse(br);
132 Iterator rvals = responses.iterator();
133 while (rvals.hasNext())
135 JSONObject val = (JSONObject) rvals.next();
136 String dbName = val.get("dbname").toString();
137 String id = val.get("primary_id").toString();
138 if (dbName != null && id != null)
140 dbName = DBRefUtils.getCanonicalName(dbName);
141 DBRefEntry dbref = new DBRefEntry(dbName, "0", id);
145 } catch (ParseException e)
152 protected URL getUrl(String identifier, String db)
154 String url = ENSEMBL_REST + "/xrefs/id/" + identifier
155 + "?content-type=application/json&external_db=" + db;
159 } catch (MalformedURLException e)