X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fanalysis%2FAnnotationSorter.java;h=81398ebcd8683cdf5bbc54c5d3ae28ecef58a438;hb=de75573aa38c4b40686256d4332c3a069b6a98ca;hp=28fa1f8a2667bb42a37b0f8e6e75bf32e90de2c1;hpb=7c5e7c1c51a48eab1b217a221df82748c35b76b1;p=jalview.git diff --git a/src/jalview/analysis/AnnotationSorter.java b/src/jalview/analysis/AnnotationSorter.java index 28fa1f8..81398eb 100644 --- a/src/jalview/analysis/AnnotationSorter.java +++ b/src/jalview/analysis/AnnotationSorter.java @@ -6,6 +6,8 @@ import jalview.datamodel.SequenceI; import java.util.Arrays; import java.util.Comparator; +import java.util.HashMap; +import java.util.Map; /** * A helper class to sort all annotations associated with an alignment in @@ -56,10 +58,15 @@ public class AnnotationSorter } } + // the alignment with respect to which annotations are sorted private final AlignmentI alignment; + // user preference for placement of non-sequence annotations private boolean showAutocalcAbove; + // working map of sequence index in alignment + private final Map sequenceIndices = new HashMap(); + /** * Constructor given an alignment and the location (top or bottom) of * Consensus and similar. @@ -202,6 +209,13 @@ public class AnnotationSorter public void sort(AlignmentAnnotation[] alignmentAnnotations, SequenceAnnotationOrder order) { + if (alignmentAnnotations == null) + { + return; + } + // cache 'alignment sequence position' for the annotations + saveSequenceIndices(alignmentAnnotations); + Comparator comparator = getComparator(order); if (alignmentAnnotations != null) @@ -214,6 +228,26 @@ public class AnnotationSorter } /** + * Calculate and save in a temporary map the position of each annotation's + * sequence (if it has one) in the alignment. Faster to do this once than for + * every annotation comparison. + * + * @param alignmentAnnotations + */ + private void saveSequenceIndices( + AlignmentAnnotation[] alignmentAnnotations) + { + sequenceIndices.clear(); + for (AlignmentAnnotation ann : alignmentAnnotations) { + SequenceI seq = ann.sequenceRef; + if (seq != null) { + int index = AlignmentUtils.getSequenceIndex(alignment, seq); + sequenceIndices.put(seq, index); + } + } + } + + /** * Get the comparator for the specified sort order. * * @param order @@ -299,8 +333,8 @@ public class AnnotationSorter return showAutocalcAbove ? 1 : -1; } // get sequence index - but note -1 means 'at end' so needs special handling - int index1 = AlignmentUtils.getSequenceIndex(alignment, seq1); - int index2 = AlignmentUtils.getSequenceIndex(alignment, seq2); + int index1 = sequenceIndices.get(seq1); + int index2 = sequenceIndices.get(seq2); if (index1 == index2) { return 0;