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";
61 public String description;
64 * a map of key-value pairs; may be populated from GFF 'column 9' data,
65 * other data sources (e.g. GenBank file), or programmatically
67 public Map<String, Object> otherDetails;
69 public Vector<String> links;
71 // Feature group can be set from a features file
72 // as a group of features between STARTGROUP and ENDGROUP markers
73 public String featureGroup;
75 public SequenceFeature()
80 * Constructs a duplicate feature. Note: Uses makes a shallow copy of the
81 * otherDetails map, so the new and original SequenceFeature may reference the
82 * same objects in the map.
86 public SequenceFeature(SequenceFeature cpy)
95 type = new String(cpy.type);
97 if (cpy.description != null)
99 description = new String(cpy.description);
101 if (cpy.featureGroup != null)
103 featureGroup = new String(cpy.featureGroup);
105 if (cpy.otherDetails != null)
109 otherDetails = (Map<String, Object>) ((HashMap<String, Object>) cpy.otherDetails)
111 } catch (Exception e)
116 if (cpy.links != null && cpy.links.size() > 0)
118 links = new Vector<String>();
119 for (int i = 0, iSize = cpy.links.size(); i < iSize; i++)
121 links.addElement(cpy.links.elementAt(i));
128 * Constructor including a Status value
135 * @param featureGroup
137 public SequenceFeature(String type, String desc, String status,
138 int begin, int end, String featureGroup)
140 this(type, desc, begin, end, featureGroup);
151 * @param featureGroup
153 SequenceFeature(String type, String desc, int begin, int end,
157 this.description = desc;
160 this.featureGroup = featureGroup;
164 * Constructor including a score value
171 * @param featureGroup
173 public SequenceFeature(String type, String desc, int begin, int end,
174 float score, String featureGroup)
176 this(type, desc, begin, end, featureGroup);
181 * Two features are considered equal if they have the same type, group,
182 * description, start, end, phase, strand, and (if present) 'Name', ID' and
183 * 'Parent' attributes.
185 * Note we need to check Parent to distinguish the same exon occurring in
186 * different transcripts (in Ensembl GFF). This allows assembly of transcript
187 * sequences from their component exon regions.
190 public boolean equals(Object o)
192 return equals(o, false);
196 * Overloaded method allows the equality test to optionally ignore the
197 * 'Parent' attribute of a feature. This supports avoiding adding many
198 * superficially duplicate 'exon' or CDS features to genomic or protein
202 * @param ignoreParent
205 public boolean equals(Object o, boolean ignoreParent)
207 if (o == null || !(o instanceof SequenceFeature))
212 SequenceFeature sf = (SequenceFeature) o;
213 boolean sameScore = Float.isNaN(score) ? Float.isNaN(sf.score)
215 if (begin != sf.begin || end != sf.end || !sameScore)
220 if (getStrand() != sf.getStrand())
225 if (!(type + description + featureGroup + getPhase()).equals(sf.type
226 + sf.description + sf.featureGroup + sf.getPhase()))
230 if (!equalAttribute(getValue("ID"), sf.getValue("ID")))
234 if (!equalAttribute(getValue("Name"), sf.getValue("Name")))
240 if (!equalAttribute(getValue("Parent"), sf.getValue("Parent")))
249 * Returns true if both values are null, are both non-null and equal
255 protected static boolean equalAttribute(Object att1, Object att2)
257 if (att1 == null && att2 == null)
263 return att1.equals(att2);
265 return att2.equals(att1);
271 * @return DOCUMENT ME!
274 public int getBegin()
279 public void setBegin(int start)
287 * @return DOCUMENT ME!
295 public void setEnd(int end)
303 * @return DOCUMENT ME!
305 public String getType()
310 public void setType(String type)
318 * @return DOCUMENT ME!
320 public String getDescription()
325 public void setDescription(String desc)
330 public String getFeatureGroup()
335 public void setFeatureGroup(String featureGroup)
337 this.featureGroup = featureGroup;
340 public void addLink(String labelLink)
344 links = new Vector<String>();
347 if (!links.contains(labelLink))
349 links.insertElementAt(labelLink, 0);
353 public float getScore()
358 public void setScore(float value)
364 * Used for getting values which are not in the basic set. eg STRAND, PHASE
370 public Object getValue(String key)
372 if (otherDetails == null)
378 return otherDetails.get(key);
383 * Returns a property value for the given key if known, else the specified
387 * @param defaultValue
390 public Object getValue(String key, Object defaultValue)
392 Object value = getValue(key);
393 return value == null ? defaultValue : value;
397 * Used for setting values which are not in the basic set. eg STRAND, FRAME
405 public void setValue(String key, Object value)
409 if (otherDetails == null)
411 otherDetails = new HashMap<String, Object>();
414 otherDetails.put(key, value);
419 * The following methods are added to maintain the castor Uniprot mapping file
422 public void setStatus(String status)
424 setValue(STATUS, status);
427 public String getStatus()
429 return (String) getValue(STATUS);
432 public void setAttributes(String attr)
434 setValue(ATTRIBUTES, attr);
437 public String getAttributes()
439 return (String) getValue(ATTRIBUTES);
442 public void setPosition(int pos)
448 public int getPosition()
454 * Return 1 for forward strand ('+' in GFF), -1 for reverse strand ('-' in
455 * GFF), and 0 for unknown or not (validly) specified
459 public int getStrand()
462 if (otherDetails != null)
464 Object str = otherDetails.get(STRAND);
469 else if ("+".equals(str))
478 * Set the value of strand
481 * should be "+" for forward, or "-" for reverse
483 public void setStrand(String strand)
485 setValue(STRAND, strand);
488 public void setPhase(String phase)
490 setValue(PHASE, phase);
493 public String getPhase()
495 return (String) getValue(PHASE);
499 * Sets the 'raw' ENA format location specifier e.g. join(12..45,89..121)
503 public void setEnaLocation(String loc)
505 setValue(LOCATION, loc);
509 * Gets the 'raw' ENA format location specifier e.g. join(12..45,89..121)
513 public String getEnaLocation()
515 return (String) getValue(LOCATION);
519 * Readable representation, for debug only, not guaranteed not to change
523 public String toString()
525 return String.format("%d %d %s %s", getBegin(), getEnd(), getType(),
530 * Overridden to ensure that whenever two objects are equal, they have the
534 public int hashCode()
536 String s = getType() + getDescription() + getFeatureGroup()
537 + getValue("ID") + getValue("Name") + getValue("Parent")
539 return s.hashCode() + getBegin() + getEnd() + (int) getScore()
544 * Answers true if the feature's start/end values represent two related
545 * positions, rather than ends of a range. Such features may be visualised or
546 * reported differently to features on a range.
549 public boolean isContactFeature()
551 // TODO abstract one day to a FeatureType class
552 if ("disulfide bond".equalsIgnoreCase(type)
553 || "disulphide bond".equalsIgnoreCase(type))
561 * Answers true if the sequence has zero start and end position
565 public boolean isNonPositional()
567 return begin == 0 && end == 0;