+ JAXBContext jc = JAXBContext.newInstance("jalview.xml.binding.embl");
+ XMLStreamReader streamReader = XMLInputFactory.newInstance()
+ .createXMLStreamReader(is);
+ javax.xml.bind.Unmarshaller um = jc.createUnmarshaller();
+ JAXBElement<ROOT> rootElement = um.unmarshal(streamReader, ROOT.class);
+ ROOT root = rootElement.getValue();
+
+ /*
+ * document root contains either "entry" or "entrySet"
+ */
+ if (root == null)
+ {
+ return entries;
+ }
+ if (root.getEntrySet() != null)
+ {
+ entries = root.getEntrySet().getEntry();
+ }
+ else if (root.getEntry() != null)
+ {
+ entries.add(root.getEntry());
+ }
+ } catch (JAXBException | XMLStreamException
+ | FactoryConfigurationError e)
+ {
+ e.printStackTrace();
+ }
+ return entries;
+ }
+
+ /**
+ * A helper method to parse XML data and construct a sequence, with any
+ * available database references and features
+ *
+ * @param emprefx
+ * @param entry
+ * @param peptides
+ * @return
+ */
+ SequenceI getSequence(String sourceDb, EntryType entry,
+ List<SequenceI> peptides)
+ {
+ String seqString = entry.getSequence();
+ if (seqString == null)
+ {
+ return null;
+ }
+ seqString = seqString.replace(" ", "").replace("\n", "").replace("\t",
+ "");
+ String accession = entry.getAccession();
+ SequenceI dna = new Sequence(sourceDb + "|" + accession, seqString);
+
+ dna.setDescription(entry.getDescription());
+ String sequenceVersion = String.valueOf(entry.getVersion().intValue());
+ DBRefEntry selfRref = new DBRefEntry(sourceDb, sequenceVersion,
+ accession);
+ dna.addDBRef(selfRref);
+ selfRref.setMap(
+ new Mapping(null, new int[]
+ { 1, dna.getLength() }, new int[] { 1, dna.getLength() }, 1,
+ 1));
+
+ /*
+ * add db references
+ */
+ List<XrefType> xrefs = entry.getXref();
+ if (xrefs != null)
+ {
+ for (XrefType xref : xrefs)
+ {
+ String acc = xref.getId();
+ String source = DBRefUtils.getCanonicalName(xref.getDb());
+ String version = xref.getSecondaryId();
+ if (version == null || "".equals(version))
+ {
+ version = "0";
+ }
+ dna.addDBRef(new DBRefEntry(source, version, acc));
+ }
+ }
+
+ SequenceIdMatcher matcher = new SequenceIdMatcher(peptides);
+ try
+ {
+ List<Feature> features = entry.getFeature();
+ if (features != null)
+ {
+ for (Feature feature : features)
+ {
+ if (FeatureProperties.isCodingFeature(sourceDb,
+ feature.getName()))
+ {
+ parseCodingFeature(entry, feature, sourceDb, dna, peptides,
+ matcher);
+ }
+ }
+ }
+ } catch (Exception e)
+ {
+ System.err.println("EMBL Record Features parsing error!");
+ System.err
+ .println("Please report the following to help@jalview.org :");
+ System.err.println("EMBL Record " + accession);
+ System.err.println("Resulted in exception: " + e.getMessage());
+ e.printStackTrace(System.err);
+ }
+
+ return dna;
+ }
+
+ /**
+ * Extracts coding region and product from a CDS feature and decorates it with
+ * annotations
+ *
+ * @param entry
+ * @param feature
+ * @param sourceDb
+ * @param dna
+ * @param peptides
+ * @param matcher
+ */
+ void parseCodingFeature(EntryType entry, Feature feature, String sourceDb,
+ SequenceI dna, List<SequenceI> peptides,
+ SequenceIdMatcher matcher)
+ {
+ final boolean isEmblCdna = sourceDb.equals(DBRefSource.EMBLCDS);
+ final String accession = entry.getAccession();
+ final String sequenceVersion = entry.getVersion().toString();
+
+ int[] exons = getCdsRanges(entry.getAccession(), feature);
+
+ String translation = null;
+ String proteinName = "";
+ String proteinId = null;
+ Map<String, String> vals = new Hashtable<>();
+
+ /*
+ * codon_start 1/2/3 in EMBL corresponds to phase 0/1/2 in CDS
+ * (phase is required for CDS features in GFF3 format)
+ */
+ int codonStart = 1;
+
+ /*
+ * parse qualifiers, saving protein translation, protein id,
+ * codon start position, product (name), and 'other values'
+ */
+ if (feature.getQualifier() != null)
+ {
+ for (Qualifier q : feature.getQualifier())
+ {
+ String qname = q.getName();
+ String value = q.getValue();
+ value = value == null ? ""
+ : value.trim().replace(" ", "").replace("\n", "")
+ .replace("\t", "");
+ if (qname.equals("translation"))
+ {
+ translation = value;
+ }
+ else if (qname.equals("protein_id"))
+ {
+ proteinId = value;
+ }
+ else if (qname.equals("codon_start"))
+ {
+ try
+ {
+ codonStart = Integer.parseInt(value.trim());
+ } catch (NumberFormatException e)
+ {
+ System.err.println("Invalid codon_start in XML for "
+ + entry.getAccession() + ": " + e.getMessage());
+ }
+ }
+ else if (qname.equals("product"))
+ {
+ // sometimes name is returned e.g. for V00488
+ proteinName = value;
+ }
+ else
+ {
+ // throw anything else into the additional properties hash
+ if (!"".equals(value))
+ {
+ vals.put(qname, value);
+ }
+ }
+ }
+ }
+
+ DBRefEntry proteinToEmblProteinRef = null;
+ exons = MappingUtils.removeStartPositions(codonStart - 1, exons);
+
+ SequenceI product = null;
+ Mapping dnaToProteinMapping = null;
+ if (translation != null && proteinName != null && proteinId != null)
+ {
+ int translationLength = translation.length();
+
+ /*
+ * look for product in peptides list, if not found, add it
+ */
+ product = matcher.findIdMatch(proteinId);
+ if (product == null)
+ {
+ product = new Sequence(proteinId, translation, 1,
+ translationLength);
+ product.setDescription(((proteinName.length() == 0)
+ ? "Protein Product from " + sourceDb
+ : proteinName));
+ peptides.add(product);
+ matcher.add(product);
+ }
+
+ // we have everything - create the mapping and perhaps the protein
+ // sequence
+ if (exons == null || exons.length == 0)