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;
24 import jalview.ws.params.InvalidArgumentException;
26 import java.util.Collections;
27 import java.util.Enumeration;
28 import java.util.Hashtable;
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;
50 // TODO is FILE needed; if not is this enum needed, or can we
51 // use FileFormatI for PDB, MMCIF?
52 PDB("pdb", "pdb"), MMCIF("mmcif", "cif"), FILE("?", "?");
55 * file extension for cached structure file; must be one that
56 * is recognised by Chimera 'open' command
57 * @see https://www.cgl.ucsf.edu/chimera/current/docs/UsersGuide/filetypes.html
62 * format specifier used in dbfetch request
63 * @see http://www.ebi.ac.uk/Tools/dbfetch/dbfetch/dbfetch.databases#pdb
67 private Type(String fmt, String ex)
73 public String getFormat()
78 public String getExtension()
84 * case insensitive matching for Type enum
89 public static Type getType(String value)
91 for (Type t : Type.values())
93 if (t.toString().equalsIgnoreCase(value))
102 * case insensitive equivalence for strings resolving to PDBEntry type
107 public boolean matches(String t)
109 return (this.toString().equalsIgnoreCase(t));
114 * Answers true if obj is a PDBEntry with the same id and chain code (both
115 * ignoring case), file, type and properties
118 public boolean equals(Object obj)
120 if (obj == null || !(obj instanceof PDBEntry))
128 PDBEntry o = (PDBEntry) obj;
131 * note that chain code is stored as a property wrapped by a
132 * CaseInsensitiveString, so we are in effect doing a
133 * case-insensitive comparison of chain codes
135 boolean idMatches = id == o.id
136 || (id != null && id.equalsIgnoreCase(o.id));
137 boolean fileMatches = file == o.file
138 || (file != null && file.equals(o.file));
139 boolean typeMatches = type == o.type
140 || (type != null && type.equals(o.type));
141 if (idMatches && fileMatches && typeMatches)
143 return properties == o.properties
144 || (properties != null && properties.equals(o.properties));
150 * Default constructor
157 * Entry point when file is not known and fileType may be string
159 * @param chain may be null
160 * @param fileType "pdb", "mmcif", or "bcif"; null defaults to mmcif
162 public PDBEntry(String pdbId, String chain, String fileType) {
163 this.id = pdbId.toLowerCase();
164 setChainCode(chain); // I note that PDB Chains ARE case-sensitive now
165 if (fileType == null)
167 switch (fileType.toLowerCase()) {
169 this.type = Type.PDB.toString();
172 this.type = Type.MMCIF.toString();
176 System.out.println("format " + fileType + " has not been implemented; using mmCIF");
177 this.type = Type.MMCIF.toString();
182 public PDBEntry(String pdbId, String chain, PDBEntry.Type type,
185 init(pdbId, chain, type, filePath);
194 void init(String pdbId, String chain, PDBEntry.Type entryType,
198 this.type = entryType == null ? null : entryType.toString();
199 this.file = filePath;
208 public PDBEntry(PDBEntry entry)
213 if (entry.properties != null)
215 properties = (Hashtable<String, Object>) entry.properties.clone();
220 * Make a PDBEntry from a DBRefEntry. The accession code is used for the PDB
221 * id, but if it is 5 characters in length, the last character is removed and
222 * set as the chain code instead.
226 public PDBEntry(DBRefEntry dbr)
228 if (!DBRefSource.PDB.equals(dbr.getSource()))
230 throw new IllegalArgumentException(
231 "Invalid source: " + dbr.getSource());
234 String pdbId = dbr.getAccessionId();
235 String chainCode = null;
236 if (pdbId.length() == PDB_ID_LENGTH + 1)
238 char chain = pdbId.charAt(PDB_ID_LENGTH);
239 if (('a' <= chain && chain <= 'z') || ('A' <= chain && chain <= 'Z'))
241 pdbId = pdbId.substring(0, PDB_ID_LENGTH);
242 chainCode = String.valueOf(chain);
245 init(pdbId, chainCode, null, null);
248 public void setFile(String f)
253 public String getFile()
258 public void setType(String t)
263 public void setType(PDBEntry.Type type)
265 this.type = type == null ? null : type.toString();
268 public String getType()
273 public void setId(String id)
278 public String getId()
286 * @param key "protocol"
289 public void setProperty(String key, Object value)
291 if (this.properties == null)
293 this.properties = new Hashtable<String, Object>();
295 properties.put(key, value);
298 public Object getProperty(String key)
300 return properties == null ? null : properties.get(key);
304 * Returns an enumeration of the keys of this object's properties (or an empty
305 * enumeration if it has no properties)
309 public Enumeration<String> getProperties()
311 if (properties == null)
313 return Collections.emptyEnumeration();
315 return properties.keys();
320 * @return null or a string for associated chain IDs
322 public String getChainCode()
324 return (properties == null || properties.get(CHAIN_ID) == null) ? null
325 : properties.get(CHAIN_ID).toString();
329 * Sets a non-case-sensitive property for the given chain code. Two PDBEntry
330 * objects which differ only in the case of their chain code are considered
331 * equal. This avoids duplication of objects in lists of PDB ids.
335 public void setChainCode(String chainCode)
337 if (chainCode == null)
339 deleteProperty(CHAIN_ID);
343 setProperty(CHAIN_ID, new CaseInsensitiveString(chainCode));
348 * Deletes the property with the given key, and returns the deleted value (or
351 Object deleteProperty(String key)
353 Object result = null;
354 if (properties != null)
356 result = properties.remove(key);
362 public String toString()
368 * Getter provided for Castor binding only. Application code should call
369 * getProperty() or getProperties() instead.
372 * @see #getProperty(String)
373 * @see #getProperties()
374 * @see jalview.ws.dbsources.Uniprot#getUniprotEntries
378 public Hashtable<String, Object> getProps()
384 * Setter provided for Castor binding only. Application code should call
385 * setProperty() instead.
391 public void setProps(Hashtable<String, Object> props)
397 * Answers true if this object is either equivalent to, or can be 'improved'
398 * by, the given entry.
400 * If newEntry has the same id (ignoring case), and doesn't have a conflicting
401 * file spec or chain code, then update this entry from its file and/or chain
405 * @return true if modifications were made
407 public boolean updateFrom(PDBEntry newEntry)
409 if (this.equals(newEntry))
414 String newId = newEntry.getId();
415 if (newId == null || getId() == null)
417 return false; // shouldn't happen
421 * id has to match (ignoring case)
423 if (!getId().equalsIgnoreCase(newId))
429 * Don't update if associated with different structure files
431 String newFile = newEntry.getFile();
432 if (newFile != null && getFile() != null && !newFile.equals(getFile()))
438 * Don't update if associated with different chains (ignoring case)
440 String newChain = newEntry.getChainCode();
441 if (newChain != null && newChain.length() > 0 && getChainCode() != null
442 && getChainCode().length() > 0
443 && !getChainCode().equalsIgnoreCase(newChain))
449 * set file path if not already set
451 String newType = newEntry.getType();
452 if (getFile() == null && newFile != null)
459 * set file type if new entry has it and we don't
460 * (for the case where file was not updated)
462 if (getType() == null && newType != null)
468 * set chain if not already set (we excluded differing
469 * chains earlier) (ignoring case change only)
471 if (newChain != null && newChain.length() > 0
472 && !newChain.equalsIgnoreCase(getChainCode()))
474 setChainCode(newChain);
478 * copy any new or modified properties
480 Enumeration<String> newProps = newEntry.getProperties();
481 while (newProps.hasMoreElements())
484 * copy properties unless value matches; this defends against changing
485 * the case of chain_code which is wrapped in a CaseInsensitiveString
487 String key = newProps.nextElement();
488 Object value = newEntry.getProperty(key);
489 if (!value.equals(getProperty(key)))
491 setProperty(key, value);