1 package jalview.ext.ensembl;
3 import jalview.api.FeatureColourI;
4 import jalview.api.FeatureSettingsI;
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.FeatureColourAdapter;
12 import jalview.schemes.FeatureSettingsAdapter;
13 import jalview.util.MapList;
14 import jalview.util.StringUtils;
16 import java.awt.Color;
17 import java.util.ArrayList;
18 import java.util.Arrays;
19 import java.util.List;
21 import com.stevesoft.pat.Regex;
24 * A class that fetches genomic sequence and all transcripts for an Ensembl gene
28 public class EnsemblGene extends EnsemblSeqProxy
30 private static final String GENE_PREFIX = "gene:";
33 * accepts anything as we will attempt lookup of gene or
34 * transcript id or gene name
36 private static final Regex ACCESSION_REGEX = new Regex(".*");
38 private static final EnsemblFeatureType[] FEATURES_TO_FETCH = {
39 EnsemblFeatureType.gene, EnsemblFeatureType.transcript,
40 EnsemblFeatureType.exon, EnsemblFeatureType.cds,
41 EnsemblFeatureType.variation };
44 public String getDbName()
46 return "ENSEMBL (GENE)";
50 protected EnsemblFeatureType[] getFeaturesToFetch()
52 return FEATURES_TO_FETCH;
56 protected EnsemblSeqType getSourceEnsemblType()
58 return EnsemblSeqType.GENOMIC;
62 * Returns an alignment containing the gene(s) for the given gene or
63 * transcript identifier, or external identifier (e.g. Uniprot id). If given a
64 * gene name or external identifier, returns any related gene sequences found
65 * for model organisms. If only a single gene is queried for, then its
66 * transcripts are also retrieved and added to the alignment. <br>
69 * <li>resolves a transcript identifier by looking up its parent gene id</li>
70 * <li>resolves an external identifier by looking up xref-ed gene ids</li>
71 * <li>fetches the gene sequence</li>
72 * <li>fetches features on the sequence</li>
73 * <li>identifies "transcript" features whose Parent is the requested gene</li>
74 * <li>fetches the transcript sequence for each transcript</li>
75 * <li>makes a mapping from the gene to each transcript</li>
76 * <li>copies features from gene to transcript sequences</li>
77 * <li>fetches the protein sequence for each transcript, maps and saves it as
78 * a cross-reference</li>
79 * <li>aligns each transcript against the gene sequence based on the position
84 * one or more identifiers separated by a space
85 * @return an alignment containing one or more genes, and possibly
86 * transcripts, or null
89 public AlignmentI getSequenceRecords(String query) throws Exception
91 // todo: tidy up handling of one or multiple accession ids
92 String[] queries = query.split(getAccessionSeparator());
95 * if given a transcript id, look up its gene parent
97 if (isTranscriptIdentifier(query))
99 // we are assuming all transcripts have the same gene parent here
100 query = new EnsemblLookup().getParent(queries[0]);
108 * if given a gene or other external name, lookup and fetch
109 * the corresponding gene for all model organisms
111 if (!isGeneIdentifier(query))
113 List<String> geneIds = new EnsemblSymbol().getIds(query);
114 if (geneIds.isEmpty())
118 String theIds = StringUtils.listToDelimitedString(geneIds,
119 getAccessionSeparator());
120 return getSequenceRecords(theIds);
124 * fetch the gene sequence(s) with features and xrefs
126 AlignmentI al = super.getSequenceRecords(query);
129 * if we retrieved a single gene, get its transcripts as well
131 if (al.getHeight() == 1)
133 getTranscripts(al, query);
140 * Attempts to get Ensembl stable identifiers for model organisms for a gene
141 * name by calling the xrefs symbol REST service to resolve the gene name.
146 protected String getGeneIdentifiersForName(String query)
148 List<String> ids = new EnsemblSymbol().getIds(query);
151 for (String id : ids)
153 if (isGeneIdentifier(id))
163 * Constructs all transcripts for the gene, as identified by "transcript"
164 * features whose Parent is the requested gene. The coding transcript
165 * sequences (i.e. with introns omitted) are added to the alignment.
171 protected void getTranscripts(AlignmentI al, String accId)
174 SequenceI gene = al.getSequenceAt(0);
175 List<SequenceFeature> transcriptFeatures = getTranscriptFeatures(accId,
178 for (SequenceFeature transcriptFeature : transcriptFeatures)
180 makeTranscript(transcriptFeature, al, gene);
183 clearGeneFeatures(gene);
187 * Remove unwanted features (transcript, exon, CDS) from the gene sequence
188 * after we have used them to derive transcripts and transfer features
192 protected void clearGeneFeatures(SequenceI gene)
194 SequenceFeature[] sfs = gene.getSequenceFeatures();
197 SequenceOntologyI so = SequenceOntologyFactory.getInstance();
198 List<SequenceFeature> filtered = new ArrayList<SequenceFeature>();
199 for (SequenceFeature sf : sfs)
201 String type = sf.getType();
202 if (!isTranscript(type) && !so.isA(type, SequenceOntologyI.EXON)
203 && !so.isA(type, SequenceOntologyI.CDS))
208 gene.setSequenceFeatures(filtered
209 .toArray(new SequenceFeature[filtered
215 * Constructs a spliced transcript sequence by finding 'exon' features for the
216 * given id (or failing that 'CDS'). Copies features on to the new sequence.
217 * 'Aligns' the new sequence against the gene sequence by padding with gaps,
218 * and adds it to the alignment.
220 * @param transcriptFeature
222 * the alignment to which to add the new sequence
224 * the parent gene sequence, with features
227 SequenceI makeTranscript(SequenceFeature transcriptFeature,
228 AlignmentI al, SequenceI gene)
230 String accId = getTranscriptId(transcriptFeature);
237 * NB we are mapping from gene sequence (not genome), so do not
238 * need to check for reverse strand (gene and transcript sequences
239 * are in forward sense)
243 * make a gene-length sequence filled with gaps
244 * we will fill in the bases for transcript regions
246 char[] seqChars = new char[gene.getLength()];
247 Arrays.fill(seqChars, al.getGapCharacter());
250 * look for exon features of the transcript, failing that for CDS
251 * (for example ENSG00000124610 has 1 CDS but no exon features)
253 String parentId = "transcript:" + accId;
254 List<SequenceFeature> splices = findFeatures(gene,
255 SequenceOntologyI.EXON, parentId);
256 if (splices.isEmpty())
258 splices = findFeatures(gene, SequenceOntologyI.CDS, parentId);
261 int transcriptLength = 0;
262 final char[] geneChars = gene.getSequence();
263 int offset = gene.getStart(); // to convert to 0-based positions
264 List<int[]> mappedFrom = new ArrayList<int[]>();
266 for (SequenceFeature sf : splices)
268 int start = sf.getBegin() - offset;
269 int end = sf.getEnd() - offset;
270 int spliceLength = end - start + 1;
271 System.arraycopy(geneChars, start, seqChars, start, spliceLength);
272 transcriptLength += spliceLength;
273 mappedFrom.add(new int[] { sf.getBegin(), sf.getEnd() });
276 Sequence transcript = new Sequence(accId, seqChars, 1, transcriptLength);
277 String geneName = (String) transcriptFeature.getValue(NAME);
278 if (geneName != null)
280 transcript.setDescription(geneName);
282 transcript.createDatasetSequence();
284 al.addSequence(transcript);
287 * transfer features to the new sequence; we use EnsemblCdna to do this,
288 * to filter out unwanted features types (see method retainFeature)
290 List<int[]> mapTo = new ArrayList<int[]>();
291 mapTo.add(new int[] { 1, transcriptLength });
292 MapList mapping = new MapList(mappedFrom, mapTo, 1, 1);
293 new EnsemblCdna().transferFeatures(gene.getSequenceFeatures(),
294 transcript.getDatasetSequence(), mapping, parentId);
297 * fetch and save cross-references
299 super.getCrossReferences(transcript);
302 * and finally fetch the protein product and save as a cross-reference
304 new EnsemblCdna().addProteinProduct(transcript);
310 * Returns the 'transcript_id' property of the sequence feature (or null)
315 protected String getTranscriptId(SequenceFeature feature)
317 return (String) feature.getValue("transcript_id");
321 * Returns a list of the transcript features on the sequence whose Parent is
322 * the gene for the accession id.
325 * @param geneSequence
328 protected List<SequenceFeature> getTranscriptFeatures(String accId,
329 SequenceI geneSequence)
331 List<SequenceFeature> transcriptFeatures = new ArrayList<SequenceFeature>();
333 String parentIdentifier = GENE_PREFIX + accId;
334 SequenceFeature[] sfs = geneSequence.getSequenceFeatures();
338 for (SequenceFeature sf : sfs)
340 if (isTranscript(sf.getType()))
342 String parent = (String) sf.getValue(PARENT);
343 if (parentIdentifier.equals(parent))
345 transcriptFeatures.add(sf);
351 return transcriptFeatures;
355 public String getDescription()
357 return "Fetches all transcripts and variant features for a gene or transcript";
361 * Default test query is a gene id (can also enter a transcript id)
364 public String getTestQuery()
366 return "ENSG00000157764"; // BRAF, 5 transcripts, reverse strand
367 // ENSG00000090266 // NDUFB2, 15 transcripts, forward strand
368 // ENSG00000101812 // H2BFM histone, 3 transcripts, forward strand
369 // ENSG00000123569 // H2BFWT histone, 2 transcripts, reverse strand
373 * Answers true for a feature of type 'gene' (or a sub-type of gene in the
374 * Sequence Ontology), whose ID is the accession we are retrieving
377 protected boolean identifiesSequence(SequenceFeature sf, String accId)
379 if (SequenceOntologyFactory.getInstance().isA(sf.getType(),
380 SequenceOntologyI.GENE))
382 String id = (String) sf.getValue(ID);
383 if ((GENE_PREFIX + accId).equals(id))
392 * Answers true unless feature type is 'gene', or 'transcript' with a parent
393 * which is a different gene. We need the gene features to identify the range,
394 * but it is redundant information on the gene sequence. Checking the parent
395 * allows us to drop transcript features which belong to different
396 * (overlapping) genes.
399 protected boolean retainFeature(SequenceFeature sf, String accessionId)
401 SequenceOntologyI so = SequenceOntologyFactory.getInstance();
402 String type = sf.getType();
403 if (so.isA(type, SequenceOntologyI.GENE))
407 if (isTranscript(type))
409 String parent = (String) sf.getValue(PARENT);
410 if (!(GENE_PREFIX + accessionId).equals(parent))
419 * Answers false. This allows an optimisation - a single 'gene' feature is all
420 * that is needed to identify the positions of the gene on the genomic
424 protected boolean isSpliceable()
430 protected List<String> getCrossReferenceDatabases()
432 // found these for ENSG00000157764 on 30/01/2016:
433 // return new String[] {"Vega_gene", "OTTG", "ENS_LRG_gene", "ArrayExpress",
434 // "EntrezGene", "HGNC", "MIM_GENE", "MIM_MORBID", "WikiGene"};
435 return super.getCrossReferenceDatabases();
439 * Override to do nothing as Ensembl doesn't return a protein sequence for a
443 protected void addProteinProduct(SequenceI querySeq)
448 public Regex getAccessionValidator()
450 return ACCESSION_REGEX;
454 public FeatureSettingsI getFeatureColourScheme()
456 return new FeatureSettingsAdapter()
458 SequenceOntologyI so = SequenceOntologyFactory.getInstance();
460 public boolean isFeatureDisplayed(String type)
462 return (so.isA(type, SequenceOntologyI.EXON) || so.isA(type,
463 SequenceOntologyI.SEQUENCE_VARIANT));
467 public FeatureColourI getFeatureColour(String type)
469 if (so.isA(type, SequenceOntologyI.EXON))
471 return new FeatureColourAdapter()
474 public boolean isColourByLabel()
480 if (so.isA(type, SequenceOntologyI.SEQUENCE_VARIANT))
482 return new FeatureColourAdapter()
486 public Color getColour()
496 * order to render sequence_variant after exon after the rest
499 public int compare(String feature1, String feature2)
501 if (so.isA(feature1, SequenceOntologyI.SEQUENCE_VARIANT))
505 if (so.isA(feature2, SequenceOntologyI.SEQUENCE_VARIANT))
509 if (so.isA(feature1, SequenceOntologyI.EXON))
513 if (so.isA(feature2, SequenceOntologyI.EXON))