2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.ext.ensembl;
23 import jalview.datamodel.AlignmentI;
24 import jalview.datamodel.DBRefEntry;
25 import jalview.util.DBRefUtils;
26 import jalview.util.JSONUtils;
28 import java.io.BufferedReader;
29 import java.io.IOException;
30 import java.net.MalformedURLException;
32 import java.util.ArrayList;
33 import java.util.Iterator;
34 import java.util.List;
37 import org.json.simple.parser.ParseException;
40 * A class to fetch cross-references from Ensembl by calling the /xrefs REST
44 * @see http://rest.ensembl.org/documentation/info/xref_id
46 class EnsemblXref extends EnsemblRestClient
49 private static final String GO_GENE_ONTOLOGY = "GO";
51 private String dbName = "ENSEMBL (xref)";
54 * Constructor given the target domain to fetch data from
58 public EnsemblXref(String d, String dbSource, String version)
62 xrefVersion = dbSource + ":" + version;
67 public String getDbName()
73 public AlignmentI getSequenceRecords(String queries) throws Exception
79 protected URL getUrl(List<String> ids) throws MalformedURLException
81 return getUrl(ids.get(0));
85 protected boolean useGetRequest()
91 * Calls the Ensembl xrefs REST endpoint and retrieves any cross-references
92 * ("primary_id") for the given identifier (Ensembl accession id) and database
93 * names. The "dbname" returned by Ensembl is canonicalised to Jalview's
94 * standard version, and a DBRefEntry constructed. Currently takes all
95 * identifiers apart from GO terms and synonyms.
98 * an Ensembl stable identifier
101 @SuppressWarnings("unchecked")
102 public List<DBRefEntry> getCrossReferences(String identifier)
104 List<DBRefEntry> result = new ArrayList<>();
105 List<String> ids = new ArrayList<>();
110 Iterator<Object> rvals = (Iterator<Object>) getJSON(
111 getUrl(identifier), ids, -1, MODE_ITERATOR, null);
112 while (rvals.hasNext())
114 Map<String, Object> val = (Map<String, Object>) rvals.next();
115 String db = val.get("dbname").toString();
116 String id = val.get("primary_id").toString();
117 if (db != null && id != null && !GO_GENE_ONTOLOGY.equals(db))
119 db = DBRefUtils.getCanonicalName(db);
120 DBRefEntry dbref = new DBRefEntry(db, getXRefVersion(), id);
124 } catch (ParseException | IOException e)
132 // * Parses "primary_id" and "dbname" values from the JSON response and
133 // * constructs a DBRefEntry. Returns a list of the DBRefEntry created. Note
135 // * don't parse "synonyms" as they appear to be either redirected or obsolete
140 // * @throws IOException
142 // @SuppressWarnings("unchecked")
143 // protected List<DBRefEntry> parseResponse(BufferedReader br)
144 // throws IOException
149 private String xrefVersion = "ENSEMBL:0";
152 * version string for Xrefs - for 2.10, hardwired for ENSEMBL:0
156 public String getXRefVersion()
162 * Returns the URL for the REST endpoint to fetch all cross-references for an
163 * identifier. Note this may return protein cross-references for nucleotide.
164 * Filter the returned list as required.
169 protected URL getUrl(String identifier)
171 String url = getDomain() + "/xrefs/id/" + identifier + CONTENT_TYPE_JSON
176 } catch (MalformedURLException e)