Merge branch 'alpha/JAL-3362_Jalview_212_alpha' into alpha/merge_212_JalviewJS_2112
[jalview.git] / src / jalview / ws / jws2 / jabaws2 / RNAalifoldClient.java
  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
  * The Jalview Authors are detailed in the 'AUTHORS' file.
  */
-package jalview.ws.jws2;
+package jalview.ws.jws2.jabaws2;
 
+import jalview.api.FeatureColourI;
 import jalview.datamodel.AlignmentAnnotation;
+import jalview.datamodel.AlignmentI;
 import jalview.datamodel.Annotation;
-import jalview.gui.AlignFrame;
+import jalview.datamodel.features.FeatureMatcherSetI;
 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;
 import java.util.LinkedHashMap;
 import java.util.List;
+import java.util.Map;
 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;
 
 /**
  * Client for the JABA RNA Alifold Service
@@ -49,29 +48,10 @@ import compbio.metadata.Argument;
  * 
  */
 
-public class RNAalifoldClient extends JabawsCalcWorker
+public class RNAalifoldClient extends JabawsAnnotationInstance
 {
 
-  String methodName;
-
-  AlignFrame af;
-
-  // keeps track of whether the RNAalifold result includes base contact
-  // probabilities
-  boolean bpScores;
-
-  public RNAalifoldClient(Jws2Instance sh, AlignFrame alignFrame,
-          WsParamSetI preset, List<Argument> paramset)
-  {
-    super(sh, alignFrame, preset, paramset);
-    af = alignFrame;
-    methodName = sh.serviceType;
-    alignedSeqs = true;
-    submitGaps = true;
-    nucleotidesAllowed = true;
-    proteinAllowed = false;
-    initViewportParams();
-  }
+  // configuration
 
   @Override
   public String getCalcId()
@@ -81,93 +61,89 @@ public class RNAalifoldClient extends JabawsCalcWorker
 
   private static String CALC_ID = "jalview.ws.jws2.RNAalifoldClient";
 
-  public static AlignAnalysisUIText getAlignAnalysisUITest()
+  public static AlignAnalysisUIText getAlignAnalysisUIText()
   {
     return new AlignAnalysisUIText(
             compbio.ws.client.Services.RNAalifoldWS.toString(),
-            jalview.ws.jws2.RNAalifoldClient.class, CALC_ID, true, false,
-            true, MessageManager.getString("label.rnalifold_calculations"),
+            jalview.ws.jws2.jabaws2.RNAalifoldClient.class, CALC_ID, true,
+            false, true, true, false, 2,
+            MessageManager.getString("label.rnalifold_calculations"),
             MessageManager.getString("tooltip.rnalifold_calculations"),
             MessageManager.getString("label.rnalifold_settings"),
             MessageManager.getString("tooltip.rnalifold_settings"));
   }
 
-  @Override
-  public String getServiceActionText()
+  public static String getServiceActionText()
   {
     return "Submitting RNA alignment for Secondary Structure prediction using "
             + "RNAalifold Service";
   }
 
-  @Override
-  boolean checkValidInputSeqs(boolean dynamic, List<FastaSequence> seqs)
+  // instance
+
+  public RNAalifoldClient(Jws2Instance handle)
   {
-    return (seqs.size() > 1);
+    super(handle);
   }
 
   @Override
-  public void updateResultAnnotation(boolean immediate)
+  List<AlignmentAnnotation> annotationFromScoreManager(AlignmentI seqs,
+          Map<String, FeatureColourI> featureColours,
+          Map<String, FeatureMatcherSetI> featureFilters)
   {
-
-    if (immediate || !calcMan.isWorking(this) && scoremanager != null)
+    List<AlignmentAnnotation> ourAnnot = new ArrayList<>();
+
+    // Unpack the ScoreManager
+    List<String> structs = ((RNAStructScoreManager) scoremanager)
+            .getStructs();
+    List<TreeSet<Score>> data = ((RNAStructScoreManager) scoremanager)
+            .getData();
+
+    // test to see if this data object contains base pair contacts
+    Score fscore = data.get(0).first();
+    boolean bpScores = (fscore.getMethod()
+            .equals(AlifoldResult.contactProbabilities.toString()));
+
+    // add annotation for the consensus sequence alignment
+    createAnnotationRowforScoreHolder(seqs, null,
+            ourAnnot,
+            getCalcId(), structs.get(0), null, null);
+
+    // Add annotations for the mfe Structure
+    createAnnotationRowforScoreHolder(seqs, null,
+            ourAnnot,
+            getCalcId(), structs.get(1), data.get(1), null);
+
+    // decide whether to add base pair contact probability histogram
+    int count = 2;
+    if (bpScores)
     {
+      createAnnotationRowforScoreHolder(seqs, null,
+              ourAnnot,
+              getCalcId(), structs.get(2), data.get(0), data.get(2));
+      count++;
+    }
 
-      List<AlignmentAnnotation> ourAnnot = new ArrayList<AlignmentAnnotation>();
-
-      // Unpack the ScoreManager
-      List<String> structs = ((RNAStructScoreManager) scoremanager)
-              .getStructs();
-      List<TreeSet<Score>> data = ((RNAStructScoreManager) scoremanager)
-              .getData();
-
-      // test to see if this data object contains base pair contacts
-      Score fscore = data.get(0).first();
-      this.bpScores = (fscore.getMethod()
-              .equals(AlifoldResult.contactProbabilities.toString()));
-
-      // add annotation for the consensus sequence alignment
-      createAnnotationRowforScoreHolder(ourAnnot, getCalcId(),
-              structs.get(0), null, null);
-
-      // Add annotations for the mfe Structure
-      createAnnotationRowforScoreHolder(ourAnnot, getCalcId(),
-              structs.get(1), data.get(1), null);
-
-      // decide whether to add base pair contact probability histogram
-      int count = 2;
-      if (bpScores)
-      {
-        createAnnotationRowforScoreHolder(ourAnnot, getCalcId(),
-                structs.get(2), data.get(0), data.get(2));
-        count++;
-      }
-
-      // Now loop for the rest of the Annotations (if there it isn't stochastic
-      // output
-      // only the centroid and MEA structures remain anyway)
-      for (int i = count; i < structs.size(); i++)
-      {
-        // The ensemble values should be displayed in the description of the
-        // first (or all?) Stochastic Backtrack Structures.
-        if (!data.get(i).first().getMethod()
-                .equals(AlifoldResult.ensembleValues.toString()))
-        {
-
-          createAnnotationRowforScoreHolder(ourAnnot, getCalcId(),
-                  structs.get(i), data.get(i), null);
-        }
-      }
-
-      if (ourAnnot.size() > 0)
+    // Now loop for the rest of the Annotations (if there it isn't stochastic
+    // output
+    // only the centroid and MEA structures remain anyway)
+    for (int i = count; i < structs.size(); i++)
+    {
+      // The ensemble values should be displayed in the description of the
+      // first (or all?) Stochastic Backtrack Structures.
+      if (!data.get(i).first().getMethod()
+              .equals(AlifoldResult.ensembleValues.toString()))
       {
 
-        updateOurAnnots(ourAnnot);
-        ap.adjustAnnotationHeight();
+        createAnnotationRowforScoreHolder(seqs, null, ourAnnot,
+                getCalcId(), structs.get(i), data.get(i), null);
       }
     }
+    return ourAnnot;
   }
 
-  protected void createAnnotationRowforScoreHolder(
+  private static void createAnnotationRowforScoreHolder(
+          AlignmentI alignment, boolean[] gapMap,
           List<AlignmentAnnotation> ourAnnot, String calcId, String struct,
           TreeSet<Score> data, TreeSet<Score> descriptionData)
   {
@@ -194,10 +170,10 @@ public class RNAalifoldClient extends JabawsCalcWorker
     String typename = typenameAndDescription[0];
     String description = typenameAndDescription[1];
 
-    AlignmentAnnotation annotation = alignViewport.getAlignment()
+    AlignmentAnnotation annotation = alignment
             .findOrCreateAnnotation(typename, calcId, false, null, null);
 
-    constructAnnotationFromScoreHolder(annotation, struct, data);
+    constructAnnotationFromScoreHolder(gapMap, annotation, struct, data);
 
     /*
      * update annotation description with the free Energy, frequency in ensemble
@@ -211,15 +187,12 @@ public class RNAalifoldClient extends JabawsCalcWorker
 
     annotation.belowAlignment = false;
     // annotation.showAllColLabels = true;
-
-    alignViewport.getAlignment().validateAnnotation(annotation);
-    af.setMenusForViewport();
-
+    annotation.validateRangeAndDisplay();
     ourAnnot.add(annotation);
   }
 
-  private AlignmentAnnotation constructAnnotationFromScoreHolder(
-          AlignmentAnnotation annotation, String struct,
+  private static AlignmentAnnotation constructAnnotationFromScoreHolder(
+          boolean[] gapMap, AlignmentAnnotation annotation, String struct,
           TreeSet<Score> data)
   {
     Annotation[] anns = new Annotation[gapMap != null ? gapMap.length + 1
@@ -231,7 +204,7 @@ public class RNAalifoldClient extends JabawsCalcWorker
 
       // The base pair probabilities are stored in a set in scoreholder. we want
       // a map
-      LinkedHashMap<Range, Float> basePairs = new LinkedHashMap<Range, Float>();
+      LinkedHashMap<Range, Float> basePairs = new LinkedHashMap<>();
       for (Score score : data)
       {
         // The Score objects contain a set of size one containing the range and
@@ -307,7 +280,7 @@ public class RNAalifoldClient extends JabawsCalcWorker
     return annotation;
   }
 
-  private String[] constructTypenameAndDescription(Score score)
+  private static String[] constructTypenameAndDescription(Score score)
   {
     String description = "";
     String typename = "";
@@ -374,10 +347,10 @@ public class RNAalifoldClient extends JabawsCalcWorker
 
   // Check whether, at position i there is a base contact and return all the
   // contacts at this position. Should be in order of descending probability.
-  private LinkedHashMap<Range, Float> isContact(
+  private static LinkedHashMap<Range, Float> isContact(
           LinkedHashMap<Range, Float> basePairs, int i)
   {
-    LinkedHashMap<Range, Float> contacts = new LinkedHashMap<Range, Float>();
+    LinkedHashMap<Range, Float> contacts = new LinkedHashMap<>();
 
     for (Range contact : basePairs.keySet())
     {
@@ -393,10 +366,11 @@ public class RNAalifoldClient extends JabawsCalcWorker
     return contacts;
   }
 
-  private char isSS(char chr)
+  private static char isSS(char chr)
   {
     String regex = "\\(|\\)|\\{|\\}|\\[|\\]";
     char ss = (Pattern.matches(regex, Character.toString(chr))) ? 'S' : ' ';
     return ss;
   }
+
 }