X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;ds=inline;f=src%2Fjalview%2Fdatamodel%2FPDBEntry.java;h=14035957fe08c181c99d2580e042f0ac756bcf8e;hb=3446323f14bb8a2842cb83f74ed3f41c99b62759;hp=1c1d192fcbabc754b547bff599aa21408d7af19c;hpb=c06a9af4331b34a8537b28b5e749c442e1bc1682;p=jalview.git diff --git a/src/jalview/datamodel/PDBEntry.java b/src/jalview/datamodel/PDBEntry.java index 1c1d192..1403595 100755 --- a/src/jalview/datamodel/PDBEntry.java +++ b/src/jalview/datamodel/PDBEntry.java @@ -20,10 +20,14 @@ */ package jalview.datamodel; +import jalview.util.CaseInsensitiveString; + import java.util.Hashtable; public class PDBEntry { + private static final int PDB_ID_LENGTH = 4; + private String file; private String type; @@ -63,31 +67,6 @@ public class PDBEntry } } - public final class ChainId - { - String chainId; - - public ChainId(String chainId) - { - this.chainId = chainId; - } - - @Override - public String toString() - { - return chainId; - } - - @Override - public boolean equals(Object o){ - if (o==null) - { - return false; - } - return chainId.equalsIgnoreCase(o.toString()); - } - } - /** * constant for storing chain code in properties table */ @@ -95,10 +74,9 @@ public class PDBEntry 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) @@ -112,14 +90,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; } /** @@ -143,6 +131,17 @@ public class PDBEntry public PDBEntry(String pdbId, String chain, PDBEntry.Type type, String filePath) { + init(pdbId, chain, type, filePath); + } + + /** + * @param pdbId + * @param chain + * @param type + * @param filePath + */ + void init(String pdbId, String chain, PDBEntry.Type type, String filePath) + { this.id = pdbId; this.type = type == null ? null : type.toString(); this.file = filePath; @@ -165,6 +164,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 file) { this.file = file; @@ -237,7 +265,7 @@ public class PDBEntry return; } // update property for non-null chainCode - properties.put(CHAIN_ID, new ChainId(chainCode)); + properties.put(CHAIN_ID, new CaseInsensitiveString(chainCode)); } @Override @@ -247,52 +275,109 @@ 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)
+ protected 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()))
+
+ /*
+ * id (less any chain code) has to match (ignoring case)
+ */
+ if (!getId().equalsIgnoreCase(newId))
{
- setType(newEntry.getType());
+ return false;
}
- if (getChainCode() == null
- || (getChainCode() != null && getChainCode().length() == 0 && newEntry
- .getChainCode() != null))
+
+ /*
+ * Don't update if associated with different structure files
+ */
+ String newFile = newEntry.getFile();
+ if (newFile != null && getFile() != null && !newFile.equals(getFile()))
{
- modified |= getChainCode() == null
- || !newEntry.getChainCode().equals(getChainCode());
- setChainCode(newEntry.getChainCode());
+ 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 properties; notice this may include chain_code,
+ * but we excluded differing chain codes earlier
+ */
if (newEntry.getProperty() != null)
{
if (properties == null)
{
properties = new Hashtable();
}
- // TODO: getProperty -> Map