Merge remote-tracking branch 'origin/tasks/JAL-3070_wsinterfaces' into alpha/JAL...
authorMateusz Warowny <mmzwarowny@dundee.ac.uk>
Fri, 27 Sep 2019 10:58:29 +0000 (11:58 +0100)
committerMateusz Warowny <mmzwarowny@dundee.ac.uk>
Fri, 27 Sep 2019 10:58:29 +0000 (11:58 +0100)
19 files changed:
src/jalview/analysis/AlignmentAnnotationUtils.java
src/jalview/api/FeatureSettingsModelI.java
src/jalview/datamodel/Alignment.java
src/jalview/datamodel/AlignmentAnnotation.java
src/jalview/datamodel/AlignmentI.java
src/jalview/datamodel/Sequence.java
src/jalview/datamodel/SequenceI.java
src/jalview/gui/AlignViewport.java
src/jalview/schemes/FeatureSettingsAdapter.java
src/jalview/ws/api/SequenceAnnotationServiceI.java
src/jalview/ws/jws2/SeqAnnotationServiceCalcWorker.java
src/jalview/ws/jws2/jabaws2/AAConClient.java
src/jalview/ws/jws2/jabaws2/AADisorderClient.java
src/jalview/ws/jws2/jabaws2/JabawsAnnotationInstance.java
src/jalview/ws/jws2/jabaws2/RNAalifoldClient.java
src/jalview/ws/slivkaws/SlivkaAnnotationServiceInstance.java
test/jalview/datamodel/AlignmentAnnotationTests.java
test/jalview/datamodel/AlignmentTest.java
test/jalview/datamodel/SequenceTest.java

index f5626ce..5c2d936 100644 (file)
@@ -75,11 +75,11 @@ public class AlignmentAnnotationUtils
      * Build a lookup, by calcId (annotation source), of all annotation types in
      * each graph group.
      */
-    Map<String, Map<Integer, List<String>>> groupLabels = new HashMap<String, Map<Integer, List<String>>>();
+    Map<String, Map<Integer, List<String>>> groupLabels = new HashMap<>();
 
     // trackers for which calcId!label combinations we have dealt with
-    List<String> addedToShown = new ArrayList<String>();
-    List<String> addedToHidden = new ArrayList<String>();
+    List<String> addedToShown = new ArrayList<>();
+    List<String> addedToHidden = new ArrayList<>();
 
     for (AlignmentAnnotation aa : annotations)
     {
@@ -99,7 +99,7 @@ public class AlignmentAnnotationUtils
         /*
          * Build a 'composite label' for types in line graph groups.
          */
-        final List<String> labelAsList = new ArrayList<String>();
+        final List<String> labelAsList = new ArrayList<>();
         final String displayLabel = aa.label;
         labelAsList.add(displayLabel);
         if (aa.graph == AlignmentAnnotation.LINE_GRAPH
@@ -239,4 +239,42 @@ public class AlignmentAnnotationUtils
     return (anns == null ? Collections.<AlignmentAnnotation> emptyList()
             : Arrays.asList(anns));
   }
+
+  /**
+   * replace an existing sequence associated annotation with another, creating
+   * association as necessary.
+   * 
+   * @param newAnnot
+   *          - annotation row associated with a sequence to be propagated to
+   *          its reference annotation
+   * @param typeName
+   *          - label used to match existing row
+   * @param calcId
+   *          - calcId for existing row
+   */
+  public static void replaceAnnotationOnAlignmentWith(
+          AlignmentAnnotation newAnnot, String typeName, String calcId)
+  {
+    if (newAnnot.sequenceRef != null)
+    {
+      SequenceI dsseq = newAnnot.sequenceRef;
+      while (dsseq.getDatasetSequence() != null)
+      {
+        dsseq = dsseq.getDatasetSequence();
+      }
+      // look for same annotation on dataset and lift this one over
+      List<AlignmentAnnotation> dsan = dsseq.getAlignmentAnnotations(calcId,
+              typeName);
+      if (dsan != null && dsan.size() > 0)
+      {
+        for (AlignmentAnnotation dssan : dsan)
+        {
+          dsseq.removeAlignmentAnnotation(dssan);
+        }
+      }
+      AlignmentAnnotation dssan = new AlignmentAnnotation(newAnnot);
+      dsseq.addAlignmentAnnotation(dssan);
+      dssan.adjustForAlignment();
+    }
+  }
 }
index c0fc523..5e910dc 100644 (file)
@@ -20,6 +20,8 @@
  */
 package jalview.api;
 
