Report invalid chars
authoramwaterhouse <Andrew Waterhouse>
Wed, 8 Feb 2006 15:21:42 +0000 (15:21 +0000)
committeramwaterhouse <Andrew Waterhouse>
Wed, 8 Feb 2006 15:21:42 +0000 (15:21 +0000)
src/jalview/appletgui/CutAndPasteTransfer.java
src/jalview/io/AlignFile.java
src/jalview/io/AppletFormatAdapter.java
src/jalview/io/BLCFile.java
src/jalview/io/ClustalFile.java
src/jalview/io/FastaFile.java
src/jalview/io/FileLoader.java
src/jalview/io/JPredFile.java
src/jalview/io/MSFfile.java
src/jalview/io/PIRFile.java
src/jalview/io/PfamFile.java

index 4fb8ec1..51d8d7f 100755 (executable)
@@ -103,8 +103,12 @@ public class CutAndPasteTransfer extends Panel implements ActionListener, MouseL
       SequenceI[] sequences = null;\r
 \r
       String format = IdentifyFile.Identify(text, "Paste");\r
-      sequences = new AppletFormatAdapter().readFile(text, "Paste", format);\r
-\r
+      try{\r
+        sequences = new AppletFormatAdapter().readFile(text, "Paste", format);\r
+      }catch(java.io.IOException ex)\r
+      {\r
+        ex.printStackTrace();\r
+      }\r
       if (sequences != null)\r
       {\r
         AlignFrame af = new AlignFrame(new Alignment(sequences), applet,\r
index f933961..ceec54b 100755 (executable)
@@ -135,12 +135,15 @@ public abstract class AlignFile extends FileParse
             if (!jalview.schemes.ResidueProperties.aaHash.containsKey(\r
                         String.valueOf(sequence.charAt(i))))\r
             {\r
+                invalidCharacter = sequence.charAt(i);\r
                 return false;\r
             }\r
 \r
         return true;\r
     }\r
 \r
+    char invalidCharacter;\r
+\r
     /**\r
      * This method must be implemented to parse the contents of the file.\r
      */\r
index f8b809e..dca2d0c 100755 (executable)
         /** DOCUMENT ME!! */\r
         public static final Vector formats = new Vector();\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
         static\r
         {\r
             formats.addElement("BLC");\r
@@ -57,6 +62,7 @@
          * @return DOCUMENT ME!\r
          */\r
         public SequenceI[] readFile(String inFile, String type, String format)\r
+            throws java.io.IOException\r
         {\r
             try\r
             {\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
index a7cc1ea..56d40f9 100755 (executable)
@@ -77,7 +77,7 @@ public class BLCFile extends AlignFile
     /**\r
      * DOCUMENT ME!\r
      */\r
-    public void parse()\r
+    public void parse() throws IOException\r
     {\r
         boolean idsFound = false;\r
         Vector ids = new Vector();\r
@@ -87,8 +87,6 @@ public class BLCFile extends AlignFile
 \r
         String line = null;\r
 \r
-        try\r
-        {\r
             do\r
             {\r
                 line = nextLine();\r
@@ -146,17 +144,23 @@ public class BLCFile extends AlignFile
 \r
             for (int i = 0; i < ids.size(); i++)\r
             {\r
-                Sequence newSeq = new Sequence(ids.elementAt(i).toString(),\r
-                        seqstrings[i].toString(),\r
-                        Integer.parseInt(starts.elementAt(i).toString()),\r
-                        Integer.parseInt(ends.elementAt(i).toString()));\r
+              Sequence newSeq = new Sequence(ids.elementAt(i).toString(),\r
+                                             seqstrings[i].toString(),\r
+                                             Integer.parseInt(starts.elementAt(i).\r
+                                                              toString()),\r
+                                             Integer.parseInt(ends.elementAt(i).toString()));\r
+\r
+              if (!isValidProteinSequence(newSeq.getSequence()))\r
+              {\r
+                throw new IOException(AppletFormatAdapter.INVALID_CHARACTERS\r
+                               +" : "+ newSeq.getName()\r
+                               +" : "+invalidCharacter);\r
+\r
+              }\r
+\r
                 seqs.addElement(newSeq);\r
             }\r
-        }\r
-        catch (Exception ex)\r
-        {\r
-            ex.printStackTrace();\r
-        }\r
+\r
     }\r
 \r
     /**\r
index e72e0df..2ea1d2f 100755 (executable)
@@ -48,7 +48,7 @@ public class ClustalFile
     super.initData();\r
   }\r
 \r
-  public void parse()\r
+  public void parse() throws IOException\r
   {\r
     int i = 0;\r
     boolean flag = false;\r
@@ -131,6 +131,14 @@ public class ClustalFile
           Sequence newSeq = parseId(headers.elementAt(i).toString());\r
           newSeq.setSequence( seqhash.get(headers.elementAt(i).toString()).toString() );\r
 \r
+          if (!isValidProteinSequence(newSeq.getSequence()))\r
+          {\r
+              throw new IOException(AppletFormatAdapter.INVALID_CHARACTERS\r
+                                    + " : " + newSeq.getName()\r
+                                    + " : " + invalidCharacter);\r
+          }\r
+\r
+\r
           seqs.addElement(newSeq);\r
         }\r
         else\r
index 37c96f6..2e203af 100755 (executable)
@@ -82,6 +82,13 @@ public class FastaFile extends AlignFile
                 {\r
                     if (count != 0)\r
                     {\r
+                      if (!isValidProteinSequence(sb.toString()))\r
+                      {\r
+                        throw new IOException(AppletFormatAdapter.INVALID_CHARACTERS\r
+                                              +" : "+seq.getName()\r
+                                              +" : "+invalidCharacter);\r
+                      }\r
+\r
                        seq.setSequence(sb.toString());\r
                        seqs.addElement(seq);\r
                     }\r
@@ -100,9 +107,11 @@ public class FastaFile extends AlignFile
 \r
         if (count > 0)\r
         {\r
-            if (!isValidProteinSequence(sb.toString().toUpperCase()))\r
+            if (!isValidProteinSequence(sb.toString()))\r
             {\r
-                throw new IOException("Invalid protein sequence");\r
+                throw new IOException(AppletFormatAdapter.INVALID_CHARACTERS\r
+                                      +" : "+seq.getName()\r
+                                      +" : "+invalidCharacter);\r
             }\r
 \r
             seq.setSequence(sb.toString());\r
index 26aff08..38959a7 100755 (executable)
@@ -78,9 +78,16 @@ public class FileLoader
     }\r
     else\r
     {\r
+      String errorMessage = AppletFormatAdapter.SUPPORTED_FORMATS;\r
+\r
       if (FormatAdapter.formats.contains(format))\r
       {\r
-        sequences = new FormatAdapter().readFile(file, protocol, format);\r
+        try{\r
+          sequences = new FormatAdapter().readFile(file, protocol, format);\r
+        }catch(java.io.IOException ex)\r
+        {\r
+          errorMessage = ex.getMessage();\r
+        }\r
       }\r
 \r
       if ( (sequences != null) && (sequences.length > 0))\r
@@ -104,10 +111,9 @@ public class FileLoader
       else\r
       {\r
         JOptionPane.showInternalMessageDialog(Desktop.desktop,\r
-                                              "Couldn't open file.\n" +\r
-                                              "Formats currently supported are\n" +\r
-                                              "Fasta, MSF, Clustal, BLC, PIR, MSP, and PFAM" // JBPNote - message should be generated through FormatAdapter!\r
-                                              , "Error loading file",\r
+                                              "Couldn't load file "+file+"\n"\r
+                                              +errorMessage,\r
+                                              "Error loading file",\r
                                               JOptionPane.WARNING_MESSAGE);\r
       }\r
     }\r
index cadf153..f5ba7d6 100755 (executable)
@@ -283,9 +283,9 @@ public class JPredFile extends AlignFile
             if (!Symscores.containsKey(ids.elementAt(i)) &&\r
                     !isValidProteinSequence(newSeq.getSequence()))\r
             {\r
-                throw new IOException(\r
-                    "JPredConcise: Not a valid protein sequence - (" +\r
-                    ids.elementAt(i).toString() + ")");\r
+                throw new IOException("JPredConcise: "\r
+                                      +AppletFormatAdapter.INVALID_CHARACTERS +" : "\r
+                                      +ids.elementAt(i).toString() + ")");\r
             }\r
 \r
             if (maxLength != seq_entries.elementAt(i).toString().length())\r
index 179204e..bae4342 100755 (executable)
@@ -70,7 +70,7 @@ public class MSFfile extends AlignFile
     /**\r
      * DOCUMENT ME!\r
      */\r
-    public void parse()\r
+    public void parse() throws IOException\r
     {\r
         int i = 0;\r
         boolean seqFlag = false;\r
@@ -147,9 +147,6 @@ public class MSFfile extends AlignFile
                 String head = headers.elementAt(i).toString();\r
                 String seq = seqhash.get(head).toString();\r
 \r
-                int start = -1;\r
-                int end =  -1;\r
-\r
                 if (maxLength < head.length())\r
                 {\r
                     maxLength = head.length();\r
@@ -157,8 +154,17 @@ public class MSFfile extends AlignFile
 \r
                 // Replace ~ with a sensible gap character\r
                 seq = seq.replace('~', '-');\r
+                if (!isValidProteinSequence(seq))\r
+                {\r
+                    throw new IOException(AppletFormatAdapter.\r
+                                          INVALID_CHARACTERS\r
+                                          + " : " + head\r
+                                          + " : " + invalidCharacter);\r
+                }\r
+\r
 \r
                 Sequence newSeq = parseId(head);\r
+\r
                 newSeq.setSequence(seq);\r
 \r
                 seqs.addElement(newSeq);\r
index 724fea8..9e05d8f 100755 (executable)
@@ -21,7 +21,6 @@ package jalview.io;
 import java.io.*;\r
 import java.util.*;\r
 \r
-import jalview.analysis.*;\r
 import jalview.datamodel.*;\r
 \r
 public class PIRFile\r
@@ -44,10 +43,8 @@ public class PIRFile
     super(inFile, type);\r
   }\r
 \r
-  public void parse()\r
+  public void parse() throws IOException\r
   {\r
-    try\r
-    {\r
       StringBuffer sequence;\r
       String line = null;\r
 \r
@@ -88,17 +85,19 @@ public class PIRFile
         {\r
           sequence.setLength(sequence.length() - 1);\r
           newSeq.setSequence(sequence.toString());\r
+          if (!isValidProteinSequence(newSeq.getSequence()))\r
+          {\r
+            throw new IOException(AppletFormatAdapter.INVALID_CHARACTERS\r
+                                  +" : "+ newSeq.getName()\r
+                                  +" : "+invalidCharacter);\r
+          }\r
+\r
           seqs.addElement(newSeq);\r
           ModellerDescription md = new ModellerDescription(newSeq.\r
               getDescription());\r
           md.updateSequenceI(newSeq);\r
         }\r
       }\r
-    }\r
-    catch (Exception ex)\r
-    {\r
-      ex.printStackTrace();\r
-    }\r
   }\r
 \r
   public String print()\r
index ec52bac..8056f38 100755 (executable)
@@ -48,8 +48,7 @@ public class PfamFile
     super.initData();\r
   }\r
 \r
-  public void parse()\r
-      throws IOException\r
+  public void parse() throws IOException\r
   {\r
     int i = 0;\r
     String line;\r
@@ -118,8 +117,9 @@ public class PfamFile
 \r
         if (!isValidProteinSequence(newSeq.getSequence()))\r
         {\r
-          throw new IOException(\r
-              "Not a valid protein sequence - (PFAM input)");\r
+          throw new IOException(AppletFormatAdapter.INVALID_CHARACTERS\r
+                                +" : "+ newSeq.getName()\r
+                                +" : "+invalidCharacter);\r
         }\r
       }\r
       else\r