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.Alignment;
24 import jalview.datamodel.AlignmentI;
25 import jalview.datamodel.Sequence;
26 import jalview.datamodel.SequenceFeature;
27 import jalview.datamodel.SequenceI;
28 import jalview.io.gff.SequenceOntologyI;
30 import java.io.BufferedReader;
31 import java.io.IOException;
32 import java.net.MalformedURLException;
34 import java.util.ArrayList;
35 import java.util.Iterator;
36 import java.util.List;
38 import org.json.simple.JSONArray;
39 import org.json.simple.JSONObject;
40 import org.json.simple.parser.JSONParser;
41 import org.json.simple.parser.ParseException;
44 * A client for fetching and processing Ensembl feature data in GFF format by
45 * calling the overlap REST service
48 * @see http://rest.ensembl.org/documentation/info/overlap_id
50 class EnsemblFeatures extends EnsemblRestClient
53 * The default features to retrieve from Ensembl
54 * can override in getSequenceRecords parameter
56 private EnsemblFeatureType[] featuresWanted = { EnsemblFeatureType.cds,
57 EnsemblFeatureType.exon, EnsemblFeatureType.variation };
60 * Default constructor (to use rest.ensembl.org)
62 public EnsemblFeatures()
68 * Constructor given the target domain to fetch data from
72 public EnsemblFeatures(String d)
78 public String getDbName()
80 return "ENSEMBL (features)";
84 * Makes a query to the REST overlap endpoint for the given sequence
85 * identifier. This returns an 'alignment' consisting of one 'dummy sequence'
86 * (the genomic sequence for which overlap features are returned by the
87 * service). This sequence will have on it sequence features which are the
88 * real information of interest, such as CDS regions or sequence variations.
91 public AlignmentI getSequenceRecords(String query) throws IOException
93 // TODO: use a vararg String... for getSequenceRecords instead?
94 List<String> queries = new ArrayList<>();
96 BufferedReader fp = getSequenceReader(queries);
102 SequenceI seq = parseFeaturesJson(fp);
103 return new Alignment(new SequenceI[] { seq });
107 * Parses the JSON response into Jalview sequence features and attaches them
108 * to a dummy sequence
113 private SequenceI parseFeaturesJson(BufferedReader br)
115 SequenceI seq = new Sequence("Dummy", "");
117 JSONParser jp = new JSONParser();
120 JSONArray responses = (JSONArray) jp.parse(br);
121 Iterator rvals = responses.iterator();
122 while (rvals.hasNext())
126 JSONObject obj = (JSONObject) rvals.next();
127 String type = obj.get("feature_type").toString();
128 int start = Integer.parseInt(obj.get("start").toString());
129 int end = Integer.parseInt(obj.get("end").toString());
130 String source = obj.get("source").toString();
131 String strand = obj.get("strand").toString();
132 Object value = obj.get("consequence_type");
133 value = obj.get("alleles");
134 JSONArray allelesArray = (JSONArray) value;
135 String alleles = allelesArray == null ? null
136 : allelesArray.toString(); // todo need as a List?
137 value = obj.get("clinical_significance");
138 JSONArray clinSigArray = (JSONArray) value;
139 String clinSig = clinSigArray == null ? null
140 : clinSigArray.toString();
143 * convert 'variation' to 'sequence_variant', and 'cds' to 'CDS'
144 * so as to have a valid SO term for the feature type
145 * ('gene', 'exon', 'transcript' don't need any conversion)
147 if ("variation".equals(type))
149 type = SequenceOntologyI.SEQUENCE_VARIANT;
151 else if (SequenceOntologyI.CDS.equalsIgnoreCase((type)))
153 type = SequenceOntologyI.CDS;
156 String desc = getFirstNotNull(obj, "alleles", "external_name",
158 SequenceFeature sf = new SequenceFeature(type, desc, start, end,
160 sf.setStrand("1".equals(strand) ? "+" : "-");
161 setFeatureAttribute(sf, obj, "id");
162 setFeatureAttribute(sf, obj, "Parent");
163 setFeatureAttribute(sf, obj, "consequence_type");
164 sf.setValue("alleles", alleles);
165 sf.setValue("clinical_significance", clinSig);
167 seq.addSequenceFeature(sf);
168 } catch (Throwable t)
170 // ignore - keep trying other features
173 } catch (ParseException | IOException e)
182 * Returns the first non-null attribute found (if any) as a string
188 protected String getFirstNotNull(JSONObject obj, String... keys)
192 for (String key : keys)
194 Object val = obj.get(key);
197 String s = val.toString();
208 * A helper method that reads the 'key' entry in the JSON object, and if not
209 * null, sets its string value as an attribute on the sequence feature
215 protected void setFeatureAttribute(SequenceFeature sf, JSONObject obj,
218 Object object = obj.get(key);
221 sf.setValue(key, object.toString());
226 * Returns a URL for the REST overlap endpoint
232 protected URL getUrl(List<String> ids) throws MalformedURLException
234 StringBuffer urlstring = new StringBuffer(128);
235 urlstring.append(getDomain()).append("/overlap/id/").append(ids.get(0));
237 // @see https://github.com/Ensembl/ensembl-rest/wiki/Output-formats
238 urlstring.append("?content-type=" + getResponseMimeType());
241 * specify object_type=gene in case is shared by transcript and/or protein;
242 * currently only fetching features for gene sequences;
243 * refactor in future if needed to fetch for transcripts
245 urlstring.append("&").append(OBJECT_TYPE).append("=")
246 .append(OBJECT_TYPE_GENE);
249 * specify features to retrieve
250 * @see http://rest.ensembl.org/documentation/info/overlap_id
251 * could make the list a configurable entry in .jalview_properties
253 for (EnsemblFeatureType feature : featuresWanted)
255 urlstring.append("&feature=").append(feature.name());
258 return new URL(urlstring.toString());
262 protected boolean useGetRequest()
268 * Returns the MIME type for GFF3. For GET requests the Content-type header
269 * describes the required encoding of the response.
272 protected String getRequestMimeType()
274 return "application/json";
278 * Returns the MIME type wanted for the response
281 protected String getResponseMimeType()
283 return "application/json";
287 * Overloaded method that allows a list of features to retrieve to be
293 * @throws IOException
295 protected AlignmentI getSequenceRecords(String accId,
296 EnsemblFeatureType[] features) throws IOException
298 featuresWanted = features;
299 return getSequenceRecords(accId);