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;
26 import java.util.Collections;
27 import java.util.Enumeration;
28 import java.util.Hashtable;
34 * constant for storing chain code in properties table
36 private static final String CHAIN_ID = "chain_code";
38 private Hashtable<String, Object> properties;
40 private static final int PDB_ID_LENGTH = 4;
42 private String fileName;
52 // TODO is FILE needed; if not is this enum needed, or can we
53 // use FileFormatI for PDB, MMCIF?
54 PDB("pdb", "pdb"), MMCIF("mmcif", "cif"), FILE("?", "?");
57 * file extension for cached structure file; must be one that
58 * is recognised by Chimera 'open' command
59 * @see https://www.cgl.ucsf.edu/chimera/current/docs/UsersGuide/filetypes.html
64 * format specifier used in dbfetch request
65 * @see http://www.ebi.ac.uk/Tools/dbfetch/dbfetch/dbfetch.databases#pdb
69 private Type(String fmt, String ex)
75 public String getFormat()
80 public String getExtension()
86 * case insensitive matching for Type enum
91 public static Type getType(String value)
93 for (Type t : Type.values())
95 if (t.toString().equalsIgnoreCase(value))
104 * case insensitive equivalence for strings resolving to PDBEntry type
109 public boolean matches(String t)
111 return (this.toString().equalsIgnoreCase(t));
116 * Answers true if obj is a PDBEntry with the same id and chain code (both
117 * ignoring case), file, type and properties
120 public boolean equals(Object obj)
122 if (obj == null || !(obj instanceof PDBEntry))
130 PDBEntry o = (PDBEntry) obj;
133 * note that chain code is stored as a property wrapped by a
134 * CaseInsensitiveString, so we are in effect doing a
135 * case-insensitive comparison of chain codes
137 boolean idMatches = id == o.id
138 || (id != null && id.equalsIgnoreCase(o.id));
139 boolean fileMatches = fileName == o.fileName
140 || (fileName != null && fileName.equals(o.fileName));
141 boolean typeMatches = type == o.type
142 || (type != null && type.equals(o.type));
143 if (idMatches && fileMatches && typeMatches)
145 return properties == o.properties
146 || (properties != null && properties.equals(o.properties));
152 * Default constructor
158 public PDBEntry(String pdbId, String chain, PDBEntry.Type type,
161 init(pdbId, chain, type, filePath);
170 private void init(String pdbId, String chain, PDBEntry.Type entryType,
174 this.type = entryType == null ? null : entryType.toString();
175 this.fileName = filePath;
184 public PDBEntry(PDBEntry entry)
186 fileName = entry.fileName;
189 if (entry.properties != null)
191 properties = (Hashtable<String, Object>) entry.properties.clone();
196 * Make a PDBEntry from a DBRefEntry. The accession code is used for the PDB
197 * id, but if it is 5 characters in length, the last character is removed and
198 * set as the chain code instead.
202 public PDBEntry(DBRefEntry dbr)
204 if (!DBRefSource.PDB.equals(dbr.getSource()))
206 throw new IllegalArgumentException(
207 "Invalid source: " + dbr.getSource());
210 String pdbId = dbr.getAccessionId();
211 String chainCode = null;
212 if (pdbId.length() == PDB_ID_LENGTH + 1)
214 char chain = pdbId.charAt(PDB_ID_LENGTH);
215 if (('a' <= chain && chain <= 'z') || ('A' <= chain && chain <= 'Z'))
217 pdbId = pdbId.substring(0, PDB_ID_LENGTH);
218 chainCode = String.valueOf(chain);
221 init(pdbId, chainCode, null, null);
224 public void setFile(String f)
229 public void setFile(File f)
234 public String getFile()
236 return (file != null ? file.getAbsolutePath() : fileName);
239 public File getFileF()
241 return (file == null ? new File(fileName) : file);
244 public void setType(String t)
249 public void setType(PDBEntry.Type type)
251 this.type = type == null ? null : type.toString();
254 public String getType()
259 public void setId(String id)
264 public String getId()
269 public void setProperty(String key, Object value)
271 if (this.properties == null)
273 this.properties = new Hashtable<String, Object>();
275 properties.put(key, value);
278 public Object getProperty(String key)
280 return properties == null ? null : properties.get(key);
284 * Returns an enumeration of the keys of this object's properties (or an empty
285 * enumeration if it has no properties)
289 public Enumeration<String> getProperties()
291 if (properties == null)
293 return Collections.emptyEnumeration();
295 return properties.keys();
300 * @return null or a string for associated chain IDs
302 public String getChainCode()
304 return (properties == null || properties.get(CHAIN_ID) == null) ? null
305 : properties.get(CHAIN_ID).toString();
309 * Sets a non-case-sensitive property for the given chain code. Two PDBEntry
310 * objects which differ only in the case of their chain code are considered
311 * equal. This avoids duplication of objects in lists of PDB ids.
315 public void setChainCode(String chainCode)
317 if (chainCode == null)
319 deleteProperty(CHAIN_ID);
323 setProperty(CHAIN_ID, new CaseInsensitiveString(chainCode));
328 * Deletes the property with the given key, and returns the deleted value (or
331 Object deleteProperty(String key)
333 Object result = null;
334 if (properties != null)
336 result = properties.remove(key);
342 public String toString()
348 * Getter provided for Castor binding only. Application code should call
349 * getProperty() or getProperties() instead.
352 * @see #getProperty(String)
353 * @see #getProperties()
354 * @see jalview.ws.dbsources.Uniprot#getUniprotEntries
358 public Hashtable<String, Object> getProps()
364 * Setter provided for Castor binding only. Application code should call
365 * setProperty() instead.
371 public void setProps(Hashtable<String, Object> props)
377 * Answers true if this object is either equivalent to, or can be 'improved'
378 * by, the given entry.
380 * If newEntry has the same id (ignoring case), and doesn't have a conflicting
381 * file spec or chain code, then update this entry from its file and/or chain
385 * @return true if modifications were made
387 public boolean updateFrom(PDBEntry newEntry)
389 if (this.equals(newEntry))
394 String newId = newEntry.getId();
395 if (newId == null || getId() == null)
397 return false; // shouldn't happen
401 * id has to match (ignoring case)
403 if (!getId().equalsIgnoreCase(newId))
409 * Don't update if associated with different structure files
411 String newFile = newEntry.getFile();
412 if (newFile != null && getFile() != null && !newFile.equals(getFile()))
418 * Don't update if associated with different chains (ignoring case)
420 String newChain = newEntry.getChainCode();
421 if (newChain != null && newChain.length() > 0 && getChainCode() != null
422 && getChainCode().length() > 0
423 && !getChainCode().equalsIgnoreCase(newChain))
429 * set file path if not already set
431 String newType = newEntry.getType();
432 if (getFile() == null && newFile != null)
436 setFile(newEntry.getFileF());
440 * set file type if new entry has it and we don't
441 * (for the case where file was not updated)
443 if (getType() == null && newType != null)
449 * set chain if not already set (we excluded differing
450 * chains earlier) (ignoring case change only)
452 if (newChain != null && newChain.length() > 0
453 && !newChain.equalsIgnoreCase(getChainCode()))
455 setChainCode(newChain);
459 * copy any new or modified properties
461 Enumeration<String> newProps = newEntry.getProperties();
462 while (newProps.hasMoreElements())
465 * copy properties unless value matches; this defends against changing
466 * the case of chain_code which is wrapped in a CaseInsensitiveString
468 String key = newProps.nextElement();
469 Object value = newEntry.getProperty(key);
470 if (!value.equals(getProperty(key)))
472 setProperty(key, value);