JAL-3956 - PDBEntry objects constructed from 3D-Beacons have authoritative IDs overri...
[jalview.git] / src / jalview / datamodel / PDBEntry.java
index 39d491b..741ef07 100755 (executable)
@@ -1,6 +1,6 @@
 /*
- * Jalview - A Sequence Alignment Editor and Viewer (Version 2.9.0b2)
- * Copyright (C) 2015 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.
  * 
@@ -22,12 +22,31 @@ package jalview.datamodel;
 
 import jalview.util.CaseInsensitiveString;
 
+import java.util.Collections;
+import java.util.Enumeration;
 import java.util.Hashtable;
 
 public class PDBEntry
 {
+
+  /**
+   * constant for storing chain code in properties table
+   */
+  private static final String CHAIN_ID = "chain_code";
+
+  private Hashtable<String, Object> properties;
+
   private static final int PDB_ID_LENGTH = 4;
 
+  /**
+   * property set when id is a 'manufactured' identifier from the structure data's filename
+   */
+  private static final String FAKED_ID = "faked_pdbid";
+  /**
+   * property set when the id is authoritative, and should be used in preference to any identifiers in the structure data
+   */
+  private static final String AUTHORITATIVE_ID = "authoritative_pdbid";
+
   private String file;
 
   private String type;
@@ -36,7 +55,39 @@ public class PDBEntry
 
   public enum Type
   {
-    PDB, MMCIF, FILE;
+    // TODO is FILE needed; if not is this enum needed, or can we
+    // use FileFormatI for PDB, MMCIF?
+    PDB("pdb", "pdb"), MMCIF("mmcif", "cif"), BCIF("bcif","bcif"),FILE("?", "?");
+
+    /*
+     * file extension for cached structure file; must be one that
+     * is recognised by Chimera 'open' command
+     * @see https://www.cgl.ucsf.edu/chimera/current/docs/UsersGuide/filetypes.html
+     */
+    String ext;
+
+    /*
+     * format specifier used in dbfetch request
+     * @see http://www.ebi.ac.uk/Tools/dbfetch/dbfetch/dbfetch.databases#pdb
+     */
+    String format;
+
+    private Type(String fmt, String ex)
+    {
+      format = fmt;
+      ext = ex;
+    }
+
+    public String getFormat()
+    {
+      return format;
+    }
+
+    public String getExtension()
+    {
+      return ext;
+    }
+
     /**
      * case insensitive matching for Type enum
      * 
@@ -68,13 +119,6 @@ public class PDBEntry
   }
 
   /**
-   * constant for storing chain code in properties table
-   */
-  private static final String CHAIN_ID = "chain_code";
-
-  Hashtable properties;
-
-  /**
    * Answers true if obj is a PDBEntry with the same id and chain code (both
    * ignoring case), file, type and properties
    */
@@ -117,17 +161,6 @@ public class PDBEntry
   {
   }
 
-  /**
-   * Constructor given file path and PDB id.
-   * 
-   * @param filePath
-   */
-  // public PDBEntry(String filePath, String pdbId)
-  // {
-  // this.file = filePath;
-  // this.id = pdbId;
-  // }
-
   public PDBEntry(String pdbId, String chain, PDBEntry.Type type,
           String filePath)
   {
@@ -137,13 +170,14 @@ public class PDBEntry
   /**
    * @param pdbId
    * @param chain
-   * @param type
+   * @param entryType
    * @param filePath
    */
-  void init(String pdbId, String chain, PDBEntry.Type type, String filePath)
+  void init(String pdbId, String chain, PDBEntry.Type entryType,
+          String filePath)
   {
     this.id = pdbId;
-    this.type = type == null ? null : type.toString();
+    this.type = entryType == null ? null : entryType.toString();
     this.file = filePath;
     setChainCode(chain);
   }
@@ -160,7 +194,7 @@ public class PDBEntry
     id = entry.id;
     if (entry.properties != null)
     {
-      properties = (Hashtable) entry.properties.clone();
+      properties = (Hashtable<String, Object>) entry.properties.clone();
     }
   }
 
@@ -175,8 +209,8 @@ public class PDBEntry
   {
     if (!DBRefSource.PDB.equals(dbr.getSource()))
     {
-      throw new IllegalArgumentException("Invalid source: "
-              + dbr.getSource());
+      throw new IllegalArgumentException(
+              "Invalid source: " + dbr.getSource());
     }
 
     String pdbId = dbr.getAccessionId();
@@ -193,9 +227,9 @@ public class PDBEntry
     init(pdbId, chainCode, null, null);
   }
 
-  public void setFile(String file)
+  public void setFile(String f)
   {
-    this.file = file;
+    this.file = f;
   }
 
   public String getFile()
@@ -228,14 +262,33 @@ public class PDBEntry
     return id;
   }
 
