copy feature: check for null values
[jalview.git] / src / jalview / datamodel / SequenceFeature.java
1 /*\r
2  * Jalview - A Sequence Alignment Editor and Viewer\r
3  * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
4  *\r
5  * This program is free software; you can redistribute it and/or\r
6  * modify it under the terms of the GNU General Public License\r
7  * as published by the Free Software Foundation; either version 2\r
8  * of the License, or (at your option) any later version.\r
9  *\r
10  * This program is distributed in the hope that it will be useful,\r
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13  * GNU General Public License for more details.\r
14  *\r
15  * You should have received a copy of the GNU General Public License\r
16  * along with this program; if not, write to the Free Software\r
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA\r
18  */\r
19 package jalview.datamodel;\r
20 \r
21 import java.util.Enumeration;\r
22 import java.util.Hashtable;\r
23 import java.util.Vector;\r
24 \r
25 /**\r
26  * DOCUMENT ME!\r
27  *\r
28  * @author $author$\r
29  * @version $Revision$\r
30  */\r
31 public class SequenceFeature\r
32 {\r
33     public int begin;\r
34     public int end;\r
35     public float score;\r
36     public String type;\r
37     public String description;\r
38     public Hashtable otherDetails;\r
39     public java.util.Vector links;\r
40 \r
41     // Feature group can be set from a features file\r
42     // as a group of features between STARTGROUP and ENDGROUP markers\r
43     public String featureGroup;\r
44 \r
45     public SequenceFeature()\r
46     {}\r
47     /**\r
48      * Constructs a duplicate feature.\r
49      * Note: Uses clone on the otherDetails so only shallow copies are made\r
50      * of additional properties and method will silently fail if unclonable\r
51      * objects are found in the hash.\r
52      * @param cpy\r
53      */\r
54     public SequenceFeature(SequenceFeature cpy) {\r
55         if (cpy!=null) {\r
56             begin = cpy.begin;\r
57             end = cpy.end;\r
58             score = cpy.score;\r
59             if (cpy.type != null)\r
60               type = new String(cpy.type);\r
61             if (cpy.description != null)\r
62               description = new String(cpy.description);\r
63             if (cpy.featureGroup != null)\r
64               featureGroup = new String(cpy.featureGroup);\r
65             if (cpy.otherDetails!=null) {\r
66                 try {\r
67                     otherDetails = (Hashtable) cpy.otherDetails.clone();\r
68                 } catch (Exception e) {\r
69                     // Uncloneable objects in the otherDetails - don't complain\r
70                 }\r
71             }\r
72             if (cpy.links!=null && cpy.links.size()>0) {\r
73                 links=new Vector();\r
74                 for (int i=0,iSize=cpy.links.size(); i<iSize; i++) {\r
75                     links.setElementAt(cpy.links.elementAt(i), i);\r
76                 }\r
77             }\r
78         }\r
79     }\r
80     public SequenceFeature(String type,\r
81                            String desc,\r
82                            String status,\r
83                            int begin, int end,\r
84                            String featureGroup)\r
85     {\r
86       this.type = type;\r
87       this.description = desc;\r
88       setValue("status", status);\r
89       this.begin = begin;\r
90       this.end = end;\r
91       this.featureGroup = featureGroup;\r
92     }\r
93 \r
94     public SequenceFeature(String type,\r
95                            String desc,\r
96                            int begin, int end,\r
97                            float score,\r
98                            String featureGroup)\r
99     {\r
100       this.type = type;\r
101       this.description = desc;\r
102       this.begin = begin;\r
103       this.end = end;\r
104       this.score = score;\r
105       this.featureGroup = featureGroup;\r
106     }\r
107 \r
108     public boolean equals(SequenceFeature sf)\r
109     {\r
110       if (begin != sf.begin\r
111           || end != sf.end\r
112           || score != sf.score)\r
113         return false;\r
114 \r
115       if(!(type+description+featureGroup).equals\r
116          (sf.type+sf.description+sf.featureGroup))\r
117         return false;\r
118 \r
119       return true;\r
120     }\r
121 \r
122 \r
123     /**\r
124      * DOCUMENT ME!\r
125      *\r
126      * @return DOCUMENT ME!\r
127      */\r
128     public int getBegin()\r
129     {\r
130         return begin;\r
131     }\r
132 \r
133     public void setBegin(int start)\r
134     {\r
135       this.begin = start;\r
136     }\r
137 \r
138     /**\r
139      * DOCUMENT ME!\r
140      *\r
141      * @return DOCUMENT ME!\r
142      */\r
143     public int getEnd()\r
144     {\r
145         return end;\r
146     }\r
147 \r
148     public void setEnd(int end)\r
149     {\r
150       this.end = end;\r
151     }\r
152 \r
153     /**\r
154      * DOCUMENT ME!\r
155      *\r
156      * @return DOCUMENT ME!\r
157      */\r
158     public String getType()\r
159     {\r
160         return type;\r
161     }\r
162 \r
163     public void setType(String type)\r
164     {\r
165       this.type = type;\r
166     }\r
167 \r
168     /**\r
169      * DOCUMENT ME!\r
170      *\r
171      * @return DOCUMENT ME!\r
172      */\r
173     public String getDescription()\r
174     {\r
175         return description;\r
176     }\r
177 \r
178     public void setDescription(String desc)\r
179     {\r
180       description = desc;\r
181     }\r
182 \r
183     public String getFeatureGroup()\r
184     {\r
185       return featureGroup;\r
186     }\r
187 \r
188     public void setFeatureGroup(String featureGroup)\r
189     {\r
190       this.featureGroup = featureGroup;\r
191     }\r
192 \r
193     public void addLink(String labelLink)\r
194     {\r
195       if(links==null)\r
196         links = new java.util.Vector();\r
197 \r
198       links.insertElementAt(labelLink,0);\r
199     }\r
200 \r
201     public float getScore()\r
202     {\r
203       return score;\r
204     }\r
205 \r
206     public void setScore(float value)\r
207     {\r
208       score = value;\r
209     }\r
210 \r
211     /**\r
212      * Used for getting values which are not in the\r
213      * basic set. eg STRAND, FRAME for GFF file\r
214      * @param key String\r
215      */\r
216     public Object getValue(String key)\r
217     {\r
218       if(otherDetails==null)\r
219         return null;\r
220       else\r
221         return otherDetails.get(key);\r
222     }\r
223 \r
224     /**\r
225      * Used for setting values which are not in the\r
226      * basic set. eg STRAND, FRAME for GFF file\r
227      * @param key   eg STRAND\r
228      * @param value eg +\r
229      */\r
230     public void setValue(String key, Object value)\r
231     {\r
232       if(value!=null)\r
233       {\r
234         if (otherDetails == null)\r
235           otherDetails = new Hashtable();\r
236 \r
237         otherDetails.put(key, value);\r
238       }\r
239     }\r
240 \r
241 \r
242     /*\r
243      * The following methods are added to maintain\r
244      * the castor Uniprot mapping file for the moment.\r
245      */\r
246     public void setStatus(String status)\r
247     {\r
248       setValue("status", status);\r
249     }\r
250 \r
251     public String getStatus()\r
252     {\r
253       if (otherDetails != null)\r
254         return otherDetails.get("status").toString();\r
255       else\r
256         return null;\r
257     }\r
258 \r
259     public void setPosition(int pos)\r
260     {\r
261       begin = pos;\r
262       end = pos;\r
263     }\r
264 \r
265     public int getPosition()\r
266     {\r
267       return begin;\r
268     }\r
269 \r
270 }\r