JAL-4134 catch NPE - though in principle this should never happen…
[jalview.git] / src / jalview / analysis / AlignmentSorter.java
index 8473904..b02d49c 100755 (executable)
@@ -25,6 +25,7 @@ import jalview.analysis.scoremodels.SimilarityParams;
 import jalview.datamodel.AlignmentAnnotation;
 import jalview.datamodel.AlignmentI;
 import jalview.datamodel.AlignmentOrder;
+import jalview.datamodel.BinaryNode;
 import jalview.datamodel.SequenceFeature;
 import jalview.datamodel.SequenceGroup;
 import jalview.datamodel.SequenceI;
@@ -143,8 +144,8 @@ public class AlignmentSorter
     }
 
     // NOTE: DO NOT USE align.setSequenceAt() here - it will NOT work
-    List<SequenceI> asq;
-    synchronized (asq = align.getSequences())
+    List<SequenceI> asq = align.getSequences();
+    synchronized (asq)
     {
       for (int i = 0; i < len; i++)
       {
@@ -179,10 +180,10 @@ public class AlignmentSorter
   public static void setOrder(AlignmentI align, SequenceI[] seqs)
   {
     // NOTE: DO NOT USE align.setSequenceAt() here - it will NOT work
-    List<SequenceI> algn;
-    synchronized (algn = align.getSequences())
+    List<SequenceI> algn = align.getSequences();
+    synchronized (algn)
     {
-      List<SequenceI> tmp = new ArrayList<SequenceI>();
+      List<SequenceI> tmp = new ArrayList<>();
 
       for (int i = 0; i < seqs.length; i++)
       {
@@ -279,7 +280,7 @@ public class AlignmentSorter
   {
     // MAINTAINS ORIGNAL SEQUENCE ORDER,
     // ORDERS BY GROUP SIZE
-    List<SequenceGroup> groups = new ArrayList<SequenceGroup>();
+    List<SequenceGroup> groups = new ArrayList<>();
 
     if (groups.hashCode() != lastGroupHash)
     {
@@ -315,7 +316,7 @@ public class AlignmentSorter
 
     // NOW ADD SEQUENCES MAINTAINING ALIGNMENT ORDER
     // /////////////////////////////////////////////
-    List<SequenceI> seqs = new ArrayList<SequenceI>();
+    List<SequenceI> seqs = new ArrayList<>();
 
     for (int i = 0; i < groups.size(); i++)
     {
@@ -357,7 +358,7 @@ public class AlignmentSorter
     // tmp2 = tmp.retainAll(mask);
     // return tmp2.addAll(mask.removeAll(tmp2))
 
-    ArrayList<SequenceI> seqs = new ArrayList<SequenceI>();
+    ArrayList<SequenceI> seqs = new ArrayList<>();
     int i, idx;
     boolean[] tmask = new boolean[mask.size()];
 
@@ -436,7 +437,7 @@ public class AlignmentSorter
   {
     int nSeq = align.getHeight();
 
-    List<SequenceI> tmp = new ArrayList<SequenceI>();
+    List<SequenceI> tmp = new ArrayList<>();
 
     tmp = _sortByTree(tree.getTopNode(), tmp, align.getSequences());
 
@@ -534,7 +535,7 @@ public class AlignmentSorter
    * 
    * @return DOCUMENT ME!
    */
-  private static List<SequenceI> _sortByTree(SequenceNode node,
+  private static List<SequenceI> _sortByTree(BinaryNode node,
           List<SequenceI> tmp, List<SequenceI> seqset)
   {
     if (node == null)
@@ -542,12 +543,14 @@ public class AlignmentSorter
       return tmp;
     }
 
-    SequenceNode left = (SequenceNode) node.left();
-    SequenceNode right = (SequenceNode) node.right();
+    BinaryNode left = (BinaryNode) node.left();
+    BinaryNode right = (BinaryNode) node.right();
 
     if ((left == null) && (right == null))
     {
-      if (!node.isPlaceholder() && (node.element() != null))
+      if (!(node instanceof SequenceNode
+              && ((SequenceNode) node).isPlaceholder())
+              && (node.element() != null))
       {
         if (node.element() instanceof SequenceI)
         {
@@ -708,14 +711,15 @@ public class AlignmentSorter
     if (method != FEATURE_SCORE && method != FEATURE_LABEL
             && method != FEATURE_DENSITY)
     {
-      String msg = String
-              .format("Implementation Error - sortByFeature method must be either '%s' or '%s'",
-                      FEATURE_SCORE, FEATURE_DENSITY);
+      String msg = String.format(
+              "Implementation Error - sortByFeature method must be either '%s' or '%s'",
+              FEATURE_SCORE, FEATURE_DENSITY);
       System.err.println(msg);
       return;
     }
 
-    flipFeatureSortIfUnchanged(method, featureTypes, groups, startCol, endCol);
+    flipFeatureSortIfUnchanged(method, featureTypes, groups, startCol,
+            endCol);
 
     SequenceI[] seqs = alignment.getSequencesArray();
 
@@ -734,8 +738,8 @@ public class AlignmentSorter
        * get sequence residues overlapping column region
        * and features for residue positions and specified types
        */
-      String[] types = featureTypes == null ? null : featureTypes
-              .toArray(new String[featureTypes.size()]);
+      String[] types = featureTypes == null ? null
+              : featureTypes.toArray(new String[featureTypes.size()]);
       List<SequenceFeature> sfs = seqs[i].findFeatures(startCol + 1,
               endCol + 1, types);