header updated
[jalview.git] / src / jalview / io / ClustalFile.java
index 74cb2d0..e3f83ee 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
@@ -27,17 +27,11 @@ import jalview.util.*;
 public class ClustalFile\r
     extends AlignFile\r
 {\r
-  Vector ids;\r
 \r
   public ClustalFile()\r
   {\r
   }\r
 \r
-  public ClustalFile(String inStr)\r
-  {\r
-    super(inStr);\r
-  }\r
-\r
   public ClustalFile(String inFile, String type)\r
       throws IOException\r
   {\r
@@ -47,18 +41,18 @@ public class ClustalFile
   public void initData()\r
   {\r
     super.initData();\r
-    ids = new Vector();\r
   }\r
 \r
-  public void parse()\r
+  public void parse() throws IOException\r
   {\r
     int i = 0;\r
     boolean flag = false;\r
 \r
     Vector headers = new Vector();\r
     Hashtable seqhash = new Hashtable();\r
-\r
-    String line;\r
+    StringBuffer tempseq;\r
+    String line, id;\r
+    StringTokenizer str;\r
 \r
     try\r
     {\r
@@ -66,14 +60,13 @@ public class ClustalFile
       {\r
         if (line.indexOf(" ") != 0)\r
         {\r
-          StringTokenizer str = new StringTokenizer(line, " ");\r
-          String id = "";\r
+          str = new StringTokenizer(line, " ");\r
 \r
           if (str.hasMoreTokens())\r
           {\r
             id = str.nextToken();\r
 \r
-            if (id.equals("CLUSTAL"))\r
+            if (id.equalsIgnoreCase("CLUSTAL"))\r
             {\r
               flag = true;\r
             }\r
@@ -81,8 +74,6 @@ public class ClustalFile
             {\r
               if (flag)\r
               {\r
-                StringBuffer tempseq;\r
-\r
                 if (seqhash.containsKey(id))\r
                 {\r
                   tempseq = (StringBuffer) seqhash.get(id);\r
@@ -105,6 +96,8 @@ public class ClustalFile
               }\r
             }\r
           }\r
+          else\r
+            flag = true;\r
         }\r
       }\r
     }\r
@@ -121,9 +114,6 @@ public class ClustalFile
       //Add sequences to the hash\r
       for (i = 0; i < headers.size(); i++)\r
       {\r
-        int start = -1;\r
-        int end = -1;\r
-\r
         if (seqhash.get(headers.elementAt(i)) != null)\r
         {\r
           if (maxLength < seqhash.get(headers.elementAt(i)).toString()\r
@@ -133,42 +123,16 @@ public class ClustalFile
                 .length();\r
           }\r
 \r
-          String head = headers.elementAt(i).toString();\r
-          start = 1;\r
-          end = seqhash.get(headers.elementAt(i)).toString().length();\r
-\r
-          if (head.indexOf("/") > 0)\r
-          {\r
-            StringTokenizer st = new StringTokenizer(head, "/");\r
-\r
-            if (st.countTokens() == 2)\r
-            {\r
-              ids.addElement(st.nextToken());\r
-\r
-              String tmp = st.nextToken();\r
-              st = new StringTokenizer(tmp, "-");\r
+          Sequence newSeq = parseId(headers.elementAt(i).toString());\r
+          newSeq.setSequence( seqhash.get(headers.elementAt(i).toString()).toString() );\r
 \r
-              if (st.countTokens() == 2)\r
-              {\r
-                start = Integer.valueOf(st.nextToken())\r
-                    .intValue();\r
-                end = Integer.valueOf(st.nextToken()).intValue();\r
-              }\r
-            }\r
-            else\r
-            {\r
-              ids.addElement(headers.elementAt(i));\r
-            }\r
-          }\r
-          else\r
+          if (!isValidProteinSequence(newSeq.getSequence()))\r
           {\r
-            ids.addElement(headers.elementAt(i));\r
+              throw new IOException(AppletFormatAdapter.INVALID_CHARACTERS\r
+                                    + " : " + newSeq.getName()\r
+                                    + " : " + invalidCharacter);\r
           }\r
 \r
-          Sequence newSeq = new Sequence(ids.elementAt(i).toString(),\r
-                                         seqhash.get(headers.elementAt(i).\r
-              toString())\r
-                                         .toString(), start, end);\r
 \r
           seqs.addElement(newSeq);\r
         }\r
@@ -187,7 +151,7 @@ public class ClustalFile
     return print(getSeqsAsArray());\r
   }\r
 \r
-  public static String print(SequenceI[] s)\r
+  public String print(SequenceI[] s)\r
   {\r
     StringBuffer out = new StringBuffer("CLUSTAL\n\n");\r
 \r
@@ -198,8 +162,7 @@ public class ClustalFile
 \r
     while ( (i < s.length) && (s[i] != null))\r
     {\r
-      String tmp = s[i].getName() + "/" + s[i].getStart() + "-" +\r
-          s[i].getEnd();\r
+      String tmp = printId(s[i]);\r
 \r
       if (s[i].getSequence().length() > max)\r
       {\r
@@ -230,8 +193,7 @@ public class ClustalFile
 \r
       while ( (j < s.length) && (s[j] != null))\r
       {\r
-        out.append(new Format("%-" + maxid + "s").form(s[j].getName() +\r
-            "/" + s[j].getStart() + "-" + s[j].getEnd()) + " ");\r
+        out.append(new Format("%-" + maxid + "s").form( printId(s[j]) + " "));\r
 \r
         int start = i * len;\r
         int end = start + len;\r