+import jalview.datamodel.features.FeatureMatcherSetI;
+
 import java.util.Comparator;
 
 /**
@@ -60,6 +62,15 @@ public interface FeatureSettingsModelI extends Comparator<String>
   FeatureColourI getFeatureColour(String type);
 
   /**
+   * Returns any filters defined for the feature type, or null if not known
+   * 
+   * @param type
+   * @return
+   */
+
+  FeatureMatcherSetI getFeatureFilters(String type);
+
+  /**
    * Returns the transparency value, from 0 (fully transparent) to 1 (fully
    * opaque)
    * 
index 70a3cc2..d124960 100755 (executable)
@@ -1628,34 +1628,50 @@ public class Alignment implements AlignmentI, AutoCloseable
           String calcId, boolean autoCalc, SequenceI seqRef,
           SequenceGroup groupRef)
   {
-    if (annotations != null)
+    AlignmentAnnotation annot = annotations == null ? null
+            : AlignmentAnnotation.findFirstAnnotation(
+              Arrays.asList(getAlignmentAnnotation()), name, calcId,
+              autoCalc, seqRef, groupRef);
+
+    if (annot == null)
     {
-      for (AlignmentAnnotation annot : getAlignmentAnnotation())
+
+      annot = new AlignmentAnnotation(name, name, new Annotation[1], 0f, 0f,
+              AlignmentAnnotation.BAR_GRAPH);
+      annot.hasText = false;
+      if (calcId != null)
       {
-        if (annot.autoCalculated == autoCalc && (name.equals(annot.label))
-                && (calcId == null || annot.getCalcId().equals(calcId))
-                && annot.sequenceRef == seqRef
-                && annot.groupRef == groupRef)
-        {
-          return annot;
-        }
+        annot.setCalcId(calcId);
+      }
+      annot.autoCalculated = autoCalc;
+      if (seqRef != null)
+      {
+        annot.setSequenceRef(seqRef);
       }
+      annot.groupRef = groupRef;
+      addAnnotation(annot);
     }
-    AlignmentAnnotation annot = new AlignmentAnnotation(name, name,
-            new Annotation[1], 0f, 0f, AlignmentAnnotation.BAR_GRAPH);
-    annot.hasText = false;
-    if (calcId != null)
+    return annot;
+  }
+
+
+  @Override
+  public AlignmentAnnotation updateFromOrCopyAnnotation(
+          AlignmentAnnotation ala)
+  {
+    AlignmentAnnotation annot = AlignmentAnnotation.findFirstAnnotation(
+            Arrays.asList(getAlignmentAnnotation()), ala.label, ala.calcId,
+            ala.autoCalculated, ala.sequenceRef, ala.groupRef);
+    if (annot == null)
     {
-      annot.setCalcId(calcId);
+      annot = new AlignmentAnnotation(ala);
+      addAnnotation(annot);
     }
-    annot.autoCalculated = autoCalc;
-    if (seqRef != null)
+    else
     {
-      annot.setSequenceRef(seqRef);
+      annot.updateAlignmentAnnotationFrom(ala);
     }
-    annot.groupRef = groupRef;
-    addAnnotation(annot);
-
+    validateAnnotation(annot);
     return annot;
   }
 
index 3e532df..4ba0ac4 100755 (executable)
@@ -710,6 +710,17 @@ public class AlignmentAnnotation
   public AlignmentAnnotation(AlignmentAnnotation annotation)
   {
     setAnnotationId();
+    updateAlignmentAnnotationFrom(annotation);
+  }
+
+  /**
+   * copy attributes and annotation from an existing annotation (used by copy
+   * constructor). This method does not update the unique annotationId
+   * 
+   * @param annotation
+   */
+  public void updateAlignmentAnnotationFrom(AlignmentAnnotation annotation)
+  {
     this.label = new String(annotation.label);
     if (annotation.description != null)
     {
@@ -1741,4 +1752,22 @@ public class AlignmentAnnotation
     this.eValue = eValue;
   }
 
+  public static AlignmentAnnotation findFirstAnnotation(
+          Iterable<AlignmentAnnotation> alignmentAnnotation, String name,
+          String calcId, boolean autoCalc, SequenceI seqRef,
+          SequenceGroup groupRef)
+  {
+
+    for (AlignmentAnnotation annot : alignmentAnnotation)
+    {
+      if (annot.autoCalculated == autoCalc && (name.equals(annot.label))
+              && (calcId == null || annot.getCalcId().equals(calcId))
+              && annot.sequenceRef == seqRef && annot.groupRef == groupRef)
+      {
+        return annot;
+      }
+    }
+    return null;
+  }
+
 }
index 7386d4b..cea1c5b 100755 (executable)
@@ -525,6 +525,17 @@ public interface AlignmentI extends AnnotatedCollectionI
           boolean autoCalc, SequenceI seqRef, SequenceGroup groupRef);
 
   /**
+   * like findOrCreateAnnotation - looks for an existing alignment annotation
+   * row with matching name, calcId, sequenceRef, groupRef and autoCalculated
+   * flag and updates it from the annotation. If none is found the annotation is
+   * added directly.
+   * 
+   * @param ala
+   * @return ala or the annotation row that was updated.
+   */
+  AlignmentAnnotation updateFromOrCopyAnnotation(AlignmentAnnotation ala);
+
+  /**
    * move the given group up or down in the alignment by the given number of
    * rows. Implementor assumes given group is already present on alignment - no
    * recalculations are triggered.
index 9ba768d..5008a3e 100755 (executable)
@@ -1052,7 +1052,8 @@ public class Sequence extends ASequence implements SequenceI
   @Override
   public ContiguousI findPositions(int fromColumn, int toColumn)
   {
-    if (toColumn < fromColumn || fromColumn < 1)
+    fromColumn = Math.max(fromColumn, 1);
+    if (toColumn < fromColumn)
     {
       return null;
     }
index d06adcb..5cddade 100755 (executable)
@@ -217,10 +217,10 @@ public interface SequenceI extends ASequenceI
    * from 1), or null if no residues are included in the range
    * 
    * @param fromColum
-   *          - first column base 1
+   *          - first column base 1. (0 and negative positions are rounded up)
    * @param toColumn
    *          - last column, base 1
-   * @return
+   * @return null if fromColum>toColumn
    */
   public ContiguousI findPositions(int fromColum, int toColumn);
 
index d6e2ae5..e60b76f 100644 (file)
@@ -39,6 +39,7 @@ import jalview.datamodel.SearchResults;
 import jalview.datamodel.SearchResultsI;
 import jalview.datamodel.SequenceGroup;
 import jalview.datamodel.SequenceI;
+import jalview.datamodel.features.FeatureMatcherSetI;
 import jalview.renderer.ResidueShader;
 import jalview.schemes.ColourSchemeI;
 import jalview.schemes.ColourSchemeProperty;
