JAL-2490 improved lookup of features for Export Features (Jalview
[jalview.git] / src / jalview / io / FeaturesFile.java
index 22b0601..028b14f 100755 (executable)
@@ -23,6 +23,7 @@ package jalview.io;
 import jalview.analysis.AlignmentUtils;
 import jalview.analysis.SequenceIdMatcher;
 import jalview.api.AlignViewportI;
+import jalview.api.FeatureColourI;
 import jalview.api.FeaturesSourceI;
 import jalview.datamodel.AlignedCodonFrame;
 import jalview.datamodel.Alignment;
@@ -33,10 +34,8 @@ import jalview.datamodel.SequenceI;
 import jalview.io.gff.GffHelperBase;
 import jalview.io.gff.GffHelperFactory;
 import jalview.io.gff.GffHelperI;
-import jalview.schemes.AnnotationColourGradient;
-import jalview.schemes.GraduatedColor;
-import jalview.schemes.UserColourScheme;
-import jalview.util.Format;
+import jalview.schemes.FeatureColour;
+import jalview.util.ColorUtils;
 import jalview.util.MapList;
 import jalview.util.ParseHtmlBodyAndLinks;
 import jalview.util.StringUtils;
@@ -45,12 +44,14 @@ import java.awt.Color;
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Collections;
+import java.util.Comparator;
 import java.util.HashMap;
-import java.util.Iterator;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
-import java.util.StringTokenizer;
+import java.util.Set;
 
 /**
  * Parses and writes features files, which may be in Jalview, GFF2 or GFF3
@@ -74,12 +75,23 @@ public class FeaturesFile extends AlignFile implements FeaturesSourceI
 
   private static final String NOTE = "Note";
 
-  protected static final String FRAME = "FRAME";
-
   protected static final String TAB = "\t";
 
   protected static final String GFF_VERSION = "##gff-version";
 
+  private static final Comparator<String> SORT_NULL_LAST = new Comparator<String>()
+  {
+    @Override
+    public int compare(String o1, String o2)
+    {
+      if (o1 == null)
+      {
+        return o2 == null ? 0 : 1;
+      }
+      return (o2 == null ? -1 : o1.compareTo(o2));
+    }
+  };
+
   private AlignmentI lastmatchedAl = null;
 
   private SequenceIdMatcher matcher = null;
@@ -99,12 +111,13 @@ public class FeaturesFile extends AlignFile implements FeaturesSourceI
    * Constructor which does not parse the file immediately
    * 
    * @param inFile
-   * @param type
+   * @param paste
    * @throws IOException
    */
-  public FeaturesFile(String inFile, String type) throws IOException
+  public FeaturesFile(String inFile, DataSourceType paste)
+          throws IOException
   {
-    super(false, inFile, type);
+    super(false, inFile, paste);
   }
 
   /**
@@ -124,7 +137,8 @@ public class FeaturesFile extends AlignFile implements FeaturesSourceI
    * @param type
    * @throws IOException
    */
-  public FeaturesFile(boolean parseImmediately, String inFile, String type)
+  public FeaturesFile(boolean parseImmediately, String inFile,
+          DataSourceType type)
           throws IOException
   {
     super(parseImmediately, inFile, type);
@@ -142,8 +156,8 @@ public class FeaturesFile extends AlignFile implements FeaturesSourceI
    *          - process html strings into plain text
    * @return true if features were added
    */
-  public boolean parse(AlignmentI align, Map<String, Object> colours,
-          boolean removeHTML)
+  public boolean parse(AlignmentI align,
+          Map<String, FeatureColourI> colours, boolean removeHTML)
   {
     return parse(align, colours, removeHTML, false);
   }
@@ -179,8 +193,9 @@ public class FeaturesFile extends AlignFile implements FeaturesSourceI
    *          - when true, ID matches to compound sequence IDs are allowed
    * @return true if features were added
    */
-  public boolean parse(AlignmentI align, Map<String, Object> colours,
-          boolean removeHTML, boolean relaxedIdmatching)
+  public boolean parse(AlignmentI align,
+          Map<String, FeatureColourI> colours, boolean removeHTML,
+          boolean relaxedIdmatching)
   {
     Map<String, String> gffProps = new HashMap<String, String>();
     /*
@@ -234,12 +249,18 @@ public class FeaturesFile extends AlignFile implements FeaturesSourceI
           else if (ft.equalsIgnoreCase("endgroup"))
           {
             // We should check whether this is the current group,
-            // but at present theres no way of showing more than 1 group
+            // but at present there's no way of showing more than 1 group
             featureGroup = null;
           }
           else
           {
-            parseFeatureColour(line, ft, gffColumns, colours);
+            String colscheme = gffColumns[1];
+            FeatureColourI colour = FeatureColour
+                    .parseJalviewFeatureColour(colscheme);
+            if (colour != null)
+            {
+              colours.put(ft, colour);
+            }
           }
           continue;
         }
@@ -271,6 +292,17 @@ public class FeaturesFile extends AlignFile implements FeaturesSourceI
       return false;
     }
 
+    /*
+     * experimental - add any dummy sequences with features to the alignment
+     * - we need them for Ensembl feature extraction - though maybe not otherwise
+     */
+    for (SequenceI newseq : newseqs)
+    {
+      if (newseq.getFeatures().hasFeatures())
+      {
+        align.addSequence(newseq);
+      }
+    }
     return true;
   }
 
@@ -288,7 +320,7 @@ public class FeaturesFile extends AlignFile implements FeaturesSourceI
    * @param featureGroup
    */
   protected boolean parseJalviewFeature(String line, String[] gffColumns,
-          AlignmentI alignment, Map<String, Object> featureColours,
+          AlignmentI alignment, Map<String, FeatureColourI> featureColours,
           boolean removeHTML, boolean relaxedIdMatching, String featureGroup)
   {
     /*
@@ -340,8 +372,8 @@ public class FeaturesFile extends AlignFile implements FeaturesSourceI
        * Perhaps an old style groups file with no colours -
        * synthesize a colour from the feature type
        */
-      UserColourScheme ucs = new UserColourScheme(ft);
-      featureColours.put(ft, ucs.findColour('A'));
+      Color colour = ColorUtils.createColourFromName(ft);
+      featureColours.put(ft, new FeatureColour(colour));
     }
     SequenceFeature sf = new SequenceFeature(ft, desc, "", startPos,
             endPos, featureGroup);
@@ -372,225 +404,6 @@ public class FeaturesFile extends AlignFile implements FeaturesSourceI
   }
 
   /**
-   * Process a feature type colour specification
-   * 
-   * @param line
-   *          the current input line (for error messages only)
-   * @param featureType
-   *          the first token on the line
-   * @param gffColumns
-   *          holds tokens on the line
-   * @param colours
-   *          map to which to add derived colour specification
-   */
-  protected void parseFeatureColour(String line, String featureType,
-          String[] gffColumns, Map<String, Object> colours)
-  {
-    Object colour = null;
-    String colscheme = gffColumns[1];
-    if (colscheme.indexOf("|") > -1
-            || colscheme.trim().equalsIgnoreCase("label"))
-    {
-      colour = parseGraduatedColourScheme(line, colscheme);
-    }
-    else
-    {
-      UserColourScheme ucs = new UserColourScheme(colscheme);
-      colour = ucs.findColour('A');
-    }
-    if (colour != null)
-    {
-      colours.put(featureType, colour);
-    }
-  }
-
-  /**
-   * Parse a Jalview graduated colour descriptor
-   * 
-   * @param line
-   * @param colourDescriptor
-   * @return
-   */
-  protected GraduatedColor parseGraduatedColourScheme(String line,
-          String colourDescriptor)
-  {
-    // Parse '|' separated graduated colourscheme fields:
-    // [label|][mincolour|maxcolour|[absolute|]minvalue|maxvalue|thresholdtype|thresholdvalue]
-    // can either provide 'label' only, first is optional, next two
-    // colors are required (but may be
-    // left blank), next is optional, nxt two min/max are required.
-    // first is either 'label'
-    // first/second and third are both hexadecimal or word equivalent
-    // colour.
-    // next two are values parsed as floats.
-    // fifth is either 'above','below', or 'none'.
-    // sixth is a float value and only required when fifth is either
-    // 'above' or 'below'.
-    StringTokenizer gcol = new StringTokenizer(colourDescriptor, "|", true);
-    // set defaults
-    float min = Float.MIN_VALUE, max = Float.MAX_VALUE;
-    boolean labelCol = false;
-    // Parse spec line
-    String mincol = gcol.nextToken();
-    if (mincol == "|")
-    {
-      System.err
-              .println("Expected either 'label' or a colour specification in the line: "
-                      + line);
-      return null;
-    }
-    String maxcol = null;
-    if (mincol.toLowerCase().indexOf("label") == 0)
-    {
-      labelCol = true;
-      mincol = (gcol.hasMoreTokens() ? gcol.nextToken() : null); // skip '|'
-      mincol = (gcol.hasMoreTokens() ? gcol.nextToken() : null);
-    }
-    String abso = null, minval, maxval;
-    if (mincol != null)
-    {
-      // at least four more tokens
-      if (mincol.equals("|"))
-      {
-        mincol = "";
-      }
-      else
-      {
-        gcol.nextToken(); // skip next '|'
-      }
-      // continue parsing rest of line
-      maxcol = gcol.nextToken();
-      if (maxcol.equals("|"))
-      {
-        maxcol = "";
-      }
-      else
-      {
-        gcol.nextToken(); // skip next '|'
-      }
-      abso = gcol.nextToken();
-      gcol.nextToken(); // skip next '|'
-      if (abso.toLowerCase().indexOf("abso") != 0)
-      {
-        minval = abso;
-        abso = null;
-      }
-      else
-      {
-        minval = gcol.nextToken();
-        gcol.nextToken(); // skip next '|'
-      }
-      maxval = gcol.nextToken();
-      if (gcol.hasMoreTokens())
-      {
-        gcol.nextToken(); // skip next '|'
-      }
-      try
-      {
-        if (minval.length() > 0)
-        {
-          min = Float.valueOf(minval);
-        }
-      } catch (Exception e)
-      {
-        System.err
-                .println("Couldn't parse the minimum value for graduated colour for type ("
-                        + colourDescriptor
-                        + ") - did you misspell 'auto' for the optional automatic colour switch ?");
-        e.printStackTrace();
-      }
-      try
-      {
-        if (maxval.length() > 0)
-        {
-          max = Float.valueOf(maxval);
-        }
-      } catch (Exception e)
-      {
-        System.err
-                .println("Couldn't parse the maximum value for graduated colour for type ("
-                        + colourDescriptor + ")");
-        e.printStackTrace();
-      }
-    }
-    else
-    {
-      // add in some dummy min/max colours for the label-only
-      // colourscheme.
-      mincol = "FFFFFF";
-      maxcol = "000000";
-    }
-
-    GraduatedColor colour = null;
-    try
-    {
-      colour = new GraduatedColor(
-              new UserColourScheme(mincol).findColour('A'),
-              new UserColourScheme(maxcol).findColour('A'), min, max);
-    } catch (Exception e)
-    {
-      System.err.println("Couldn't parse the graduated colour scheme ("
-              + colourDescriptor + ")");
-      e.printStackTrace();
-    }
-    if (colour != null)
-    {
-      colour.setColourByLabel(labelCol);
-      colour.setAutoScaled(abso == null);
-      // add in any additional parameters
-      String ttype = null, tval = null;
-      if (gcol.hasMoreTokens())
-      {
-        // threshold type and possibly a threshold value
-        ttype = gcol.nextToken();
-        if (ttype.toLowerCase().startsWith("below"))
-        {
-          colour.setThreshType(AnnotationColourGradient.BELOW_THRESHOLD);
-        }
-        else if (ttype.toLowerCase().startsWith("above"))
-        {
-          colour.setThreshType(AnnotationColourGradient.ABOVE_THRESHOLD);
-        }
-        else
-        {
-          colour.setThreshType(AnnotationColourGradient.NO_THRESHOLD);
-          if (!ttype.toLowerCase().startsWith("no"))
-          {
-            System.err.println("Ignoring unrecognised threshold type : "
-                    + ttype);
-          }
-        }
-      }
-      if (colour.getThreshType() != AnnotationColourGradient.NO_THRESHOLD)
-      {
-        try
-        {
-          gcol.nextToken();
-          tval = gcol.nextToken();
-          colour.setThresh(new Float(tval).floatValue());
-        } catch (Exception e)
-        {
-          System.err.println("Couldn't parse threshold value as a float: ("
-                  + tval + ")");
-          e.printStackTrace();
-        }
-      }
-      // parse the thresh-is-min token ?
-      if (gcol.hasMoreTokens())
-      {
-        System.err
-                .println("Ignoring additional tokens in parameters in graduated colour specification\n");
-        while (gcol.hasMoreTokens())
-        {
-          System.err.println("|" + gcol.nextToken());
-        }
-        System.err.println("\n");
-      }
-    }
-    return colour;
-  }
-
-  /**
    * clear any temporary handles used to speed up ID matching
    */
   protected void resetMatcher()
@@ -675,8 +488,11 @@ public class FeaturesFile extends AlignFile implements FeaturesSourceI
     ParseHtmlBodyAndLinks parsed = new ParseHtmlBodyAndLinks(
             sf.getDescription(), removeHTML, newline);
 
-    sf.description = (removeHTML) ? parsed.getNonHtmlContent()
-            : sf.description;
+    if (removeHTML)
+    {
+      sf.setDescription(parsed.getNonHtmlContent());
+    }
+
     for (String link : parsed.getLinks())
     {
       sf.addLink(link);
@@ -694,7 +510,7 @@ public class FeaturesFile extends AlignFile implements FeaturesSourceI
    * @return features file contents
    */
   public String printJalviewFormat(SequenceI[] sequences,
-          Map<String, Object> visible)
+          Map<String, FeatureColourI> visible)
   {
     return printJalviewFormat(sequences, visible, true, true);
   }
@@ -714,200 +530,131 @@ public class FeaturesFile extends AlignFile implements FeaturesSourceI
    * @return features file contents
    */
   public String printJalviewFormat(SequenceI[] sequences,
-          Map<String, Object> visible, boolean visOnly, boolean nonpos)
+          Map<String, FeatureColourI> visible, boolean visOnly,
+          boolean nonpos)
   {
-    StringBuilder out = new StringBuilder(256);
-    boolean featuresGen = false;
-    if (visOnly && !nonpos && (visible == null || visible.size() < 1))
+    if (visOnly && !nonpos && (visible == null || visible.isEmpty()))
     {
       // no point continuing.
       return "No Features Visible";
     }
 
-    if (visible != null && visOnly)
+    /*
+     * write out feature colours (if we know them)
+     */
+    // TODO: decide if feature links should also be written here ?
+    StringBuilder out = new StringBuilder(256);
+    if (visible != null && visOnly) // todo why visOnly test?
     {
-      // write feature colours only if we're given them and we are generating
-      // viewed features
-      // TODO: decide if feature links should also be written here ?
-      Iterator<String> en = visible.keySet().iterator();
-      String featureType, color;
-      while (en.hasNext())
+      for (Entry<String, FeatureColourI> featureColour : visible.entrySet())
       {
-        featureType = en.next().toString();
-
-        if (visible.get(featureType) instanceof GraduatedColor)
-        {
-          GraduatedColor gc = (GraduatedColor) visible.get(featureType);
-          color = (gc.isColourByLabel() ? "label|" : "")
-                  + Format.getHexString(gc.getMinColor()) + "|"
-                  + Format.getHexString(gc.getMaxColor())
-                  + (gc.isAutoScale() ? "|" : "|abso|") + gc.getMin() + "|"
-                  + gc.getMax() + "|";
-          if (gc.getThreshType() != AnnotationColourGradient.NO_THRESHOLD)
-          {
-            if (gc.getThreshType() == AnnotationColourGradient.BELOW_THRESHOLD)
-            {
-              color += "below";
-            }
-            else
-            {
-              if (gc.getThreshType() != AnnotationColourGradient.ABOVE_THRESHOLD)
-              {
-                System.err.println("WARNING: Unsupported threshold type ("
-                        + gc.getThreshType() + ") : Assuming 'above'");
-              }
-              color += "above";
-            }
-            // add the value
-            color += "|" + gc.getThresh();
-          }
-          else
-          {
-            color += "none";
-          }
-        }
-        else if (visible.get(featureType) instanceof Color)
-        {
-          color = Format.getHexString((Color) visible.get(featureType));
-        }
-        else
-        {
-          // legacy support for integer objects containing colour triplet values
-          color = Format.getHexString(new Color(Integer.parseInt(visible
-                  .get(featureType).toString())));
-        }
-        out.append(featureType);
-        out.append(TAB);
-        out.append(color);
-        out.append(newline);
+        FeatureColourI colour = featureColour.getValue();
+        out.append(colour.toJalviewFormat(featureColour.getKey())).append(
+                newline);
       }
     }
+
     // Work out which groups are both present and visible
-    List<String> groups = new ArrayList<String>();
-    int groupIndex = 0;
-    boolean isnonpos = false;
+    Set<String> groups = new HashSet<String>();
+    String[] types = visible == null ? null : visible.keySet().toArray(
+            new String[visible.keySet().size()]);
 
-    SequenceFeature[] features;
     for (int i = 0; i < sequences.length; i++)
     {
-      features = sequences[i].getSequenceFeatures();
-      if (features != null)
+      groups.addAll(sequences[i].getFeatures()
+              .getFeatureGroups(true, types));
+      if (nonpos)
       {
-        for (int j = 0; j < features.length; j++)
-        {
-          isnonpos = features[j].begin == 0 && features[j].end == 0;
-          if ((!nonpos && isnonpos)
-                  || (!isnonpos && visOnly && !visible
-                          .containsKey(features[j].type)))
-          {
-            continue;
-          }
-
-          if (features[j].featureGroup != null
-                  && !groups.contains(features[j].featureGroup))
-          {
-            groups.add(features[j].featureGroup);
-          }
-        }
+        groups.addAll(sequences[i].getFeatures().getFeatureGroups(false,
+                types));
       }
     }
 
-    String group = null;
-    do
+    /*
+     * sort distinct groups so null group is output last
+     */
+    List<String> sortedGroups = new ArrayList<String>(groups);
+    Collections.sort(sortedGroups, SORT_NULL_LAST);
+
+    // TODO check where null group should be output
+    boolean foundSome = false;
+    for (String group : sortedGroups)
     {
-      if (groups.size() > 0 && groupIndex < groups.size())
+      if (group != null)
       {
-        group = groups.get(groupIndex);
         out.append(newline);
         out.append("STARTGROUP").append(TAB);
         out.append(group);
         out.append(newline);
       }
-      else
-      {
-        group = null;
-      }
 
+      /*
+       * output features within groups (non-positional first if wanted)
+       */
       for (int i = 0; i < sequences.length; i++)
       {
-        features = sequences[i].getSequenceFeatures();
-        if (features != null)
+        List<SequenceFeature> features = new ArrayList<SequenceFeature>();
+        if (nonpos)
         {
-          for (int j = 0; j < features.length; j++)
-          {
-            isnonpos = features[j].begin == 0 && features[j].end == 0;
-            if ((!nonpos && isnonpos)
-                    || (!isnonpos && visOnly && !visible
-                            .containsKey(features[j].type)))
-            {
-              // skip if feature is nonpos and we ignore them or if we only
-              // output visible and it isn't non-pos and it's not visible
-              continue;
-            }
+          features.addAll(sequences[i].getFeatures().getFeaturesForGroup(
+                  false, group, types));
+        }
+        features.addAll(sequences[i].getFeatures().getFeaturesForGroup(
+                true, group, types));
 
-            if (group != null
-                    && (features[j].featureGroup == null || !features[j].featureGroup
-                            .equals(group)))
+        for (SequenceFeature sequenceFeature : features)
+        {
+          // we have features to output
+          foundSome = true;
+          if (sequenceFeature.description == null
+                  || sequenceFeature.description.equals(""))
+          {
+            out.append(sequenceFeature.type).append(TAB);
+          }
+          else
+          {
+            if (sequenceFeature.links != null
+                    && sequenceFeature.getDescription().indexOf("<html>") == -1)
             {
-              continue;
+              out.append("<html>");
             }
 
-            if (group == null && features[j].featureGroup != null)
+            out.append(sequenceFeature.description);
+            if (sequenceFeature.links != null)
             {
-              continue;
-            }
-            // we have features to output
-            featuresGen = true;
-            if (features[j].description == null
-                    || features[j].description.equals(""))
-            {
-              out.append(features[j].type).append(TAB);
-            }
-            else
-            {
-              if (features[j].links != null
-                      && features[j].getDescription().indexOf("<html>") == -1)
+              for (int l = 0; l < sequenceFeature.links.size(); l++)
               {
-                out.append("<html>");
-              }
+                String label = sequenceFeature.links.elementAt(l);
+                String href = label.substring(label.indexOf("|") + 1);
+                label = label.substring(0, label.indexOf("|"));
 
-              out.append(features[j].description + " ");
-              if (features[j].links != null)
-              {
-                for (int l = 0; l < features[j].links.size(); l++)
+                if (sequenceFeature.description.indexOf(href) == -1)
                 {
-                  String label = features[j].links.elementAt(l).toString();
-                  String href = label.substring(label.indexOf("|") + 1);
-                  label = label.substring(0, label.indexOf("|"));
-
-                  if (features[j].description.indexOf(href) == -1)
-                  {
-                    out.append("<a href=\"" + href + "\">" + label + "</a>");
-                  }
-                }
-
-                if (features[j].getDescription().indexOf("</html>") == -1)
-                {
-                  out.append("</html>");
+                  out.append(" <a href=\"" + href + "\">" + label + "</a>");
                 }
               }
 
-              out.append(TAB);
+              if (sequenceFeature.getDescription().indexOf("</html>") == -1)
+              {
+                out.append("</html>");
+              }
             }
-            out.append(sequences[i].getName());
-            out.append("\t-1\t");
-            out.append(features[j].begin);
+
             out.append(TAB);
-            out.append(features[j].end);
+          }
+          out.append(sequences[i].getName());
+          out.append("\t-1\t");
+          out.append(sequenceFeature.begin);
+          out.append(TAB);
+          out.append(sequenceFeature.end);
+          out.append(TAB);
+          out.append(sequenceFeature.type);
+          if (!Float.isNaN(sequenceFeature.score))
+          {
             out.append(TAB);
-            out.append(features[j].type);
-            if (!Float.isNaN(features[j].score))
-            {
-              out.append(TAB);
-              out.append(features[j].score);
-            }
-            out.append(newline);
+            out.append(sequenceFeature.score);
           }
+          out.append(newline);
         }
       }
 
@@ -916,21 +663,10 @@ public class FeaturesFile extends AlignFile implements FeaturesSourceI
         out.append("ENDGROUP").append(TAB);
         out.append(group);
         out.append(newline);
-        groupIndex++;
-      }
-      else
-      {
-        break;
       }
-
-    } while (groupIndex < groups.size() + 1);
-
-    if (!featuresGen)
-    {
-      return "No Features Visible";
     }
 
-    return out.toString();
+    return foundSome ? out.toString() : "No Features Visible";
   }
 
   /**
@@ -957,7 +693,8 @@ public class FeaturesFile extends AlignFile implements FeaturesSourceI
       dataset = new Alignment(new SequenceI[] {});
     }
 
-    boolean parseResult = parse(dataset, null, false, true);
+    Map<String, FeatureColourI> featureColours = new HashMap<String, FeatureColourI>();
+    boolean parseResult = parse(dataset, featureColours, false, true);
     if (!parseResult)
     {
       // pass error up somehow
@@ -978,9 +715,10 @@ public class FeaturesFile extends AlignFile implements FeaturesSourceI
    * @return error message
    */
   @Override
