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));
115 * Constructor including a Status value
122 * @param featureGroup
124 public SequenceFeature(String type, String desc, String status,
125 int begin, int end, String featureGroup)
127 this(type, desc, begin, end, featureGroup);
138 * @param featureGroup
140 SequenceFeature(String type, String desc, int begin, int end,
144 this.description = desc;
147 this.featureGroup = featureGroup;
151 * Constructor including a score value
158 * @param featureGroup
160 public SequenceFeature(String type, String desc, int begin, int end,
161 float score, String featureGroup)
163 this(type, desc, begin, end, featureGroup);
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.
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.
177 public boolean equals(Object o)
179 return equals(o, false);
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
189 * @param ignoreParent
192 public boolean equals(Object o, boolean ignoreParent)
194 if (o == null || !(o instanceof SequenceFeature))
199 SequenceFeature sf = (SequenceFeature) o;
200 if (begin != sf.begin || end != sf.end || score != sf.score)
205 if (getStrand() != sf.getStrand())
210 if (!(type + description + featureGroup + getPhase()).equals(sf.type
211 + sf.description + sf.featureGroup + sf.getPhase()))
215 if (!equalAttribute(getValue("ID"), sf.getValue("ID")))
219 if (!equalAttribute(getValue("Name"), sf.getValue("Name")))
225 if (!equalAttribute(getValue("Parent"), sf.getValue("Parent")))
234 * Returns true if both values are null, are both non-null and equal
240 protected static boolean equalAttribute(Object att1, Object att2)
242 if (att1 == null && att2 == null)
248 return att1.equals(att2);
250 return att2.equals(att1);
256 * @return DOCUMENT ME!
258 public int getBegin()
263 public void setBegin(int start)
271 * @return DOCUMENT ME!
278 public void setEnd(int end)
286 * @return DOCUMENT ME!
288 public String getType()
293 public void setType(String type)
301 * @return DOCUMENT ME!
303 public String getDescription()
308 public void setDescription(String desc)
313 public String getFeatureGroup()
318 public void setFeatureGroup(String featureGroup)
320 this.featureGroup = featureGroup;
323 public void addLink(String labelLink)
327 links = new Vector<String>();
330 links.insertElementAt(labelLink, 0);
333 public float getScore()
338 public void setScore(float value)
344 * Used for getting values which are not in the basic set. eg STRAND, PHASE
350 public Object getValue(String key)
352 if (otherDetails == null)
358 return otherDetails.get(key);
363 * Returns a property value for the given key if known, else the specified
367 * @param defaultValue
370 public Object getValue(String key, Object defaultValue)
372 Object value = getValue(key);
373 return value == null ? defaultValue : value;
377 * Used for setting values which are not in the basic set. eg STRAND, FRAME
385 public void setValue(String key, Object value)
389 if (otherDetails == null)
391 otherDetails = new HashMap<String, Object>();
394 otherDetails.put(key, value);
399 * The following methods are added to maintain the castor Uniprot mapping file
402 public void setStatus(String status)
404 setValue(STATUS, status);
407 public String getStatus()
409 return (String) getValue(STATUS);
412 public void setAttributes(String attr)
414 setValue(ATTRIBUTES, attr);
417 public String getAttributes()
419 return (String) getValue(ATTRIBUTES);
422 public void setPosition(int pos)
428 public int getPosition()
434 * Return 1 for forward strand ('+' in GFF), -1 for reverse strand ('-' in
435 * GFF), and 0 for unknown or not (validly) specified
439 public int getStrand()
442 if (otherDetails != null)
444 Object str = otherDetails.get(STRAND);
449 else if ("+".equals(str))
458 * Set the value of strand
461 * should be "+" for forward, or "-" for reverse
463 public void setStrand(String strand)
465 setValue(STRAND, strand);
468 public void setPhase(String phase)
470 setValue(PHASE, phase);
473 public String getPhase()
475 return (String) getValue(PHASE);
479 * Readable representation, for debug only, not guaranteed not to change
483 public String toString()
485 return String.format("%d %d %s %s", getBegin(), getEnd(), getType(),
490 * Overridden to ensure that whenever two objects are equal, they have the
494 public int hashCode()
496 String s = getType() + getDescription() + getFeatureGroup()
497 + getValue("ID") + getValue("Name") + getValue("Parent")
499 return s.hashCode() + getBegin() + getEnd() + (int) getScore()