JAL-4121 Added a NPE check caught in tests. Added --gui to relevant tests.
[jalview.git] / src / jalview / analysis / AlignmentUtils.java
index fdc74e2..0906872 100644 (file)
@@ -20,8 +20,6 @@
  */
 package jalview.analysis;
 
-import java.util.Locale;
-
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
@@ -31,6 +29,7 @@ import java.util.HashSet;
 import java.util.Iterator;
 import java.util.LinkedHashMap;
 import java.util.List;
+import java.util.Locale;
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.NoSuchElementException;
@@ -46,6 +45,7 @@ import jalview.datamodel.AlignedCodonFrame.SequenceToSequenceMapping;
 import jalview.datamodel.Alignment;
 import jalview.datamodel.AlignmentAnnotation;
 import jalview.datamodel.AlignmentI;
+import jalview.datamodel.ContactMatrixI;
 import jalview.datamodel.DBRefEntry;
 import jalview.datamodel.GeneLociI;
 import jalview.datamodel.IncompleteCodonException;
@@ -184,10 +184,10 @@ public class AlignmentUtils
       // TODO use Character.toLowerCase to avoid creating String objects?
       char[] upstream = new String(ds
               .getSequence(s.getStart() - 1 - ustream_ds, s.getStart() - 1))
-                      .toLowerCase(Locale.ROOT).toCharArray();
+              .toLowerCase(Locale.ROOT).toCharArray();
       char[] downstream = new String(
               ds.getSequence(s_end - 1, s_end + dstream_ds))
-                      .toLowerCase(Locale.ROOT).toCharArray();
+              .toLowerCase(Locale.ROOT).toCharArray();
       char[] coreseq = s.getSequence();
       char[] nseq = new char[offset + upstream.length + downstream.length
               + coreseq.length];
@@ -1471,8 +1471,19 @@ public class AlignmentUtils
          */
         final Iterable<AlignmentAnnotation> matchedAlignmentAnnotations = al
                 .findAnnotations(seq, dsann.getCalcId(), dsann.label);
-        if (!matchedAlignmentAnnotations.iterator().hasNext())
+        boolean found=false;
+        if (matchedAlignmentAnnotations != null)
         {
+          for (AlignmentAnnotation matched:matchedAlignmentAnnotations)
+          {
+            if (dsann.description.equals(matched.description))
+            {
+              found=true;
+              break;
+            }
+          }
+        }
+        if (!found) {
           result.add(dsann);
           if (labelForCalcId != null)
           {
@@ -1517,7 +1528,7 @@ public class AlignmentUtils
           startRes = selectionGroup.getStartRes();
           endRes = selectionGroup.getEndRes();
         }
-        copyAnn.restrict(startRes, endRes);
+        copyAnn.restrict(startRes, endRes + 0);
 
         /*
          * Add to the sequence (sets copyAnn.datasetSequence), unless the
@@ -1525,6 +1536,12 @@ public class AlignmentUtils
          */
         if (!seq.hasAnnotation(ann))
         {
+          ContactMatrixI cm = seq.getDatasetSequence()
+                  .getContactMatrixFor(ann);
+          if (cm != null)
+          {
+            seq.addContactListFor(copyAnn, cm);
+          }
           seq.addAlignmentAnnotation(copyAnn);
         }
         // adjust for gaps
@@ -1534,6 +1551,7 @@ public class AlignmentUtils
         copyAnn.visible = true;
       }
     }
+
   }
 
   /**
@@ -1573,6 +1591,21 @@ public class AlignmentUtils
     }
   }
 
+  public static AlignmentAnnotation getFirstSequenceAnnotationOfType(
+          AlignmentI al, int graphType)
+  {
+    AlignmentAnnotation[] anns = al.getAlignmentAnnotation();
+    if (anns != null)
+    {
+      for (AlignmentAnnotation aa : anns)
+      {
+        if (aa.sequenceRef != null && aa.graph == graphType)
+          return aa;
+      }
+    }
+    return null;
+  }
+
   /**
    * Returns true if either sequence has a cross-reference to the other
    *