JAL-1443 bugfix to prevent loading of unidentifiable gibberish data
authortcofoegbu <tcnofoegbu@dundee.ac.uk>
Tue, 21 Jul 2015 16:59:01 +0000 (17:59 +0100)
committertcofoegbu <tcnofoegbu@dundee.ac.uk>
Tue, 21 Jul 2015 16:59:01 +0000 (17:59 +0100)
resources/lang/Messages.properties
src/jalview/datamodel/Alignment.java
src/jalview/datamodel/AlignmentI.java
src/jalview/gui/CutAndPasteTransfer.java
src/jalview/io/AppletFormatAdapter.java
src/jalview/io/FileLoader.java
src/jalview/io/IdentifyFile.java

index b2bf58b..b4c2d04 100644 (file)
@@ -1245,3 +1245,7 @@ info.enter_search_text_to_enable = Enter Search Text to Enable
 info.search_in_annotation_label = Search in {0} Label
 info.search_in_annotation_description = Search in {0} Description
 info.change_threshold_mode_to_enable = Change Threshold Mode to Enable
+label.null_or_unidentifiable_data = Null or unidentifiable data content pasted!
+label.unidentifiable_data = Unidentifiable Data
+label.null_or_invalid_alignment = Null or invalid alignment data!
+label.unable_to_create_alignment = Unable to create alignment
\ No newline at end of file
index 81046f1..15df67b 100755 (executable)
  */
 package jalview.datamodel;
 
+import jalview.analysis.AlignmentUtils;
+import jalview.io.FastaFile;
+import jalview.util.MessageManager;
+
 import java.util.ArrayList;
 import java.util.Enumeration;
 import java.util.HashSet;
@@ -30,10 +34,6 @@ import java.util.Map;
 import java.util.Set;
 import java.util.Vector;
 
-import jalview.analysis.AlignmentUtils;
-import jalview.io.FastaFile;
-import jalview.util.MessageManager;
-
 /**
  * Data structure to hold and manipulate a multiple sequence alignment
  */
@@ -1734,4 +1734,19 @@ public class Alignment implements AlignmentI
     }
     return names;
   }
+
+  @Override
+  public boolean hasValidSequence()
+  {
+    boolean hasValidSeq = false;
+    for (SequenceI seq : getSequences())
+    {
+      if (seq.getSequenceAsString().matches("(-*[a-zA-Z]-*){1}[a-zA-Z-]*"))
+      {
+        hasValidSeq = true;
+        break;
+      }
+    }
+    return hasValidSeq;
+  }
 }
index 67e5743..8194bf0 100755 (executable)
@@ -517,4 +517,12 @@ public interface AlignmentI extends AnnotatedCollectionI
    * @return
    */
   Set<String> getSequenceNames();
+
+  /**
+   * Checks if the alignment has at least one sequence with one non-gaped
+   * residue
+   * 
+   * @return
+   */
+  public boolean hasValidSequence();
 }
index 979d1aa..e08bfcd 100644 (file)
@@ -24,6 +24,7 @@ import jalview.api.AlignViewportI;
 import jalview.api.AlignmentViewPanel;
 import jalview.api.ComplexAlignFile;
 import jalview.api.FeaturesDisplayedI;
+import jalview.bin.Jalview;
 import jalview.datamodel.AlignmentI;
 import jalview.datamodel.ColumnSelection;
 import jalview.datamodel.SequenceI;
@@ -202,6 +203,21 @@ public class CutAndPasteTransfer extends GCutAndPasteTransfer
     }
 
     String format = new IdentifyFile().Identify(text, "Paste");
+    if (format == null || format.equalsIgnoreCase("EMPTY DATA FILE"))
+    {
+      System.err.println(MessageManager
+              .getString("label.null_or_unidentifiable_data"));
+      if (!Jalview.isHeadlessMode())
+      {
+        JOptionPane.showMessageDialog(null,
+ MessageManager
+                .getString("label.null_or_unidentifiable_data"),
+                MessageManager.getString("label.unidentifiable_data"),
+                JOptionPane.ERROR_MESSAGE);
+      }
+      return;
+    }
+
     // TODO: identify feature, annotation or tree file and parse appropriately.
     AlignmentI al = null;
 
@@ -224,7 +240,7 @@ public class CutAndPasteTransfer extends GCutAndPasteTransfer
       }
     }
 
-    if (al != null)
+    if (al != null && al.hasValidSequence())
     {
       String title = MessageManager.formatMessage(
               "label.input_cut_paste_params", new String[]
@@ -276,6 +292,22 @@ public class CutAndPasteTransfer extends GCutAndPasteTransfer
         }
       }
     }
+    else
+    {
+      System.err.println(MessageManager
+              .getString("label.null_or_invalid_alignment"));
+      if (!Jalview.isHeadlessMode())
+      {
+        JOptionPane
+                .showMessageDialog(
+                        null,
+                        MessageManager
+                                .getString("label.null_or_invalid_alignment"),
+                        MessageManager
+                                .getString("label.unable_to_create_alignment"),
+                        JOptionPane.ERROR_MESSAGE);
+      }
+    }
   }
 
 
index 90400f4..02de02e 100755 (executable)
@@ -205,6 +205,10 @@ public class AppletFormatAdapter
   public static final boolean isValidFormat(String format,
           boolean forwriting)
   {
+    if (format == null)
+    {
+      return false;
+    }
     boolean valid = false;
     String[] format_list = (forwriting) ? WRITEABLE_FORMATS
             : READABLE_FORMATS;
index 3aded05..73c10df 100755 (executable)
@@ -265,11 +265,12 @@ public class FileLoader implements Runnable
       {
         Desktop.instance.stopLoading();
         System.err.println("The input file \"" + file
-                + "\" has no data content!");
+                + "\" has null or unidentifiable data content!");
         if (!Jalview.isHeadlessMode())
         {
-          JOptionPane.showMessageDialog(null, "The input file \""
-                  + file + "\" has no data content!", "Empty data file",
+          JOptionPane.showMessageDialog(null, "The input file \"" + file
+                  + "\" has null or unidentifiable data content!",
+                  "Empty data file",
                   JOptionPane.ERROR_MESSAGE);
         }
         return;
index dae8f82..17ba32e 100755 (executable)
@@ -84,7 +84,7 @@ public class IdentifyFile
    */
   public String Identify(FileParse source, boolean closeSource)
   {
-    String reply = "PFAM";
+    String reply = null;
     String data;
     int length = 0;
     boolean lineswereskipped = false;