JAL-1610 check reference annotations against alignment, not sequence
[jalview.git] / src / jalview / gui / PopupMenu.java
index 9976471..c2bee83 100644 (file)
@@ -27,6 +27,7 @@ import jalview.commands.ChangeCaseCommand;
 import jalview.commands.EditCommand;
 import jalview.commands.EditCommand.Action;
 import jalview.datamodel.AlignmentAnnotation;
+import jalview.datamodel.AlignmentI;
 import jalview.datamodel.Annotation;
 import jalview.datamodel.DBRefEntry;
 import jalview.datamodel.PDBEntry;
@@ -1765,12 +1766,13 @@ public class PopupMenu extends JPopupMenu
 
   /**
    * Check for any annotations on the underlying dataset sequences (for the
-   * current selection group) which are not on the alignment annotations for the
-   * sequence. If any are found, enable the option to add them to the alignment.
-   * The criteria for 'on the alignment' is finding an alignment annotation on
-   * the sequence, that matches on calcId and label. A tooltip is also
-   * constructed that displays the source (calcId) and type (label) of the
-   * annotations that can be added.
+   * current selection group) which are not 'on the alignment'.If any are found,
+   * enable the option to add them to the alignment. The criteria for 'on the
+   * alignment' is finding an alignment annotation on the alignment, matched on
+   * calcId, label and sequenceRef.
+   * 
+   * A tooltip is also constructed that displays the source (calcId) and type
+   * (label) of the annotations that can be added.
    * 
    * @param menuItem
    * @param forSequences
@@ -1797,10 +1799,11 @@ public class PopupMenu extends JPopupMenu
     /*
      * For each sequence selected in the alignment, make a list of any
      * annotations on the underlying dataset sequence which are not already on
-     * the sequence in the alignment.
+     * the alignment.
      * 
      * Build a map of { alignmentSequence, <List of annotations to add> }
      */
+    AlignmentI al = this.ap.av.getAlignment();
     final Map<SequenceI, List<AlignmentAnnotation>> candidates = new LinkedHashMap<SequenceI, List<AlignmentAnnotation>>();
     for (SequenceI seq : forSequences)
     {
@@ -1818,11 +1821,12 @@ public class PopupMenu extends JPopupMenu
       for (AlignmentAnnotation dsann : datasetAnnotations)
       {
         /*
-         * If the sequence has no annotation that matches this one, then add
-         * this one to the results list.
+         * Find matching annotations on the alignment.
          */
-        if (seq.getAlignmentAnnotations(dsann.getCalcId(), dsann.label)
-                .isEmpty())
+        final Iterable<AlignmentAnnotation> matchedAlignmentAnnotations = al
+                .findAnnotations(seq, dsann.getCalcId(),
+                        dsann.label);
+        if (!matchedAlignmentAnnotations.iterator().hasNext())
         {
           result.add(dsann);
           tipEntries.put(dsann.getCalcId(), dsann.label);
@@ -1891,8 +1895,14 @@ public class PopupMenu extends JPopupMenu
         }
         copyAnn.restrict(startRes, endRes);
 
-        // add to the sequence (sets copyAnn.datasetSequence)
-        seq.addAlignmentAnnotation(copyAnn);
+        /*
+         * Add to the sequence (sets copyAnn.datasetSequence), unless the
+         * original annotation is already on the sequence.
+         */
+        if (!seq.hasAnnotation(ann))
+        {
+          seq.addAlignmentAnnotation(copyAnn);
+        }
         // adjust for gaps
         copyAnn.adjustForAlignment();
         // add to the alignment and set visible