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;
49 // TODO is FILE needed; if not is this needed or can we
50 // use FileFormatI for PDB, MMCIF?
51 PDB("pdb", "xml"), MMCIF("mmcif", "mmcif"), FILE("?", "?");
56 private Type(String fmt, String ex)
62 public String getFormat()
66 public String getExtension()
72 * case insensitive matching for Type enum
77 public static Type getType(String value)
79 for (Type t : Type.values())
81 if (t.toString().equalsIgnoreCase(value))
90 * case insensitive equivalence for strings resolving to PDBEntry type
95 public boolean matches(String t)
97 return (this.toString().equalsIgnoreCase(t));
102 * Answers true if obj is a PDBEntry with the same id and chain code (both
103 * ignoring case), file, type and properties
106 public boolean equals(Object obj)
108 if (obj == null || !(obj instanceof PDBEntry))
116 PDBEntry o = (PDBEntry) obj;
119 * note that chain code is stored as a property wrapped by a
120 * CaseInsensitiveString, so we are in effect doing a
121 * case-insensitive comparison of chain codes
123 boolean idMatches = id == o.id
124 || (id != null && id.equalsIgnoreCase(o.id));
125 boolean fileMatches = file == o.file
126 || (file != null && file.equals(o.file));
127 boolean typeMatches = type == o.type
128 || (type != null && type.equals(o.type));
129 if (idMatches && fileMatches && typeMatches)
131 return properties == o.properties
132 || (properties != null && properties.equals(o.properties));
138 * Default constructor
145 public PDBEntry(String pdbId, String chain, PDBEntry.Type type,
148 init(pdbId, chain, type, filePath);
157 void init(String pdbId, String chain, PDBEntry.Type entryType, String filePath)
160 this.type = entryType == null ? null : entryType.toString();
161 this.file = filePath;
170 public PDBEntry(PDBEntry entry)
175 if (entry.properties != null)
177 properties = (Hashtable<String, Object>) entry.properties.clone();
182 * Make a PDBEntry from a DBRefEntry. The accession code is used for the PDB
183 * id, but if it is 5 characters in length, the last character is removed and
184 * set as the chain code instead.
188 public PDBEntry(DBRefEntry dbr)
190 if (!DBRefSource.PDB.equals(dbr.getSource()))
192 throw new IllegalArgumentException("Invalid source: "
196 String pdbId = dbr.getAccessionId();
197 String chainCode = null;
198 if (pdbId.length() == PDB_ID_LENGTH + 1)
200 char chain = pdbId.charAt(PDB_ID_LENGTH);
201 if (('a' <= chain && chain <= 'z') || ('A' <= chain && chain <= 'Z'))
203 pdbId = pdbId.substring(0, PDB_ID_LENGTH);
204 chainCode = String.valueOf(chain);
207 init(pdbId, chainCode, null, null);
210 public void setFile(String f)
215 public String getFile()
220 public void setType(String t)
225 public void setType(PDBEntry.Type type)
227 this.type = type == null ? null : type.toString();
230 public String getType()
235 public void setId(String id)
240 public String getId()
245 public void setProperty(String key, Object value)
247 if (this.properties == null)
249 this.properties = new Hashtable<String, Object>();
251 properties.put(key, value);
254 public Object getProperty(String key)
256 return properties == null ? null : properties.get(key);
260 * Returns an enumeration of the keys of this object's properties (or an empty
261 * enumeration if it has no properties)
265 public Enumeration<String> getProperties()
267 if (properties == null)
269 return Collections.emptyEnumeration();
271 return properties.keys();
276 * @return null or a string for associated chain IDs
278 public String getChainCode()
280 return (properties == null || properties.get(CHAIN_ID) == null) ? null
281 : properties.get(CHAIN_ID).toString();
285 * Sets a non-case-sensitive property for the given chain code. Two PDBEntry
286 * objects which differ only in the case of their chain code are considered
287 * equal. This avoids duplication of objects in lists of PDB ids.
291 public void setChainCode(String chainCode)
293 if (chainCode == null)
295 deleteProperty(CHAIN_ID);
299 setProperty(CHAIN_ID, new CaseInsensitiveString(chainCode));
304 * Deletes the property with the given key, and returns the deleted value (or
307 Object deleteProperty(String key)
309 Object result = null;
310 if (properties != null)
312 result = properties.remove(key);
318 public String toString()
324 * Getter provided for Castor binding only. Application code should call
325 * getProperty() or getProperties() instead.
328 * @see #getProperty(String)
329 * @see #getProperties()
330 * @see jalview.ws.dbsources.Uniprot#getUniprotEntries
334 public Hashtable<String, Object> getProps()
340 * Setter provided for Castor binding only. Application code should call
341 * setProperty() instead.
347 public void setProps(Hashtable<String, Object> props)
353 * Answers true if this object is either equivalent to, or can be 'improved'
354 * by, the given entry.
356 * If newEntry has the same id (ignoring case), and doesn't have a conflicting
357 * file spec or chain code, then update this entry from its file and/or chain
361 * @return true if modifications were made
363 public boolean updateFrom(PDBEntry newEntry)
365 if (this.equals(newEntry))
370 String newId = newEntry.getId();
371 if (newId == null || getId() == null)
373 return false; // shouldn't happen
377 * id has to match (ignoring case)
379 if (!getId().equalsIgnoreCase(newId))
385 * Don't update if associated with different structure files
387 String newFile = newEntry.getFile();
388 if (newFile != null && getFile() != null && !newFile.equals(getFile()))
394 * Don't update if associated with different chains (ignoring case)
396 String newChain = newEntry.getChainCode();
397 if (newChain != null && newChain.length() > 0 && getChainCode() != null
398 && getChainCode().length() > 0
399 && !getChainCode().equalsIgnoreCase(newChain))
405 * set file path if not already set
407 String newType = newEntry.getType();
408 if (getFile() == null && newFile != null)
415 * set file type if new entry has it and we don't
416 * (for the case where file was not updated)
418 if (getType() == null && newType != null)
424 * set chain if not already set (we excluded differing
425 * chains earlier) (ignoring case change only)
427 if (newChain != null && newChain.length() > 0
428 && !newChain.equalsIgnoreCase(getChainCode()))
430 setChainCode(newChain);
434 * copy any new or modified properties
436 Enumeration<String> newProps = newEntry.getProperties();
437 while (newProps.hasMoreElements())
440 * copy properties unless value matches; this defends against changing
441 * the case of chain_code which is wrapped in a CaseInsensitiveString
443 String key = newProps.nextElement();
444 Object value = newEntry.getProperty(key);
445 if (!value.equals(getProperty(key)))
447 setProperty(key, value);