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 java.util.Collections;
24 import java.util.Enumeration;
25 import java.util.Hashtable;
27 import jalview.io.StructureFile;
28 import jalview.util.CaseInsensitiveString;
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;
43 * property set when id is a 'manufactured' identifier from the structure
46 private static final String FAKED_ID = "faked_pdbid";
49 * property set when the id is authoritative, and should be used in preference
50 * to any identifiers in the structure data
52 private static final String AUTHORITATIVE_ID = "authoritative_pdbid";
60 private StructureFile sf = null;
64 // TODO is FILE needed; if not is this enum needed, or can we
65 // use FileFormatI for PDB, MMCIF?
66 PDB("pdb", "pdb"), MMCIF("mmcif", "cif"), BCIF("bcif", "bcif"),
70 * file extension for cached structure file; must be one that
71 * is recognised by Chimera 'open' command
72 * @see https://www.cgl.ucsf.edu/chimera/current/docs/UsersGuide/filetypes.html
77 * format specifier used in dbfetch request
78 * @see http://www.ebi.ac.uk/Tools/dbfetch/dbfetch/dbfetch.databases#pdb
82 private Type(String fmt, String ex)
88 public String getFormat()
93 public String getExtension()
99 * case insensitive matching for Type enum
104 public static Type getType(String value)
106 for (Type t : Type.values())
108 if (t.toString().equalsIgnoreCase(value))
117 * case insensitive equivalence for strings resolving to PDBEntry type
122 public boolean matches(String t)
124 return (this.toString().equalsIgnoreCase(t));
129 * Answers true if obj is a PDBEntry with the same id and chain code (both
130 * ignoring case), file, type and properties
133 public boolean equals(Object obj)
135 if (obj == null || !(obj instanceof PDBEntry))
143 PDBEntry o = (PDBEntry) obj;
146 * note that chain code is stored as a property wrapped by a
147 * CaseInsensitiveString, so we are in effect doing a
148 * case-insensitive comparison of chain codes
150 boolean idMatches = id == o.id
151 || (id != null && id.equalsIgnoreCase(o.id));
152 boolean fileMatches = file == o.file
153 || (file != null && file.equals(o.file));
154 boolean typeMatches = type == o.type
155 || (type != null && type.equals(o.type));
156 if (idMatches && fileMatches && typeMatches)
158 return properties == o.properties
159 || (properties != null && properties.equals(o.properties));
165 * Default constructor
171 public PDBEntry(String pdbId, String chain, PDBEntry.Type type,
174 init(pdbId, chain, type, filePath);
183 void init(String pdbId, String chain, PDBEntry.Type entryType,
187 this.type = entryType == null ? null : entryType.toString();
188 this.file = filePath;
197 public PDBEntry(PDBEntry entry)
202 if (entry.properties != null)
204 properties = (Hashtable<String, Object>) entry.properties.clone();
209 * Make a PDBEntry from a DBRefEntry. The accession code is used for the PDB
210 * id, but if it is 5 characters in length, the last character is removed and
211 * set as the chain code instead.
215 public PDBEntry(DBRefEntry dbr)
217 if (!DBRefSource.PDB.equals(dbr.getSource()))
219 throw new IllegalArgumentException(
220 "Invalid source: " + dbr.getSource());
223 String pdbId = dbr.getAccessionId();
224 String chainCode = null;
225 if (pdbId.length() == PDB_ID_LENGTH + 1)
227 char chain = pdbId.charAt(PDB_ID_LENGTH);
228 if (('a' <= chain && chain <= 'z') || ('A' <= chain && chain <= 'Z'))
230 pdbId = pdbId.substring(0, PDB_ID_LENGTH);
231 chainCode = String.valueOf(chain);
234 init(pdbId, chainCode, null, null);
237 public void setFile(String f)
242 public String getFile()
247 public void setType(String t)
252 public void setType(PDBEntry.Type type)
254 this.type = type == null ? null : type.toString();
257 public String getType()
262 public void setId(String id)
267 public String getId()
272 public void setProperty(String key, Object value)
274 if (this.properties == null)
276 this.properties = new Hashtable<String, Object>();
278 properties.put(key, value);
281 public Object getProperty(String key)
283 return properties == null ? null : properties.get(key);
287 * Returns an enumeration of the keys of this object's properties (or an empty
288 * enumeration if it has no properties)
292 public Enumeration<String> getProperties()
294 if (properties == null)
296 return Collections.emptyEnumeration();
298 return properties.keys();
303 * @return null or a string for associated chain IDs
305 public String getChainCode()
307 return (properties == null || properties.get(CHAIN_ID) == null) ? null
308 : properties.get(CHAIN_ID).toString();
312 * Sets a non-case-sensitive property for the given chain code. Two PDBEntry
313 * objects which differ only in the case of their chain code are considered
314 * equal. This avoids duplication of objects in lists of PDB ids.
318 public void setChainCode(String chainCode)
320 if (chainCode == null)
322 deleteProperty(CHAIN_ID);
326 setProperty(CHAIN_ID, new CaseInsensitiveString(chainCode));
331 * Deletes the property with the given key, and returns the deleted value (or
334 Object deleteProperty(String key)
336 Object result = null;
337 if (properties != null)
339 result = properties.remove(key);
345 public String toString()
351 * Getter provided for Castor binding only. Application code should call
352 * getProperty() or getProperties() instead.
355 * @see #getProperty(String)
356 * @see #getProperties()
357 * @see jalview.ws.dbsources.Uniprot#getUniprotEntries
361 public Hashtable<String, Object> getProps()
367 * Setter provided for Castor binding only. Application code should call
368 * setProperty() instead.
374 public void setProps(Hashtable<String, Object> props)
380 * Answers true if this object is either equivalent to, or can be 'improved'
381 * by, the given entry.
383 * If newEntry has the same id (ignoring case), and doesn't have a conflicting
384 * file spec or chain code, then update this entry from its file and/or chain
388 * @return true if modifications were made
390 public boolean updateFrom(PDBEntry newEntry)
392 if (this.equals(newEntry))
397 String newId = newEntry.getId();
398 if (newId == null || getId() == null)
400 return false; // shouldn't happen
403 boolean idMatches = getId().equalsIgnoreCase(newId);
406 * Don't update if associated with different structure files
408 String newFile = newEntry.getFile();
409 if (newFile != null && getFile() != null)
411 if (!newFile.equals(getFile()))
420 // this shouldn't happen, but could do if the id from the
421 // file is not the same as the id from the authority that provided
423 if (!newEntry.fakedPDBId() && !isAuthoritative())
426 } // otherwise we can update
432 // one has data, one doesn't ..
436 } // otherwise maybe can update
440 * Don't update if associated with different chains (ignoring case)
442 String newChain = newEntry.getChainCode();
443 if (newChain != null && newChain.length() > 0 && getChainCode() != null
444 && getChainCode().length() > 0
445 && !getChainCode().equalsIgnoreCase(newChain))
451 * set file path if not already set
453 String newType = newEntry.getType();
454 if (getFile() == null && newFile != null)
461 * set file type if new entry has it and we don't
462 * (for the case where file was not updated)
464 if (getType() == null && newType != null)
470 * set chain if not already set (we excluded differing
471 * chains earlier) (ignoring case change only)
473 if (newChain != null && newChain.length() > 0
474 && !newChain.equalsIgnoreCase(getChainCode()))
476 setChainCode(newChain);
480 * copy any new or modified properties
482 Enumeration<String> newProps = newEntry.getProperties();
483 while (newProps.hasMoreElements())
486 * copy properties unless value matches; this defends against changing
487 * the case of chain_code which is wrapped in a CaseInsensitiveString
489 String key = newProps.nextElement();
490 Object value = newEntry.getProperty(key);
491 if (FAKED_ID.equals(key) || AUTHORITATIVE_ID.equals(key))
493 // we never update the fake ID property
496 if (!value.equals(getProperty(key)))
498 setProperty(key, value);
504 public void setAuthoritative(boolean isAuthoritative)
506 setProperty(AUTHORITATIVE_ID, Boolean.valueOf(isAuthoritative));
511 * @return true if the identifier should be preferred over any identifiers
512 * embedded in the structure data
514 public boolean isAuthoritative()
516 if (_hasProperty(AUTHORITATIVE_ID))
518 return ((Boolean) getProperty(AUTHORITATIVE_ID));
524 * set when Jalview has manufactured the ID using a local filename
528 public boolean fakedPDBId()
530 if (_hasProperty(FAKED_ID))
537 public void setFakedPDBId(boolean faked)
541 setProperty(FAKED_ID, Boolean.TRUE);
545 if (properties != null)
547 properties.remove(FAKED_ID);
552 private boolean _hasProperty(final String key)
554 return (properties != null && properties.containsKey(key));
557 private static final String RETRIEVE_FROM = "RETRIEVE_FROM";
559 private static final String PROVIDER = "PROVIDER";
561 private static final String MODELPAGE = "PROVIDERPAGE";
563 private static final String PROVIDERCATEGORY = "PROVIDERCATEGORY";
566 * Permanent URI for retrieving the original structure data
570 public void setRetrievalUrl(String urlStr)
572 setProperty(RETRIEVE_FROM, urlStr);
575 public boolean hasRetrievalUrl()
577 return _hasProperty(RETRIEVE_FROM);
581 * get the Permanent URI for retrieving the original structure data
583 public String getRetrievalUrl()
585 return (String) getProperty(RETRIEVE_FROM);
589 * Data provider name - from 3D Beacons
593 public void setProvider(String provider)
595 setProperty(PROVIDER, provider);
599 * Get Data provider name - from 3D Beacons
602 public String getProvider()
604 return (String) getProperty(PROVIDER);
608 * Permanent URI for retrieving the original structure data
612 public void setProviderPage(String urlStr)
614 setProperty(MODELPAGE, urlStr);
618 * get the Permanent URI for retrieving the original structure data
620 public String getProviderPage()
622 return (String) getProperty(MODELPAGE);
625 public boolean hasProviderPage()
627 return _hasProperty(MODELPAGE);
630 public boolean hasProvider()
632 return _hasProperty(PROVIDER);
635 public StructureFile getStructureFile()
640 public void setStructureFile(StructureFile f)
645 public boolean hasStructureFile()
647 return sf != null && sf.inFile != null && sf.inFile.exists();
650 public void setProviderCategory(String providerCategory)
652 setProperty(PROVIDERCATEGORY, providerCategory);
655 public String getProviderCategory()
657 return (String) getProperty(PROVIDERCATEGORY);
660 public boolean hasProviderCategory()
662 return _hasProperty(PROVIDERCATEGORY);