X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fanalysis%2FAlignmentSorter.java;h=427db84270b79c1957490d7d2d046cee34590deb;hb=5721b73c6d5de84b21a989a8734e4c161f5dc630;hp=bddf6e39c8005d91fad63d8f89d917ee8bb83710;hpb=93cdde7955263e9c0fc81621d73cd3157c02b944;p=jalview.git diff --git a/src/jalview/analysis/AlignmentSorter.java b/src/jalview/analysis/AlignmentSorter.java index bddf6e3..427db84 100755 --- a/src/jalview/analysis/AlignmentSorter.java +++ b/src/jalview/analysis/AlignmentSorter.java @@ -29,6 +29,7 @@ import jalview.datamodel.SequenceFeature; import jalview.datamodel.SequenceGroup; import jalview.datamodel.SequenceI; import jalview.datamodel.SequenceNode; +import jalview.util.Platform; import jalview.util.QuickSort; import java.util.ArrayList; @@ -53,38 +54,89 @@ import java.util.List; */ public class AlignmentSorter { + + /** + * types of feature ordering: Sort by score : average score - or total score - + * over all features in region Sort by feature label text: (or if null - + * feature type text) - numerical or alphabetical Sort by feature density: + * based on counts - ignoring individual text or scores for each feature + */ + public static final String FEATURE_SCORE = "average_score"; + + public static final String FEATURE_LABEL = "text"; + + public static final String FEATURE_DENSITY = "density"; + + static AlignmentSorter instance; + + public static AlignmentSorter getInstance() + { + + // BH 2019.05.08 need to isolate static fields in JavaScript + + AlignmentSorter i = instance; + @SuppressWarnings("unused") + ThreadGroup g = null; + if (Platform.isJS()) + { + g = Thread.currentThread().getThreadGroup(); + /** + * @j2sNative i = g._jalviewAlignmentSorterInstance; + * + */ + } + if (i == null) + { + i = new AlignmentSorter(); + + if (Platform.isJS()) + { + /** + * @j2sNative g._jalviewAlignmentSorterInstance = i; + * + */ + } + else + { + instance = i; + } + } + return i; + } + /* * todo: refactor searches to follow a basic pattern: (search property, last * search state, current sort direction) */ - static boolean sortIdAscending = true; + boolean sortIdAscending = true; - static int lastGroupHash = 0; + int lastGroupHash = 0; - static boolean sortGroupAscending = true; + boolean sortGroupAscending = true; - static AlignmentOrder lastOrder = null; + AlignmentOrder lastOrder = null; - static boolean sortOrderAscending = true; + boolean sortOrderAscending = true; - static TreeModel lastTree = null; + TreeModel lastTree = null; - static boolean sortTreeAscending = true; + boolean sortTreeAscending = true; - /* + + /** * last Annotation Label used for sort by Annotation score */ - private static String lastSortByAnnotation; + private String lastSortByAnnotation; - /* - * string hash of last arguments to sortByFeature - * (sort order toggles if this is unchanged between sorts) + /** + * string hash of last arguments to sortByFeature (sort order toggles if this + * is unchanged between sorts) */ - private static String sortByFeatureCriteria; + private String sortByFeatureCriteria; - private static boolean sortByFeatureAscending = true; + private boolean sortByFeatureAscending = true; - private static boolean sortLengthAscending; + private boolean sortLengthAscending; /** * Sorts sequences in the alignment by Percentage Identity with the given @@ -108,8 +160,9 @@ public class AlignmentSorter true); for (int i = 0; i < nSeq; i++) { - scores[i] = (float) PIDModel.computePID(align.getSequenceAt(i) - .getSequenceAsString(), refSeq, pidParams); + scores[i] = (float) PIDModel.computePID( + align.getSequenceAt(i).getSequenceAsString(), refSeq, + pidParams); seqs[i] = align.getSequenceAt(i); } @@ -142,8 +195,8 @@ public class AlignmentSorter } // NOTE: DO NOT USE align.setSequenceAt() here - it will NOT work - List asq; - synchronized (asq = align.getSequences()) + List asq = align.getSequences(); + synchronized (asq) { for (int i = 0; i < len; i++) { @@ -178,10 +231,10 @@ public class AlignmentSorter public static void setOrder(AlignmentI align, SequenceI[] seqs) { // NOTE: DO NOT USE align.setSequenceAt() here - it will NOT work - List algn; - synchronized (algn = align.getSequences()) + List algn = align.getSequences(); + synchronized (algn) { - List tmp = new ArrayList(); + List tmp = new ArrayList<>(); for (int i = 0; i < seqs.length; i++) { @@ -221,7 +274,8 @@ public class AlignmentSorter QuickSort.sort(ids, seqs); - if (sortIdAscending) + AlignmentSorter as = getInstance(); + if (as.sortIdAscending) { setReverseOrder(align, seqs); } @@ -230,7 +284,7 @@ public class AlignmentSorter setOrder(align, seqs); } - sortIdAscending = !sortIdAscending; + as.sortIdAscending = !as.sortIdAscending; } /** @@ -254,7 +308,9 @@ public class AlignmentSorter QuickSort.sort(length, seqs); - if (sortLengthAscending) + AlignmentSorter as = getInstance(); + + if (as.sortLengthAscending) { setReverseOrder(align, seqs); } @@ -263,7 +319,7 @@ public class AlignmentSorter setOrder(align, seqs); } - sortLengthAscending = !sortLengthAscending; + as.sortLengthAscending = !as.sortLengthAscending; } /** @@ -278,16 +334,18 @@ public class AlignmentSorter { // MAINTAINS ORIGNAL SEQUENCE ORDER, // ORDERS BY GROUP SIZE - List groups = new ArrayList(); + List groups = new ArrayList<>(); - if (groups.hashCode() != lastGroupHash) + AlignmentSorter as = getInstance(); + + if (groups.hashCode() != as.lastGroupHash) { - sortGroupAscending = true; - lastGroupHash = groups.hashCode(); + as.sortGroupAscending = true; + as.lastGroupHash = groups.hashCode(); } else { - sortGroupAscending = !sortGroupAscending; + as.sortGroupAscending = !as.sortGroupAscending; } // SORTS GROUPS BY SIZE @@ -314,7 +372,7 @@ public class AlignmentSorter // NOW ADD SEQUENCES MAINTAINING ALIGNMENT ORDER // ///////////////////////////////////////////// - List seqs = new ArrayList(); + List seqs = new ArrayList<>(); for (int i = 0; i < groups.size(); i++) { @@ -327,7 +385,7 @@ public class AlignmentSorter } } - if (sortGroupAscending) + if (as.sortGroupAscending) { setOrder(align, seqs); } @@ -356,7 +414,7 @@ public class AlignmentSorter // tmp2 = tmp.retainAll(mask); // return tmp2.addAll(mask.removeAll(tmp2)) - ArrayList seqs = new ArrayList(); + ArrayList seqs = new ArrayList<>(); int i, idx; boolean[] tmask = new boolean[mask.size()]; @@ -400,22 +458,25 @@ public class AlignmentSorter // Get an ordered vector of sequences which may also be present in align List tmp = order.getOrder(); - if (lastOrder == order) + AlignmentSorter as = getInstance(); + + if (as.lastOrder == order) { - sortOrderAscending = !sortOrderAscending; + as.sortOrderAscending = !as.sortOrderAscending; } else { - sortOrderAscending = true; + as.sortOrderAscending = true; } - if (sortOrderAscending) + if (as.sortOrderAscending) { setOrder(align, tmp); } else { - setReverseOrder(align, vectorSubsetToArray(tmp, align.getSequences())); + setReverseOrder(align, + vectorSubsetToArray(tmp, align.getSequences())); } } @@ -434,7 +495,7 @@ public class AlignmentSorter { int nSeq = align.getHeight(); - List tmp = new ArrayList(); + List tmp = new ArrayList<>(); tmp = _sortByTree(tree.getTopNode(), tmp, align.getSequences()); @@ -450,12 +511,9 @@ public class AlignmentSorter if (tmp.size() != nSeq) { - System.err - .println("WARNING: tmp.size()=" - + tmp.size() - + " != nseq=" - + nSeq - + " in getOrderByTree - tree contains sequences not in alignment"); + System.err.println("WARNING: tmp.size()=" + tmp.size() + " != nseq=" + + nSeq + + " in getOrderByTree - tree contains sequences not in alignment"); } } @@ -474,24 +532,27 @@ public class AlignmentSorter { List tmp = getOrderByTree(align, tree); + AlignmentSorter as = getInstance(); + // tmp should properly permute align with tree. - if (lastTree != tree) + if (as.lastTree != tree) { - sortTreeAscending = true; - lastTree = tree; + as.sortTreeAscending = true; + as.lastTree = tree; } else { - sortTreeAscending = !sortTreeAscending; + as.sortTreeAscending = !as.sortTreeAscending; } - if (sortTreeAscending) + if (as.sortTreeAscending) { setOrder(align, tmp); } else { - setReverseOrder(align, vectorSubsetToArray(tmp, align.getSequences())); + setReverseOrder(align, + vectorSubsetToArray(tmp, align.getSequences())); } } @@ -658,9 +719,12 @@ public class AlignmentSorter } jalview.util.QuickSort.sort(scores, seqs); - if (lastSortByAnnotation != scoreLabel) + + AlignmentSorter as = getInstance(); + + if (as.lastSortByAnnotation != scoreLabel) { - lastSortByAnnotation = scoreLabel; + as.lastSortByAnnotation = scoreLabel; setOrder(alignment, seqs); } else @@ -670,39 +734,6 @@ public class AlignmentSorter } /** - * types of feature ordering: Sort by score : average score - or total score - - * over all features in region Sort by feature label text: (or if null - - * feature type text) - numerical or alphabetical Sort by feature density: - * based on counts - ignoring individual text or scores for each feature - */ - public static String FEATURE_SCORE = "average_score"; - - public static String FEATURE_LABEL = "text"; - - public static String FEATURE_DENSITY = "density"; - - private static boolean containsIgnoreCase(final String lab, - final List labs) - { - if (labs == null) - { - return true; - } - if (lab == null) - { - return false; - } - for (String label : labs) - { - if (lab.equalsIgnoreCase(label)) - { - return true; - } - } - return false; - } - - /** * Sort sequences by feature score or density, optionally restricted by * feature types, feature groups, or alignment start/end positions. *

@@ -755,13 +786,10 @@ public class AlignmentSorter * get sequence residues overlapping column region * and features for residue positions and specified types */ - // TODO new method findPositions(startCol, endCol)? JAL-2544 - int startResidue = seqs[i].findPosition(startCol); - int endResidue = seqs[i].findPosition(endCol); String[] types = featureTypes == null ? null : featureTypes .toArray(new String[featureTypes.size()]); - List sfs = seqs[i].findFeatures(startResidue, - endResidue, types); + List sfs = seqs[i].findFeatures(startCol + 1, + endCol + 1, types); seqScores[i] = 0; scores[i] = 0.0; @@ -772,18 +800,6 @@ public class AlignmentSorter SequenceFeature sf = it.next(); /* - * double-check feature overlaps columns (JAL-2544) - * (could avoid this with a findPositions(fromCol, toCol) method) - * findIndex returns base 1 column values, startCol/endCol are base 0 - */ - if (seqs[i].findIndex(sf.getBegin()) > endCol + 1 - || seqs[i].findIndex(sf.getEnd()) < startCol + 1) - { - it.remove(); - continue; - } - - /* * accept all features with null or empty group, otherwise * check group is one of the currently visible groups */ @@ -845,6 +861,8 @@ public class AlignmentSorter } } + boolean doSort = false; + if (FEATURE_SCORE.equals(method)) { if (hasScores == 0) @@ -870,7 +888,7 @@ public class AlignmentSorter } } } - QuickSort.sortByDouble(scores, seqs, sortByFeatureAscending); + doSort = true; } else if (FEATURE_DENSITY.equals(method)) { @@ -882,9 +900,12 @@ public class AlignmentSorter // System.err.println("Sorting on Density: seq "+seqs[i].getName()+ // " Feats: "+featureCount+" Score : "+scores[i]); } - QuickSort.sortByDouble(scores, seqs, sortByFeatureAscending); + doSort = true; + } + if (doSort) + { + QuickSort.sortByDouble(scores, seqs, getInstance().sortByFeatureAscending); } - setOrder(alignment, seqs); } @@ -919,16 +940,17 @@ public class AlignmentSorter /* * if resorting on the same criteria, toggle sort order */ - if (sortByFeatureCriteria == null - || !scoreCriteria.equals(sortByFeatureCriteria)) + AlignmentSorter as = getInstance(); + if (as.sortByFeatureCriteria == null + || !scoreCriteria.equals(as.sortByFeatureCriteria)) { - sortByFeatureAscending = true; + as.sortByFeatureAscending = true; } else { - sortByFeatureAscending = !sortByFeatureAscending; + as.sortByFeatureAscending = !as.sortByFeatureAscending; } - sortByFeatureCriteria = scoreCriteria; + as.sortByFeatureCriteria = scoreCriteria; } }