1 package jalview.ext.ensembl;
3 import jalview.datamodel.AlignmentI;
4 import jalview.datamodel.Sequence;
5 import jalview.datamodel.SequenceFeature;
6 import jalview.datamodel.SequenceI;
7 import jalview.io.gff.SequenceOntologyFactory;
8 import jalview.io.gff.SequenceOntologyI;
9 import jalview.util.MapList;
11 import java.io.IOException;
12 import java.util.ArrayList;
13 import java.util.Arrays;
14 import java.util.List;
16 import com.stevesoft.pat.Regex;
19 * A class that fetches genomic sequence and all transcripts for an Ensembl gene
23 public class EnsemblGene extends EnsemblSeqProxy
25 private static final String GENE_PREFIX = "gene:";
27 // TODO modify to accept other species e.g. ENSMUSGnnn
28 private static final Regex ACCESSION_REGEX = new Regex(
29 "(ENSG|ENST)[0-9]{11}$");
31 private static final EnsemblFeatureType[] FEATURES_TO_FETCH = {
32 EnsemblFeatureType.gene, EnsemblFeatureType.transcript,
33 EnsemblFeatureType.exon, EnsemblFeatureType.cds,
34 EnsemblFeatureType.variation };
37 public String getDbName()
39 return "ENSEMBL (GENE)";
43 protected EnsemblFeatureType[] getFeaturesToFetch()
45 return FEATURES_TO_FETCH;
49 protected EnsemblSeqType getSourceEnsemblType()
51 return EnsemblSeqType.GENOMIC;
55 * Builds an alignment of all transcripts for the requested gene:
57 * <li>fetches the gene sequence</li>
58 * <li>fetches features on the sequence</li>
59 * <li>identifies "transcript" features whose Parent is the requested gene</li>
60 * <li>fetches the transcript sequence for each transcript</li>
61 * <li>makes a mapping from the gene to each transcript</li>
62 * <li>copies features from gene to transcript sequences</li>
63 * <li>fetches the protein sequence for each transcript, maps and saves it as
64 * a cross-reference</li>
65 * <li>aligns each transcript against the gene sequence based on the position
70 public AlignmentI getSequenceRecords(String query) throws Exception
72 List<String> transcriptsWanted = null;
74 if (isTranscriptIdentifier(query))
76 transcriptsWanted = Arrays.asList(query
77 .split(getAccessionSeparator()));
78 query = getGeneForTranscript(query);
85 AlignmentI al = super.getSequenceRecords(query);
86 if (al.getHeight() > 0)
88 getTranscripts(al, query, transcriptsWanted);
95 * Gets the parent gene identifier for a given transcript identifier, by
96 * retrieving 'transcript' features overlapping the transcript, and finding
97 * the Parent property of the feature whose id is the given identifier.
102 protected String getGeneForTranscript(String transcriptId)
104 String geneId = null;
107 * reduce multiple transcripts (e.g. from Uniprot x-ref) to the first
108 * one only as representative (they should all have the same gene)
110 transcriptId = transcriptId.split(getAccessionSeparator())[0];
114 EnsemblFeatureType[] geneFeature = new EnsemblFeatureType[] { EnsemblFeatureType.transcript };
115 AlignmentI al = new EnsemblFeatures().getSequenceRecords(
116 transcriptId, geneFeature);
117 if (al != null && al.getHeight() > 0)
119 SequenceFeature[] sfs = al.getSequenceAt(0).getSequenceFeatures();
122 for (SequenceFeature sf : sfs)
124 if (transcriptId.equals(getTranscriptId(sf)))
126 String parent = (String) sf.getValue(PARENT);
127 if (parent != null && parent.startsWith(GENE_PREFIX))
129 geneId = parent.substring(5);
137 } catch (IOException e)
139 System.err.println("Error retrieving gene id for " + transcriptId
140 + ": " + e.getMessage());
146 * Constructs all transcripts for the gene, as identified by "transcript"
147 * features whose Parent is the requested gene. The coding transcript
148 * sequences (i.e. with introns omitted) are added to the alignment.
152 * @param transcriptsWanted
153 * optional list of transcript ids to filter by
156 protected void getTranscripts(AlignmentI al, String accId,
157 List<String> transcriptsWanted)
160 SequenceI gene = al.getSequenceAt(0);
161 List<SequenceFeature> transcriptFeatures = getTranscriptFeatures(accId,
162 gene, transcriptsWanted);
164 for (SequenceFeature transcriptFeature : transcriptFeatures)
166 makeTranscript(transcriptFeature, al, gene);
171 * Constructs a spliced transcript sequence by finding 'exon' features for the
172 * given id (or failing that 'CDS'). Copies features on to the new sequence.
173 * 'Aligns' the new sequence against the gene sequence by padding with gaps,
174 * and adds it to the alignment.
176 * @param transcriptFeature
178 * the alignment to which to add the new sequence
180 * the parent gene sequence, with features
183 SequenceI makeTranscript(SequenceFeature transcriptFeature,
184 AlignmentI al, SequenceI gene)
186 String accId = getTranscriptId(transcriptFeature);
193 * NB we are mapping from gene sequence (not genome), so do not
194 * need to check for reverse strand (gene and transcript sequences
195 * are in forward sense)
199 * make a gene-length sequence filled with gaps
200 * we will fill in the bases for transcript regions
202 char[] seqChars = new char[gene.getLength()];
203 Arrays.fill(seqChars, al.getGapCharacter());
206 * look for exon features of the transcript, failing that for CDS
207 * (for example ENSG00000124610 has 1 CDS but no exon features)
209 String parentId = "transcript:" + accId;
210 List<SequenceFeature> splices = findFeatures(gene,
211 SequenceOntologyI.EXON, parentId);
212 if (splices.isEmpty())
214 splices = findFeatures(gene, SequenceOntologyI.CDS, parentId);
217 int transcriptLength = 0;
218 final char[] geneChars = gene.getSequence();
219 int offset = gene.getStart(); // to convert to 0-based positions
220 List<int[]> mappedFrom = new ArrayList<int[]>();
222 for (SequenceFeature sf : splices)
224 int start = sf.getBegin() - offset;
225 int end = sf.getEnd() - offset;
226 int spliceLength = end - start + 1;
227 System.arraycopy(geneChars, start, seqChars, start, spliceLength);
228 transcriptLength += spliceLength;
229 mappedFrom.add(new int[] { sf.getBegin(), sf.getEnd() });
232 Sequence transcript = new Sequence(accId, seqChars, 1, transcriptLength);
233 String geneName = (String) transcriptFeature.getValue(NAME);
234 if (geneName != null)
236 transcript.setDescription(geneName);
238 transcript.createDatasetSequence();
240 al.addSequence(transcript);
243 * transfer features to the new sequence; we use EnsemblCdna to do this,
244 * to filter out unwanted features types (see method retainFeature)
246 List<int[]> mapTo = new ArrayList<int[]>();
247 mapTo.add(new int[] { 1, transcriptLength });
248 MapList mapping = new MapList(mappedFrom, mapTo, 1, 1);
249 new EnsemblCdna().transferFeatures(gene.getSequenceFeatures(),
250 transcript.getDatasetSequence(), mapping, parentId);
253 * and finally fetch the protein product and save as a cross-reference
255 new EnsemblCdna().addProteinProduct(transcript);
261 * Returns the 'transcript_id' property of the sequence feature (or null)
266 protected String getTranscriptId(SequenceFeature feature)
268 return (String) feature.getValue("transcript_id");
272 * Returns a list of the transcript features on the sequence whose Parent is
273 * the gene for the accession id.
276 * @param geneSequence
277 * @param transcriptsWanted
278 * optional list of ids to filter on
281 protected List<SequenceFeature> getTranscriptFeatures(String accId,
282 SequenceI geneSequence, List<String> transcriptsWanted)
284 List<SequenceFeature> transcriptFeatures = new ArrayList<SequenceFeature>();
286 String parentIdentifier = GENE_PREFIX + accId;
287 SequenceFeature[] sfs = geneSequence.getSequenceFeatures();
291 for (SequenceFeature sf : sfs)
293 if (isTranscript(sf.getType()))
295 if (transcriptsWanted != null)
297 String transcriptId = (String) sf.getValue("transcript_id");
298 if (!transcriptsWanted.contains(transcriptId))
303 String parent = (String) sf.getValue(PARENT);
304 if (parentIdentifier.equals(parent))
306 transcriptFeatures.add(sf);
312 return transcriptFeatures;
316 public String getDescription()
318 return "Fetches all transcripts and variant features for a gene or transcript";
322 * Default test query is a gene id (can also enter a transcript id)
325 public String getTestQuery()
327 return "ENSG00000157764"; // BRAF, 5 transcripts, reverse strand
328 // ENSG00000090266 // NDUFB2, 15 transcripts, forward strand
329 // ENSG00000101812 // H2BFM histone, 3 transcripts, forward strand
330 // ENSG00000123569 // H2BFWT histone, 2 transcripts, reverse strand
334 * Answers true for a feature of type 'gene' (or a sub-type of gene in the
335 * Sequence Ontology), whose ID is the accession we are retrieving
338 protected boolean identifiesSequence(SequenceFeature sf, String accId)
340 if (SequenceOntologyFactory.getInstance().isA(sf.getType(),
341 SequenceOntologyI.GENE))
343 String id = (String) sf.getValue(ID);
344 if ((GENE_PREFIX + accId).equals(id))
353 * Answers true unless feature type is 'gene', or 'transcript' with a parent
354 * which is a different gene. We need the gene features to identify the range,
355 * but it is redundant information on the gene sequence. Checking the parent
356 * allows us to drop transcript features which belong to different
357 * (overlapping) genes.
360 protected boolean retainFeature(SequenceFeature sf, String accessionId)
362 if (SequenceOntologyFactory.getInstance().isA(sf.getType(),
363 SequenceOntologyI.GENE))
368 if (isTranscript(sf.getType()))
370 String parent = (String) sf.getValue(PARENT);
371 if (!(GENE_PREFIX + accessionId).equals(parent))
380 * Answers false. This allows an optimisation - a single 'gene' feature is all
381 * that is needed to identify the positions of the gene on the genomic
385 protected boolean isSpliceable()
391 protected List<String> getCrossReferenceDatabases()
393 // found these for ENSG00000157764 on 30/01/2016:
394 // return new String[] {"Vega_gene", "OTTG", "ENS_LRG_gene", "ArrayExpress",
395 // "EntrezGene", "HGNC", "MIM_GENE", "MIM_MORBID", "WikiGene"};
396 return super.getCrossReferenceDatabases();
400 * Override to do nothing as Ensembl doesn't return a protein sequence for a
404 protected void addProteinProduct(SequenceI querySeq)
409 public Regex getAccessionValidator()
411 return ACCESSION_REGEX;