JAL-3116 Uniprot XML unmarshalled using JAXB not Castor
[jalview.git] / src / jalview / datamodel / xdb / embl / EmblFile.java
index f69011c..8a32c13 100644 (file)
@@ -1,6 +1,6 @@
 /*
- * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2b1)
- * Copyright (C) 2014 The Jalview Authors
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
  * 
  * This file is part of Jalview.
  * 
  */
 package jalview.datamodel.xdb.embl;
 
+import jalview.datamodel.DBRefEntry;
+import jalview.ws.dbsources.Uniprot;
+
 import java.io.File;
 import java.io.FileReader;
+import java.io.PrintWriter;
 import java.io.Reader;
 import java.util.Vector;
 
 import org.exolab.castor.mapping.Mapping;
 import org.exolab.castor.xml.Unmarshaller;
 
+/**
+ * Data model for entries returned from an EMBL query, as marshalled by a Castor
+ * binding file
+ * 
+ * For example: http://www.ebi.ac.uk/Tools/dbfetch/dbfetch/embl/x53828/emblxml
+ * 
+ * @see embl_mapping.xml
+ */
 public class EmblFile
 {
-  Vector entries;
+  Vector<EmblEntry> entries;
+
+  Vector<EmblError> errors;
 
-  Vector errors;
+  String text;
 
   /**
    * @return the entries
    */
-  public Vector getEntries()
+  public Vector<EmblEntry> getEntries()
   {
     return entries;
   }
@@ -46,7 +60,7 @@ public class EmblFile
    * @param entries
    *          the entries to set
    */
-  public void setEntries(Vector entries)
+  public void setEntries(Vector<EmblEntry> entries)
   {
     this.entries = entries;
   }
@@ -54,7 +68,7 @@ public class EmblFile
   /**
    * @return the errors
    */
-  public Vector getErrors()
+  public Vector<EmblError> getErrors()
   {
     return errors;
   }
@@ -63,7 +77,7 @@ public class EmblFile
    * @param errors
    *          the errors to set
    */
-  public void setErrors(Vector errors)
+  public void setErrors(Vector<EmblError> errors)
   {
     this.errors = errors;
   }
@@ -77,7 +91,9 @@ public class EmblFile
   public static EmblFile getEmblFile(File file)
   {
     if (file == null)
+    {
       return null;
+    }
     try
     {
       return EmblFile.getEmblFile(new FileReader(file));
@@ -96,6 +112,7 @@ public class EmblFile
     {
       // 1. Load the mapping information from the file
       Mapping map = new Mapping(record.getClass().getClassLoader());
+
       java.net.URL url = record.getClass().getResource("/embl_mapping.xml");
       map.loadMapping(url);
 
@@ -104,18 +121,22 @@ public class EmblFile
       try
       {
         // uncomment to DEBUG EMBLFile reading
-        if (((String) jalview.bin.Cache.getDefault(
-                jalview.bin.Cache.CASTORLOGLEVEL, "debug"))
+        if (jalview.bin.Cache
+                .getDefault(jalview.bin.Cache.CASTORLOGLEVEL, "debug")
                 .equalsIgnoreCase("DEBUG"))
+        {
           unmar.setDebug(jalview.bin.Cache.log.isDebugEnabled());
+        }
       } catch (Exception e)
       {
       }
-      ;
       unmar.setIgnoreExtraElements(true);
+      unmar.setIgnoreExtraAttributes(true);
       unmar.setMapping(map);
-
+      unmar.setLogWriter(new PrintWriter(System.out));
       record = (EmblFile) unmar.unmarshal(file);
+
+      canonicaliseDbRefs(record);
     } catch (Exception e)
     {
       e.printStackTrace(System.err);
@@ -125,22 +146,58 @@ public class EmblFile
     return record;
   }
 
-  public static void main(String args[])
+  /**
+   * Change blank version to "0" in any DBRefEntry, to ensure consistent
+   * comparison with other DBRefEntry in Jalview
+   * 
+   * @param record
+   * @see Uniprot#getDbVersion
+   */
+  static void canonicaliseDbRefs(EmblFile record)
   {
-    File mf = null;
-    if (args.length == 1)
+    if (record.getEntries() == null)
     {
-      mf = new File(args[0]);
+      return;
     }
-    if (!mf.exists())
+    for (EmblEntry entry : record.getEntries())
     {
-      mf = new File(
-              "C:\\Documents and Settings\\JimP\\workspace-3.2\\Jalview Release\\schemas\\embleRecordV1.1.xml");
+      if (entry.getDbRefs() != null)
+      {
+        for (DBRefEntry dbref : entry.getDbRefs())
+        {
+          if ("".equals(dbref.getVersion()))
+          {
+            dbref.setVersion("0");
+          }
+        }
+      }
+
+      if (entry.getFeatures() != null)
+      {
+        for (EmblFeature feature : entry.getFeatures())
+        {
+          if (feature.getDbRefs() != null)
+          {
+            for (DBRefEntry dbref : feature.getDbRefs())
+            {
+              if ("".equals(dbref.getVersion()))
+              {
+                dbref.setVersion("0");
+              }
+            }
+          }
+        }
+      }
     }
-    EmblFile myfile = EmblFile.getEmblFile(mf);
-    if (myfile != null && myfile.entries != null
-            && myfile.entries.size() > 0)
-      System.out.println(myfile.entries.size() + " Records read. (" + mf
-              + ")");
+  }
+
+  public String getText()
+  {
+    return text;
+  }
+
+  public void setText(String text)
+  {
+    this.text = text;
   }
 }