JAL-2629 fix issue where Stockholm file would print two terminators
[jalview.git] / src / jalview / io / StockholmFile.java
index 8d340f2..3644ff9 100644 (file)
@@ -163,8 +163,8 @@ public class StockholmFile extends AlignFile
 
       for (int k = 0; k < rna.length(); k++)
       {
-        ann[k] = new Annotation(annot[k], "", Rna.getRNASecStrucState(
-                annot[k]).charAt(0), 0f);
+        ann[k] = new Annotation(annot[k], "",
+                Rna.getRNASecStrucState(annot[k]).charAt(0), 0f);
 
       }
       AlignmentAnnotation align = new AlignmentAnnotation("Sec. str.",
@@ -207,9 +207,8 @@ public class StockholmFile extends AlignFile
     r = new Regex("# STOCKHOLM ([\\d\\.]+)");
     if (!r.search(nextLine()))
     {
-      throw new IOException(
-              MessageManager
-                      .getString("exception.stockholm_invalid_format"));
+      throw new IOException(MessageManager
+              .getString("exception.stockholm_invalid_format"));
     }
     else
     {
@@ -439,8 +438,8 @@ public class StockholmFile extends AlignFile
         {
           // logger.error("Could not parse sequence line: " + line);
           throw new IOException(MessageManager.formatMessage(
-                  "exception.couldnt_parse_sequence_line",
-                  new String[] { line }));
+                  "exception.couldnt_parse_sequence_line", new String[]
+                  { line }));
         }
         String ns = seqs.get(x.stringMatched(1));
         if (ns == null)
@@ -671,8 +670,8 @@ public class StockholmFile extends AlignFile
           // }
           else
           {
-            System.err
-                    .println("Warning - couldn't parse sequence annotation row line:\n"
+            System.err.println(
+                    "Warning - couldn't parse sequence annotation row line:\n"
                             + line);
             // throw new IOException("Error parsing " + line);
           }
@@ -680,8 +679,8 @@ public class StockholmFile extends AlignFile
         else
         {
           throw new IOException(MessageManager.formatMessage(
-                  "exception.unknown_annotation_detected", new String[] {
-                      annType, annContent }));
+                  "exception.unknown_annotation_detected", new String[]
+                  { annType, annContent }));
         }
       }
     }
@@ -794,8 +793,10 @@ public class StockholmFile extends AlignFile
     {
       for (DBRefEntry d : dbrs)
       {
-        jalview.util.MapList mp = new jalview.util.MapList(new int[] {
-            seqO.getStart(), seqO.getEnd() }, new int[] { st, en }, 1, 1);
+        jalview.util.MapList mp = new jalview.util.MapList(
+                new int[]
+                { seqO.getStart(), seqO.getEnd() }, new int[] { st, en }, 1,
+                1);
         jalview.datamodel.Mapping mping = new Mapping(mp);
         d.setMap(mping);
       }
@@ -815,8 +816,9 @@ public class StockholmFile extends AlignFile
     String type = label;
     if (label.contains("_cons"))
     {
-      type = (label.indexOf("_cons") == label.length() - 5) ? label
-              .substring(0, label.length() - 5) : label;
+      type = (label.indexOf("_cons") == label.length() - 5)
+              ? label.substring(0, label.length() - 5)
+              : label;
     }
     boolean ss = false, posterior = false;
     type = id2type(type);
@@ -850,14 +852,14 @@ public class StockholmFile extends AlignFile
             ann.secondaryStructure = ResidueProperties.getDssp3state(pos)
                     .charAt(0);
 
-          if (ann.secondaryStructure == pos.charAt(0))
-          {
-            ann.displayCharacter = ""; // null; // " ";
-          }
-          else
-          {
-            ann.displayCharacter = " " + ann.displayCharacter;
-          }
+            if (ann.secondaryStructure == pos.charAt(0))
+            {
+              ann.displayCharacter = ""; // null; // " ";
+            }
+            else
+            {
+              ann.displayCharacter = " " + ann.displayCharacter;
+            }
           }
         }
 
