Any character non aa or nucleotide is a space
[jalview.git] / src / jalview / io / FastaFile.java
index 37c96f6..684f867 100755 (executable)
@@ -1,6 +1,6 @@
 /*\r
 * Jalview - A Sequence Alignment Editor and Viewer\r
-* Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
+* Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
 *\r
 * This program is free software; you can redistribute it and/or\r
 * modify it under the terms of the GNU General Public License\r
@@ -22,6 +22,8 @@ import jalview.datamodel.*;
 \r
 import java.io.*;\r
 \r
+import java.util.*;\r
+\r
 \r
 /**\r
  * DOCUMENT ME!\r
@@ -31,21 +33,18 @@ import java.io.*;
  */\r
 public class FastaFile extends AlignFile\r
 {\r
-    /**\r
-     * Creates a new FastaFile object.\r
-     */\r
-    public FastaFile()\r
-    {\r
-    }\r
+  /**\r
+   * Length of a sequence line\r
+   */\r
+  int len = 72;\r
+\r
+  StringBuffer out;\r
 \r
     /**\r
      * Creates a new FastaFile object.\r
-     *\r
-     * @param inStr DOCUMENT ME!\r
      */\r
-    public FastaFile(String inStr)\r
+    public FastaFile()\r
     {\r
-        super(inStr);\r
     }\r
 \r
     /**\r
@@ -69,27 +68,57 @@ public class FastaFile extends AlignFile
     public void parse() throws IOException\r
     {\r
         StringBuffer sb = new StringBuffer();\r
-        int count = 0;\r
+        boolean firstLine = true;\r
 \r
         String line;\r
         Sequence seq = null;\r
 \r
+        boolean annotation = false;\r
+\r
         while ((line = nextLine()) != null)\r
         {\r
+            line = line.trim();\r
             if (line.length() > 0)\r
             {\r
-                if (line.charAt(0)=='>')\r
+              if (line.charAt(0)=='>')\r
                 {\r
-                    if (count != 0)\r
+                  if (line.startsWith(">#_"))\r
+                  {\r
+                    if (annotation)\r
+                    {\r
+                      Annotation[] anots = new Annotation[sb.length()];\r
+                      String anotString = sb.toString();\r
+                      for (int i = 0; i < sb.length(); i++)\r
+                      {\r
+                        anots[i] = new Annotation(anotString.substring(i, i+1),\r
+                                                  null,\r
+                                                  ' ', 0);\r
+                      }\r
+                      AlignmentAnnotation aa = new AlignmentAnnotation(\r
+                          seq.getName().substring(2), seq.getDescription(),\r
+                          anots);\r
+\r
+                      annotations.addElement(aa);\r
+                    }\r
+                  }\r
+                  else\r
+                    annotation = false;\r
+\r
+                    if (!firstLine)\r
                     {\r
                        seq.setSequence(sb.toString());\r
-                       seqs.addElement(seq);\r
+\r
+                       if (!annotation)\r
+                         seqs.addElement(seq);\r
                     }\r
 \r
                     seq = parseId(line.substring(1));\r
+                    firstLine = false;\r
 \r
-                    count++;\r
                     sb = new StringBuffer();\r
+\r
+                    if (line.startsWith(">#_"))\r
+                      annotation = true;\r
                 }\r
                 else\r
                 {\r
@@ -98,19 +127,30 @@ public class FastaFile extends AlignFile
             }\r
         }\r
 \r
-        if (count > 0)\r
+        if (annotation)\r
         {\r
-            if (!isValidProteinSequence(sb.toString().toUpperCase()))\r
-            {\r
-                throw new IOException("Invalid protein sequence");\r
-            }\r
+          Annotation[] anots = new Annotation[sb.length()];\r
+          String anotString = sb.toString();\r
+          for (int i = 0; i < sb.length(); i++)\r
+          {\r
+            anots[i] = new Annotation(anotString.substring(i, i + 1),\r
+                                      null,\r
+                                      ' ', 0);\r
+          }\r
+          AlignmentAnnotation aa = new AlignmentAnnotation(\r
+              seq.getName().substring(2), seq.getDescription(),\r
+              anots);\r
+\r
+          annotations.addElement(aa);\r
+        }\r
 \r
+        else if (!firstLine)\r
+        {\r
             seq.setSequence(sb.toString());\r
             seqs.addElement(seq);\r
         }\r
     }\r
 \r
-\r
     /**\r
      * DOCUMENT ME!\r
      *\r
@@ -123,8 +163,7 @@ public class FastaFile extends AlignFile
      */\r
     public String print(SequenceI[] s)\r
     {\r
-        int len = 72;\r
-        StringBuffer out = new StringBuffer();\r
+        out = new StringBuffer();\r
         int i = 0;\r
 \r
         while ((i < s.length) && (s[i] != null))\r
@@ -144,11 +183,11 @@ public class FastaFile extends AlignFile
 \r
                 if (end < s[i].getLength())\r
                 {\r
-                    out.append(s[i].getSequence(start, end) + "\n");\r
+                    out.append(s[i].getSequenceAsString(start, end) + "\n");\r
                 }\r
                 else if (start < s[i].getLength())\r
                 {\r
-                    out.append(s[i].getSequence(start, s[i].getLength()) + "\n");\r
+                    out.append(s[i].getSequenceAsString(start, s[i].getLength()) + "\n");\r
                 }\r
             }\r
 \r