@@ -1053,6 +1054,9 @@ public void setNormaliseSequenceLogo(boolean state)
     {
       FeatureColourI preferredColour = featureSettings
               .getFeatureColour(type);
+      FeatureMatcherSetI preferredFilters = featureSettings
+              .getFeatureFilters(type);
+
       FeatureColourI origColour = fr.getFeatureStyle(type);
       if (!mergeOnly || (!origRenderOrder.contains(type)
               || origColour == null
@@ -1067,6 +1071,11 @@ public void setNormaliseSequenceLogo(boolean state)
         {
           fr.setColour(type, preferredColour);
         }
+        if (preferredFilters != null
+                && (!mergeOnly || fr.getFeatureFilter(type) != null))
+        {
+          fr.setFeatureFilter(type, preferredFilters);
+        }
         if (featureSettings.isFeatureDisplayed(type))
         {
           displayed.setVisible(type);
index b15e4cf..3d82f9b 100644 (file)
@@ -22,6 +22,7 @@ package jalview.schemes;
 
 import jalview.api.FeatureColourI;
 import jalview.api.FeatureSettingsModelI;
+import jalview.datamodel.features.FeatureMatcherSetI;
 
 /**
  * An adapter class that may be extended to instantiate feature colour schemes
@@ -65,4 +66,10 @@ public class FeatureSettingsAdapter implements FeatureSettingsModelI
     return false;
   }
 
+  @Override
+  public FeatureMatcherSetI getFeatureFilters(String type)
+  {
+    return null;
+  }
+
 }
index a224bac..9ca2d31 100644 (file)
@@ -1,13 +1,14 @@
 package jalview.ws.api;
 
+import jalview.api.FeatureColourI;
 import jalview.datamodel.AlignmentAnnotation;
 import jalview.datamodel.SequenceI;
-import jalview.ws.gui.AnnotationWsJob;
-import jalview.ws.jws2.SeqAnnotationServiceCalcWorker;
+import jalview.datamodel.features.FeatureMatcherSetI;
 import jalview.ws.params.ArgumentI;
 import jalview.ws.params.WsParamSetI;
 
 import java.util.List;
+import java.util.Map;
 
 public interface SequenceAnnotationServiceI extends JalviewWebServiceI
 {
@@ -27,14 +28,24 @@ public interface SequenceAnnotationServiceI extends JalviewWebServiceI
           List<ArgumentI> paramset) throws Throwable;
 
   /**
-   * todo: move to SequenceAnnotationResult
+   * materialise annotation and features for sequences input to the service
    * 
-   * @param running
-   * @param abstractJabaCalcWorker
-   * @return
+   * @param job
+   * @param seqs
+   *          - features and alignment annotation added to these will be
+   *          imported to the dataset for the alignment
+   * @param featureColours
+   *          - container for feature colours - any defined will be merged with
+   *          viewport
+   * @param featureFilters
+   *          - container for filters - any defined will be merged with viewport
+   * @return sequence and alignment annotation rows that should be made
+   *         visible/updated on alignment
+   * @throws Throwable
    */
-  List<AlignmentAnnotation> getAlignmentAnnotation(AnnotationWsJob running,
-          SeqAnnotationServiceCalcWorker abstractJabaCalcWorker) throws Throwable;
-
+  List<AlignmentAnnotation> getAnnotationResult(JobId job,
+          List<SequenceI> seqs, Map<String, FeatureColourI> featureColours,
+          Map<String, FeatureMatcherSetI> featureFilters) throws Throwable;
+  
 
 }
index 6ecc01f..6279ccd 100644 (file)
 package jalview.ws.jws2;
 
 import jalview.analysis.AlignSeq;
+import jalview.analysis.AlignmentAnnotationUtils;
 import jalview.analysis.SeqsetUtils;
 import jalview.api.AlignViewportI;
 import jalview.api.AlignmentViewPanel;
+import jalview.api.FeatureColourI;
 import jalview.bin.Cache;
 import jalview.datamodel.AlignmentAnnotation;
 import jalview.datamodel.AlignmentI;
 import jalview.datamodel.AnnotatedCollectionI;
+import jalview.datamodel.Annotation;
+import jalview.datamodel.ContiguousI;
+import jalview.datamodel.Mapping;
 import jalview.datamodel.SequenceI;
+import jalview.datamodel.features.FeatureMatcherSetI;
 import jalview.gui.AlignFrame;
 import jalview.gui.Desktop;
 import jalview.gui.IProgressIndicator;
 import jalview.gui.IProgressIndicatorHandler;
 import jalview.gui.JvOptionPane;
+import jalview.schemes.FeatureSettingsAdapter;
 import jalview.schemes.ResidueProperties;
+import jalview.util.MapList;
 import jalview.util.MessageManager;
 import jalview.workers.AlignCalcWorker;
 import jalview.ws.api.CancellableI;
@@ -191,6 +199,7 @@ public class SeqAnnotationServiceCalcWorker extends AlignCalcWorker
     bySequence = !service.isAlignmentAnalysis();
     filterNonStandardResidues = service.isFilterSymbols();
     min_valid_seqs = service.getMinimumInputSequences();
+    submitGaps = service.isAlignmentAnalysis();
 
     if (service.isInteractiveUpdate())
     {
@@ -247,8 +256,6 @@ public class SeqAnnotationServiceCalcWorker extends AlignCalcWorker
         return;
       }
 
-      AlignmentAnnotation[] aa = alignViewport.getAlignment()
-              .getAlignmentAnnotation();
       if (guiProgress != null)
       {
         guiProgress.setProgressBar(service.getActionText(),
@@ -298,7 +305,6 @@ public class SeqAnnotationServiceCalcWorker extends AlignCalcWorker
       // and poll for updates until job finishes, fails or becomes stale
       
       boolean finished = false;
-      long rpos = 0;
       do
       {
         Cache.log.debug("Updating status for annotation service.");
@@ -326,11 +332,11 @@ public class SeqAnnotationServiceCalcWorker extends AlignCalcWorker
               if (cancellable
                         && ((CancellableI) annotService).cancel(running))
               {
-                System.err.println("Cancelled AACon job: " + rslt);
+                System.err.println("Cancelled job: " + rslt);
               }
               else
               {
-                System.err.println("FAILED TO CANCEL AACon job: " + rslt);
+                System.err.println("FAILED TO CANCEL job: " + rslt);
               }
   
             } catch (Exception x)
@@ -388,10 +394,23 @@ public class SeqAnnotationServiceCalcWorker extends AlignCalcWorker
       running.setFeatureRenderer(
               ((jalview.gui.AlignmentPanel) ap).cloneFeatureRenderer());
       Cache.log.debug("retrieving job results.");
+      final Map<String, FeatureColourI> featureColours = new HashMap<>();
+      final Map<String, FeatureMatcherSetI> featureFilters = new HashMap<>();
       List<AlignmentAnnotation> returnedAnnot = annotService
-              .getAlignmentAnnotation(running, this);
+              .getAnnotationResult(running.getJobHandle(), seqs,
+                      featureColours, featureFilters);
+
       Cache.log.debug("Obtained " + (returnedAnnot == null ? "no rows"
               : ("" + returnedAnnot.size())));
+      Cache.log.debug("There were " + featureColours.size()
+              + " feature colours and " + featureFilters.size()
+              + " filters defined.");
+
+      // TODO
+      // copy over each annotation row reurned and also defined on each
+      // sequence, excluding regions not annotated due to gapMap/column
+      // visibility
+
       running.setAnnotation(returnedAnnot);
 
       if (running.hasResults())
@@ -401,19 +420,42 @@ public class SeqAnnotationServiceCalcWorker extends AlignCalcWorker
         updateResultAnnotation(true);
         if (running.isTransferSequenceFeatures())
         {
+          // TODO
+          // look at each sequence and lift over any features, excluding regions
+          // not annotated due to gapMap/column visibility
+
           jalview.bin.Cache.log.debug(
                   "Updating feature display settings and transferring features from Job "
                           + rslt + " at " + service.getUri());
-          ((jalview.gui.AlignmentPanel) ap)
-                  .updateFeatureRendererFrom(running.getFeatureRenderer());
+          // TODO: consider merge rather than apply here
+          alignViewport.applyFeaturesStyle(new FeatureSettingsAdapter()
+          {
+            @Override
+            public FeatureColourI getFeatureColour(String type)
+            {
+              return featureColours.get(type);
+            }
+
+            @Override
+            public FeatureMatcherSetI getFeatureFilters(String type)
+            {
+              return featureFilters.get(type);
+            }
+
+            @Override
+            public boolean isFeatureDisplayed(String type)
+            {
+              return featureColours.containsKey(type);
+            }
+
+          });
           // TODO: JAL-1150 - create sequence feature settings API for defining
           // styles and enabling/disabling feature overlay on alignment panel
 
           if (alignFrame.alignPanel == ap)
           {
-            // only do this if the alignFrame is currently showing this view.
-            Desktop.getAlignFrameFor(alignViewport)
-                    .setShowSeqFeatures(true);
+            alignViewport.setShowSequenceFeatures(true);
+            alignFrame.setMenusForViewport();
           }
         }
         ap.adjustAnnotationHeight();
@@ -680,8 +722,137 @@ public class SeqAnnotationServiceCalcWorker extends AlignCalcWorker
     if ((immediate || !calcMan.isWorking(this)) && running != null
             && running.hasResults())
     {
-      List<AlignmentAnnotation> ourAnnot = running.getAnnotation();
-      updateOurAnnots(ourAnnot);
+      List<AlignmentAnnotation> ourAnnot = running.getAnnotation(),
+              newAnnots = new ArrayList<>();
+      //
+      // update graphGroup for all annotation
+      //
+      /**
+       * find a graphGroup greater than any existing ones this could be a method
+       * provided by alignment Alignment.getNewGraphGroup() - returns next
+       * unused graph group
+       */
+      int graphGroup = 1;
+      if (alignViewport.getAlignment().getAlignmentAnnotation() != null)
+      {
+        for (AlignmentAnnotation ala : alignViewport.getAlignment()
+                .getAlignmentAnnotation())
+        {
+          if (ala.graphGroup > graphGroup)
+          {
+            graphGroup = ala.graphGroup;
+          }
+        }
+      }
+      /**
+       * update graphGroup in the annotation rows returned from service
+       */
+      // TODO: look at sequence annotation rows and update graph groups in the
+      // case of reference annotation.
+      for (AlignmentAnnotation ala : ourAnnot)
+      {
+        if (ala.graphGroup > 0)
+        {
+          ala.graphGroup += graphGroup;
+        }
+        SequenceI aseq = null;
+
+        /**
+         * transfer sequence refs and adjust gapmap
+         */
+        if (ala.sequenceRef != null)
+        {
+          SequenceI seq = running.getSeqNames()
+                  .get(ala.sequenceRef.getName());
+          aseq = seq;
+          while (seq.getDatasetSequence() != null)
+          {
+            seq = seq.getDatasetSequence();
+          }
+        }
+        Annotation[] resAnnot = ala.annotations,
+                gappedAnnot = new Annotation[Math.max(
+                        alignViewport.getAlignment().getWidth(),
+                        gapMap.length)];
+        for (int p = 0, ap = start; ap < gappedAnnot.length; ap++)
+        {
+          if (gapMap != null && gapMap.length > ap && !gapMap[ap])
+          {
+            gappedAnnot[ap] = new Annotation("", "", ' ', Float.NaN);
+          }
+          else if (p < resAnnot.length)
+          {
+            gappedAnnot[ap] = resAnnot[p++];
+          }
+        }
+        ala.sequenceRef = aseq;
+        ala.annotations = gappedAnnot;
+        AlignmentAnnotation newAnnot = getAlignViewport().getAlignment()
+                .updateFromOrCopyAnnotation(ala);
+        if (aseq != null)
+        {
+
+          aseq.addAlignmentAnnotation(newAnnot);
+          newAnnot.adjustForAlignment();
+
+          AlignmentAnnotationUtils.replaceAnnotationOnAlignmentWith(
+                  newAnnot, newAnnot.label, newAnnot.getCalcId());
+        }
+        newAnnots.add(newAnnot);
+
+      }
+      for (SequenceI sq : running.getSeqs())
+      {
+        if (!sq.getFeatures().hasFeatures()
+                && (sq.getDBRefs() == null || sq.getDBRefs().length == 0))
+        {
+          continue;
+        }
+        running.setTransferSequenceFeatures(true);
+        SequenceI seq = running.getSeqNames().get(sq.getName());
+        SequenceI dseq;
+        ContiguousI seqRange = seq.findPositions(start, end);
+
+        while ((dseq = seq).getDatasetSequence() != null)
+        {
+          seq = seq.getDatasetSequence();
+        }
+        List<ContiguousI> sourceRange = new ArrayList();
+        if (gapMap != null && gapMap.length >= end)
+        {
+          int lastcol = start, col = start;
+          do
+          {
+            if (col == end || !gapMap[col])
+            {
+              if (lastcol <= (col - 1))
+              {
+                seqRange = seq.findPositions(lastcol, col);
+                sourceRange.add(seqRange);
+              }
+              lastcol = col + 1;
+            }
+          } while (++col <= end);
+        }
+        else
+        {
+          sourceRange.add(seq.findPositions(start, end));
+        }
+        int i = 0;
+        int source_startend[] = new int[sourceRange.size() * 2];
+
+        for (ContiguousI range : sourceRange)
+        {
+          source_startend[i++] = range.getBegin();
+          source_startend[i++] = range.getEnd();
+        }
+        Mapping mp = new Mapping(
+                new MapList(source_startend, new int[]
+                { seq.getStart(), seq.getEnd() }, 1, 1));
+        dseq.transferAnnotation(sq, mp);
+
+      }
+      updateOurAnnots(newAnnots);
     }
   }
 
@@ -741,15 +912,17 @@ public class SeqAnnotationServiceCalcWorker extends AlignCalcWorker
         }
       }
       our.clear();
-      // validate rows and update Alignmment state
-      for (AlignmentAnnotation an : ourAnnots)
-      {
-        alignViewport.getAlignment().validateAnnotation(an);
-      }
-      // TODO: may need a menu refresh after this
-      // af.setMenusForViewport();
-      ap.adjustAnnotationHeight();
     }
+
+    // validate rows and update Alignmment state
+    for (AlignmentAnnotation an : ourAnnots)
+    {
+      alignViewport.getAlignment().validateAnnotation(an);
+    }
+    // TODO: may need a menu refresh after this
+    // af.setMenusForViewport();
+    ap.adjustAnnotationHeight();
+
   }
 
   public SequenceAnnotationServiceI getService()
