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