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.io.gff.SequenceOntologyFactory;
30 import jalview.io.gff.SequenceOntologyI;
31 import jalview.schemes.FeatureColour;
32 import jalview.schemes.FeatureSettingsAdapter;
33 import jalview.util.MapList;
35 import java.awt.Color;
36 import java.io.UnsupportedEncodingException;
37 import java.net.URLDecoder;
38 import java.util.ArrayList;
39 import java.util.Arrays;
40 import java.util.List;
42 import com.stevesoft.pat.Regex;
45 * A class that fetches genomic sequence and all transcripts for an Ensembl gene
49 public class EnsemblGene extends EnsemblSeqProxy
51 private static final String GENE_PREFIX = "gene:";
54 * accepts anything as we will attempt lookup of gene or
55 * transcript id or gene name
57 private static final Regex ACCESSION_REGEX = new Regex(".*");
59 private static final EnsemblFeatureType[] FEATURES_TO_FETCH = {
60 EnsemblFeatureType.gene, EnsemblFeatureType.transcript,
61 EnsemblFeatureType.exon, EnsemblFeatureType.cds,
62 EnsemblFeatureType.variation };
65 * Default constructor (to use rest.ensembl.org)
73 * Constructor given the target domain to fetch data from
77 public EnsemblGene(String d)
83 public String getDbName()
89 protected EnsemblFeatureType[] getFeaturesToFetch()
91 return FEATURES_TO_FETCH;
95 protected EnsemblSeqType getSourceEnsemblType()
97 return EnsemblSeqType.GENOMIC;
101 * Returns an alignment containing the gene(s) for the given gene or
102 * transcript identifier, or external identifier (e.g. Uniprot id). If given a
103 * gene name or external identifier, returns any related gene sequences found
104 * for model organisms. If only a single gene is queried for, then its
105 * transcripts are also retrieved and added to the alignment. <br>
108 * <li>resolves a transcript identifier by looking up its parent gene id</li>
109 * <li>resolves an external identifier by looking up xref-ed gene ids</li>
110 * <li>fetches the gene sequence</li>
111 * <li>fetches features on the sequence</li>
112 * <li>identifies "transcript" features whose Parent is the requested
114 * <li>fetches the transcript sequence for each transcript</li>
115 * <li>makes a mapping from the gene to each transcript</li>
116 * <li>copies features from gene to transcript sequences</li>
117 * <li>fetches the protein sequence for each transcript, maps and saves it as
118 * a cross-reference</li>
119 * <li>aligns each transcript against the gene sequence based on the position
124 * a single gene or transcript identifier or gene name
125 * @return an alignment containing a gene, and possibly transcripts, or null
128 public AlignmentI getSequenceRecords(String query) throws Exception
131 * convert to a non-duplicated list of gene identifiers
133 List<String> geneIds = getGeneIds(query);
135 AlignmentI al = null;
136 for (String geneId : geneIds)
139 * fetch the gene sequence(s) with features and xrefs
141 AlignmentI geneAlignment = super.getSequenceRecords(geneId);
142 if (geneAlignment == null)
146 if (geneAlignment.getHeight() == 1)
148 getTranscripts(geneAlignment, geneId);
156 al.append(geneAlignment);
163 * Converts a query, which may contain one or more gene or transcript
164 * identifiers, into a non-redundant list of gene identifiers.
169 List<String> getGeneIds(String accessions)
171 List<String> geneIds = new ArrayList<String>();
173 for (String acc : accessions.split(getAccessionSeparator()))
175 if (isGeneIdentifier(acc))
177 if (!geneIds.contains(acc))
184 * if given a transcript id, look up its gene parent
186 else if (isTranscriptIdentifier(acc))
188 String geneId = new EnsemblLookup(getDomain()).getParent(acc);
189 if (geneId != null && !geneIds.contains(geneId))
194 else if (isProteinIdentifier(acc))
196 String tscriptId = new EnsemblLookup(getDomain()).getParent(acc);
197 if (tscriptId != null)
199 String geneId = new EnsemblLookup(getDomain())
200 .getParent(tscriptId);
202 if (geneId != null && !geneIds.contains(geneId))
207 // NOTE - acc is lost if it resembles an ENS.+ ID but isn't actually
208 // resolving to one... e.g. ENSMICP00000009241
211 * if given a gene or other external name, lookup and fetch
212 * the corresponding gene for all model organisms
216 List<String> ids = new EnsemblSymbol(getDomain(), getDbSource(),
217 getDbVersion()).getIds(acc);
218 for (String geneId : ids)
220 if (!geneIds.contains(geneId))
231 * Attempts to get Ensembl stable identifiers for model organisms for a gene
232 * name by calling the xrefs symbol REST service to resolve the gene name.
237 protected String getGeneIdentifiersForName(String query)
239 List<String> ids = new EnsemblSymbol(getDomain(), getDbSource(),
240 getDbVersion()).getIds(query);
243 for (String id : ids)
245 if (isGeneIdentifier(id))
255 * Constructs all transcripts for the gene, as identified by "transcript"
256 * features whose Parent is the requested gene. The coding transcript
257 * sequences (i.e. with introns omitted) are added to the alignment.
263 protected void getTranscripts(AlignmentI al, String accId)
266 SequenceI gene = al.getSequenceAt(0);
267 List<SequenceFeature> transcriptFeatures = getTranscriptFeatures(accId,
270 for (SequenceFeature transcriptFeature : transcriptFeatures)
272 makeTranscript(transcriptFeature, al, gene);
275 clearGeneFeatures(gene);
279 * Remove unwanted features (transcript, exon, CDS) from the gene sequence
280 * after we have used them to derive transcripts and transfer features
284 protected void clearGeneFeatures(SequenceI gene)
286 SequenceFeature[] sfs = gene.getSequenceFeatures();
289 SequenceOntologyI so = SequenceOntologyFactory.getInstance();
290 List<SequenceFeature> filtered = new ArrayList<SequenceFeature>();
291 for (SequenceFeature sf : sfs)
293 String type = sf.getType();
294 if (!isTranscript(type) && !so.isA(type, SequenceOntologyI.EXON)
295 && !so.isA(type, SequenceOntologyI.CDS))
300 gene.setSequenceFeatures(
301 filtered.toArray(new SequenceFeature[filtered.size()]));
306 * Constructs a spliced transcript sequence by finding 'exon' features for the
307 * given id (or failing that 'CDS'). Copies features on to the new sequence.
308 * 'Aligns' the new sequence against the gene sequence by padding with gaps,
309 * and adds it to the alignment.
311 * @param transcriptFeature
313 * the alignment to which to add the new sequence
315 * the parent gene sequence, with features
318 SequenceI makeTranscript(SequenceFeature transcriptFeature, AlignmentI al,
321 String accId = getTranscriptId(transcriptFeature);
328 * NB we are mapping from gene sequence (not genome), so do not
329 * need to check for reverse strand (gene and transcript sequences
330 * are in forward sense)
334 * make a gene-length sequence filled with gaps
335 * we will fill in the bases for transcript regions
337 char[] seqChars = new char[gene.getLength()];
338 Arrays.fill(seqChars, al.getGapCharacter());
341 * look for exon features of the transcript, failing that for CDS
342 * (for example ENSG00000124610 has 1 CDS but no exon features)
344 String parentId = "transcript:" + accId;
345 List<SequenceFeature> splices = findFeatures(gene,
346 SequenceOntologyI.EXON, parentId);
347 if (splices.isEmpty())
349 splices = findFeatures(gene, SequenceOntologyI.CDS, parentId);
352 int transcriptLength = 0;
353 final char[] geneChars = gene.getSequence();
354 int offset = gene.getStart(); // to convert to 0-based positions
355 List<int[]> mappedFrom = new ArrayList<int[]>();
357 for (SequenceFeature sf : splices)
359 int start = sf.getBegin() - offset;
360 int end = sf.getEnd() - offset;
361 int spliceLength = end - start + 1;
362 System.arraycopy(geneChars, start, seqChars, start, spliceLength);
363 transcriptLength += spliceLength;
364 mappedFrom.add(new int[] { sf.getBegin(), sf.getEnd() });
367 Sequence transcript = new Sequence(accId, seqChars, 1,
371 * Ensembl has gene name as transcript Name
372 * EnsemblGenomes doesn't, but has a url-encoded description field
374 String description = (String) transcriptFeature.getValue(NAME);
375 if (description == null)
377 description = (String) transcriptFeature.getValue(DESCRIPTION);
379 if (description != null)
383 transcript.setDescription(URLDecoder.decode(description, "UTF-8"));
384 } catch (UnsupportedEncodingException e)
386 e.printStackTrace(); // as if
389 transcript.createDatasetSequence();
391 al.addSequence(transcript);
394 * transfer features to the new sequence; we use EnsemblCdna to do this,
395 * to filter out unwanted features types (see method retainFeature)
397 List<int[]> mapTo = new ArrayList<int[]>();
398 mapTo.add(new int[] { 1, transcriptLength });
399 MapList mapping = new MapList(mappedFrom, mapTo, 1, 1);
400 EnsemblCdna cdna = new EnsemblCdna(getDomain());
401 cdna.transferFeatures(gene.getSequenceFeatures(),
402 transcript.getDatasetSequence(), mapping, parentId);
405 * fetch and save cross-references
407 cdna.getCrossReferences(transcript);
410 * and finally fetch the protein product and save as a cross-reference
412 cdna.addProteinProduct(transcript);
418 * Returns the 'transcript_id' property of the sequence feature (or null)
423 protected String getTranscriptId(SequenceFeature feature)
425 return (String) feature.getValue("transcript_id");
429 * Returns a list of the transcript features on the sequence whose Parent is
430 * the gene for the accession id.
433 * @param geneSequence
436 protected List<SequenceFeature> getTranscriptFeatures(String accId,
437 SequenceI geneSequence)
439 List<SequenceFeature> transcriptFeatures = new ArrayList<SequenceFeature>();
441 String parentIdentifier = GENE_PREFIX + accId;
442 SequenceFeature[] sfs = geneSequence.getSequenceFeatures();
446 for (SequenceFeature sf : sfs)
448 if (isTranscript(sf.getType()))
450 String parent = (String) sf.getValue(PARENT);
451 if (parentIdentifier.equals(parent))
453 transcriptFeatures.add(sf);
459 return transcriptFeatures;
463 public String getDescription()
465 return "Fetches all transcripts and variant features for a gene or transcript";
469 * Default test query is a gene id (can also enter a transcript id)
472 public String getTestQuery()
474 return "ENSG00000157764"; // BRAF, 5 transcripts, reverse strand
475 // ENSG00000090266 // NDUFB2, 15 transcripts, forward strand
476 // ENSG00000101812 // H2BFM histone, 3 transcripts, forward strand
477 // ENSG00000123569 // H2BFWT histone, 2 transcripts, reverse strand
481 * Answers true for a feature of type 'gene' (or a sub-type of gene in the
482 * Sequence Ontology), whose ID is the accession we are retrieving
485 protected boolean identifiesSequence(SequenceFeature sf, String accId)
487 if (SequenceOntologyFactory.getInstance().isA(sf.getType(),
488 SequenceOntologyI.GENE))
490 String id = (String) sf.getValue(ID);
491 if ((GENE_PREFIX + accId).equals(id))
500 * Answers true unless feature type is 'gene', or 'transcript' with a parent
501 * which is a different gene. We need the gene features to identify the range,
502 * but it is redundant information on the gene sequence. Checking the parent
503 * allows us to drop transcript features which belong to different
504 * (overlapping) genes.
507 protected boolean retainFeature(SequenceFeature sf, String accessionId)
509 SequenceOntologyI so = SequenceOntologyFactory.getInstance();
510 String type = sf.getType();
511 if (so.isA(type, SequenceOntologyI.GENE))
515 if (isTranscript(type))
517 String parent = (String) sf.getValue(PARENT);
518 if (!(GENE_PREFIX + accessionId).equals(parent))
527 * Answers false. This allows an optimisation - a single 'gene' feature is all
528 * that is needed to identify the positions of the gene on the genomic
532 protected boolean isSpliceable()
538 * Override to do nothing as Ensembl doesn't return a protein sequence for a
542 protected void addProteinProduct(SequenceI querySeq)
547 public Regex getAccessionValidator()
549 return ACCESSION_REGEX;
553 * Returns a descriptor for suitable feature display settings with
555 * <li>only exon or sequence_variant features (or their subtypes in the
556 * Sequence Ontology) visible</li>
557 * <li>variant features coloured red</li>
558 * <li>exon features coloured by label (exon name)</li>
559 * <li>variants displayed above (on top of) exons</li>
563 public FeatureSettingsModelI getFeatureColourScheme()
565 return new FeatureSettingsAdapter()
567 SequenceOntologyI so = SequenceOntologyFactory.getInstance();
570 public boolean isFeatureDisplayed(String type)
572 return (so.isA(type, SequenceOntologyI.EXON)
573 || so.isA(type, SequenceOntologyI.SEQUENCE_VARIANT));
577 public FeatureColourI getFeatureColour(String type)
579 if (so.isA(type, SequenceOntologyI.EXON))
581 return new FeatureColour()
584 public boolean isColourByLabel()
590 if (so.isA(type, SequenceOntologyI.SEQUENCE_VARIANT))
592 return new FeatureColour()
596 public Color getColour()
606 * order to render sequence_variant after exon after the rest
609 public int compare(String feature1, String feature2)
611 if (so.isA(feature1, SequenceOntologyI.SEQUENCE_VARIANT))
615 if (so.isA(feature2, SequenceOntologyI.SEQUENCE_VARIANT))
619 if (so.isA(feature1, SequenceOntologyI.EXON))
623 if (so.isA(feature2, SequenceOntologyI.EXON))