@@ -916,13 +918,6 @@ public class StockholmFile extends AlignFile
   @Override
   public String print(SequenceI[] s, boolean jvSuffix)
   {
-    String string = print(s, jvSuffix, false);
-    return string;
-  }
-
-  public String print(SequenceI[] s, boolean jvSuffix,
-          boolean removeAnnotations)
-  {
     out = new StringBuffer();
     out.append("# STOCKHOLM 1.0");
     out.append(newline);
@@ -987,8 +982,8 @@ public class StockholmFile extends AlignFile
       {
         Object idd = en.nextElement();
         String type = (String) dataRef.remove(idd);
-        out.append(new Format("%-" + (maxid - 2) + "s").form("#=GS "
-                + idd.toString() + " "));
+        out.append(new Format("%-" + (maxid - 2) + "s")
+                .form("#=GS " + idd.toString() + " "));
         if (type.contains("PFAM") || type.contains("RFAM"))
         {
 
@@ -1003,102 +998,101 @@ public class StockholmFile extends AlignFile
     }
 
 
-      // output annotations
-      while (i < s.length && s[i] != null)
+    // output annotations
+    while (i < s.length && s[i] != null)
+    {
+      AlignmentAnnotation[] alAnot = s[i].getAnnotation();
+      if (alAnot != null)
       {
-        AlignmentAnnotation[] alAnot = s[i].getAnnotation();
-      if (alAnot != null && !removeAnnotations)
+        Annotation[] ann;
+        for (int j = 0; j < alAnot.length; j++)
         {
-          Annotation[] ann;
-          for (int j = 0; j < alAnot.length; j++)
-          {
 
-            String key = type2id(alAnot[j].label);
-            boolean isrna = alAnot[j].isValidStruc();
+          String key = type2id(alAnot[j].label);
+          boolean isrna = alAnot[j].isValidStruc();
+          if (isrna)
+          {
+            // hardwire to secondary structure if there is RNA secondary
+            // structure on the annotation
+            key = "SS";
+          }
+          if (key == null)
+          {
 
-            if (isrna)
-            {
-              // hardwire to secondary structure if there is RNA secondary
-              // structure on the annotation
-              key = "SS";
-            }
-            if (key == null)
-            {
+            continue;
+          }
 
-              continue;
-            }
+          // out.append("#=GR ");
+          out.append(new Format("%-" + maxid + "s").form(
+                  "#=GR " + printId(s[i], jvSuffix) + " " + key + " "));
+          ann = alAnot[j].annotations;
+          String seq = "";
+          for (int k = 0; k < ann.length; k++)
+          {
+            seq += outputCharacter(key, k, isrna, ann, s[i]);
 
-            // out.append("#=GR ");
-            out.append(new Format("%-" + maxid + "s").form(
-                    "#=GR " + printId(s[i], jvSuffix) + " " + key + " "));
-            ann = alAnot[j].annotations;
-            String seq = "";
-            for (int k = 0; k < ann.length; k++)
-            {
-              seq += outputCharacter(key, k, isrna, ann, s[i]);
-            }
-            out.append(seq);
-            out.append(newline);
-          }
         }
-
-        out.append(new Format("%-" + maxid + "s")
-                .form(printId(s[i], jvSuffix) + " "));
-        out.append(s[i].getSequenceAsString());
-        out.append(newline);
-        i++;
+          out.append(seq);
+          out.append(newline);
+        }
       }
 
-    if (!removeAnnotations)
+      out.append(new Format("%-" + maxid + "s")
+              .form(printId(s[i], jvSuffix) + " "));
+      out.append(s[i].getSequenceAsString());
+      out.append(newline);
+      i++;
+    }
+
+    // alignment annotation
+    AlignmentAnnotation aa;
+    if (al.getAlignmentAnnotation() != null)
     {
-      // alignment annotation
-      AlignmentAnnotation aa;
-      if (al.getAlignmentAnnotation() != null)
+      for (int ia = 0; ia < al.getAlignmentAnnotation().length; ia++)
       {
-        for (int ia = 0; ia < al.getAlignmentAnnotation().length; ia++)
+        aa = al.getAlignmentAnnotation()[ia];
+        if (aa.autoCalculated || !aa.visible || aa.sequenceRef != null)
         {
-          aa = al.getAlignmentAnnotation()[ia];
-          if (aa.autoCalculated || !aa.visible || aa.sequenceRef != null)
-          {
-            continue;
-          }
-          String seq = "";
-          String label;
-          String key = "";
-          if (aa.label.equals("seq"))
+          continue;
+        }
+        String seq = "";
+        String label;
+        String key = "";
+        if (aa.label.equals("seq"))
+        {
+          label = "seq_cons";
+        }
+        else
+        {
+          key = type2id(aa.label.toLowerCase());
+          if (key == null)
           {
-            label = "seq_cons";
+            label = aa.label;
           }
           else
           {
-            key = type2id(aa.label.toLowerCase());
-            if (key == null)
-            {
-              label = aa.label;
-            }
-            else
-            {
-              label = key + "_cons";
-            }
-          }
-          if (label == null)
-          {
-            label = aa.label;
+            label = key + "_cons";
           }
-          label = label.replace(" ", "_");
+        }
+        if (label == null)
+        {
+          label = aa.label;
+        }
+        label = label.replace(" ", "_");
 
-          out.append(new Format("%-" + maxid + "s")
-                  .form("#=GC " + label + " "));
-          boolean isrna = aa.isValidStruc();
-          for (int j = 0; j < aa.annotations.length; j++)
-          {
-            seq += outputCharacter(key, j, isrna, aa.annotations, null);
-          }
-          out.append(seq);
-          out.append(newline);
+        out.append(
+                new Format("%-" + maxid + "s").form("#=GC " + label + " "));
+        boolean isrna = aa.isValidStruc();
+        for (int j = 0; j < aa.annotations.length; j++)
+        {
+          seq += outputCharacter(key, j, isrna, aa.annotations, null);
         }
+        out.append(seq);
+        out.append(newline);
       }
     }
+    
+
 
     out.append("//");
     out.append(newline);
@@ -1121,8 +1115,10 @@ public class StockholmFile extends AlignFile
   {
     char seq = ' ';
     Annotation annot = ann[k];
-    String ch = (annot == null) ? ((sequenceI == null) ? "-" : Character
-            .toString(sequenceI.getCharAt(k))) : annot.displayCharacter;
+    String ch = (annot == null)
+            ? ((sequenceI == null) ? "-"
+                    : Character.toString(sequenceI.getCharAt(k)))
+            : annot.displayCharacter;
     if (key != null && key.equals("SS"))
     {
       if (annot == null)
@@ -1161,8 +1157,6 @@ public class StockholmFile extends AlignFile
     out.append("# STOCKHOLM 1.0");
     out.append(newline);
     print(getSeqsAsArray(), false);
-
-    out.append(newline);
     return out.toString();
   }
 
@@ -1198,8 +1192,8 @@ public class StockholmFile extends AlignFile
     {
       return (String) typeIds.get(id);
     }
-    System.err.println("Warning : Unknown Stockholm annotation type code "
-            + id);
+    System.err.println(
+            "Warning : Unknown Stockholm annotation type code " + id);
     return id;
   }
 
@@ -1220,8 +1214,8 @@ public class StockholmFile extends AlignFile
     {
       return key;
     }
-    System.err.println("Warning : Unknown Stockholm annotation type: "
-            + type);
+    System.err.println(
+            "Warning : Unknown Stockholm annotation type: " + type);
     return key;
   }