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.api.FeatureColourI;
24 import jalview.api.FeatureSettingsModelI;
25 import jalview.datamodel.AlignmentI;
26 import jalview.datamodel.Sequence;
27 import jalview.datamodel.SequenceFeature;
28 import jalview.datamodel.SequenceI;
29 import jalview.datamodel.features.SequenceFeatures;
30 import jalview.io.gff.SequenceOntologyFactory;
31 import jalview.io.gff.SequenceOntologyI;
32 import jalview.schemes.FeatureColour;
33 import jalview.schemes.FeatureSettingsAdapter;
34 import jalview.util.MapList;
36 import java.awt.Color;
37 import java.io.UnsupportedEncodingException;
38 import java.net.URLDecoder;
39 import java.util.ArrayList;
40 import java.util.Arrays;
41 import java.util.List;
43 import com.stevesoft.pat.Regex;
46 * A class that fetches genomic sequence and all transcripts for an Ensembl gene
50 public class EnsemblGene extends EnsemblSeqProxy
52 private static final String GENE_PREFIX = "gene:";
55 * accepts anything as we will attempt lookup of gene or
56 * transcript id or gene name
58 private static final Regex ACCESSION_REGEX = new Regex(".*");
60 private static final EnsemblFeatureType[] FEATURES_TO_FETCH = {
61 EnsemblFeatureType.gene, EnsemblFeatureType.transcript,
62 EnsemblFeatureType.exon, EnsemblFeatureType.cds,
63 EnsemblFeatureType.variation };
66 * Default constructor (to use rest.ensembl.org)
74 * Constructor given the target domain to fetch data from
78 public EnsemblGene(String d)
84 public String getDbName()
90 protected EnsemblFeatureType[] getFeaturesToFetch()
92 return FEATURES_TO_FETCH;
96 protected EnsemblSeqType getSourceEnsemblType()
98 return EnsemblSeqType.GENOMIC;
102 protected String getObjectType()
104 return OBJECT_TYPE_GENE;
108 * Returns an alignment containing the gene(s) for the given gene or
109 * transcript identifier, or external identifier (e.g. Uniprot id). If given a
110 * gene name or external identifier, returns any related gene sequences found
111 * for model organisms. If only a single gene is queried for, then its
112 * transcripts are also retrieved and added to the alignment. <br>
115 * <li>resolves a transcript identifier by looking up its parent gene id</li>
116 * <li>resolves an external identifier by looking up xref-ed gene ids</li>
117 * <li>fetches the gene sequence</li>
118 * <li>fetches features on the sequence</li>
119 * <li>identifies "transcript" features whose Parent is the requested
121 * <li>fetches the transcript sequence for each transcript</li>
122 * <li>makes a mapping from the gene to each transcript</li>
123 * <li>copies features from gene to transcript sequences</li>
124 * <li>fetches the protein sequence for each transcript, maps and saves it as
125 * a cross-reference</li>
126 * <li>aligns each transcript against the gene sequence based on the position
131 * a single gene or transcript identifier or gene name
132 * @return an alignment containing a gene, and possibly transcripts, or null
135 public AlignmentI getSequenceRecords(String query) throws Exception
138 * convert to a non-duplicated list of gene identifiers
140 List<String> geneIds = getGeneIds(query);
142 AlignmentI al = null;
143 for (String geneId : geneIds)
146 * fetch the gene sequence(s) with features and xrefs
148 AlignmentI geneAlignment = super.getSequenceRecords(geneId);
149 if (geneAlignment == null)
153 if (geneAlignment.getHeight() == 1)
155 // ensure id has 'correct' case for the Ensembl identifier
156 geneId = geneAlignment.getSequenceAt(0).getName();
157 getTranscripts(geneAlignment, geneId);
165 al.append(geneAlignment);
172 * Converts a query, which may contain one or more gene, transcript, or
173 * external (to Ensembl) identifiers, into a non-redundant list of gene
179 List<String> getGeneIds(String accessions)
181 List<String> geneIds = new ArrayList<>();
183 for (String acc : accessions.split(getAccessionSeparator()))
186 * First try lookup as an Ensembl (gene or transcript) identifier
188 String geneId = new EnsemblLookup(getDomain()).getGeneId(acc);
191 if (!geneIds.contains(geneId))
199 * if given a gene or other external name, lookup and fetch
200 * the corresponding gene for all model organisms
202 List<String> ids = new EnsemblSymbol(getDomain(), getDbSource(),
203 getDbVersion()).getGeneIds(acc);
204 for (String id : ids)
206 if (!geneIds.contains(id))
217 * Constructs all transcripts for the gene, as identified by "transcript"
218 * features whose Parent is the requested gene. The coding transcript
219 * sequences (i.e. with introns omitted) are added to the alignment.
225 protected void getTranscripts(AlignmentI al, String accId)
228 SequenceI gene = al.getSequenceAt(0);
229 List<SequenceFeature> transcriptFeatures = getTranscriptFeatures(accId,
232 for (SequenceFeature transcriptFeature : transcriptFeatures)
234 makeTranscript(transcriptFeature, al, gene);
237 clearGeneFeatures(gene);
241 * Remove unwanted features (transcript, exon, CDS) from the gene sequence
242 * after we have used them to derive transcripts and transfer features
246 protected void clearGeneFeatures(SequenceI gene)
249 * Note we include NMD_transcript_variant here because it behaves like
250 * 'transcript' in Ensembl, although strictly speaking it is not
251 * (it is a sub-type of sequence_variant)
253 String[] soTerms = new String[] {
254 SequenceOntologyI.NMD_TRANSCRIPT_VARIANT,
255 SequenceOntologyI.TRANSCRIPT, SequenceOntologyI.EXON,
256 SequenceOntologyI.CDS };
257 List<SequenceFeature> sfs = gene.getFeatures().getFeaturesByOntology(
259 for (SequenceFeature sf : sfs)
261 gene.deleteFeature(sf);
266 * Constructs a spliced transcript sequence by finding 'exon' features for the
267 * given id (or failing that 'CDS'). Copies features on to the new sequence.
268 * 'Aligns' the new sequence against the gene sequence by padding with gaps,
269 * and adds it to the alignment.
271 * @param transcriptFeature
273 * the alignment to which to add the new sequence
275 * the parent gene sequence, with features
278 SequenceI makeTranscript(SequenceFeature transcriptFeature, AlignmentI al,
281 String accId = getTranscriptId(transcriptFeature);
288 * NB we are mapping from gene sequence (not genome), so do not
289 * need to check for reverse strand (gene and transcript sequences
290 * are in forward sense)
294 * make a gene-length sequence filled with gaps
295 * we will fill in the bases for transcript regions
297 char[] seqChars = new char[gene.getLength()];
298 Arrays.fill(seqChars, al.getGapCharacter());
301 * look for exon features of the transcript, failing that for CDS
302 * (for example ENSG00000124610 has 1 CDS but no exon features)
304 String parentId = "transcript:" + accId;
305 List<SequenceFeature> splices = findFeatures(gene,
306 SequenceOntologyI.EXON, parentId);
307 if (splices.isEmpty())
309 splices = findFeatures(gene, SequenceOntologyI.CDS, parentId);
311 SequenceFeatures.sortFeatures(splices, true);
313 int transcriptLength = 0;
314 final char[] geneChars = gene.getSequence();
315 int offset = gene.getStart(); // to convert to 0-based positions
316 List<int[]> mappedFrom = new ArrayList<>();
318 for (SequenceFeature sf : splices)
320 int start = sf.getBegin() - offset;
321 int end = sf.getEnd() - offset;
322 int spliceLength = end - start + 1;
323 System.arraycopy(geneChars, start, seqChars, start, spliceLength);
324 transcriptLength += spliceLength;
325 mappedFrom.add(new int[] { sf.getBegin(), sf.getEnd() });
328 Sequence transcript = new Sequence(accId, seqChars, 1,
332 * Ensembl has gene name as transcript Name
333 * EnsemblGenomes doesn't, but has a url-encoded description field
335 String description = (String) transcriptFeature.getValue(NAME);
336 if (description == null)
338 description = (String) transcriptFeature.getValue(DESCRIPTION);
340 if (description != null)
344 transcript.setDescription(URLDecoder.decode(description, "UTF-8"));
345 } catch (UnsupportedEncodingException e)
347 e.printStackTrace(); // as if
350 transcript.createDatasetSequence();
352 al.addSequence(transcript);
355 * transfer features to the new sequence; we use EnsemblCdna to do this,
356 * to filter out unwanted features types (see method retainFeature)
358 List<int[]> mapTo = new ArrayList<>();
359 mapTo.add(new int[] { 1, transcriptLength });
360 MapList mapping = new MapList(mappedFrom, mapTo, 1, 1);
361 EnsemblCdna cdna = new EnsemblCdna(getDomain());
362 cdna.transferFeatures(gene.getFeatures().getPositionalFeatures(),
363 transcript.getDatasetSequence(), mapping, parentId);
366 * fetch and save cross-references
368 cdna.getCrossReferences(transcript);
371 * and finally fetch the protein product and save as a cross-reference
373 cdna.addProteinProduct(transcript);
379 * Returns the 'transcript_id' property of the sequence feature (or null)
384 protected String getTranscriptId(SequenceFeature feature)
386 return (String) feature.getValue("transcript_id");
390 * Returns a list of the transcript features on the sequence whose Parent is
391 * the gene for the accession id.
393 * Transcript features are those of type "transcript", or any of its sub-types
394 * in the Sequence Ontology e.g. "mRNA", "processed_transcript". We also
395 * include "NMD_transcript_variant", because this type behaves like a
396 * transcript identifier in Ensembl, although strictly speaking it is not in
400 * @param geneSequence
403 protected List<SequenceFeature> getTranscriptFeatures(String accId,
404 SequenceI geneSequence)
406 List<SequenceFeature> transcriptFeatures = new ArrayList<>();
408 String parentIdentifier = GENE_PREFIX + accId;
410 List<SequenceFeature> sfs = geneSequence.getFeatures()
411 .getFeaturesByOntology(SequenceOntologyI.TRANSCRIPT);
412 sfs.addAll(geneSequence.getFeatures().getPositionalFeatures(
413 SequenceOntologyI.NMD_TRANSCRIPT_VARIANT));
415 for (SequenceFeature sf : sfs)
417 String parent = (String) sf.getValue(PARENT);
418 if (parentIdentifier.equalsIgnoreCase(parent))
420 transcriptFeatures.add(sf);
424 return transcriptFeatures;
428 public String getDescription()
430 return "Fetches all transcripts and variant features for a gene or transcript";
434 * Default test query is a gene id (can also enter a transcript id)
437 public String getTestQuery()
439 return "ENSG00000157764"; // BRAF, 5 transcripts, reverse strand
440 // ENSG00000090266 // NDUFB2, 15 transcripts, forward strand
441 // ENSG00000101812 // H2BFM histone, 3 transcripts, forward strand
442 // ENSG00000123569 // H2BFWT histone, 2 transcripts, reverse strand
446 * Answers true for a feature of type 'gene' (or a sub-type of gene in the
447 * Sequence Ontology), whose ID is the accession we are retrieving
450 protected boolean identifiesSequence(SequenceFeature sf, String accId)
452 if (SequenceOntologyFactory.getInstance().isA(sf.getType(),
453 SequenceOntologyI.GENE))
455 // NB features as gff use 'ID'; rest services return as 'id'
456 String id = (String) sf.getValue("ID");
457 if ((GENE_PREFIX + accId).equalsIgnoreCase(id))
466 * Answers true unless feature type is 'gene', or 'transcript' with a parent
467 * which is a different gene. We need the gene features to identify the range,
468 * but it is redundant information on the gene sequence. Checking the parent
469 * allows us to drop transcript features which belong to different
470 * (overlapping) genes.
473 protected boolean retainFeature(SequenceFeature sf, String accessionId)
475 SequenceOntologyI so = SequenceOntologyFactory.getInstance();
476 String type = sf.getType();
477 if (so.isA(type, SequenceOntologyI.GENE))
481 if (isTranscript(type))
483 String parent = (String) sf.getValue(PARENT);
484 if (!(GENE_PREFIX + accessionId).equalsIgnoreCase(parent))
493 * Answers false. This allows an optimisation - a single 'gene' feature is all
494 * that is needed to identify the positions of the gene on the genomic
498 protected boolean isSpliceable()
504 * Override to do nothing as Ensembl doesn't return a protein sequence for a
508 protected void addProteinProduct(SequenceI querySeq)
513 public Regex getAccessionValidator()
515 return ACCESSION_REGEX;
519 * Returns a descriptor for suitable feature display settings with
521 * <li>only exon or sequence_variant features (or their subtypes in the
522 * Sequence Ontology) visible</li>
523 * <li>variant features coloured red</li>
524 * <li>exon features coloured by label (exon name)</li>
525 * <li>variants displayed above (on top of) exons</li>
529 public FeatureSettingsModelI getFeatureColourScheme()
531 return new FeatureSettingsAdapter()
533 SequenceOntologyI so = SequenceOntologyFactory.getInstance();
536 public boolean isFeatureDisplayed(String type)
538 return (so.isA(type, SequenceOntologyI.EXON)
539 || so.isA(type, SequenceOntologyI.SEQUENCE_VARIANT));
543 public FeatureColourI getFeatureColour(String type)
545 if (so.isA(type, SequenceOntologyI.EXON))
547 return new FeatureColour()
550 public boolean isColourByLabel()
556 if (so.isA(type, SequenceOntologyI.SEQUENCE_VARIANT))
558 return new FeatureColour()
562 public Color getColour()
572 * order to render sequence_variant after exon after the rest
575 public int compare(String feature1, String feature2)
577 if (so.isA(feature1, SequenceOntologyI.SEQUENCE_VARIANT))
581 if (so.isA(feature2, SequenceOntologyI.SEQUENCE_VARIANT))
585 if (so.isA(feature1, SequenceOntologyI.EXON))
589 if (so.isA(feature2, SequenceOntologyI.EXON))