1 package jalview.ext.ensembl;
3 import jalview.api.ColorI;
4 import jalview.api.FeatureColourI;
5 import jalview.api.FeatureSettingsModelI;
6 import jalview.datamodel.AlignmentI;
7 import jalview.datamodel.Sequence;
8 import jalview.datamodel.SequenceFeature;
9 import jalview.datamodel.SequenceI;
10 import jalview.io.gff.SequenceOntologyFactory;
11 import jalview.io.gff.SequenceOntologyI;
12 import jalview.schemes.Colour;
13 import jalview.schemes.FeatureColourAdapter;
14 import jalview.schemes.FeatureSettingsAdapter;
15 import jalview.util.MapList;
17 import java.io.UnsupportedEncodingException;
18 import java.net.URLDecoder;
19 import java.util.ArrayList;
20 import java.util.Arrays;
21 import java.util.List;
23 import com.stevesoft.pat.Regex;
26 * A class that fetches genomic sequence and all transcripts for an Ensembl gene
30 public class EnsemblGene extends EnsemblSeqProxy
32 private static final String GENE_PREFIX = "gene:";
35 * accepts anything as we will attempt lookup of gene or
36 * transcript id or gene name
38 private static final Regex ACCESSION_REGEX = new Regex(".*");
40 private static final EnsemblFeatureType[] FEATURES_TO_FETCH = {
41 EnsemblFeatureType.gene, EnsemblFeatureType.transcript,
42 EnsemblFeatureType.exon, EnsemblFeatureType.cds,
43 EnsemblFeatureType.variation };
46 * Default constructor (to use rest.ensembl.org)
54 * Constructor given the target domain to fetch data from
58 public EnsemblGene(String d)
64 public String getDbName()
70 protected EnsemblFeatureType[] getFeaturesToFetch()
72 return FEATURES_TO_FETCH;
76 protected EnsemblSeqType getSourceEnsemblType()
78 return EnsemblSeqType.GENOMIC;
82 * Returns an alignment containing the gene(s) for the given gene or
83 * transcript identifier, or external identifier (e.g. Uniprot id). If given a
84 * gene name or external identifier, returns any related gene sequences found
85 * for model organisms. If only a single gene is queried for, then its
86 * transcripts are also retrieved and added to the alignment. <br>
89 * <li>resolves a transcript identifier by looking up its parent gene id</li>
90 * <li>resolves an external identifier by looking up xref-ed gene ids</li>
91 * <li>fetches the gene sequence</li>
92 * <li>fetches features on the sequence</li>
93 * <li>identifies "transcript" features whose Parent is the requested gene</li>
94 * <li>fetches the transcript sequence for each transcript</li>
95 * <li>makes a mapping from the gene to each transcript</li>
96 * <li>copies features from gene to transcript sequences</li>
97 * <li>fetches the protein sequence for each transcript, maps and saves it as
98 * a cross-reference</li>
99 * <li>aligns each transcript against the gene sequence based on the position
104 * a single gene or transcript identifier or gene name
105 * @return an alignment containing a gene, and possibly transcripts, or null
108 public AlignmentI getSequenceRecords(String query) throws Exception
111 * convert to a non-duplicated list of gene identifiers
113 List<String> geneIds = getGeneIds(query);
115 AlignmentI al = null;
116 for (String geneId : geneIds)
119 * fetch the gene sequence(s) with features and xrefs
121 AlignmentI geneAlignment = super.getSequenceRecords(geneId);
122 if (geneAlignment == null)
126 if (geneAlignment.getHeight() == 1)
128 getTranscripts(geneAlignment, geneId);
136 al.append(geneAlignment);
143 * Converts a query, which may contain one or more gene or transcript
144 * identifiers, into a non-redundant list of gene identifiers.
149 List<String> getGeneIds(String accessions)
151 List<String> geneIds = new ArrayList<String>();
153 for (String acc : accessions.split(getAccessionSeparator()))
155 if (isGeneIdentifier(acc))
157 if (!geneIds.contains(acc))
164 * if given a transcript id, look up its gene parent
166 else if (isTranscriptIdentifier(acc))
168 String geneId = new EnsemblLookup(getDomain()).getParent(acc);
169 if (geneId != null && !geneIds.contains(geneId))
176 * if given a gene or other external name, lookup and fetch
177 * the corresponding gene for all model organisms
181 List<String> ids = new EnsemblSymbol(getDomain(), getDbSource(),
182 getDbVersion()).getIds(acc);
183 for (String geneId : ids)
185 if (!geneIds.contains(geneId))
196 * Attempts to get Ensembl stable identifiers for model organisms for a gene
197 * name by calling the xrefs symbol REST service to resolve the gene name.
202 protected String getGeneIdentifiersForName(String query)
204 List<String> ids = new EnsemblSymbol(getDomain(), getDbSource(),
205 getDbVersion()).getIds(query);
208 for (String id : ids)
210 if (isGeneIdentifier(id))
220 * Constructs all transcripts for the gene, as identified by "transcript"
221 * features whose Parent is the requested gene. The coding transcript
222 * sequences (i.e. with introns omitted) are added to the alignment.
228 protected void getTranscripts(AlignmentI al, String accId)
231 SequenceI gene = al.getSequenceAt(0);
232 List<SequenceFeature> transcriptFeatures = getTranscriptFeatures(accId,
235 for (SequenceFeature transcriptFeature : transcriptFeatures)
237 makeTranscript(transcriptFeature, al, gene);
240 clearGeneFeatures(gene);
244 * Remove unwanted features (transcript, exon, CDS) from the gene sequence
245 * after we have used them to derive transcripts and transfer features
249 protected void clearGeneFeatures(SequenceI gene)
251 SequenceFeature[] sfs = gene.getSequenceFeatures();
254 SequenceOntologyI so = SequenceOntologyFactory.getInstance();
255 List<SequenceFeature> filtered = new ArrayList<SequenceFeature>();
256 for (SequenceFeature sf : sfs)
258 String type = sf.getType();
259 if (!isTranscript(type) && !so.isA(type, SequenceOntologyI.EXON)
260 && !so.isA(type, SequenceOntologyI.CDS))
265 gene.setSequenceFeatures(filtered
266 .toArray(new SequenceFeature[filtered
272 * Constructs a spliced transcript sequence by finding 'exon' features for the
273 * given id (or failing that 'CDS'). Copies features on to the new sequence.
274 * 'Aligns' the new sequence against the gene sequence by padding with gaps,
275 * and adds it to the alignment.
277 * @param transcriptFeature
279 * the alignment to which to add the new sequence
281 * the parent gene sequence, with features
284 SequenceI makeTranscript(SequenceFeature transcriptFeature,
285 AlignmentI al, SequenceI gene)
287 String accId = getTranscriptId(transcriptFeature);
294 * NB we are mapping from gene sequence (not genome), so do not
295 * need to check for reverse strand (gene and transcript sequences
296 * are in forward sense)
300 * make a gene-length sequence filled with gaps
301 * we will fill in the bases for transcript regions
303 char[] seqChars = new char[gene.getLength()];
304 Arrays.fill(seqChars, al.getGapCharacter());
307 * look for exon features of the transcript, failing that for CDS
308 * (for example ENSG00000124610 has 1 CDS but no exon features)
310 String parentId = "transcript:" + accId;
311 List<SequenceFeature> splices = findFeatures(gene,
312 SequenceOntologyI.EXON, parentId);
313 if (splices.isEmpty())
315 splices = findFeatures(gene, SequenceOntologyI.CDS, parentId);
318 int transcriptLength = 0;
319 final char[] geneChars = gene.getSequence();
320 int offset = gene.getStart(); // to convert to 0-based positions
321 List<int[]> mappedFrom = new ArrayList<int[]>();
323 for (SequenceFeature sf : splices)
325 int start = sf.getBegin() - offset;
326 int end = sf.getEnd() - offset;
327 int spliceLength = end - start + 1;
328 System.arraycopy(geneChars, start, seqChars, start, spliceLength);
329 transcriptLength += spliceLength;
330 mappedFrom.add(new int[] { sf.getBegin(), sf.getEnd() });
333 Sequence transcript = new Sequence(accId, seqChars, 1, transcriptLength);
336 * Ensembl has gene name as transcript Name
337 * EnsemblGenomes doesn't, but has a url-encoded description field
339 String description = (String) transcriptFeature.getValue(NAME);
340 if (description == null)
342 description = (String) transcriptFeature.getValue(DESCRIPTION);
344 if (description != null)
348 transcript.setDescription(URLDecoder.decode(description, "UTF-8"));
349 } catch (UnsupportedEncodingException e)
351 e.printStackTrace(); // as if
354 transcript.createDatasetSequence();
356 al.addSequence(transcript);
359 * transfer features to the new sequence; we use EnsemblCdna to do this,
360 * to filter out unwanted features types (see method retainFeature)
362 List<int[]> mapTo = new ArrayList<int[]>();
363 mapTo.add(new int[] { 1, transcriptLength });
364 MapList mapping = new MapList(mappedFrom, mapTo, 1, 1);
365 EnsemblCdna cdna = new EnsemblCdna(getDomain());
366 cdna.transferFeatures(gene.getSequenceFeatures(),
367 transcript.getDatasetSequence(), mapping, parentId);
370 * fetch and save cross-references
372 cdna.getCrossReferences(transcript);
375 * and finally fetch the protein product and save as a cross-reference
377 cdna.addProteinProduct(transcript);
383 * Returns the 'transcript_id' property of the sequence feature (or null)
388 protected String getTranscriptId(SequenceFeature feature)
390 return (String) feature.getValue("transcript_id");
394 * Returns a list of the transcript features on the sequence whose Parent is
395 * the gene for the accession id.
398 * @param geneSequence
401 protected List<SequenceFeature> getTranscriptFeatures(String accId,
402 SequenceI geneSequence)
404 List<SequenceFeature> transcriptFeatures = new ArrayList<SequenceFeature>();
406 String parentIdentifier = GENE_PREFIX + accId;
407 SequenceFeature[] sfs = geneSequence.getSequenceFeatures();
411 for (SequenceFeature sf : sfs)
413 if (isTranscript(sf.getType()))
415 String parent = (String) sf.getValue(PARENT);
416 if (parentIdentifier.equals(parent))
418 transcriptFeatures.add(sf);
424 return transcriptFeatures;
428 public String getDescription()
430 return "Fetches all transcripts and variant features for a gene or transcript";
434 * Default test query is a gene id (can also enter a transcript id)
437 public String getTestQuery()
439 return "ENSG00000157764"; // BRAF, 5 transcripts, reverse strand
440 // ENSG00000090266 // NDUFB2, 15 transcripts, forward strand
441 // ENSG00000101812 // H2BFM histone, 3 transcripts, forward strand
442 // ENSG00000123569 // H2BFWT histone, 2 transcripts, reverse strand
446 * Answers true for a feature of type 'gene' (or a sub-type of gene in the
447 * Sequence Ontology), whose ID is the accession we are retrieving
450 protected boolean identifiesSequence(SequenceFeature sf, String accId)
452 if (SequenceOntologyFactory.getInstance().isA(sf.getType(),
453 SequenceOntologyI.GENE))
455 String id = (String) sf.getValue(ID);
456 if ((GENE_PREFIX + accId).equals(id))
465 * Answers true unless feature type is 'gene', or 'transcript' with a parent
466 * which is a different gene. We need the gene features to identify the range,
467 * but it is redundant information on the gene sequence. Checking the parent
468 * allows us to drop transcript features which belong to different
469 * (overlapping) genes.
472 protected boolean retainFeature(SequenceFeature sf, String accessionId)
474 SequenceOntologyI so = SequenceOntologyFactory.getInstance();
475 String type = sf.getType();
476 if (so.isA(type, SequenceOntologyI.GENE))
480 if (isTranscript(type))
482 String parent = (String) sf.getValue(PARENT);
483 if (!(GENE_PREFIX + accessionId).equals(parent))
492 * Answers false. This allows an optimisation - a single 'gene' feature is all
493 * that is needed to identify the positions of the gene on the genomic
497 protected boolean isSpliceable()
503 * Override to do nothing as Ensembl doesn't return a protein sequence for a
507 protected void addProteinProduct(SequenceI querySeq)
512 public Regex getAccessionValidator()
514 return ACCESSION_REGEX;
518 * Returns a descriptor for suitable feature display settings with
520 * <li>only exon or sequence_variant features (or their subtypes in the
521 * Sequence Ontology) visible</li>
522 * <li>variant features coloured red</li>
523 * <li>exon features coloured by label (exon name)</li>
524 * <li>variants displayed above (on top of) exons</li>
528 public FeatureSettingsModelI getFeatureColourScheme()
530 return new FeatureSettingsAdapter()
532 SequenceOntologyI so = SequenceOntologyFactory.getInstance();
534 public boolean isFeatureDisplayed(String type)
536 return (so.isA(type, SequenceOntologyI.EXON) || so.isA(type,
537 SequenceOntologyI.SEQUENCE_VARIANT));
541 public FeatureColourI getFeatureColour(String type)
543 if (so.isA(type, SequenceOntologyI.EXON))
545 return new FeatureColourAdapter()
548 public boolean isColourByLabel()
554 if (so.isA(type, SequenceOntologyI.SEQUENCE_VARIANT))
556 return new FeatureColourAdapter()
560 public ColorI getColour()
570 * order to render sequence_variant after exon after the rest
573 public int compare(String feature1, String feature2)
575 if (so.isA(feature1, SequenceOntologyI.SEQUENCE_VARIANT))
579 if (so.isA(feature2, SequenceOntologyI.SEQUENCE_VARIANT))
583 if (so.isA(feature1, SequenceOntologyI.EXON))
587 if (so.isA(feature2, SequenceOntologyI.EXON))