in progress
[jalview.git] / forester / java / src / org / forester / archaeopteryx / Util.java
index d347ab1..05823d0 100644 (file)
@@ -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;
@@ -57,7 +59,6 @@ import javax.swing.text.MaskFormatter;
 
 import org.forester.io.parsers.PhylogenyParser;
 import org.forester.io.parsers.tol.TolParser;
-import org.forester.io.parsers.util.PhylogenyParserException;
 import org.forester.phylogeny.Phylogeny;
 import org.forester.phylogeny.PhylogenyMethods;
 import org.forester.phylogeny.PhylogenyNode;
@@ -67,9 +68,10 @@ import org.forester.phylogeny.factories.ParserBasedPhylogenyFactory;
 import org.forester.phylogeny.factories.PhylogenyFactory;
 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
 import org.forester.phylogeny.iterators.PreorderTreeIterator;
+import org.forester.util.DescriptiveStatistics;
 import org.forester.util.ForesterUtil;
 
-final class Util {
+public final class Util {
 
     private final static String[] AVAILABLE_FONT_FAMILIES_SORTED = GraphicsEnvironment.getLocalGraphicsEnvironment()
                                                                          .getAvailableFontFamilyNames();
@@ -288,6 +290,39 @@ 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<PhylogenyNode> descs = PhylogenyMethods.getAllDescendants( n );
+                for( final PhylogenyNode desc : descs ) {
+                    desc.getBranchData().setBranchColor( c );
+                }
+            }
+        }
+    }
+
+    final static String[] getAllRanks( final Phylogeny tree ) {
+        final SortedSet<String> ranks = new TreeSet<String>();
+        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 );
@@ -308,37 +343,79 @@ final class Util {
         }
     }
 
-    final static String createDescriptionForTab( final Phylogeny phy, final String full_path ) {
+    final static String crateBasicInformation( final Phylogeny phy ) {
         final StringBuilder desc = new StringBuilder();
         if ( ( phy != null ) && !phy.isEmpty() ) {
             if ( !ForesterUtil.isEmpty( phy.getName() ) ) {
-                desc.append( "name: " );
+                desc.append( "Name: " );
                 desc.append( phy.getName() );
-                desc.append( "; " );
+                desc.append( "\n" );
             }
             if ( phy.getIdentifier() != null ) {
-                desc.append( "id: " );
+                desc.append( "Id: " );
                 desc.append( phy.getIdentifier() );
-                desc.append( "; " );
+                desc.append( "\n" );
             }
-            desc.append( "rooted: " );
+            desc.append( "Rooted: " );
             desc.append( phy.isRooted() );
-            desc.append( "; " );
-            desc.append( "rerootable: " );
+            desc.append( "\n" );
+            desc.append( "Rerootable: " );
             desc.append( phy.isRerootable() );
-            desc.append( "; " );
-            desc.append( "external nodes: " );
+            desc.append( "\n" );
+            desc.append( "Node sum: " );
+            desc.append( phy.getNodeCount() );
+            desc.append( "\n" );
+            desc.append( "External node sum: " );
             desc.append( phy.getNumberOfExternalNodes() );
-            desc.append( "; " );
-            desc.append( "branches: " );
+            desc.append( "\n" );
+            desc.append( "Internal node sum: " );
+            desc.append( phy.getNodeCount() - phy.getNumberOfExternalNodes() );
+            desc.append( "\n" );
+            desc.append( "Branche sum: " );
             desc.append( phy.getNumberOfBranches() );
-            desc.append( "; " );
-            desc.append( "maximum descendants: " );
-            desc.append( PhylogenyMethods.calculateMaximumNumberOfDescendantsPerNode( phy ) );
-            if ( !ForesterUtil.isEmpty( full_path ) && ( full_path.length() <= 50 ) ) {
-                desc.append( "; " );
-                desc.append( "path: " );
-                desc.append( full_path );
+            desc.append( "\n" );
+            desc.append( "Depth: " );
+            desc.append( PhylogenyMethods.calculateMaxDepth( phy ) );
+            desc.append( "\n" );
+            desc.append( "Maximum distance to root: " );
+            desc.append( ForesterUtil.round( PhylogenyMethods.calculateMaxDistanceToRoot( phy ), 6 ) );
+            desc.append( "\n" );
+            desc.append( "Descendants per node statistics: " );
+            final DescriptiveStatistics ds = PhylogenyMethods.calculatNumberOfDescendantsPerNodeStatistics( phy );
+            desc.append( "\n" );
+            desc.append( "    Median: " + ForesterUtil.round( ds.median(), 2 ) );
+            desc.append( "\n" );
+            desc.append( "    Mean: " + ForesterUtil.round( ds.arithmeticMean(), 2 ) );
+            desc.append( "\n" );
+            desc.append( "    SD: " + ForesterUtil.round( ds.sampleStandardDeviation(), 2 ) );
+            desc.append( "\n" );
+            desc.append( "    Minimum: " + ForesterUtil.roundToInt( ds.getMin() ) );
+            desc.append( "\n" );
+            desc.append( "    Maximum: " + ForesterUtil.roundToInt( ds.getMax() ) );
+            desc.append( "\n" );
+            final DescriptiveStatistics cs = PhylogenyMethods.calculatConfidenceStatistics( phy );
+            if ( cs.getN() > 1 ) {
+                desc.append( "Support statistics: " );
+                desc.append( "\n" );
+                desc.append( "    Branches with support: " + cs.getN() );
+                desc.append( "\n" );
+                desc.append( "    Median: " + ForesterUtil.round( cs.median(), 6 ) );
+                desc.append( "\n" );
+                desc.append( "    Mean: " + ForesterUtil.round( cs.arithmeticMean(), 6 ) );
+                desc.append( "\n" );
+                if ( cs.getN() > 2 ) {
+                    desc.append( "    SD: " + ForesterUtil.round( cs.sampleStandardDeviation(), 6 ) );
+                    desc.append( "\n" );
+                }
+                desc.append( "    Minimum: " + ForesterUtil.roundToInt( cs.getMin() ) );
+                desc.append( "\n" );
+                desc.append( "    Maximum: " + ForesterUtil.roundToInt( cs.getMax() ) );
+                desc.append( "\n" );
+            }
+            final Set<Taxonomy> taxs = PhylogenyMethods.obtainDistinctTaxonomies( phy.getRoot() );
+            if ( taxs != null ) {
+                desc.append( "Distinct external taxonomies: " );
+                desc.append( taxs.size() );
             }
         }
         return desc.toString();
@@ -571,19 +648,10 @@ final class Util {
         System.out.println( "[" + applet_name + "] > " + message );
     }
 
-    final static void printWarningMessage( final String name, final String message ) {
+    public final static void printWarningMessage( final String name, final String message ) {
         System.out.println( "[" + name + "] > " + message );
     }
 
-    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 static Phylogeny[] readPhylogeniesFromUrl( final URL url, final boolean phyloxml_validate_against_xsd )
             throws FileNotFoundException, IOException {
         final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();