JAL-1803 extracted class ChainId to jalview.util.CaseInsensitiveString,
[jalview.git] / src / jalview / datamodel / PDBEntry.java
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 jalview.util.CaseInsensitiveString;
24
25 import java.util.Hashtable;
26
27 public class PDBEntry
28 {
29   private String file;
30
31   private String type;
32
33   private String id;
34
35   public enum Type
36   {
37     PDB, MMCIF, FILE;
38     /**
39      * case insensitive matching for Type enum
40      * 
41      * @param value
42      * @return
43      */
44     public static Type getType(String value)
45     {
46       for (Type t : Type.values())
47       {
48         if (t.toString().equalsIgnoreCase(value))
49         {
50           return t;
51         }
52       }
53       return null;
54     }
55
56     /**
57      * case insensitive equivalence for strings resolving to PDBEntry type
58      * 
59      * @param t
60      * @return
61      */
62     public boolean matches(String t)
63     {
64       return (this.toString().equalsIgnoreCase(t));
65     }
66   }
67
68   /**
69    * constant for storing chain code in properties table
70    */
71   private static final String CHAIN_ID = "chain_code";
72
73   Hashtable properties;
74
75   /*
76    * (non-Javadoc)
77    * 
78    * @see java.lang.Object#equals(java.lang.Object)
79    */
80   @Override
81   public boolean equals(Object obj)
82   {
83     if (obj == null || !(obj instanceof PDBEntry))
84     {
85       return false;
86     }
87     if (obj == this)
88     {
89       return true;
90     }
91     PDBEntry o = (PDBEntry) obj;
92     return (type == o.type || (type != null && o.type != null && o.type
93             .equals(type)))
94             && (id == o.id || (id != null && o.id != null && o.id
95                     .equalsIgnoreCase(id)))
96             && (properties == o.properties || (properties != null
97                     && o.properties != null && properties
98                       .equals(o.properties)));
99
100   }
101
102   /**
103    * Default constructor
104    */
105   public PDBEntry()
106   {
107   }
108
109   /**
110    * Constructor given file path and PDB id.
111    * 
112    * @param filePath
113    */
114   // public PDBEntry(String filePath, String pdbId)
115   // {
116   // this.file = filePath;
117   // this.id = pdbId;
118   // }
119
120   public PDBEntry(String pdbId, String chain, PDBEntry.Type type,
121           String filePath)
122   {
123     this.id = pdbId;
124     this.type = type == null ? null : type.toString();
125     this.file = filePath;
126     setChainCode(chain);
127   }
128
129   /**
130    * Copy constructor.
131    * 
132    * @param entry
133    */
134   public PDBEntry(PDBEntry entry)
135   {
136     file = entry.file;
137     type = entry.type;
138     id = entry.id;
139     if (entry.properties != null)
140     {
141       properties = (Hashtable) entry.properties.clone();
142     }
143   }
144
145   public void setFile(String file)
146   {
147     this.file = file;
148   }
149
150   public String getFile()
151   {
152     return file;
153   }
154
155   public void setType(String t)
156   {
157     this.type = t;
158   }
159
160   public void setType(PDBEntry.Type type)
161   {
162     this.type = type == null ? null : type.toString();
163   }
164
165   public String getType()
166   {
167     return type;
168   }
169
170   public void setId(String id)
171   {
172     this.id = id;
173   }
174
175   public String getId()
176   {
177     return id;
178   }
179
180   public void setProperty(Hashtable property)
181   {
182     this.properties = property;
183   }
184
185   public Hashtable getProperty()
186   {
187     return properties;
188   }
189
190   /**
191    * 
192    * @return null or a string for associated chain IDs
193    */
194   public String getChainCode()
195   {
196     return (properties == null || properties.get(CHAIN_ID) == null) ? null
197             : properties.get(CHAIN_ID).toString();
198   }
199
200   public void setChainCode(String chainCode)
201   {
202     if (properties == null)
203     {
204       if (chainCode == null)
205       {
206         // nothing to do.
207         return;
208       }
209       properties = new Hashtable();
210     }
211     if (chainCode == null)
212     {
213       properties.remove(CHAIN_ID);
214       return;
215     }
216     // update property for non-null chainCode
217     properties.put(CHAIN_ID, new CaseInsensitiveString(chainCode));
218   }
219
220   @Override
221   public String toString()
222   {
223     return id;
224   }
225
226   /**
227    * update entry with details from another entry concerning the same PDB
228    * ID/file spec.
229    * 
230    * @param newEntry
231    * @return true if modifications were made
232    */
233   public boolean updateFrom(PDBEntry newEntry)
234   {
235     boolean modified = false;
236
237     if (getFile() == null)
238     {
239       // update file and type of file
240       modified |= newEntry.getFile() != null;
241       setFile(newEntry.getFile());
242     }
243     if (newEntry.getType() != null && newEntry.getFile() != null
244             && newEntry.getFile().equals(getFile()))
245     {
246       setType(newEntry.getType());
247     }
248     if (getChainCode() == null
249             || (getChainCode() != null && getChainCode().length() == 0 && newEntry
250                     .getChainCode() != null))
251     {
252       modified |= getChainCode() == null
253               || !newEntry.getChainCode().equals(getChainCode());
254       setChainCode(newEntry.getChainCode());
255     }
256     if (newEntry.getProperty() != null)
257     {
258       if (properties == null)
259       {
260         properties = new Hashtable();
261       }
262       // TODO: getProperty -> Map<String,String>
263       for (Object p : newEntry.getProperty().keySet())
264       {
265         if (properties.get(p) == null
266                 || !properties.get(p).equals(newEntry.getProperty().get(p)))
267         {
268           modified = true;
269         }
270         properties.put(p, newEntry.getProperty().get(p));
271       }
272     }
273     return modified;
274   }
275 }