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.datamodel.DBRefEntry;
25 import jalview.datamodel.DBRefSource;
26 import jalview.datamodel.FeatureProperties;
27 import jalview.datamodel.Mapping;
28 import jalview.datamodel.Sequence;
29 import jalview.datamodel.SequenceFeature;
30 import jalview.datamodel.SequenceI;
31 import jalview.util.DBRefUtils;
32 import jalview.util.DnaUtils;
33 import jalview.util.MapList;
34 import jalview.util.MappingUtils;
35 import jalview.util.StringUtils;
37 import java.util.Arrays;
38 import java.util.Hashtable;
39 import java.util.List;
41 import java.util.Map.Entry;
42 import java.util.Vector;
43 import java.util.regex.Pattern;
46 * Data model for one entry returned from an EMBL query, as marshalled by a
50 * http://www.ebi.ac.uk/Tools/dbfetch/dbfetch?db=ena_sequence&id=J03321
53 * @see embl_mapping.xml
55 public class EmblEntry
57 private static final Pattern SPACE_PATTERN = Pattern.compile(" ");
63 String sequenceVersion;
71 String sequenceLength;
73 String taxonomicDivision;
77 String firstPublicDate;
79 String firstPublicRelease;
81 String lastUpdatedDate;
83 String lastUpdatedRelease;
85 Vector<String> keywords;
87 Vector<DBRefEntry> dbRefs;
89 Vector<EmblFeature> features;
91 EmblSequence sequence;
94 * @return the accession
96 public String getAccession()
103 * the accession to set
105 public void setAccession(String accession)
107 this.accession = accession;
113 public Vector<DBRefEntry> getDbRefs()
122 public void setDbRefs(Vector<DBRefEntry> dbRefs)
124 this.dbRefs = dbRefs;
128 * @return the features
130 public Vector<EmblFeature> getFeatures()
137 * the features to set
139 public void setFeatures(Vector<EmblFeature> features)
141 this.features = features;
145 * @return the keywords
147 public Vector<String> getKeywords()
154 * the keywords to set
156 public void setKeywords(Vector<String> keywords)
158 this.keywords = keywords;
162 * @return the sequence
164 public EmblSequence getSequence()
171 * the sequence to set
173 public void setSequence(EmblSequence sequence)
175 this.sequence = sequence;
179 * Recover annotated sequences from EMBL file
183 * a list of protein products found so far (to add to)
184 * @return dna dataset sequence with DBRefs and features
186 public SequenceI getSequence(String sourceDb, List<SequenceI> peptides)
188 SequenceI dna = new Sequence(sourceDb + "|" + accession,
189 sequence.getSequence());
190 dna.setDescription(description);
191 DBRefEntry retrievedref = new DBRefEntry(sourceDb,
192 getSequenceVersion(), accession);
193 dna.addDBRef(retrievedref);
194 // add map to indicate the sequence is a valid coordinate frame for the
196 retrievedref.setMap(new Mapping(null, new int[] { 1, dna.getLength() },
197 new int[] { 1, dna.getLength() }, 1, 1));
198 // TODO: transform EMBL Database refs to canonical form
201 for (DBRefEntry dbref : dbRefs)
203 dbref.setSource(DBRefUtils.getCanonicalName(dbref.getSource()));
210 for (EmblFeature feature : features)
212 if (feature.dbRefs != null)
214 for (DBRefEntry dbref : feature.dbRefs)
217 * convert UniProtKB/Swiss-Prot to UNIPROT
219 dbref.setSource(DBRefUtils.getCanonicalName(dbref.getSource()));
223 if (FeatureProperties.isCodingFeature(sourceDb, feature.getName()))
225 parseCodingFeature(feature, sourceDb, dna, peptides);
228 } catch (Exception e)
230 System.err.println("EMBL Record Features parsing error!");
232 .println("Please report the following to help@jalview.org :");
233 System.err.println("EMBL Record " + accession);
234 System.err.println("Resulted in exception: " + e.getMessage());
235 e.printStackTrace(System.err);
242 * Extracts coding region and product from a CDS feature and properly decorate
243 * it with annotations.
248 * source database for the EMBLXML
250 * parent dna sequence for this record
252 * list of protein product sequences for Embl entry
254 void parseCodingFeature(EmblFeature feature, String sourceDb,
255 SequenceI dna, List<SequenceI> peptides)
257 boolean isEmblCdna = sourceDb.equals(DBRefSource.EMBLCDS);
259 int[] exon = getCdsRanges(feature);
264 Map<String, String> vals = new Hashtable<String, String>();
265 SequenceIdMatcher matcher = new SequenceIdMatcher(peptides);
268 * codon_start 1/2/3 in EMBL corresponds to phase 0/1/2 in CDS
269 * (phase is required for CDS features in GFF3 format)
274 * parse qualifiers, saving protein translation, protein id,
275 * codon start position, product (name), and 'other values'
277 if (feature.getQualifiers() != null)
279 for (Qualifier q : feature.getQualifiers())
281 String qname = q.getName();
282 if (qname.equals("translation"))
284 // remove all spaces (precompiled String.replaceAll(" ", ""))
285 prseq = SPACE_PATTERN.matcher(q.getValues()[0]).replaceAll("");
287 else if (qname.equals("protein_id"))
289 prid = q.getValues()[0];
291 else if (qname.equals("codon_start"))
295 codonStart = Integer.parseInt(q.getValues()[0]);
296 } catch (NumberFormatException e)
298 System.err.println("Invalid codon_start in XML for "
299 + accession + ": " + e.getMessage());
302 else if (qname.equals("product"))
304 // sometimes name is returned e.g. for V00488
305 prname = q.getValues()[0];
309 // throw anything else into the additional properties hash
310 String[] qvals = q.getValues();
313 String commaSeparated = StringUtils.arrayToSeparatorList(qvals,
315 vals.put(qname, commaSeparated);
321 DBRefEntry protEMBLCDS = null;
322 exon = MappingUtils.removeStartPositions(codonStart - 1, exon);
323 boolean noProteinDbref = true;
325 SequenceI product = null;
327 if (prseq != null && prname != null && prid != null)
330 * look for product in peptides list, if not found, add it
332 product = matcher.findIdMatch(prid);
335 product = new Sequence(prid, prseq, 1, prseq.length());
336 product.setDescription(((prname.length() == 0) ? "Protein Product from "
339 peptides.add(product);
340 matcher.add(product);
343 // we have everything - create the mapping and perhaps the protein
345 if (exon == null || exon.length == 0)
348 .println("Implementation Notice: EMBLCDS records not properly supported yet - Making up the CDNA region of this sequence... may be incorrect ("
349 + sourceDb + ":" + getAccession() + ")");
350 if (prseq.length() * 3 == (1 - codonStart + dna.getSequence().length))
353 .println("Not allowing for additional stop codon at end of cDNA fragment... !");
354 // this might occur for CDS sequences where no features are
356 exon = new int[] { dna.getStart() + (codonStart - 1),
358 map = new Mapping(product, exon, new int[] { 1, prseq.length() },
361 if ((prseq.length() + 1) * 3 == (1 - codonStart + dna.getSequence().length))
364 .println("Allowing for additional stop codon at end of cDNA fragment... will probably cause an error in VAMSAs!");
365 exon = new int[] { dna.getStart() + (codonStart - 1),
367 map = new Mapping(product, exon, new int[] { 1, prseq.length() },
373 // Trim the exon mapping if necessary - the given product may only be a
374 // fragment of a larger protein. (EMBL:AY043181 is an example)
378 // TODO: Add a DbRef back to the parent EMBL sequence with the exon
380 // if given a dataset reference, search dataset for parent EMBL
381 // sequence if it exists and set its map
382 // make a new feature annotating the coding contig
386 // final product length truncation check
387 // TODO should from range include stop codon even if not in protein
388 // in order to include stop codon in CDS sequence (as done for
390 int[] cdsRanges = adjustForProteinLength(prseq.length(), exon);
391 map = new Mapping(product, cdsRanges, new int[] { 1,
392 prseq.length() }, 3, 1);
393 // reconstruct the EMBLCDS entry
394 // TODO: this is only necessary when there codon annotation is
395 // complete (I think JBPNote)
396 DBRefEntry pcdnaref = new DBRefEntry();
397 pcdnaref.setAccessionId(prid);
398 pcdnaref.setSource(DBRefSource.EMBLCDS);
399 pcdnaref.setVersion(getSequenceVersion()); // same as parent EMBL
401 MapList mp = new MapList(new int[] { 1, prseq.length() },
402 new int[] { 1 + (codonStart - 1),
403 (codonStart - 1) + 3 * prseq.length() }, 1, 3);
404 pcdnaref.setMap(new Mapping(mp));
407 product.addDBRef(pcdnaref);
408 protEMBLCDS = new DBRefEntry(pcdnaref);
409 protEMBLCDS.setSource(DBRefSource.EMBLCDSProduct);
410 product.addDBRef(protEMBLCDS);
414 // add cds feature to dna seq - this may include the stop codon
415 for (int xint = 0; exon != null && xint < exon.length; xint += 2)
417 SequenceFeature sf = makeCdsFeature(exon, xint, prname, prid, vals,
419 sf.setType(feature.getName()); // "CDS"
420 sf.setEnaLocation(feature.getLocation());
421 sf.setFeatureGroup(sourceDb);
422 dna.addSequenceFeature(sf);
427 * add mappings for Uniprot xrefs
429 if (feature.dbRefs != null)
431 boolean mappingUsed = false;
432 for (DBRefEntry ref : feature.dbRefs)
434 if (ref.getSource().equals(DBRefSource.UNIPROT))
436 String proteinSeqName = DBRefSource.UNIPROT + "|"
437 + ref.getAccessionId();
438 if (map != null && map.getTo() != null)
443 * two or more Uniprot xrefs for the same CDS -
444 * each needs a distinct Mapping (as to a different sequence)
446 map = new Mapping(map);
451 * try to locate the protein mapped to (possibly by a
452 * previous CDS feature)
454 SequenceI proteinSeq = matcher.findIdMatch(proteinSeqName);
455 if (proteinSeq == null)
457 proteinSeq = new Sequence(proteinSeqName,
458 product.getSequenceAsString());
459 matcher.add(proteinSeq);
460 peptides.add(proteinSeq);
462 map.setTo(proteinSeq);
463 map.getTo().addDBRef(
464 new DBRefEntry(ref.getSource(), ref.getVersion(), ref
468 noProteinDbref = false;
472 DBRefEntry pref = new DBRefEntry(ref.getSource(),
473 ref.getVersion(), ref.getAccessionId());
474 pref.setMap(null); // reference is direct
475 product.addDBRef(pref);
476 // Add converse mapping reference
479 Mapping pmap = new Mapping(dna, map.getMap().getInverse());
480 pref = new DBRefEntry(sourceDb, getSequenceVersion(),
481 this.getAccession());
483 if (map.getTo() != null)
485 map.getTo().addDBRef(pref);
490 if (noProteinDbref && product != null)
492 // add protein coding reference to dna sequence so xref matches
493 if (protEMBLCDS == null)
495 protEMBLCDS = new DBRefEntry();
496 protEMBLCDS.setAccessionId(prid);
497 protEMBLCDS.setSource(DBRefSource.EMBLCDSProduct);
498 protEMBLCDS.setVersion(getSequenceVersion());
500 .setMap(new Mapping(product, map.getMap().getInverse()));
502 product.addDBRef(protEMBLCDS);
504 // Add converse mapping reference
507 Mapping pmap = new Mapping(product, protEMBLCDS.getMap().getMap()
509 DBRefEntry ncMap = new DBRefEntry(protEMBLCDS);
511 if (map.getTo() != null)
521 * Helper method to construct a SequenceFeature for one cds range
524 * array of cds [start, end, ...] positions
525 * @param exonStartIndex
526 * offset into the exons array
528 * @param proteinAccessionId
530 * map of 'miscellaneous values' for feature
532 * codon start position for CDS (1/2/3, normally 1)
535 protected SequenceFeature makeCdsFeature(int[] exons, int exonStartIndex,
536 String proteinName, String proteinAccessionId,
537 Map<String, String> vals, int codonStart)
539 int exonNumber = exonStartIndex / 2 + 1;
540 SequenceFeature sf = new SequenceFeature();
541 sf.setBegin(Math.min(exons[exonStartIndex], exons[exonStartIndex + 1]));
542 sf.setEnd(Math.max(exons[exonStartIndex], exons[exonStartIndex + 1]));
543 sf.setDescription(String.format("Exon %d for protein '%s' EMBLCDS:%s",
544 exonNumber, proteinName, proteinAccessionId));
545 sf.setPhase(String.valueOf(codonStart - 1));
546 sf.setStrand(exons[exonStartIndex] <= exons[exonStartIndex + 1] ? "+"
548 sf.setValue(FeatureProperties.EXONPOS, exonNumber);
549 sf.setValue(FeatureProperties.EXONPRODUCT, proteinName);
552 StringBuilder sb = new StringBuilder();
553 boolean first = true;
554 for (Entry<String, String> val : vals.entrySet())
560 sb.append(val.getKey()).append("=").append(val.getValue());
562 sf.setValue(val.getKey(), val.getValue());
564 sf.setAttributes(sb.toString());
570 * Returns the CDS positions as a list of [start, end, start, end...]
571 * positions. If on the reverse strand, these will be in descending order.
576 protected int[] getCdsRanges(EmblFeature feature)
578 if (feature.location == null)
582 List<int[]> ranges = DnaUtils.parseLocation(feature.location);
583 return ranges == null ? new int[] {} : listToArray(ranges);
587 * Converts a list of [start, end] ranges to a single array of [start, end,
593 int[] listToArray(List<int[]> ranges)
595 int[] result = new int[ranges.size() * 2];
597 for (int[] range : ranges)
599 result[i++] = range[0];
600 result[i++] = range[1];
606 * truncate the last exon interval to the prlength'th codon
612 static int[] adjustForProteinLength(int prlength, int[] exon)
614 if (prlength <= 0 || exon == null)
618 int desiredCdsLength = prlength * 3;
619 int exonLength = MappingUtils.getLength(Arrays.asList(exon));
622 * assuming here exon might include stop codon in addition to protein codons
624 if (desiredCdsLength == exonLength
625 || desiredCdsLength == exonLength - 3)
633 origxon = new int[exon.length];
634 System.arraycopy(exon, 0, origxon, 0, exon.length);
636 for (int x = 0; x < exon.length; x += 2)
638 cdspos += Math.abs(exon[x + 1] - exon[x]) + 1;
639 if (desiredCdsLength <= cdspos)
641 // advanced beyond last codon.
643 if (desiredCdsLength != cdspos)
646 // .println("Truncating final exon interval on region by "
647 // + (cdspos - cdslength));
651 * shrink the final exon - reduce end position if forward
652 * strand, increase it if reverse
654 if (exon[x + 1] >= exon[x])
656 endxon = exon[x + 1] - cdspos + desiredCdsLength;
660 endxon = exon[x + 1] + cdspos - desiredCdsLength;
668 // and trim the exon interval set if necessary
669 int[] nxon = new int[sxpos + 2];
670 System.arraycopy(exon, 0, nxon, 0, sxpos + 2);
671 nxon[sxpos + 1] = endxon; // update the end boundary for the new exon
678 public String getSequenceVersion()
680 return sequenceVersion;
683 public void setSequenceVersion(String sequenceVersion)
685 this.sequenceVersion = sequenceVersion;
688 public String getSequenceLength()
690 return sequenceLength;
693 public void setSequenceLength(String sequenceLength)
695 this.sequenceLength = sequenceLength;
698 public String getEntryVersion()
703 public void setEntryVersion(String entryVersion)
705 this.entryVersion = entryVersion;
708 public String getMoleculeType()
713 public void setMoleculeType(String moleculeType)
715 this.moleculeType = moleculeType;
718 public String getTopology()
723 public void setTopology(String topology)
725 this.topology = topology;
728 public String getTaxonomicDivision()
730 return taxonomicDivision;
733 public void setTaxonomicDivision(String taxonomicDivision)
735 this.taxonomicDivision = taxonomicDivision;
738 public String getDescription()
743 public void setDescription(String description)
745 this.description = description;
748 public String getFirstPublicDate()
750 return firstPublicDate;
753 public void setFirstPublicDate(String firstPublicDate)
755 this.firstPublicDate = firstPublicDate;
758 public String getFirstPublicRelease()
760 return firstPublicRelease;
763 public void setFirstPublicRelease(String firstPublicRelease)
765 this.firstPublicRelease = firstPublicRelease;
768 public String getLastUpdatedDate()
770 return lastUpdatedDate;
773 public void setLastUpdatedDate(String lastUpdatedDate)
775 this.lastUpdatedDate = lastUpdatedDate;
778 public String getLastUpdatedRelease()
780 return lastUpdatedRelease;
783 public void setLastUpdatedRelease(String lastUpdatedRelease)
785 this.lastUpdatedRelease = lastUpdatedRelease;
788 public String getDataClass()
793 public void setDataClass(String dataClass)
795 this.dataClass = dataClass;