JAL-1499 parse files with position number comments like [24]
[jalview.git] / src / jalview / io / MegaFile.java
index 2f5e35a..1126cff 100644 (file)
@@ -252,8 +252,10 @@ public class MegaFile extends AlignFile
   }
 
   /**
-   * Returns the next line that is not a comment, or null at end of file.
-   * Comments in MEGA are within [ ] brackets, and may be nested.
+   * Returns the next non-comment line (or part line), or null at end of file.
+   * Comments in MEGA are within [ ] brackets, and may be nested. They may occur
+   * anywhere within a line (for example at the end with position numbers); this
+   * method returns the line with any comments removed.
    * 
    * @param depth
    *          current depth of nesting of comments while parsing
@@ -272,16 +274,6 @@ public class MegaFile extends AlignFile
       }
       return data;
     }
-    int leftBracket = data.indexOf(COMMENT_START);
-
-    /*
-     * reject unnested comment following data on the same line
-     */
-    if (depth == 0 && leftBracket > 0)
-    {
-      throw new FileFormatException(
-              "Can't parse comment following data at " + data);
-    }
 
     /*
      * If we are in a (possibly nested) comment after parsing this line, keep
@@ -295,15 +287,10 @@ public class MegaFile extends AlignFile
     else
     {
       /*
-       * not in a comment by end of this line; return what is left (or the next
-       * line if that is empty)
+       * not in a comment by end of this line; return what is left
        */
       String nonCommentPart = getNonCommentContent(data, depth);
-      // if (nonCommentPart.length() > 0)
-      // {
-        return nonCommentPart;
-      // }
-      // return nextNonCommentLine(0);
+      return nonCommentPart;
     }
   }
 
@@ -1215,10 +1202,19 @@ public class MegaFile extends AlignFile
   public String print(AlignmentI al)
   {
     this.nucleotide = al.isNucleotide();
+
     String lineLength = (String) al.getProperty(PROP_LINELENGTH);
     this.positionsPerLine = lineLength == null ? DEFAULT_LINE_LENGTH : Integer
             .parseInt(lineLength);
-    return printHeaders(al) + print(al.getSequencesArray());
+
+    String interleave = (String) al.getProperty(PROP_INTERLEAVED);
+    if (interleave != null)
+    {
+      this.interleaved = Boolean.valueOf(interleave);
+    }
+
+    String headers = printHeaders(al);
+    return headers + print(al.getSequencesArray());
   }
 
   /**