-  public void setProperty(Hashtable property)
+  public void setProperty(String key, Object value)
   {
-    this.properties = property;
+    if (this.properties == null)
+    {
+      this.properties = new Hashtable<String, Object>();
+    }
+    properties.put(key, value);
   }
 
-  public Hashtable getProperty()
+  public Object getProperty(String key)
   {
-    return properties;
+    return properties == null ? null : properties.get(key);
+  }
+
+  /**
+   * Returns an enumeration of the keys of this object's properties (or an empty
+   * enumeration if it has no properties)
+   * 
+   * @return
+   */
+  public Enumeration<String> getProperties()
+  {
+    if (properties == null)
+    {
+      return Collections.emptyEnumeration();
+    }
+    return properties.keys();
   }
 
   /**
@@ -248,24 +301,37 @@ public class PDBEntry
             : properties.get(CHAIN_ID).toString();
   }
 
+  /**
+   * Sets a non-case-sensitive property for the given chain code. Two PDBEntry
+   * objects which differ only in the case of their chain code are considered
+   * equal. This avoids duplication of objects in lists of PDB ids.
+   * 
+   * @param chainCode
+   */
   public void setChainCode(String chainCode)
   {
-    if (properties == null)
+    if (chainCode == null)
     {
-      if (chainCode == null)
-      {
-        // nothing to do.
-        return;
-      }
-      properties = new Hashtable();
+      deleteProperty(CHAIN_ID);
     }
-    if (chainCode == null)
+    else
     {
-      properties.remove(CHAIN_ID);
-      return;
+      setProperty(CHAIN_ID, new CaseInsensitiveString(chainCode));
     }
-    // update property for non-null chainCode
-    properties.put(CHAIN_ID, new CaseInsensitiveString(chainCode));
+  }
+
+  /**
+   * Deletes the property with the given key, and returns the deleted value (or
+   * null)
+   */
+  Object deleteProperty(String key)
+  {
+    Object result = null;
+    if (properties != null)
+    {
+      result = properties.remove(key);
+    }
+    return result;
   }
 
   @Override
@@ -275,6 +341,35 @@ public class PDBEntry
   }
 
   /**
+   * Getter provided for Castor binding only. Application code should call
+   * getProperty() or getProperties() instead.
+   * 
+   * @deprecated
+   * @see #getProperty(String)
+   * @see #getProperties()
+   * @see jalview.ws.dbsources.Uniprot#getUniprotEntries
+   * @return
+   */
+  @Deprecated
+  public Hashtable<String, Object> getProps()
+  {
+    return properties;
+  }
+
+  /**
+   * Setter provided for Castor binding only. Application code should call
+   * setProperty() instead.
+   * 
+   * @deprecated
+   * @return
+   */
+  @Deprecated
+  public void setProps(Hashtable<String, Object> props)
+  {
+    properties = props;
+  }
+
+  /**
    * Answers true if this object is either equivalent to, or can be 'improved'
    * by, the given entry.
    * <p>
@@ -285,7 +380,7 @@ public class PDBEntry
    * @param newEntry
    * @return true if modifications were made
    */
