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 java.io.BufferedReader;
24 import java.io.IOException;
25 import java.net.MalformedURLException;
27 import java.util.ArrayList;
28 import java.util.Iterator;
29 import java.util.List;
31 import org.json.simple.JSONArray;
32 import org.json.simple.JSONObject;
33 import org.json.simple.parser.JSONParser;
34 import org.json.simple.parser.ParseException;
37 * A client for the Ensembl xrefs/symbol REST service;
39 * @see http://rest.ensembl.org/documentation/info/xref_external
43 public class EnsemblSymbol extends EnsemblXref
45 private static final String GENE = "gene";
46 private static final String TYPE = "type";
48 * Constructor given the target domain to fetch data from
54 public EnsemblSymbol(String domain, String dbName, String dbVersion)
56 super(domain, dbName, dbVersion);
60 * Returns the first "id" value in gene identifier format from the JSON
61 * response, or null if none found
67 protected String parseSymbolResponse(BufferedReader br) throws IOException
69 JSONParser jp = new JSONParser();
73 JSONArray responses = (JSONArray) jp.parse(br);
74 Iterator rvals = responses.iterator();
75 while (rvals.hasNext())
77 JSONObject val = (JSONObject) rvals.next();
78 String id = val.get(ID).toString();
79 String type = val.get(TYPE).toString();
80 if (id != null && GENE.equals(type))
86 } catch (ParseException e)
94 * Constructs the URL for the REST symbol endpoint
97 * the accession id (Ensembl or external)
99 * a species name recognisable by Ensembl
101 * an optional type to filter the response (gene, transcript,
105 protected URL getUrl(String id, Species species, String... type)
107 StringBuilder sb = new StringBuilder();
108 sb.append(getDomain()).append("/xrefs/symbol/")
109 .append(species.toString()).append("/").append(id)
110 .append(CONTENT_TYPE_JSON);
111 for (String t : type)
113 sb.append("&object_type=").append(t);
117 String url = sb.toString();
119 } catch (MalformedURLException e)
126 * Calls the Ensembl xrefs REST 'symbol' endpoint and retrieves any gene ids
127 * for the given identifier, for any known model organisms
132 public List<String> getGeneIds(String identifier)
134 List<String> result = new ArrayList<String>();
135 List<String> ids = new ArrayList<String>();
138 String[] queries = identifier.split(getAccessionSeparator());
139 BufferedReader br = null;
142 for (String query : queries)
144 for (Species taxon : Species.getModelOrganisms())
146 URL url = getUrl(query, taxon, GENE);
149 br = getHttpResponse(url, ids);
152 String geneId = parseSymbolResponse(br);
153 System.out.println(url + " returned " + geneId);
154 if (geneId != null && !result.contains(geneId))
162 } catch (IOException e)
172 } catch (IOException e)