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 data's filename
44 private static final String FAKED_ID = "faked_pdbid";
46 * property set when the id is authoritative, and should be used in preference to any identifiers in the structure data
48 private static final String AUTHORITATIVE_ID = "authoritative_pdbid";
58 // TODO is FILE needed; if not is this enum needed, or can we
59 // use FileFormatI for PDB, MMCIF?
60 PDB("pdb", "pdb"), MMCIF("mmcif", "cif"), BCIF("bcif","bcif"),FILE("?", "?");
63 * file extension for cached structure file; must be one that
64 * is recognised by Chimera 'open' command
65 * @see https://www.cgl.ucsf.edu/chimera/current/docs/UsersGuide/filetypes.html
70 * format specifier used in dbfetch request
71 * @see http://www.ebi.ac.uk/Tools/dbfetch/dbfetch/dbfetch.databases#pdb
75 private Type(String fmt, String ex)
81 public String getFormat()
86 public String getExtension()
92 * case insensitive matching for Type enum
97 public static Type getType(String value)
99 for (Type t : Type.values())
101 if (t.toString().equalsIgnoreCase(value))
110 * case insensitive equivalence for strings resolving to PDBEntry type
115 public boolean matches(String t)
117 return (this.toString().equalsIgnoreCase(t));
122 * Answers true if obj is a PDBEntry with the same id and chain code (both
123 * ignoring case), file, type and properties
126 public boolean equals(Object obj)
128 if (obj == null || !(obj instanceof PDBEntry))
136 PDBEntry o = (PDBEntry) obj;
139 * note that chain code is stored as a property wrapped by a
140 * CaseInsensitiveString, so we are in effect doing a
141 * case-insensitive comparison of chain codes
143 boolean idMatches = id == o.id
144 || (id != null && id.equalsIgnoreCase(o.id));
145 boolean fileMatches = file == o.file
146 || (file != null && file.equals(o.file));
147 boolean typeMatches = type == o.type
148 || (type != null && type.equals(o.type));
149 if (idMatches && fileMatches && typeMatches)
151 return properties == o.properties
152 || (properties != null && properties.equals(o.properties));
158 * Default constructor
164 public PDBEntry(String pdbId, String chain, PDBEntry.Type type,
167 init(pdbId, chain, type, filePath);
176 void init(String pdbId, String chain, PDBEntry.Type entryType,
180 this.type = entryType == null ? null : entryType.toString();
181 this.file = filePath;
190 public PDBEntry(PDBEntry entry)
195 if (entry.properties != null)
197 properties = (Hashtable<String, Object>) entry.properties.clone();
202 * Make a PDBEntry from a DBRefEntry. The accession code is used for the PDB
203 * id, but if it is 5 characters in length, the last character is removed and
204 * set as the chain code instead.
208 public PDBEntry(DBRefEntry dbr)
210 if (!DBRefSource.PDB.equals(dbr.getSource()))
212 throw new IllegalArgumentException(
213 "Invalid source: " + dbr.getSource());
216 String pdbId = dbr.getAccessionId();
217 String chainCode = null;
218 if (pdbId.length() == PDB_ID_LENGTH + 1)
220 char chain = pdbId.charAt(PDB_ID_LENGTH);
221 if (('a' <= chain && chain <= 'z') || ('A' <= chain && chain <= 'Z'))
223 pdbId = pdbId.substring(0, PDB_ID_LENGTH);
224 chainCode = String.valueOf(chain);
227 init(pdbId, chainCode, null, null);
230 public void setFile(String f)
235 public String getFile()
240 public void setType(String t)
245 public void setType(PDBEntry.Type type)
247 this.type = type == null ? null : type.toString();
250 public String getType()
255 public void setId(String id)
260 public String getId()
265 public void setProperty(String key, Object value)
267 if (this.properties == null)
269 this.properties = new Hashtable<String, Object>();
271 properties.put(key, value);
274 public Object getProperty(String key)
276 return properties == null ? null : properties.get(key);
280 * Returns an enumeration of the keys of this object's properties (or an empty
281 * enumeration if it has no properties)
285 public Enumeration<String> getProperties()
287 if (properties == null)
289 return Collections.emptyEnumeration();
291 return properties.keys();
296 * @return null or a string for associated chain IDs
298 public String getChainCode()
300 return (properties == null || properties.get(CHAIN_ID) == null) ? null
301 : properties.get(CHAIN_ID).toString();
305 * Sets a non-case-sensitive property for the given chain code. Two PDBEntry
306 * objects which differ only in the case of their chain code are considered
307 * equal. This avoids duplication of objects in lists of PDB ids.
311 public void setChainCode(String chainCode)
313 if (chainCode == null)
315 deleteProperty(CHAIN_ID);
319 setProperty(CHAIN_ID, new CaseInsensitiveString(chainCode));
324 * Deletes the property with the given key, and returns the deleted value (or
327 Object deleteProperty(String key)
329 Object result = null;
330 if (properties != null)
332 result = properties.remove(key);
338 public String toString()
344 * Getter provided for Castor binding only. Application code should call
345 * getProperty() or getProperties() instead.
348 * @see #getProperty(String)
349 * @see #getProperties()
350 * @see jalview.ws.dbsources.Uniprot#getUniprotEntries
354 public Hashtable<String, Object> getProps()
360 * Setter provided for Castor binding only. Application code should call
361 * setProperty() instead.
367 public void setProps(Hashtable<String, Object> props)
373 * Answers true if this object is either equivalent to, or can be 'improved'
374 * by, the given entry.
376 * If newEntry has the same id (ignoring case), and doesn't have a conflicting
377 * file spec or chain code, then update this entry from its file and/or chain
381 * @return true if modifications were made
383 public boolean updateFrom(PDBEntry newEntry)
385 if (this.equals(newEntry))
390 String newId = newEntry.getId();
391 if (newId == null || getId() == null)
393 return false; // shouldn't happen
396 boolean idMatches = getId().equalsIgnoreCase(newId);
399 * Don't update if associated with different structure files
401 String newFile = newEntry.getFile();
402 if (newFile != null && getFile() != null)
404 if (!newFile.equals(getFile()))
413 // this shouldn't happen, but could do if the id from the
414 // file is not the same as the id from the authority that provided
416 if (!newEntry.fakedPDBId() && !isAuthoritative())
419 } // otherwise we can update
425 // one has data, one doesn't ..
429 } // otherwise maybe can update
433 * Don't update if associated with different chains (ignoring case)
435 String newChain = newEntry.getChainCode();
436 if (newChain != null && newChain.length() > 0 && getChainCode() != null
437 && getChainCode().length() > 0
438 && !getChainCode().equalsIgnoreCase(newChain))
444 * set file path if not already set
446 String newType = newEntry.getType();
447 if (getFile() == null && newFile != null)
454 * set file type if new entry has it and we don't
455 * (for the case where file was not updated)
457 if (getType() == null && newType != null)
463 * set chain if not already set (we excluded differing
464 * chains earlier) (ignoring case change only)
466 if (newChain != null && newChain.length() > 0
467 && !newChain.equalsIgnoreCase(getChainCode()))
469 setChainCode(newChain);
473 * copy any new or modified properties
475 Enumeration<String> newProps = newEntry.getProperties();
476 while (newProps.hasMoreElements())
479 * copy properties unless value matches; this defends against changing
480 * the case of chain_code which is wrapped in a CaseInsensitiveString
482 String key = newProps.nextElement();
483 Object value = newEntry.getProperty(key);
484 if (FAKED_ID.equals(key) || AUTHORITATIVE_ID.equals(key))
486 // we never update the fake ID property
489 if (!value.equals(getProperty(key)))
491 setProperty(key, value);
497 public void setAuthoritative(boolean isAuthoritative)
499 setProperty(AUTHORITATIVE_ID, Boolean.valueOf(isAuthoritative));
504 * @return true if the identifier should be preferred over any identifiers
505 * embedded in the structure data
507 public boolean isAuthoritative()
509 if (_hasProperty(AUTHORITATIVE_ID))
511 return ((Boolean)getProperty(AUTHORITATIVE_ID));
517 * set when Jalview has manufactured the ID using a local filename
520 public boolean fakedPDBId()
522 if (_hasProperty(FAKED_ID))
528 public void setFakedPDBId(boolean faked)
532 setProperty(FAKED_ID, Boolean.TRUE);
536 if (properties!=null) {
537 properties.remove(FAKED_ID);
542 private boolean _hasProperty(final String key)
544 return (properties != null && properties.containsKey(key));
547 private static final String RETRIEVE_FROM = "RETRIEVE_FROM";
549 private static final String PROVIDER = "PROVIDER";
551 private static final String MODELPAGE = "PROVIDERPAGE";
554 * Permanent URI for retrieving the original structure data
558 public void setRetrievalUrl(String urlStr)
560 setProperty(RETRIEVE_FROM, urlStr);
563 public boolean hasRetrievalUrl()
565 return _hasProperty(RETRIEVE_FROM);
569 * get the Permanent URI for retrieving the original structure data
571 public String getRetrievalUrl()
573 return (String) getProperty(RETRIEVE_FROM);
577 * Data provider name - from 3D Beacons
581 public void setProvider(String provider)
583 setProperty(PROVIDER, provider);
587 * Get Data provider name - from 3D Beacons
590 public String getProvider()
592 return (String) getProperty(PROVIDER);
596 * Permanent URI for retrieving the original structure data
600 public void setProviderPage(String urlStr)
602 setProperty(MODELPAGE, urlStr);
606 * get the Permanent URI for retrieving the original structure data
608 public String getProviderPage()
610 return (String) getProperty(MODELPAGE);
613 public boolean hasProviderPage()
615 return _hasProperty(MODELPAGE);
618 public boolean hasProvider()
620 return _hasProperty(PROVIDER);