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 key for feature number designed not to conflict with real GFF data
43 private static final String NUMBER = "!Num";
46 * ATTRIBUTES is reserved for the GFF 'column 9' data, formatted as
47 * name1=value1;name2=value2,value3;...etc
49 private static final String ATTRIBUTES = "ATTRIBUTES";
59 public String description;
61 public Map<String, Object> otherDetails;
63 public Vector<String> links;
65 // Feature group can be set from a features file
66 // as a group of features between STARTGROUP and ENDGROUP markers
67 public String featureGroup;
69 public SequenceFeature()
74 * Constructs a duplicate feature. Note: Uses makes a shallow copy of the
75 * otherDetails map, so the new and original SequenceFeature may reference the
76 * same objects in the map.
80 public SequenceFeature(SequenceFeature cpy)
89 type = new String(cpy.type);
91 if (cpy.description != null)
93 description = new String(cpy.description);
95 if (cpy.featureGroup != null)
97 featureGroup = new String(cpy.featureGroup);
99 if (cpy.otherDetails != null)
103 otherDetails = (Map<String, Object>) ((HashMap<String, Object>) cpy.otherDetails)
105 } catch (Exception e)
110 if (cpy.links != null && cpy.links.size() > 0)
112 links = new Vector<String>();
113 for (int i = 0, iSize = cpy.links.size(); i < iSize; i++)
115 links.addElement(cpy.links.elementAt(i));
122 * Constructor including a Status value
129 * @param featureGroup
131 public SequenceFeature(String type, String desc, String status,
132 int begin, int end, String featureGroup)
134 this(type, desc, begin, end, featureGroup);
145 * @param featureGroup
147 SequenceFeature(String type, String desc, int begin, int end,
151 this.description = desc;
154 this.featureGroup = featureGroup;
158 * Constructor including a score value
165 * @param featureGroup
167 public SequenceFeature(String type, String desc, int begin, int end,
168 float score, String featureGroup)
170 this(type, desc, begin, end, featureGroup);
175 * Two features are considered equal if they have the same type, group,
176 * description, start, end, phase, strand, and (if present) 'Name', ID' and
177 * 'Parent' attributes.
179 * Note we need to check Parent to distinguish the same exon occurring in
180 * different transcripts (in Ensembl GFF). This allows assembly of transcript
181 * sequences from their component exon regions.
184 public boolean equals(Object o)
186 return equals(o, false);
190 * Overloaded method allows the equality test to optionally ignore the
191 * 'Parent' attribute of a feature. This supports avoiding adding many
192 * superficially duplicate 'exon' or CDS features to genomic or protein
196 * @param ignoreParent
199 public boolean equals(Object o, boolean ignoreParent)
201 if (o == null || !(o instanceof SequenceFeature))
206 SequenceFeature sf = (SequenceFeature) o;
207 if (begin != sf.begin || end != sf.end || score != sf.score)
212 if (getStrand() != sf.getStrand())
217 if (!(type + description + featureGroup + getPhase()).equals(sf.type
218 + sf.description + sf.featureGroup + sf.getPhase()))
222 if (!equalAttribute(getValue("ID"), sf.getValue("ID")))
226 if (!equalAttribute(getValue("Name"), sf.getValue("Name")))
232 if (!equalAttribute(getValue("Parent"), sf.getValue("Parent")))
241 * Returns true if both values are null, are both non-null and equal
247 protected static boolean equalAttribute(Object att1, Object att2)
249 if (att1 == null && att2 == null)
255 return att1.equals(att2);
257 return att2.equals(att1);
263 * @return DOCUMENT ME!
265 public int getBegin()
270 public void setBegin(int start)
278 * @return DOCUMENT ME!
285 public void setEnd(int end)
293 * @return DOCUMENT ME!
295 public String getType()
300 public void setType(String type)
308 * @return DOCUMENT ME!
310 public String getDescription()
315 public void setDescription(String desc)
320 public String getFeatureGroup()
325 public void setFeatureGroup(String featureGroup)
327 this.featureGroup = featureGroup;
330 public void addLink(String labelLink)
334 links = new Vector<String>();
337 links.insertElementAt(labelLink, 0);
340 public float getScore()
345 public void setScore(float value)
351 * Used for getting values which are not in the basic set. eg STRAND, PHASE
357 public Object getValue(String key)
359 if (otherDetails == null)
365 return otherDetails.get(key);
370 * Returns a property value for the given key if known, else the specified
374 * @param defaultValue
377 public Object getValue(String key, Object defaultValue)
379 Object value = getValue(key);
380 return value == null ? defaultValue : value;
384 * Used for setting values which are not in the basic set. eg STRAND, FRAME
392 public void setValue(String key, Object value)
396 if (otherDetails == null)
398 otherDetails = new HashMap<String, Object>();
401 otherDetails.put(key, value);
406 * The following methods are added to maintain the castor Uniprot mapping file
409 public void setStatus(String status)
411 setValue(STATUS, status);
414 public String getStatus()
416 return (String) getValue(STATUS);
419 public void setAttributes(String attr)
421 setValue(ATTRIBUTES, attr);
424 public String getAttributes()
426 return (String) getValue(ATTRIBUTES);
429 public void setPosition(int pos)
435 public int getPosition()
441 * Return 1 for forward strand ('+' in GFF), -1 for reverse strand ('-' in
442 * GFF), and 0 for unknown or not (validly) specified
446 public int getStrand()
449 if (otherDetails != null)
451 Object str = otherDetails.get(STRAND);
456 else if ("+".equals(str))
465 * Set the value of strand
468 * should be "+" for forward, or "-" for reverse
470 public void setStrand(String strand)
472 setValue(STRAND, strand);
475 public void setPhase(String phase)
477 setValue(PHASE, phase);
480 public String getPhase()
482 return (String) getValue(PHASE);
486 * Set the ordinal number of this feature on the sequence
490 public void setFeatureNumber(int num)
492 setValue(NUMBER, String.valueOf(num));
496 * Returns the feature number if set, else 0. Intended for use as the ordinal
497 * position of the feature on the sequence for features of the same type.
501 public int getFeatureNumber()
505 return Integer.parseInt((String) getValue(NUMBER));
506 } catch (Exception e)
508 // property absent or not numeric
514 * Readable representation, for debug only, not guaranteed not to change
518 public String toString()
520 return String.format("%d %d %s %s", getBegin(), getEnd(), getType(),
525 * Overridden to ensure that whenever two objects are equal, they have the
529 public int hashCode()
531 String s = getType() + getDescription() + getFeatureGroup()
532 + getValue("ID") + getValue("Name") + getValue("Parent")
534 return s.hashCode() + getBegin() + getEnd() + (int) getScore()