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.util.CaseInsensitiveString;
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
158 * Entry point when file is not known and fileType may be string
160 * @param chain may be null
161 * @param fileType "pdb", "mmcif", or "bcif"; null defaults to mmcif
163 public PDBEntry(String pdbId, String chain, String fileType) {
164 this.id = pdbId.toLowerCase();
165 setChainCode(chain); // I note that PDB Chains ARE case-sensitive now
166 if (fileType == null)
168 switch (fileType.toLowerCase()) {
170 this.type = Type.PDB.toString();
173 this.type = Type.MMCIF.toString();
177 System.out.println("format " + fileType + " has not been implemented; using mmCIF");
178 this.type = Type.MMCIF.toString();
183 public PDBEntry(String pdbId, String chain, PDBEntry.Type type,
186 init(pdbId, chain, type, filePath);
195 void init(String pdbId, String chain, PDBEntry.Type entryType,
199 this.type = entryType == null ? null : entryType.toString();
200 this.file = filePath;
209 public PDBEntry(PDBEntry entry)
214 if (entry.properties != null)
216 properties = (Hashtable<String, Object>) entry.properties.clone();
221 * Make a PDBEntry from a DBRefEntry. The accession code is used for the PDB
222 * id, but if it is 5 characters in length, the last character is removed and
223 * set as the chain code instead.
227 public PDBEntry(DBRefEntry dbr)
229 if (!DBRefSource.PDB.equals(dbr.getSource()))
231 throw new IllegalArgumentException(
232 "Invalid source: " + dbr.getSource());
235 String pdbId = dbr.getAccessionId();
236 String chainCode = null;
237 if (pdbId.length() == PDB_ID_LENGTH + 1)
239 char chain = pdbId.charAt(PDB_ID_LENGTH);
240 if (('a' <= chain && chain <= 'z') || ('A' <= chain && chain <= 'Z'))
242 pdbId = pdbId.substring(0, PDB_ID_LENGTH);
243 chainCode = String.valueOf(chain);
246 init(pdbId, chainCode, null, null);
249 public void setFile(String f)
254 public String getFile()
259 public void setType(String t)
264 public void setType(PDBEntry.Type type)
266 this.type = type == null ? null : type.toString();
269 public String getType()
274 public void setId(String id)
279 public String getId()
287 * @param key "protocol"
290 public void setProperty(String key, Object value)
292 if (this.properties == null)
294 this.properties = new Hashtable<String, Object>();
296 properties.put(key, value);
299 public Object getProperty(String key)
301 return properties == null ? null : properties.get(key);
305 * Returns an enumeration of the keys of this object's properties (or an empty
306 * enumeration if it has no properties)
310 public Enumeration<String> getProperties()
312 if (properties == null)
314 return Collections.emptyEnumeration();
316 return properties.keys();
321 * @return null or a string for associated chain IDs
323 public String getChainCode()
325 return (properties == null || properties.get(CHAIN_ID) == null) ? null
326 : properties.get(CHAIN_ID).toString();
330 * Sets a non-case-sensitive property for the given chain code. Two PDBEntry
331 * objects which differ only in the case of their chain code are considered
332 * equal. This avoids duplication of objects in lists of PDB ids.
336 public void setChainCode(String chainCode)
338 if (chainCode == null)
340 deleteProperty(CHAIN_ID);
344 setProperty(CHAIN_ID, new CaseInsensitiveString(chainCode));
349 * Deletes the property with the given key, and returns the deleted value (or
352 Object deleteProperty(String key)
354 Object result = null;
355 if (properties != null)
357 result = properties.remove(key);
363 public String toString()
369 * Getter provided for Castor binding only. Application code should call
370 * getProperty() or getProperties() instead.
373 * @see #getProperty(String)
374 * @see #getProperties()
375 * @see jalview.ws.dbsources.Uniprot#getUniprotEntries
379 public Hashtable<String, Object> getProps()
385 * Setter provided for Castor binding only. Application code should call
386 * setProperty() instead.
392 public void setProps(Hashtable<String, Object> props)
398 * Answers true if this object is either equivalent to, or can be 'improved'
399 * by, the given entry.
401 * If newEntry has the same id (ignoring case), and doesn't have a conflicting
402 * file spec or chain code, then update this entry from its file and/or chain
406 * @return true if modifications were made
408 public boolean updateFrom(PDBEntry newEntry)
410 if (this.equals(newEntry))
415 String newId = newEntry.getId();
416 if (newId == null || getId() == null)
418 return false; // shouldn't happen
421 boolean idMatches = getId().equalsIgnoreCase(newId);
424 * Don't update if associated with different structure files
426 String newFile = newEntry.getFile();
427 if (newFile != null && getFile() != null)
429 if (!newFile.equals(getFile()))
438 // this shouldn't happen, but could do if the id from the
439 // file is not the same as the id from the authority that provided
441 if (!newEntry.fakedPDBId())
444 } // otherwise we can update
450 // one has data, one doesn't ..
454 } // otherwise maybe can update
458 * Don't update if associated with different chains (ignoring case)
460 String newChain = newEntry.getChainCode();
461 if (newChain != null && newChain.length() > 0 && getChainCode() != null
462 && getChainCode().length() > 0
463 && !getChainCode().equalsIgnoreCase(newChain))
469 * set file path if not already set
471 String newType = newEntry.getType();
472 if (getFile() == null && newFile != null)
479 * set file type if new entry has it and we don't
480 * (for the case where file was not updated)
482 if (getType() == null && newType != null)
488 * set chain if not already set (we excluded differing
489 * chains earlier) (ignoring case change only)
491 if (newChain != null && newChain.length() > 0
492 && !newChain.equalsIgnoreCase(getChainCode()))
494 setChainCode(newChain);
498 * copy any new or modified properties
500 Enumeration<String> newProps = newEntry.getProperties();
501 while (newProps.hasMoreElements())
504 * copy properties unless value matches; this defends against changing
505 * the case of chain_code which is wrapped in a CaseInsensitiveString
507 String key = newProps.nextElement();
508 Object value = newEntry.getProperty(key);
509 if (FAKED_ID.equals(key))
511 // we never update the fake ID property
514 if (!value.equals(getProperty(key)))
516 setProperty(key, value);
523 * set when Jalview has manufactured the ID using a local filename
526 public boolean fakedPDBId()
528 if (_hasProperty(FAKED_ID))
534 public void setFakedPDBId(boolean faked)
538 setProperty(FAKED_ID, Boolean.TRUE);
542 if (properties!=null) {
543 properties.remove(FAKED_ID);
548 private boolean _hasProperty(final String key)
550 return (properties != null && properties.containsKey(key));
553 private static final String RETRIEVE_FROM = "RETRIEVE_FROM";
555 private static final String PROVIDER = "PROVIDER";
557 private static final String MODELPAGE = "PROVIDERPAGE";
560 * Permanent URI for retrieving the original structure data
564 public void setRetrievalUrl(String urlStr)
566 setProperty(RETRIEVE_FROM, urlStr);
569 public boolean hasRetrievalUrl()
571 return _hasProperty(RETRIEVE_FROM);
575 * get the Permanent URI for retrieving the original structure data
577 public String getRetrievalUrl()
579 return (String) getProperty(RETRIEVE_FROM);
583 * Data provider name - from 3D Beacons
587 public void setProvider(String provider)
589 setProperty(PROVIDER, provider);
593 * Get Data provider name - from 3D Beacons
596 public String getProvider()
598 return (String) getProperty(PROVIDER);
602 * Permanent URI for retrieving the original structure data
606 public void setProviderPage(String urlStr)
608 setProperty(MODELPAGE, urlStr);
612 * get the Permanent URI for retrieving the original structure data
614 public String getProviderPage()
616 return (String) getProperty(MODELPAGE);
619 public boolean hasProviderPage()
621 return _hasProperty(MODELPAGE);
624 public boolean hasProvider()
626 return _hasProperty(PROVIDER);