JAL-2089 patch broken merge to master for Release 2.10.0b1
[jalview.git] / src / jalview / ws / jws2 / RNAalifoldClient.java
index d86f15a..9ca6d2e 100644 (file)
@@ -1,12 +1,32 @@
+/*
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
+ * 
+ * This file is part of Jalview.
+ * 
+ * Jalview is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License 
+ * as published by the Free Software Foundation, either version 3
+ * of the License, or (at your option) any later version.
+ *  
+ * Jalview is distributed in the hope that it will be useful, but 
+ * WITHOUT ANY WARRANTY; without even the implied warranty 
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
+ * PURPOSE.  See the GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
+ * The Jalview Authors are detailed in the 'AUTHORS' file.
+ */
 package jalview.ws.jws2;
 
-import jalview.api.AlignCalcWorkerI;
 import jalview.datamodel.AlignmentAnnotation;
 import jalview.datamodel.Annotation;
 import jalview.gui.AlignFrame;
-import jalview.ws.jws2.dm.AAConSettings;
+import jalview.util.MessageManager;
 import jalview.ws.jws2.jabaws2.Jws2Instance;
 import jalview.ws.params.WsParamSetI;
+import jalview.ws.uimodel.AlignAnalysisUIText;
 
 import java.text.MessageFormat;
 import java.util.ArrayList;
@@ -15,14 +35,21 @@ import java.util.List;
 import java.util.TreeSet;
 import java.util.regex.Pattern;
 
+import compbio.data.sequence.FastaSequence;
 import compbio.data.sequence.RNAStructReader.AlifoldResult;
 import compbio.data.sequence.RNAStructScoreManager;
 import compbio.data.sequence.Range;
 import compbio.data.sequence.Score;
 import compbio.metadata.Argument;
 
-public class RNAalifoldClient extends JabawsAlignCalcWorker implements
-        AlignCalcWorkerI
+/**
+ * Client for the JABA RNA Alifold Service
+ * 
+ * @author daluke - Daniel Barton
+ * 
+ */
+
+public class RNAalifoldClient extends JabawsCalcWorker
 {
 
   String methodName;
@@ -37,25 +64,32 @@ public class RNAalifoldClient extends JabawsAlignCalcWorker implements
           WsParamSetI preset, List<Argument> paramset)
   {
     super(sh, alignFrame, preset, paramset);
-
-    if (arguments == null)
-      arguments = new ArrayList<Argument>();
-
     af = alignFrame;
     methodName = sh.serviceType;
-
+    alignedSeqs = true;
+    submitGaps = true;
     nucleotidesAllowed = true;
     proteinAllowed = false;
     initViewportParams();
   }
 
-  protected void initViewportParams()
+  @Override
+  public String getCalcId()
+  {
+    return CALC_ID;
+  }
+
+  private static String CALC_ID = "jalview.ws.jws2.RNAalifoldClient";
+
+  public static AlignAnalysisUIText getAlignAnalysisUITest()
   {
-    ((jalview.gui.AlignViewport) alignViewport).setCalcIdSettingsFor(
-            getCalcId(),
-            new AAConSettings(true, service, this.preset,
-                    (arguments != null) ? JabaParamStore
-                            .getJwsArgsfromJaba(arguments) : null), true);
+    return new AlignAnalysisUIText(
+            compbio.ws.client.Services.RNAalifoldWS.toString(),
+            jalview.ws.jws2.RNAalifoldClient.class, CALC_ID, true, false,
+            true, MessageManager.getString("label.rnalifold_calculations"),
+            MessageManager.getString("tooltip.rnalifold_calculations"),
+            MessageManager.getString("label.rnalifold_settings"),
+            MessageManager.getString("tooltip.rnalifold_settings"));
   }
 
   @Override
@@ -66,6 +100,12 @@ public class RNAalifoldClient extends JabawsAlignCalcWorker implements
   }
 
   @Override
