X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fanalysis%2FAlignmentSorter.java;h=6b8ea4abb3fd70426f3e078ab04e6d6be75cef45;hb=3d0101179759ef157b088ea135423cd909512d9f;hp=103fbf09c1fc4d6b1cb502763d4ebf1b301f93ec;hpb=838e4f91d4a53dd315640dbc9ff6ef7a815ee576;p=jalview.git diff --git a/src/jalview/analysis/AlignmentSorter.java b/src/jalview/analysis/AlignmentSorter.java index 103fbf0..6b8ea4a 100755 --- a/src/jalview/analysis/AlignmentSorter.java +++ b/src/jalview/analysis/AlignmentSorter.java @@ -1,6 +1,6 @@ /* - * Jalview - A Sequence Alignment Editor and Viewer (Version 2.9.0b1) - * Copyright (C) 2015 The Jalview Authors + * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$) + * Copyright (C) $$Year-Rel$$ The Jalview Authors * * This file is part of Jalview. * @@ -20,6 +20,8 @@ */ package jalview.analysis; +import jalview.analysis.scoremodels.PIDModel; +import jalview.analysis.scoremodels.SimilarityParams; import jalview.datamodel.AlignmentAnnotation; import jalview.datamodel.AlignmentI; import jalview.datamodel.AlignmentOrder; @@ -27,11 +29,11 @@ import jalview.datamodel.SequenceFeature; import jalview.datamodel.SequenceGroup; import jalview.datamodel.SequenceI; import jalview.datamodel.SequenceNode; -import jalview.util.Comparison; import jalview.util.MessageManager; import jalview.util.QuickSort; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; /** @@ -65,7 +67,7 @@ public class AlignmentSorter static boolean sortOrderAscending = true; - static NJTree lastTree = null; + static TreeModel lastTree = null; static boolean sortTreeAscending = true; @@ -86,46 +88,30 @@ public class AlignmentSorter private static boolean sortLengthAscending; /** - * Sort by Percentage Identity w.r.t. s + * Sorts sequences in the alignment by Percentage Identity with the given + * reference sequence, sorting the highest identity to the top * * @param align * AlignmentI * @param s * SequenceI - * @param tosort - * sequences from align that are to be sorted. - */ - public static void sortByPID(AlignmentI align, SequenceI s, - SequenceI[] tosort) - { - sortByPID(align, s, tosort, 0, -1); - } - - /** - * Sort by Percentage Identity w.r.t. s - * - * @param align - * AlignmentI - * @param s - * SequenceI - * @param tosort - * sequences from align that are to be sorted. - * @param start - * start column (0 for beginning * @param end */ - public static void sortByPID(AlignmentI align, SequenceI s, - SequenceI[] tosort, int start, int end) + public static void sortByPID(AlignmentI align, SequenceI s) { int nSeq = align.getHeight(); float[] scores = new float[nSeq]; SequenceI[] seqs = new SequenceI[nSeq]; + String refSeq = s.getSequenceAsString(); + SimilarityParams pidParams = new SimilarityParams(true, true, true, + true); for (int i = 0; i < nSeq; i++) { - scores[i] = Comparison.PID(align.getSequenceAt(i) - .getSequenceAsString(), s.getSequenceAsString()); + scores[i] = (float) PIDModel.computePID( + align.getSequenceAt(i).getSequenceAsString(), refSeq, + pidParams); seqs[i] = align.getSequenceAt(i); } @@ -431,7 +417,8 @@ public class AlignmentSorter } else { - setReverseOrder(align, vectorSubsetToArray(tmp, align.getSequences())); + setReverseOrder(align, + vectorSubsetToArray(tmp, align.getSequences())); } } @@ -446,7 +433,7 @@ public class AlignmentSorter * @return DOCUMENT ME! */ private static List getOrderByTree(AlignmentI align, - NJTree tree) + TreeModel tree) { int nSeq = align.getHeight(); @@ -466,12 +453,9 @@ public class AlignmentSorter if (tmp.size() != nSeq) { - System.err - .println("WARNING: tmp.size()=" - + tmp.size() - + " != nseq=" - + nSeq - + " in getOrderByTree - tree contains sequences not in alignment"); + System.err.println("WARNING: tmp.size()=" + tmp.size() + " != nseq=" + + nSeq + + " in getOrderByTree - tree contains sequences not in alignment"); } } @@ -486,7 +470,7 @@ public class AlignmentSorter * @param tree * tree which has */ - public static void sortByTree(AlignmentI align, NJTree tree) + public static void sortByTree(AlignmentI align, TreeModel tree) { List tmp = getOrderByTree(align, tree); @@ -507,7 +491,8 @@ public class AlignmentSorter } else { - setReverseOrder(align, vectorSubsetToArray(tmp, align.getSequences())); + setReverseOrder(align, + vectorSubsetToArray(tmp, align.getSequences())); } } @@ -718,13 +703,15 @@ public class AlignmentSorter public static void sortByFeature(String featureLabel, String groupLabel, int start, int stop, AlignmentI alignment, String method) { - sortByFeature(featureLabel == null ? null - : new String[] { featureLabel }, groupLabel == null ? null - : new String[] { groupLabel }, start, stop, alignment, method); + sortByFeature( + featureLabel == null ? null : Arrays.asList(new String[] + { featureLabel }), + groupLabel == null ? null : Arrays.asList(new String[] + { groupLabel }), start, stop, alignment, method); } private static boolean containsIgnoreCase(final String lab, - final String[] labs) + final List labs) { if (labs == null) { @@ -734,9 +721,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; } @@ -744,31 +731,51 @@ public class AlignmentSorter return false; } - public static void sortByFeature(String[] featureLabels, - String[] groupLabels, int start, int stop, AlignmentI alignment, - String method) + 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( - MessageManager - .getString("error.implementation_error_sortbyfeature")); + throw new Error(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) + { + for (String label : featureLabels) + { + scoreLabel.append(label); + } + } + if (groupLabels != null) + { + for (String label : groupLabels) + { + scoreLabel.append(label); + } + } + + /* + * if resorting the same feature, toggle sort order + */ + if (lastSortByFeatureScore == null + || !scoreLabel.toString().equals(lastSortByFeatureScore)) { - scoreLabel.append(featureLabels[i] == null ? "null" - : featureLabels[i]); + sortByFeatureScoreAscending = true; } - for (int i = 0; groupLabels != null && i < groupLabels.length; i++) + else { - scoreLabel.append(groupLabels[i] == null ? "null" : groupLabels[i]); + sortByFeatureScoreAscending = !sortByFeatureScoreAscending; } + lastSortByFeatureScore = scoreLabel.toString(); + SequenceI[] seqs = alignment.getSequencesArray(); boolean[] hasScore = new boolean[seqs.length]; // per sequence score @@ -802,17 +809,22 @@ public class AlignmentSorter for (int f = 0; f < sf.length; f++) { // filter for selection criteria - if ( - // ignore features outwith alignment start-stop positions. - (sf[f].end < sstart || sf[f].begin > sstop) || - // or ignore based on selection criteria - (featureLabels != null && !AlignmentSorter - .containsIgnoreCase(sf[f].type, featureLabels)) - || (groupLabels != null - // problem here: we cannot eliminate null feature group features - && (sf[f].getFeatureGroup() != null && !AlignmentSorter - .containsIgnoreCase(sf[f].getFeatureGroup(), - groupLabels)))) + SequenceFeature feature = sf[f]; + + /* + * double-check feature overlaps columns (JAL-2544) + * (could avoid this with a findPositions(fromCol, toCol) method) + * findIndex returns base 1 column values, startCol/endCol are base 0 + */ + boolean noOverlap = seqs[i].findIndex(feature.getBegin()) > stop + 1 + || seqs[i].findIndex(feature.getEnd()) < start + 1; + boolean skipFeatureType = featureLabels != null && !AlignmentSorter + .containsIgnoreCase(feature.type, featureLabels); + boolean skipFeatureGroup = groupLabels != null + && (feature.getFeatureGroup() != null + && !AlignmentSorter.containsIgnoreCase( + feature.getFeatureGroup(), groupLabels)); + if (noOverlap || skipFeatureType || skipFeatureGroup) { // forget about this feature sf[f] = null; @@ -821,7 +833,7 @@ public class AlignmentSorter else { // or, also take a look at the scores if necessary. - if (!ignoreScore && !Float.isNaN(sf[f].getScore())) + if (!ignoreScore && !Float.isNaN(feature.getScore())) { if (seqScores[i] == 0) { @@ -829,7 +841,7 @@ public class AlignmentSorter } seqScores[i]++; hasScore[i] = true; - scores[i] += sf[f].getScore(); // take the first instance of this + scores[i] += feature.getScore(); // take the first instance of this // score. } } @@ -852,10 +864,11 @@ public class AlignmentSorter String[] labs = new String[fs.length]; for (int l = 0; l < labs.length; l++) { - labs[l] = (fs[l].getDescription() != null ? fs[l] - .getDescription() : fs[l].getType()); + 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]) @@ -898,32 +911,27 @@ 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 { @@ -933,24 +941,8 @@ public class AlignmentSorter 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); } }