2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
21 package jalview.datamodel;
23 import java.util.HashMap;
25 import java.util.Vector;
33 public class SequenceFeature
35 private static final String STATUS = "status";
37 private static final String STRAND = "STRAND";
39 // private key for Phase designed not to conflict with real GFF data
40 private static final String PHASE = "!Phase";
42 private static final String ATTRIBUTES = "ATTRIBUTES";
52 public String description;
54 public Map<String, Object> otherDetails;
56 public Vector<String> links;
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;
62 public SequenceFeature()
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.
73 public SequenceFeature(SequenceFeature cpy)
82 type = new String(cpy.type);
84 if (cpy.description != null)
86 description = new String(cpy.description);
88 if (cpy.featureGroup != null)
90 featureGroup = new String(cpy.featureGroup);
92 if (cpy.otherDetails != null)
96 otherDetails = (Map<String, Object>) ((HashMap<String, Object>) cpy.otherDetails)
103 if (cpy.links != null && cpy.links.size() > 0)
105 links = new Vector<String>();
106 for (int i = 0, iSize = cpy.links.size(); i < iSize; i++)
108 links.addElement(cpy.links.elementAt(i));
114 public SequenceFeature(String type, String desc, String status,
115 int begin, int end, String featureGroup)
118 this.description = desc;
119 setValue(STATUS, status);
122 this.featureGroup = featureGroup;
125 public SequenceFeature(String type, String desc, int begin, int end,
126 float score, String featureGroup)
129 this.description = desc;
133 this.featureGroup = featureGroup;
137 * Two features are considered equal if they have the same type, group,
138 * description, start, end, phase, strand, and (if present) 'Name', ID' and
139 * 'Parent' attributes.
141 * Note we need to check Parent to distinguish the same exon occurring in
142 * different transcripts (in Ensembl GFF). This allows assembly of transcript
143 * sequences from their component exon regions.
146 public boolean equals(Object o)
148 return equals(o, false);
152 * Overloaded method allows the equality test to optionally ignore the
153 * 'Parent' attribute of a feature. This supports avoiding adding many
154 * superficially duplicate 'exon' or CDS features to genomic or protein
158 * @param ignoreParent
161 public boolean equals(Object o, boolean ignoreParent)
163 if (o == null || !(o instanceof SequenceFeature))
168 SequenceFeature sf = (SequenceFeature) o;
169 if (begin != sf.begin || end != sf.end || score != sf.score)
174 if (getStrand() != sf.getStrand())
179 if (!(type + description + featureGroup + getPhase()).equals(sf.type
180 + sf.description + sf.featureGroup + sf.getPhase()))
184 if (!equalAttribute(getValue("ID"), sf.getValue("ID")))
188 if (!equalAttribute(getValue("Name"), sf.getValue("Name")))
194 if (!equalAttribute(getValue("Parent"), sf.getValue("Parent")))
203 * Returns true if both values are null, are both non-null and equal
209 protected static boolean equalAttribute(Object att1, Object att2)
211 if (att1 == null && att2 == null)
217 return att1.equals(att2);
219 return att2.equals(att1);
225 * @return DOCUMENT ME!
227 public int getBegin()
232 public void setBegin(int start)
240 * @return DOCUMENT ME!
247 public void setEnd(int end)
255 * @return DOCUMENT ME!
257 public String getType()
262 public void setType(String type)
270 * @return DOCUMENT ME!
272 public String getDescription()
277 public void setDescription(String desc)
282 public String getFeatureGroup()
287 public void setFeatureGroup(String featureGroup)
289 this.featureGroup = featureGroup;
292 public void addLink(String labelLink)
296 links = new Vector<String>();
299 links.insertElementAt(labelLink, 0);
302 public float getScore()
307 public void setScore(float value)
313 * Used for getting values which are not in the basic set. eg STRAND, PHASE
319 public Object getValue(String key)
321 if (otherDetails == null)
327 return otherDetails.get(key);
332 * Returns a property value for the given key if known, else the specified
336 * @param defaultValue
339 public Object getValue(String key, Object defaultValue)
341 Object value = getValue(key);
342 return value == null ? defaultValue : value;
346 * Used for setting values which are not in the basic set. eg STRAND, FRAME
354 public void setValue(String key, Object value)
358 if (otherDetails == null)
360 otherDetails = new HashMap<String, Object>();
363 otherDetails.put(key, value);
368 * The following methods are added to maintain the castor Uniprot mapping file
371 public void setStatus(String status)
373 setValue(STATUS, status);
376 public String getStatus()
378 return (String) getValue(STATUS);
381 public void setAttributes(String attr)
383 setValue(ATTRIBUTES, attr);
386 public String getAttributes()
388 return (String) getValue(ATTRIBUTES);
391 public void setPosition(int pos)
397 public int getPosition()
403 * Return 1 for forward strand ('+' in GFF), -1 for reverse strand ('-' in
404 * GFF), and 0 for unknown or not (validly) specified
408 public int getStrand()
411 if (otherDetails != null)
413 Object str = otherDetails.get(STRAND);
418 else if ("+".equals(str))
426 public void setStrand(String strand)
428 setValue(STRAND, strand);
431 public void setPhase(String phase)
433 setValue(PHASE, phase);
436 public String getPhase()
438 return (String) getValue(PHASE);
442 * Readable representation, for debug only, not guaranteed not to change
446 public String toString()
448 return String.format("%d %d %s %s", getBegin(), getEnd(), getType(),
453 * Overridden to ensure that whenever two objects are equal, they have the
457 public int hashCode()
459 String s = getType() + getDescription() + getFeatureGroup()
460 + getValue("ID") + getValue("Name") + getValue("Parent")
462 return s.hashCode() + getBegin() + getEnd() + (int) getScore()