index f0a4b67..18b6ce1 100644 (file)
  */
 package jalview.ws.jws2.jabaws2;
 
-import jalview.api.AlignViewportI;
+import jalview.api.FeatureColourI;
 import jalview.datamodel.AlignmentAnnotation;
+import jalview.datamodel.AlignmentI;
+import jalview.datamodel.features.FeatureMatcherSetI;
 import jalview.util.MessageManager;
-import jalview.ws.gui.AnnotationWsJob;
 import jalview.ws.uimodel.AlignAnalysisUIText;
 
 import java.util.ArrayList;
@@ -64,13 +65,17 @@ public class AAConClient extends JabawsAnnotationInstance
   }
 
   @Override
-  List<AlignmentAnnotation> annotationFromScoreManager(
-          AnnotationWsJob running, AlignViewportI alignViewport,
-          boolean[] gapMap)
+  List<AlignmentAnnotation> annotationFromScoreManager(AlignmentI seqs,
+          Map<String, FeatureColourI> featureColours,
+          Map<String, FeatureMatcherSetI> featureFilters)
   {
+    return aacons_annotation(seqs.getWidth(), seqs, null);
+  }
 
+  private List<AlignmentAnnotation> aacons_annotation(int alWidth,
+          AlignmentI alignViewport, boolean[] gapMap)
+  {
     Map<String, TreeSet<Score>> scoremap = scoremanager.asMap();
-    int alWidth = alignViewport.getAlignment().getWidth();
     ArrayList<AlignmentAnnotation> ourAnnot = new ArrayList<>();
     for (String score : scoremap.keySet())
     {
@@ -91,8 +96,9 @@ public class AAConClient extends JabawsAnnotationInstance
         }
         else
         {
-          createAnnotationRowsForScores(alignViewport, gapMap, ourAnnot,
-                  getCalcId(), alWidth, scr);
+          createAnnotationRowsForScores(alignViewport, null, ourAnnot,
+                  getCalcId(),
+                  scr.getScores().size(), scr);
         }
       }
     }