+  boolean checkValidInputSeqs(boolean dynamic, List<FastaSequence> seqs)
+  {
+    return (seqs.size() > 1);
+  }
+
+  @Override
   public void updateResultAnnotation(boolean immediate)
   {
 
@@ -139,11 +179,15 @@ public class RNAalifoldClient extends JabawsAlignCalcWorker implements
      * same data object as was overwritten with the contact probabilites data.
      */
     if (data == null)
+    {
       data = compbio.data.sequence.RNAStructReader
               .newEmptyScore(AlifoldResult.consensusAlignment);
+    }
 
     if (descriptionData == null)
+    {
       descriptionData = data;
+    }
 
     String[] typenameAndDescription = constructTypenameAndDescription(descriptionData
             .first());
@@ -177,7 +221,8 @@ public class RNAalifoldClient extends JabawsAlignCalcWorker implements
   private AlignmentAnnotation constructAnnotationFromScoreHolder(
           AlignmentAnnotation annotation, String struct, TreeSet<Score> data)
   {
-    Annotation[] anns = new Annotation[struct.length()];
+    Annotation[] anns = new Annotation[gapMap != null ? gapMap.length + 1
+            : struct.length()];
 
     if (data != null
             && data.size() > 1
@@ -195,9 +240,17 @@ public class RNAalifoldClient extends JabawsAlignCalcWorker implements
         basePairs.put(score.getRanges().first(), new Float(score
                 .getScores().get(0)));
       }
-      for (int i = 0; i < struct.length(); i++)
-      {
 
+      for (int i = 0, ri = 0, iEnd = struct.length(); i < iEnd; i++, ri++)
+      {
+        if (gapMap != null)
+        {
+          // skip any gapped columns in the input data
+          while (!gapMap[ri])
+          {
+            ri++;
+          }
+        }
         // Return all the contacts associated with position i
         LinkedHashMap<Range, Float> contacts = isContact(basePairs, i + 1);
 
@@ -214,23 +267,36 @@ public class RNAalifoldClient extends JabawsAlignCalcWorker implements
           {
             float t = contacts.get(contact);
             if (t > prob)
+            {
               prob = t;
+            }
             description += Integer.toString(contact.from) + "->"
                     + Integer.toString(contact.to) + ": "
                     + Float.toString(t) + "%  |  ";
           }
         }
 
-        anns[i] = new Annotation(struct.substring(i, i + 1), description,
+        anns[ri] = new Annotation(struct.substring(i, i + 1), description,
                 isSS(struct.charAt(i)), prob);
       }
     }
     else if (data == null || data.size() == 1)
     {
-      for (int i = 0; i < struct.length(); i++)
+      for (int i = 0, ri = 0, iEnd = struct.length(); i < iEnd; i++, ri++)
       {
-
-        anns[i] = new Annotation(struct.substring(i, i + 1), "",
+        if (gapMap != null)
+        {
+          // skip any gapped columns in the input data
+          while (!gapMap[ri] && ri < gapMap.length)
+          {
+            ri++;
+          }
+          if (ri == gapMap.length)
+          {
+            break;
+          }
+        }
+        anns[ri] = new Annotation(struct.substring(i, i + 1), "",
                 isSS(struct.charAt(i)), Float.NaN);
       }
 
@@ -283,7 +349,9 @@ public class RNAalifoldClient extends JabawsAlignCalcWorker implements
                 score.getScores().get(0), score.getScores().get(1));
       }
       else
+      {
         description = "Stochastic Backtrack Structure";
+      }
     }
     else if (datatype.equals(AlifoldResult.MEAStucture.toString()))
     {
@@ -303,8 +371,7 @@ public class RNAalifoldClient extends JabawsAlignCalcWorker implements
       description = typename;
     }
 
-    return new String[]
-    { typename, description };
+    return new String[] { typename, description };
   }
 
   // Check whether, at position i there is a base contact and return all the
@@ -320,7 +387,9 @@ public class RNAalifoldClient extends JabawsAlignCalcWorker implements
       // ordering of the Scores TreeSet in ScoreManager which is, descending
       // probability
       if (contact.from == i || contact.to == i)
+      {
         contacts.put(contact, basePairs.get(contact));
+      }
     }
 
     return contacts;
@@ -332,10 +401,4 @@ public class RNAalifoldClient extends JabawsAlignCalcWorker implements
     char ss = (Pattern.matches(regex, Character.toString(chr))) ? 'S' : ' ';
     return ss;
   }
-
-  public String getCalcId()
-  {
-    return SequenceAnnotationWSClient.AAConCalcId;
-  }
-
 }