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 ENA location designed not to conflict with real GFF data
43 private static final String LOCATION = "!Location";
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;
62 * a map of key-value pairs; may be populated from GFF 'column 9' data,
63 * other data sources (e.g. GenBank file), or programmatically
65 public Map<String, Object> otherDetails;
67 public Vector<String> links;
69 // Feature group can be set from a features file
70 // as a group of features between STARTGROUP and ENDGROUP markers
71 public String featureGroup;
73 public SequenceFeature()
78 * Constructs a duplicate feature. Note: Uses makes a shallow copy of the
79 * otherDetails map, so the new and original SequenceFeature may reference the
80 * same objects in the map.
84 public SequenceFeature(SequenceFeature cpy)
93 type = new String(cpy.type);
95 if (cpy.description != null)
97 description = new String(cpy.description);
99 if (cpy.featureGroup != null)
101 featureGroup = new String(cpy.featureGroup);
103 if (cpy.otherDetails != null)
107 otherDetails = (Map<String, Object>) ((HashMap<String, Object>) cpy.otherDetails)
109 } catch (Exception e)
114 if (cpy.links != null && cpy.links.size() > 0)
116 links = new Vector<String>();
117 for (int i = 0, iSize = cpy.links.size(); i < iSize; i++)
119 links.addElement(cpy.links.elementAt(i));
126 * Constructor including a Status value
133 * @param featureGroup
135 public SequenceFeature(String type, String desc, String status,
136 int begin, int end, String featureGroup)
138 this(type, desc, begin, end, featureGroup);
149 * @param featureGroup
151 SequenceFeature(String type, String desc, int begin, int end,
155 this.description = desc;
158 this.featureGroup = featureGroup;
162 * Constructor including a score value
169 * @param featureGroup
171 public SequenceFeature(String type, String desc, int begin, int end,
172 float score, String featureGroup)
174 this(type, desc, begin, end, featureGroup);
179 * Two features are considered equal if they have the same type, group,
180 * description, start, end, phase, strand, and (if present) 'Name', ID' and
181 * 'Parent' attributes.
183 * Note we need to check Parent to distinguish the same exon occurring in
184 * different transcripts (in Ensembl GFF). This allows assembly of transcript
185 * sequences from their component exon regions.
188 public boolean equals(Object o)
190 return equals(o, false);
194 * Overloaded method allows the equality test to optionally ignore the
195 * 'Parent' attribute of a feature. This supports avoiding adding many
196 * superficially duplicate 'exon' or CDS features to genomic or protein
200 * @param ignoreParent
203 public boolean equals(Object o, boolean ignoreParent)
205 if (o == null || !(o instanceof SequenceFeature))
210 SequenceFeature sf = (SequenceFeature) o;
211 if (begin != sf.begin || end != sf.end || score != sf.score)
216 if (getStrand() != sf.getStrand())
221 if (!(type + description + featureGroup + getPhase()).equals(sf.type
222 + sf.description + sf.featureGroup + sf.getPhase()))
226 if (!equalAttribute(getValue("ID"), sf.getValue("ID")))
230 if (!equalAttribute(getValue("Name"), sf.getValue("Name")))
236 if (!equalAttribute(getValue("Parent"), sf.getValue("Parent")))
245 * Returns true if both values are null, are both non-null and equal
251 protected static boolean equalAttribute(Object att1, Object att2)
253 if (att1 == null && att2 == null)
259 return att1.equals(att2);
261 return att2.equals(att1);
267 * @return DOCUMENT ME!
269 public int getBegin()
274 public void setBegin(int start)
282 * @return DOCUMENT ME!
289 public void setEnd(int end)
297 * @return DOCUMENT ME!
299 public String getType()
304 public void setType(String type)
312 * @return DOCUMENT ME!
314 public String getDescription()
319 public void setDescription(String desc)
324 public String getFeatureGroup()
329 public void setFeatureGroup(String featureGroup)
331 this.featureGroup = featureGroup;
334 public void addLink(String labelLink)
338 links = new Vector<String>();
341 links.insertElementAt(labelLink, 0);
344 public float getScore()
349 public void setScore(float value)
355 * Used for getting values which are not in the basic set. eg STRAND, PHASE
361 public Object getValue(String key)
363 if (otherDetails == null)
369 return otherDetails.get(key);
374 * Returns a property value for the given key if known, else the specified
378 * @param defaultValue
381 public Object getValue(String key, Object defaultValue)
383 Object value = getValue(key);
384 return value == null ? defaultValue : value;
388 * Used for setting values which are not in the basic set. eg STRAND, FRAME
396 public void setValue(String key, Object value)
400 if (otherDetails == null)
402 otherDetails = new HashMap<String, Object>();
405 otherDetails.put(key, value);
410 * The following methods are added to maintain the castor Uniprot mapping file
413 public void setStatus(String status)
415 setValue(STATUS, status);
418 public String getStatus()
420 return (String) getValue(STATUS);
423 public void setAttributes(String attr)
425 setValue(ATTRIBUTES, attr);
428 public String getAttributes()
430 return (String) getValue(ATTRIBUTES);
433 public void setPosition(int pos)
439 public int getPosition()
445 * Return 1 for forward strand ('+' in GFF), -1 for reverse strand ('-' in
446 * GFF), and 0 for unknown or not (validly) specified
450 public int getStrand()
453 if (otherDetails != null)
455 Object str = otherDetails.get(STRAND);
460 else if ("+".equals(str))
469 * Set the value of strand
472 * should be "+" for forward, or "-" for reverse
474 public void setStrand(String strand)
476 setValue(STRAND, strand);
479 public void setPhase(String phase)
481 setValue(PHASE, phase);
484 public String getPhase()
486 return (String) getValue(PHASE);
490 * Sets the 'raw' ENA format location specifier e.g. join(12..45,89..121)
494 public void setEnaLocation(String loc)
496 setValue(LOCATION, loc);
500 * Gets the 'raw' ENA format location specifier e.g. join(12..45,89..121)
504 public String getEnaLocation()
506 return (String) getValue(LOCATION);
510 * Readable representation, for debug only, not guaranteed not to change
514 public String toString()
516 return String.format("%d %d %s %s", getBegin(), getEnd(), getType(),
521 * Overridden to ensure that whenever two objects are equal, they have the
525 public int hashCode()
527 String s = getType() + getDescription() + getFeatureGroup()
528 + getValue("ID") + getValue("Name") + getValue("Parent")
530 return s.hashCode() + getBegin() + getEnd() + (int) getScore()