JAL-3066 Sequence annotation services.
[jalview.git] / src / jalview / ws / jws2 / SeqAnnotationServiceCalcWorker.java
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()