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 * Default constructor (to use rest.ensembl.org)
52 * Constructor given the target domain to fetch data from
56 public EnsemblGene(String d)
62 public String getDbName()
68 protected EnsemblFeatureType[] getFeaturesToFetch()
70 return FEATURES_TO_FETCH;
74 protected EnsemblSeqType getSourceEnsemblType()
76 return EnsemblSeqType.GENOMIC;
80 * Returns an alignment containing the gene(s) for the given gene or
81 * transcript identifier, or external identifier (e.g. Uniprot id). If given a
82 * gene name or external identifier, returns any related gene sequences found
83 * for model organisms. If only a single gene is queried for, then its
84 * transcripts are also retrieved and added to the alignment. <br>
87 * <li>resolves a transcript identifier by looking up its parent gene id</li>
88 * <li>resolves an external identifier by looking up xref-ed gene ids</li>
89 * <li>fetches the gene sequence</li>
90 * <li>fetches features on the sequence</li>
91 * <li>identifies "transcript" features whose Parent is the requested gene</li>
92 * <li>fetches the transcript sequence for each transcript</li>
93 * <li>makes a mapping from the gene to each transcript</li>
94 * <li>copies features from gene to transcript sequences</li>
95 * <li>fetches the protein sequence for each transcript, maps and saves it as
96 * a cross-reference</li>
97 * <li>aligns each transcript against the gene sequence based on the position
102 * one or more identifiers separated by a space
103 * @return an alignment containing one or more genes, and possibly
104 * transcripts, or null
107 public AlignmentI getSequenceRecords(String query) throws Exception
109 // todo: tidy up handling of one or multiple accession ids
110 String[] queries = query.split(getAccessionSeparator());
113 * if given a transcript id, look up its gene parent
115 if (isTranscriptIdentifier(query))
117 // we are assuming all transcripts have the same gene parent here
118 query = new EnsemblLookup(getDomain()).getParent(queries[0]);
126 * if given a gene or other external name, lookup and fetch
127 * the corresponding gene for all model organisms
129 if (!isGeneIdentifier(query))
131 List<String> geneIds = new EnsemblSymbol(getDomain()).getIds(query);
132 if (geneIds.isEmpty())
136 String theIds = StringUtils.listToDelimitedString(geneIds,
137 getAccessionSeparator());
138 return getSequenceRecords(theIds);
142 * fetch the gene sequence(s) with features and xrefs
144 AlignmentI al = super.getSequenceRecords(query);
147 * if we retrieved a single gene, get its transcripts as well
149 if (al.getHeight() == 1)
151 getTranscripts(al, query);
158 * Attempts to get Ensembl stable identifiers for model organisms for a gene
159 * name by calling the xrefs symbol REST service to resolve the gene name.
164 protected String getGeneIdentifiersForName(String query)
166 List<String> ids = new EnsemblSymbol(getDomain()).getIds(query);
169 for (String id : ids)
171 if (isGeneIdentifier(id))
181 * Constructs all transcripts for the gene, as identified by "transcript"
182 * features whose Parent is the requested gene. The coding transcript
183 * sequences (i.e. with introns omitted) are added to the alignment.
189 protected void getTranscripts(AlignmentI al, String accId)
192 SequenceI gene = al.getSequenceAt(0);
193 List<SequenceFeature> transcriptFeatures = getTranscriptFeatures(accId,
196 for (SequenceFeature transcriptFeature : transcriptFeatures)
198 makeTranscript(transcriptFeature, al, gene);
201 clearGeneFeatures(gene);
205 * Remove unwanted features (transcript, exon, CDS) from the gene sequence
206 * after we have used them to derive transcripts and transfer features
210 protected void clearGeneFeatures(SequenceI gene)
212 SequenceFeature[] sfs = gene.getSequenceFeatures();
215 SequenceOntologyI so = SequenceOntologyFactory.getInstance();
216 List<SequenceFeature> filtered = new ArrayList<SequenceFeature>();
217 for (SequenceFeature sf : sfs)
219 String type = sf.getType();
220 if (!isTranscript(type) && !so.isA(type, SequenceOntologyI.EXON)
221 && !so.isA(type, SequenceOntologyI.CDS))
226 gene.setSequenceFeatures(filtered
227 .toArray(new SequenceFeature[filtered
233 * Constructs a spliced transcript sequence by finding 'exon' features for the
234 * given id (or failing that 'CDS'). Copies features on to the new sequence.
235 * 'Aligns' the new sequence against the gene sequence by padding with gaps,
236 * and adds it to the alignment.
238 * @param transcriptFeature
240 * the alignment to which to add the new sequence
242 * the parent gene sequence, with features
245 SequenceI makeTranscript(SequenceFeature transcriptFeature,
246 AlignmentI al, SequenceI gene)
248 String accId = getTranscriptId(transcriptFeature);
255 * NB we are mapping from gene sequence (not genome), so do not
256 * need to check for reverse strand (gene and transcript sequences
257 * are in forward sense)
261 * make a gene-length sequence filled with gaps
262 * we will fill in the bases for transcript regions
264 char[] seqChars = new char[gene.getLength()];
265 Arrays.fill(seqChars, al.getGapCharacter());
268 * look for exon features of the transcript, failing that for CDS
269 * (for example ENSG00000124610 has 1 CDS but no exon features)
271 String parentId = "transcript:" + accId;
272 List<SequenceFeature> splices = findFeatures(gene,
273 SequenceOntologyI.EXON, parentId);
274 if (splices.isEmpty())
276 splices = findFeatures(gene, SequenceOntologyI.CDS, parentId);
279 int transcriptLength = 0;
280 final char[] geneChars = gene.getSequence();
281 int offset = gene.getStart(); // to convert to 0-based positions
282 List<int[]> mappedFrom = new ArrayList<int[]>();
284 for (SequenceFeature sf : splices)
286 int start = sf.getBegin() - offset;
287 int end = sf.getEnd() - offset;
288 int spliceLength = end - start + 1;
289 System.arraycopy(geneChars, start, seqChars, start, spliceLength);
290 transcriptLength += spliceLength;
291 mappedFrom.add(new int[] { sf.getBegin(), sf.getEnd() });
294 Sequence transcript = new Sequence(accId, seqChars, 1, transcriptLength);
295 String geneName = (String) transcriptFeature.getValue(NAME);
296 if (geneName != null)
298 transcript.setDescription(geneName);
300 transcript.createDatasetSequence();
302 al.addSequence(transcript);
305 * transfer features to the new sequence; we use EnsemblCdna to do this,
306 * to filter out unwanted features types (see method retainFeature)
308 List<int[]> mapTo = new ArrayList<int[]>();
309 mapTo.add(new int[] { 1, transcriptLength });
310 MapList mapping = new MapList(mappedFrom, mapTo, 1, 1);
311 new EnsemblCdna(getDomain()).transferFeatures(
312 gene.getSequenceFeatures(), transcript.getDatasetSequence(),
316 * fetch and save cross-references
318 super.getCrossReferences(transcript);
321 * and finally fetch the protein product and save as a cross-reference
323 new EnsemblCdna(getDomain()).addProteinProduct(transcript);
329 * Returns the 'transcript_id' property of the sequence feature (or null)
334 protected String getTranscriptId(SequenceFeature feature)
336 return (String) feature.getValue("transcript_id");
340 * Returns a list of the transcript features on the sequence whose Parent is
341 * the gene for the accession id.
344 * @param geneSequence
347 protected List<SequenceFeature> getTranscriptFeatures(String accId,
348 SequenceI geneSequence)
350 List<SequenceFeature> transcriptFeatures = new ArrayList<SequenceFeature>();
352 String parentIdentifier = GENE_PREFIX + accId;
353 SequenceFeature[] sfs = geneSequence.getSequenceFeatures();
357 for (SequenceFeature sf : sfs)
359 if (isTranscript(sf.getType()))
361 String parent = (String) sf.getValue(PARENT);
362 if (parentIdentifier.equals(parent))
364 transcriptFeatures.add(sf);
370 return transcriptFeatures;
374 public String getDescription()
376 return "Fetches all transcripts and variant features for a gene or transcript";
380 * Default test query is a gene id (can also enter a transcript id)
383 public String getTestQuery()
385 return "ENSG00000157764"; // BRAF, 5 transcripts, reverse strand
386 // ENSG00000090266 // NDUFB2, 15 transcripts, forward strand
387 // ENSG00000101812 // H2BFM histone, 3 transcripts, forward strand
388 // ENSG00000123569 // H2BFWT histone, 2 transcripts, reverse strand
392 * Answers true for a feature of type 'gene' (or a sub-type of gene in the
393 * Sequence Ontology), whose ID is the accession we are retrieving
396 protected boolean identifiesSequence(SequenceFeature sf, String accId)
398 if (SequenceOntologyFactory.getInstance().isA(sf.getType(),
399 SequenceOntologyI.GENE))
401 String id = (String) sf.getValue(ID);
402 if ((GENE_PREFIX + accId).equals(id))
411 * Answers true unless feature type is 'gene', or 'transcript' with a parent
412 * which is a different gene. We need the gene features to identify the range,
413 * but it is redundant information on the gene sequence. Checking the parent
414 * allows us to drop transcript features which belong to different
415 * (overlapping) genes.
418 protected boolean retainFeature(SequenceFeature sf, String accessionId)
420 SequenceOntologyI so = SequenceOntologyFactory.getInstance();
421 String type = sf.getType();
422 if (so.isA(type, SequenceOntologyI.GENE))
426 if (isTranscript(type))
428 String parent = (String) sf.getValue(PARENT);
429 if (!(GENE_PREFIX + accessionId).equals(parent))
438 * Answers false. This allows an optimisation - a single 'gene' feature is all
439 * that is needed to identify the positions of the gene on the genomic
443 protected boolean isSpliceable()
449 protected List<String> getCrossReferenceDatabases()
451 // found these for ENSG00000157764 on 30/01/2016:
452 // return new String[] {"Vega_gene", "OTTG", "ENS_LRG_gene", "ArrayExpress",
453 // "EntrezGene", "HGNC", "MIM_GENE", "MIM_MORBID", "WikiGene"};
454 return super.getCrossReferenceDatabases();
458 * Override to do nothing as Ensembl doesn't return a protein sequence for a
462 protected void addProteinProduct(SequenceI querySeq)
467 public Regex getAccessionValidator()
469 return ACCESSION_REGEX;
473 public FeatureSettingsI getFeatureColourScheme()
475 return new FeatureSettingsAdapter()
477 SequenceOntologyI so = SequenceOntologyFactory.getInstance();
479 public boolean isFeatureDisplayed(String type)
481 return (so.isA(type, SequenceOntologyI.EXON) || so.isA(type,
482 SequenceOntologyI.SEQUENCE_VARIANT));
486 public FeatureColourI getFeatureColour(String type)
488 if (so.isA(type, SequenceOntologyI.EXON))
490 return new FeatureColourAdapter()
493 public boolean isColourByLabel()
499 if (so.isA(type, SequenceOntologyI.SEQUENCE_VARIANT))
501 return new FeatureColourAdapter()
505 public Color getColour()
515 * order to render sequence_variant after exon after the rest
518 public int compare(String feature1, String feature2)
520 if (so.isA(feature1, SequenceOntologyI.SEQUENCE_VARIANT))
524 if (so.isA(feature2, SequenceOntologyI.SEQUENCE_VARIANT))
528 if (so.isA(feature1, SequenceOntologyI.EXON))
532 if (so.isA(feature2, SequenceOntologyI.EXON))