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 gene</li>
113 * <li>fetches the transcript sequence for each transcript</li>
114 * <li>makes a mapping from the gene to each transcript</li>
115 * <li>copies features from gene to transcript sequences</li>
116 * <li>fetches the protein sequence for each transcript, maps and saves it as
117 * a cross-reference</li>
118 * <li>aligns each transcript against the gene sequence based on the position
123 * a single gene or transcript identifier or gene name
124 * @return an alignment containing a gene, and possibly transcripts, or null
127 public AlignmentI getSequenceRecords(String query) throws Exception
130 * convert to a non-duplicated list of gene identifiers
132 List<String> geneIds = getGeneIds(query);
134 AlignmentI al = null;
135 for (String geneId : geneIds)
138 * fetch the gene sequence(s) with features and xrefs
140 AlignmentI geneAlignment = super.getSequenceRecords(geneId);
141 if (geneAlignment == null)
145 if (geneAlignment.getHeight() == 1)
147 getTranscripts(geneAlignment, geneId);
155 al.append(geneAlignment);
162 * Converts a query, which may contain one or more gene, transcript, or
163 * external (to Ensembl) identifiers, into a non-redundant list of gene
169 List<String> getGeneIds(String accessions)
171 List<String> geneIds = new ArrayList<String>();
173 for (String acc : accessions.split(getAccessionSeparator()))
176 * First try lookup as an Ensembl (gene or transcript) identifier
178 String geneId = new EnsemblLookup(getDomain()).getGeneId(acc);
181 if (!geneIds.contains(geneId))
189 * if given a gene or other external name, lookup and fetch
190 * the corresponding gene for all model organisms
192 List<String> ids = new EnsemblSymbol(getDomain(), getDbSource(),
193 getDbVersion()).getGeneIds(acc);
194 for (String id : ids)
196 if (!geneIds.contains(id))
207 * Constructs all transcripts for the gene, as identified by "transcript"
208 * features whose Parent is the requested gene. The coding transcript
209 * sequences (i.e. with introns omitted) are added to the alignment.
215 protected void getTranscripts(AlignmentI al, String accId)
218 SequenceI gene = al.getSequenceAt(0);
219 List<SequenceFeature> transcriptFeatures = getTranscriptFeatures(accId,
222 for (SequenceFeature transcriptFeature : transcriptFeatures)
224 makeTranscript(transcriptFeature, al, gene);
227 clearGeneFeatures(gene);
231 * Remove unwanted features (transcript, exon, CDS) from the gene sequence
232 * after we have used them to derive transcripts and transfer features
236 protected void clearGeneFeatures(SequenceI gene)
238 SequenceFeature[] sfs = gene.getSequenceFeatures();
241 SequenceOntologyI so = SequenceOntologyFactory.getInstance();
242 List<SequenceFeature> filtered = new ArrayList<SequenceFeature>();
243 for (SequenceFeature sf : sfs)
245 String type = sf.getType();
246 if (!isTranscript(type) && !so.isA(type, SequenceOntologyI.EXON)
247 && !so.isA(type, SequenceOntologyI.CDS))
252 gene.setSequenceFeatures(filtered
253 .toArray(new SequenceFeature[filtered.size()]));
258 * Constructs a spliced transcript sequence by finding 'exon' features for the
259 * given id (or failing that 'CDS'). Copies features on to the new sequence.
260 * 'Aligns' the new sequence against the gene sequence by padding with gaps,
261 * and adds it to the alignment.
263 * @param transcriptFeature
265 * the alignment to which to add the new sequence
267 * the parent gene sequence, with features
270 SequenceI makeTranscript(SequenceFeature transcriptFeature,
271 AlignmentI al, SequenceI gene)
273 String accId = getTranscriptId(transcriptFeature);
280 * NB we are mapping from gene sequence (not genome), so do not
281 * need to check for reverse strand (gene and transcript sequences
282 * are in forward sense)
286 * make a gene-length sequence filled with gaps
287 * we will fill in the bases for transcript regions
289 char[] seqChars = new char[gene.getLength()];
290 Arrays.fill(seqChars, al.getGapCharacter());
293 * look for exon features of the transcript, failing that for CDS
294 * (for example ENSG00000124610 has 1 CDS but no exon features)
296 String parentId = "transcript:" + accId;
297 List<SequenceFeature> splices = findFeatures(gene,
298 SequenceOntologyI.EXON, parentId);
299 if (splices.isEmpty())
301 splices = findFeatures(gene, SequenceOntologyI.CDS, parentId);
304 int transcriptLength = 0;
305 final char[] geneChars = gene.getSequence();
306 int offset = gene.getStart(); // to convert to 0-based positions
307 List<int[]> mappedFrom = new ArrayList<int[]>();
309 for (SequenceFeature sf : splices)
311 int start = sf.getBegin() - offset;
312 int end = sf.getEnd() - offset;
313 int spliceLength = end - start + 1;
314 System.arraycopy(geneChars, start, seqChars, start, spliceLength);
315 transcriptLength += spliceLength;
316 mappedFrom.add(new int[] { sf.getBegin(), sf.getEnd() });
319 Sequence transcript = new Sequence(accId, seqChars, 1, transcriptLength);
322 * Ensembl has gene name as transcript Name
323 * EnsemblGenomes doesn't, but has a url-encoded description field
325 String description = (String) transcriptFeature.getValue(NAME);
326 if (description == null)
328 description = (String) transcriptFeature.getValue(DESCRIPTION);
330 if (description != null)
334 transcript.setDescription(URLDecoder.decode(description, "UTF-8"));
335 } catch (UnsupportedEncodingException e)
337 e.printStackTrace(); // as if
340 transcript.createDatasetSequence();
342 al.addSequence(transcript);
345 * transfer features to the new sequence; we use EnsemblCdna to do this,
346 * to filter out unwanted features types (see method retainFeature)
348 List<int[]> mapTo = new ArrayList<int[]>();
349 mapTo.add(new int[] { 1, transcriptLength });
350 MapList mapping = new MapList(mappedFrom, mapTo, 1, 1);
351 EnsemblCdna cdna = new EnsemblCdna(getDomain());
352 cdna.transferFeatures(gene.getSequenceFeatures(),
353 transcript.getDatasetSequence(), mapping, parentId);
356 * fetch and save cross-references
358 cdna.getCrossReferences(transcript);
361 * and finally fetch the protein product and save as a cross-reference
363 cdna.addProteinProduct(transcript);
369 * Returns the 'transcript_id' property of the sequence feature (or null)
374 protected String getTranscriptId(SequenceFeature feature)
376 return (String) feature.getValue("transcript_id");
380 * Returns a list of the transcript features on the sequence whose Parent is
381 * the gene for the accession id.
384 * @param geneSequence
387 protected List<SequenceFeature> getTranscriptFeatures(String accId,
388 SequenceI geneSequence)
390 List<SequenceFeature> transcriptFeatures = new ArrayList<SequenceFeature>();
392 String parentIdentifier = GENE_PREFIX + accId;
393 SequenceFeature[] sfs = geneSequence.getSequenceFeatures();
397 for (SequenceFeature sf : sfs)
399 if (isTranscript(sf.getType()))
401 String parent = (String) sf.getValue(PARENT);
402 if (parentIdentifier.equals(parent))
404 transcriptFeatures.add(sf);
410 return transcriptFeatures;
414 public String getDescription()
416 return "Fetches all transcripts and variant features for a gene or transcript";
420 * Default test query is a gene id (can also enter a transcript id)
423 public String getTestQuery()
425 return "ENSG00000157764"; // BRAF, 5 transcripts, reverse strand
426 // ENSG00000090266 // NDUFB2, 15 transcripts, forward strand
427 // ENSG00000101812 // H2BFM histone, 3 transcripts, forward strand
428 // ENSG00000123569 // H2BFWT histone, 2 transcripts, reverse strand
432 * Answers true for a feature of type 'gene' (or a sub-type of gene in the
433 * Sequence Ontology), whose ID is the accession we are retrieving
436 protected boolean identifiesSequence(SequenceFeature sf, String accId)
438 if (SequenceOntologyFactory.getInstance().isA(sf.getType(),
439 SequenceOntologyI.GENE))
441 String id = (String) sf.getValue(ID);
442 if ((GENE_PREFIX + accId).equals(id))
451 * Answers true unless feature type is 'gene', or 'transcript' with a parent
452 * which is a different gene. We need the gene features to identify the range,
453 * but it is redundant information on the gene sequence. Checking the parent
454 * allows us to drop transcript features which belong to different
455 * (overlapping) genes.
458 protected boolean retainFeature(SequenceFeature sf, String accessionId)
460 SequenceOntologyI so = SequenceOntologyFactory.getInstance();
461 String type = sf.getType();
462 if (so.isA(type, SequenceOntologyI.GENE))
466 if (isTranscript(type))
468 String parent = (String) sf.getValue(PARENT);
469 if (!(GENE_PREFIX + accessionId).equals(parent))
478 * Answers false. This allows an optimisation - a single 'gene' feature is all
479 * that is needed to identify the positions of the gene on the genomic
483 protected boolean isSpliceable()
489 * Override to do nothing as Ensembl doesn't return a protein sequence for a
493 protected void addProteinProduct(SequenceI querySeq)
498 public Regex getAccessionValidator()
500 return ACCESSION_REGEX;
504 * Returns a descriptor for suitable feature display settings with
506 * <li>only exon or sequence_variant features (or their subtypes in the
507 * Sequence Ontology) visible</li>
508 * <li>variant features coloured red</li>
509 * <li>exon features coloured by label (exon name)</li>
510 * <li>variants displayed above (on top of) exons</li>
514 public FeatureSettingsModelI getFeatureColourScheme()
516 return new FeatureSettingsAdapter()
518 SequenceOntologyI so = SequenceOntologyFactory.getInstance();
521 public boolean isFeatureDisplayed(String type)
523 return (so.isA(type, SequenceOntologyI.EXON) || so.isA(type,
524 SequenceOntologyI.SEQUENCE_VARIANT));
528 public FeatureColourI getFeatureColour(String type)
530 if (so.isA(type, SequenceOntologyI.EXON))
532 return new FeatureColour()
535 public boolean isColourByLabel()
541 if (so.isA(type, SequenceOntologyI.SEQUENCE_VARIANT))
543 return new FeatureColour()
547 public Color getColour()
557 * order to render sequence_variant after exon after the rest
560 public int compare(String feature1, String feature2)
562 if (so.isA(feature1, SequenceOntologyI.SEQUENCE_VARIANT))
566 if (so.isA(feature2, SequenceOntologyI.SEQUENCE_VARIANT))
570 if (so.isA(feature1, SequenceOntologyI.EXON))
574 if (so.isA(feature2, SequenceOntologyI.EXON))