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.GeneLociI;
27 import jalview.datamodel.Sequence;
28 import jalview.datamodel.SequenceFeature;
29 import jalview.datamodel.SequenceI;
30 import jalview.datamodel.features.SequenceFeatures;
31 import jalview.io.gff.SequenceOntologyFactory;
32 import jalview.io.gff.SequenceOntologyI;
33 import jalview.schemes.FeatureColour;
34 import jalview.schemes.FeatureSettingsAdapter;
35 import jalview.util.MapList;
37 import java.awt.Color;
38 import java.io.UnsupportedEncodingException;
39 import java.net.URLDecoder;
40 import java.util.ArrayList;
41 import java.util.Arrays;
42 import java.util.List;
44 import com.stevesoft.pat.Regex;
47 * A class that fetches genomic sequence and all transcripts for an Ensembl gene
51 public class EnsemblGene extends EnsemblSeqProxy
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 };
64 private static final String CHROMOSOME = "chromosome";
67 * Default constructor (to use rest.ensembl.org)
75 * Constructor given the target domain to fetch data from
79 public EnsemblGene(String d)
85 public String getDbName()
91 protected EnsemblFeatureType[] getFeaturesToFetch()
93 return FEATURES_TO_FETCH;
97 protected EnsemblSeqType getSourceEnsemblType()
99 return EnsemblSeqType.GENOMIC;
103 protected String getObjectType()
105 return OBJECT_TYPE_GENE;
109 * Returns an alignment containing the gene(s) for the given gene or
110 * transcript identifier, or external identifier (e.g. Uniprot id). If given a
111 * gene name or external identifier, returns any related gene sequences found
112 * for model organisms. If only a single gene is queried for, then its
113 * transcripts are also retrieved and added to the alignment. <br>
116 * <li>resolves a transcript identifier by looking up its parent gene id</li>
117 * <li>resolves an external identifier by looking up xref-ed gene ids</li>
118 * <li>fetches the gene sequence</li>
119 * <li>fetches features on the sequence</li>
120 * <li>identifies "transcript" features whose Parent is the requested
122 * <li>fetches the transcript sequence for each transcript</li>
123 * <li>makes a mapping from the gene to each transcript</li>
124 * <li>copies features from gene to transcript sequences</li>
125 * <li>fetches the protein sequence for each transcript, maps and saves it as
126 * a cross-reference</li>
127 * <li>aligns each transcript against the gene sequence based on the position
132 * a single gene or transcript identifier or gene name
133 * @return an alignment containing a gene, and possibly transcripts, or null
136 public AlignmentI getSequenceRecords(String query) throws Exception
139 * convert to a non-duplicated list of gene identifiers
141 List<String> geneIds = getGeneIds(query);
143 AlignmentI al = null;
144 for (String geneId : geneIds)
147 * fetch the gene sequence(s) with features and xrefs
149 AlignmentI geneAlignment = super.getSequenceRecords(geneId);
150 if (geneAlignment == null)
155 if (geneAlignment.getHeight() == 1)
157 // ensure id has 'correct' case for the Ensembl identifier
158 geneId = geneAlignment.getSequenceAt(0).getName();
160 findGeneLoci(geneAlignment.getSequenceAt(0), geneId);
162 getTranscripts(geneAlignment, geneId);
170 al.append(geneAlignment);
177 * Calls the /lookup/id REST service, parses the response for gene
178 * coordinates, and if successful, adds these to the sequence. If this fails,
179 * fall back on trying to parse the sequence description in case it is in
180 * Ensembl-gene format e.g. chromosome:GRCh38:17:45051610:45109016:1.
185 void findGeneLoci(SequenceI seq, String geneId)
187 GeneLociI geneLoci = new EnsemblLookup(getDomain()).getGeneLoci(geneId);
188 if (geneLoci != null)
190 seq.setGeneLoci(geneLoci.getSpeciesId(), geneLoci.getAssemblyId(),
191 geneLoci.getChromosomeId(), geneLoci.getMapping());
195 parseChromosomeLocations(seq);
200 * Parses and saves fields of an Ensembl-style description e.g.
201 * chromosome:GRCh38:17:45051610:45109016:1
205 boolean parseChromosomeLocations(SequenceI seq)
207 String description = seq.getDescription();
208 if (description == null)
212 String[] tokens = description.split(":");
213 if (tokens.length == 6 && tokens[0].startsWith(CHROMOSOME))
215 String ref = tokens[1];
216 String chrom = tokens[2];
219 int chStart = Integer.parseInt(tokens[3]);
220 int chEnd = Integer.parseInt(tokens[4]);
221 boolean forwardStrand = "1".equals(tokens[5]);
222 String species = ""; // not known here
223 int[] from = new int[] { seq.getStart(), seq.getEnd() };
224 int[] to = new int[] { forwardStrand ? chStart : chEnd,
225 forwardStrand ? chEnd : chStart };
226 MapList map = new MapList(from, to, 1, 1);
227 seq.setGeneLoci(species, ref, chrom, map);
229 } catch (NumberFormatException e)
231 System.err.println("Bad integers in description " + description);
238 * Converts a query, which may contain one or more gene, transcript, or
239 * external (to Ensembl) identifiers, into a non-redundant list of gene
245 List<String> getGeneIds(String accessions)
247 List<String> geneIds = new ArrayList<>();
249 for (String acc : accessions.split(getAccessionSeparator()))
252 * First try lookup as an Ensembl (gene or transcript) identifier
254 String geneId = new EnsemblLookup(getDomain()).getGeneId(acc);
257 if (!geneIds.contains(geneId))
265 * if given a gene or other external name, lookup and fetch
266 * the corresponding gene for all model organisms
268 List<String> ids = new EnsemblSymbol(getDomain(), getDbSource(),
269 getDbVersion()).getGeneIds(acc);
270 for (String id : ids)
272 if (!geneIds.contains(id))
283 * Constructs all transcripts for the gene, as identified by "transcript"
284 * features whose Parent is the requested gene. The coding transcript
285 * sequences (i.e. with introns omitted) are added to the alignment.
291 protected void getTranscripts(AlignmentI al, String accId)
294 SequenceI gene = al.getSequenceAt(0);
295 List<SequenceFeature> transcriptFeatures = getTranscriptFeatures(accId,
298 for (SequenceFeature transcriptFeature : transcriptFeatures)
300 makeTranscript(transcriptFeature, al, gene);
303 clearGeneFeatures(gene);
307 * Remove unwanted features (transcript, exon, CDS) from the gene sequence
308 * after we have used them to derive transcripts and transfer features
312 protected void clearGeneFeatures(SequenceI gene)
315 * Note we include NMD_transcript_variant here because it behaves like
316 * 'transcript' in Ensembl, although strictly speaking it is not
317 * (it is a sub-type of sequence_variant)
319 String[] soTerms = new String[] {
320 SequenceOntologyI.NMD_TRANSCRIPT_VARIANT,
321 SequenceOntologyI.TRANSCRIPT, SequenceOntologyI.EXON,
322 SequenceOntologyI.CDS };
323 List<SequenceFeature> sfs = gene.getFeatures().getFeaturesByOntology(
325 for (SequenceFeature sf : sfs)
327 gene.deleteFeature(sf);
332 * Constructs a spliced transcript sequence by finding 'exon' features for the
333 * given id (or failing that 'CDS'). Copies features on to the new sequence.
334 * 'Aligns' the new sequence against the gene sequence by padding with gaps,
335 * and adds it to the alignment.
337 * @param transcriptFeature
339 * the alignment to which to add the new sequence
341 * the parent gene sequence, with features
344 SequenceI makeTranscript(SequenceFeature transcriptFeature, AlignmentI al,
347 String accId = getTranscriptId(transcriptFeature);
354 * NB we are mapping from gene sequence (not genome), so do not
355 * need to check for reverse strand (gene and transcript sequences
356 * are in forward sense)
360 * make a gene-length sequence filled with gaps
361 * we will fill in the bases for transcript regions
363 char[] seqChars = new char[gene.getLength()];
364 Arrays.fill(seqChars, al.getGapCharacter());
367 * look for exon features of the transcript, failing that for CDS
368 * (for example ENSG00000124610 has 1 CDS but no exon features)
370 String parentId = accId;
371 List<SequenceFeature> splices = findFeatures(gene,
372 SequenceOntologyI.EXON, parentId);
373 if (splices.isEmpty())
375 splices = findFeatures(gene, SequenceOntologyI.CDS, parentId);
377 SequenceFeatures.sortFeatures(splices, true);
379 int transcriptLength = 0;
380 final char[] geneChars = gene.getSequence();
381 int offset = gene.getStart(); // to convert to 0-based positions
382 List<int[]> mappedFrom = new ArrayList<>();
384 for (SequenceFeature sf : splices)
386 int start = sf.getBegin() - offset;
387 int end = sf.getEnd() - offset;
388 int spliceLength = end - start + 1;
389 System.arraycopy(geneChars, start, seqChars, start, spliceLength);
390 transcriptLength += spliceLength;
391 mappedFrom.add(new int[] { sf.getBegin(), sf.getEnd() });
394 Sequence transcript = new Sequence(accId, seqChars, 1,
398 * Ensembl has gene name as transcript Name
399 * EnsemblGenomes doesn't, but has a url-encoded description field
401 String description = transcriptFeature.getDescription();
402 if (description == null)
404 description = (String) transcriptFeature.getValue(DESCRIPTION);
406 if (description != null)
410 transcript.setDescription(URLDecoder.decode(description, "UTF-8"));
411 } catch (UnsupportedEncodingException e)
413 e.printStackTrace(); // as if
416 transcript.createDatasetSequence();
418 al.addSequence(transcript);
421 * transfer features to the new sequence; we use EnsemblCdna to do this,
422 * to filter out unwanted features types (see method retainFeature)
424 List<int[]> mapTo = new ArrayList<>();
425 mapTo.add(new int[] { 1, transcriptLength });
426 MapList mapping = new MapList(mappedFrom, mapTo, 1, 1);
427 EnsemblCdna cdna = new EnsemblCdna(getDomain());
428 cdna.transferFeatures(gene.getFeatures().getPositionalFeatures(),
429 transcript.getDatasetSequence(), mapping, parentId);
431 mapTranscriptToChromosome(transcript, gene, mapping);
434 * fetch and save cross-references
436 cdna.getCrossReferences(transcript);
439 * and finally fetch the protein product and save as a cross-reference
441 cdna.addProteinProduct(transcript);
447 * If the gene has a mapping to chromosome coordinates, derive the transcript
448 * chromosome regions and save on the transcript sequence
453 * the mapping from gene to transcript positions
455 protected void mapTranscriptToChromosome(SequenceI transcript,
456 SequenceI gene, MapList mapping)
458 GeneLociI loci = gene.getGeneLoci();
464 MapList geneMapping = loci.getMapping();
466 List<int[]> exons = mapping.getFromRanges();
467 List<int[]> transcriptLoci = new ArrayList<>();
469 for (int[] exon : exons)
471 transcriptLoci.add(geneMapping.locateInTo(exon[0], exon[1]));
474 List<int[]> transcriptRange = Arrays.asList(new int[] {
475 transcript.getStart(), transcript.getEnd() });
476 MapList mapList = new MapList(transcriptRange, transcriptLoci, 1, 1);
478 transcript.setGeneLoci(loci.getSpeciesId(), loci.getAssemblyId(),
479 loci.getChromosomeId(), mapList);
483 * Returns the 'transcript_id' property of the sequence feature (or null)
488 protected String getTranscriptId(SequenceFeature feature)
490 return (String) feature.getValue(JSON_ID);
494 * Returns a list of the transcript features on the sequence whose Parent is
495 * the gene for the accession id.
497 * Transcript features are those of type "transcript", or any of its sub-types
498 * in the Sequence Ontology e.g. "mRNA", "processed_transcript". We also
499 * include "NMD_transcript_variant", because this type behaves like a
500 * transcript identifier in Ensembl, although strictly speaking it is not in
504 * @param geneSequence
507 protected List<SequenceFeature> getTranscriptFeatures(String accId,
508 SequenceI geneSequence)
510 List<SequenceFeature> transcriptFeatures = new ArrayList<>();
512 String parentIdentifier = accId;
514 List<SequenceFeature> sfs = geneSequence.getFeatures()
515 .getFeaturesByOntology(SequenceOntologyI.TRANSCRIPT);
516 sfs.addAll(geneSequence.getFeatures().getPositionalFeatures(
517 SequenceOntologyI.NMD_TRANSCRIPT_VARIANT));
519 for (SequenceFeature sf : sfs)
521 String parent = (String) sf.getValue(PARENT);
522 if (parentIdentifier.equalsIgnoreCase(parent))
524 transcriptFeatures.add(sf);
528 return transcriptFeatures;
532 public String getDescription()
534 return "Fetches all transcripts and variant features for a gene or transcript";
538 * Default test query is a gene id (can also enter a transcript id)
541 public String getTestQuery()
543 return "ENSG00000157764"; // BRAF, 5 transcripts, reverse strand
544 // ENSG00000090266 // NDUFB2, 15 transcripts, forward strand
545 // ENSG00000101812 // H2BFM histone, 3 transcripts, forward strand
546 // ENSG00000123569 // H2BFWT histone, 2 transcripts, reverse strand
550 * Answers a list of sequence features (if any) whose type is 'gene' (or a
551 * subtype of gene in the Sequence Ontology), and whose ID is the accession we
555 protected List<SequenceFeature> getIdentifyingFeatures(SequenceI seq,
558 List<SequenceFeature> result = new ArrayList<>();
559 List<SequenceFeature> sfs = seq.getFeatures()
560 .getFeaturesByOntology(SequenceOntologyI.GENE);
561 for (SequenceFeature sf : sfs)
563 String id = (String) sf.getValue(JSON_ID);
564 if (accId.equalsIgnoreCase(id))
573 * Answers true unless feature type is 'gene', or 'transcript' with a parent
574 * which is a different gene. We need the gene features to identify the range,
575 * but it is redundant information on the gene sequence. Checking the parent
576 * allows us to drop transcript features which belong to different
577 * (overlapping) genes.
580 protected boolean retainFeature(SequenceFeature sf, String accessionId)
582 SequenceOntologyI so = SequenceOntologyFactory.getInstance();
583 String type = sf.getType();
584 if (so.isA(type, SequenceOntologyI.GENE))
588 if (isTranscript(type))
590 String parent = (String) sf.getValue(PARENT);
591 if (!accessionId.equalsIgnoreCase(parent))
600 * Override to do nothing as Ensembl doesn't return a protein sequence for a
604 protected void addProteinProduct(SequenceI querySeq)
609 public Regex getAccessionValidator()
611 return ACCESSION_REGEX;
615 * Returns a descriptor for suitable feature display settings with
617 * <li>only exon or sequence_variant features (or their subtypes in the
618 * Sequence Ontology) visible</li>
619 * <li>variant features coloured red</li>
620 * <li>exon features coloured by label (exon name)</li>
621 * <li>variants displayed above (on top of) exons</li>
625 public FeatureSettingsModelI getFeatureColourScheme()
627 return new FeatureSettingsAdapter()
629 SequenceOntologyI so = SequenceOntologyFactory.getInstance();
632 public boolean isFeatureHidden(String type)
634 return (!so.isA(type, SequenceOntologyI.EXON)
635 && !so.isA(type, SequenceOntologyI.SEQUENCE_VARIANT));
639 public FeatureColourI getFeatureColour(String type)
641 if (so.isA(type, SequenceOntologyI.EXON))
643 return new FeatureColour()
646 public boolean isColourByLabel()
652 if (so.isA(type, SequenceOntologyI.SEQUENCE_VARIANT))
654 return new FeatureColour()
658 public Color getColour()
668 * order to render sequence_variant after exon after the rest
671 public int compare(String feature1, String feature2)
673 if (so.isA(feature1, SequenceOntologyI.SEQUENCE_VARIANT))
677 if (so.isA(feature2, SequenceOntologyI.SEQUENCE_VARIANT))
681 if (so.isA(feature1, SequenceOntologyI.EXON))
685 if (so.isA(feature2, SequenceOntologyI.EXON))