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.analysis.SequenceIdMatcher;
24 import jalview.bin.Cache;
25 import jalview.datamodel.DBRefEntry;
26 import jalview.datamodel.DBRefSource;
27 import jalview.datamodel.FeatureProperties;
28 import jalview.datamodel.Mapping;
29 import jalview.datamodel.Sequence;
30 import jalview.datamodel.SequenceFeature;
31 import jalview.datamodel.SequenceI;
32 import jalview.util.DBRefUtils;
33 import jalview.util.DnaUtils;
34 import jalview.util.MapList;
35 import jalview.util.MappingUtils;
36 import jalview.util.StringUtils;
38 import java.text.ParseException;
39 import java.util.Arrays;
40 import java.util.Hashtable;
41 import java.util.List;
43 import java.util.Map.Entry;
44 import java.util.Vector;
45 import java.util.regex.Pattern;
48 * Data model for one entry returned from an EMBL query, as marshalled by a
52 * http://www.ebi.ac.uk/Tools/dbfetch/dbfetch?db=ena_sequence&id=J03321
55 * @see embl_mapping.xml
57 public class EmblEntry
59 private static final Pattern SPACE_PATTERN = Pattern.compile(" ");
65 String sequenceVersion;
73 String sequenceLength;
75 String taxonomicDivision;
79 String firstPublicDate;
81 String firstPublicRelease;
83 String lastUpdatedDate;
85 String lastUpdatedRelease;
87 Vector<String> keywords;
89 Vector<DBRefEntry> dbRefs;
91 Vector<EmblFeature> features;
93 EmblSequence sequence;
96 * @return the accession
98 public String getAccession()
105 * the accession to set
107 public void setAccession(String accession)
109 this.accession = accession;
115 public Vector<DBRefEntry> getDbRefs()
124 public void setDbRefs(Vector<DBRefEntry> dbRefs)
126 this.dbRefs = dbRefs;
130 * @return the features
132 public Vector<EmblFeature> getFeatures()
139 * the features to set
141 public void setFeatures(Vector<EmblFeature> features)
143 this.features = features;
147 * @return the keywords
149 public Vector<String> getKeywords()
156 * the keywords to set
158 public void setKeywords(Vector<String> keywords)
160 this.keywords = keywords;
164 * @return the sequence
166 public EmblSequence getSequence()
173 * the sequence to set
175 public void setSequence(EmblSequence sequence)
177 this.sequence = sequence;
181 * Recover annotated sequences from EMBL file
185 * a list of protein products found so far (to add to)
186 * @return dna dataset sequence with DBRefs and features
188 public SequenceI getSequence(String sourceDb, List<SequenceI> peptides)
190 SequenceI dna = makeSequence(sourceDb);
191 dna.setDescription(description);
192 DBRefEntry retrievedref = new DBRefEntry(sourceDb,
193 getSequenceVersion(), accession);
194 dna.addDBRef(retrievedref);
195 dna.setSourceDBRef(retrievedref);
196 // add map to indicate the sequence is a valid coordinate frame for the
198 retrievedref.setMap(new Mapping(null, new int[] { 1, dna.getLength() },
199 new int[] { 1, dna.getLength() }, 1, 1));
203 * transform EMBL Database refs to canonical form
207 for (DBRefEntry dbref : dbRefs)
209 dbref.setSource(DBRefUtils.getCanonicalName(dbref.getSource()));
214 SequenceIdMatcher matcher = new SequenceIdMatcher(peptides);
217 for (EmblFeature feature : features)
219 if (FeatureProperties.isCodingFeature(sourceDb, feature.getName()))
221 parseCodingFeature(feature, sourceDb, dna, peptides, matcher);
224 } catch (Exception e)
226 System.err.println("EMBL Record Features parsing error!");
228 .println("Please report the following to help@jalview.org :");
229 System.err.println("EMBL Record " + accession);
230 System.err.println("Resulted in exception: " + e.getMessage());
231 e.printStackTrace(System.err);
241 SequenceI makeSequence(String sourceDb)
243 SequenceI dna = new Sequence(sourceDb + "|" + accession,
244 sequence.getSequence());
249 * Extracts coding region and product from a CDS feature and properly decorate
250 * it with annotations.
255 * source database for the EMBLXML
257 * parent dna sequence for this record
259 * list of protein product sequences for Embl entry
261 * helper to match xrefs in already retrieved sequences
263 void parseCodingFeature(EmblFeature feature, String sourceDb,
264 SequenceI dna, List<SequenceI> peptides, SequenceIdMatcher matcher)
266 boolean isEmblCdna = sourceDb.equals(DBRefSource.EMBLCDS);
268 int[] exons = getCdsRanges(feature);
270 String translation = null;
271 String proteinName = "";
272 String proteinId = null;
273 Map<String, String> vals = new Hashtable<String, String>();
276 * codon_start 1/2/3 in EMBL corresponds to phase 0/1/2 in CDS
277 * (phase is required for CDS features in GFF3 format)
282 * parse qualifiers, saving protein translation, protein id,
283 * codon start position, product (name), and 'other values'
285 if (feature.getQualifiers() != null)
287 for (Qualifier q : feature.getQualifiers())
289 String qname = q.getName();
290 if (qname.equals("translation"))
292 // remove all spaces (precompiled String.replaceAll(" ", ""))
293 translation = SPACE_PATTERN.matcher(q.getValues()[0]).replaceAll("");
295 else if (qname.equals("protein_id"))
297 proteinId = q.getValues()[0].trim();
299 else if (qname.equals("codon_start"))
303 codonStart = Integer.parseInt(q.getValues()[0].trim());
304 } catch (NumberFormatException e)
306 System.err.println("Invalid codon_start in XML for "
307 + accession + ": " + e.getMessage());
310 else if (qname.equals("product"))
312 // sometimes name is returned e.g. for V00488
313 proteinName = q.getValues()[0].trim();
317 // throw anything else into the additional properties hash
318 String[] qvals = q.getValues();
321 String commaSeparated = StringUtils.arrayToSeparatorList(qvals,
323 vals.put(qname, commaSeparated);
329 DBRefEntry proteinToEmblProteinRef = null;
330 exons = MappingUtils.removeStartPositions(codonStart - 1, exons);
332 SequenceI product = null;
333 Mapping dnaToProteinMapping = null;
334 if (translation != null && proteinName != null && proteinId != null)
336 int translationLength = translation.length();
339 * look for product in peptides list, if not found, add it
341 product = matcher.findIdMatch(proteinId);
344 product = new Sequence(proteinId, translation, 1, translationLength);
345 product.setDescription(((proteinName.length() == 0) ? "Protein Product from "
348 peptides.add(product);
349 matcher.add(product);
352 // we have everything - create the mapping and perhaps the protein
354 if (exons == null || exons.length == 0)
357 * workaround until we handle dna location for CDS sequence
358 * e.g. location="X53828.1:60..1058" correctly
361 .println("Implementation Notice: EMBLCDS records not properly supported yet - Making up the CDNA region of this sequence... may be incorrect ("
362 + sourceDb + ":" + getAccession() + ")");
363 if (translationLength * 3 == (1 - codonStart + dna.getSequence().length))
366 .println("Not allowing for additional stop codon at end of cDNA fragment... !");
367 // this might occur for CDS sequences where no features are marked
368 exons = new int[] { dna.getStart() + (codonStart - 1),
370 dnaToProteinMapping = new Mapping(product, exons, new int[] { 1,
371 translationLength }, 3, 1);
373 if ((translationLength + 1) * 3 == (1 - codonStart + dna
374 .getSequence().length))
377 .println("Allowing for additional stop codon at end of cDNA fragment... will probably cause an error in VAMSAs!");
378 exons = new int[] { dna.getStart() + (codonStart - 1),
380 dnaToProteinMapping = new Mapping(product, exons, new int[] { 1,
381 translationLength }, 3, 1);
386 // Trim the exon mapping if necessary - the given product may only be a
387 // fragment of a larger protein. (EMBL:AY043181 is an example)
391 // TODO: Add a DbRef back to the parent EMBL sequence with the exon
393 // if given a dataset reference, search dataset for parent EMBL
394 // sequence if it exists and set its map
395 // make a new feature annotating the coding contig
399 // final product length truncation check
400 int[] cdsRanges = adjustForProteinLength(translationLength, exons);
401 dnaToProteinMapping = new Mapping(product, cdsRanges, new int[] {
402 1, translationLength }, 3, 1);
406 * make xref with mapping from protein to EMBL dna
408 DBRefEntry proteinToEmblRef = new DBRefEntry(DBRefSource.EMBL,
409 getSequenceVersion(), proteinId, new Mapping(
410 dnaToProteinMapping.getMap().getInverse()));
411 product.addDBRef(proteinToEmblRef);
414 * make xref from protein to EMBLCDS; we assume here that the
415 * CDS sequence version is same as dna sequence (?!)
417 MapList proteinToCdsMapList = new MapList(new int[] { 1,
418 translationLength }, new int[] { 1 + (codonStart - 1),
419 (codonStart - 1) + 3 * translationLength }, 1, 3);
420 DBRefEntry proteinToEmblCdsRef = new DBRefEntry(
421 DBRefSource.EMBLCDS, getSequenceVersion(), proteinId,
422 new Mapping(proteinToCdsMapList));
423 product.addDBRef(proteinToEmblCdsRef);
426 * make 'direct' xref from protein to EMBLCDSPROTEIN
428 proteinToEmblProteinRef = new DBRefEntry(proteinToEmblCdsRef);
429 proteinToEmblProteinRef.setSource(DBRefSource.EMBLCDSProduct);
430 proteinToEmblProteinRef.setMap(null);
431 product.addDBRef(proteinToEmblProteinRef);
437 * add cds features to dna sequence
439 for (int xint = 0; exons != null && xint < exons.length; xint += 2)
441 SequenceFeature sf = makeCdsFeature(exons, xint, proteinName,
442 proteinId, vals, codonStart);
443 sf.setType(feature.getName()); // "CDS"
444 sf.setEnaLocation(feature.getLocation());
445 sf.setFeatureGroup(sourceDb);
446 dna.addSequenceFeature(sf);
451 * add feature dbRefs to sequence, and mappings for Uniprot xrefs
453 boolean hasUniprotDbref = false;
454 if (feature.dbRefs != null)
456 boolean mappingUsed = false;
457 for (DBRefEntry ref : feature.dbRefs)
460 * ensure UniProtKB/Swiss-Prot converted to UNIPROT
462 String source = DBRefUtils.getCanonicalName(ref.getSource());
463 ref.setSource(source);
464 DBRefEntry proteinDbRef = new DBRefEntry(ref.getSource(), ref.getVersion(), ref
466 if (source.equals(DBRefSource.UNIPROT))
468 String proteinSeqName = DBRefSource.UNIPROT + "|"
469 + ref.getAccessionId();
470 if (dnaToProteinMapping != null && dnaToProteinMapping.getTo() != null)
475 * two or more Uniprot xrefs for the same CDS -
476 * each needs a distinct Mapping (as to a different sequence)
478 dnaToProteinMapping = new Mapping(dnaToProteinMapping);
483 * try to locate the protein mapped to (possibly by a
484 * previous CDS feature); if not found, construct it from
485 * the EMBL translation
487 SequenceI proteinSeq = matcher.findIdMatch(proteinSeqName);
488 if (proteinSeq == null)
490 proteinSeq = new Sequence(proteinSeqName,
491 product.getSequenceAsString());
492 matcher.add(proteinSeq);
493 peptides.add(proteinSeq);
495 dnaToProteinMapping.setTo(proteinSeq);
496 dnaToProteinMapping.setMappedFromId(proteinId);
497 proteinSeq.addDBRef(proteinDbRef);
498 proteinSeq.setSourceDBRef(proteinDbRef);
499 ref.setMap(dnaToProteinMapping);
501 hasUniprotDbref = true;
506 * copy feature dbref to our protein product
508 DBRefEntry pref = proteinDbRef;
509 pref.setMap(null); // reference is direct
510 product.addDBRef(pref);
511 // Add converse mapping reference
512 if (dnaToProteinMapping != null)
514 Mapping pmap = new Mapping(dna, dnaToProteinMapping.getMap()
516 pref = new DBRefEntry(sourceDb, getSequenceVersion(),
517 this.getAccession());
519 if (dnaToProteinMapping.getTo() != null)
521 dnaToProteinMapping.getTo().addDBRef(pref);
530 * if we have a product (translation) but no explicit Uniprot dbref
531 * (example: EMBL AAFI02000057 protein_id EAL65544.1)
532 * then construct mappings to an assumed EMBLCDSPROTEIN accession
534 if (!hasUniprotDbref && product != null)
536 if (proteinToEmblProteinRef == null)
538 // assuming CDSPROTEIN sequence version = dna version (?!)
539 proteinToEmblProteinRef = new DBRefEntry(
540 DBRefSource.EMBLCDSProduct, getSequenceVersion(), proteinId);
542 product.addDBRef(proteinToEmblProteinRef);
543 product.setSourceDBRef(proteinToEmblProteinRef);
545 if (dnaToProteinMapping != null
546 && dnaToProteinMapping.getTo() != null)
548 DBRefEntry dnaToEmblProteinRef = new DBRefEntry(
549 DBRefSource.EMBLCDSProduct, getSequenceVersion(), proteinId);
550 dnaToEmblProteinRef.setMap(dnaToProteinMapping);
551 dnaToProteinMapping.setMappedFromId(proteinId);
552 dna.addDBRef(dnaToEmblProteinRef);
558 * Helper method to construct a SequenceFeature for one cds range
561 * array of cds [start, end, ...] positions
562 * @param exonStartIndex
563 * offset into the exons array
565 * @param proteinAccessionId
567 * map of 'miscellaneous values' for feature
569 * codon start position for CDS (1/2/3, normally 1)
572 protected SequenceFeature makeCdsFeature(int[] exons, int exonStartIndex,
573 String proteinName, String proteinAccessionId,
574 Map<String, String> vals, int codonStart)
576 int exonNumber = exonStartIndex / 2 + 1;
577 SequenceFeature sf = new SequenceFeature();
578 sf.setBegin(Math.min(exons[exonStartIndex], exons[exonStartIndex + 1]));
579 sf.setEnd(Math.max(exons[exonStartIndex], exons[exonStartIndex + 1]));
580 sf.setDescription(String.format("Exon %d for protein '%s' EMBLCDS:%s",
581 exonNumber, proteinName, proteinAccessionId));
582 sf.setPhase(String.valueOf(codonStart - 1));
583 sf.setStrand(exons[exonStartIndex] <= exons[exonStartIndex + 1] ? "+"
585 sf.setValue(FeatureProperties.EXONPOS, exonNumber);
586 sf.setValue(FeatureProperties.EXONPRODUCT, proteinName);
589 StringBuilder sb = new StringBuilder();
590 boolean first = true;
591 for (Entry<String, String> val : vals.entrySet())
597 sb.append(val.getKey()).append("=").append(val.getValue());
599 sf.setValue(val.getKey(), val.getValue());
601 sf.setAttributes(sb.toString());
607 * Returns the CDS positions as a single array of [start, end, start, end...]
608 * positions. If on the reverse strand, these will be in descending order.
613 protected int[] getCdsRanges(EmblFeature feature)
615 if (feature.location == null)
622 List<int[]> ranges = DnaUtils.parseLocation(feature.location);
623 return listToArray(ranges);
624 } catch (ParseException e)
626 Cache.log.warn(String.format(
627 "Not parsing inexact CDS location %s in ENA %s",
628 feature.location, this.accession));
634 * Converts a list of [start, end] ranges to a single array of [start, end,
640 int[] listToArray(List<int[]> ranges)
642 int[] result = new int[ranges.size() * 2];
644 for (int[] range : ranges)
646 result[i++] = range[0];
647 result[i++] = range[1];
653 * Truncates (if necessary) the exon intervals to match 3 times the length of
654 * the protein; also accepts 3 bases longer (for stop codon not included in
657 * @param proteinLength
659 * an array of [start, end, start, end...] intervals
660 * @return the same array (if unchanged) or a truncated copy
662 static int[] adjustForProteinLength(int proteinLength, int[] exon)
664 if (proteinLength <= 0 || exon == null)
668 int expectedCdsLength = proteinLength * 3;
669 int exonLength = MappingUtils.getLength(Arrays.asList(exon));
672 * if exon length matches protein, or is shorter, or longer by the
673 * length of a stop codon (3 bases), then leave it unchanged
675 if (expectedCdsLength >= exonLength
676 || expectedCdsLength == exonLength - 3)
684 origxon = new int[exon.length];
685 System.arraycopy(exon, 0, origxon, 0, exon.length);
687 for (int x = 0; x < exon.length; x += 2)
689 cdspos += Math.abs(exon[x + 1] - exon[x]) + 1;
690 if (expectedCdsLength <= cdspos)
692 // advanced beyond last codon.
694 if (expectedCdsLength != cdspos)
697 // .println("Truncating final exon interval on region by "
698 // + (cdspos - cdslength));
702 * shrink the final exon - reduce end position if forward
703 * strand, increase it if reverse
705 if (exon[x + 1] >= exon[x])
707 endxon = exon[x + 1] - cdspos + expectedCdsLength;
711 endxon = exon[x + 1] + cdspos - expectedCdsLength;
719 // and trim the exon interval set if necessary
720 int[] nxon = new int[sxpos + 2];
721 System.arraycopy(exon, 0, nxon, 0, sxpos + 2);
722 nxon[sxpos + 1] = endxon; // update the end boundary for the new exon
729 public String getSequenceVersion()
731 return sequenceVersion;
734 public void setSequenceVersion(String sequenceVersion)
736 this.sequenceVersion = sequenceVersion;
739 public String getSequenceLength()
741 return sequenceLength;
744 public void setSequenceLength(String sequenceLength)
746 this.sequenceLength = sequenceLength;
749 public String getEntryVersion()
754 public void setEntryVersion(String entryVersion)
756 this.entryVersion = entryVersion;
759 public String getMoleculeType()
764 public void setMoleculeType(String moleculeType)
766 this.moleculeType = moleculeType;
769 public String getTopology()
774 public void setTopology(String topology)
776 this.topology = topology;
779 public String getTaxonomicDivision()
781 return taxonomicDivision;
784 public void setTaxonomicDivision(String taxonomicDivision)
786 this.taxonomicDivision = taxonomicDivision;
789 public String getDescription()
794 public void setDescription(String description)
796 this.description = description;
799 public String getFirstPublicDate()
801 return firstPublicDate;
804 public void setFirstPublicDate(String firstPublicDate)
806 this.firstPublicDate = firstPublicDate;
809 public String getFirstPublicRelease()
811 return firstPublicRelease;
814 public void setFirstPublicRelease(String firstPublicRelease)
816 this.firstPublicRelease = firstPublicRelease;
819 public String getLastUpdatedDate()
821 return lastUpdatedDate;
824 public void setLastUpdatedDate(String lastUpdatedDate)
826 this.lastUpdatedDate = lastUpdatedDate;
829 public String getLastUpdatedRelease()
831 return lastUpdatedRelease;
834 public void setLastUpdatedRelease(String lastUpdatedRelease)
836 this.lastUpdatedRelease = lastUpdatedRelease;
839 public String getDataClass()
844 public void setDataClass(String dataClass)
846 this.dataClass = dataClass;