From 933e28e48b4f3bf98851cedd99ef345d45bf25a3 Mon Sep 17 00:00:00 2001 From: Thomas <170013569@dundee.ac.uk> Date: Fri, 12 Jul 2019 14:43:55 +0100 Subject: [PATCH] JAL-2629 add sort by evalue and bit score --- resources/lang/Messages.properties | 2 + src/jalview/analysis/AlignmentSorter.java | 98 +++++++++++++++++++++++++++-- src/jalview/appletgui/AlignFrame.java | 32 ++++++++++ src/jalview/gui/AlignFrame.java | 22 +++++++ src/jalview/jbgui/GAlignFrame.java | 30 +++++++++ 5 files changed, 179 insertions(+), 5 deletions(-) diff --git a/resources/lang/Messages.properties b/resources/lang/Messages.properties index a2d5ab8..2c0fbb9 100644 --- a/resources/lang/Messages.properties +++ b/resources/lang/Messages.properties @@ -60,6 +60,8 @@ action.boxes = Boxes action.text = Text action.by_pairwise_id = By Pairwise Identity action.by_id = By Id +action.by_evalue = By E-Value +action.by_bit_score = By Bit Score action.by_length = By Length action.by_group = By Group action.unmark_as_reference = Unmark as Reference diff --git a/src/jalview/analysis/AlignmentSorter.java b/src/jalview/analysis/AlignmentSorter.java index 8473904..2d05865 100755 --- a/src/jalview/analysis/AlignmentSorter.java +++ b/src/jalview/analysis/AlignmentSorter.java @@ -86,6 +86,10 @@ public class AlignmentSorter private static boolean sortLengthAscending; + private static boolean sortEValueAscending; + + private static boolean sortBitScoreAscending; + /** * Sorts sequences in the alignment by Percentage Identity with the given * reference sequence, sorting the highest identity to the top @@ -182,7 +186,7 @@ public class AlignmentSorter List algn; synchronized (algn = align.getSequences()) { - List tmp = new ArrayList(); + List tmp = new ArrayList<>(); for (int i = 0; i < seqs.length; i++) { @@ -268,6 +272,90 @@ public class AlignmentSorter } /** + * Sorts by sequence evalue. Currently moves all sequences without an evalue to + * the top of the alignment. + * + * @param align + * The alignment object to sort + */ + public static void sortByEValue(AlignmentI align) + { + int nSeq = align.getHeight(); + + double[] evalue = new double[nSeq]; + SequenceI[] seqs = new SequenceI[nSeq]; + + for (int i = 0; i < nSeq; i++) + { + seqs[i] = align.getSequenceAt(i); + AlignmentAnnotation[] ann = seqs[i].getAnnotation("Search Scores"); + if (ann != null) + { + evalue[i] = ann[0].getEValue(); + } + else + { + evalue[i] = -1; + } + } + + QuickSort.sort(evalue, seqs); + + if (sortEValueAscending) + { + setReverseOrder(align, seqs); + } + else + { + setOrder(align, seqs); + } + + sortEValueAscending = !sortEValueAscending; + } + + /** + * Sorts by sequence bit score. Currently moves all sequences without a bit + * score to the top of the alignment + * + * @param align + * The alignment object to sort + */ + public static void sortByBitScore(AlignmentI align) + { + int nSeq = align.getHeight(); + + double[] score = new double[nSeq]; + SequenceI[] seqs = new SequenceI[nSeq]; + + for (int i = 0; i < nSeq; i++) + { + seqs[i] = align.getSequenceAt(i); + AlignmentAnnotation[] ann = seqs[i].getAnnotation("Search Scores"); + if (ann != null) + { + score[i] = ann[0].getEValue(); + } + else + { + score[i] = -1; + } + } + + QuickSort.sort(score, seqs); + + if (sortBitScoreAscending) + { + setReverseOrder(align, seqs); + } + else + { + setOrder(align, seqs); + } + + sortBitScoreAscending = !sortBitScoreAscending; + } + + /** * Sorts the alignment by size of group.
* Maintains the order of sequences in each group by order in given alignment * object. @@ -279,7 +367,7 @@ public class AlignmentSorter { // MAINTAINS ORIGNAL SEQUENCE ORDER, // ORDERS BY GROUP SIZE - List groups = new ArrayList(); + List groups = new ArrayList<>(); if (groups.hashCode() != lastGroupHash) { @@ -315,7 +403,7 @@ public class AlignmentSorter // NOW ADD SEQUENCES MAINTAINING ALIGNMENT ORDER // ///////////////////////////////////////////// - List seqs = new ArrayList(); + List seqs = new ArrayList<>(); for (int i = 0; i < groups.size(); i++) { @@ -357,7 +445,7 @@ public class AlignmentSorter // tmp2 = tmp.retainAll(mask); // return tmp2.addAll(mask.removeAll(tmp2)) - ArrayList seqs = new ArrayList(); + ArrayList seqs = new ArrayList<>(); int i, idx; boolean[] tmask = new boolean[mask.size()]; @@ -436,7 +524,7 @@ public class AlignmentSorter { int nSeq = align.getHeight(); - List tmp = new ArrayList(); + List tmp = new ArrayList<>(); tmp = _sortByTree(tree.getTopNode(), tmp, align.getSequences()); diff --git a/src/jalview/appletgui/AlignFrame.java b/src/jalview/appletgui/AlignFrame.java index 0b78527..b32acef 100644 --- a/src/jalview/appletgui/AlignFrame.java +++ b/src/jalview/appletgui/AlignFrame.java @@ -1324,6 +1324,14 @@ public class AlignFrame extends EmbmenuFrame implements ActionListener, { sortGroupMenuItem_actionPerformed(); } + else if (source == sortEValueMenuItem) + { + sortEValueMenuItem_actionPerformed(); + } + else if (source == sortBitScoreMenuItem) + { + sortBitScoreMenuItem_actionPerformed(); + } else if (source == removeRedundancyMenuItem) { removeRedundancyMenuItem_actionPerformed(); @@ -2772,6 +2780,26 @@ public class AlignFrame extends EmbmenuFrame implements ActionListener, } + public void sortEValueMenuItem_actionPerformed() + { + SequenceI[] oldOrder = viewport.getAlignment().getSequencesArray(); + AlignmentSorter.sortByEValue(viewport.getAlignment()); + addHistoryItem(new OrderCommand("Group Sort", oldOrder, + viewport.getAlignment())); + alignPanel.paintAlignment(true, false); + + } + + public void sortBitScoreMenuItem_actionPerformed() + { + SequenceI[] oldOrder = viewport.getAlignment().getSequencesArray(); + AlignmentSorter.sortByBitScore(viewport.getAlignment()); + addHistoryItem(new OrderCommand("Group Sort", oldOrder, + viewport.getAlignment())); + alignPanel.paintAlignment(true, false); + + } + public void removeRedundancyMenuItem_actionPerformed() { new RedundancyPanel(alignPanel); @@ -3122,6 +3150,10 @@ public class AlignFrame extends EmbmenuFrame implements ActionListener, MenuItem sortGroupMenuItem = new MenuItem(); + MenuItem sortEValueMenuItem = new MenuItem(); + + MenuItem sortBitScoreMenuItem = new MenuItem(); + MenuItem removeRedundancyMenuItem = new MenuItem(); MenuItem pairwiseAlignmentMenuItem = new MenuItem(); diff --git a/src/jalview/gui/AlignFrame.java b/src/jalview/gui/AlignFrame.java index a429ece..796957e 100644 --- a/src/jalview/gui/AlignFrame.java +++ b/src/jalview/gui/AlignFrame.java @@ -3786,6 +3786,28 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener, alignPanel.paintAlignment(true, false); } + @Override + public void sortEValueMenuItem_actionPerformed(ActionEvent e) + { + SequenceI[] oldOrder = viewport.getAlignment().getSequencesArray(); + AlignmentSorter.sortByEValue(viewport.getAlignment()); + addHistoryItem(new OrderCommand("Group Sort", oldOrder, + viewport.getAlignment())); + alignPanel.paintAlignment(true, false); + + } + + @Override + public void sortBitScoreMenuItem_actionPerformed(ActionEvent e) + { + SequenceI[] oldOrder = viewport.getAlignment().getSequencesArray(); + AlignmentSorter.sortByBitScore(viewport.getAlignment()); + addHistoryItem(new OrderCommand("Group Sort", oldOrder, + viewport.getAlignment())); + alignPanel.paintAlignment(true, false); + + } + /** * DOCUMENT ME! * diff --git a/src/jalview/jbgui/GAlignFrame.java b/src/jalview/jbgui/GAlignFrame.java index 62165cd..579da08 100755 --- a/src/jalview/jbgui/GAlignFrame.java +++ b/src/jalview/jbgui/GAlignFrame.java @@ -518,6 +518,26 @@ public class GAlignFrame extends JInternalFrame sortGroupMenuItem_actionPerformed(e); } }); + JMenuItem sortEValueMenuItem = new JMenuItem( + MessageManager.getString("action.by_evalue")); + sortEValueMenuItem.addActionListener(new ActionListener() + { + @Override + public void actionPerformed(ActionEvent e) + { + sortEValueMenuItem_actionPerformed(e); + } + }); + JMenuItem sortBitScoreMenuItem = new JMenuItem( + MessageManager.getString("action.by_bit_score")); + sortBitScoreMenuItem.addActionListener(new ActionListener() + { + @Override + public void actionPerformed(ActionEvent e) + { + sortBitScoreMenuItem_actionPerformed(e); + } + }); JMenuItem removeRedundancyMenuItem = new JMenuItem( MessageManager.getString("action.remove_redundancy")); @@ -1899,6 +1919,8 @@ public class GAlignFrame extends JInternalFrame sort.add(sortLengthMenuItem); sort.add(sortGroupMenuItem); sort.add(sortPairwiseMenuItem); + sort.add(sortEValueMenuItem); + sort.add(sortBitScoreMenuItem); sort.add(sortByTreeMenu); calculateMenu.add(sort); calculateMenu.add(calculateTree); @@ -2544,6 +2566,14 @@ public class GAlignFrame extends JInternalFrame { } + protected void sortEValueMenuItem_actionPerformed(ActionEvent e) + { + } + + protected void sortBitScoreMenuItem_actionPerformed(ActionEvent e) + { + } + protected void removeRedundancyMenuItem_actionPerformed(ActionEvent e) { } -- 1.7.10.2