/* * Jalview - A Sequence Alignment Editor and Viewer * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ package jalview.analysis; import jalview.datamodel.*; import jalview.io.*; import jalview.util.*; import java.util.*; /** Data structure to hold and manipulate a multiple sequence alignment */ public class AlignmentSorter { /** */ static boolean sortIdAscending = true; static int lastGroupHash = 0; static boolean sortGroupAscending = true; static AlignmentOrder lastOrder = null; static boolean sortOrderAscending = true; static NJTree lastTree = null; static boolean sortTreeAscending = true; private AlignmentSorter() { try { jbInit(); } catch (Exception ex) { ex.printStackTrace(); } } public static void sortGroups(AlignmentI align) { Vector groups = align.getGroups(); int nGroup = groups.size(); float[] arr = new float[nGroup]; Object[] s = new Object[nGroup]; for (int i = 0; i < nGroup; i++) { arr[i] = ((SequenceGroup) groups.elementAt(i)).getSize(); s[i] = groups.elementAt(i); } QuickSort.sort(arr, s); // align..setGroups(newg); } /** * Sort by Percentage Identity * * @param align AlignmentI * @param s SequenceI */ public static void sortByPID(AlignmentI align, SequenceI s) { int nSeq = align.getHeight(); float[] scores = new float[nSeq]; SequenceI[] seqs = new SequenceI[nSeq]; for (int i = 0; i < nSeq; i++) { scores[i] = Comparison.PID(align.getSequenceAt(i), s); seqs[i] = align.getSequenceAt(i); } QuickSort.sort(scores, 0, scores.length - 1, seqs); setReverseOrder(align, seqs); } private static void setReverseOrder(AlignmentI align, SequenceI[] seqs) { int nSeq = seqs.length; 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 for (int i = 0; i < len; i++) { //SequenceI tmp = seqs[i]; align.getSequences().setElementAt(seqs[nSeq - i - 1], i); align.getSequences().setElementAt(seqs[i], nSeq - i - 1); } } private static void setOrder(AlignmentI align, Vector tmp) { setOrder(align, vectorSubsetToArray(tmp, align.getSequences())); } private static void setOrder(AlignmentI align, SequenceI[] seqs) { // NOTE: DO NOT USE align.setSequenceAt() here - it will NOT work Vector algn = align.getSequences(); for (int i = 0, p = 0; i < seqs.length; i++) algn.setElementAt(seqs[i], p++); } public static void sortByID(AlignmentI align) { int nSeq = align.getHeight(); String[] ids = new String[nSeq]; SequenceI[] seqs = new SequenceI[nSeq]; for (int i = 0; i < nSeq; i++) { ids[i] = align.getSequenceAt(i).getName(); seqs[i] = align.getSequenceAt(i); } QuickSort.sort(ids, seqs); if (sortIdAscending) { setReverseOrder(align, seqs); } else { setOrder(align, seqs); } sortIdAscending = !sortIdAscending; } public static void sortByGroup(AlignmentI align) { Vector groups = align.getGroups(); if (groups.hashCode() != lastGroupHash) { sortGroupAscending = true; lastGroupHash = groups.hashCode(); } else { sortGroupAscending = !sortGroupAscending; } Vector seqs = new Vector(); for (int i = 0; i < groups.size(); i++) { SequenceGroup sg = (SequenceGroup) groups.elementAt(i); for (int j = 0; j < sg.getSize(); j++) { seqs.addElement(sg.getSequenceAt(j)); } } // Deletions can happen so this check may fail /* if (seqs.size() != nSeq) { System.err.println("ERROR: tmp.size() != nseq in sortByGroups"); if (seqs.size() < nSeq) { addStrays(align,seqs); } } */ if (sortGroupAscending) { setOrder(align, seqs); } else { setReverseOrder(align, vectorSubsetToArray(seqs, align.getSequences())); } } private static SequenceI[] vectorToArray(Vector tmp) { SequenceI[] seqs = new SequenceI[tmp.size()]; for (int i = 0; i < tmp.size(); i++) { seqs[i] = (SequenceI) tmp.elementAt(i); } return seqs; } private static SequenceI[] vectorSubsetToArray(Vector tmp, Vector mask) { Vector seqs = new Vector(); int i; int m; int p; boolean[] tmask = new boolean[m = mask.size()]; for (i = 0; i < m; i++) tmask[i] = true; for (i = 0; i < tmp.size(); i++) { Object sq; if (mask.contains(sq = tmp.elementAt(i))) { tmask[mask.indexOf(sq)] = false; seqs.addElement(sq); m--; } } for (i = 0; i < tmask.length; i++) if (tmask[i]) { seqs.addElement(mask.elementAt(i)); } return vectorToArray(seqs); } public static void sortBy(AlignmentI align, AlignmentOrder order) { // Get an ordered vector of sequences which may also be present in align Vector tmp = order.getOrder(); // if (tmp.size()