1 package jalview.ext.ensembl;
3 import jalview.analysis.AlignmentUtils;
4 import jalview.datamodel.Alignment;
5 import jalview.datamodel.AlignmentI;
6 import jalview.datamodel.DBRefEntry;
7 import jalview.datamodel.DBRefSource;
8 import jalview.datamodel.Mapping;
9 import jalview.datamodel.SequenceFeature;
10 import jalview.datamodel.SequenceI;
11 import jalview.exceptions.JalviewException;
12 import jalview.io.FastaFile;
13 import jalview.io.FileParse;
14 import jalview.io.gff.SequenceOntologyFactory;
15 import jalview.io.gff.SequenceOntologyI;
16 import jalview.util.DBRefUtils;
17 import jalview.util.MapList;
19 import java.io.IOException;
20 import java.net.MalformedURLException;
22 import java.util.ArrayList;
23 import java.util.Arrays;
24 import java.util.Collections;
25 import java.util.Comparator;
26 import java.util.List;
29 * Base class for Ensembl sequence fetchers
31 * @see http://rest.ensembl.org/documentation/info/sequence_id
34 public abstract class EnsemblSeqProxy extends EnsemblRestClient
36 private static final List<String> CROSS_REFERENCES = Arrays
37 .asList(new String[] { "CCDS", "Uniprot/SWISSPROT",
38 "Uniprot/SPTREMBL" });
40 protected static final String CONSEQUENCE_TYPE = "consequence_type";
42 protected static final String PARENT = "Parent";
44 protected static final String ID = "ID";
46 protected static final String NAME = "Name";
48 protected static final String DESCRIPTION = "description";
51 * enum for 'type' parameter to the /sequence REST service
53 public enum EnsemblSeqType
56 * type=genomic to fetch full dna including introns
61 * type=cdna to fetch dna including UTRs
66 * type=cds to fetch coding dna excluding UTRs
71 * type=protein to fetch peptide product sequence
76 * the value of the 'type' parameter to fetch this version of
81 EnsemblSeqType(String t)
86 public String getType()
94 * A comparator to sort ranges into ascending start position order
96 private class RangeSorter implements Comparator<int[]>
100 RangeSorter(boolean forward)
106 public int compare(int[] o1, int[] o2)
108 return (forwards ? 1 : -1) * Integer.compare(o1[0], o2[0]);
114 * Default constructor (to use rest.ensembl.org)
116 public EnsemblSeqProxy()
122 * Constructor given the target domain to fetch data from
124 public EnsemblSeqProxy(String d)
130 * Makes the sequence queries to Ensembl's REST service and returns an
131 * alignment consisting of the returned sequences.
134 public AlignmentI getSequenceRecords(String query) throws Exception
136 // TODO use a String... query vararg instead?
138 // danger: accession separator used as a regex here, a string elsewhere
139 // in this case it is ok (it is just a space), but (e.g.) '\' would not be
140 List<String> allIds = Arrays.asList(query
141 .split(getAccessionSeparator()));
142 AlignmentI alignment = null;
146 * execute queries, if necessary in batches of the
147 * maximum allowed number of ids
149 int maxQueryCount = getMaximumQueryCount();
150 for (int v = 0, vSize = allIds.size(); v < vSize; v += maxQueryCount)
152 int p = Math.min(vSize, v + maxQueryCount);
153 List<String> ids = allIds.subList(v, p);
156 alignment = fetchSequences(ids, alignment);
157 } catch (Throwable r)
160 String msg = "Aborting ID retrieval after " + v
161 + " chunks. Unexpected problem (" + r.getLocalizedMessage()
163 System.err.println(msg);
168 if (alignment == null)
174 * fetch and transfer genomic sequence features,
175 * fetch protein product and add as cross-reference
177 for (String accId : allIds)
179 addFeaturesAndProduct(accId, alignment);
182 for (SequenceI seq : alignment.getSequences())
184 getCrossReferences(seq);
191 * Fetches Ensembl features using the /overlap REST endpoint, and adds them to
192 * the sequence in the alignment. Also fetches the protein product, maps it
193 * from the CDS features of the sequence, and saves it as a cross-reference of
199 protected void addFeaturesAndProduct(String accId, AlignmentI alignment)
201 if (alignment == null)
209 * get 'dummy' genomic sequence with exon, cds and variation features
211 SequenceI genomicSequence = null;
212 EnsemblFeatures gffFetcher = new EnsemblFeatures(getDomain());
213 EnsemblFeatureType[] features = getFeaturesToFetch();
214 AlignmentI geneFeatures = gffFetcher.getSequenceRecords(accId,
216 if (geneFeatures.getHeight() > 0)
218 genomicSequence = geneFeatures.getSequenceAt(0);
220 if (genomicSequence != null)
223 * transfer features to the query sequence
225 SequenceI querySeq = alignment.findName(accId);
226 if (transferFeatures(accId, genomicSequence, querySeq))
230 * fetch and map protein product, and add it as a cross-reference
231 * of the retrieved sequence
233 addProteinProduct(querySeq);
236 } catch (IOException e)
238 System.err.println("Error transferring Ensembl features: "
244 * Returns those sequence feature types to fetch from Ensembl. We may want
245 * features either because they are of interest to the user, or as means to
246 * identify the locations of the sequence on the genomic sequence (CDS
247 * features identify CDS, exon features identify cDNA etc).
251 protected abstract EnsemblFeatureType[] getFeaturesToFetch();
254 * Fetches and maps the protein product, and adds it as a cross-reference of
255 * the retrieved sequence
257 protected void addProteinProduct(SequenceI querySeq)
259 String accId = querySeq.getName();
262 AlignmentI protein = new EnsemblProtein(getDomain())
263 .getSequenceRecords(accId);
264 if (protein == null || protein.getHeight() == 0)
266 System.out.println("No protein product found for " + accId);
269 SequenceI proteinSeq = protein.getSequenceAt(0);
272 * need dataset sequences (to be the subject of mappings)
274 proteinSeq.createDatasetSequence();
275 querySeq.createDatasetSequence();
277 MapList mapList = AlignmentUtils.mapCdsToProtein(querySeq, proteinSeq);
280 // clunky: ensure Uniprot xref if we have one is on mapped sequence
281 SequenceI ds = proteinSeq.getDatasetSequence();
282 ds.setSourceDBRef(proteinSeq.getSourceDBRef());
283 Mapping map = new Mapping(ds, mapList);
284 DBRefEntry dbr = new DBRefEntry(getDbSource(), getDbVersion(),
286 querySeq.getDatasetSequence().addDBRef(dbr);
289 * copy exon features to protein, compute peptide variants from dna
290 * variants and add as features on the protein sequence ta-da
292 AlignmentUtils.computeProteinFeatures(querySeq, proteinSeq, mapList);
294 } catch (Exception e)
297 .println(String.format("Error retrieving protein for %s: %s",
298 accId, e.getMessage()));
303 * Get database xrefs from Ensembl, and attach them to the sequence
307 protected void getCrossReferences(SequenceI seq)
309 while (seq.getDatasetSequence() != null)
311 seq = seq.getDatasetSequence();
314 EnsemblXref xrefFetcher = new EnsemblXref(getDomain());
315 List<DBRefEntry> xrefs = xrefFetcher.getCrossReferences(seq.getName(),
316 getCrossReferenceDatabases());
317 for (DBRefEntry xref : xrefs)
321 * Save any Uniprot xref to be the reference for SIFTS mapping
323 if (DBRefSource.UNIPROT.equals(xref.getSource()))
325 seq.setSourceDBRef(xref);
331 * Returns a list of database names to be used when fetching cross-references.
332 * Specifically, the names are used to filter data returned by the Ensembl
333 * xrefs REST service on the value in field 'dbname'.
337 protected List<String> getCrossReferenceDatabases()
339 return CROSS_REFERENCES;
343 * Fetches sequences for the list of accession ids and adds them to the
344 * alignment. Returns the extended (or created) alignment.
349 * @throws JalviewException
350 * @throws IOException
352 protected AlignmentI fetchSequences(List<String> ids, AlignmentI alignment)
353 throws JalviewException, IOException
355 if (!isEnsemblAvailable())
358 throw new JalviewException("ENSEMBL Rest API not available.");
360 FileParse fp = getSequenceReader(ids);
361 FastaFile fr = new FastaFile(fp);
362 if (fr.hasWarningMessage())
364 System.out.println(String.format(
365 "Warning when retrieving %d ids %s\n%s", ids.size(),
366 ids.toString(), fr.getWarningMessage()));
368 else if (fr.getSeqs().size() != ids.size())
370 System.out.println(String.format(
371 "Only retrieved %d sequences for %d query strings", fr
372 .getSeqs().size(), ids.size()));
375 if (fr.getSeqs().size() == 1 && fr.getSeqs().get(0).getLength() == 0)
378 * POST request has returned an empty FASTA file e.g. for invalid id
380 throw new IOException("No data returned for " + ids);
383 if (fr.getSeqs().size() > 0)
385 AlignmentI seqal = new Alignment(
386 fr.getSeqsAsArray());
387 for (SequenceI sq:seqal.getSequences())
389 if (sq.getDescription() == null)
391 sq.setDescription(getDbName());
393 String name = sq.getName();
394 if (ids.contains(name)
395 || ids.contains(name.replace("ENSP", "ENST")))
397 DBRefUtils.parseToDbRef(sq, DBRefSource.ENSEMBL, "0", name);
400 if (alignment == null)
406 alignment.append(seqal);
413 * Returns the URL for the REST call
416 * @throws MalformedURLException
419 protected URL getUrl(List<String> ids) throws MalformedURLException
422 * a single id is included in the URL path
423 * multiple ids go in the POST body instead
425 StringBuffer urlstring = new StringBuffer(128);
426 urlstring.append(getDomain() + "/sequence/id");
429 urlstring.append("/").append(ids.get(0));
431 // @see https://github.com/Ensembl/ensembl-rest/wiki/Output-formats
432 urlstring.append("?type=").append(getSourceEnsemblType().getType());
433 urlstring.append(("&Accept=text/x-fasta"));
435 URL url = new URL(urlstring.toString());
440 * A sequence/id POST request currently allows up to 50 queries
442 * @see http://rest.ensembl.org/documentation/info/sequence_id_post
445 public int getMaximumQueryCount()
451 protected boolean useGetRequest()
457 protected String getRequestMimeType(boolean multipleIds)
459 return multipleIds ? "application/json" : "text/x-fasta";
463 protected String getResponseMimeType()
465 return "text/x-fasta";
470 * @return the configured sequence return type for this source
472 protected abstract EnsemblSeqType getSourceEnsemblType();
475 * Returns a list of [start, end] genomic ranges corresponding to the sequence
478 * The correspondence between the frames of reference is made by locating
479 * those features on the genomic sequence which identify the retrieved
480 * sequence. Specifically
482 * <li>genomic sequence is identified by "transcript" features with
483 * ID=transcript:transcriptId</li>
484 * <li>cdna sequence is identified by "exon" features with
485 * Parent=transcript:transcriptId</li>
486 * <li>cds sequence is identified by "CDS" features with
487 * Parent=transcript:transcriptId</li>
490 * The returned ranges are sorted to run forwards (for positive strand) or
491 * backwards (for negative strand). Aborts and returns null if both positive
492 * and negative strand are found (this should not normally happen).
494 * @param sourceSequence
497 * the start position of the sequence we are mapping to
500 protected MapList getGenomicRangesFromFeatures(SequenceI sourceSequence,
501 String accId, int start)
503 SequenceFeature[] sfs = sourceSequence.getSequenceFeatures();
510 * generously initial size for number of cds regions
511 * (worst case titin Q8WZ42 has c. 313 exons)
513 List<int[]> regions = new ArrayList<int[]>(100);
514 int mappedLength = 0;
515 int direction = 1; // forward
516 boolean directionSet = false;
518 for (SequenceFeature sf : sfs)
521 * accept the target feature type or a specialisation of it
522 * (e.g. coding_exon for exon)
524 if (identifiesSequence(sf, accId))
526 int strand = sf.getStrand();
527 strand = strand == 0 ? 1 : strand; // treat unknown as forward
529 if (directionSet && strand != direction)
531 // abort - mix of forward and backward
532 System.err.println("Error: forward and backward strand for "
540 * add to CDS ranges, semi-sorted forwards/backwards
544 regions.add(0, new int[] { sf.getEnd(), sf.getBegin() });
548 regions.add(new int[] { sf.getBegin(), sf.getEnd() });
550 mappedLength += Math.abs(sf.getEnd() - sf.getBegin() + 1);
555 * 'gene' sequence is contiguous so we can stop as soon as its
556 * identifying feature has been found
563 if (regions.isEmpty())
565 System.out.println("Failed to identify target sequence for " + accId
566 + " from genomic features");
571 * a final sort is needed since Ensembl returns CDS sorted within source
572 * (havana / ensembl_havana)
574 Collections.sort(regions, new RangeSorter(direction == 1));
576 List<int[]> to = Arrays.asList(new int[] { start,
577 start + mappedLength - 1 });
579 return new MapList(regions, to, 1, 1);
583 * Answers true if the sequence being retrieved may occupy discontiguous
584 * regions on the genomic sequence.
586 protected boolean isSpliceable()
592 * Returns true if the sequence feature marks positions of the genomic
593 * sequence feature which are within the sequence being retrieved. For
594 * example, an 'exon' feature whose parent is the target transcript marks the
595 * cdna positions of the transcript.
601 protected abstract boolean identifiesSequence(SequenceFeature sf,
605 * Transfers the sequence feature to the target sequence, locating its start
606 * and end range based on the mapping. Features which do not overlap the
607 * target sequence are ignored.
610 * @param targetSequence
612 * mapping from the sequence feature's coordinates to the target
615 protected void transferFeature(SequenceFeature sf,
616 SequenceI targetSequence, MapList mapping)
618 int start = sf.getBegin();
619 int end = sf.getEnd();
620 int[] mappedRange = mapping.locateInTo(start, end);
622 if (mappedRange != null)
624 SequenceFeature copy = new SequenceFeature(sf);
625 copy.setBegin(Math.min(mappedRange[0], mappedRange[1]));
626 copy.setEnd(Math.max(mappedRange[0], mappedRange[1]));
627 targetSequence.addSequenceFeature(copy);
630 * for sequence_variant, make an additional feature with consequence
632 // if (SequenceOntologyFactory.getInstance().isA(sf.getType(),
633 // SequenceOntologyI.SEQUENCE_VARIANT))
635 // String consequence = (String) sf.getValue(CONSEQUENCE_TYPE);
636 // if (consequence != null)
638 // SequenceFeature sf2 = new SequenceFeature("consequence",
639 // consequence, copy.getBegin(), copy.getEnd(), 0f,
641 // targetSequence.addSequenceFeature(sf2);
648 * Transfers features from sourceSequence to targetSequence
651 * @param sourceSequence
652 * @param targetSequence
653 * @return true if any features were transferred, else false
655 protected boolean transferFeatures(String accessionId,
656 SequenceI sourceSequence, SequenceI targetSequence)
658 if (sourceSequence == null || targetSequence == null)
663 // long start = System.currentTimeMillis();
664 SequenceFeature[] sfs = sourceSequence.getSequenceFeatures();
665 MapList mapping = getGenomicRangesFromFeatures(sourceSequence, accessionId,
666 targetSequence.getStart());
672 boolean result = transferFeatures(sfs, targetSequence, mapping,
674 // System.out.println("transferFeatures (" + (sfs.length) + " --> "
675 // + targetSequence.getSequenceFeatures().length + ") to "
676 // + targetSequence.getName()
677 // + " took " + (System.currentTimeMillis() - start) + "ms");
682 * Transfer features to the target sequence. The start/end positions are
683 * converted using the mapping. Features which do not overlap are ignored.
684 * Features whose parent is not the specified identifier are also ignored.
687 * @param targetSequence
692 protected boolean transferFeatures(SequenceFeature[] features,
693 SequenceI targetSequence, MapList mapping, String parentId)
695 final boolean forwardStrand = mapping.isFromForwardStrand();
698 * sort features by start position (descending if reverse strand)
699 * before transferring (in forwards order) to the target sequence
701 Arrays.sort(features, new Comparator<SequenceFeature>()
704 public int compare(SequenceFeature o1, SequenceFeature o2)
706 int c = Integer.compare(o1.getBegin(), o2.getBegin());
707 return forwardStrand ? c : -c;
711 boolean transferred = false;
712 for (SequenceFeature sf : features)
714 if (retainFeature(sf, parentId))
716 transferFeature(sf, targetSequence, mapping);
724 * Answers true if the feature type is one we want to keep for the sequence.
725 * Some features are only retrieved in order to identify the sequence range,
726 * and may then be discarded as redundant information (e.g. "CDS" feature for
729 @SuppressWarnings("unused")
730 protected boolean retainFeature(SequenceFeature sf, String accessionId)
732 return true; // override as required
736 * Answers true if the feature has a Parent which refers to the given
737 * accession id, or if the feature has no parent. Answers false if the
738 * feature's Parent is for a different accession id.
744 protected boolean featureMayBelong(SequenceFeature sf, String identifier)
746 String parent = (String) sf.getValue(PARENT);
747 // using contains to allow for prefix "gene:", "transcript:" etc
748 if (parent != null && !parent.contains(identifier))
750 // this genomic feature belongs to a different transcript
757 public String getDescription()
759 return "Ensembl " + getSourceEnsemblType().getType()
760 + " sequence with variant features";
764 * Returns a (possibly empty) list of features on the sequence which have the
765 * specified sequence ontology type (or a sub-type of it), and the given
766 * identifier as parent
773 protected List<SequenceFeature> findFeatures(SequenceI sequence,
774 String type, String parentId)
776 List<SequenceFeature> result = new ArrayList<SequenceFeature>();
778 SequenceFeature[] sfs = sequence.getSequenceFeatures();
780 SequenceOntologyI so = SequenceOntologyFactory.getInstance();
781 for (SequenceFeature sf :sfs) {
782 if (so.isA(sf.getType(), type))
784 String parent = (String) sf.getValue(PARENT);
785 if (parent.equals(parentId))
796 * Answers true if the feature type is either 'NMD_transcript_variant' or
797 * 'transcript' or one of its sub-types in the Sequence Ontology. This is
798 * needed because NMD_transcript_variant behaves like 'transcript' in Ensembl
799 * although strictly speaking it is not (it is a sub-type of
805 public static boolean isTranscript(String featureType)
807 return SequenceOntologyI.NMD_TRANSCRIPT_VARIANT.equals(featureType)
808 || SequenceOntologyFactory.getInstance().isA(featureType,
809 SequenceOntologyI.TRANSCRIPT);