-  protected boolean updateFrom(PDBEntry newEntry)
+  public boolean updateFrom(PDBEntry newEntry)
   {
     if (this.equals(newEntry))
     {
@@ -298,21 +393,40 @@ public class PDBEntry
       return false; // shouldn't happen
     }
 
-    /*
-     * id (less any chain code) has to match (ignoring case)
-     */
-    if (!getId().equalsIgnoreCase(newId))
-    {
-      return false;
-    }
+    boolean idMatches = getId().equalsIgnoreCase(newId);
 
     /*
      * Don't update if associated with different structure files
      */
     String newFile = newEntry.getFile();
-    if (newFile != null && getFile() != null && !newFile.equals(getFile()))
+    if (newFile != null && getFile() != null)
     {
-      return false;
+      if (!newFile.equals(getFile()))
+      {
+        return false;
+      }
+      else
+      {
+        // files match.
+        if (!idMatches)
+        {
+          // this shouldn't happen, but could do if the id from the
+          // file is not the same as the id from the authority that provided
+          // the file
+          if (!newEntry.fakedPDBId() && !isAuthoritative())
+          {
+            return false;
+          } // otherwise we can update
+        }
+      }
+    }
+    else
+    {
+      // one has data, one doesn't ..
+      if (!idMatches)
+      {
+        return false;
+      } // otherwise maybe can update
     }
 
     /*
@@ -356,28 +470,153 @@ public class PDBEntry
     }
 
     /*
-     * copy any new properties; notice this may include chain_code,
-     * but we excluded differing chain codes earlier
+     * copy any new or modified properties
      */
-    if (newEntry.getProperty() != null)
+    Enumeration<String> newProps = newEntry.getProperties();
+    while (newProps.hasMoreElements())
     {
-      if (properties == null)
+      /*
+       * copy properties unless value matches; this defends against changing
+       * the case of chain_code which is wrapped in a CaseInsensitiveString
+       */
+      String key = newProps.nextElement();
+      Object value = newEntry.getProperty(key);
+      if (FAKED_ID.equals(key) || AUTHORITATIVE_ID.equals(key))
       {
-        properties = new Hashtable();
+        // we never update the fake ID property
+        continue;
       }
-      for (Object p : newEntry.getProperty().keySet())
+      if (!value.equals(getProperty(key)))
       {
-        /*
-         * copy properties unless value matches; this defends against changing
-         * the case of chain_code which is wrapped in a CaseInsensitiveString
-         */
-        Object value = newEntry.getProperty().get(p);
-        if (!value.equals(properties.get(p)))
-        {
-          properties.put(p, newEntry.getProperty().get(p));
-        }
+        setProperty(key, value);
       }
     }
     return true;
   }
+  
+  public void setAuthoritative(boolean isAuthoritative)
+  {
+    setProperty(AUTHORITATIVE_ID, Boolean.valueOf(isAuthoritative));
+  }
+  
+  /**
+   * 
+   * @return true if the identifier should be preferred over any identifiers
+   *         embedded in the structure data
+   */
+  public boolean isAuthoritative()
+  {
+    if (_hasProperty(AUTHORITATIVE_ID))
+    {
+      return ((Boolean)getProperty(AUTHORITATIVE_ID));
+    }
+    return false;
+  }
+
+  /**
+   * set when Jalview has manufactured the ID using a local filename
+   * @return
+   */
+  public boolean fakedPDBId()
+  {
+    if (_hasProperty(FAKED_ID))
+    {
+      return true;
+    }
+    return false;
+  }
+  public void setFakedPDBId(boolean faked)
+  {
+    if (faked)
+    {
+      setProperty(FAKED_ID, Boolean.TRUE);
+    }
+    else 
+    {
+      if (properties!=null) {
+        properties.remove(FAKED_ID);
+      }
+    }
+  }
+
+  private boolean _hasProperty(final String key)
+  {
+    return (properties != null && properties.containsKey(key));
+  }
+
+  private static final String RETRIEVE_FROM = "RETRIEVE_FROM";
+
+  private static final String PROVIDER = "PROVIDER";
+
+  private static final String MODELPAGE = "PROVIDERPAGE";
+
+  /**
+   * Permanent URI for retrieving the original structure data
+   * 
+   * @param urlStr
+   */
+  public void setRetrievalUrl(String urlStr)
+  {
+    setProperty(RETRIEVE_FROM, urlStr);
+  }
+
+  public boolean hasRetrievalUrl()
+  {
+    return _hasProperty(RETRIEVE_FROM);
+  }
+
+  /**
+   * get the Permanent URI for retrieving the original structure data
+   */
+  public String getRetrievalUrl()
+  {
+    return (String) getProperty(RETRIEVE_FROM);
+  }
+
+  /**
+   * Data provider name - from 3D Beacons
+   * 
+   * @param provider
+   */
+  public void setProvider(String provider)
+  {
+    setProperty(PROVIDER, provider);
+  }
+
+  /**
+   * Get Data provider name - from 3D Beacons
+   * 
+   */
+  public String getProvider()
+  {
+    return (String) getProperty(PROVIDER);
+  }
+
+  /**
+   * Permanent URI for retrieving the original structure data
+   * 
+   * @param urlStr
+   */
+  public void setProviderPage(String urlStr)
+  {
+    setProperty(MODELPAGE, urlStr);
+  }
+
+  /**
+   * get the Permanent URI for retrieving the original structure data
+   */
+  public String getProviderPage()
+  {
+    return (String) getProperty(MODELPAGE);
+  }
+
+  public boolean hasProviderPage()
+  {
+    return _hasProperty(MODELPAGE);
+  }
+
+  public boolean hasProvider()
+  {
+    return _hasProperty(PROVIDER);
+  }
 }