index 6e65cce..a221aa0 100644 (file)
  */
 package jalview.ws.jws2.jabaws2;
 
-import jalview.api.AlignViewportI;
+import jalview.analysis.AlignmentAnnotationUtils;
 import jalview.api.FeatureColourI;
 import jalview.bin.Cache;
 import jalview.datamodel.AlignmentAnnotation;
+import jalview.datamodel.AlignmentI;
 import jalview.datamodel.GraphLine;
 import jalview.datamodel.SequenceFeature;
 import jalview.datamodel.SequenceI;
+import jalview.datamodel.features.FeatureMatcherSetI;
 import jalview.schemes.FeatureColour;
 import jalview.util.ColorUtils;
-import jalview.ws.gui.AnnotationWsJob;
 
 import java.awt.Color;
 import java.util.ArrayList;
@@ -61,41 +62,26 @@ public class AADisorderClient extends JabawsAnnotationInstance
   }
 
   @Override
-  List<AlignmentAnnotation> annotationFromScoreManager(
-          AnnotationWsJob running, AlignViewportI alignViewport,
-          boolean[] gapMap)
+  List<AlignmentAnnotation> annotationFromScoreManager(AlignmentI seqs,
+          Map<String, FeatureColourI> featureColours,
+          Map<String, FeatureMatcherSetI> featureFilters)
   {
 
     Map<String, String[]> featureTypeMap = featureMap.get(our.getName());
     Map<String, Map<String, Object>> annotTypeMap = annotMap
             .get(our.getName());
     boolean dispFeatures = false;
-    Map<String, Object> fc = new Hashtable<>();
+    Map<String, SequenceFeature> fc = new Hashtable<>(),
+            fex = new Hashtable();
     List<AlignmentAnnotation> ourAnnot = new ArrayList<>();
-    /**
-     * grouping for any annotation rows created
-     */
-    int graphGroup = 1;
-    if (alignViewport.getAlignment().getAlignmentAnnotation() != null)
-    {
-      for (AlignmentAnnotation ala : alignViewport.getAlignment()
-              .getAlignmentAnnotation())
-      {
-        if (ala.graphGroup > graphGroup)
-        {
-          graphGroup = ala.graphGroup;
-        }
-      }
-    }
+    int graphGroup = 1, lastAnnot = 0;
 
-    // TODO: downgrade code below so we don't annotate sequences directly
-    Map<String, SequenceI> seqNames = running.getSeqNames();
-    int start = running.getStartPos();
-    for (String seqId : seqNames.keySet())
+    for (SequenceI seq : seqs.getSequences())
     {
+      String seqId = seq.getName();
       boolean sameGroup = false;
-      SequenceI dseq, aseq, seq = seqNames.get(seqId);
-      int base = seq.findPosition(start) - 1;
+      SequenceI dseq, aseq;
+      int base = seq.findPosition(0) - 1;
       aseq = seq;
       while ((dseq = seq).getDatasetSequence() != null)
       {
@@ -109,10 +95,10 @@ public class AADisorderClient extends JabawsAnnotationInstance
       {
         Cache.log.info("Couldn't recover disorder prediction for sequence "
                 + seq.getName() + "(Prediction name was " + seqId + ")"
-                + "\nSee http://issues.jalview.org/browse/JAL-1319 for one possible reason why disorder predictions might fail.");
+                + "\nSee http://issues.jalview.org/browse/JAL-1319 for one possible reason why disorder predictions might fail.",
+                q);
       }
       float last = Float.NaN, val = Float.NaN;
-      int lastAnnot = ourAnnot.size();
       if (scores != null && scores.scores != null)
       {
         for (Score scr : scores.scores)
@@ -124,7 +110,7 @@ public class AADisorderClient extends JabawsAnnotationInstance
             // make features on sequence
             for (Range rn : scr.getRanges())
             {
-
+              // TODO: Create virtual feature settings
               SequenceFeature sf;
               String[] type = featureTypeMap.get(scr.getMethod());
               if (type == null)
@@ -146,12 +132,16 @@ public class AADisorderClient extends JabawsAnnotationInstance
                         base + rn.to, methodName);
               }
               dseq.addSequenceFeature(sf);
-              if (last != val && !Float.isNaN(last))
+              // mark feature as requiring a graduated colourscheme if has
+              // variable scores
+              if (!Float.isNaN(last) && !Float.isNaN(val) && last != val)
               {
                 fc.put(sf.getType(), sf);
+              } else 
+              {
+                fex.put(sf.getType(), sf);
               }
               last = val;
-              running.setTransferSequenceFeatures(true);
               dispFeatures = true;
             }
           }
