JAL-34 fix up comparison annotation so it gets shown and scaled properly
authorJames Procter <j.procter@dundee.ac.uk>
Thu, 18 Jan 2024 17:34:24 +0000 (17:34 +0000)
committerJames Procter <j.procter@dundee.ac.uk>
Thu, 18 Jan 2024 17:34:24 +0000 (17:34 +0000)
src/jalview/api/AlignViewportI.java
src/jalview/viewmodel/AlignmentViewport.java
src/jalview/workers/AlignmentComparisonThread.java

index cde9e1a..4e03e45 100644 (file)
@@ -569,4 +569,6 @@ public interface AlignViewportI extends ViewStyleI
   ContactMatrixI getContactMatrix(AlignmentAnnotation alignmentAnnotation);
 
   boolean is3di();
+
+  public AlignmentAnnotation getComparisonAnnotation();
 }
index 3ab2fce..22d863a 100644 (file)
@@ -60,6 +60,7 @@ import jalview.datamodel.Sequence;
 import jalview.datamodel.SequenceCollectionI;
 import jalview.datamodel.SequenceGroup;
 import jalview.datamodel.SequenceI;
+import jalview.gui.AlignmentPanel;
 import jalview.gui.QuitHandler;
 import jalview.project.Jalview2XML;
 import jalview.renderer.ResidueShader;
@@ -903,8 +904,8 @@ public abstract class AlignmentViewport
         if (calculator.getRegisteredWorkersOfClass(
                 AlignmentComparisonThread.class) == null)
         {
-          calculator
-                  .registerWorker(new AlignmentComparisonThread(this, ap));
+          initAlignmentComparison(ap);
+          ap.adjustAnnotationHeight();
         }
       }
     }
@@ -1347,6 +1348,10 @@ public abstract class AlignmentViewport
    */
   private boolean followHighlight = true;
 
+  private AlignmentAnnotation aligComparison;
+
+  private boolean showComparison = true;
+
   /**
    * Property change listener for changes in alignment
    * 
@@ -2095,6 +2100,40 @@ public abstract class AlignmentViewport
     }
   }
 
+  private void initAlignmentComparison(final AlignmentViewPanel ap)
+  {
+    if (getCodingComplement() == null
+            || getCodingComplement().isNucleotide() != isNucleotide())
+    {
+      return;
+    }
+
+    if (aligComparison == null)
+    {
+      aligComparison = new AlignmentAnnotation("Comparision",
+              MessageManager.getString("label.alignment_comparison_descr"),
+              new Annotation[1], 0f, 100f, AlignmentAnnotation.BAR_GRAPH);
+      aligComparison.hasText = true;
+      aligComparison.autoCalculated = true;
+
+      if (showComparison)
+      {
+        alignment.addAnnotation(aligComparison);
+      }
+    }
+
+    if (calculator.getRegisteredWorkersOfClass(
+            AlignmentComparisonThread.class) == null)
+    {
+      calculator.registerWorker(new AlignmentComparisonThread(this, ap));
+    }
+  }
+  @Override
+  public AlignmentAnnotation getComparisonAnnotation()
+  {
+    return aligComparison;
+  }
+
   /*
    * (non-Javadoc)
    * 
index dfdbb87..8ec1683 100644 (file)
@@ -25,6 +25,7 @@ import jalview.analysis.Conservation;
 import jalview.analysis.scoremodels.ScoreModels;
 import jalview.api.AlignViewportI;
 import jalview.api.AlignmentViewPanel;
+import jalview.bin.Console;
 import jalview.datamodel.AlignedCodonFrame;
 import jalview.datamodel.AlignmentAnnotation;
 import jalview.datamodel.AlignmentI;
@@ -51,11 +52,13 @@ public class AlignmentComparisonThread extends AlignCalcWorker
           AlignmentViewPanel alignPanel)
   {
     super(alignViewport, alignPanel);
+    comparisonAnnot = alignViewport.getComparisonAnnotation();
   }
 
-  Annotation[] correspondence = new Annotation[] {};
+  Annotation[] correspondence = new Annotation[1];
   AlignmentAnnotation comparisonAnnot=null;
   int alWidth;
+  private int maxCor=1;
 
   @Override
   public void run()
@@ -97,7 +100,7 @@ public class AlignmentComparisonThread extends AlignCalcWorker
 
         return;
       }
-      comparisonAnnot = alignViewport.getAlignment().findOrCreateAnnotation("Comparison", "CORRESPONDENCE", true, null, null);
+      
       comparisonAnnot.annotations=correspondence;
       ourAnnot.add(comparisonAnnot);
       ourAnnots = ourAnnot;
@@ -105,8 +108,9 @@ public class AlignmentComparisonThread extends AlignCalcWorker
       try
       {
         computeColumnCorrespondence(alignViewport, codingComplement);
-      } catch (IndexOutOfBoundsException x)
+      } catch (Throwable x)
       {
+        Console.error("Unexpected error computing similarity of alignments",x);
         // probable race condition. just finish and return without any fuss.
         calcMan.workerComplete(this);
         return;
@@ -151,13 +155,16 @@ public class AlignmentComparisonThread extends AlignCalcWorker
 
     int colEnd = alignViewport.getAlignment().getWidth();
     Annotation[] colCorrsp = new Annotation[colEnd];
+    maxCor=0;
     for (int col = 0; col < colEnd; col++)
     {
       int[] theirWidth = MappingUtils.findMappedColumns(col, ourMappings,
               us, them, alignViewport.getGapCharacter());
+      int wid =theirWidth != null ? Math.abs(theirWidth[1] - theirWidth[0])
+              : 0;
       colCorrsp[col] = new Annotation(
-              theirWidth != null ? Math.abs(theirWidth[1] - theirWidth[0])
-                      : 0);
+              wid);
+      maxCor = Math.max(maxCor, wid);
     }
     correspondence=colCorrsp;
   }
@@ -167,7 +174,9 @@ public class AlignmentComparisonThread extends AlignCalcWorker
     if (b || !calcMan.isWorking(this) && correspondence!=null)
     {
       comparisonAnnot.annotations = correspondence;
+      comparisonAnnot.graphMax=(float)maxCor;
       comparisonAnnot.validateRangeAndDisplay();
+      ap.paintAlignment(false, false);
     }
   }