From 6c96743ff6c65895e4b96523d0e3d05f6dc3cb01 Mon Sep 17 00:00:00 2001 From: gmungoc Date: Tue, 13 Sep 2016 15:28:33 +0100 Subject: [PATCH] JAL-2196 refactor PDBEntry.getProperty,setProperty,getProperties --- resources/uniprot_mapping.xml | 2 +- src/MCview/PDBViewer.java | 21 ++- src/jalview/appletgui/AlignFrame.java | 10 +- src/jalview/appletgui/AppletJmol.java | 8 +- src/jalview/datamodel/PDBEntry.java | 136 ++++++++++++++------ src/jalview/gui/Jalview2XML.java | 14 +- src/jalview/io/StructureFile.java | 5 +- .../structures/models/AAStructureBindingModel.java | 23 ++-- src/jalview/util/DBRefUtils.java | 3 - test/jalview/datamodel/PDBEntryTest.java | 2 +- test/jalview/ws/dbsources/UniprotTest.java | 8 +- 11 files changed, 135 insertions(+), 97 deletions(-) diff --git a/resources/uniprot_mapping.xml b/resources/uniprot_mapping.xml index a8634af..4a981ad 100755 --- a/resources/uniprot_mapping.xml +++ b/resources/uniprot_mapping.xml @@ -80,7 +80,7 @@ - + diff --git a/src/MCview/PDBViewer.java b/src/MCview/PDBViewer.java index 66ce147..0f6ef27 100755 --- a/src/MCview/PDBViewer.java +++ b/src/MCview/PDBViewer.java @@ -128,18 +128,17 @@ public class PDBViewer extends JInternalFrame implements Runnable worker.start(); } - if (pdbentry.getProperty() != null) + String method = (String) pdbentry.getProperty("method"); + if (method != null) { - if (pdbentry.getProperty().get("method") != null) - { - title.append(" Method: "); - title.append(pdbentry.getProperty().get("method")); - } - if (pdbentry.getProperty().get("chains") != null) - { - title.append(" Chain:"); - title.append(pdbentry.getProperty().get("chains")); - } + title.append(" Method: "); + title.append(method); + } + String ch = (String) pdbentry.getProperty("chains"); + if (ch != null) + { + title.append(" Chain:"); + title.append(ch); } Desktop.addInternalFrame(this, title.toString(), 400, 400); } diff --git a/src/jalview/appletgui/AlignFrame.java b/src/jalview/appletgui/AlignFrame.java index 0312015..e9e0786 100644 --- a/src/jalview/appletgui/AlignFrame.java +++ b/src/jalview/appletgui/AlignFrame.java @@ -102,7 +102,6 @@ import java.net.URLEncoder; import java.util.Arrays; import java.util.Deque; import java.util.HashMap; -import java.util.Hashtable; import java.util.List; import java.util.Map; import java.util.StringTokenizer; @@ -4028,12 +4027,7 @@ public class AlignFrame extends EmbmenuFrame implements ActionListener, } if (needtoadd) { - // make a note of the access mode and add - if (pdbentry.getProperty() == null) - { - pdbentry.setProperty(new Hashtable()); - } - pdbentry.getProperty().put("protocol", protocol); + pdbentry.setProperty("protocol", protocol); toaddpdb.addPDBId(pdbentry); alignPanel.getStructureSelectionManager() .registerPDBEntry(pdbentry); @@ -4084,7 +4078,7 @@ public class AlignFrame extends EmbmenuFrame implements ActionListener, if (protocol == null || protocol.trim().length() == 0 || protocol.equals("null")) { - protocol = (String) pdb.getProperty().get("protocol"); + protocol = (String) pdb.getProperty("protocol"); if (protocol == null) { System.err.println("Couldn't work out protocol to open structure: " diff --git a/src/jalview/appletgui/AppletJmol.java b/src/jalview/appletgui/AppletJmol.java index 8374721..b925284 100644 --- a/src/jalview/appletgui/AppletJmol.java +++ b/src/jalview/appletgui/AppletJmol.java @@ -60,7 +60,6 @@ import java.awt.event.KeyListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.util.ArrayList; -import java.util.Hashtable; import java.util.List; import java.util.Vector; @@ -290,11 +289,8 @@ public class AppletJmol extends EmbmenuFrame implements closeViewer(); } }); - if (pdbentry.getProperty() == null) - { - pdbentry.setProperty(new Hashtable()); - pdbentry.getProperty().put("protocol", protocol); - } + pdbentry.setProperty("protocol", protocol); + if (pdbentry.getFile() != null) { // import structure data from pdbentry.getFile based on given protocol diff --git a/src/jalview/datamodel/PDBEntry.java b/src/jalview/datamodel/PDBEntry.java index 198b4a6..7c2d290 100755 --- a/src/jalview/datamodel/PDBEntry.java +++ b/src/jalview/datamodel/PDBEntry.java @@ -22,10 +22,21 @@ package jalview.datamodel; import jalview.util.CaseInsensitiveString; +import java.util.Collections; +import java.util.Enumeration; import java.util.Hashtable; +import java.util.Map.Entry; public class PDBEntry { + + /** + * constant for storing chain code in properties table + */ + private static final String CHAIN_ID = "chain_code"; + + Hashtable properties; + private String file; private String type; @@ -65,12 +76,6 @@ public class PDBEntry } } - /** - * constant for storing chain code in properties table - */ - private static final String CHAIN_ID = "chain_code"; - - Hashtable properties; /* * (non-Javadoc) @@ -138,13 +143,13 @@ public class PDBEntry id = entry.id; if (entry.properties != null) { - properties = (Hashtable) entry.properties.clone(); + properties = (Hashtable) entry.properties.clone(); } } - public void setFile(String file) + public void setFile(String f) { - this.file = file; + this.file = f; } public String getFile() @@ -177,14 +182,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(); + } + 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(); } /** @@ -197,24 +221,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 + { + 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) { - properties.remove(CHAIN_ID); - return; + result = properties.remove(key); } - // update property for non-null chainCode - properties.put(CHAIN_ID, new CaseInsensitiveString(chainCode)); + return result; } @Override @@ -224,6 +261,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 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; + } + + /** * update entry with details from another entry concerning the same PDB * ID/file spec. * @@ -237,7 +303,7 @@ public class PDBEntry if (getFile() == null) { // update file and type of file - modified |= newEntry.getFile() != null; + modified = newEntry.getFile() != null; setFile(newEntry.getFile()); } if (newEntry.getType() != null && newEntry.getFile() != null @@ -249,25 +315,19 @@ public class PDBEntry || (getChainCode() != null && getChainCode().length() == 0 && newEntry .getChainCode() != null)) { - modified |= getChainCode() == null - || !newEntry.getChainCode().equals(getChainCode()); + modified |= (getChainCode() == null || !newEntry.getChainCode() + .equals(getChainCode())); setChainCode(newEntry.getChainCode()); } - if (newEntry.getProperty() != null) + if (newEntry.properties != null) { - if (properties == null) - { - properties = new Hashtable(); - } - // TODO: getProperty -> Map - for (Object p : newEntry.getProperty().keySet()) + for (Entry entry : newEntry.properties.entrySet()) { - if (properties.get(p) == null - || !properties.get(p).equals(newEntry.getProperty().get(p))) + if (!entry.getValue().equals(getProperty(entry.getKey()))) { modified = true; } - properties.put(p, newEntry.getProperty().get(p)); + setProperty(entry.getKey(), entry.getValue()); } } return modified; diff --git a/src/jalview/gui/Jalview2XML.java b/src/jalview/gui/Jalview2XML.java index 7093a9a..aad4bb8 100644 --- a/src/jalview/gui/Jalview2XML.java +++ b/src/jalview/gui/Jalview2XML.java @@ -982,17 +982,16 @@ public class Jalview2XML } } - if (entry.getProperty() != null && !entry.getProperty().isEmpty()) + Enumeration props = entry.getProperties(); + if (props.hasMoreElements()) { PdbentryItem item = new PdbentryItem(); - Hashtable properties = entry.getProperty(); - Enumeration en2 = properties.keys(); - while (en2.hasMoreElements()) + while (props.hasMoreElements()) { Property prop = new Property(); - String key = en2.nextElement().toString(); + String key = props.nextElement(); prop.setName(key); - prop.setValue(properties.get(key).toString()); + prop.setValue(entry.getProperty(key).toString()); item.addProperty(prop); } pdb.addPdbentryItem(item); @@ -3019,12 +3018,11 @@ public class Jalview2XML } if (ids[p].getPdbentryItem() != null) { - entry.setProperty(new Hashtable()); for (PdbentryItem item : ids[p].getPdbentryItem()) { for (Property pr : item.getProperty()) { - entry.getProperty().put(pr.getName(), pr.getValue()); + entry.setProperty(pr.getName(), pr.getValue()); } } } diff --git a/src/jalview/io/StructureFile.java b/src/jalview/io/StructureFile.java index 4a6a1c2..6ce907a 100644 --- a/src/jalview/io/StructureFile.java +++ b/src/jalview/io/StructureFile.java @@ -15,7 +15,6 @@ import jalview.structure.StructureImportSettings; import java.awt.Color; import java.io.IOException; import java.lang.reflect.Constructor; -import java.util.Hashtable; import java.util.List; import java.util.Vector; @@ -92,7 +91,6 @@ public abstract class StructureFile extends AlignFile { } - @SuppressWarnings("rawtypes") protected SequenceI postProcessChain(PDBChain chain) { SequenceI pdbSequence = chain.sequence; @@ -100,10 +98,9 @@ public abstract class StructureFile extends AlignFile PDBEntry entry = new PDBEntry(); entry.setId(getId()); entry.setType(getStructureFileType()); - entry.setProperty(new Hashtable()); if (chain.id != null) { - entry.setChainCode(String.valueOf(chain.id)); + entry.setChainCode(chain.id); } if (inFile != null) { diff --git a/src/jalview/structures/models/AAStructureBindingModel.java b/src/jalview/structures/models/AAStructureBindingModel.java index dc42315..fc32fa3 100644 --- a/src/jalview/structures/models/AAStructureBindingModel.java +++ b/src/jalview/structures/models/AAStructureBindingModel.java @@ -239,24 +239,21 @@ public abstract class AAStructureBindingModel extends // TODO: give a more informative title when multiple structures are // displayed. StringBuilder title = new StringBuilder(64); - final PDBEntry pdbEntry = getPdbEntry(0); + final PDBEntry pdbe = getPdbEntry(0); title.append(viewerName + " view for " + getSequence()[0][0].getName() - + ":" + pdbEntry.getId()); + + ":" + pdbe.getId()); if (verbose) { - if (pdbEntry.getProperty() != null) + String method = (String) pdbe.getProperty("method"); + if (method != null) { - if (pdbEntry.getProperty().get("method") != null) - { - title.append(" Method: "); - title.append(pdbEntry.getProperty().get("method")); - } - if (pdbEntry.getProperty().get("chains") != null) - { - title.append(" Chain:"); - title.append(pdbEntry.getProperty().get("chains")); - } + title.append(" Method: ").append(method); + } + String chain = (String) pdbe.getProperty("chains"); + if (chain != null) + { + title.append(" Chain:").append(chain); } } return title.toString(); diff --git a/src/jalview/util/DBRefUtils.java b/src/jalview/util/DBRefUtils.java index 405f6e6..4a0a55d 100755 --- a/src/jalview/util/DBRefUtils.java +++ b/src/jalview/util/DBRefUtils.java @@ -28,7 +28,6 @@ import jalview.datamodel.SequenceI; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; -import java.util.Hashtable; import java.util.List; import java.util.Map; import java.util.Set; @@ -503,9 +502,7 @@ public class DBRefUtils PDBEntry pdbr = new PDBEntry(); pdbr.setId(pdbid); pdbr.setType(PDBEntry.Type.PDB); - pdbr.setProperty(new Hashtable()); pdbr.setChainCode(chaincode); - // pdbr.getProperty().put("CHAIN", chaincode); seq.addPDBId(pdbr); } else diff --git a/test/jalview/datamodel/PDBEntryTest.java b/test/jalview/datamodel/PDBEntryTest.java index 1195393..df29437 100644 --- a/test/jalview/datamodel/PDBEntryTest.java +++ b/test/jalview/datamodel/PDBEntryTest.java @@ -82,7 +82,7 @@ public class PDBEntryTest /* * change string wrapper property to string... */ - case1.getProperty().put("chain_code", "a"); + case1.setProperty("chain_code", "a"); assertFalse(pdbEntry.equals(case1)); assertFalse(case1.equals(pdbEntry)); } diff --git a/test/jalview/ws/dbsources/UniprotTest.java b/test/jalview/ws/dbsources/UniprotTest.java index 72e599d..26fcaf0 100644 --- a/test/jalview/ws/dbsources/UniprotTest.java +++ b/test/jalview/ws/dbsources/UniprotTest.java @@ -21,6 +21,7 @@ package jalview.ws.dbsources; import static org.testng.AssertJUnit.assertEquals; +import static org.testng.AssertJUnit.assertFalse; import static org.testng.AssertJUnit.assertNull; import jalview.datamodel.PDBEntry; @@ -114,14 +115,13 @@ public class UniprotTest PDBEntry xref = xrefs.get(0); assertEquals("2FSQ", xref.getId()); assertEquals("PDB", xref.getType()); - assertEquals(2, xref.getProperty().size()); - assertEquals("X-ray", xref.getProperty().get("method")); - assertEquals("1.40", xref.getProperty().get("resolution")); + assertEquals("X-ray", xref.getProperty("method")); + assertEquals("1.40", xref.getProperty("resolution")); xref = xrefs.get(1); assertEquals("2FSR", xref.getId()); assertEquals("PDBsum", xref.getType()); - assertNull(xref.getProperty()); + assertFalse(xref.getProperties().hasMoreElements()); } /** -- 1.7.10.2