-  public String print()
+  public String print(SequenceI[] sqs, boolean jvsuffix)
   {
-    return "Use printGffFormat() or printJalviewFormat()";
+    System.out.println("Use printGffFormat() or printJalviewFormat()");
+    return null;
   }
 
   /**
@@ -994,7 +732,7 @@ public class FeaturesFile extends AlignFile implements FeaturesSourceI
    * @return
    */
   public String printGffFormat(SequenceI[] sequences,
-          Map<String, Object> visible)
+          Map<String, FeatureColourI> visible)
   {
     return printGffFormat(sequences, visible, true, true);
   }
@@ -1011,11 +749,12 @@ public class FeaturesFile extends AlignFile implements FeaturesSourceI
    * @return
    */
   public String printGffFormat(SequenceI[] sequences,
-          Map<String, Object> visible, boolean outputVisibleOnly,
+          Map<String, FeatureColourI> visible, boolean outputVisibleOnly,
           boolean includeNonPositionalFeatures)
   {
     StringBuilder out = new StringBuilder(256);
-    out.append(String.format("%s %d\n", GFF_VERSION, gffVersion));
+    int version = gffVersion == 0 ? 2 : gffVersion;
+    out.append(String.format("%s %d\n", GFF_VERSION, version));
     String source;
     boolean isnonpos;
     for (SequenceI seq : sequences)
@@ -1067,7 +806,8 @@ public class FeaturesFile extends AlignFile implements FeaturesSourceI
           out.append(strand == 1 ? "+" : (strand == -1 ? "-" : "."));
           out.append(TAB);
 
-          out.append(sf.getValue(FRAME, "."));
+          String phase = sf.getPhase();
+          out.append(phase == null ? "." : phase);
 
           // miscellaneous key-values (GFF column 9)
           String attributes = sf.getAttributes();
@@ -1319,6 +1059,11 @@ public class FeaturesFile extends AlignFile implements FeaturesSourceI
          * replace parsed sequence with the realised forward reference
          */
         includedseqs.set(p, dummyseq);
+
+        /*
+         * and remove from the newseqs list
+         */
+        newseqs.remove(dummyseq);
       }
     }