added CSV export of annotation rows to application.
[jalview.git] / src / jalview / io / AnnotationFile.java
index 4acd2a1..577b7e4 100755 (executable)
@@ -882,4 +882,33 @@ public class AnnotationFile
       al.setProperty(key,value);\r
     }\r
   }\r
+\r
+  /**\r
+   * Write annotations as a CSV file of the form 'label, value, value, ...' for each row.\r
+   * @param annotations\r
+   * @return CSV file as a string.\r
+   */\r
+  public String printCSVAnnotations(AlignmentAnnotation[] annotations)\r
+  {\r
+    StringBuffer sp = new StringBuffer();\r
+    for (int i=0; i<annotations.length; i++)\r
+    {\r
+      String atos = annotations[i].toString();\r
+      int p = 0;\r
+      do {\r
+        int cp = atos.indexOf("\n", p);\r
+        sp.append(annotations[i].label);\r
+        sp.append(",");\r
+        if (cp>p)\r
+        {\r
+          sp.append(atos.substring(p, cp+1));\r
+        } else {\r
+          sp.append(atos.substring(p));\r
+          sp.append("\n");\r
+        }\r
+        p = cp+1;\r
+      } while (p>0);\r
+    }\r
+    return sp.toString();\r
+  }\r
 }\r