1 package jalview.ext.ensembl;
3 import jalview.api.FeatureSettingsI;
4 import jalview.datamodel.AlignmentI;
5 import jalview.datamodel.Sequence;
6 import jalview.datamodel.SequenceFeature;
7 import jalview.datamodel.SequenceI;
8 import jalview.io.gff.SequenceOntologyFactory;
9 import jalview.io.gff.SequenceOntologyI;
10 import jalview.schemes.FeatureColourScheme;
11 import jalview.util.MapList;
12 import jalview.util.StringUtils;
14 import java.util.ArrayList;
15 import java.util.Arrays;
16 import java.util.List;
18 import com.stevesoft.pat.Regex;
21 * A class that fetches genomic sequence and all transcripts for an Ensembl gene
25 public class EnsemblGene extends EnsemblSeqProxy
27 private static final String GENE_PREFIX = "gene:";
30 * accepts anything as we will attempt lookup of gene or
31 * transcript id or gene name
33 private static final Regex ACCESSION_REGEX = new Regex(".*");
35 private static final EnsemblFeatureType[] FEATURES_TO_FETCH = {
36 EnsemblFeatureType.gene, EnsemblFeatureType.transcript,
37 EnsemblFeatureType.exon, EnsemblFeatureType.cds,
38 EnsemblFeatureType.variation };
41 public String getDbName()
43 return "ENSEMBL (GENE)";
47 protected EnsemblFeatureType[] getFeaturesToFetch()
49 return FEATURES_TO_FETCH;
53 protected EnsemblSeqType getSourceEnsemblType()
55 return EnsemblSeqType.GENOMIC;
59 * Returns an alignment containing the gene(s) for the given gene or
60 * transcript identifier, or external identifier (e.g. Uniprot id). If given a
61 * gene name or external identifier, returns any related gene sequences found
62 * for model organisms. If only a single gene is queried for, then its
63 * transcripts are also retrieved and added to the alignment. <br>
66 * <li>resolves a transcript identifier by looking up its parent gene id</li>
67 * <li>resolves an external identifier by looking up xref-ed gene ids</li>
68 * <li>fetches the gene sequence</li>
69 * <li>fetches features on the sequence</li>
70 * <li>identifies "transcript" features whose Parent is the requested gene</li>
71 * <li>fetches the transcript sequence for each transcript</li>
72 * <li>makes a mapping from the gene to each transcript</li>
73 * <li>copies features from gene to transcript sequences</li>
74 * <li>fetches the protein sequence for each transcript, maps and saves it as
75 * a cross-reference</li>
76 * <li>aligns each transcript against the gene sequence based on the position
81 * one or more identifiers separated by a space
82 * @return an alignment containing one or more genes, and possibly
83 * transcripts, or null
86 public AlignmentI getSequenceRecords(String query) throws Exception
88 // todo: tidy up handling of one or multiple accession ids
89 String[] queries = query.split(getAccessionSeparator());
92 * if given a transcript id, look up its gene parent
94 if (isTranscriptIdentifier(query))
96 // we are assuming all transcripts have the same gene parent here
97 query = new EnsemblLookup().getParent(queries[0]);
105 * if given a gene or other external name, lookup and fetch
106 * the corresponding gene for all model organisms
108 if (!isGeneIdentifier(query))
110 List<String> geneIds = new EnsemblSymbol().getIds(query);
111 if (geneIds.isEmpty())
115 String theIds = StringUtils.listToDelimitedString(geneIds,
116 getAccessionSeparator());
117 return getSequenceRecords(theIds);
120 AlignmentI al = super.getSequenceRecords(query);
123 * if we retrieved a single gene, get its transcripts as well
125 if (al.getHeight() == 1)
127 getTranscripts(al, query);
134 * Attempts to get Ensembl stable identifiers for model organisms for a gene
135 * name by calling the xrefs symbol REST service to resolve the gene name.
140 protected String getGeneIdentifiersForName(String query)
142 List<String> ids = new EnsemblSymbol().getIds(query);
145 for (String id : ids)
147 if (isGeneIdentifier(id))
157 * Constructs all transcripts for the gene, as identified by "transcript"
158 * features whose Parent is the requested gene. The coding transcript
159 * sequences (i.e. with introns omitted) are added to the alignment.
165 protected void getTranscripts(AlignmentI al, String accId)
168 SequenceI gene = al.getSequenceAt(0);
169 List<SequenceFeature> transcriptFeatures = getTranscriptFeatures(accId,
172 for (SequenceFeature transcriptFeature : transcriptFeatures)
174 makeTranscript(transcriptFeature, al, gene);
179 * Constructs a spliced transcript sequence by finding 'exon' features for the
180 * given id (or failing that 'CDS'). Copies features on to the new sequence.
181 * 'Aligns' the new sequence against the gene sequence by padding with gaps,
182 * and adds it to the alignment.
184 * @param transcriptFeature
186 * the alignment to which to add the new sequence
188 * the parent gene sequence, with features
191 SequenceI makeTranscript(SequenceFeature transcriptFeature,
192 AlignmentI al, SequenceI gene)
194 String accId = getTranscriptId(transcriptFeature);
201 * NB we are mapping from gene sequence (not genome), so do not
202 * need to check for reverse strand (gene and transcript sequences
203 * are in forward sense)
207 * make a gene-length sequence filled with gaps
208 * we will fill in the bases for transcript regions
210 char[] seqChars = new char[gene.getLength()];
211 Arrays.fill(seqChars, al.getGapCharacter());
214 * look for exon features of the transcript, failing that for CDS
215 * (for example ENSG00000124610 has 1 CDS but no exon features)
217 String parentId = "transcript:" + accId;
218 List<SequenceFeature> splices = findFeatures(gene,
219 SequenceOntologyI.EXON, parentId);
220 if (splices.isEmpty())
222 splices = findFeatures(gene, SequenceOntologyI.CDS, parentId);
225 int transcriptLength = 0;
226 final char[] geneChars = gene.getSequence();
227 int offset = gene.getStart(); // to convert to 0-based positions
228 List<int[]> mappedFrom = new ArrayList<int[]>();
230 for (SequenceFeature sf : splices)
232 int start = sf.getBegin() - offset;
233 int end = sf.getEnd() - offset;
234 int spliceLength = end - start + 1;
235 System.arraycopy(geneChars, start, seqChars, start, spliceLength);
236 transcriptLength += spliceLength;
237 mappedFrom.add(new int[] { sf.getBegin(), sf.getEnd() });
240 Sequence transcript = new Sequence(accId, seqChars, 1, transcriptLength);
241 String geneName = (String) transcriptFeature.getValue(NAME);
242 if (geneName != null)
244 transcript.setDescription(geneName);
246 transcript.createDatasetSequence();
248 al.addSequence(transcript);
251 * transfer features to the new sequence; we use EnsemblCdna to do this,
252 * to filter out unwanted features types (see method retainFeature)
254 List<int[]> mapTo = new ArrayList<int[]>();
255 mapTo.add(new int[] { 1, transcriptLength });
256 MapList mapping = new MapList(mappedFrom, mapTo, 1, 1);
257 new EnsemblCdna().transferFeatures(gene.getSequenceFeatures(),
258 transcript.getDatasetSequence(), mapping, parentId);
261 * fetch and save cross-references
263 super.getCrossReferences(transcript);
266 * and finally fetch the protein product and save as a cross-reference
268 new EnsemblCdna().addProteinProduct(transcript);
274 * Returns the 'transcript_id' property of the sequence feature (or null)
279 protected String getTranscriptId(SequenceFeature feature)
281 return (String) feature.getValue("transcript_id");
285 * Returns a list of the transcript features on the sequence whose Parent is
286 * the gene for the accession id.
289 * @param geneSequence
292 protected List<SequenceFeature> getTranscriptFeatures(String accId,
293 SequenceI geneSequence)
295 List<SequenceFeature> transcriptFeatures = new ArrayList<SequenceFeature>();
297 String parentIdentifier = GENE_PREFIX + accId;
298 SequenceFeature[] sfs = geneSequence.getSequenceFeatures();
302 for (SequenceFeature sf : sfs)
304 if (isTranscript(sf.getType()))
306 String parent = (String) sf.getValue(PARENT);
307 if (parentIdentifier.equals(parent))
309 transcriptFeatures.add(sf);
315 return transcriptFeatures;
319 public String getDescription()
321 return "Fetches all transcripts and variant features for a gene or transcript";
325 * Default test query is a gene id (can also enter a transcript id)
328 public String getTestQuery()
330 return "ENSG00000157764"; // BRAF, 5 transcripts, reverse strand
331 // ENSG00000090266 // NDUFB2, 15 transcripts, forward strand
332 // ENSG00000101812 // H2BFM histone, 3 transcripts, forward strand
333 // ENSG00000123569 // H2BFWT histone, 2 transcripts, reverse strand
337 * Answers true for a feature of type 'gene' (or a sub-type of gene in the
338 * Sequence Ontology), whose ID is the accession we are retrieving
341 protected boolean identifiesSequence(SequenceFeature sf, String accId)
343 if (SequenceOntologyFactory.getInstance().isA(sf.getType(),
344 SequenceOntologyI.GENE))
346 String id = (String) sf.getValue(ID);
347 if ((GENE_PREFIX + accId).equals(id))
356 * Answers true unless feature type is 'gene', or 'transcript' with a parent
357 * which is a different gene. We need the gene features to identify the range,
358 * but it is redundant information on the gene sequence. Checking the parent
359 * allows us to drop transcript features which belong to different
360 * (overlapping) genes.
363 protected boolean retainFeature(SequenceFeature sf, String accessionId)
365 if (SequenceOntologyFactory.getInstance().isA(sf.getType(),
366 SequenceOntologyI.GENE))
371 if (isTranscript(sf.getType()))
373 String parent = (String) sf.getValue(PARENT);
374 if (!(GENE_PREFIX + accessionId).equals(parent))
383 * Answers false. This allows an optimisation - a single 'gene' feature is all
384 * that is needed to identify the positions of the gene on the genomic
388 protected boolean isSpliceable()
394 protected List<String> getCrossReferenceDatabases()
396 // found these for ENSG00000157764 on 30/01/2016:
397 // return new String[] {"Vega_gene", "OTTG", "ENS_LRG_gene", "ArrayExpress",
398 // "EntrezGene", "HGNC", "MIM_GENE", "MIM_MORBID", "WikiGene"};
399 return super.getCrossReferenceDatabases();
403 * Override to do nothing as Ensembl doesn't return a protein sequence for a
407 protected void addProteinProduct(SequenceI querySeq)
412 public Regex getAccessionValidator()
414 return ACCESSION_REGEX;
418 public FeatureSettingsI getFeatureColourScheme()
420 return FeatureColourScheme.EnsemblVariants;