@@ -163,7 +153,7 @@ public class AADisorderClient extends JabawsAnnotationInstance
             }
             String typename, calcName;
             AlignmentAnnotation annot = createAnnotationRowsForScores(
-                    alignViewport, gapMap, ourAnnot,
+                    seqs, null, ourAnnot,
                     typename = our.getName() + " (" + scr.getMethod() + ")",
                     calcName = our.getNameURI() + "/" + scr.getMethod(),
                     aseq, base + 1, scr);
@@ -218,8 +208,8 @@ public class AADisorderClient extends JabawsAnnotationInstance
             }
             annot._linecolour = col;
             // finally, update any dataset annotation
-            replaceAnnotationOnAlignmentWith(annot, typename, calcName,
-                    aseq);
+            AlignmentAnnotationUtils.replaceAnnotationOnAlignmentWith(annot,
+                    typename, calcName);
           }
         }
       }
@@ -232,20 +222,29 @@ public class AADisorderClient extends JabawsAnnotationInstance
     {
       if (dispFeatures)
       {
-        jalview.api.FeatureRenderer fr = running.getFeatureRenderer();
-        for (String ft : fc.keySet())
+        // TODO: virtual feature settings
+        // feature colours need to merged with current viewport's colours
+        // simple feature colours promoted to colour-by-score ranges using
+        // currently assigned or created feature colour
+        for (String ft : fex.keySet())
         {
-          FeatureColourI gc = fr.getFeatureStyle(ft);
-          if (gc.isSimpleColour())
+          Color col = ColorUtils.createColourFromName(ft);
+          // set graduated color as fading to white for minimum, and
+          // autoscaling to values on alignment
+          
+          FeatureColourI ggc;
+          if (fc.get(ft) != null)
           {
-            // set graduated color as fading to white for minimum, and
-            // autoscaling to values on alignment
-            FeatureColourI ggc = new FeatureColour(gc.getColour(),
-                    Color.white, gc.getColour(), Color.white,
-                    Float.MIN_VALUE, Float.MAX_VALUE);
+            ggc = new FeatureColour(col, Color.white, col,
+
+                  Color.white, Float.MIN_VALUE, Float.MAX_VALUE);
             ggc.setAutoScaled(true);
-            fr.setColour(ft, ggc);
           }
+          else
+          {
+            ggc = new FeatureColour(col);
+          }
+          featureColours.put(ft, ggc);
         }
 
       }
index e7f3022..a52b515 100644 (file)
@@ -1,22 +1,24 @@
 package jalview.ws.jws2.jabaws2;
 
-import jalview.api.AlignViewportI;
+import jalview.api.FeatureColourI;
+import jalview.datamodel.Alignment;
 import jalview.datamodel.AlignmentAnnotation;
+import jalview.datamodel.AlignmentI;
 import jalview.datamodel.Annotation;
 import jalview.datamodel.SequenceI;
+import jalview.datamodel.features.FeatureMatcherSetI;
 import jalview.util.MessageManager;
 import jalview.ws.api.JobId;
 import jalview.ws.api.SequenceAnnotationServiceI;
-import jalview.ws.gui.AnnotationWsJob;
 import jalview.ws.jws2.JabaParamStore;
 import jalview.ws.jws2.JabaPreset;
-import jalview.ws.jws2.SeqAnnotationServiceCalcWorker;
 import jalview.ws.params.ArgumentI;
 import jalview.ws.params.WsParamSetI;
 
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Map;
 
 import compbio.data.msa.SequenceAnnotation;
 import compbio.data.sequence.FastaSequence;
@@ -106,37 +108,47 @@ public abstract class JabawsAnnotationInstance
     return new JobId(our.getServiceType(), our.getName(), rslt);
   }
 
+
   @Override
