labs)
{
if (labs == null)
{
@@ -738,9 +692,9 @@ public class AlignmentSorter
{
return false;
}
- for (int q = 0; q < labs.length; q++)
+ for (String label : labs)
{
- if (labs[q] != null && lab.equalsIgnoreCase(labs[q]))
+ if (lab.equalsIgnoreCase(label))
{
return true;
}
@@ -748,30 +702,72 @@ public class AlignmentSorter
return false;
}
- public static void sortByFeature(String[] featureLabels,
- String[] groupLabels, int start, int stop, AlignmentI alignment,
- String method)
+ /**
+ * Sort sequences by feature score or density, optionally restricted by
+ * feature types, feature groups, or alignment start/end positions.
+ *
+ * If the sort is repeated for the same combination of types and groups, sort
+ * order is reversed.
+ *
+ * @param featureLabels
+ * a list of feature types to include (or null for all)
+ * @param groupLabels
+ * a list of feature groups to include (or null for all)
+ * @param start
+ * start column position to include (base zero)
+ * @param stop
+ * end column position to include (base zero)
+ * @param alignment
+ * the alignment to be sorted
+ * @param method
+ * either "average_score" or "density" ("text" not yet implemented)
+ */
+ public static void sortByFeature(List featureLabels,
+ List groupLabels, int start, int stop,
+ AlignmentI alignment, String method)
{
if (method != FEATURE_SCORE && method != FEATURE_LABEL
&& method != FEATURE_DENSITY)
{
throw new Error(
- "Implementation Error - sortByFeature method must be one of FEATURE_SCORE, FEATURE_LABEL or FEATURE_DENSITY.");
+ MessageManager
+ .getString("error.implementation_error_sortbyfeature"));
}
+
boolean ignoreScore = method != FEATURE_SCORE;
StringBuffer scoreLabel = new StringBuffer();
scoreLabel.append(start + stop + method);
// This doesn't quite work yet - we'd like to have a canonical ordering that
// can be preserved from call to call
- for (int i = 0; featureLabels != null && i < featureLabels.length; i++)
+ if (featureLabels != null)
{
- scoreLabel.append(featureLabels[i] == null ? "null"
- : featureLabels[i]);
+ for (String label : featureLabels)
+ {
+ scoreLabel.append(label);
+ }
}
- for (int i = 0; groupLabels != null && i < groupLabels.length; i++)
+ if (groupLabels != null)
{
- scoreLabel.append(groupLabels[i] == null ? "null" : groupLabels[i]);
+ for (String label : groupLabels)
+ {
+ scoreLabel.append(label);
+ }
}
+
+ /*
+ * if resorting the same feature, toggle sort order
+ */
+ if (lastSortByFeatureScore == null
+ || !scoreLabel.toString().equals(lastSortByFeatureScore))
+ {
+ sortByFeatureScoreAscending = true;
+ }
+ else
+ {
+ sortByFeatureScoreAscending = !sortByFeatureScoreAscending;
+ }
+ lastSortByFeatureScore = scoreLabel.toString();
+
SequenceI[] seqs = alignment.getSequencesArray();
boolean[] hasScore = new boolean[seqs.length]; // per sequence score
@@ -784,10 +780,6 @@ public class AlignmentSorter
for (int i = 0; i < seqs.length; i++)
{
SequenceFeature[] sf = seqs[i].getSequenceFeatures();
- if (sf == null && seqs[i].getDatasetSequence() != null)
- {
- sf = seqs[i].getDatasetSequence().getSequenceFeatures();
- }
if (sf == null)
{
sf = new SequenceFeature[0];
@@ -828,7 +820,7 @@ public class AlignmentSorter
else
{
// or, also take a look at the scores if necessary.
- if (!ignoreScore && sf[f].getScore() != Float.NaN)
+ if (!ignoreScore && !Float.isNaN(sf[f].getScore()))
{
if (seqScores[i] == 0)
{
@@ -862,7 +854,7 @@ public class AlignmentSorter
labs[l] = (fs[l].getDescription() != null ? fs[l]
.getDescription() : fs[l].getType());
}
- jalview.util.QuickSort.sort(labs, ((Object[]) feats[i]));
+ QuickSort.sort(labs, ((Object[]) feats[i]));
}
}
if (hasScore[i])
@@ -905,58 +897,38 @@ public class AlignmentSorter
}
else
{
- int nf = (feats[i] == null) ? 0
- : ((SequenceFeature[]) feats[i]).length;
- // System.err.println("Sorting on Score: seq "+seqs[i].getName()+
- // " Feats: "+nf+" Score : "+scores[i]);
+ // int nf = (feats[i] == null) ? 0
+ // : ((SequenceFeature[]) feats[i]).length;
+ // // System.err.println("Sorting on Score: seq " +
+ // seqs[i].getName()
+ // + " Feats: " + nf + " Score : " + scores[i]);
}
}
}
-
- jalview.util.QuickSort.sort(scores, seqs);
+ QuickSort.sortByDouble(scores, seqs, sortByFeatureScoreAscending);
}
else if (method == FEATURE_DENSITY)
{
-
- // break ties between equivalent numbers for adjacent sequences by adding
- // 1/Nseq*i on the original order
- double fr = 0.9 / (1.0 * seqs.length);
for (int i = 0; i < seqs.length; i++)
{
- double nf;
- scores[i] = (0.05 + fr * i)
- + (nf = ((feats[i] == null) ? 0.0
- : 1.0 * ((SequenceFeature[]) feats[i]).length));
+ int featureCount = feats[i] == null ? 0
+ : ((SequenceFeature[]) feats[i]).length;
+ scores[i] = featureCount;
// System.err.println("Sorting on Density: seq "+seqs[i].getName()+
- // " Feats: "+nf+" Score : "+scores[i]);
+ // " Feats: "+featureCount+" Score : "+scores[i]);
}
- jalview.util.QuickSort.sort(scores, seqs);
+ QuickSort.sortByDouble(scores, seqs, sortByFeatureScoreAscending);
}
else
{
if (method == FEATURE_LABEL)
{
- throw new Error("Not yet implemented.");
+ throw new Error(
+ MessageManager.getString("error.not_yet_implemented"));
}
}
- if (lastSortByFeatureScore == null
- || !scoreLabel.toString().equals(lastSortByFeatureScore))
- {
- sortByFeatureScoreAscending = true;
- }
- else
- {
- sortByFeatureScoreAscending = !sortByFeatureScoreAscending;
- }
- if (sortByFeatureScoreAscending)
- {
- setOrder(alignment, seqs);
- }
- else
- {
- setReverseOrder(alignment, seqs);
- }
- lastSortByFeatureScore = scoreLabel.toString();
+
+ setOrder(alignment, seqs);
}
}