import jalview.analysis.scoremodels.PIDModel;
import jalview.analysis.scoremodels.SimilarityParams;
+import jalview.bin.Instance;
import jalview.datamodel.AlignmentAnnotation;
import jalview.datamodel.AlignmentI;
import jalview.datamodel.AlignmentOrder;
*/
public class AlignmentSorter
{
+
+ private AlignmentSorter()
+ {
+ // singleton
+ }
+ /**
+ * 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";
+
+ public static AlignmentSorter getInstance()
+ {
+ Instance j = Instance.getInstance();
+ return (j.alignmentSorter == null
+ ? j.alignmentSorter = new AlignmentSorter()
+ : j.alignmentSorter);
+ }
+
/*
* 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
}
// 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++)
{
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++)
{
QuickSort.sort(ids, seqs);
- if (sortIdAscending)
+ AlignmentSorter as = getInstance();
+ if (as.sortIdAscending)
{
setReverseOrder(align, seqs);
}
setOrder(align, seqs);
}
- sortIdAscending = !sortIdAscending;
+ as.sortIdAscending = !as.sortIdAscending;
}
/**
QuickSort.sort(length, seqs);
- if (sortLengthAscending)
+ AlignmentSorter as = getInstance();
+
+ if (as.sortLengthAscending)
{
setReverseOrder(align, seqs);
}
setOrder(align, seqs);
}
- sortLengthAscending = !sortLengthAscending;
+ as.sortLengthAscending = !as.sortLengthAscending;
}
/**
{
// MAINTAINS ORIGNAL SEQUENCE ORDER,
// ORDERS BY GROUP SIZE
- List<SequenceGroup> groups = new ArrayList<SequenceGroup>();
+ List<SequenceGroup> groups = new ArrayList<>();
+
+ AlignmentSorter as = getInstance();
- if (groups.hashCode() != lastGroupHash)
+ 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
// 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++)
{
}
}
- if (sortGroupAscending)
+ if (as.sortGroupAscending)
{
setOrder(align, seqs);
}
// 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()];
// Get an ordered vector of sequences which may also be present in align
List<SequenceI> 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);
}
{
int nSeq = align.getHeight();
- List<SequenceI> tmp = new ArrayList<SequenceI>();
+ List<SequenceI> tmp = new ArrayList<>();
tmp = _sortByTree(tree.getTopNode(), tmp, align.getSequences());
{
List<SequenceI> 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);
}
}
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
}
/**
- * 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";
-
- /**
* Sort sequences by feature score or density, optionally restricted by
* feature types, feature groups, or alignment start/end positions.
* <p>
}
}
+ boolean doSort = false;
+
if (FEATURE_SCORE.equals(method))
{
if (hasScores == 0)
}
}
}
- QuickSort.sortByDouble(scores, seqs, sortByFeatureAscending);
+ doSort = true;
}
else if (FEATURE_DENSITY.equals(method))
{
// 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);
}
/*
* 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;
}
}