Merge branch 'bug/JAL-1583_MSA_webservice_validation' into Release_2_8_2_Branch
[jalview.git] / src / jalview / ws / jws2 / MsaWSClient.java
index 09a6d8d..c3661c3 100644 (file)
@@ -22,6 +22,7 @@ package jalview.ws.jws2;
 
 import jalview.datamodel.Alignment;
 import jalview.datamodel.AlignmentView;
+import jalview.datamodel.SequenceI;
 import jalview.gui.AlignFrame;
 import jalview.gui.Desktop;
 import jalview.gui.JvSwingUtils;
@@ -127,8 +128,10 @@ public class MsaWSClient extends Jws2Client
 
       return;
     }
+
     startMsaWSClient(altitle, msa, submitGaps, preserveOrder, seqdataset);
 
+
   }
 
   public MsaWSClient()
@@ -173,9 +176,55 @@ public class MsaWSClient extends Jws2Client
             WsURL, wsInfo, alignFrame, WebServiceName, jobtitle, msa,
             submitGaps, preserveOrder, seqdataset);
     wsInfo.setthisService(msathread);
+
     msathread.start();
+
   }
 
+  private boolean isValidAlignment(AlignmentView av)
+  {
+    int validSeqCount = 0;
+    List<SequenceI> seqs = av.getVisibleAlignment('c').getSequences(); // .getSequences();
+    if (seqs.size() < 2)
+    {
+      JOptionPane
+              .showMessageDialog(
+                      alignFrame,
+                      "A minimum of two sequences is required to perform this operation",
+                      "Invalid selection", JOptionPane.INFORMATION_MESSAGE);
+
+      return false;
+    }
+
+    for (SequenceI seq : seqs)
+    {
+
+      if (seq.getSequenceAsString().matches("(-*[a-zA-Z]-*){3}[a-zA-Z-]*"))
+      {
+        ++validSeqCount;
+      }
+      if (validSeqCount > 1)
+      {
+        return true;
+      }
+    }
+
+    JOptionPane
+            .showMessageDialog(
+                    alignFrame,
+                    "A minimum of two sequences with at least three non-gap character \nis required to perform this operation",
+                    "Invalid selection", JOptionPane.INFORMATION_MESSAGE);
+
+    return false;
+  }
+
+  public static void main(String[] args)
+  {
+    System.out.println("A".matches("(-*[a-zA-Z]-*){1}[a-zA-Z-]*"));
+  }
+
+
+
   protected String getServiceActionKey()
   {
     return "MsaWS";
@@ -250,9 +299,13 @@ public class MsaWSClient extends Jws2Client
         public void actionPerformed(ActionEvent e)
         {
           AlignmentView msa = alignFrame.gatherSequencesForAlignment();
+
+          if (isValidAlignment(msa))
+          {
           new MsaWSClient(service, alignFrame.getTitle(), msa, withGaps,
                   true, alignFrame.getViewport().getAlignment()
                           .getDataset(), alignFrame);
+          }
 
         }
       });
@@ -271,9 +324,13 @@ public class MsaWSClient extends Jws2Client
           public void actionPerformed(ActionEvent e)
           {
             AlignmentView msa = alignFrame.gatherSequencesForAlignment();
+
+            if (isValidAlignment(msa))
+            {
             new MsaWSClient(service, null, null, true, alignFrame
                     .getTitle(), msa, withGaps, true, alignFrame
                     .getViewport().getAlignment().getDataset(), alignFrame);
+            }
 
           }
         });
@@ -324,9 +381,13 @@ public class MsaWSClient extends Jws2Client
               {
                 AlignmentView msa = alignFrame
                         .gatherSequencesForAlignment();
+
+                if (isValidAlignment(msa))
+                {
                 new MsaWSClient(service, preset, alignFrame.getTitle(),
                         msa, false, true, alignFrame.getViewport()
                                 .getAlignment().getDataset(), alignFrame);
+                }
 
               }