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;
42 * property set when id is a 'manufactured' identifier from the structure
45 private static final String FAKED_ID = "faked_pdbid";
48 * property set when the id is authoritative, and should be used in preference
49 * to any identifiers in the structure data
51 private static final String AUTHORITATIVE_ID = "authoritative_pdbid";
61 // TODO is FILE needed; if not is this enum needed, or can we
62 // use FileFormatI for PDB, MMCIF?
63 PDB("pdb", "pdb"), MMCIF("mmcif", "cif"), BCIF("bcif", "bcif"),
67 * file extension for cached structure file; must be one that
68 * is recognised by Chimera 'open' command
69 * @see https://www.cgl.ucsf.edu/chimera/current/docs/UsersGuide/filetypes.html
74 * format specifier used in dbfetch request
75 * @see http://www.ebi.ac.uk/Tools/dbfetch/dbfetch/dbfetch.databases#pdb
79 private Type(String fmt, String ex)
85 public String getFormat()
90 public String getExtension()
96 * case insensitive matching for Type enum
101 public static Type getType(String value)
103 for (Type t : Type.values())
105 if (t.toString().equalsIgnoreCase(value))
114 * case insensitive equivalence for strings resolving to PDBEntry type
119 public boolean matches(String t)
121 return (this.toString().equalsIgnoreCase(t));
126 * Answers true if obj is a PDBEntry with the same id and chain code (both
127 * ignoring case), file, type and properties
130 public boolean equals(Object obj)
132 if (obj == null || !(obj instanceof PDBEntry))
140 PDBEntry o = (PDBEntry) obj;
143 * note that chain code is stored as a property wrapped by a
144 * CaseInsensitiveString, so we are in effect doing a
145 * case-insensitive comparison of chain codes
147 boolean idMatches = id == o.id
148 || (id != null && id.equalsIgnoreCase(o.id));
149 boolean fileMatches = file == o.file
150 || (file != null && file.equals(o.file));
151 boolean typeMatches = type == o.type
152 || (type != null && type.equals(o.type));
153 if (idMatches && fileMatches && typeMatches)
155 return properties == o.properties
156 || (properties != null && properties.equals(o.properties));
162 * Default constructor
168 public PDBEntry(String pdbId, String chain, PDBEntry.Type type,
171 init(pdbId, chain, type, filePath);
180 void init(String pdbId, String chain, PDBEntry.Type entryType,
184 this.type = entryType == null ? null : entryType.toString();
185 this.file = filePath;
194 public PDBEntry(PDBEntry entry)
199 if (entry.properties != null)
201 properties = (Hashtable<String, Object>) entry.properties.clone();
206 * Make a PDBEntry from a DBRefEntry. The accession code is used for the PDB
207 * id, but if it is 5 characters in length, the last character is removed and
208 * set as the chain code instead.
212 public PDBEntry(DBRefEntry dbr)
214 if (!DBRefSource.PDB.equals(dbr.getSource()))
216 throw new IllegalArgumentException(
217 "Invalid source: " + dbr.getSource());
220 String pdbId = dbr.getAccessionId();
221 String chainCode = null;
222 if (pdbId.length() == PDB_ID_LENGTH + 1)
224 char chain = pdbId.charAt(PDB_ID_LENGTH);
225 if (('a' <= chain && chain <= 'z') || ('A' <= chain && chain <= 'Z'))
227 pdbId = pdbId.substring(0, PDB_ID_LENGTH);
228 chainCode = String.valueOf(chain);
231 init(pdbId, chainCode, null, null);
234 public void setFile(String f)
239 public String getFile()
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
400 boolean idMatches = getId().equalsIgnoreCase(newId);
403 * Don't update if associated with different structure files
405 String newFile = newEntry.getFile();
406 if (newFile != null && getFile() != null)
408 if (!newFile.equals(getFile()))
417 // this shouldn't happen, but could do if the id from the
418 // file is not the same as the id from the authority that provided
420 if (!newEntry.fakedPDBId() && !isAuthoritative())
423 } // otherwise we can update
429 // one has data, one doesn't ..
433 } // otherwise maybe can update
437 * Don't update if associated with different chains (ignoring case)
439 String newChain = newEntry.getChainCode();
440 if (newChain != null && newChain.length() > 0 && getChainCode() != null
441 && getChainCode().length() > 0
442 && !getChainCode().equalsIgnoreCase(newChain))
448 * set file path if not already set
450 String newType = newEntry.getType();
451 if (getFile() == null && newFile != null)
458 * set file type if new entry has it and we don't
459 * (for the case where file was not updated)
461 if (getType() == null && newType != null)
467 * set chain if not already set (we excluded differing
468 * chains earlier) (ignoring case change only)
470 if (newChain != null && newChain.length() > 0
471 && !newChain.equalsIgnoreCase(getChainCode()))
473 setChainCode(newChain);
477 * copy any new or modified properties
479 Enumeration<String> newProps = newEntry.getProperties();
480 while (newProps.hasMoreElements())
483 * copy properties unless value matches; this defends against changing
484 * the case of chain_code which is wrapped in a CaseInsensitiveString
486 String key = newProps.nextElement();
487 Object value = newEntry.getProperty(key);
488 if (FAKED_ID.equals(key) || AUTHORITATIVE_ID.equals(key))
490 // we never update the fake ID property
493 if (!value.equals(getProperty(key)))
495 setProperty(key, value);
501 public void setAuthoritative(boolean isAuthoritative)
503 setProperty(AUTHORITATIVE_ID, Boolean.valueOf(isAuthoritative));
508 * @return true if the identifier should be preferred over any identifiers
509 * embedded in the structure data
511 public boolean isAuthoritative()
513 if (_hasProperty(AUTHORITATIVE_ID))
515 return ((Boolean) getProperty(AUTHORITATIVE_ID));
521 * set when Jalview has manufactured the ID using a local filename
525 public boolean fakedPDBId()
527 if (_hasProperty(FAKED_ID))
534 public void setFakedPDBId(boolean faked)
538 setProperty(FAKED_ID, Boolean.TRUE);
542 if (properties != null)
544 properties.remove(FAKED_ID);
549 private boolean _hasProperty(final String key)
551 return (properties != null && properties.containsKey(key));
554 private static final String RETRIEVE_FROM = "RETRIEVE_FROM";
556 private static final String PROVIDER = "PROVIDER";
558 private static final String MODELPAGE = "PROVIDERPAGE";
561 * Permanent URI for retrieving the original structure data
565 public void setRetrievalUrl(String urlStr)
567 setProperty(RETRIEVE_FROM, urlStr);
570 public boolean hasRetrievalUrl()
572 return _hasProperty(RETRIEVE_FROM);
576 * get the Permanent URI for retrieving the original structure data
578 public String getRetrievalUrl()
580 return (String) getProperty(RETRIEVE_FROM);
584 * Data provider name - from 3D Beacons
588 public void setProvider(String provider)
590 setProperty(PROVIDER, provider);
594 * Get Data provider name - from 3D Beacons
597 public String getProvider()
599 return (String) getProperty(PROVIDER);
603 * Permanent URI for retrieving the original structure data
607 public void setProviderPage(String urlStr)
609 setProperty(MODELPAGE, urlStr);
613 * get the Permanent URI for retrieving the original structure data
615 public String getProviderPage()
617 return (String) getProperty(MODELPAGE);
620 public boolean hasProviderPage()
622 return _hasProperty(MODELPAGE);
625 public boolean hasProvider()
627 return _hasProperty(PROVIDER);