2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.datamodel.xdb.embl;
23 import jalview.datamodel.DBRefEntry;
24 import jalview.datamodel.DBRefSource;
25 import jalview.datamodel.FeatureProperties;
26 import jalview.datamodel.Mapping;
27 import jalview.datamodel.Sequence;
28 import jalview.datamodel.SequenceFeature;
29 import jalview.datamodel.SequenceI;
30 import jalview.util.DBRefUtils;
31 import jalview.util.MapList;
33 import java.util.Hashtable;
34 import java.util.Map.Entry;
35 import java.util.Vector;
38 * Data model for one entry returned from an EMBL query, as marshalled by a
41 * For example: http://www.ebi.ac.uk/Tools/dbfetch/dbfetch/embl/x53828/emblxml
43 * @see embl_mapping.xml
45 public class EmblEntry
61 Vector<String> keywords;
63 Vector<DBRefEntry> dbRefs;
65 Vector<EmblFeature> features;
67 EmblSequence sequence;
70 * @return the accession
72 public String getAccession()
79 * the accession to set
81 public void setAccession(String accession)
83 this.accession = accession;
89 public Vector<DBRefEntry> getDbRefs()
98 public void setDbRefs(Vector<DBRefEntry> dbRefs)
100 this.dbRefs = dbRefs;
106 public String getDesc()
115 public void setDesc(String desc)
121 * @return the features
123 public Vector<EmblFeature> getFeatures()
130 * the features to set
132 public void setFeatures(Vector<EmblFeature> features)
134 this.features = features;
138 * @return the keywords
140 public Vector<String> getKeywords()
147 * the keywords to set
149 public void setKeywords(Vector<String> keywords)
151 this.keywords = keywords;
155 * @return the lastUpdated
157 public String getLastUpdated()
164 * the lastUpdated to set
166 public void setLastUpdated(String lastUpdated)
168 this.lastUpdated = lastUpdated;
172 * @return the releaseCreated
174 public String getRCreated()
180 * @param releaseCreated
181 * the releaseCreated to set
183 public void setRCreated(String releaseCreated)
185 this.rCreated = releaseCreated;
189 * @return the releaseLastUpdated
191 public String getRLastUpdated()
197 * @param releaseLastUpdated
198 * the releaseLastUpdated to set
200 public void setRLastUpdated(String releaseLastUpdated)
202 this.rLastUpdated = releaseLastUpdated;
206 * @return the sequence
208 public EmblSequence getSequence()
215 * the sequence to set
217 public void setSequence(EmblSequence sequence)
219 this.sequence = sequence;
223 * @return the taxDivision
225 public String getTaxDivision()
232 * the taxDivision to set
234 public void setTaxDivision(String taxDivision)
236 this.taxDivision = taxDivision;
240 * @return the version
242 public String getVersion()
251 public void setVersion(String version)
253 this.version = version;
257 * EMBL Feature support is limited. The text below is included for the benefit
258 * of any developer working on improving EMBL feature import in Jalview.
259 * Extract from EMBL feature specification see
260 * http://www.embl-ebi.ac.uk/embl/Documentation
261 * /FT_definitions/feature_table.html 3.5 Location 3.5.1 Purpose
263 * The location indicates the region of the presented sequence which
264 * corresponds to a feature.
266 * 3.5.2 Format and conventions The location contains at least one sequence
267 * location descriptor and may contain one or more operators with one or more
268 * sequence location descriptors. Base numbers refer to the numbering in the
269 * entry. This numbering designates the first base (5' end) of the presented
270 * sequence as base 1. Base locations beyond the range of the presented
271 * sequence may not be used in location descriptors, the only exception being
272 * location in a remote entry (see 3.5.2.1, e).
274 * Location operators and descriptors are discussed in more detail below.
276 * 3.5.2.1 Location descriptors
278 * The location descriptor can be one of the following: (a) a single base
279 * number (b) a site between two indicated adjoining bases (c) a single base
280 * chosen from within a specified range of bases (not allowed for new entries)
281 * (d) the base numbers delimiting a sequence span (e) a remote entry
282 * identifier followed by a local location descriptor (i.e., a-d)
284 * A site between two adjoining nucleotides, such as endonucleolytic cleavage
285 * site, is indicated by listing the two points separated by a carat (^). The
286 * permitted formats for this descriptor are n^n+1 (for example 55^56), or,
287 * for circular molecules, n^1, where "n" is the full length of the molecule,
288 * ie 1000^1 for circular molecule with length 1000.
290 * A single base chosen from a range of bases is indicated by the first base
291 * number and the last base number of the range separated by a single period
292 * (e.g., '12.21' indicates a single base taken from between the indicated
293 * points). From October 2006 the usage of this descriptor is restricted : it
294 * is illegal to use "a single base from a range" (c) either on its own or in
295 * combination with the "sequence span" (d) descriptor for newly created
296 * entries. The existing entries where such descriptors exist are going to be
299 * Sequence spans are indicated by the starting base number and the ending
300 * base number separated by two periods (e.g., '34..456'). The '<' and '>'
301 * symbols may be used with the starting and ending base numbers to indicate
302 * that an end point is beyond the specified base number. The starting and
303 * ending base positions can be represented as distinct base numbers
304 * ('34..456') or a site between two indicated adjoining bases.
306 * A location in a remote entry (not the entry to which the feature table
307 * belongs) can be specified by giving the accession-number and sequence
308 * version of the remote entry, followed by a colon ":", followed by a
309 * location descriptor which applies to that entry's sequence (i.e.
310 * J12345.1:1..15, see also examples below)
314 * The location operator is a prefix that specifies what must be done to the
315 * indicated sequence to find or construct the location corresponding to the
316 * feature. A list of operators is given below with their definitions and most
319 * complement(location) Find the complement of the presented sequence in the
320 * span specified by " location" (i.e., read the complement of the presented
321 * strand in its 5'-to-3' direction)
323 * join(location,location, ... location) The indicated elements should be
324 * joined (placed end-to-end) to form one contiguous sequence
326 * order(location,location, ... location) The elements can be found in the
327 * specified order (5' to 3' direction), but nothing is implied about the
328 * reasonableness about joining them
330 * Note : location operator "complement" can be used in combination with
331 * either " join" or "order" within the same location; combinations of "join"
332 * and "order" within the same location (nested operators) are illegal.
336 * 3.5.3 Location examples
338 * The following is a list of common location descriptors with their meanings:
340 * Location Description
342 * 467 Points to a single base in the presented sequence
344 * 340..565 Points to a continuous range of bases bounded by and including the
345 * starting and ending bases
347 * <345..500 Indicates that the exact lower boundary point of a feature is
348 * unknown. The location begins at some base previous to the first base
349 * specified (which need not be contained in the presented sequence) and
350 * continues to and includes the ending base
352 * <1..888 The feature starts before the first sequenced base and continues to
353 * and includes base 888
355 * 1..>888 The feature starts at the first sequenced base and continues beyond
358 * 102.110 Indicates that the exact location is unknown but that it is one of
359 * the bases between bases 102 and 110, inclusive
361 * 123^124 Points to a site between bases 123 and 124
363 * join(12..78,134..202) Regions 12 to 78 and 134 to 202 should be joined to
364 * form one contiguous sequence
367 * complement(34..126) Start at the base complementary to 126 and finish at
368 * the base complementary to base 34 (the feature is on the strand
369 * complementary to the presented strand)
372 * complement(join(2691..4571,4918..5163)) Joins regions 2691 to 4571 and 4918
373 * to 5163, then complements the joined segments (the feature is on the strand
374 * complementary to the presented strand)
376 * join(complement(4918..5163),complement(2691..4571)) Complements regions
377 * 4918 to 5163 and 2691 to 4571, then joins the complemented segments (the
378 * feature is on the strand complementary to the presented strand)
380 * J00194.1:100..202 Points to bases 100 to 202, inclusive, in the entry (in
381 * this database) with primary accession number 'J00194'
383 * join(1..100,J00194.1:100..202) Joins region 1..100 of the existing entry
384 * with the region 100..202 of remote entry J00194
387 * Recover annotated sequences from EMBL file
390 * don't return nucleic acid sequences
394 * don't return any translated protein sequences marked in features
395 * @return dataset sequences with DBRefs and features - DNA always comes first
397 public SequenceI[] getSequences(boolean noNa,
398 boolean noPeptide, String sourceDb)
399 { // TODO: ensure emblEntry.getSequences behaves correctly for returning all
400 // cases of noNa and noPeptide
401 Vector<SequenceI> seqs = new Vector<SequenceI>();
405 // In theory we still need to create this if noNa is set to avoid a null
407 dna = new Sequence(sourceDb + "|" + accession, sequence.getSequence());
408 dna.setDescription(desc);
409 DBRefEntry retrievedref = new DBRefEntry(sourceDb, version, accession);
410 dna.addDBRef(retrievedref);
411 // add map to indicate the sequence is a valid coordinate frame for the
413 retrievedref.setMap(new Mapping(null, new int[]
414 { 1, dna.getLength() }, new int[]
415 { 1, dna.getLength() }, 1, 1));
416 // TODO: transform EMBL Database refs to canonical form
419 for (DBRefEntry dbref : dbRefs)
427 for (EmblFeature feature: features)
431 if (feature.dbRefs != null)
433 for (DBRefEntry dbref : feature.dbRefs)
439 if (FeatureProperties.isCodingFeature(sourceDb, feature.getName()))
441 parseCodingFeature(feature, sourceDb, seqs, dna, noPeptide);
445 // General feature type.
446 // TODO this is just duplicated code ??
449 if (feature.dbRefs != null)
451 for (DBRefEntry dbref : feature.dbRefs)
459 } catch (Exception e)
461 System.err.println("EMBL Record Features parsing error!");
463 .println("Please report the following to help@jalview.org :");
464 System.err.println("EMBL Record " + accession);
465 System.err.println("Resulted in exception: " + e.getMessage());
466 e.printStackTrace(System.err);
468 if (!noNa && dna != null)
472 SequenceI[] sqs = new SequenceI[seqs.size()];
473 for (int i = 0, j = seqs.size(); i < j; i++)
475 sqs[i] = seqs.elementAt(i);
482 * attempt to extract coding region and product from a feature and properly
483 * decorate it with annotations.
488 * source database for the EMBLXML
490 * place where sequences go
492 * parent dna sequence for this record
494 * flag for generation of Peptide sequence objects
496 private void parseCodingFeature(EmblFeature feature, String sourceDb,
497 Vector<SequenceI> seqs, Sequence dna, boolean noPeptide)
499 boolean isEmblCdna = sourceDb.equals(DBRefSource.EMBLCDS);
500 // extract coding region(s)
503 if (feature.locations != null)
505 for (EmblFeatureLocations loc : feature.locations)
507 int[] se = loc.getElementRanges(accession);
514 int[] t = new int[exon.length + se.length];
515 System.arraycopy(exon, 0, t, 0, exon.length);
516 System.arraycopy(se, 0, t, exon.length, se.length);
522 String prname = new String();
524 Hashtable<String, String> vals = new Hashtable<String, String>();
527 if (feature.getQualifiers() != null)
529 for (Qualifier q : feature.getQualifiers())
531 String qname = q.getName();
532 if (qname.equals("translation"))
534 StringBuilder prsq = new StringBuilder(q.getValues()[0]);
535 int p = prsq.indexOf(" ");
538 prsq.deleteCharAt(p);
539 p = prsq.indexOf(" ", p);
541 prseq = prsq.toString();
545 else if (qname.equals("protein_id"))
547 prid = q.getValues()[0];
549 else if (qname.equals("codon_start"))
551 prstart = Integer.parseInt(q.getValues()[0]);
553 else if (qname.equals("product"))
555 prname = q.getValues()[0];
559 // throw anything else into the additional properties hash
560 String[] s = q.getValues();
561 StringBuilder sb = new StringBuilder();
564 for (int i = 0; i < s.length; i++)
570 vals.put(qname, sb.toString());
574 Sequence product = null;
575 DBRefEntry protEMBLCDS = null;
576 exon = adjustForPrStart(prstart, exon);
577 boolean noProteinDbref = true;
579 if (prseq != null && prname != null && prid != null)
582 product = new Sequence(prid, prseq, 1, prseq.length());
583 product.setDescription(((prname.length() == 0) ? "Protein Product from "
588 // Protein is also added to vector of sequences returned
591 // we have everything - create the mapping and perhaps the protein
593 if (exon == null || exon.length == 0)
596 .println("Implementation Notice: EMBLCDS records not properly supported yet - Making up the CDNA region of this sequence... may be incorrect ("
597 + sourceDb + ":" + getAccession() + ")");
598 if (prseq.length() * 3 == (1 - prstart + dna.getSequence().length))
601 .println("Not allowing for additional stop codon at end of cDNA fragment... !");
602 // this might occur for CDS sequences where no features are
605 { dna.getStart() + (prstart - 1), dna.getEnd() };
606 map = new Mapping(product, exon, new int[]
607 { 1, prseq.length() }, 3, 1);
609 if ((prseq.length() + 1) * 3 == (1 - prstart + dna.getSequence().length))
612 .println("Allowing for additional stop codon at end of cDNA fragment... will probably cause an error in VAMSAs!");
614 { dna.getStart() + (prstart - 1), dna.getEnd() - 3 };
615 map = new Mapping(product, exon, new int[]
616 { 1, prseq.length() }, 3, 1);
621 // Trim the exon mapping if necessary - the given product may only be a
622 // fragment of a larger protein. (EMBL:AY043181 is an example)
626 // TODO: Add a DbRef back to the parent EMBL sequence with the exon
628 // if given a dataset reference, search dataset for parent EMBL
629 // sequence if it exists and set its map
630 // make a new feature annotating the coding contig
634 // final product length trunctation check
636 map = new Mapping(product,
637 adjustForProteinLength(prseq.length(), exon), new int[]
638 { 1, prseq.length() }, 3, 1);
639 // reconstruct the EMBLCDS entry
640 // TODO: this is only necessary when there codon annotation is
641 // complete (I think JBPNote)
642 DBRefEntry pcdnaref = new DBRefEntry();
643 pcdnaref.setAccessionId(prid);
644 pcdnaref.setSource(DBRefSource.EMBLCDS);
645 pcdnaref.setVersion(getVersion()); // same as parent EMBL version.
646 MapList mp = new MapList(new int[]
647 { 1, prseq.length() }, new int[]
648 { 1 + (prstart - 1), (prstart - 1) + 3 * prseq.length() }, 1, 3);
649 // { 1 + (prstart - 1) * 3,
650 // 1 + (prstart - 1) * 3 + prseq.length() * 3 - 1 }, new int[]
651 // { 1prstart, prstart + prseq.length() - 1 }, 3, 1);
652 pcdnaref.setMap(new Mapping(mp));
655 product.addDBRef(pcdnaref);
656 protEMBLCDS = new DBRefEntry(pcdnaref);
657 protEMBLCDS.setSource(DBRefSource.EMBLCDSProduct);
658 product.addDBRef(protEMBLCDS);
664 // add cds feature to dna seq - this may include the stop codon
665 for (int xint = 0; exon != null && xint < exon.length; xint += 2)
667 SequenceFeature sf = new SequenceFeature();
668 sf.setBegin(exon[xint]);
669 sf.setEnd(exon[xint + 1]);
670 sf.setType(feature.getName());
671 sf.setFeatureGroup(sourceDb);
672 sf.setDescription("Exon " + (1 + xint / 2) + " for protein '"
673 + prname + "' EMBLCDS:" + prid);
674 sf.setValue(FeatureProperties.EXONPOS, new Integer(1 + xint));
675 sf.setValue(FeatureProperties.EXONPRODUCT, prname);
678 for (Entry<String, String> val : vals.entrySet())
680 sf.setValue(val.getKey(), val.getValue());
683 dna.addSequenceFeature(sf);
686 // add dbRefs to sequence
687 if (feature.dbRefs != null)
689 for (DBRefEntry ref : feature.dbRefs)
691 ref.setSource(DBRefUtils.getCanonicalName(ref
693 // Hard code the kind of protein product accessions that EMBL cite
694 if (ref.getSource().equals(DBRefSource.UNIPROT))
697 if (map != null && map.getTo() != null)
699 map.getTo().addDBRef(
700 new DBRefEntry(ref.getSource(), ref.getVersion(), ref
701 .getAccessionId())); // don't copy map over.
702 if (map.getTo().getName().indexOf(prid) == 0)
705 DBRefSource.UNIPROT + "|"
706 + ref.getAccessionId());
709 noProteinDbref = false;
713 DBRefEntry pref = new DBRefEntry(ref.getSource(),
714 ref.getVersion(), ref.getAccessionId());
715 pref.setMap(null); // reference is direct
716 product.addDBRef(pref);
717 // Add converse mapping reference
720 Mapping pmap = new Mapping(dna, map.getMap().getInverse());
721 pref = new DBRefEntry(sourceDb, getVersion(),
722 this.getAccession());
724 if (map.getTo() != null)
726 map.getTo().addDBRef(pref);
732 if (noProteinDbref && product != null)
734 // add protein coding reference to dna sequence so xref matches
735 if (protEMBLCDS == null)
737 protEMBLCDS = new DBRefEntry();
738 protEMBLCDS.setAccessionId(prid);
739 protEMBLCDS.setSource(DBRefSource.EMBLCDSProduct);
740 protEMBLCDS.setVersion(getVersion());
742 .setMap(new Mapping(product, map.getMap().getInverse()));
744 product.addDBRef(protEMBLCDS);
746 // Add converse mapping reference
749 Mapping pmap = new Mapping(product, protEMBLCDS.getMap().getMap()
751 DBRefEntry ncMap = new DBRefEntry(protEMBLCDS);
753 if (map.getTo() != null)
762 private int[] adjustForPrStart(int prstart, int[] exon)
765 int origxon[], sxpos = -1;
766 int sxstart, sxstop; // unnecessary variables used for debugging
767 // first adjust range for codon start attribute
770 origxon = new int[exon.length];
771 System.arraycopy(exon, 0, origxon, 0, exon.length);
773 for (int x = 0; x < exon.length && sxpos == -1; x += 2)
775 cdspos += exon[x + 1] - exon[x] + 1;
776 if (prstart <= cdspos)
780 sxstop = exon[x + 1];
781 // and adjust start boundary of first exon.
782 exon[x] = exon[x + 1] - cdspos + prstart;
789 int[] nxon = new int[exon.length - sxpos];
790 System.arraycopy(exon, sxpos, nxon, 0, exon.length - sxpos);
798 * truncate the last exon interval to the prlength'th codon
804 private int[] adjustForProteinLength(int prlength, int[] exon)
807 int origxon[], sxpos = -1, endxon = 0, cdslength = prlength * 3;
808 int sxstart, sxstop; // unnecessary variables used for debugging
809 // first adjust range for codon start attribute
810 if (prlength >= 1 && exon != null)
812 origxon = new int[exon.length];
813 System.arraycopy(exon, 0, origxon, 0, exon.length);
815 for (int x = 0; x < exon.length && sxpos == -1; x += 2)
817 cdspos += exon[x + 1] - exon[x] + 1;
818 if (cdslength <= cdspos)
820 // advanced beyond last codon.
823 sxstop = exon[x + 1];
824 if (cdslength != cdspos)
827 .println("Truncating final exon interval on region by "
828 + (cdspos - cdslength));
830 // locate the new end boundary of final exon as endxon
831 endxon = exon[x + 1] - cdspos + cdslength;
838 // and trim the exon interval set if necessary
839 int[] nxon = new int[sxpos + 2];
840 System.arraycopy(exon, 0, nxon, 0, sxpos + 2);
841 nxon[sxpos + 1] = endxon; // update the end boundary for the new exon