-  public List<AlignmentAnnotation> getAlignmentAnnotation(
-          AnnotationWsJob running,
-          SeqAnnotationServiceCalcWorker abstractJabaCalcWorker) throws Throwable
+  public
+  List<AlignmentAnnotation> getAnnotationResult(JobId job,
+          List<SequenceI> seqs, Map<String, FeatureColourI> featureColours,
+          Map<String, FeatureMatcherSetI> featureFilters) throws Throwable
   {
     if (scoremanager == null)
     {
       // TODO: raise annotation unavailable exception ?
-      scoremanager = service.getAnnotation(running.getJobId());
+      scoremanager = service.getAnnotation(job.getJobId());
     }
     if (scoremanager == null)
     {
       return List.of();
     }
-
-    return annotationFromScoreManager(running,
-            abstractJabaCalcWorker.getAlignViewport(),
-            abstractJabaCalcWorker.getGapMap());
+    /**
+     * dummy alignment to perform annotation on
+     */
+    AlignmentI newal = new Alignment(seqs.toArray(new SequenceI[0]));
+    List<AlignmentAnnotation> ourAnnot = annotationFromScoreManager(newal,
+            featureColours, featureFilters);
+    return ourAnnot;
   }
 
   /**
    * service specific annotation creation method
    * 
-   * @param running
-   * @param alignViewport
-   * @param gapMap
+   * @param seqs
+   *          - sequences to be annotated with results
+   * @param featureColours
+   *          - - updated with any colours imported during result processing
+   * @param featureFilters
+   *          - updated with any filters imported during result processing
+   * 
    * @return
    */
   abstract List<AlignmentAnnotation> annotationFromScoreManager(
-          AnnotationWsJob running,
-          AlignViewportI alignViewport, boolean[] gapMap);
+          AlignmentI seqs, Map<String, FeatureColourI> featureColours,
+          Map<String, FeatureMatcherSetI> featureFilters);
+
 
   /**
    * create and complete an annotation row from a JABAWS score object
@@ -149,16 +161,16 @@ public abstract class JabawsAnnotationInstance
    * @param scr
    */
 
