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 * names. The "dbname" returned by Ensembl is canonicalised to Jalview's
64 * standard version, and a DBRefEntry constructed. If no databases are
65 * specified, all available cross-references are retrieved.
71 public List<DBRefEntry> getCrossReferences(String identifier,
72 List<String> databases)
74 List<DBRefEntry> result = new ArrayList<DBRefEntry>();
75 List<String> ids = new ArrayList<String>();
78 BufferedReader br = null;
81 URL url = getUrl(identifier);
84 br = getHttpResponse(url, ids);
86 return (parseResponse(br, databases));
87 } catch (IOException e)
97 } catch (IOException e)
108 * Parses "primary_id" and "dbname" values from the JSON response and
109 * constructs a DBRefEntry if the dbname is in the list supplied. Returns a
110 * list of DBRefEntry created.
115 * @throws IOException
117 protected List<DBRefEntry> parseResponse(BufferedReader br,
118 List<String> databases)
121 JSONParser jp = new JSONParser();
122 List<DBRefEntry> result = new ArrayList<DBRefEntry>();
125 JSONArray responses = (JSONArray) jp.parse(br);
126 Iterator rvals = responses.iterator();
127 while (rvals.hasNext())
129 JSONObject val = (JSONObject) rvals.next();
130 String dbName = val.get("dbname").toString();
131 if (databases != null && !databases.isEmpty()
132 && !databases.contains(dbName))
136 String id = val.get("primary_id").toString();
137 if (dbName != null && id != null)
139 dbName = DBRefUtils.getCanonicalName(dbName);
140 DBRefEntry dbref = new DBRefEntry(dbName, "0", id);
144 } catch (ParseException e)
152 * Returns the URL for the REST endpoint to fetch all cross-references for an
153 * identifier. Note this may return protein cross-references for nucleotide.
154 * Filter the returned list as required.
159 protected URL getUrl(String identifier)
161 String url = ENSEMBL_REST + "/xrefs/id/" + identifier
162 + "?content-type=application/json&all_levels=1";
166 } catch (MalformedURLException e)