HEADER and ATOM must be position 0
[jalview.git] / src / jalview / io / AppletFormatAdapter.java
index f8b809e..24085d2 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
     public class AppletFormatAdapter\r
     {\r
         /** DOCUMENT ME!! */\r
-        public static final Vector formats = new Vector();\r
-\r
-        static\r
+        public static final String [] READABLE_FORMATS = new String[]\r
         {\r
-            formats.addElement("BLC");\r
-            formats.addElement("CLUSTAL");\r
-            formats.addElement("FASTA");\r
-            formats.addElement("MSF");\r
-            formats.addElement("PileUp");\r
-            formats.addElement("PIR");\r
-            formats.addElement("PFAM");\r
-        }\r
+            "BLC", "CLUSTAL", "FASTA", "MSF", "PileUp", "PIR", "PFAM", "STH", "PDB"\r
+        };\r
+\r
+        public static final String [] WRITEABLE_FORMATS = new String[]\r
+            {\r
+            "BLC", "CLUSTAL", "FASTA", "MSF", "PileUp", "PIR", "PFAM"\r
+        };\r
+\r
+\r
+        public static String INVALID_CHARACTERS = "Contains invalid characters";\r
+\r
+        public static String SUPPORTED_FORMATS = "Formats currently supported are\n" +\r
+                                                 "Fasta, MSF, Clustal, BLC, PIR, MSP, and PFAM";\r
+\r
+\r
+        public static String FILE = "File";\r
+        public static String URL = "URL";\r
+        public static String PASTE = "Paste";\r
+        public static String CLASSLOADER = "ClassLoader";\r
+\r
 \r
         AlignFile afile = null;\r
+        String inFile;\r
+\r
+        public static final boolean isValidFormat(String format)\r
+        {\r
+          boolean valid = false;\r
+          for(int i=0; i<READABLE_FORMATS.length; i++)\r
+            if(READABLE_FORMATS[i].equalsIgnoreCase(format))\r
+              return true;\r
+\r
+          return valid;\r
+        }\r
 \r
         /**\r
          * DOCUMENT ME!\r
          * @return DOCUMENT ME!\r
          */\r
         public SequenceI[] readFile(String inFile, String type, String format)\r
+            throws java.io.IOException\r
         {\r
+            this.inFile = inFile;\r
             try\r
             {\r
-                if (format.equals("FASTA"))\r
-                {\r
-                    afile = new FastaFile(inFile, type);\r
-                }\r
-                else if (format.equals("MSF"))\r
-                {\r
-                    afile = new MSFfile(inFile, type);\r
-                }\r
-                else if (format.equals("PileUp"))\r
-                {\r
-                    afile = new PileUpfile(inFile, type);\r
-                }\r
-                else if (format.equals("CLUSTAL"))\r
-                {\r
-                    afile = new ClustalFile(inFile, type);\r
-                }\r
-                else if (format.equals("BLC"))\r
-                {\r
-                    afile = new BLCFile(inFile, type);\r
-                }\r
-                else if (format.equals("PIR"))\r
-                {\r
-                    afile = new PIRFile(inFile, type);\r
-                }\r
-                else if (format.equals("PFAM"))\r
-                {\r
-                    afile = new PfamFile(inFile, type);\r
-                }\r
+              if (format.equals("FASTA"))\r
+              {\r
+                afile = new FastaFile(inFile, type);\r
+              }\r
+              else if (format.equals("MSF"))\r
+              {\r
+                afile = new MSFfile(inFile, type);\r
+              }\r
+              else if (format.equals("PileUp"))\r
+              {\r
+                afile = new PileUpfile(inFile, type);\r
+              }\r
+              else if (format.equals("CLUSTAL"))\r
+              {\r
+                afile = new ClustalFile(inFile, type);\r
+              }\r
+              else if (format.equals("BLC"))\r
+              {\r
+                afile = new BLCFile(inFile, type);\r
+              }\r
+              else if (format.equals("PIR"))\r
+              {\r
+                afile = new PIRFile(inFile, type);\r
+              }\r
+              else if (format.equals("PFAM"))\r
+              {\r
+                afile = new PfamFile(inFile, type);\r
+              }\r
+              else if (format.equals("JnetFile"))\r
+              {\r
+                afile = new JPredFile(inFile, type);\r
+                ( (JPredFile) afile).removeNonSequences();\r
+              }\r
+              else if (format.equals("PDB"))\r
+              {\r
+                afile = new MCview.PDBfile(inFile, type);\r
+              }\r
+              else if (format.equals("STH"))\r
+              {\r
+                afile = new StockholmFile(inFile, type);\r
+              }\r
+\r
 \r
-                return afile.getSeqsAsArray();\r
+              return afile.getSeqsAsArray();\r
             }\r
             catch (Exception e)\r
             {\r
+              e.printStackTrace();\r
               System.err.println("Failed to read alignment using the '" + format +\r
                                  "' reader.\n"+e);\r
 \r
+              if(e.getMessage()!=null && e.getMessage().startsWith(INVALID_CHARACTERS))\r
+                throw new java.io.IOException(e.getMessage());\r
+\r
               // Finally test if the user has pasted just the sequence, no id\r
               if(type.equalsIgnoreCase("Paste"))\r
               {\r
                   // Possible sequence is just residues with no label\r
                   afile = new FastaFile(">UNKNOWN\n" + inFile, "Paste");\r
                   return afile.getSeqsAsArray();\r
-                }catch(Exception ex)\r
+                }\r
+                catch(Exception ex)\r
                 {\r
-                  System.err.println("Failed to read alignment using the 'FASTA' reader.\n"+e);\r
+                  if(ex.toString().startsWith(INVALID_CHARACTERS))\r
+                    throw new java.io.IOException(e.getMessage());\r
+\r
                   ex.printStackTrace();\r
                 }\r
-\r
               }\r
-            }\r
 \r
-            return null;\r
+              // If we get to this stage, the format was not supported\r
+              throw new java.io.IOException(SUPPORTED_FORMATS);\r
+            }\r
         }\r
 \r
 \r
                 {\r
                     afile = new PfamFile();\r
                 }\r
+                else if (format.equalsIgnoreCase("STH"))\r
+                {\r
+                  afile = new StockholmFile();\r
+                }\r
+\r
 \r
                 afile.addJVSuffix(jvsuffix);\r
 \r