From 15a1e4bec0836b935805c18e67e4cf9e48454617 Mon Sep 17 00:00:00 2001 From: "cmzmasek@gmail.com" Date: Sat, 17 Sep 2011 19:38:29 +0000 Subject: [PATCH] colorization via rank added --- .../org/forester/archaeopteryx/Configuration.java | 4 +- .../src/org/forester/archaeopteryx/Constants.java | 4 +- .../src/org/forester/archaeopteryx/MainFrame.java | 28 ++++++ .../archaeopteryx/MainFrameApplication.java | 3 + .../src/org/forester/archaeopteryx/TreePanel.java | 14 +++ .../java/src/org/forester/archaeopteryx/Util.java | 35 ++++++++ .../java/src/org/forester/util/ForesterUtil.java | 91 +++++++++++--------- 7 files changed, 134 insertions(+), 45 deletions(-) diff --git a/forester/java/src/org/forester/archaeopteryx/Configuration.java b/forester/java/src/org/forester/archaeopteryx/Configuration.java index 11183b0..32cec55 100644 --- a/forester/java/src/org/forester/archaeopteryx/Configuration.java +++ b/forester/java/src/org/forester/archaeopteryx/Configuration.java @@ -168,9 +168,9 @@ public final class Configuration { final static String clickto_options[][] = { { "Display Node Data", "display" }, { "Collapse/Uncollapse", "display" }, { "Root/Reroot", "display" }, { "Sub/Super Tree", "display" }, { "Swap Descendants", "display" }, { "Colorize Subtree", "display" }, - { "Open Sequence Web", "nodisplay" }, { "Open Taxonomy Web", "nodisplay" }, { "Cut Subtree", "display" }, + { "Open Sequence Web", "display" }, { "Open Taxonomy Web", "display" }, { "Cut Subtree", "display" }, { "Copy Subtree", "display" }, { "Paste Subtree", "display" }, { "Delete Subtree/Node", "display" }, - { "Add New Node", "display" }, { "Edit Node Data", "display" }, { "Blast", "display" } }; + { "Add New Node", "display" }, { "Edit Node Data", "display" }, { "Blast", "nodisplay" } }; // This option is selected in the dropdown int default_clickto = Configuration.display_node_data; // -------------- diff --git a/forester/java/src/org/forester/archaeopteryx/Constants.java b/forester/java/src/org/forester/archaeopteryx/Constants.java index 088cf93..ea932fe 100644 --- a/forester/java/src/org/forester/archaeopteryx/Constants.java +++ b/forester/java/src/org/forester/archaeopteryx/Constants.java @@ -41,8 +41,8 @@ public final class Constants { public final static boolean __SNAPSHOT_RELEASE = true; // TODO remove me public final static boolean __SYNTH_LF = false; // TODO remove me public final static String PRG_NAME = "Archaeopteryx"; - final static String VERSION = "0.962 beta B48"; - final static String PRG_DATE = "2011.05.27"; + final static String VERSION = "0.962 beta 2N"; + final static String PRG_DATE = "2011.09.17"; final static String DEFAULT_CONFIGURATION_FILE_NAME = "_aptx_configuration_file"; final static String[] DEFAULT_FONT_CHOICES = { "Verdana", "Tahoma", "Arial", "Helvetica", "Dialog", "Lucida Sans", "SansSerif", "Sans-serif", "Sans" }; diff --git a/forester/java/src/org/forester/archaeopteryx/MainFrame.java b/forester/java/src/org/forester/archaeopteryx/MainFrame.java index 5e78865..0e8f328 100644 --- a/forester/java/src/org/forester/archaeopteryx/MainFrame.java +++ b/forester/java/src/org/forester/archaeopteryx/MainFrame.java @@ -116,6 +116,7 @@ public abstract class MainFrame extends JFrame implements ActionListener { JMenuItem _midpoint_root_item; JMenuItem _taxcolor_item; JMenuItem _confcolor_item; + JMenuItem _color_rank_jmi; JMenuItem _infer_common_sn_names_item; JMenuItem _collapse_species_specific_subtrees; JMenuItem _collapse_below_threshold; //TODO implememt me @@ -231,6 +232,9 @@ public abstract class MainFrame extends JFrame implements ActionListener { else if ( o == _confcolor_item ) { confColor(); } + else if ( o == _color_rank_jmi ) { + colorRank(); + } else if ( o == _infer_common_sn_names_item ) { if ( isSubtreeDisplayed() ) { return; @@ -639,6 +643,30 @@ public abstract class MainFrame extends JFrame implements ActionListener { } } + void colorRank() { + if ( _mainpanel.getCurrentTreePanel() != null ) { + final String[] ranks = Util.getAllRanks( _mainpanel.getCurrentTreePanel().getPhylogeny() ); + if ( ranks.length < 1 ) { + JOptionPane.showMessageDialog( this, + "No rank information was found", + "No Rank Inoformation", + JOptionPane.WARNING_MESSAGE ); + return; + } + final String rank = ( String ) JOptionPane + .showInputDialog( this, + "What rank should the colorization be based on", + "Rank Selection", + JOptionPane.PLAIN_MESSAGE, + null, + ranks, + null ); + if ( !ForesterUtil.isEmpty( rank ) ) { + _mainpanel.getCurrentTreePanel().colorRank( rank ); + } + } + } + void customizeCheckBoxMenuItem( final JCheckBoxMenuItem item, final boolean is_selected ) { if ( item != null ) { item.setFont( MainFrame.menu_font ); diff --git a/forester/java/src/org/forester/archaeopteryx/MainFrameApplication.java b/forester/java/src/org/forester/archaeopteryx/MainFrameApplication.java index 5c4d546..42b6cb6 100644 --- a/forester/java/src/org/forester/archaeopteryx/MainFrameApplication.java +++ b/forester/java/src/org/forester/archaeopteryx/MainFrameApplication.java @@ -836,6 +836,9 @@ public final class MainFrameApplication extends MainFrame { _tools_menu = createMenu( "Tools", getConfiguration() ); _tools_menu.add( _confcolor_item = new JMenuItem( "Colorize Branches Depending on Confidence" ) ); customizeJMenuItem( _confcolor_item ); + _tools_menu.add( _color_rank_jmi = new JMenuItem( "Colorize Subtrees via Taxonomic Rank" ) ); + customizeJMenuItem( _color_rank_jmi ); + _color_rank_jmi.setToolTipText( "for example, at \"Class\" level, colorize mammal specific subtree red" ); _tools_menu.add( _taxcolor_item = new JMenuItem( "Taxonomy Colorize Branches" ) ); customizeJMenuItem( _taxcolor_item ); _tools_menu.add( _remove_branch_color_item = new JMenuItem( "Delete Branch Colors" ) ); diff --git a/forester/java/src/org/forester/archaeopteryx/TreePanel.java b/forester/java/src/org/forester/archaeopteryx/TreePanel.java index 9680205..136bfd5 100644 --- a/forester/java/src/org/forester/archaeopteryx/TreePanel.java +++ b/forester/java/src/org/forester/archaeopteryx/TreePanel.java @@ -773,6 +773,20 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee repaint(); } + final void colorRank( final String rank ) { + if ( ( _phylogeny == null ) || ( _phylogeny.getNumberOfExternalNodes() < 2 ) ) { + return; + } + setWaitCursor(); + Util.colorPhylogenyAccordingToRanks( _phylogeny, rank, this ); + _control_panel.setColorBranches( true ); + if ( _control_panel.getColorBranchesCb() != null ) { + _control_panel.getColorBranchesCb().setSelected( true ); + } + setArrowCursor(); + repaint(); + } + final private void copySubtree( final PhylogenyNode node ) { if ( getPhylogenyGraphicsType() == PHYLOGENY_GRAPHICS_TYPE.UNROOTED ) { errorMessageNoCutCopyPasteInUnrootedDisplay(); diff --git a/forester/java/src/org/forester/archaeopteryx/Util.java b/forester/java/src/org/forester/archaeopteryx/Util.java index 198dc1a..05823d0 100644 --- a/forester/java/src/org/forester/archaeopteryx/Util.java +++ b/forester/java/src/org/forester/archaeopteryx/Util.java @@ -45,6 +45,8 @@ import java.util.Iterator; import java.util.List; import java.util.Locale; import java.util.Set; +import java.util.SortedSet; +import java.util.TreeSet; import javax.imageio.IIOImage; import javax.imageio.ImageIO; @@ -288,6 +290,39 @@ public final class Util { } } + final static void colorPhylogenyAccordingToRanks( final Phylogeny tree, + final String rank, + final TreePanel tree_panel ) { + for( final PhylogenyNodeIterator it = tree.iteratorPostorder(); it.hasNext(); ) { + final PhylogenyNode n = it.next(); + if ( n.getNodeData().isHasTaxonomy() + && ( !ForesterUtil.isEmpty( n.getNodeData().getTaxonomy().getRank() ) && n.getNodeData() + .getTaxonomy().getRank().equalsIgnoreCase( rank ) ) + && ( !ForesterUtil.isEmpty( n.getNodeData().getTaxonomy().getScientificName() ) + || !ForesterUtil.isEmpty( n.getNodeData().getTaxonomy().getCommonName() ) || !ForesterUtil + .isEmpty( n.getNodeData().getTaxonomy().getTaxonomyCode() ) ) ) { + final BranchColor c = new BranchColor( tree_panel.calculateTaxonomyBasedColor( n.getNodeData() + .getTaxonomy() ) ); + n.getBranchData().setBranchColor( c ); + final List descs = PhylogenyMethods.getAllDescendants( n ); + for( final PhylogenyNode desc : descs ) { + desc.getBranchData().setBranchColor( c ); + } + } + } + } + + final static String[] getAllRanks( final Phylogeny tree ) { + final SortedSet ranks = new TreeSet(); + for( final PhylogenyNodeIterator it = tree.iteratorPreorder(); it.hasNext(); ) { + final PhylogenyNode n = it.next(); + if ( n.getNodeData().isHasTaxonomy() && !ForesterUtil.isEmpty( n.getNodeData().getTaxonomy().getRank() ) ) { + ranks.add( n.getNodeData().getTaxonomy().getRank() ); + } + } + return ForesterUtil.stringSetToArray( ranks ); + } + final static void colorPhylogenyAccordingToExternalTaxonomy( final Phylogeny tree, final TreePanel tree_panel ) { for( final PhylogenyNodeIterator it = tree.iteratorPreorder(); it.hasNext(); ) { it.next().getBranchData().setBranchColor( null ); diff --git a/forester/java/src/org/forester/util/ForesterUtil.java b/forester/java/src/org/forester/util/ForesterUtil.java index 111dc40..ebbb8fe 100644 --- a/forester/java/src/org/forester/util/ForesterUtil.java +++ b/forester/java/src/org/forester/util/ForesterUtil.java @@ -108,45 +108,12 @@ public final class ForesterUtil { private ForesterUtil() { } - public final static Phylogeny[] readPhylogenies( final PhylogenyParser parser, final File file ) throws IOException { - final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance(); - final Phylogeny[] trees = factory.create( file, parser ); - if ( ( trees == null ) || ( trees.length == 0 ) ) { - throw new PhylogenyParserException( "Unable to parse phylogeny from file: " + file ); - } - return trees; - } - final public static void appendSeparatorIfNotEmpty( final StringBuffer sb, final char separator ) { if ( sb.length() > 0 ) { sb.append( separator ); } } - final public static boolean isEmpty( final List l ) { - if ( ( l == null ) || l.isEmpty() ) { - return true; - } - for( final Object o : l ) { - if ( o != null ) { - return false; - } - } - return true; - } - - final public static boolean isEmpty( final Set s ) { - if ( ( s == null ) || s.isEmpty() ) { - return true; - } - for( final Object o : s ) { - if ( o != null ) { - return false; - } - } - return true; - } - /** * This calculates a color. If value is equal to min the returned color is * minColor, if value is equal to max the returned color is maxColor, @@ -688,6 +655,34 @@ public final class ForesterUtil { return true; } + final public static boolean isContainsParanthesesableNhCharacter( final String nh ) { + return PARANTHESESABLE_NH_CHARS_PATTERN.matcher( nh ).find(); + } + + final public static boolean isEmpty( final List l ) { + if ( ( l == null ) || l.isEmpty() ) { + return true; + } + for( final Object o : l ) { + if ( o != null ) { + return false; + } + } + return true; + } + + final public static boolean isEmpty( final Set s ) { + if ( ( s == null ) || s.isEmpty() ) { + return true; + } + for( final Object o : s ) { + if ( o != null ) { + return false; + } + } + return true; + } + final public static boolean isEmpty( final String s ) { return ( ( s == null ) || ( s.length() < 1 ) ); } @@ -1010,6 +1005,15 @@ public final class ForesterUtil { System.out.println( "[" + prg_name + "] > " + message ); } + public final static Phylogeny[] readPhylogenies( final PhylogenyParser parser, final File file ) throws IOException { + final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance(); + final Phylogeny[] trees = factory.create( file, parser ); + if ( ( trees == null ) || ( trees.length == 0 ) ) { + throw new PhylogenyParserException( "Unable to parse phylogeny from file: " + file ); + } + return trees; + } + final public static String removeSuffix( final String file_name ) { final int i = file_name.lastIndexOf( '.' ); if ( i > 1 ) { @@ -1035,10 +1039,6 @@ public final class ForesterUtil { return s; } - final public static boolean isContainsParanthesesableNhCharacter( final String nh ) { - return PARANTHESESABLE_NH_CHARS_PATTERN.matcher( nh ).find(); - } - final public static String replaceIllegalNhCharacters( final String nh ) { if ( nh == null ) { return ""; @@ -1066,14 +1066,14 @@ public final class ForesterUtil { return ( int ) ( d + 0.5 ); } - final public static short roundToShort( final double d ) { - return ( short ) ( d + 0.5 ); - } - final public static int roundToInt( final float f ) { return ( int ) ( f + 0.5f ); } + final public static short roundToShort( final double d ) { + return ( short ) ( d + 0.5 ); + } + final public static String sanitizeString( final String s ) { if ( s == null ) { return ""; @@ -1099,6 +1099,15 @@ public final class ForesterUtil { return sb.toString(); } + final public static String[] stringSetToArray( final Set strings ) { + final String[] str_array = new String[ strings.size() ]; + int i = 0; + for( final String e : strings ) { + str_array[ i++ ] = e; + } + return str_array; + } + final static public void transferInternalNamesToBootstrapSupport( final Phylogeny phy ) { final PhylogenyNodeIterator it = phy.iteratorPostorder(); while ( it.hasNext() ) { -- 1.7.10.2