From: BobHanson Date: Fri, 5 Jun 2020 13:46:03 +0000 (-0500) Subject: JAL-3446 AlignmentSorter/Test fixed for instance X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=41c32b8a1d148bce8578c73a110276162cddd939;p=jalview.git JAL-3446 AlignmentSorter/Test fixed for instance --- diff --git a/src/jalview/analysis/AlignmentSorter.java b/src/jalview/analysis/AlignmentSorter.java index 8634db6..af9b5df 100755 --- a/src/jalview/analysis/AlignmentSorter.java +++ b/src/jalview/analysis/AlignmentSorter.java @@ -61,7 +61,7 @@ public class AlignmentSorter implements ApplicationSingletonI // private singleton } - private static AlignmentSorter getInstance() + public static AlignmentSorter getInstance() { return (AlignmentSorter) ApplicationSingletonProvider .getInstance(AlignmentSorter.class); @@ -142,93 +142,10 @@ public class AlignmentSorter implements ApplicationSingletonI } QuickSort.sort(scores, seqs); - setReverseOrder(align, seqs); } /** - * Reverse the order of the sort - * - * @param align - * DOCUMENT ME! - * @param seqs - * DOCUMENT ME! - */ - private static void setReverseOrder(AlignmentI align, SequenceI[] seqs) - { - int nSeq = seqs.length; - - int len = 0; - - if ((nSeq % 2) == 0) - { - len = nSeq / 2; - } - else - { - len = (nSeq + 1) / 2; - } - - // NOTE: DO NOT USE align.setSequenceAt() here - it will NOT work - List asq = align.getSequences(); - synchronized (asq) - { - for (int i = 0; i < len; i++) - { - // SequenceI tmp = seqs[i]; - asq.set(i, seqs[nSeq - i - 1]); - asq.set(nSeq - i - 1, seqs[i]); - } - } - } - - /** - * Sets the Alignment object with the given sequences - * - * @param align - * Alignment object to be updated - * @param tmp - * sequences as a vector - */ - private static void setOrder(AlignmentI align, List tmp) - { - setOrder(align, vectorSubsetToArray(tmp, align.getSequences())); - } - - /** - * Sets the Alignment object with the given sequences - * - * @param align - * DOCUMENT ME! - * @param seqs - * sequences as an array - */ - public static void setOrder(AlignmentI align, SequenceI[] seqs) - { - // NOTE: DO NOT USE align.setSequenceAt() here - it will NOT work - List algn = align.getSequences(); - synchronized (algn) - { - List tmp = new ArrayList<>(); - - for (int i = 0; i < seqs.length; i++) - { - if (algn.contains(seqs[i])) - { - tmp.add(seqs[i]); - } - } - - algn.clear(); - // User may have hidden seqs, then clicked undo or redo - for (int i = 0; i < tmp.size(); i++) - { - algn.add(tmp.get(i)); - } - } - } - - /** * Sorts by ID. Numbers are sorted before letters. * * @param align @@ -248,18 +165,9 @@ public class AlignmentSorter implements ApplicationSingletonI } QuickSort.sort(ids, seqs); - AlignmentSorter as = getInstance(); - if (as.sortIdAscending) - { - setReverseOrder(align, seqs); - } - else - { - setOrder(align, seqs); - } - as.sortIdAscending = !as.sortIdAscending; + sort(align, seqs, as.sortIdAscending); } /** @@ -282,19 +190,9 @@ public class AlignmentSorter implements ApplicationSingletonI } QuickSort.sort(length, seqs); - AlignmentSorter as = getInstance(); - - if (as.sortLengthAscending) - { - setReverseOrder(align, seqs); - } - else - { - setOrder(align, seqs); - } - as.sortLengthAscending = !as.sortLengthAscending; + sort(align, seqs, as.sortLengthAscending); } /** @@ -347,7 +245,7 @@ public class AlignmentSorter implements ApplicationSingletonI // NOW ADD SEQUENCES MAINTAINING ALIGNMENT ORDER // ///////////////////////////////////////////// - List seqs = new ArrayList<>(); + List tmp = new ArrayList<>(); for (int i = 0; i < groups.size(); i++) { @@ -356,19 +254,10 @@ public class AlignmentSorter implements ApplicationSingletonI for (int j = 0; j < orderedseqs.length; j++) { - seqs.add(orderedseqs[j]); + tmp.add(orderedseqs[j]); } } - - if (as.sortGroupAscending) - { - setOrder(align, seqs); - } - else - { - setReverseOrder(align, - vectorSubsetToArray(seqs, align.getSequences())); - } + sort(align, tmp, as.sortGroupAscending); } /** @@ -443,16 +332,7 @@ public class AlignmentSorter implements ApplicationSingletonI { as.sortOrderAscending = true; } - - if (as.sortOrderAscending) - { - setOrder(align, tmp); - } - else - { - setReverseOrder(align, - vectorSubsetToArray(tmp, align.getSequences())); - } + sort(align, tmp, as.sortOrderAscending); } /** @@ -519,16 +399,7 @@ public class AlignmentSorter implements ApplicationSingletonI { as.sortTreeAscending = !as.sortTreeAscending; } - - if (as.sortTreeAscending) - { - setOrder(align, tmp); - } - else - { - setReverseOrder(align, - vectorSubsetToArray(tmp, align.getSequences())); - } + sort(align, tmp, as.sortTreeAscending); } /** @@ -622,7 +493,7 @@ public class AlignmentSorter implements ApplicationSingletonI for (int i = 0; i < alignment.length; i++) { - ids[i] = (new Float(alignment[i].getName().substring(8))) + ids[i] = (Float.valueOf(alignment[i].getName().substring(8))) .floatValue(); } @@ -928,4 +799,96 @@ public class AlignmentSorter implements ApplicationSingletonI as.sortByFeatureCriteria = scoreCriteria; } + private static void sort(AlignmentI align, List tmp, + boolean ascending) + { + sort(align, vectorSubsetToArray(tmp, align.getSequences()), ascending); + } + + private static void sort(AlignmentI align, SequenceI[] seqs, + boolean ascending) + { + if (ascending) + { + setOrder(align, seqs); + } + else + { + setReverseOrder(align, seqs); + } + + } + + + /** + * Sets the Alignment object with the given sequences + * + * @param align + * DOCUMENT ME! + * @param seqs + * sequences as an array + */ + public static void setOrder(AlignmentI align, SequenceI[] seqs) + { + // NOTE: DO NOT USE align.setSequenceAt() here - it will NOT work + List algn = align.getSequences(); + synchronized (algn) + { + List tmp = new ArrayList<>(); + + for (int i = 0; i < seqs.length; i++) + { + if (algn.contains(seqs[i])) + { + tmp.add(seqs[i]); + } + } + + algn.clear(); + // User may have hidden seqs, then clicked undo or redo + for (int i = 0; i < tmp.size(); i++) + { + algn.add(tmp.get(i)); + } + } + } + + /** + * Reverse the order of the sort + * + * @param align + * DOCUMENT ME! + * @param seqs + * DOCUMENT ME! + */ + private static void setReverseOrder(AlignmentI align, SequenceI[] seqs) + { + int nSeq = seqs.length; + + int len = (nSeq + (nSeq % 2)) / 2; +// int len = 0; +// +// if ((nSeq % 2) == 0) +// { +// len = nSeq / 2; +// } +// else +// { +// len = (nSeq + 1) / 2; +// } + + // NOTE: DO NOT USE align.setSequenceAt() here - it will NOT work + List asq = align.getSequences(); + synchronized (asq) + { + for (int i = 0; i < len; i++) + { + // SequenceI tmp = seqs[i]; + asq.set(i, seqs[nSeq - i - 1]); + asq.set(nSeq - i - 1, seqs[i]); + } + } + } + + } diff --git a/test/jalview/analysis/AlignmentSorterTest.java b/test/jalview/analysis/AlignmentSorterTest.java index 3b9be23..a566be9 100644 --- a/test/jalview/analysis/AlignmentSorterTest.java +++ b/test/jalview/analysis/AlignmentSorterTest.java @@ -31,7 +31,7 @@ public class AlignmentSorterTest /* * sort with no score features does nothing */ - PA.setValue(AlignmentSorter.class, "sortByFeatureCriteria", null); + PA.setValue(AlignmentSorter.getInstance(), "sortByFeatureCriteria", null); AlignmentSorter.sortByFeature(null, null, 0, al.getWidth(), al, AlignmentSorter.FEATURE_SCORE); @@ -64,7 +64,7 @@ public class AlignmentSorterTest * sort by ascending score, no filter on feature type or group * NB sort order for the same feature set (none) gets toggled, so descending */ - PA.setValue(AlignmentSorter.class, "sortByFeatureAscending", true); + PA.setValue(AlignmentSorter.getInstance(), "sortByFeatureAscending", true); AlignmentSorter.sortByFeature(null, null, 0, al.getWidth(), al, AlignmentSorter.FEATURE_SCORE); assertSame(al.getSequenceAt(3), seq3); // -0.5