JAL-2049 small refactor of constructors to avoid code duplication
[jalview.git] / src / jalview / datamodel / SequenceFeature.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.HashMap;
24 import java.util.Map;
25 import java.util.Vector;
26
27 /**
28  * DOCUMENT ME!
29  * 
30  * @author $author$
31  * @version $Revision$
32  */
33 public class SequenceFeature
34 {
35   private static final String STATUS = "status";
36
37   private static final String STRAND = "STRAND";
38
39   // private key for Phase designed not to conflict with real GFF data
40   private static final String PHASE = "!Phase";
41
42   private static final String ATTRIBUTES = "ATTRIBUTES";
43
44   public int begin;
45
46   public int end;
47
48   public float score;
49
50   public String type;
51
52   public String description;
53
54   public Map<String, Object> otherDetails;
55
56   public Vector<String> links;
57
58   // Feature group can be set from a features file
59   // as a group of features between STARTGROUP and ENDGROUP markers
60   public String featureGroup;
61
62   public SequenceFeature()
63   {
64   }
65
66   /**
67    * Constructs a duplicate feature. Note: Uses makes a shallow copy of the
68    * otherDetails map, so the new and original SequenceFeature may reference the
69    * same objects in the map.
70    * 
71    * @param cpy
72    */
73   public SequenceFeature(SequenceFeature cpy)
74   {
75     if (cpy != null)
76     {
77       begin = cpy.begin;
78       end = cpy.end;
79       score = cpy.score;
80       if (cpy.type != null)
81       {
82         type = new String(cpy.type);
83       }
84       if (cpy.description != null)
85       {
86         description = new String(cpy.description);
87       }
88       if (cpy.featureGroup != null)
89       {
90         featureGroup = new String(cpy.featureGroup);
91       }
92       if (cpy.otherDetails != null)
93       {
94         try
95         {
96           otherDetails = (Map<String, Object>) ((HashMap<String, Object>) cpy.otherDetails)
97                   .clone();
98         } catch (Exception e)
99         {
100           // ignore
101         }
102       }
103       if (cpy.links != null && cpy.links.size() > 0)
104       {
105         links = new Vector<String>();
106         for (int i = 0, iSize = cpy.links.size(); i < iSize; i++)
107         {
108           links.addElement(cpy.links.elementAt(i));
109         }
110       }
111     }
112   }
113
114   /**
115    * Constructor including a Status value
116    * 
117    * @param type
118    * @param desc
119    * @param status
120    * @param begin
121    * @param end
122    * @param featureGroup
123    */
124   public SequenceFeature(String type, String desc, String status,
125           int begin, int end, String featureGroup)
126   {
127     this(type, desc, begin, end, featureGroup);
128     setStatus(status);
129   }
130
131   /**
132    * Constructor
133    * 
134    * @param type
135    * @param desc
136    * @param begin
137    * @param end
138    * @param featureGroup
139    */
140   SequenceFeature(String type, String desc, int begin, int end,
141           String featureGroup)
142   {
143     this.type = type;
144     this.description = desc;
145     this.begin = begin;
146     this.end = end;
147     this.featureGroup = featureGroup;
148   }
149
150   /**
151    * Constructor including a score value
152    * 
153    * @param type
154    * @param desc
155    * @param begin
156    * @param end
157    * @param score
158    * @param featureGroup
159    */
160   public SequenceFeature(String type, String desc, int begin, int end,
161           float score, String featureGroup)
162   {
163     this(type, desc, begin, end, featureGroup);
164     this.score = score;
165   }
166
167   /**
168    * Two features are considered equal if they have the same type, group,
169    * description, start, end, phase, strand, and (if present) 'Name', ID' and
170    * 'Parent' attributes.
171    * 
172    * Note we need to check Parent to distinguish the same exon occurring in
173    * different transcripts (in Ensembl GFF). This allows assembly of transcript
174    * sequences from their component exon regions.
175    */
176   @Override
177   public boolean equals(Object o)
178   {
179     return equals(o, false);
180   }
181
182   /**
183    * Overloaded method allows the equality test to optionally ignore the
184    * 'Parent' attribute of a feature. This supports avoiding adding many
185    * superficially duplicate 'exon' or CDS features to genomic or protein
186    * sequence.
187    * 
188    * @param o
189    * @param ignoreParent
190    * @return
191    */
192   public boolean equals(Object o, boolean ignoreParent)
193   {
194     if (o == null || !(o instanceof SequenceFeature))
195     {
196       return false;
197     }
198
199     SequenceFeature sf = (SequenceFeature) o;
200     if (begin != sf.begin || end != sf.end || score != sf.score)
201     {
202       return false;
203     }
204
205     if (getStrand() != sf.getStrand())
206     {
207       return false;
208     }
209
210     if (!(type + description + featureGroup + getPhase()).equals(sf.type
211             + sf.description + sf.featureGroup + sf.getPhase()))
212     {
213       return false;
214     }
215     if (!equalAttribute(getValue("ID"), sf.getValue("ID")))
216     {
217       return false;
218     }
219     if (!equalAttribute(getValue("Name"), sf.getValue("Name")))
220     {
221       return false;
222     }
223     if (!ignoreParent)
224     {
225       if (!equalAttribute(getValue("Parent"), sf.getValue("Parent")))
226       {
227         return false;
228       }
229     }
230     return true;
231   }
232
233   /**
234    * Returns true if both values are null, are both non-null and equal
235    * 
236    * @param att1
237    * @param att2
238    * @return
239    */
240   protected static boolean equalAttribute(Object att1, Object att2)
241   {
242     if (att1 == null && att2 == null)
243     {
244       return true;
245     }
246     if (att1 != null)
247     {
248       return att1.equals(att2);
249     }
250     return att2.equals(att1);
251   }
252
253   /**
254    * DOCUMENT ME!
255    * 
256    * @return DOCUMENT ME!
257    */
258   public int getBegin()
259   {
260     return begin;
261   }
262
263   public void setBegin(int start)
264   {
265     this.begin = start;
266   }
267
268   /**
269    * DOCUMENT ME!
270    * 
271    * @return DOCUMENT ME!
272    */
273   public int getEnd()
274   {
275     return end;
276   }
277
278   public void setEnd(int end)
279   {
280     this.end = end;
281   }
282
283   /**
284    * DOCUMENT ME!
285    * 
286    * @return DOCUMENT ME!
287    */
288   public String getType()
289   {
290     return type;
291   }
292
293   public void setType(String type)
294   {
295     this.type = type;
296   }
297
298   /**
299    * DOCUMENT ME!
300    * 
301    * @return DOCUMENT ME!
302    */
303   public String getDescription()
304   {
305     return description;
306   }
307
308   public void setDescription(String desc)
309   {
310     description = desc;
311   }
312
313   public String getFeatureGroup()
314   {
315     return featureGroup;
316   }
317
318   public void setFeatureGroup(String featureGroup)
319   {
320     this.featureGroup = featureGroup;
321   }
322
323   public void addLink(String labelLink)
324   {
325     if (links == null)
326     {
327       links = new Vector<String>();
328     }
329
330     links.insertElementAt(labelLink, 0);
331   }
332
333   public float getScore()
334   {
335     return score;
336   }
337
338   public void setScore(float value)
339   {
340     score = value;
341   }
342
343   /**
344    * Used for getting values which are not in the basic set. eg STRAND, PHASE
345    * for GFF file
346    * 
347    * @param key
348    *          String
349    */
350   public Object getValue(String key)
351   {
352     if (otherDetails == null)
353     {
354       return null;
355     }
356     else
357     {
358       return otherDetails.get(key);
359     }
360   }
361
362   /**
363    * Returns a property value for the given key if known, else the specified
364    * default value
365    * 
366    * @param key
367    * @param defaultValue
368    * @return
369    */
370   public Object getValue(String key, Object defaultValue)
371   {
372     Object value = getValue(key);
373     return value == null ? defaultValue : value;
374   }
375
376   /**
377    * Used for setting values which are not in the basic set. eg STRAND, FRAME
378    * for GFF file
379    * 
380    * @param key
381    *          eg STRAND
382    * @param value
383    *          eg +
384    */
385   public void setValue(String key, Object value)
386   {
387     if (value != null)
388     {
389       if (otherDetails == null)
390       {
391         otherDetails = new HashMap<String, Object>();
392       }
393
394       otherDetails.put(key, value);
395     }
396   }
397
398   /*
399    * The following methods are added to maintain the castor Uniprot mapping file
400    * for the moment.
401    */
402   public void setStatus(String status)
403   {
404     setValue(STATUS, status);
405   }
406
407   public String getStatus()
408   {
409     return (String) getValue(STATUS);
410   }
411
412   public void setAttributes(String attr)
413   {
414     setValue(ATTRIBUTES, attr);
415   }
416
417   public String getAttributes()
418   {
419     return (String) getValue(ATTRIBUTES);
420   }
421
422   public void setPosition(int pos)
423   {
424     begin = pos;
425     end = pos;
426   }
427
428   public int getPosition()
429   {
430     return begin;
431   }
432
433   /**
434    * Return 1 for forward strand ('+' in GFF), -1 for reverse strand ('-' in
435    * GFF), and 0 for unknown or not (validly) specified
436    * 
437    * @return
438    */
439   public int getStrand()
440   {
441     int strand = 0;
442     if (otherDetails != null)
443     {
444       Object str = otherDetails.get(STRAND);
445       if ("-".equals(str))
446       {
447         strand = -1;
448       }
449       else if ("+".equals(str))
450       {
451         strand = 1;
452       }
453     }
454     return strand;
455   }
456
457   /**
458    * Set the value of strand
459    * 
460    * @param strand
461    *          should be "+" for forward, or "-" for reverse
462    */
463   public void setStrand(String strand)
464   {
465     setValue(STRAND, strand);
466   }
467
468   public void setPhase(String phase)
469   {
470     setValue(PHASE, phase);
471   }
472
473   public String getPhase()
474   {
475     return (String) getValue(PHASE);
476   }
477
478   /**
479    * Readable representation, for debug only, not guaranteed not to change
480    * between versions
481    */
482   @Override
483   public String toString()
484   {
485     return String.format("%d %d %s %s", getBegin(), getEnd(), getType(),
486             getDescription());
487   }
488
489   /**
490    * Overridden to ensure that whenever two objects are equal, they have the
491    * same hashCode
492    */
493   @Override
494   public int hashCode()
495   {
496     String s = getType() + getDescription() + getFeatureGroup()
497             + getValue("ID") + getValue("Name") + getValue("Parent")
498             + getPhase();
499     return s.hashCode() + getBegin() + getEnd() + (int) getScore()
500             + getStrand();
501   }
502 }