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 jalview.datamodel.features.FeatureLocationI;
25 import java.util.HashMap;
27 import java.util.Vector;
35 public class SequenceFeature implements FeatureLocationI
37 private static final String STATUS = "status";
39 private static final String STRAND = "STRAND";
41 // private key for Phase designed not to conflict with real GFF data
42 private static final String PHASE = "!Phase";
44 // private key for ENA location designed not to conflict with real GFF data
45 private static final String LOCATION = "!Location";
48 * ATTRIBUTES is reserved for the GFF 'column 9' data, formatted as
49 * name1=value1;name2=value2,value3;...etc
51 private static final String ATTRIBUTES = "ATTRIBUTES";
54 * type, begin, end, featureGroup are final to ensure that
55 * the integrity of SequenceFeatures data store can't be
56 * broken by direct update of these fields
58 public final String type;
60 public final int begin;
64 public final String featureGroup;
68 public String description;
71 * a map of key-value pairs; may be populated from GFF 'column 9' data,
72 * other data sources (e.g. GenBank file), or programmatically
74 public Map<String, Object> otherDetails;
76 public Vector<String> links;
79 * Constructs a duplicate feature. Note: Uses makes a shallow copy of the
80 * otherDetails map, so the new and original SequenceFeature may reference the
81 * same objects in the map.
85 public SequenceFeature(SequenceFeature cpy)
87 this(cpy, cpy.getBegin(), cpy.getEnd(), cpy.getFeatureGroup());
90 description = cpy.description;
91 if (cpy.otherDetails != null)
95 otherDetails = (Map<String, Object>) ((HashMap<String, Object>) cpy.otherDetails)
102 if (cpy.links != null && cpy.links.size() > 0)
104 links = new Vector<String>();
105 for (int i = 0, iSize = cpy.links.size(); i < iSize; i++)
107 links.addElement(cpy.links.elementAt(i));
113 * Constructor including a Status value
120 * @param featureGroup
122 public SequenceFeature(String type, String desc, String status,
123 int begin, int end, String featureGroup)
125 this(type, desc, begin, end, featureGroup);
136 * @param featureGroup
138 SequenceFeature(String type, String desc, int begin, int end,
142 this.description = desc;
145 this.featureGroup = featureGroup;
149 * Constructor including a score value
156 * @param featureGroup
158 public SequenceFeature(String type, String desc, int begin, int end,
159 float score, String featureGroup)
161 this(type, desc, begin, end, featureGroup);
166 * A copy constructor that allows the begin and end positions and group to be
174 public SequenceFeature(SequenceFeature sf, int newBegin, int newEnd,
177 this(sf.getType(), newBegin, newEnd, newGroup);
181 * Constructor that sets the final fields type, begin, end, group
188 private SequenceFeature(String theType, int theBegin, int theEnd,
194 featureGroup = theGroup;
198 * Two features are considered equal if they have the same type, group,
199 * description, start, end, phase, strand, and (if present) 'Name', ID' and
200 * 'Parent' attributes.
202 * Note we need to check Parent to distinguish the same exon occurring in
203 * different transcripts (in Ensembl GFF). This allows assembly of transcript
204 * sequences from their component exon regions.
207 public boolean equals(Object o)
209 return equals(o, false);
213 * Overloaded method allows the equality test to optionally ignore the
214 * 'Parent' attribute of a feature. This supports avoiding adding many
215 * superficially duplicate 'exon' or CDS features to genomic or protein
219 * @param ignoreParent
222 public boolean equals(Object o, boolean ignoreParent)
224 if (o == null || !(o instanceof SequenceFeature))
229 SequenceFeature sf = (SequenceFeature) o;
230 boolean sameScore = Float.isNaN(score) ? Float.isNaN(sf.score)
232 if (begin != sf.begin || end != sf.end || !sameScore)
237 if (getStrand() != sf.getStrand())
242 if (!(type + description + featureGroup + getPhase()).equals(sf.type
243 + sf.description + sf.featureGroup + sf.getPhase()))
247 if (!equalAttribute(getValue("ID"), sf.getValue("ID")))
251 if (!equalAttribute(getValue("Name"), sf.getValue("Name")))
257 if (!equalAttribute(getValue("Parent"), sf.getValue("Parent")))
266 * Returns true if both values are null, are both non-null and equal
272 protected static boolean equalAttribute(Object att1, Object att2)
274 if (att1 == null && att2 == null)
280 return att1.equals(att2);
282 return att2.equals(att1);
288 * @return DOCUMENT ME!
291 public int getBegin()
299 * @return DOCUMENT ME!
310 * @return DOCUMENT ME!
312 public String getType()
320 * @return DOCUMENT ME!
322 public String getDescription()
327 public void setDescription(String desc)
332 public String getFeatureGroup()
337 public void addLink(String labelLink)
341 links = new Vector<String>();
344 if (!links.contains(labelLink))
346 links.insertElementAt(labelLink, 0);
350 public float getScore()
355 public void setScore(float value)
361 * Used for getting values which are not in the basic set. eg STRAND, PHASE
367 public Object getValue(String key)
369 if (otherDetails == null)
375 return otherDetails.get(key);
380 * Returns a property value for the given key if known, else the specified
384 * @param defaultValue
387 public Object getValue(String key, Object defaultValue)
389 Object value = getValue(key);
390 return value == null ? defaultValue : value;
394 * Used for setting values which are not in the basic set. eg STRAND, FRAME
402 public void setValue(String key, Object value)
406 if (otherDetails == null)
408 otherDetails = new HashMap<String, Object>();
411 otherDetails.put(key, value);
416 * The following methods are added to maintain the castor Uniprot mapping file
419 public void setStatus(String status)
421 setValue(STATUS, status);
424 public String getStatus()
426 return (String) getValue(STATUS);
429 public void setAttributes(String attr)
431 setValue(ATTRIBUTES, attr);
434 public String getAttributes()
436 return (String) getValue(ATTRIBUTES);
440 * Return 1 for forward strand ('+' in GFF), -1 for reverse strand ('-' in
441 * GFF), and 0 for unknown or not (validly) specified
445 public int getStrand()
448 if (otherDetails != null)
450 Object str = otherDetails.get(STRAND);
455 else if ("+".equals(str))
464 * Set the value of strand
467 * should be "+" for forward, or "-" for reverse
469 public void setStrand(String strand)
471 setValue(STRAND, strand);
474 public void setPhase(String phase)
476 setValue(PHASE, phase);
479 public String getPhase()
481 return (String) getValue(PHASE);
485 * Sets the 'raw' ENA format location specifier e.g. join(12..45,89..121)
489 public void setEnaLocation(String loc)
491 setValue(LOCATION, loc);
495 * Gets the 'raw' ENA format location specifier e.g. join(12..45,89..121)
499 public String getEnaLocation()
501 return (String) getValue(LOCATION);
505 * Readable representation, for debug only, not guaranteed not to change
509 public String toString()
511 return String.format("%d %d %s %s", getBegin(), getEnd(), getType(),
516 * Overridden to ensure that whenever two objects are equal, they have the
520 public int hashCode()
522 String s = getType() + getDescription() + getFeatureGroup()
523 + getValue("ID") + getValue("Name") + getValue("Parent")
525 return s.hashCode() + getBegin() + getEnd() + (int) getScore()
530 * Answers true if the feature's start/end values represent two related
531 * positions, rather than ends of a range. Such features may be visualised or
532 * reported differently to features on a range.
535 public boolean isContactFeature()
537 // TODO abstract one day to a FeatureType class
538 if ("disulfide bond".equalsIgnoreCase(type)
539 || "disulphide bond".equalsIgnoreCase(type))
547 * Answers true if the sequence has zero start and end position
551 public boolean isNonPositional()
553 return begin == 0 && end == 0;