JAL-2629 remove gap padding, and now has option to remove annotations
authorTZVanaalten <TZVanaalten@LS30916.ad.lifesci.dundee.ac.uk>
Mon, 14 Aug 2017 14:49:33 +0000 (15:49 +0100)
committerTZVanaalten <TZVanaalten@LS30916.ad.lifesci.dundee.ac.uk>
Mon, 14 Aug 2017 14:49:33 +0000 (15:49 +0100)
src/jalview/io/StockholmFile.java

index ead653f..8d340f2 100644 (file)
@@ -916,6 +916,13 @@ 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);
@@ -995,102 +1002,101 @@ public class StockholmFile extends AlignFile
       }
     }
 
-    // output annotations
-    while (i < s.length && s[i] != null)
-    {
-      AlignmentAnnotation[] alAnot = s[i].getAnnotation();
-      if (alAnot != null)
+
+      // output annotations
+      while (i < s.length && s[i] != null)
       {
-        Annotation[] ann;
-        for (int j = 0; j < alAnot.length; j++)
+        AlignmentAnnotation[] alAnot = s[i].getAnnotation();
+      if (alAnot != null && !removeAnnotations)
         {
+          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++)
-          {
-            char character = outputCharacter(key, k, isrna, ann, s[i]);
-            if (character == ' ')
+            // 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++)
             {
-              character = '.';
+              seq += outputCharacter(key, k, isrna, ann, s[i]);
             }
-            seq += character;
+            out.append(seq);
+            out.append(newline);
           }
-          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(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)
+    if (!removeAnnotations)
     {
-      for (int ia = 0; ia < al.getAlignmentAnnotation().length; ia++)
+      // alignment annotation
+      AlignmentAnnotation aa;
+      if (al.getAlignmentAnnotation() != 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"))
+        for (int ia = 0; ia < al.getAlignmentAnnotation().length; ia++)
         {
-          label = "seq_cons";
-        }
-        else
-        {
-          key = type2id(aa.label.toLowerCase());
-          if (key == null)
+          aa = al.getAlignmentAnnotation()[ia];
+          if (aa.autoCalculated || !aa.visible || aa.sequenceRef != null)
           {
-            label = aa.label;
+            continue;
+          }
+          String seq = "";
+          String label;
+          String key = "";
+          if (aa.label.equals("seq"))
+          {
+            label = "seq_cons";
           }
           else
           {
-            label = key + "_cons";
+            key = type2id(aa.label.toLowerCase());
+            if (key == null)
+            {
+              label = aa.label;
+            }
+            else
+            {
+              label = key + "_cons";
+            }
           }
-        }
-        if (label == null)
-        {
-          label = aa.label;
-        }
-        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(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(seq);
-        out.append(newline);
       }
     }