1 package jalview.ext.ensembl;
3 import jalview.api.FeatureColourI;
4 import jalview.api.FeatureSettingsModelI;
5 import jalview.datamodel.AlignmentI;
6 import jalview.datamodel.Sequence;
7 import jalview.datamodel.SequenceFeature;
8 import jalview.datamodel.SequenceI;
9 import jalview.io.gff.SequenceOntologyFactory;
10 import jalview.io.gff.SequenceOntologyI;
11 import jalview.schemes.FeatureColour;
12 import jalview.schemes.FeatureSettingsAdapter;
13 import jalview.util.MapList;
15 import java.awt.Color;
16 import java.io.UnsupportedEncodingException;
17 import java.net.URLDecoder;
18 import java.util.ArrayList;
19 import java.util.Arrays;
20 import java.util.List;
22 import com.stevesoft.pat.Regex;
25 * A class that fetches genomic sequence and all transcripts for an Ensembl gene
29 public class EnsemblGene extends EnsemblSeqProxy
31 private static final String GENE_PREFIX = "gene:";
34 * accepts anything as we will attempt lookup of gene or
35 * transcript id or gene name
37 private static final Regex ACCESSION_REGEX = new Regex(".*");
39 private static final EnsemblFeatureType[] FEATURES_TO_FETCH = {
40 EnsemblFeatureType.gene, EnsemblFeatureType.transcript,
41 EnsemblFeatureType.exon, EnsemblFeatureType.cds,
42 EnsemblFeatureType.variation };
45 * Default constructor (to use rest.ensembl.org)
53 * Constructor given the target domain to fetch data from
57 public EnsemblGene(String d)
63 public String getDbName()
69 protected EnsemblFeatureType[] getFeaturesToFetch()
71 return FEATURES_TO_FETCH;
75 protected EnsemblSeqType getSourceEnsemblType()
77 return EnsemblSeqType.GENOMIC;
81 * Returns an alignment containing the gene(s) for the given gene or
82 * transcript identifier, or external identifier (e.g. Uniprot id). If given a
83 * gene name or external identifier, returns any related gene sequences found
84 * for model organisms. If only a single gene is queried for, then its
85 * transcripts are also retrieved and added to the alignment. <br>
88 * <li>resolves a transcript identifier by looking up its parent gene id</li>
89 * <li>resolves an external identifier by looking up xref-ed gene ids</li>
90 * <li>fetches the gene sequence</li>
91 * <li>fetches features on the sequence</li>
92 * <li>identifies "transcript" features whose Parent is the requested gene</li>
93 * <li>fetches the transcript sequence for each transcript</li>
94 * <li>makes a mapping from the gene to each transcript</li>
95 * <li>copies features from gene to transcript sequences</li>
96 * <li>fetches the protein sequence for each transcript, maps and saves it as
97 * a cross-reference</li>
98 * <li>aligns each transcript against the gene sequence based on the position
103 * a single gene or transcript identifier or gene name
104 * @return an alignment containing a gene, and possibly transcripts, or null
107 public AlignmentI getSequenceRecords(String query) throws Exception
110 * convert to a non-duplicated list of gene identifiers
112 List<String> geneIds = getGeneIds(query);
114 AlignmentI al = null;
115 for (String geneId : geneIds)
118 * fetch the gene sequence(s) with features and xrefs
120 AlignmentI geneAlignment = super.getSequenceRecords(geneId);
121 if (geneAlignment == null)
125 if (geneAlignment.getHeight() == 1)
127 getTranscripts(geneAlignment, geneId);
135 al.append(geneAlignment);
142 * Converts a query, which may contain one or more gene or transcript
143 * identifiers, into a non-redundant list of gene identifiers.
148 List<String> getGeneIds(String accessions)
150 List<String> geneIds = new ArrayList<String>();
152 for (String acc : accessions.split(getAccessionSeparator()))
154 if (isGeneIdentifier(acc))
156 if (!geneIds.contains(acc))
163 * if given a transcript id, look up its gene parent
165 else if (isTranscriptIdentifier(acc))
167 String geneId = new EnsemblLookup(getDomain()).getParent(acc);
168 if (geneId != null && !geneIds.contains(geneId))
175 * if given a gene or other external name, lookup and fetch
176 * the corresponding gene for all model organisms
180 List<String> ids = new EnsemblSymbol(getDomain(), getDbSource(),
181 getDbVersion()).getIds(acc);
182 for (String geneId : ids)
184 if (!geneIds.contains(geneId))
195 * Attempts to get Ensembl stable identifiers for model organisms for a gene
196 * name by calling the xrefs symbol REST service to resolve the gene name.
201 protected String getGeneIdentifiersForName(String query)
203 List<String> ids = new EnsemblSymbol(getDomain(), getDbSource(),
204 getDbVersion()).getIds(query);
207 for (String id : ids)
209 if (isGeneIdentifier(id))
219 * Constructs all transcripts for the gene, as identified by "transcript"
220 * features whose Parent is the requested gene. The coding transcript
221 * sequences (i.e. with introns omitted) are added to the alignment.
227 protected void getTranscripts(AlignmentI al, String accId)
230 SequenceI gene = al.getSequenceAt(0);
231 List<SequenceFeature> transcriptFeatures = getTranscriptFeatures(accId,
234 for (SequenceFeature transcriptFeature : transcriptFeatures)
236 makeTranscript(transcriptFeature, al, gene);
239 clearGeneFeatures(gene);
243 * Remove unwanted features (transcript, exon, CDS) from the gene sequence
244 * after we have used them to derive transcripts and transfer features
248 protected void clearGeneFeatures(SequenceI gene)
250 SequenceFeature[] sfs = gene.getSequenceFeatures();
253 SequenceOntologyI so = SequenceOntologyFactory.getInstance();
254 List<SequenceFeature> filtered = new ArrayList<SequenceFeature>();
255 for (SequenceFeature sf : sfs)
257 String type = sf.getType();
258 if (!isTranscript(type) && !so.isA(type, SequenceOntologyI.EXON)
259 && !so.isA(type, SequenceOntologyI.CDS))
264 gene.setSequenceFeatures(filtered
265 .toArray(new SequenceFeature[filtered
271 * Constructs a spliced transcript sequence by finding 'exon' features for the
272 * given id (or failing that 'CDS'). Copies features on to the new sequence.
273 * 'Aligns' the new sequence against the gene sequence by padding with gaps,
274 * and adds it to the alignment.
276 * @param transcriptFeature
278 * the alignment to which to add the new sequence
280 * the parent gene sequence, with features
283 SequenceI makeTranscript(SequenceFeature transcriptFeature,
284 AlignmentI al, SequenceI gene)
286 String accId = getTranscriptId(transcriptFeature);
293 * NB we are mapping from gene sequence (not genome), so do not
294 * need to check for reverse strand (gene and transcript sequences
295 * are in forward sense)
299 * make a gene-length sequence filled with gaps
300 * we will fill in the bases for transcript regions
302 char[] seqChars = new char[gene.getLength()];
303 Arrays.fill(seqChars, al.getGapCharacter());
306 * look for exon features of the transcript, failing that for CDS
307 * (for example ENSG00000124610 has 1 CDS but no exon features)
309 String parentId = "transcript:" + accId;
310 List<SequenceFeature> splices = findFeatures(gene,
311 SequenceOntologyI.EXON, parentId);
312 if (splices.isEmpty())
314 splices = findFeatures(gene, SequenceOntologyI.CDS, parentId);
317 int transcriptLength = 0;
318 final char[] geneChars = gene.getSequence();
319 int offset = gene.getStart(); // to convert to 0-based positions
320 List<int[]> mappedFrom = new ArrayList<int[]>();
322 for (SequenceFeature sf : splices)
324 int start = sf.getBegin() - offset;
325 int end = sf.getEnd() - offset;
326 int spliceLength = end - start + 1;
327 System.arraycopy(geneChars, start, seqChars, start, spliceLength);
328 transcriptLength += spliceLength;
329 mappedFrom.add(new int[] { sf.getBegin(), sf.getEnd() });
332 Sequence transcript = new Sequence(accId, seqChars, 1, transcriptLength);
335 * Ensembl has gene name as transcript Name
336 * EnsemblGenomes doesn't, but has a url-encoded description field
338 String description = (String) transcriptFeature.getValue(NAME);
339 if (description == null)
341 description = (String) transcriptFeature.getValue(DESCRIPTION);
343 if (description != null)
347 transcript.setDescription(URLDecoder.decode(description, "UTF-8"));
348 } catch (UnsupportedEncodingException e)
350 e.printStackTrace(); // as if
353 transcript.createDatasetSequence();
355 al.addSequence(transcript);
358 * transfer features to the new sequence; we use EnsemblCdna to do this,
359 * to filter out unwanted features types (see method retainFeature)
361 List<int[]> mapTo = new ArrayList<int[]>();
362 mapTo.add(new int[] { 1, transcriptLength });
363 MapList mapping = new MapList(mappedFrom, mapTo, 1, 1);
364 EnsemblCdna cdna = new EnsemblCdna(getDomain());
365 cdna.transferFeatures(gene.getSequenceFeatures(),
366 transcript.getDatasetSequence(), mapping, parentId);
369 * fetch and save cross-references
371 cdna.getCrossReferences(transcript);
374 * and finally fetch the protein product and save as a cross-reference
376 cdna.addProteinProduct(transcript);
382 * Returns the 'transcript_id' property of the sequence feature (or null)
387 protected String getTranscriptId(SequenceFeature feature)
389 return (String) feature.getValue("transcript_id");
393 * Returns a list of the transcript features on the sequence whose Parent is
394 * the gene for the accession id.
397 * @param geneSequence
400 protected List<SequenceFeature> getTranscriptFeatures(String accId,
401 SequenceI geneSequence)
403 List<SequenceFeature> transcriptFeatures = new ArrayList<SequenceFeature>();
405 String parentIdentifier = GENE_PREFIX + accId;
406 SequenceFeature[] sfs = geneSequence.getSequenceFeatures();
410 for (SequenceFeature sf : sfs)
412 if (isTranscript(sf.getType()))
414 String parent = (String) sf.getValue(PARENT);
415 if (parentIdentifier.equals(parent))
417 transcriptFeatures.add(sf);
423 return transcriptFeatures;
427 public String getDescription()
429 return "Fetches all transcripts and variant features for a gene or transcript";
433 * Default test query is a gene id (can also enter a transcript id)
436 public String getTestQuery()
438 return "ENSG00000157764"; // BRAF, 5 transcripts, reverse strand
439 // ENSG00000090266 // NDUFB2, 15 transcripts, forward strand
440 // ENSG00000101812 // H2BFM histone, 3 transcripts, forward strand
441 // ENSG00000123569 // H2BFWT histone, 2 transcripts, reverse strand
445 * Answers true for a feature of type 'gene' (or a sub-type of gene in the
446 * Sequence Ontology), whose ID is the accession we are retrieving
449 protected boolean identifiesSequence(SequenceFeature sf, String accId)
451 if (SequenceOntologyFactory.getInstance().isA(sf.getType(),
452 SequenceOntologyI.GENE))
454 String id = (String) sf.getValue(ID);
455 if ((GENE_PREFIX + accId).equals(id))
464 * Answers true unless feature type is 'gene', or 'transcript' with a parent
465 * which is a different gene. We need the gene features to identify the range,
466 * but it is redundant information on the gene sequence. Checking the parent
467 * allows us to drop transcript features which belong to different
468 * (overlapping) genes.
471 protected boolean retainFeature(SequenceFeature sf, String accessionId)
473 SequenceOntologyI so = SequenceOntologyFactory.getInstance();
474 String type = sf.getType();
475 if (so.isA(type, SequenceOntologyI.GENE))
479 if (isTranscript(type))
481 String parent = (String) sf.getValue(PARENT);
482 if (!(GENE_PREFIX + accessionId).equals(parent))
491 * Answers false. This allows an optimisation - a single 'gene' feature is all
492 * that is needed to identify the positions of the gene on the genomic
496 protected boolean isSpliceable()
502 * Override to do nothing as Ensembl doesn't return a protein sequence for a
506 protected void addProteinProduct(SequenceI querySeq)
511 public Regex getAccessionValidator()
513 return ACCESSION_REGEX;
517 * Returns a descriptor for suitable feature display settings with
519 * <li>only exon or sequence_variant features (or their subtypes in the
520 * Sequence Ontology) visible</li>
521 * <li>variant features coloured red</li>
522 * <li>exon features coloured by label (exon name)</li>
523 * <li>variants displayed above (on top of) exons</li>
527 public FeatureSettingsModelI getFeatureColourScheme()
529 return new FeatureSettingsAdapter()
531 SequenceOntologyI so = SequenceOntologyFactory.getInstance();
533 public boolean isFeatureDisplayed(String type)
535 return (so.isA(type, SequenceOntologyI.EXON) || so.isA(type,
536 SequenceOntologyI.SEQUENCE_VARIANT));
540 public FeatureColourI getFeatureColour(String type)
542 if (so.isA(type, SequenceOntologyI.EXON))
544 return new FeatureColour()
547 public boolean isColourByLabel()
553 if (so.isA(type, SequenceOntologyI.SEQUENCE_VARIANT))
555 return new FeatureColour()
559 public Color getColour()
569 * order to render sequence_variant after exon after the rest
572 public int compare(String feature1, String feature2)
574 if (so.isA(feature1, SequenceOntologyI.SEQUENCE_VARIANT))
578 if (so.isA(feature2, SequenceOntologyI.SEQUENCE_VARIANT))
582 if (so.isA(feature1, SequenceOntologyI.EXON))
586 if (so.isA(feature2, SequenceOntologyI.EXON))