-  protected void createAnnotationRowsForScores(AlignViewportI alignViewport,
+  protected void createAnnotationRowsForScores(AlignmentI al_result,
           boolean[] gapMap, List<AlignmentAnnotation> ourAnnot,
           String calcId,
           int alWidth, Score scr)
   {
     // simple annotation row
-    AlignmentAnnotation annotation = alignViewport.getAlignment()
+    AlignmentAnnotation annotation = al_result
             .findOrCreateAnnotation(scr.getMethod(), calcId, true, null,
                     null);
-    if (alWidth == gapMap.length) // scr.getScores().size())
+    if (gapMap == null || alWidth == gapMap.length) // scr.getScores().size())
     {
       constructAnnotationFromScore(gapMap, annotation, 0,
               alWidth, scr);
@@ -180,7 +192,7 @@ public abstract class JabawsAnnotationInstance
    * @return
    */
   protected AlignmentAnnotation createAnnotationRowsForScores(
-          AlignViewportI alignViewport, boolean[] gapMap,
+          AlignmentI alignment, boolean[] gapMap,
           List<AlignmentAnnotation> ourAnnot, String typeName,
           String calcId, SequenceI dseq, int base, Score scr)
   {
@@ -191,7 +203,7 @@ public abstract class JabawsAnnotationInstance
     // scr.getMethod(), typeName, new Annotation[]
     // {}, 0, -1, AlignmentAnnotation.LINE_GRAPH);
     // annotation.setCalcId(calcId);
-    AlignmentAnnotation annotation = alignViewport.getAlignment()
+    AlignmentAnnotation annotation = alignment
             .findOrCreateAnnotation(typeName, calcId, false, dseq, null);
     constructAnnotationFromScore(gapMap, annotation, 0, dseq.getLength(),
             scr);
@@ -203,44 +215,6 @@ public abstract class JabawsAnnotationInstance
   }
 
   /**
-   * replace an existing sequence associated annotation with another, creating
-   * association as necessary
-   * 
-   * @param newAnnot
-   *          - annotation row used to create an instance on the dataset
-   *          sequence.
-   * @param typeName
-   *          - label used to match existing row
-   * @param calcId
-   *          - calcId for existing row
-   * @param aSeq
-   *          - alignment sequence with reference to destination dataet sequence
-   */
-  protected void replaceAnnotationOnAlignmentWith(
-          AlignmentAnnotation newAnnot, String typeName, String calcId,
-          SequenceI aSeq)
-  {
-    SequenceI dsseq = aSeq.getDatasetSequence();
-    while (dsseq.getDatasetSequence() != null)
-    {
-      dsseq = dsseq.getDatasetSequence();
-    }
-    // look for same annotation on dataset and lift this one over
-    List<AlignmentAnnotation> dsan = dsseq.getAlignmentAnnotations(calcId,
-            typeName);
-    if (dsan != null && dsan.size() > 0)
-    {
-      for (AlignmentAnnotation dssan : dsan)
-      {
-        dsseq.removeAlignmentAnnotation(dssan);
-      }
-    }
-    AlignmentAnnotation dssan = new AlignmentAnnotation(newAnnot);
-    dsseq.addAlignmentAnnotation(dssan);
-    dssan.adjustForAlignment();
-  }
-
-  /**
    * create column annotation elements from Jabaws score object
    * 
    * @param gapMap
index cde7707..1b07fe6 100644 (file)
  */
 package jalview.ws.jws2.jabaws2;
 
-import jalview.api.AlignViewportI;
+import jalview.api.FeatureColourI;
 import jalview.datamodel.AlignmentAnnotation;
+import jalview.datamodel.AlignmentI;
 import jalview.datamodel.Annotation;
+import jalview.datamodel.features.FeatureMatcherSetI;
 import jalview.util.MessageManager;
-import jalview.ws.gui.AnnotationWsJob;
 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;
 
@@ -85,9 +87,9 @@ public class RNAalifoldClient extends JabawsAnnotationInstance
   }
 
   @Override
-  List<AlignmentAnnotation> annotationFromScoreManager(
-          AnnotationWsJob running, AlignViewportI alignViewport,
-          boolean[] gapMap)
+  List<AlignmentAnnotation> annotationFromScoreManager(AlignmentI seqs,
+          Map<String, FeatureColourI> featureColours,
+          Map<String, FeatureMatcherSetI> featureFilters)
   {
     List<AlignmentAnnotation> ourAnnot = new ArrayList<>();
 
@@ -103,18 +105,21 @@ public class RNAalifoldClient extends JabawsAnnotationInstance
             .equals(AlifoldResult.contactProbabilities.toString()));
 
     // add annotation for the consensus sequence alignment
-    createAnnotationRowforScoreHolder(alignViewport, gapMap, ourAnnot,
+    createAnnotationRowforScoreHolder(seqs, null,
+            ourAnnot,
             getCalcId(), structs.get(0), null, null);
 
     // Add annotations for the mfe Structure
-    createAnnotationRowforScoreHolder(alignViewport, gapMap, ourAnnot,
+    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(alignViewport, gapMap, ourAnnot,
+      createAnnotationRowforScoreHolder(seqs, null,
+              ourAnnot,
               getCalcId(), structs.get(2), data.get(0), data.get(2));
       count++;
     }
@@ -130,7 +135,7 @@ public class RNAalifoldClient extends JabawsAnnotationInstance
               .equals(AlifoldResult.ensembleValues.toString()))
       {
 
-        createAnnotationRowforScoreHolder(alignViewport, gapMap, ourAnnot,
+        createAnnotationRowforScoreHolder(seqs, null, ourAnnot,
                 getCalcId(), structs.get(i), data.get(i), null);
       }
     }
@@ -138,7 +143,7 @@ public class RNAalifoldClient extends JabawsAnnotationInstance
   }
 
   private static void createAnnotationRowforScoreHolder(
-          AlignViewportI alignViewport, boolean[] gapMap,
+          AlignmentI alignment, boolean[] gapMap,
           List<AlignmentAnnotation> ourAnnot, String calcId, String struct,
           TreeSet<Score> data, TreeSet<Score> descriptionData)
   {
@@ -165,7 +170,7 @@ public class RNAalifoldClient extends JabawsAnnotationInstance
     String typename = typenameAndDescription[0];
     String description = typenameAndDescription[1];
 
-    AlignmentAnnotation annotation = alignViewport.getAlignment()
+    AlignmentAnnotation annotation = alignment
             .findOrCreateAnnotation(typename, calcId, false, null, null);
 
     constructAnnotationFromScoreHolder(gapMap, annotation, struct, data);
index 4d2c4fb..4fb35a0 100644 (file)
@@ -1,11 +1,11 @@
 package jalview.ws.slivkaws;
 
+import jalview.api.FeatureColourI;
 import jalview.datamodel.AlignmentAnnotation;
 import jalview.datamodel.SequenceI;
+import jalview.datamodel.features.FeatureMatcherSetI;
 import jalview.ws.api.JobId;
 import jalview.ws.api.SequenceAnnotationServiceI;
-import jalview.ws.gui.AnnotationWsJob;
-import jalview.ws.jws2.SeqAnnotationServiceCalcWorker;
 import jalview.ws.params.ArgumentI;
 import jalview.ws.params.WsParamSetI;
 
@@ -13,6 +13,7 @@ import java.io.ByteArrayInputStream;
 import java.io.InputStream;
 import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 
 import uk.ac.dundee.compbio.slivkaclient.FormField;
 import uk.ac.dundee.compbio.slivkaclient.RemoteFile;
@@ -66,8 +67,9 @@ public class SlivkaAnnotationServiceInstance extends SlivkaWSInstance implements
   }
 
   @Override
-  public List<AlignmentAnnotation> getAlignmentAnnotation(AnnotationWsJob running,
-      SeqAnnotationServiceCalcWorker abstractJabaCalcWorker) throws Throwable
+  public List<AlignmentAnnotation> getAnnotationResult(JobId job,
+          List<SequenceI> seqs, Map<String, FeatureColourI> featureColours,
+          Map<String, FeatureMatcherSetI> featureFilters) throws Throwable
   {
     // TODO Auto-generated method stub
     return null;
index 19a725e..ade9150 100644 (file)
@@ -49,6 +49,11 @@ public class AlignmentAnnotationTests
     createAnnotation(sq);
     AlignmentAnnotation alc, alo = sq.getAnnotation()[0];
     alc = new AlignmentAnnotation(alo);
+
+    // TODO: this only tests string equals (which is unreliable), should use
+    // refactored tests from StockholmFileTest
+    Assert.assertEquals(alc.toString(), alo.toString());
+
     for (String key : alo.getProperties())
     {
       assertEquals("Property mismatch", alo.getProperty(key),
index fa73aba..6dad024 100644 (file)
@@ -1354,6 +1354,34 @@ public class AlignmentTest
     assertEquals("", ala.getCalcId());
   }
 
+  @Test(groups = {"Functional"})
+  public void testUpdateFromOrAddAnnotation()
+  {
+    SequenceI seq = new Sequence("seq1", "FRMLPSRT-A--L-");
+    AlignmentI alignment = new Alignment(new SequenceI[] { seq });
+
+    AlignmentAnnotation ala = alignment.findOrCreateAnnotation(
+            "Temperature Factor", null, false, seq, null);
+
+    assertNotNull(ala);
+    assertEquals(seq, ala.sequenceRef);
+    assertEquals("", ala.getCalcId());
+
+    // Assuming findOrCreateForNullCalcId passed then this should work
+
+    assertTrue(ala == alignment.updateFromOrCopyAnnotation(ala));
+    AlignmentAnnotation updatedAla = new AlignmentAnnotation(ala);
+    updatedAla.description = "updated Description";
+    Assert.assertTrue(
+            ala == alignment.updateFromOrCopyAnnotation(updatedAla));
+    Assert.assertEquals(ala.toString(), updatedAla.toString());
+    updatedAla.calcId = "newCalcId";
+    AlignmentAnnotation newUpdatedAla = alignment
+            .updateFromOrCopyAnnotation(updatedAla);
+    Assert.assertTrue(updatedAla != newUpdatedAla);
+    Assert.assertEquals(updatedAla.toString(), newUpdatedAla.toString());
+  }
+
   @Test(groups = "Functional")
   public void testPropagateInsertions()
   {
index c344645..b607321 100644 (file)
@@ -298,8 +298,6 @@ public class SequenceTest
      * invalid inputs
      */
     assertNull(sq.findPositions(6, 5));
-    assertNull(sq.findPositions(0, 5));
-    assertNull(sq.findPositions(-1, 5));
 
     /*
      * all gapped ranges
@@ -342,6 +340,16 @@ public class SequenceTest
     assertEquals(new Range(11, 12), sq.findPositions(5, 10)); // DE
     assertEquals(new Range(8, 13), sq.findPositions(1, 13)); // the lot
     assertEquals(new Range(8, 13), sq.findPositions(1, 99));
+
+    /**
+     * now try on a sequence with no gaps
+     */
+    sq.createDatasetSequence();
+    assertEquals(new Range(8, 13),
+            sq.getDatasetSequence().findPositions(1, 99));
+    assertEquals(new Range(8, 13),
+            sq.getDatasetSequence().findPositions(0, 99));
+
   }
 
   /**