Clover icon

Coverage Report

  1. Project Clover database Fri Nov 15 2024 13:56:46 GMT
  2. Package jalview.datamodel

File PDBEntry.java

 

Coverage histogram

../../img/srcFileCovDistChart8.png
20% of files have more coverage

Code metrics

74
153
67
2
786
484
124
0.81
2.28
33.5
1.85

Classes

Class Line # Actions
PDBEntry 32 144 118
0.741007274.1%
PDBEntry.Type 64 9 6
0.7575%
 

Contributing tests

This file is covered by 81 tests. .

Source view

1    /*
2    * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3    * Copyright (C) $$Year-Rel$$ The Jalview Authors
4    *
5    * This file is part of Jalview.
6    *
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.
11    *
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.
16    *
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.
20    */
21    package jalview.datamodel;
22   
23    import java.util.Collections;
24    import java.util.Enumeration;
25    import java.util.Hashtable;
26   
27    import jalview.io.DataSourceType;
28    import jalview.io.StructureFile;
29    import jalview.structure.StructureImportSettings.TFType;
30    import jalview.util.CaseInsensitiveString;
31   
 
32    public class PDBEntry
33    {
34   
35    /**
36    * constant for storing chain code in properties table
37    */
38    private static final String CHAIN_ID = "chain_code";
39   
40    private Hashtable<String, Object> properties;
41   
42    private static final int PDB_ID_LENGTH = 4;
43   
44    /**
45    * property set when id is a 'manufactured' identifier from the structure
46    * data's filename
47    */
48    private static final String FAKED_ID = "faked_pdbid";
49   
50    /**
51    * property set when the id is authoritative, and should be used in preference
52    * to any identifiers in the structure data
53    */
54    private static final String AUTHORITATIVE_ID = "authoritative_pdbid";
55   
56    private String file;
57   
58    private String type;
59   
60    private String id;
61   
62    private StructureFile sf = null;
63   
 
64    public enum Type
65    {
66    // TODO is FILE needed; if not is this enum needed, or can we
67    // use FileFormatI for PDB, MMCIF?
68    PDB("pdb", "pdb"), MMCIF("mmcif", "cif"), BCIF("bcif", "bcif"),
69    FILE("?", "?");
70   
71    /*
72    * file extension for cached structure file; must be one that
73    * is recognised by Chimera 'open' command
74    * @see https://www.cgl.ucsf.edu/chimera/current/docs/UsersGuide/filetypes.html
75    */
76    String ext;
77   
78    /*
79    * format specifier used in dbfetch request
80    * @see http://www.ebi.ac.uk/Tools/dbfetch/dbfetch/dbfetch.databases#pdb
81    */
82    String format;
83   
 
84  220 toggle private Type(String fmt, String ex)
85    {
86  220 format = fmt;
87  220 ext = ex;
88    }
89   
 
90  0 toggle public String getFormat()
91    {
92  0 return format;
93    }
94   
 
95  0 toggle public String getExtension()
96    {
97  0 return ext;
98    }
99   
100    /**
101    * case insensitive matching for Type enum
102    *
103    * @param value
104    * @return
105    */
 
106  96 toggle public static Type getType(String value)
107    {
108  96 for (Type t : Type.values())
109    {
110  108 if (t.toString().equalsIgnoreCase(value))
111    {
112  95 return t;
113    }
114    }
115  1 return null;
116    }
117   
118    /**
119    * case insensitive equivalence for strings resolving to PDBEntry type
120    *
121    * @param t
122    * @return
123    */
 
124  4 toggle public boolean matches(String t)
125    {
126  4 return (this.toString().equalsIgnoreCase(t));
127    }
128    }
129   
130    /**
131    * Answers true if obj is a PDBEntry with the same id and chain code (both
132    * ignoring case), file, type and properties
133    */
 
134  17542 toggle @Override
135    public boolean equals(Object obj)
136    {
137  17542 if (obj == null || !(obj instanceof PDBEntry))
138    {
139  2 return false;
140    }
141  17540 if (obj == this)
142    {
143  11 return true;
144    }
145  17529 PDBEntry o = (PDBEntry) obj;
146   
147    /*
148    * note that chain code is stored as a property wrapped by a
149    * CaseInsensitiveString, so we are in effect doing a
150    * case-insensitive comparison of chain codes
151    */
152  17529 boolean idMatches = id == o.id
153    || (id != null && id.equalsIgnoreCase(o.id));
154  17529 boolean fileMatches = file == o.file
155    || (file != null && file.equals(o.file));
156  17529 boolean typeMatches = type == o.type
157    || (type != null && type.equals(o.type));
158  17529 if (idMatches && fileMatches && typeMatches)
159    {
160  379 return properties == o.properties
161    || (properties != null && properties.equals(o.properties));
162    }
163  17150 return false;
164    }
165   
166    /**
167    * Default constructor
168    */
 
169  738 toggle public PDBEntry()
170    {
171    }
172   
 
173  91 toggle public PDBEntry(String pdbId, String chain, PDBEntry.Type type,
174    String filePath)
175    {
176  91 init(pdbId, chain, type, filePath);
177    }
178   
179    /**
180    * @param pdbId
181    * @param chain
182    * @param entryType
183    * @param filePath
184    */
 
185  303 toggle void init(String pdbId, String chain, PDBEntry.Type entryType,
186    String filePath)
187    {
188  303 this.id = pdbId;
189  303 this.type = entryType == null ? null : entryType.toString();
190  303 this.file = filePath;
191  303 setChainCode(chain);
192    }
193   
194    /**
195    * Copy constructor.
196    *
197    * @param entry
198    */
 
199  146 toggle public PDBEntry(PDBEntry entry)
200    {
201  146 file = entry.file;
202  146 type = entry.type;
203  146 id = entry.id;
204  146 if (entry.properties != null)
205    {
206  6 properties = (Hashtable<String, Object>) entry.properties.clone();
207    }
208    }
209   
210    /**
211    * Make a PDBEntry from a DBRefEntry. The accession code is used for the PDB
212    * id, but if it is 5 characters in length, the last character is removed and
213    * set as the chain code instead.
214    *
215    * @param dbr
216    */
 
217  213 toggle public PDBEntry(DBRefEntry dbr)
218    {
219  213 if (!DBRefSource.PDB.equals(dbr.getSource()))
220    {
221  1 throw new IllegalArgumentException(
222    "Invalid source: " + dbr.getSource());
223    }
224   
225  212 String pdbId = dbr.getAccessionId();
226  212 String chainCode = null;
227  212 if (pdbId.length() == PDB_ID_LENGTH + 1)
228    {
229  17 char chain = pdbId.charAt(PDB_ID_LENGTH);
230  17 if (('a' <= chain && chain <= 'z') || ('A' <= chain && chain <= 'Z'))
231    {
232  15 pdbId = pdbId.substring(0, PDB_ID_LENGTH);
233  15 chainCode = String.valueOf(chain);
234    }
235    }
236  212 init(pdbId, chainCode, null, null);
237    }
238   
 
239  475 toggle public void setFile(String f)
240    {
241  475 this.file = f;
242    }
243   
 
244  19030 toggle public String getFile()
245    {
246  19030 return file;
247    }
248   
 
249  313 toggle public void setType(String t)
250    {
251  313 this.type = t;
252    }
253   
 
254  120 toggle public void setType(PDBEntry.Type type)
255    {
256  120 this.type = type == null ? null : type.toString();
257    }
258   
 
259  361 toggle public String getType()
260    {
261  361 return type;
262    }
263   
 
264  738 toggle public void setId(String id)
265    {
266  738 this.id = id;
267    }
268   
 
269  52038 toggle public String getId()
270    {
271  52038 return id;
272    }
273   
 
274  638 toggle public void setProperty(String key, Object value)
275    {
276  638 if (this.properties == null)
277    {
278  497 this.properties = new Hashtable<String, Object>();
279    }
280  638 if (key!=null && value==null)
281    {
282  0 properties.remove(key);
283  0 return;
284    }
285    // null key throws a runtime exception
286  638 properties.put(key, value);
287    }
288   
 
289  397 toggle public Object getProperty(String key)
290    {
291  397 return properties == null ? null : properties.get(key);
292    }
293   
294    /**
295    * Returns an enumeration of the keys of this object's properties (or an empty
296    * enumeration if it has no properties)
297    *
298    * @return
299    */
 
300  203 toggle public Enumeration<String> getProperties()
301    {
302  203 if (properties == null)
303    {
304  114 return Collections.emptyEnumeration();
305    }
306  89 return properties.keys();
307    }
308   
309    /**
310    *
311    * @return null or a string for associated chain IDs
312    */
 
313  386 toggle public String getChainCode()
314    {
315  386 return (properties == null || properties.get(CHAIN_ID) == null) ? null
316    : properties.get(CHAIN_ID).toString();
317    }
318   
319    /**
320    * Sets a non-case-sensitive property for the given chain code. Two PDBEntry
321    * objects which differ only in the case of their chain code are considered
322    * equal. This avoids duplication of objects in lists of PDB ids.
323    *
324    * @param chainCode
325    */
 
326  653 toggle public void setChainCode(String chainCode)
327    {
328  653 if (chainCode == null)
329    {
330  227 deleteProperty(CHAIN_ID);
331    }
332    else
333    {
334  426 setProperty(CHAIN_ID, new CaseInsensitiveString(chainCode));
335    }
336    }
337   
338    /**
339    * Deletes the property with the given key, and returns the deleted value (or
340    * null)
341    */
 
342  227 toggle Object deleteProperty(String key)
343    {
344  227 Object result = null;
345  227 if (properties != null)
346    {
347  1 result = properties.remove(key);
348    }
349  227 return result;
350    }
351   
 
352  26 toggle @Override
353    public String toString()
354    {
355  26 return id;
356    }
357   
358    /**
359    * Getter provided for Castor binding only. Application code should call
360    * getProperty() or getProperties() instead.
361    *
362    * @deprecated
363    * @see #getProperty(String)
364    * @see #getProperties()
365    * @see jalview.ws.dbsources.Uniprot#getUniprotEntries
366    * @return
367    */
 
368  0 toggle @Deprecated
369    public Hashtable<String, Object> getProps()
370    {
371  0 return properties;
372    }
373   
374    /**
375    * Setter provided for Castor binding only. Application code should call
376    * setProperty() instead.
377    *
378    * @deprecated
379    * @return
380    */
 
381  0 toggle @Deprecated
382    public void setProps(Hashtable<String, Object> props)
383    {
384  0 properties = props;
385    }
386   
387    /**
388    * Answers true if this object is either equivalent to, or can be 'improved'
389    * by, the given entry.
390    * <p>
391    * If newEntry has the same id (ignoring case), and doesn't have a conflicting
392    * file spec or chain code, then update this entry from its file and/or chain
393    * code.
394    *
395    * @param newEntry
396    * @return true if modifications were made
397    */
 
398  17500 toggle public boolean updateFrom(PDBEntry newEntry)
399    {
400  17500 if (this.equals(newEntry))
401    {
402  329 return true;
403    }
404   
405  17171 String newId = newEntry.getId();
406  17171 if (newId == null || getId() == null)
407    {
408  0 return false; // shouldn't happen
409    }
410   
411  17171 boolean idMatches = getId().equalsIgnoreCase(newId);
412   
413    /*
414    * Don't update if associated with different structure files
415    */
416  17171 String newFile = newEntry.getFile();
417  17171 if (newFile != null && getFile() != null)
418    {
419  179 if (!newFile.equals(getFile()))
420    {
421  103 return false;
422    }
423    else
424    {
425    // files match.
426  76 if (!idMatches)
427    {
428    // this shouldn't happen, but could do if the id from the
429    // file is not the same as the id from the authority that provided
430    // the file
431  2 if (!newEntry.fakedPDBId() && !isAuthoritative())
432    {
433  0 return false;
434    } // otherwise we can update
435    }
436    }
437    }
438    else
439    {
440    // one has data, one doesn't ..
441  16992 if (!idMatches)
442    {
443  16919 return false;
444    } // otherwise maybe can update
445    }
446   
447    /*
448    * Don't update if associated with different chains (ignoring case)
449    */
450  149 String newChain = newEntry.getChainCode();
451  149 if (newChain != null && newChain.length() > 0 && getChainCode() != null
452    && getChainCode().length() > 0
453    && !getChainCode().equalsIgnoreCase(newChain))
454    {
455  8 return false;
456    }
457   
458    /*
459    * set file path if not already set
460    */
461  141 String newType = newEntry.getType();
462  141 if (getFile() == null && newFile != null)
463    {
464  3 setFile(newFile);
465  3 setType(newType);
466    }
467   
468    /*
469    * set file type if new entry has it and we don't
470    * (for the case where file was not updated)
471    */
472  141 if (getType() == null && newType != null)
473    {
474  7 setType(newType);
475    }
476   
477    /*
478    * set chain if not already set (we excluded differing
479    * chains earlier) (ignoring case change only)
480    */
481  141 if (newChain != null && newChain.length() > 0
482    && !newChain.equalsIgnoreCase(getChainCode()))
483    {
484  18 setChainCode(newChain);
485    }
486   
487    /*
488    * copy any new or modified properties
489    */
490  141 Enumeration<String> newProps = newEntry.getProperties();
491  186 while (newProps.hasMoreElements())
492    {
493    /*
494    * copy properties unless value matches; this defends against changing
495    * the case of chain_code which is wrapped in a CaseInsensitiveString
496    */
497  45 String key = newProps.nextElement();
498  45 Object value = newEntry.getProperty(key);
499  45 if (FAKED_ID.equals(key) || AUTHORITATIVE_ID.equals(key))
500    {
501    // we never update the fake ID property
502  1 continue;
503    }
504  44 if (!value.equals(getProperty(key)))
505    {
506  11 setProperty(key, value);
507    }
508    }
509  141 return true;
510    }
511   
 
512  1 toggle public void setAuthoritative(boolean isAuthoritative)
513    {
514  1 setProperty(AUTHORITATIVE_ID, Boolean.valueOf(isAuthoritative));
515    }
516   
517    /**
518    *
519    * @return true if the identifier should be preferred over any identifiers
520    * embedded in the structure data
521    */
 
522  3 toggle public boolean isAuthoritative()
523    {
524  3 if (_hasProperty(AUTHORITATIVE_ID))
525    {
526  2 return ((Boolean) getProperty(AUTHORITATIVE_ID));
527    }
528  1 return false;
529    }
530   
531    /**
532    * set when Jalview has manufactured the ID using a local filename
533    *
534    * @return
535    */
 
536  4 toggle public boolean fakedPDBId()
537    {
538  4 if (_hasProperty(FAKED_ID))
539    {
540  2 return true;
541    }
542  2 return false;
543    }
544   
 
545  304 toggle public void setFakedPDBId(boolean faked)
546    {
547  304 if (faked)
548    {
549  96 setProperty(FAKED_ID, Boolean.TRUE);
550    }
551    else
552    {
553  208 if (properties != null)
554    {
555  0 properties.remove(FAKED_ID);
556    }
557    }
558    }
559   
 
560  98 toggle private boolean _hasProperty(final String key)
561    {
562  98 return (properties != null && properties.containsKey(key));
563    }
564   
565    private static final String RETRIEVE_FROM = "RETRIEVE_FROM";
566   
567    private static final String PROVIDER = "PROVIDER";
568   
569    private static final String MODELPAGE = "PROVIDERPAGE";
570   
571    private static final String PROVIDERCATEGORY = "PROVIDERCATEGORY";
572   
573   
574    private static final String MODELCONFIDENCE = "MODELCONFIDENCE";
575   
576    private static final String MODELCONFTYPE = "MODELCONFTYPE";
577   
578    private static final String MODELCONFVER = "MODELCONFVER";
579   
580    // Pre 2.11.4 properties - case preserved
581    private static final String TEMPFACTYPE = "TFType";
582    private static final String PAEFILE = "PAEFile";
583    private final static String PROTOCOL="protocol";
584   
585    /**
586    * Permanent URI for retrieving the original structure data
587    *
588    * @param urlStr
589    */
 
590  0 toggle public void setRetrievalUrl(String urlStr)
591    {
592  0 setProperty(RETRIEVE_FROM, urlStr);
593    }
594   
 
595  0 toggle public boolean hasRetrievalUrl()
596    {
597  0 return _hasProperty(RETRIEVE_FROM);
598    }
599   
600    /**
601    * get the Permanent URI for retrieving the original structure data
602    */
 
603  0 toggle public String getRetrievalUrl()
604    {
605  0 return (String) getProperty(RETRIEVE_FROM);
606    }
607   
608    /**
609    * Data provider name - from 3D Beacons
610    *
611    * @param provider
612    */
 
613  0 toggle public void setProvider(String provider)
614    {
615  0 setProperty(PROVIDER, provider);
616    }
617   
618    /**
619    * Get Data provider name - from 3D Beacons
620    *
621    */
 
622  0 toggle public String getProvider()
623    {
624  0 return (String) getProperty(PROVIDER);
625    }
626   
627    /**
628    * Permanent URI for retrieving the original structure data
629    *
630    * @param urlStr
631    */
 
632  0 toggle public void setProviderPage(String urlStr)
633    {
634  0 setProperty(MODELPAGE, urlStr);
635    }
636   
637    /**
638    * get the Permanent URI for retrieving the original structure data
639    */
 
640  0 toggle public String getProviderPage()
641    {
642  0 return (String) getProperty(MODELPAGE);
643    }
644   
 
645  0 toggle public boolean hasProviderPage()
646    {
647  0 return _hasProperty(MODELPAGE);
648    }
649   
 
650  0 toggle public boolean hasProvider()
651    {
652  0 return _hasProperty(PROVIDER);
653    }
654   
 
655  0 toggle public StructureFile getStructureFile()
656    {
657  0 return sf;
658    }
659   
 
660  55 toggle public void setStructureFile(StructureFile f)
661    {
662  55 sf = f;
663    }
664   
 
665  0 toggle public boolean hasStructureFile()
666    {
667  0 return sf != null && sf.inFile != null && sf.inFile.exists();
668    }
669   
 
670  0 toggle public void setProviderCategory(String providerCategory)
671    {
672  0 setProperty(PROVIDERCATEGORY, providerCategory);
673    }
674   
 
675  0 toggle public String getProviderCategory()
676    {
677  0 return (String) getProperty(PROVIDERCATEGORY);
678    }
679   
 
680  0 toggle public boolean hasProviderCategory()
681    {
682  0 return _hasProperty(PROVIDERCATEGORY);
683    }
684   
 
685  37 toggle public void setTempFacType(TFType tempfactype)
686    {
687  37 setProperty(TEMPFACTYPE, tempfactype.name());
688    }
689   
690    /**
691    * String version of TFType enum
692    * @return
693    */
 
694  0 toggle public String getTempFacType()
695    {
696  0 return (String) getProperty(TEMPFACTYPE);
697    }
 
698  46 toggle public TFType getTempFacTypeTFType()
699    {
700  46 if (_hasProperty(TEMPFACTYPE)) {
701  15 return TFType.valueOf((String) getProperty(TEMPFACTYPE));
702    }
703  31 return null;
704    }
705   
 
706  0 toggle public boolean hasTempFacType()
707    {
708  0 return _hasProperty(TEMPFACTYPE);
709    }
710   
 
711  0 toggle public void setModelConfidenceType(String modelConfType2)
712    {
713  0 setProperty(MODELCONFTYPE, modelConfType2);
714    }
715   
 
716  0 toggle public boolean hasModelConfidenceType()
717    {
718  0 return _hasProperty(MODELCONFTYPE);
719    }
720   
 
721  0 toggle public String getModelConfidenceType()
722    {
723  0 return (String) getProperty(MODELCONFTYPE);
724    }
725   
 
726  0 toggle public void setModelConfidenceVersion(String modelConfVer2)
727    {
728  0 setProperty(MODELCONFVER, modelConfVer2);
729    }
730   
 
731  0 toggle public boolean hasModelConfidenceVersion()
732    {
733  0 return _hasProperty(MODELCONFVER);
734    }
735   
 
736  0 toggle public String getModelConfidenceVersion()
737    {
738  0 return (String) getProperty(MODELCONFVER);
739    }
740   
 
741  0 toggle public void setModelConfidence(Double modelConf)
742    {
743  0 setProperty(MODELCONFIDENCE, modelConf);
744    }
745   
 
746  0 toggle public boolean hasModelConfidence()
747    {
748  0 return _hasProperty(MODELCONFIDENCE);
749    }
750   
 
751  0 toggle public Double getModelConfidence()
752    {
753  0 return (Double) getProperty(MODELCONFIDENCE);
754    }
755   
 
756  36 toggle public void setPAEFile(String paeFilename)
757    {
758  36 setProperty(PAEFILE,paeFilename);
759    }
 
760  45 toggle public String getPAEFile()
761    {
762  45 return (String) getProperty(PAEFILE);
763    }
 
764  0 toggle public boolean hasPAEFile()
765    {
766  0 return _hasProperty(PAEFILE);
767    }
768   
 
769  0 toggle public void setProtocol(DataSourceType protocol)
770    {
771  0 setProperty(PROTOCOL, protocol.name());
772    }
 
773  0 toggle public boolean hasProtocol()
774    {
775  0 return _hasProperty(PROTOCOL);
776    }
 
777  45 toggle public DataSourceType getProtocol()
778    {
779  45 if (_hasProperty(PROTOCOL))
780    {
781  0 return DataSourceType.valueOf((String) getProperty(PROTOCOL));
782    }
783    // default protocol is
784  45 return DataSourceType.FILE;
785    }
786    }