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;
41 private static final String FAKED_ID = "faked_pdbid";
51 // TODO is FILE needed; if not is this enum needed, or can we
52 // use FileFormatI for PDB, MMCIF?
53 PDB("pdb", "pdb"), MMCIF("mmcif", "cif"), BCIF("bcif","bcif"),FILE("?", "?");
56 * file extension for cached structure file; must be one that
57 * is recognised by Chimera 'open' command
58 * @see https://www.cgl.ucsf.edu/chimera/current/docs/UsersGuide/filetypes.html
63 * format specifier used in dbfetch request
64 * @see http://www.ebi.ac.uk/Tools/dbfetch/dbfetch/dbfetch.databases#pdb
68 private Type(String fmt, String ex)
74 public String getFormat()
79 public String getExtension()
85 * case insensitive matching for Type enum
90 public static Type getType(String value)
92 for (Type t : Type.values())
94 if (t.toString().equalsIgnoreCase(value))
103 * case insensitive equivalence for strings resolving to PDBEntry type
108 public boolean matches(String t)
110 return (this.toString().equalsIgnoreCase(t));
115 * Answers true if obj is a PDBEntry with the same id and chain code (both
116 * ignoring case), file, type and properties
119 public boolean equals(Object obj)
121 if (obj == null || !(obj instanceof PDBEntry))
129 PDBEntry o = (PDBEntry) obj;
132 * note that chain code is stored as a property wrapped by a
133 * CaseInsensitiveString, so we are in effect doing a
134 * case-insensitive comparison of chain codes
136 boolean idMatches = id == o.id
137 || (id != null && id.equalsIgnoreCase(o.id));
138 boolean fileMatches = file == o.file
139 || (file != null && file.equals(o.file));
140 boolean typeMatches = type == o.type
141 || (type != null && type.equals(o.type));
142 if (idMatches && fileMatches && typeMatches)
144 return properties == o.properties
145 || (properties != null && properties.equals(o.properties));
151 * Default constructor
157 public PDBEntry(String pdbId, String chain, PDBEntry.Type type,
160 init(pdbId, chain, type, filePath);
169 void init(String pdbId, String chain, PDBEntry.Type entryType,
173 this.type = entryType == null ? null : entryType.toString();
174 this.file = filePath;
183 public PDBEntry(PDBEntry entry)
188 if (entry.properties != null)
190 properties = (Hashtable<String, Object>) entry.properties.clone();
195 * Make a PDBEntry from a DBRefEntry. The accession code is used for the PDB
196 * id, but if it is 5 characters in length, the last character is removed and
197 * set as the chain code instead.
201 public PDBEntry(DBRefEntry dbr)
203 if (!DBRefSource.PDB.equals(dbr.getSource()))
205 throw new IllegalArgumentException(
206 "Invalid source: " + dbr.getSource());
209 String pdbId = dbr.getAccessionId();
210 String chainCode = null;
211 if (pdbId.length() == PDB_ID_LENGTH + 1)
213 char chain = pdbId.charAt(PDB_ID_LENGTH);
214 if (('a' <= chain && chain <= 'z') || ('A' <= chain && chain <= 'Z'))
216 pdbId = pdbId.substring(0, PDB_ID_LENGTH);
217 chainCode = String.valueOf(chain);
220 init(pdbId, chainCode, null, null);
223 public void setFile(String f)
228 public String getFile()
233 public void setType(String t)
238 public void setType(PDBEntry.Type type)
240 this.type = type == null ? null : type.toString();
243 public String getType()
248 public void setId(String id)
253 public String getId()
258 public void setProperty(String key, Object value)
260 if (this.properties == null)
262 this.properties = new Hashtable<String, Object>();
264 properties.put(key, value);
267 public Object getProperty(String key)
269 return properties == null ? null : properties.get(key);
273 * Returns an enumeration of the keys of this object's properties (or an empty
274 * enumeration if it has no properties)
278 public Enumeration<String> getProperties()
280 if (properties == null)
282 return Collections.emptyEnumeration();
284 return properties.keys();
289 * @return null or a string for associated chain IDs
291 public String getChainCode()
293 return (properties == null || properties.get(CHAIN_ID) == null) ? null
294 : properties.get(CHAIN_ID).toString();
298 * Sets a non-case-sensitive property for the given chain code. Two PDBEntry
299 * objects which differ only in the case of their chain code are considered
300 * equal. This avoids duplication of objects in lists of PDB ids.
304 public void setChainCode(String chainCode)
306 if (chainCode == null)
308 deleteProperty(CHAIN_ID);
312 setProperty(CHAIN_ID, new CaseInsensitiveString(chainCode));
317 * Deletes the property with the given key, and returns the deleted value (or
320 Object deleteProperty(String key)
322 Object result = null;
323 if (properties != null)
325 result = properties.remove(key);
331 public String toString()
337 * Getter provided for Castor binding only. Application code should call
338 * getProperty() or getProperties() instead.
341 * @see #getProperty(String)
342 * @see #getProperties()
343 * @see jalview.ws.dbsources.Uniprot#getUniprotEntries
347 public Hashtable<String, Object> getProps()
353 * Setter provided for Castor binding only. Application code should call
354 * setProperty() instead.
360 public void setProps(Hashtable<String, Object> props)
366 * Answers true if this object is either equivalent to, or can be 'improved'
367 * by, the given entry.
369 * If newEntry has the same id (ignoring case), and doesn't have a conflicting
370 * file spec or chain code, then update this entry from its file and/or chain
374 * @return true if modifications were made
376 public boolean updateFrom(PDBEntry newEntry)
378 if (this.equals(newEntry))
383 String newId = newEntry.getId();
384 if (newId == null || getId() == null)
386 return false; // shouldn't happen
389 boolean idMatches = getId().equalsIgnoreCase(newId);
392 * Don't update if associated with different structure files
394 String newFile = newEntry.getFile();
395 if (newFile != null && getFile() != null)
397 if (!newFile.equals(getFile()))
406 // this shouldn't happen, but could do if the id from the
407 // file is not the same as the id from the authority that provided
409 if (!newEntry.fakedPDBId())
412 } // otherwise we can update
418 // one has data, one doesn't ..
422 } // otherwise maybe can update
426 * Don't update if associated with different chains (ignoring case)
428 String newChain = newEntry.getChainCode();
429 if (newChain != null && newChain.length() > 0 && getChainCode() != null
430 && getChainCode().length() > 0
431 && !getChainCode().equalsIgnoreCase(newChain))
437 * set file path if not already set
439 String newType = newEntry.getType();
440 if (getFile() == null && newFile != null)
447 * set file type if new entry has it and we don't
448 * (for the case where file was not updated)
450 if (getType() == null && newType != null)
456 * set chain if not already set (we excluded differing
457 * chains earlier) (ignoring case change only)
459 if (newChain != null && newChain.length() > 0
460 && !newChain.equalsIgnoreCase(getChainCode()))
462 setChainCode(newChain);
466 * copy any new or modified properties
468 Enumeration<String> newProps = newEntry.getProperties();
469 while (newProps.hasMoreElements())
472 * copy properties unless value matches; this defends against changing
473 * the case of chain_code which is wrapped in a CaseInsensitiveString
475 String key = newProps.nextElement();
476 Object value = newEntry.getProperty(key);
477 if (FAKED_ID.equals(key))
479 // we never update the fake ID property
482 if (!value.equals(getProperty(key)))
484 setProperty(key, value);
491 * set when Jalview has manufactured the ID using a local filename
494 public boolean fakedPDBId()
496 if (_hasProperty(FAKED_ID))
502 public void setFakedPDBId(boolean faked)
506 setProperty(FAKED_ID, Boolean.TRUE);
510 if (properties!=null) {
511 properties.remove(FAKED_ID);
516 private boolean _hasProperty(final String key)
518 return (properties != null && properties.containsKey(key));
521 private static final String RETRIEVE_FROM = "RETRIEVE_FROM";
523 private static final String PROVIDER = "PROVIDER";
525 private static final String MODELPAGE = "PROVIDERPAGE";
528 * Permanent URI for retrieving the original structure data
532 public void setRetrievalUrl(String urlStr)
534 setProperty(RETRIEVE_FROM, urlStr);
537 public boolean hasRetrievalUrl()
539 return _hasProperty(RETRIEVE_FROM);
543 * get the Permanent URI for retrieving the original structure data
545 public String getRetrievalUrl()
547 return (String) getProperty(RETRIEVE_FROM);
551 * Data provider name - from 3D Beacons
555 public void setProvider(String provider)
557 setProperty(PROVIDER, provider);
561 * Get Data provider name - from 3D Beacons
564 public String getProvider()
566 return (String) getProperty(PROVIDER);
570 * Permanent URI for retrieving the original structure data
574 public void setProviderPage(String urlStr)
576 setProperty(MODELPAGE, urlStr);
580 * get the Permanent URI for retrieving the original structure data
582 public String getProviderPage()
584 return (String) getProperty(MODELPAGE);
587 public boolean hasProviderPage()
589 return _hasProperty(MODELPAGE);
592 public boolean hasProvider()
594 return _hasProperty(PROVIDER);