2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.datamodel;
23 import jalview.util.CaseInsensitiveString;
25 import java.util.Collections;
26 import java.util.Enumeration;
27 import java.util.Hashtable;
33 * constant for storing chain code in properties table
35 private static final String CHAIN_ID = "chain_code";
37 private Hashtable<String, Object> properties;
39 private static final int PDB_ID_LENGTH = 4;
51 * case insensitive matching for Type enum
56 public static Type getType(String value)
58 for (Type t : Type.values())
60 if (t.toString().equalsIgnoreCase(value))
69 * case insensitive equivalence for strings resolving to PDBEntry type
74 public boolean matches(String t)
76 return (this.toString().equalsIgnoreCase(t));
82 * Answers true if obj is a PDBEntry with the same id and chain code (both
83 * ignoring case), file, type and properties
86 public boolean equals(Object obj)
88 if (obj == null || !(obj instanceof PDBEntry))
96 PDBEntry o = (PDBEntry) obj;
99 * note that chain code is stored as a property wrapped by a
100 * CaseInsensitiveString, so we are in effect doing a
101 * case-insensitive comparison of chain codes
103 boolean idMatches = id == o.id
104 || (id != null && id.equalsIgnoreCase(o.id));
105 boolean fileMatches = file == o.file
106 || (file != null && file.equals(o.file));
107 boolean typeMatches = type == o.type
108 || (type != null && type.equals(o.type));
109 if (idMatches && fileMatches && typeMatches)
111 return properties == o.properties
112 || (properties != null && properties.equals(o.properties));
118 * Default constructor
125 * Constructor given file path and PDB id.
129 // public PDBEntry(String filePath, String pdbId)
131 // this.file = filePath;
135 public PDBEntry(String pdbId, String chain, PDBEntry.Type type,
138 init(pdbId, chain, type, filePath);
147 void init(String pdbId, String chain, PDBEntry.Type entryType, String filePath)
150 this.type = entryType == null ? null : entryType.toString();
151 this.file = filePath;
160 public PDBEntry(PDBEntry entry)
165 if (entry.properties != null)
167 properties = (Hashtable<String, Object>) entry.properties.clone();
172 * Make a PDBEntry from a DBRefEntry. The accession code is used for the PDB
173 * id, but if it is 5 characters in length, the last character is removed and
174 * set as the chain code instead.
178 public PDBEntry(DBRefEntry dbr)
180 if (!DBRefSource.PDB.equals(dbr.getSource()))
182 throw new IllegalArgumentException("Invalid source: "
186 String pdbId = dbr.getAccessionId();
187 String chainCode = null;
188 if (pdbId.length() == PDB_ID_LENGTH + 1)
190 char chain = pdbId.charAt(PDB_ID_LENGTH);
191 if (('a' <= chain && chain <= 'z') || ('A' <= chain && chain <= 'Z'))
193 pdbId = pdbId.substring(0, PDB_ID_LENGTH);
194 chainCode = String.valueOf(chain);
197 init(pdbId, chainCode, null, null);
200 public void setFile(String f)
205 public String getFile()
210 public void setType(String t)
215 public void setType(PDBEntry.Type type)
217 this.type = type == null ? null : type.toString();
220 public String getType()
225 public void setId(String id)
230 public String getId()
235 public void setProperty(String key, Object value)
237 if (this.properties == null)
239 this.properties = new Hashtable<String, Object>();
241 properties.put(key, value);
244 public Object getProperty(String key)
246 return properties == null ? null : properties.get(key);
250 * Returns an enumeration of the keys of this object's properties (or an empty
251 * enumeration if it has no properties)
255 public Enumeration<String> getProperties()
257 if (properties == null)
259 return Collections.emptyEnumeration();
261 return properties.keys();
266 * @return null or a string for associated chain IDs
268 public String getChainCode()
270 return (properties == null || properties.get(CHAIN_ID) == null) ? null
271 : properties.get(CHAIN_ID).toString();
275 * Sets a non-case-sensitive property for the given chain code. Two PDBEntry
276 * objects which differ only in the case of their chain code are considered
277 * equal. This avoids duplication of objects in lists of PDB ids.
281 public void setChainCode(String chainCode)
283 if (chainCode == null)
285 deleteProperty(CHAIN_ID);
289 setProperty(CHAIN_ID, new CaseInsensitiveString(chainCode));
294 * Deletes the property with the given key, and returns the deleted value (or
297 Object deleteProperty(String key)
299 Object result = null;
300 if (properties != null)
302 result = properties.remove(key);
308 public String toString()
314 * Getter provided for Castor binding only. Application code should call
315 * getProperty() or getProperties() instead.
318 * @see #getProperty(String)
319 * @see #getProperties()
320 * @see jalview.ws.dbsources.Uniprot#getUniprotEntries
324 public Hashtable<String, Object> getProps()
330 * Setter provided for Castor binding only. Application code should call
331 * setProperty() instead.
337 public void setProps(Hashtable<String, Object> props)
343 * Answers true if this object is either equivalent to, or can be 'improved'
344 * by, the given entry.
346 * If newEntry has the same id (ignoring case), and doesn't have a conflicting
347 * file spec or chain code, then update this entry from its file and/or chain
351 * @return true if modifications were made
353 public boolean updateFrom(PDBEntry newEntry)
355 if (this.equals(newEntry))
360 String newId = newEntry.getId();
361 if (newId == null || getId() == null)
363 return false; // shouldn't happen
367 * id has to match (ignoring case)
369 if (!getId().equalsIgnoreCase(newId))
375 * Don't update if associated with different structure files
377 String newFile = newEntry.getFile();
378 if (newFile != null && getFile() != null && !newFile.equals(getFile()))
384 * Don't update if associated with different chains (ignoring case)
386 String newChain = newEntry.getChainCode();
387 if (newChain != null && newChain.length() > 0 && getChainCode() != null
388 && getChainCode().length() > 0
389 && !getChainCode().equalsIgnoreCase(newChain))
395 * set file path if not already set
397 String newType = newEntry.getType();
398 if (getFile() == null && newFile != null)
405 * set file type if new entry has it and we don't
406 * (for the case where file was not updated)
408 if (getType() == null && newType != null)
414 * set chain if not already set (we excluded differing
415 * chains earlier) (ignoring case change only)
417 if (newChain != null && newChain.length() > 0
418 && !newChain.equalsIgnoreCase(getChainCode()))
420 setChainCode(newChain);
424 * copy any new or modified properties
426 Enumeration<String> newProps = newEntry.getProperties();
427 while (newProps.hasMoreElements())
430 * copy properties unless value matches; this defends against changing
431 * the case of chain_code which is wrapped in a CaseInsensitiveString
433 String key = newProps.nextElement();
434 Object value = newEntry.getProperty(key);
435 if (!value.equals(getProperty(key)))
437 setProperty(key, value);