X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fdatamodel%2FPDBEntry.java;h=741ef0774cd9ed2ec3ed9463089215f51ff61e42;hb=584ab9644955bec02d96448361b8aac5b9542599;hp=7c2d2909274363e60b8ff57fc973c369aff834b5;hpb=6c96743ff6c65895e4b96523d0e3d05f6dc3cb01;p=jalview.git diff --git a/src/jalview/datamodel/PDBEntry.java b/src/jalview/datamodel/PDBEntry.java index 7c2d290..741ef07 100755 --- a/src/jalview/datamodel/PDBEntry.java +++ b/src/jalview/datamodel/PDBEntry.java @@ -25,7 +25,6 @@ import jalview.util.CaseInsensitiveString; import java.util.Collections; import java.util.Enumeration; import java.util.Hashtable; -import java.util.Map.Entry; public class PDBEntry { @@ -35,7 +34,18 @@ public class PDBEntry */ private static final String CHAIN_ID = "chain_code"; - Hashtable properties; + private Hashtable 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; @@ -45,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 * @@ -76,11 +118,9 @@ public class PDBEntry } } - - /* - * (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) @@ -94,14 +134,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))) - && (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; } /** @@ -111,22 +161,23 @@ public class PDBEntry { } + public PDBEntry(String pdbId, String chain, PDBEntry.Type type, + String filePath) + { + init(pdbId, chain, type, filePath); + } + /** - * Constructor given file path and PDB id. - * + * @param pdbId + * @param chain + * @param entryType * @param filePath */ - // public PDBEntry(String filePath, String pdbId) - // { - // this.file = filePath; - // this.id = pdbId; - // } - - public PDBEntry(String pdbId, String chain, PDBEntry.Type type, + 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); } @@ -147,6 +198,35 @@ public class PDBEntry } } + /** + * 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 f) { this.file = f; @@ -290,46 +370,253 @@ public class PDBEntry } /** - * update entry with details from another entry concerning the same PDB - * ID/file spec. + * 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) { - boolean modified = false; + if (this.equals(newEntry)) + { + return true; + } - if (getFile() == null) + String newId = newEntry.getId(); + if (newId == null || getId() == null) { - // update file and type of file - modified = newEntry.getFile() != null; - setFile(newEntry.getFile()); + return false; // shouldn't happen } - if (newEntry.getType() != null && newEntry.getFile() != null - && newEntry.getFile().equals(getFile())) + + boolean idMatches = getId().equalsIgnoreCase(newId); + + /* + * Don't update if associated with different structure files + */ + String newFile = newEntry.getFile(); + if (newFile != null && getFile() != null) { - setType(newEntry.getType()); + 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 + } + } } - if (getChainCode() == null - || (getChainCode() != null && getChainCode().length() == 0 && newEntry - .getChainCode() != null)) + else { - modified |= (getChainCode() == null || !newEntry.getChainCode() - .equals(getChainCode())); - setChainCode(newEntry.getChainCode()); + // one has data, one doesn't .. + if (!idMatches) + { + return false; + } // otherwise maybe can update + } + + /* + * 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); } - if (newEntry.properties != null) + + /* + * 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()) { - for (Entry entry : newEntry.properties.entrySet()) + /* + * 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)) { - if (!entry.getValue().equals(getProperty(entry.getKey()))) - { - modified = true; - } - setProperty(entry.getKey(), entry.getValue()); + // we never update the fake ID property + continue; + } + if (!value.equals(getProperty(key))) + { + 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); } } - return modified; + } + + 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); } }