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 enum needed, or can we
50 // use FileFormatI for PDB, MMCIF?
51 PDB("pdb", "pdb"), MMCIF("mmcif", "cif"), FILE("?", "?");
54 * file extension for cached structure file; must be one that
55 * is recognised by Chimera 'open' command
56 * @see https://www.cgl.ucsf.edu/chimera/current/docs/UsersGuide/filetypes.html
61 * format specifier used in dbfetch request
62 * @see http://www.ebi.ac.uk/Tools/dbfetch/dbfetch/dbfetch.databases#pdb
66 private Type(String fmt, String ex)
72 public String getFormat()
77 public String getExtension()
83 * case insensitive matching for Type enum
88 public static Type getType(String value)
90 for (Type t : Type.values())
92 if (t.toString().equalsIgnoreCase(value))
101 * case insensitive equivalence for strings resolving to PDBEntry type
106 public boolean matches(String t)
108 return (this.toString().equalsIgnoreCase(t));
113 * Answers true if obj is a PDBEntry with the same id and chain code (both
114 * ignoring case), file, type and properties
117 public boolean equals(Object obj)
119 if (obj == null || !(obj instanceof PDBEntry))
127 PDBEntry o = (PDBEntry) obj;
130 * note that chain code is stored as a property wrapped by a
131 * CaseInsensitiveString, so we are in effect doing a
132 * case-insensitive comparison of chain codes
134 boolean idMatches = id == o.id
135 || (id != null && id.equalsIgnoreCase(o.id));
136 boolean fileMatches = file == o.file
137 || (file != null && file.equals(o.file));
138 boolean typeMatches = type == o.type
139 || (type != null && type.equals(o.type));
140 if (idMatches && fileMatches && typeMatches)
142 return properties == o.properties
143 || (properties != null && properties.equals(o.properties));
149 * Default constructor
155 public PDBEntry(String pdbId, String chain, PDBEntry.Type type,
158 init(pdbId, chain, type, filePath);
167 void init(String pdbId, String chain, PDBEntry.Type entryType,
171 this.type = entryType == null ? null : entryType.toString();
172 this.file = filePath;
181 public PDBEntry(PDBEntry entry)
186 if (entry.properties != null)
188 properties = (Hashtable<String, Object>) entry.properties.clone();
193 * Make a PDBEntry from a DBRefEntry. The accession code is used for the PDB
194 * id, but if it is 5 characters in length, the last character is removed and
195 * set as the chain code instead.
199 public PDBEntry(DBRefEntry dbr)
201 if (!DBRefSource.PDB.equals(dbr.getSource()))
203 throw new IllegalArgumentException(
204 "Invalid source: " + dbr.getSource());
207 String pdbId = dbr.getAccessionId();
208 String chainCode = null;
209 if (pdbId.length() == PDB_ID_LENGTH + 1)
211 char chain = pdbId.charAt(PDB_ID_LENGTH);
212 if (('a' <= chain && chain <= 'z') || ('A' <= chain && chain <= 'Z'))
214 pdbId = pdbId.substring(0, PDB_ID_LENGTH);
215 chainCode = String.valueOf(chain);
218 init(pdbId, chainCode, null, null);
221 public void setFile(String f)
226 public String getFile()
231 public void setType(String t)
236 public void setType(PDBEntry.Type type)
238 this.type = type == null ? null : type.toString();
241 public String getType()
246 public void setId(String id)
251 public String getId()
256 public void setProperty(String key, Object value)
258 if (this.properties == null)
260 this.properties = new Hashtable<String, Object>();
262 properties.put(key, value);
265 public Object getProperty(String key)
267 return properties == null ? null : properties.get(key);
271 * Returns an enumeration of the keys of this object's properties (or an empty
272 * enumeration if it has no properties)
276 public Enumeration<String> getProperties()
278 if (properties == null)
280 return Collections.emptyEnumeration();
282 return properties.keys();
287 * @return null or a string for associated chain IDs
289 public String getChainCode()
291 return (properties == null || properties.get(CHAIN_ID) == null) ? null
292 : properties.get(CHAIN_ID).toString();
296 * Sets a non-case-sensitive property for the given chain code. Two PDBEntry
297 * objects which differ only in the case of their chain code are considered
298 * equal. This avoids duplication of objects in lists of PDB ids.
302 public void setChainCode(String chainCode)
304 if (chainCode == null)
306 deleteProperty(CHAIN_ID);
310 setProperty(CHAIN_ID, new CaseInsensitiveString(chainCode));
315 * Deletes the property with the given key, and returns the deleted value (or
318 Object deleteProperty(String key)
320 Object result = null;
321 if (properties != null)
323 result = properties.remove(key);
329 public String toString()
335 * Getter provided for Castor binding only. Application code should call
336 * getProperty() or getProperties() instead.
339 * @see #getProperty(String)
340 * @see #getProperties()
341 * @see jalview.ws.dbsources.Uniprot#getUniprotEntries
345 public Hashtable<String, Object> getProps()
351 * Setter provided for Castor binding only. Application code should call
352 * setProperty() instead.
358 public void setProps(Hashtable<String, Object> props)
364 * Answers true if this object is either equivalent to, or can be 'improved'
365 * by, the given entry.
367 * If newEntry has the same id (ignoring case), and doesn't have a conflicting
368 * file spec or chain code, then update this entry from its file and/or chain
372 * @return true if modifications were made
374 public boolean updateFrom(PDBEntry newEntry)
376 if (this.equals(newEntry))
381 String newId = newEntry.getId();
382 if (newId == null || getId() == null)
384 return false; // shouldn't happen
387 boolean idMatches = getId().equalsIgnoreCase(newId);
390 * Don't update if associated with different structure files
392 String newFile = newEntry.getFile();
393 if (newFile != null && getFile() != null)
395 if (!newFile.equals(getFile()))
404 // this shouldn't happen, but could do if the id from the
405 // file is not the same as the id from the authority that provided
413 // one has data, one doesn't ..
417 } // otherwise maybe can update
421 * Don't update if associated with different chains (ignoring case)
423 String newChain = newEntry.getChainCode();
424 if (newChain != null && newChain.length() > 0 && getChainCode() != null
425 && getChainCode().length() > 0
426 && !getChainCode().equalsIgnoreCase(newChain))
432 * set file path if not already set
434 String newType = newEntry.getType();
435 if (getFile() == null && newFile != null)
442 * set file type if new entry has it and we don't
443 * (for the case where file was not updated)
445 if (getType() == null && newType != null)
451 * set chain if not already set (we excluded differing
452 * chains earlier) (ignoring case change only)
454 if (newChain != null && newChain.length() > 0
455 && !newChain.equalsIgnoreCase(getChainCode()))
457 setChainCode(newChain);
461 * copy any new or modified properties
463 Enumeration<String> newProps = newEntry.getProperties();
464 while (newProps.hasMoreElements())
467 * copy properties unless value matches; this defends against changing
468 * the case of chain_code which is wrapped in a CaseInsensitiveString
470 String key = newProps.nextElement();
471 Object value = newEntry.getProperty(key);
472 if (!value.equals(getProperty(key)))
474 setProperty(key, value);
481 private static final String RETRIEVE_FROM="RETRIEVE_FROM";
483 * Permanent URI for retrieving the original structure data
486 public void setRetrievalUrl(String urlStr)
488 setProperty(RETRIEVE_FROM, urlStr);
491 * get the Permanent URI for retrieving the original structure data
493 public String getRetrievalUrl()
495 return (String) getProperty(RETRIEVE_FROM);
498 public boolean hasRetrievalUrl()
500 return (properties!=null && properties.containsKey(RETRIEVE_FROM));