2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2b1)
3 * Copyright (C) 2014 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 java.util.Enumeration;
24 import java.util.Hashtable;
25 import java.util.Iterator;
26 import java.util.Vector;
28 import jalview.datamodel.DBRefEntry;
29 import jalview.datamodel.DBRefSource;
30 import jalview.datamodel.FeatureProperties;
31 import jalview.datamodel.Mapping;
32 import jalview.datamodel.Sequence;
33 import jalview.datamodel.SequenceFeature;
34 import jalview.datamodel.SequenceI;
37 * Data model for one entry returned from an EMBL query, as marshalled by a
40 * For example: http://www.ebi.ac.uk/Tools/dbfetch/dbfetch/embl/x53828/emblxml
42 * @see embl_mapping.xml
44 public class EmblEntry
60 Vector<String> keywords;
62 Vector<DBRefEntry> dbRefs;
64 Vector<EmblFeature> features;
66 EmblSequence sequence;
69 * @return the accession
71 public String getAccession()
78 * the accession to set
80 public void setAccession(String accession)
82 this.accession = accession;
88 public Vector<DBRefEntry> getDbRefs()
97 public void setDbRefs(Vector<DBRefEntry> dbRefs)
105 public String getDesc()
114 public void setDesc(String desc)
120 * @return the features
122 public Vector<EmblFeature> getFeatures()
129 * the features to set
131 public void setFeatures(Vector<EmblFeature> features)
133 this.features = features;
137 * @return the keywords
139 public Vector<String> getKeywords()
146 * the keywords to set
148 public void setKeywords(Vector<String> keywords)
150 this.keywords = keywords;
154 * @return the lastUpdated
156 public String getLastUpdated()
163 * the lastUpdated to set
165 public void setLastUpdated(String lastUpdated)
167 this.lastUpdated = lastUpdated;
171 * @return the releaseCreated
173 public String getRCreated()
179 * @param releaseCreated
180 * the releaseCreated to set
182 public void setRCreated(String releaseCreated)
184 this.rCreated = releaseCreated;
188 * @return the releaseLastUpdated
190 public String getRLastUpdated()
196 * @param releaseLastUpdated
197 * the releaseLastUpdated to set
199 public void setRLastUpdated(String releaseLastUpdated)
201 this.rLastUpdated = releaseLastUpdated;
205 * @return the sequence
207 public EmblSequence getSequence()
214 * the sequence to set
216 public void setSequence(EmblSequence sequence)
218 this.sequence = sequence;
222 * @return the taxDivision
224 public String getTaxDivision()
231 * the taxDivision to set
233 public void setTaxDivision(String taxDivision)
235 this.taxDivision = taxDivision;
239 * @return the version
241 public String getVersion()
250 public void setVersion(String version)
252 this.version = version;
256 * EMBL Feature support is limited. The text below is included for the benefit
257 * of any developer working on improving EMBL feature import in Jalview.
258 * Extract from EMBL feature specification see
259 * http://www.embl-ebi.ac.uk/embl/Documentation
260 * /FT_definitions/feature_table.html 3.5 Location 3.5.1 Purpose
262 * The location indicates the region of the presented sequence which
263 * corresponds to a feature.
265 * 3.5.2 Format and conventions The location contains at least one sequence
266 * location descriptor and may contain one or more operators with one or more
267 * sequence location descriptors. Base numbers refer to the numbering in the
268 * entry. This numbering designates the first base (5' end) of the presented
269 * sequence as base 1. Base locations beyond the range of the presented
270 * sequence may not be used in location descriptors, the only exception being
271 * location in a remote entry (see 3.5.2.1, e).
273 * Location operators and descriptors are discussed in more detail below.
275 * 3.5.2.1 Location descriptors
277 * The location descriptor can be one of the following: (a) a single base
278 * number (b) a site between two indicated adjoining bases (c) a single base
279 * chosen from within a specified range of bases (not allowed for new entries)
280 * (d) the base numbers delimiting a sequence span (e) a remote entry
281 * identifier followed by a local location descriptor (i.e., a-d)
283 * A site between two adjoining nucleotides, such as endonucleolytic cleavage
284 * site, is indicated by listing the two points separated by a carat (^). The
285 * permitted formats for this descriptor are n^n+1 (for example 55^56), or,
286 * for circular molecules, n^1, where "n" is the full length of the molecule,
287 * ie 1000^1 for circular molecule with length 1000.
289 * A single base chosen from a range of bases is indicated by the first base
290 * number and the last base number of the range separated by a single period
291 * (e.g., '12.21' indicates a single base taken from between the indicated
292 * points). From October 2006 the usage of this descriptor is restricted : it
293 * is illegal to use "a single base from a range" (c) either on its own or in
294 * combination with the "sequence span" (d) descriptor for newly created
295 * entries. The existing entries where such descriptors exist are going to be
298 * Sequence spans are indicated by the starting base number and the ending
299 * base number separated by two periods (e.g., '34..456'). The '<' and '>'
300 * symbols may be used with the starting and ending base numbers to indicate
301 * that an end point is beyond the specified base number. The starting and
302 * ending base positions can be represented as distinct base numbers
303 * ('34..456') or a site between two indicated adjoining bases.
305 * A location in a remote entry (not the entry to which the feature table
306 * belongs) can be specified by giving the accession-number and sequence
307 * version of the remote entry, followed by a colon ":", followed by a
308 * location descriptor which applies to that entry's sequence (i.e.
309 * J12345.1:1..15, see also examples below)
313 * The location operator is a prefix that specifies what must be done to the
314 * indicated sequence to find or construct the location corresponding to the
315 * feature. A list of operators is given below with their definitions and most
318 * complement(location) Find the complement of the presented sequence in the
319 * span specified by " location" (i.e., read the complement of the presented
320 * strand in its 5'-to-3' direction)
322 * join(location,location, ... location) The indicated elements should be
323 * joined (placed end-to-end) to form one contiguous sequence
325 * order(location,location, ... location) The elements can be found in the
326 * specified order (5' to 3' direction), but nothing is implied about the
327 * reasonableness about joining them
329 * Note : location operator "complement" can be used in combination with
330 * either " join" or "order" within the same location; combinations of "join"
331 * and "order" within the same location (nested operators) are illegal.
335 * 3.5.3 Location examples
337 * The following is a list of common location descriptors with their meanings:
339 * Location Description
341 * 467 Points to a single base in the presented sequence
343 * 340..565 Points to a continuous range of bases bounded by and including the
344 * starting and ending bases
346 * <345..500 Indicates that the exact lower boundary point of a feature is
347 * unknown. The location begins at some base previous to the first base
348 * specified (which need not be contained in the presented sequence) and
349 * continues to and includes the ending base
351 * <1..888 The feature starts before the first sequenced base and continues to
352 * and includes base 888
354 * 1..>888 The feature starts at the first sequenced base and continues beyond
357 * 102.110 Indicates that the exact location is unknown but that it is one of
358 * the bases between bases 102 and 110, inclusive
360 * 123^124 Points to a site between bases 123 and 124
362 * join(12..78,134..202) Regions 12 to 78 and 134 to 202 should be joined to
363 * form one contiguous sequence
366 * complement(34..126) Start at the base complementary to 126 and finish at
367 * the base complementary to base 34 (the feature is on the strand
368 * complementary to the presented strand)
371 * complement(join(2691..4571,4918..5163)) Joins regions 2691 to 4571 and 4918
372 * to 5163, then complements the joined segments (the feature is on the strand
373 * complementary to the presented strand)
375 * join(complement(4918..5163),complement(2691..4571)) Complements regions
376 * 4918 to 5163 and 2691 to 4571, then joins the complemented segments (the
377 * feature is on the strand complementary to the presented strand)
379 * J00194.1:100..202 Points to bases 100 to 202, inclusive, in the entry (in
380 * this database) with primary accession number 'J00194'
382 * join(1..100,J00194.1:100..202) Joins region 1..100 of the existing entry
383 * with the region 100..202 of remote entry J00194
386 * Recover annotated sequences from EMBL file
389 * don't return nucleic acid sequences
393 * don't return any translated protein sequences marked in features
394 * @return dataset sequences with DBRefs and features - DNA always comes first
396 public jalview.datamodel.SequenceI[] getSequences(boolean noNa,
397 boolean noPeptide, String sourceDb)
398 { // TODO: ensure emblEntry.getSequences behaves correctly for returning all
399 // cases of noNa and noPeptide
400 Vector seqs = new Vector();
404 // In theory we still need to create this if noNa is set to avoid a null
406 dna = new Sequence(sourceDb + "|" + accession, sequence.getSequence());
407 dna.setDescription(desc);
408 DBRefEntry retrievedref = new DBRefEntry(sourceDb, version, accession);
409 dna.addDBRef(retrievedref);
410 // add map to indicate the sequence is a valid coordinate frame for the
412 retrievedref.setMap(new Mapping(null, new int[]
413 { 1, dna.getLength() }, new int[]
414 { 1, dna.getLength() }, 1, 1));
415 // TODO: transform EMBL Database refs to canonical form
417 for (Iterator i = dbRefs.iterator(); i.hasNext(); dna
418 .addDBRef((DBRefEntry) i.next()))
423 for (Iterator i = features.iterator(); i.hasNext();)
425 EmblFeature feature = (EmblFeature) i.next();
428 if (feature.dbRefs != null && feature.dbRefs.size() > 0)
430 for (Iterator dbr = feature.dbRefs.iterator(); dbr.hasNext(); dna
431 .addDBRef((DBRefEntry) dbr.next()))
435 if (FeatureProperties.isCodingFeature(sourceDb, feature.getName()))
437 parseCodingFeature(feature, sourceDb, seqs, dna, noPeptide);
441 // General feature type.
444 if (feature.dbRefs != null && feature.dbRefs.size() > 0)
446 for (Iterator dbr = feature.dbRefs.iterator(); dbr.hasNext(); dna
447 .addDBRef((DBRefEntry) dbr.next()))
453 } catch (Exception e)
455 System.err.println("EMBL Record Features parsing error!");
457 .println("Please report the following to help@jalview.org :");
458 System.err.println("EMBL Record " + accession);
459 System.err.println("Resulted in exception: " + e.getMessage());
460 e.printStackTrace(System.err);
462 if (!noNa && dna != null)
466 SequenceI[] sqs = new SequenceI[seqs.size()];
467 for (int i = 0, j = seqs.size(); i < j; i++)
469 sqs[i] = (SequenceI) seqs.elementAt(i);
476 * attempt to extract coding region and product from a feature and properly
477 * decorate it with annotations.
482 * source database for the EMBLXML
484 * place where sequences go
486 * parent dna sequence for this record
488 * flag for generation of Peptide sequence objects
490 private void parseCodingFeature(EmblFeature feature, String sourceDb,
491 Vector seqs, Sequence dna, boolean noPeptide)
493 boolean isEmblCdna = sourceDb.equals(DBRefSource.EMBLCDS);
494 // extract coding region(s)
495 jalview.datamodel.Mapping map = null;
497 if (feature.locations != null && feature.locations.size() > 0)
499 for (Enumeration locs = feature.locations.elements(); locs
502 EmblFeatureLocations loc = (EmblFeatureLocations) locs
504 int[] se = loc.getElementRanges(accession);
511 int[] t = new int[exon.length + se.length];
512 System.arraycopy(exon, 0, t, 0, exon.length);
513 System.arraycopy(se, 0, t, exon.length, se.length);
519 String prname = new String();
521 Hashtable vals = new Hashtable();
524 if (feature.getQualifiers() != null
525 && feature.getQualifiers().size() > 0)
527 for (Iterator quals = feature.getQualifiers().iterator(); quals
530 Qualifier q = (Qualifier) quals.next();
531 if (q.getName().equals("translation"))
533 StringBuffer prsq = new StringBuffer(q.getValues()[0]);
534 int p = prsq.indexOf(" ");
537 prsq.deleteCharAt(p);
538 p = prsq.indexOf(" ", p);
540 prseq = prsq.toString();
544 else if (q.getName().equals("protein_id"))
546 prid = q.getValues()[0];
548 else if (q.getName().equals("codon_start"))
550 prstart = Integer.parseInt(q.getValues()[0]);
552 else if (q.getName().equals("product"))
554 prname = q.getValues()[0];
558 // throw anything else into the additional properties hash
559 String[] s = q.getValues();
560 StringBuffer sb = new StringBuffer();
563 for (int i = 0; i < s.length; i++)
569 vals.put(q.getName(), sb.toString());
573 Sequence product = null;
574 exon = adjustForPrStart(prstart, exon);
576 if (prseq != null && prname != null && prid != null)
579 product = new Sequence(prid, prseq, 1, prseq.length());
580 product.setDescription(((prname.length() == 0) ? "Protein Product from "
585 // Protein is also added to vector of sequences returned
588 // we have everything - create the mapping and perhaps the protein
590 if (exon == null || exon.length == 0)
593 .println("Implementation Notice: EMBLCDS records not properly supported yet - Making up the CDNA region of this sequence... may be incorrect ("
594 + sourceDb + ":" + getAccession() + ")");
595 if (prseq.length() * 3 == (1 - prstart + dna.getSequence().length))
598 .println("Not allowing for additional stop codon at end of cDNA fragment... !");
599 // this might occur for CDS sequences where no features are
602 { dna.getStart() + (prstart - 1), dna.getEnd() };
603 map = new jalview.datamodel.Mapping(product, exon, new int[]
604 { 1, prseq.length() }, 3, 1);
606 if ((prseq.length() + 1) * 3 == (1 - prstart + dna.getSequence().length))
609 .println("Allowing for additional stop codon at end of cDNA fragment... will probably cause an error in VAMSAs!");
611 { dna.getStart() + (prstart - 1), dna.getEnd() - 3 };
612 map = new jalview.datamodel.Mapping(product, exon, new int[]
613 { 1, prseq.length() }, 3, 1);
618 // Trim the exon mapping if necessary - the given product may only be a
619 // fragment of a larger protein. (EMBL:AY043181 is an example)
623 // TODO: Add a DbRef back to the parent EMBL sequence with the exon
625 // if given a dataset reference, search dataset for parent EMBL
626 // sequence if it exists and set its map
627 // make a new feature annotating the coding contig
631 // final product length trunctation check
633 map = new jalview.datamodel.Mapping(product,
634 adjustForProteinLength(prseq.length(), exon), new int[]
635 { 1, prseq.length() }, 3, 1);
636 // reconstruct the EMBLCDS entry
637 // TODO: this is only necessary when there codon annotation is
638 // complete (I think JBPNote)
639 DBRefEntry pcdnaref = new DBRefEntry();
640 pcdnaref.setAccessionId(prid);
641 pcdnaref.setSource(DBRefSource.EMBLCDS);
642 pcdnaref.setVersion(getVersion()); // same as parent EMBL version.
643 jalview.util.MapList mp = new jalview.util.MapList(new int[]
644 { 1, prseq.length() }, new int[]
645 { 1 + (prstart - 1), (prstart - 1) + 3 * prseq.length() }, 1, 3);
646 // { 1 + (prstart - 1) * 3,
647 // 1 + (prstart - 1) * 3 + prseq.length() * 3 - 1 }, new int[]
648 // { 1prstart, prstart + prseq.length() - 1 }, 3, 1);
649 pcdnaref.setMap(new Mapping(mp));
651 product.addDBRef(pcdnaref);
655 // add cds feature to dna seq - this may include the stop codon
656 for (int xint = 0; exon != null && xint < exon.length; xint += 2)
658 SequenceFeature sf = new SequenceFeature();
659 sf.setBegin(exon[xint]);
660 sf.setEnd(exon[xint + 1]);
661 sf.setType(feature.getName());
662 sf.setFeatureGroup(sourceDb);
663 sf.setDescription("Exon " + (1 + (int) (xint / 2))
664 + " for protein '" + prname + "' EMBLCDS:" + prid);
665 sf.setValue(FeatureProperties.EXONPOS, new Integer(1 + xint));
666 sf.setValue(FeatureProperties.EXONPRODUCT, prname);
667 if (vals != null && vals.size() > 0)
669 Enumeration kv = vals.elements();
670 while (kv.hasMoreElements())
672 Object key = kv.nextElement();
674 sf.setValue(key.toString(), vals.get(key));
677 dna.addSequenceFeature(sf);
680 // add dbRefs to sequence
681 if (feature.dbRefs != null && feature.dbRefs.size() > 0)
683 for (Iterator dbr = feature.dbRefs.iterator(); dbr.hasNext();)
685 DBRefEntry ref = (DBRefEntry) dbr.next();
686 ref.setSource(jalview.util.DBRefUtils.getCanonicalName(ref
688 // Hard code the kind of protein product accessions that EMBL cite
689 if (ref.getSource().equals(jalview.datamodel.DBRefSource.UNIPROT))
692 if (map != null && map.getTo() != null)
694 map.getTo().addDBRef(
695 new DBRefEntry(ref.getSource(), ref.getVersion(), ref
696 .getAccessionId())); // don't copy map over.
697 if (map.getTo().getName().indexOf(prid) == 0)
700 jalview.datamodel.DBRefSource.UNIPROT + "|"
701 + ref.getAccessionId());
707 DBRefEntry pref = new DBRefEntry(ref.getSource(),
708 ref.getVersion(), ref.getAccessionId());
709 pref.setMap(null); // reference is direct
710 product.addDBRef(pref);
711 // Add converse mapping reference
714 Mapping pmap = new Mapping(dna, map.getMap().getInverse());
715 pref = new DBRefEntry(sourceDb, getVersion(),
716 this.getAccession());
718 if (map.getTo() != null)
720 map.getTo().addDBRef(pref);
729 private int[] adjustForPrStart(int prstart, int[] exon)
732 int origxon[], sxpos = -1;
733 int sxstart, sxstop; // unnecessary variables used for debugging
734 // first adjust range for codon start attribute
737 origxon = new int[exon.length];
738 System.arraycopy(exon, 0, origxon, 0, exon.length);
740 for (int x = 0; x < exon.length && sxpos == -1; x += 2)
742 cdspos += exon[x + 1] - exon[x] + 1;
743 if (prstart <= cdspos)
747 sxstop = exon[x + 1];
748 // and adjust start boundary of first exon.
749 exon[x] = exon[x + 1] - cdspos + prstart;
756 int[] nxon = new int[exon.length - sxpos];
757 System.arraycopy(exon, sxpos, nxon, 0, exon.length - sxpos);
765 * truncate the last exon interval to the prlength'th codon
771 private int[] adjustForProteinLength(int prlength, int[] exon)
774 int origxon[], sxpos = -1, endxon = 0, cdslength = prlength * 3;
775 int sxstart, sxstop; // unnecessary variables used for debugging
776 // first adjust range for codon start attribute
777 if (prlength >= 1 && exon != null)
779 origxon = new int[exon.length];
780 System.arraycopy(exon, 0, origxon, 0, exon.length);
782 for (int x = 0; x < exon.length && sxpos == -1; x += 2)
784 cdspos += exon[x + 1] - exon[x] + 1;
785 if (cdslength <= cdspos)
787 // advanced beyond last codon.
790 sxstop = exon[x + 1];
791 if (cdslength != cdspos)
794 .println("Truncating final exon interval on region by "
795 + (cdspos - cdslength));
797 // locate the new end boundary of final exon as endxon
798 endxon = exon[x + 1] - cdspos + cdslength;
805 // and trim the exon interval set if necessary
806 int[] nxon = new int[sxpos + 2];
807 System.arraycopy(exon, 0, nxon, 0, sxpos + 2);
808 nxon[sxpos + 1] = endxon; // update the end boundary for the new exon