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;
21 * A class to fetch cross-references from Ensembl by calling the /xrefs REST
27 class EnsemblXref extends EnsemblRestClient
31 public String getDbName()
33 return "ENSEMBL (xref)";
37 public AlignmentI getSequenceRecords(String queries) throws Exception
43 protected URL getUrl(List<String> ids) throws MalformedURLException
45 // TODO Auto-generated method stub
50 protected boolean useGetRequest()
56 protected String getRequestMimeType(boolean multipleIds)
58 return "application/json";
62 protected String getResponseMimeType()
64 return "application/json";
68 * Calls the Ensembl xrefs REST endpoint and retrieves any cross-references
69 * ("primary_id") for the given identifier (Ensembl accession id) and database
70 * names. The "dbname" returned by Ensembl is canonicalised to Jalview's
71 * standard version, and a DBRefEntry constructed. If no databases are
72 * specified, all available cross-references are retrieved.
78 public List<DBRefEntry> getCrossReferences(String identifier,
79 List<String> databases)
81 List<DBRefEntry> result = new ArrayList<DBRefEntry>();
82 List<String> ids = new ArrayList<String>();
85 BufferedReader br = null;
88 URL url = getUrl(identifier);
91 br = getHttpResponse(url, ids);
93 return (parseResponse(br, databases));
94 } catch (IOException e)
104 } catch (IOException e)
115 * Parses "primary_id" and "dbname" values from the JSON response and
116 * constructs a DBRefEntry if the dbname is in the list supplied. Returns a
117 * list of DBRefEntry created.
122 * @throws IOException
124 protected List<DBRefEntry> parseResponse(BufferedReader br,
125 List<String> databases)
128 JSONParser jp = new JSONParser();
129 List<DBRefEntry> result = new ArrayList<DBRefEntry>();
132 JSONArray responses = (JSONArray) jp.parse(br);
133 Iterator rvals = responses.iterator();
134 while (rvals.hasNext())
136 JSONObject val = (JSONObject) rvals.next();
137 String dbName = val.get("dbname").toString();
138 if (databases != null && !databases.isEmpty()
139 && !databases.contains(dbName))
143 String id = val.get("primary_id").toString();
144 if (dbName != null && id != null)
146 dbName = DBRefUtils.getCanonicalName(dbName);
147 DBRefEntry dbref = new DBRefEntry(dbName, "0", id);
151 } catch (ParseException e)
159 * Returns the URL for the REST endpoint to fetch all cross-references for an
160 * identifier. Note this may return protein cross-references for nucleotide.
161 * Filter the returned list as required.
166 protected URL getUrl(String identifier)
168 String url = ENSEMBL_REST + "/xrefs/id/" + identifier
169 + "?content-type=application/json&all_levels=1";
173 } catch (MalformedURLException e)