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