X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fdatamodel%2FPDBEntry.java;h=6a6ccd0f7d1904425a7e9dc0476d4f4d088da187;hb=37de9310bec3501cbc6381e0c3dcb282fcaad812;hp=db792e270fd7d6042eac33df4b026f84cc00ad3f;hpb=0e90e35fa87e18e80a340f982110b03c5eccc503;p=jalview.git diff --git a/src/jalview/datamodel/PDBEntry.java b/src/jalview/datamodel/PDBEntry.java index db792e2..6a6ccd0 100755 --- a/src/jalview/datamodel/PDBEntry.java +++ b/src/jalview/datamodel/PDBEntry.java @@ -20,28 +20,67 @@ */ 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 properties; + + private static final int PDB_ID_LENGTH = 4; + private String file; private String type; private String id; - private String chainCode; - public enum Type { - PDB, FILE + PDB, MMCIF, FILE; + /** + * case insensitive matching for Type enum + * + * @param value + * @return + */ + public static Type getType(String value) + { + for (Type t : Type.values()) + { + if (t.toString().equalsIgnoreCase(value)) + { + return t; + } + } + return null; + } + + /** + * case insensitive equivalence for strings resolving to PDBEntry type + * + * @param t + * @return + */ + public boolean matches(String t) + { + return (this.toString().equalsIgnoreCase(t)); + } } - Hashtable properties; - /* - * (non-Javadoc) - * - * @see java.lang.Object#equals(java.lang.Object) + + /** + * Answers true if obj is a PDBEntry with the same id and chain code (both + * ignoring case), file, type and properties */ @Override public boolean equals(Object obj) @@ -55,18 +94,24 @@ public class PDBEntry return true; } PDBEntry o = (PDBEntry) obj; - return - (type == o.type || (type != null && o.type != null && o.type - .equals(type))) - && (id == o.id || (id != null && o.id != null && o.id - .equalsIgnoreCase(id))) - && (chainCode == o.chainCode || (chainCode != null - && o.chainCode != null && o.chainCode - .equalsIgnoreCase(chainCode))) - && (properties == o.properties || (properties != null - && o.properties != null && properties - .equals(o.properties))); - + + /* + * note that chain code is stored as a property wrapped by a + * CaseInsensitiveString, so we are in effect doing a + * case-insensitive comparison of chain codes + */ + boolean idMatches = id == o.id + || (id != null && id.equalsIgnoreCase(o.id)); + boolean fileMatches = file == o.file + || (file != null && file.equals(o.file)); + boolean typeMatches = type == o.type + || (type != null && type.equals(o.type)); + if (idMatches && fileMatches && typeMatches) + { + return properties == o.properties + || (properties != null && properties.equals(o.properties)); + } + return false; } /** @@ -86,14 +131,25 @@ public class PDBEntry // this.file = filePath; // this.id = pdbId; // } - + public PDBEntry(String pdbId, String chain, PDBEntry.Type type, String filePath) { + init(pdbId, chain, type, filePath); + } + + /** + * @param pdbId + * @param chain + * @param entryType + * @param filePath + */ + void init(String pdbId, String chain, PDBEntry.Type entryType, String filePath) + { this.id = pdbId; - this.chainCode = chain; - this.type = type == null ? null : type.toString(); + this.type = entryType == null ? null : entryType.toString(); this.file = filePath; + setChainCode(chain); } /** @@ -106,16 +162,44 @@ public class PDBEntry file = entry.file; type = entry.type; id = entry.id; - chainCode = entry.chainCode; if (entry.properties != null) { - properties = (Hashtable) entry.properties.clone(); + properties = (Hashtable) entry.properties.clone(); + } + } + + /** + * Make a PDBEntry from a DBRefEntry. The accession code is used for the PDB + * id, but if it is 5 characters in length, the last character is removed and + * set as the chain code instead. + * + * @param dbr + */ + public PDBEntry(DBRefEntry dbr) + { + if (!DBRefSource.PDB.equals(dbr.getSource())) + { + throw new IllegalArgumentException("Invalid source: " + + dbr.getSource()); + } + + String pdbId = dbr.getAccessionId(); + String chainCode = null; + if (pdbId.length() == PDB_ID_LENGTH + 1) + { + char chain = pdbId.charAt(PDB_ID_LENGTH); + if (('a' <= chain && chain <= 'z') || ('A' <= chain && chain <= 'Z')) + { + pdbId = pdbId.substring(0, PDB_ID_LENGTH); + chainCode = String.valueOf(chain); + } } + init(pdbId, chainCode, null, null); } - public void setFile(String file) + public void setFile(String f) { - this.file = file; + this.file = f; } public String getFile() @@ -127,6 +211,7 @@ public class PDBEntry { this.type = t; } + public void setType(PDBEntry.Type type) { this.type = type == null ? null : type.toString(); @@ -147,24 +232,211 @@ 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(); + } + 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 getProperties() + { + if (properties == null) + { + return Collections.emptyEnumeration(); + } + return properties.keys(); } + /** + * + * @return null or a string for associated chain IDs + */ public String getChainCode() { - return chainCode; + return (properties == null || properties.get(CHAIN_ID) == null) ? null + : 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) { - this.chainCode = chainCode; + if (chainCode == null) + { + deleteProperty(CHAIN_ID); + } + else + { + setProperty(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 + public String toString() + { + return id; + } + + /** + * 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 getProps() + { + return properties; } + /** + * Setter provided for Castor binding only. Application code should call + * setProperty() instead. + * + * @deprecated + * @return + */ + @Deprecated + public void setProps(Hashtable props) + { + properties = props; + } + + /** + * Answers true if this object is either equivalent to, or can be 'improved' + * by, the given entry. + *

+ * If newEntry has the same id (ignoring case), and doesn't have a conflicting + * file spec or chain code, then update this entry from its file and/or chain + * code. + * + * @param newEntry + * @return true if modifications were made + */ + public boolean updateFrom(PDBEntry newEntry) + { + if (this.equals(newEntry)) + { + return true; + } + + String newId = newEntry.getId(); + if (newId == null || getId() == null) + { + return false; // shouldn't happen + } + + /* + * id has to match (ignoring case) + */ + if (!getId().equalsIgnoreCase(newId)) + { + return false; + } + + /* + * Don't update if associated with different structure files + */ + String newFile = newEntry.getFile(); + if (newFile != null && getFile() != null && !newFile.equals(getFile())) + { + return false; + } + + /* + * Don't update if associated with different chains (ignoring case) + */ + String newChain = newEntry.getChainCode(); + if (newChain != null && newChain.length() > 0 && getChainCode() != null + && getChainCode().length() > 0 + && !getChainCode().equalsIgnoreCase(newChain)) + { + return false; + } + + /* + * set file path if not already set + */ + String newType = newEntry.getType(); + if (getFile() == null && newFile != null) + { + setFile(newFile); + setType(newType); + } + + /* + * set file type if new entry has it and we don't + * (for the case where file was not updated) + */ + if (getType() == null && newType != null) + { + setType(newType); + } + + /* + * set chain if not already set (we excluded differing + * chains earlier) (ignoring case change only) + */ + if (newChain != null && newChain.length() > 0 + && !newChain.equalsIgnoreCase(getChainCode())) + { + setChainCode(newChain); + } + + /* + * copy any new or modified properties + */ + Enumeration newProps = newEntry.getProperties(); + while (newProps.hasMoreElements()) + { + /* + * 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 (!value.equals(getProperty(key))) + { + setProperty(key, value); + } + } + return true; + } }