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 boolean sameScore = Float.isNaN(score) ? Float.isNaN(sf.score)
213 if (begin != sf.begin || end != sf.end || !sameScore)
218 if (getStrand() != sf.getStrand())
223 if (!(type + description + featureGroup + getPhase()).equals(sf.type
224 + sf.description + sf.featureGroup + sf.getPhase()))
228 if (!equalAttribute(getValue("ID"), sf.getValue("ID")))
232 if (!equalAttribute(getValue("Name"), sf.getValue("Name")))
238 if (!equalAttribute(getValue("Parent"), sf.getValue("Parent")))
247 * Returns true if both values are null, are both non-null and equal
253 protected static boolean equalAttribute(Object att1, Object att2)
255 if (att1 == null && att2 == null)
261 return att1.equals(att2);
263 return att2.equals(att1);
269 * @return DOCUMENT ME!
271 public int getBegin()
276 public void setBegin(int start)
284 * @return DOCUMENT ME!
291 public void setEnd(int end)
299 * @return DOCUMENT ME!
301 public String getType()
306 public void setType(String type)
314 * @return DOCUMENT ME!
316 public String getDescription()
321 public void setDescription(String desc)
326 public String getFeatureGroup()
331 public void setFeatureGroup(String featureGroup)
333 this.featureGroup = featureGroup;
336 public void addLink(String labelLink)
340 links = new Vector<String>();
343 links.insertElementAt(labelLink, 0);
346 public float getScore()
351 public void setScore(float value)
357 * Used for getting values which are not in the basic set. eg STRAND, PHASE
363 public Object getValue(String key)
365 if (otherDetails == null)
371 return otherDetails.get(key);
376 * Returns a property value for the given key if known, else the specified
380 * @param defaultValue
383 public Object getValue(String key, Object defaultValue)
385 Object value = getValue(key);
386 return value == null ? defaultValue : value;
390 * Used for setting values which are not in the basic set. eg STRAND, FRAME
398 public void setValue(String key, Object value)
402 if (otherDetails == null)
404 otherDetails = new HashMap<String, Object>();
407 otherDetails.put(key, value);
412 * The following methods are added to maintain the castor Uniprot mapping file
415 public void setStatus(String status)
417 setValue(STATUS, status);
420 public String getStatus()
422 return (String) getValue(STATUS);
425 public void setAttributes(String attr)
427 setValue(ATTRIBUTES, attr);
430 public String getAttributes()
432 return (String) getValue(ATTRIBUTES);
435 public void setPosition(int pos)
441 public int getPosition()
447 * Return 1 for forward strand ('+' in GFF), -1 for reverse strand ('-' in
448 * GFF), and 0 for unknown or not (validly) specified
452 public int getStrand()
455 if (otherDetails != null)
457 Object str = otherDetails.get(STRAND);
462 else if ("+".equals(str))
471 * Set the value of strand
474 * should be "+" for forward, or "-" for reverse
476 public void setStrand(String strand)
478 setValue(STRAND, strand);
481 public void setPhase(String phase)
483 setValue(PHASE, phase);
486 public String getPhase()
488 return (String) getValue(PHASE);
492 * Sets the 'raw' ENA format location specifier e.g. join(12..45,89..121)
496 public void setEnaLocation(String loc)
498 setValue(LOCATION, loc);
502 * Gets the 'raw' ENA format location specifier e.g. join(12..45,89..121)
506 public String getEnaLocation()
508 return (String) getValue(LOCATION);
512 * Readable representation, for debug only, not guaranteed not to change
516 public String toString()
518 return String.format("%d %d %s %s", getBegin(), getEnd(), getType(),
523 * Overridden to ensure that whenever two objects are equal, they have the
527 public int hashCode()
529 String s = getType() + getDescription() + getFeatureGroup()
530 + getValue("ID") + getValue("Name") + getValue("Parent")
532 return s.hashCode() + getBegin() + getEnd() + (int) getScore()
537 * Answers true if the feature's start/end values represent two related
538 * positions, rather than ends of a range. Such features may be visualised or
539 * reported differently to features on a range.
541 public boolean isContactFeature()
543 // TODO abstract one day to a FeatureType class
544 if ("disulfide bond".equalsIgnoreCase(type)
545 || "disulphide bond".equalsIgnoreCase(type))