+ /**
+ * Set the alignment's sequences list to contain the sequences from a
+ * temporary list, first adding all the elements from the tmp list, then adding all sequences in the alignment that
+ * are not in the list. Option to do the final sort either in order or in reverse order.
+ *
+ * @param align The alignment being sorted
+ * @param tmp
+ * the temporary sequence list
+ * @param ascending
+ * false for reversed order; only sequences already in
+ * the alignment will be used (which is actually already guaranteed
+ * by vectorSubsetToArray)
+ */
+ private static void set(AlignmentI align, List<SequenceI> tmp,
+ boolean ascending)
+ {
+ set(align, vectorSubsetToArray(align.getSequences(), tmp), ascending);
+ }
+
+ /**
+ * Set the alignment's sequences list to contain these sequences, either in
+ * this order or its reverse.
+ *
+ * @param align
+ * @param seqs
+ * the new sequence array
+ * @param ascending
+ * false for reversed order; if ascending, only sequences already in
+ * the alignment will be used; if descending, then a direct 1:1
+ * replacement is made
+ */
+ private static void set(AlignmentI align, SequenceI[] seqs,
+ boolean ascending)
+ {
+ if (ascending)
+ {
+ setOrder(align, seqs);
+ }
+ else
+ {
+ setReverseOrder(align, seqs);
+ }
+
+ }
+
+ /**
+ * Replace the alignment's sequences with values in an array, clearing the
+ * alignment's sequence list and filtering for sequences that are actually in
+ * the alignment already.
+ *
+ * @param align
+ * the Alignment
+ * @param seqs
+ * the array of replacement values, of any length
+ */
+ public static void setOrder(AlignmentI align, SequenceI[] seqs)
+ {
+ // NOTE: DO NOT USE align.setSequenceAt() here - it will NOT work
+ List<SequenceI> seqList = align.getSequences();
+ synchronized (seqList)
+ {
+ List<SequenceI> tmp = new ArrayList<>();
+
+ for (int i = 0; i < seqs.length; i++)
+ {
+ if (seqList.contains(seqs[i]))
+ {
+ tmp.add(seqs[i]);
+ }
+ }
+
+ seqList.clear();
+ // User may have hidden seqs, then clicked undo or redo
+ for (int i = 0; i < tmp.size(); i++)
+ {
+ seqList.add(tmp.get(i));
+ }
+ }
+ }
+
+ /**
+ * Replace the alignment's sequences or a subset of those sequences with
+ * values in an array in reverse order. All sequences are replaced; no check
+ * is made that these sequences are in the alignment already.
+ *
+ * @param align
+ * the Alignment
+ * @param seqs
+ * the array of replacement values, length must be less than or equal
+ * to Alignment.sequences.size()
+ */
+ 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<SequenceI> seqList = align.getSequences();
+ synchronized (seqList)
+ {
+ for (int i = 0; i < len; i++)
+ {
+ // SequenceI tmp = seqs[i];
+ seqList.set(i, seqs[nSeq - i - 1]);
+ seqList.set(nSeq - i - 1, seqs[i]);
+ }
+ }
+ }
+
+ /**
+ * Create and array of reordered sequences in order first from tmp that are
+ * present in seqList already, then, after that, any remaining sequences in
+ * seqList not in tmp. Any sequences in tmp that are not in seqList already
+ * are discarded.
+ *
+ * @param seqList
+ * thread safe collection of sequences originally in the alignment
+ * @param tmp
+ * thread safe collection of sequences or subsequences possibly in
+ * seqList
+ *
+ * @return intersect(tmp,seqList)+intersect(complement(tmp),seqList)
+ */
+ private static SequenceI[] vectorSubsetToArray(List<SequenceI> seqList,
+ List<SequenceI> tmp)
+ {
+ ArrayList<SequenceI> seqs = new ArrayList<>();
+ int n = seqList.size();
+ BitSet bs = new BitSet(n);
+ bs.set(0, n);
+ for (int i = 0, nt = tmp.size(); i < nt; i++)
+ {
+ SequenceI sq = tmp.get(i);
+ int idx = seqList.indexOf(sq);
+ if (idx >= 0 && bs.get(idx))
+ {
+ seqs.add(sq);
+ bs.clear(idx);
+ }
+ }
+
+ for (int i = bs.nextSetBit(0); i >= 0; i = bs.nextSetBit(i + 1))
+ {
+ seqs.add(seqList.get(i));
+ }
+
+ return seqs.toArray(new SequenceI[seqs.size()]);
+ }
+