9e1399aca043cc93633bec01165c083e2c0d8c18
[jalview.git] / src / jalview / datamodel / SequenceFeature.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3  * Copyright (C) 2014 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.*;
24
25 /**
26  * DOCUMENT ME!
27  * 
28  * @author $author$
29  * @version $Revision$
30  */
31 public class SequenceFeature
32 {
33   public int begin;
34
35   public int end;
36
37   public float score;
38
39   public String type;
40
41   public String description;
42
43   public Hashtable otherDetails;
44
45   public java.util.Vector links;
46
47   // Feature group can be set from a features file
48   // as a group of features between STARTGROUP and ENDGROUP markers
49   public String featureGroup;
50
51   public SequenceFeature()
52   {
53   }
54
55   /**
56    * Constructs a duplicate feature. Note: Uses clone on the otherDetails so
57    * only shallow copies are made of additional properties and method will
58    * silently fail if unclonable objects are found in the hash.
59    * 
60    * @param cpy
61    */
62   public SequenceFeature(SequenceFeature cpy)
63   {
64     if (cpy != null)
65     {
66       begin = cpy.begin;
67       end = cpy.end;
68       score = cpy.score;
69       if (cpy.type != null)
70       {
71         type = new String(cpy.type);
72       }
73       if (cpy.description != null)
74       {
75         description = new String(cpy.description);
76       }
77       if (cpy.featureGroup != null)
78       {
79         featureGroup = new String(cpy.featureGroup);
80       }
81       if (cpy.otherDetails != null)
82       {
83         try
84         {
85           otherDetails = (Hashtable) cpy.otherDetails.clone();
86         } catch (Exception e)
87         {
88           // Uncloneable objects in the otherDetails - don't complain
89         }
90       }
91       if (cpy.links != null && cpy.links.size() > 0)
92       {
93         links = new Vector();
94         for (int i = 0, iSize = cpy.links.size(); i < iSize; i++)
95         {
96           links.addElement(cpy.links.elementAt(i));
97         }
98       }
99     }
100   }
101
102   public SequenceFeature(String type, String desc, String status,
103           int begin, int end, String featureGroup)
104   {
105     this.type = type;
106     this.description = desc;
107     setValue("status", status);
108     this.begin = begin;
109     this.end = end;
110     this.featureGroup = featureGroup;
111   }
112
113   public SequenceFeature(String type, String desc, int begin, int end,
114           float score, String featureGroup)
115   {
116     this.type = type;
117     this.description = desc;
118     this.begin = begin;
119     this.end = end;
120     this.score = score;
121     this.featureGroup = featureGroup;
122   }
123
124   public boolean equals(SequenceFeature sf)
125   {
126     if (begin != sf.begin || end != sf.end || score != sf.score)
127     {
128       return false;
129     }
130
131     if (!(type + description + featureGroup).equals(sf.type
132             + 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 basic set. eg STRAND, FRAME
232    * for GFF file
233    * 
234    * @param key
235    *          String
236    */
237   public Object getValue(String key)
238   {
239     if (otherDetails == null)
240     {
241       return null;
242     }
243     else
244     {
245       return otherDetails.get(key);
246     }
247   }
248
249   /**
250    * Used for setting values which are not in the basic set. eg STRAND, FRAME
251    * for GFF file
252    * 
253    * @param key
254    *          eg STRAND
255    * @param value
256    *          eg +
257    */
258   public void setValue(String key, Object value)
259   {
260     if (value != null)
261     {
262       if (otherDetails == null)
263       {
264         otherDetails = new Hashtable();
265       }
266
267       otherDetails.put(key, value);
268     }
269   }
270
271   /*
272    * The following methods are added to maintain the castor Uniprot mapping file
273    * for the moment.
274    */
275   public void setStatus(String status)
276   {
277     setValue("status", status);
278   }
279
280   public String getStatus()
281   {
282     if (otherDetails != null)
283     {
284       String stat = (String) otherDetails.get("status");
285       if (stat != null)
286         return new String(stat);
287     }
288     return null;
289   }
290
291   public void setPosition(int pos)
292   {
293     begin = pos;
294     end = pos;
295   }
296
297   public int getPosition()
298   {
299     return begin;
300   }
301
302 }