JAL-1705 derive transcript sequences from gene sequence/GFF; handle CDS
[jalview.git] / src / jalview / datamodel / SequenceFeature.java
index 6fed6ee..252f46c 100755 (executable)
@@ -1,23 +1,28 @@
 /*
- * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
- * Copyright (C) 2011 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
  * 
  * This file is part of Jalview.
  * 
  * Jalview is free software: you can redistribute it and/or
  * modify it under the terms of the GNU General Public License 
- * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
- * 
+ * as published by the Free Software Foundation, either version 3
+ * of the License, or (at your option) any later version.
+ *  
  * Jalview is distributed in the hope that it will be useful, but 
  * WITHOUT ANY WARRANTY; without even the implied warranty 
  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
  * PURPOSE.  See the GNU General Public License for more details.
  * 
- * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
+ * You should have received a copy of the GNU General Public License
+ * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
+ * The Jalview Authors are detailed in the 'AUTHORS' file.
  */
 package jalview.datamodel;
 
-import java.util.*;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Vector;
 
 /**
  * DOCUMENT ME!
@@ -27,6 +32,15 @@ import java.util.*;
  */
 public class SequenceFeature
 {
+  private static final String STATUS = "status";
+
+  private static final String STRAND = "STRAND";
+
+  // private key for Phase designed not to conflict with real GFF data
+  private static final String PHASE = "!Phase";
+
+  private static final String ATTRIBUTES = "ATTRIBUTES";
+
   public int begin;
 
   public int end;
@@ -37,9 +51,9 @@ public class SequenceFeature
 
   public String description;
 
-  public Hashtable otherDetails;
+  public Map<String, Object> otherDetails;
 
-  public java.util.Vector links;
+  public Vector<String> links;
 
   // Feature group can be set from a features file
   // as a group of features between STARTGROUP and ENDGROUP markers
@@ -50,9 +64,9 @@ public class SequenceFeature
   }
 
   /**
-   * Constructs a duplicate feature. Note: Uses clone on the otherDetails so
-   * only shallow copies are made of additional properties and method will
-   * silently fail if unclonable objects are found in the hash.
+   * Constructs a duplicate feature. Note: Uses makes a shallow copy of the
+   * otherDetails map, so the new and original SequenceFeature may reference the
+   * same objects in the map.
    * 
    * @param cpy
    */
@@ -79,15 +93,16 @@ public class SequenceFeature
       {
         try
         {
-          otherDetails = (Hashtable) cpy.otherDetails.clone();
+          otherDetails = (Map<String, Object>) ((HashMap<String, Object>) cpy.otherDetails)
+                  .clone();
         } catch (Exception e)
         {
-          // Uncloneable objects in the otherDetails - don't complain
+          // ignore
         }
       }
       if (cpy.links != null && cpy.links.size() > 0)
       {
-        links = new Vector();
+        links = new Vector<String>();
         for (int i = 0, iSize = cpy.links.size(); i < iSize; i++)
         {
           links.addElement(cpy.links.elementAt(i));
@@ -101,7 +116,7 @@ public class SequenceFeature
   {
     this.type = type;
     this.description = desc;
-    setValue("status", status);
+    setValue(STATUS, status);
     this.begin = begin;
     this.end = end;
     this.featureGroup = featureGroup;
@@ -208,7 +223,7 @@ public class SequenceFeature
   {
     if (links == null)
     {
-      links = new java.util.Vector();
+      links = new Vector<String>();
     }
 
     links.insertElementAt(labelLink, 0);
@@ -225,7 +240,7 @@ public class SequenceFeature
   }
 
   /**
-   * Used for getting values which are not in the basic set. eg STRAND, FRAME
+   * Used for getting values which are not in the basic set. eg STRAND, PHASE
    * for GFF file
    * 
    * @param key
@@ -244,6 +259,20 @@ public class SequenceFeature
   }
 
   /**
+   * Returns a property value for the given key if known, else the specified
+   * default value
+   * 
+   * @param key
+   * @param defaultValue
+   * @return
+   */
+  public Object getValue(String key, Object defaultValue)
+  {
+    Object value = getValue(key);
+    return value == null ? defaultValue : value;
+  }
+
+  /**
    * Used for setting values which are not in the basic set. eg STRAND, FRAME
    * for GFF file
    * 
@@ -258,7 +287,7 @@ public class SequenceFeature
     {
       if (otherDetails == null)
       {
-        otherDetails = new Hashtable();
+        otherDetails = new HashMap<String, Object>();
       }
 
       otherDetails.put(key, value);
@@ -271,18 +300,22 @@ public class SequenceFeature
    */
   public void setStatus(String status)
   {
-    setValue("status", status);
+    setValue(STATUS, status);
   }
 
   public String getStatus()
   {
-    if (otherDetails != null)
-    {
-      String stat = (String) otherDetails.get("status");
-      if (stat != null)
-        return new String(stat);
-    }
-    return null;
+    return (String) getValue(STATUS);
+  }
+
+  public void setAttributes(String attr)
+  {
+    setValue(ATTRIBUTES, attr);
+  }
+
+  public String getAttributes()
+  {
+    return (String) getValue(ATTRIBUTES);
   }
 
   public void setPosition(int pos)
@@ -296,4 +329,53 @@ public class SequenceFeature
     return begin;
   }
 
+  /**
+   * Return 1 for forward strand ('+' in GFF), -1 for reverse strand ('-' in
+   * GFF), and 0 for unknown or not (validly) specified
+   * 
+   * @return
+   */
+  public int getStrand()
+  {
+    int strand = 0;
+    if (otherDetails != null)
+    {
+      Object str = otherDetails.get(STRAND);
+      if ("-".equals(str))
+      {
+        strand = -1;
+      }
+      else if ("+".equals(str))
+      {
+        strand = 1;
+      }
+    }
+    return strand;
+  }
+
+  public void setStrand(String strand)
+  {
+    setValue(STRAND, strand);
+  }
+
+  public void setPhase(String phase)
+  {
+    setValue(PHASE, phase);
+  }
+
+  public String getPhase()
+  {
+    return (String) getValue(PHASE);
+  }
+
+  /**
+   * Readable representation, for debug only, not guaranteed not to change
+   * between versions
+   */
+  @Override
+  public String toString()
+  {
+    return String.format("%d %d %s %s", getBegin(), getEnd(), getType(),
+            getDescription());
+  }
 }