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.IOException;
24 import java.net.MalformedURLException;
26 import java.util.ArrayList;
27 import java.util.Iterator;
28 import java.util.List;
31 import org.json.simple.parser.ParseException;
33 import jalview.datamodel.Alignment;
34 import jalview.datamodel.AlignmentI;
35 import jalview.datamodel.Sequence;
36 import jalview.datamodel.SequenceFeature;
37 import jalview.datamodel.SequenceI;
38 import jalview.io.gff.SequenceOntologyI;
39 import jalview.util.JSONUtils;
42 * A client for fetching and processing Ensembl feature data in GFF format by
43 * calling the overlap REST service
46 * @see http://rest.ensembl.org/documentation/info/overlap_id
48 class EnsemblFeatures extends EnsemblRestClient
51 * The default features to retrieve from Ensembl
52 * can override in getSequenceRecords parameter
54 private EnsemblFeatureType[] featuresWanted = { EnsemblFeatureType.cds,
55 EnsemblFeatureType.exon, EnsemblFeatureType.variation };
58 * Default constructor (to use rest.ensembl.org)
60 public EnsemblFeatures()
66 * Constructor given the target domain to fetch data from
70 public EnsemblFeatures(String d)
76 public String getDbName()
78 return "ENSEMBL (features)";
82 * Makes a query to the REST overlap endpoint for the given sequence
83 * identifier. This returns an 'alignment' consisting of one 'dummy sequence'
84 * (the genomic sequence for which overlap features are returned by the
85 * service). This sequence will have on it sequence features which are the
86 * real information of interest, such as CDS regions or sequence variations.
89 public AlignmentI getSequenceRecords(String query) throws IOException
91 // TODO: use a vararg String... for getSequenceRecords instead?
93 List<String> queries = new ArrayList<>();
95 SequenceI seq = parseFeaturesJson(queries);
98 return new Alignment(new SequenceI[] { seq });
103 * Parses the JSON response into Jalview sequence features and attaches them
104 * to a dummy sequence
109 @SuppressWarnings("unchecked")
110 private SequenceI parseFeaturesJson(List<String> queries)
112 SequenceI seq = new Sequence("Dummy", "");
115 Iterator<Object> rvals = (Iterator<Object>) getJSON(null, queries, -1, MODE_ITERATOR, null);
120 while (rvals.hasNext())
124 Map<String, Object> obj = (Map<String, Object>) rvals.next();
125 String type = obj.get("feature_type").toString();
126 int start = Integer.parseInt(obj.get("start").toString());
127 int end = Integer.parseInt(obj.get("end").toString());
128 String source = obj.get("source").toString();
129 String strand = obj.get("strand").toString();
130 Object phase = obj.get("phase");
131 String alleles = JSONUtils
132 .arrayToStringList((List<Object>) obj.get("alleles"));
133 String clinSig = JSONUtils
135 (List<Object>) obj.get("clinical_significance"));
138 * convert 'variation' to 'sequence_variant', and 'cds' to 'CDS'
139 * so as to have a valid SO term for the feature type
140 * ('gene', 'exon', 'transcript' don't need any conversion)
142 if ("variation".equals(type))
144 type = SequenceOntologyI.SEQUENCE_VARIANT;
146 else if (SequenceOntologyI.CDS.equalsIgnoreCase((type)))
148 type = SequenceOntologyI.CDS;
151 String desc = getFirstNotNull(obj, "alleles", "external_name",
153 SequenceFeature sf = new SequenceFeature(type, desc, start, end,
155 sf.setStrand("1".equals(strand) ? "+" : "-");
158 sf.setPhase(phase.toString());
160 setFeatureAttribute(sf, obj, "id");
161 setFeatureAttribute(sf, obj, "Parent");
162 setFeatureAttribute(sf, obj, "consequence_type");
163 sf.setValue("alleles", alleles);
164 sf.setValue("clinical_significance", clinSig);
166 seq.addSequenceFeature(sf);
168 } catch (Throwable t)
170 // ignore - keep trying other features
173 } catch (ParseException | IOException e)
183 * Returns the first non-null attribute found (if any) as a string, formatted
184 * suitably for display as feature description or tooltip. Answers null if
185 * none of the attribute keys is present.
191 @SuppressWarnings("unchecked")
192 protected String getFirstNotNull(Map<String, Object> obj, String... keys)
194 for (String key : keys)
196 Object val = obj.get(key);
199 String s = val instanceof List<?>
200 ? JSONUtils.arrayToStringList((List<Object>) val)
212 * A helper method that reads the 'key' entry in the JSON object, and if not
213 * null, sets its string value as an attribute on the sequence feature
219 protected void setFeatureAttribute(SequenceFeature sf, Map<String, Object> obj,
222 Object object = obj.get(key);
225 sf.setValue(key, object.toString());
230 * Returns a URL for the REST overlap endpoint
236 protected URL getUrl(List<String> ids) throws MalformedURLException
238 StringBuffer urlstring = new StringBuffer(128);
239 urlstring.append(getDomain()).append("/overlap/id/").append(ids.get(0));
241 // @see https://github.com/Ensembl/ensembl-rest/wiki/Output-formats
242 urlstring.append("?content-type=" + getResponseMimeType());
245 * specify object_type=gene in case is shared by transcript and/or protein;
246 * currently only fetching features for gene sequences;
247 * refactor in future if needed to fetch for transcripts
249 urlstring.append("&").append(OBJECT_TYPE).append("=")
250 .append(OBJECT_TYPE_GENE);
253 * specify features to retrieve
254 * @see http://rest.ensembl.org/documentation/info/overlap_id
255 * could make the list a configurable entry in .jalview_properties
257 for (EnsemblFeatureType feature : featuresWanted)
259 urlstring.append("&feature=").append(feature.name());
262 return new URL(urlstring.toString());
266 protected boolean useGetRequest()
272 * Returns the MIME type for GFF3. For GET requests the Content-type header
273 * describes the required encoding of the response.
276 protected String getRequestMimeType()
278 return "application/json";
282 * Returns the MIME type wanted for the response
285 protected String getResponseMimeType()
287 return "application/json";
291 * Overloaded method that allows a list of features to retrieve to be
297 * @throws IOException
299 protected AlignmentI getSequenceRecords(String accId,
300 EnsemblFeatureType[] features) throws IOException
302 featuresWanted = features;
303 return getSequenceRecords(accId);