in progress (special coloring is still true)
[jalview.git] / forester / java / src / org / forester / rio / RIO.java
index 0cbe6dd..1f4f3e6 100644 (file)
@@ -78,8 +78,10 @@ public final class RIO {
     private final boolean                    _produce_log;
     private final boolean                    _verbose;
     private final REROOTING                  _rerooting;
+    private final Phylogeny                  _species_tree;
+    private Phylogeny                        _min_dub_gene_tree;
 
-    private RIO( final Phylogeny[] gene_trees,
+    private RIO( final IteratingPhylogenyParser p,
                  final Phylogeny species_tree,
                  final ALGORITHM algorithm,
                  final REROOTING rerooting,
@@ -87,15 +89,17 @@ public final class RIO {
                  int first,
                  int last,
                  final boolean produce_log,
-                 final boolean verbose ) throws IOException, SDIException, RIOException {
+                 final boolean verbose,
+                 final boolean transfer_taxonomy ) throws IOException, SDIException, RIOException {
         if ( ( last == DEFAULT_RANGE ) && ( first >= 0 ) ) {
-            last = gene_trees.length - 1;
+            last = END_OF_GT;
         }
         else if ( ( first == DEFAULT_RANGE ) && ( last >= 0 ) ) {
             first = 0;
         }
         removeSingleDescendentsNodes( species_tree, verbose );
-        checkPreconditions( gene_trees, species_tree, rerooting, outgroup, first, last );
+        p.reset();
+        checkPreconditions( p, species_tree, rerooting, outgroup, first, last );
         _produce_log = produce_log;
         _verbose = verbose;
         _rerooting = rerooting;
@@ -106,10 +110,12 @@ public final class RIO {
         _analyzed_gene_trees = null;
         _removed_gene_tree_nodes = null;
         _duplications_stats = new BasicDescriptiveStatistics();
-        inferOrthologs( gene_trees, species_tree, algorithm, outgroup, first, last );
+        p.reset();
+        inferOrthologs( p, species_tree, algorithm, outgroup, first, last, transfer_taxonomy );
+        _species_tree = species_tree;
     }
 
-    private RIO( final IteratingPhylogenyParser p,
+    private RIO( final Phylogeny[] gene_trees,
                  final Phylogeny species_tree,
                  final ALGORITHM algorithm,
                  final REROOTING rerooting,
@@ -117,16 +123,16 @@ public final class RIO {
                  int first,
                  int last,
                  final boolean produce_log,
-                 final boolean verbose ) throws IOException, SDIException, RIOException {
+                 final boolean verbose,
+                 final boolean transfer_taxonomy ) throws IOException, SDIException, RIOException {
         if ( ( last == DEFAULT_RANGE ) && ( first >= 0 ) ) {
-            last = END_OF_GT;
+            last = gene_trees.length - 1;
         }
         else if ( ( first == DEFAULT_RANGE ) && ( last >= 0 ) ) {
             first = 0;
         }
         removeSingleDescendentsNodes( species_tree, verbose );
-        p.reset();
-        checkPreconditions( p, species_tree, rerooting, outgroup, first, last );
+        checkPreconditions( gene_trees, species_tree, rerooting, outgroup, first, last );
         _produce_log = produce_log;
         _verbose = verbose;
         _rerooting = rerooting;
@@ -137,8 +143,8 @@ public final class RIO {
         _analyzed_gene_trees = null;
         _removed_gene_tree_nodes = null;
         _duplications_stats = new BasicDescriptiveStatistics();
-        p.reset();
-        inferOrthologs( p, species_tree, algorithm, outgroup, first, last );
+        inferOrthologs( gene_trees, species_tree, algorithm, outgroup, first, last, transfer_taxonomy );
+        _species_tree = species_tree;
     }
 
     public final Phylogeny[] getAnalyzedGeneTrees() {
@@ -159,6 +165,10 @@ public final class RIO {
         return _ext_nodes;
     }
 
+    public final TaxonomyComparisonBase getGSDIRtaxCompBase() {
+        return _gsdir_tax_comp_base;
+    }
+
     /**
      * Returns the numbers of number of int nodes in gene trees analyzed (after
      * stripping).
@@ -169,80 +179,24 @@ public final class RIO {
         return _int_nodes;
     }
 
-    public final TaxonomyComparisonBase getGSDIRtaxCompBase() {
-        return _gsdir_tax_comp_base;
-    }
-
     public final StringBuilder getLog() {
         return _log;
     }
 
+    final public Phylogeny getMinDuplicationsGeneTree() {
+        return _min_dub_gene_tree;
+    }
+
+    public final IntMatrix getOrthologTable() {
+        return _m;
+    }
+
     public final List<PhylogenyNode> getRemovedGeneTreeNodes() {
         return _removed_gene_tree_nodes;
     }
 
-    private final void inferOrthologs( final Phylogeny[] gene_trees,
-                                       final Phylogeny species_tree,
-                                       final ALGORITHM algorithm,
-                                       final String outgroup,
-                                       final int first,
-                                       final int last ) throws SDIException, RIOException, FileNotFoundException,
-            IOException {
-        if ( algorithm == ALGORITHM.SDIR ) {
-            // Removes from species_tree all species not found in gene_tree.
-            PhylogenyMethods.taxonomyBasedDeletionOfExternalNodes( gene_trees[ 0 ], species_tree );
-            if ( species_tree.isEmpty() ) {
-                throw new RIOException( "failed to establish species based mapping between gene and species trees" );
-            }
-        }
-        final Phylogeny[] my_gene_trees;
-        if ( ( first >= 0 ) && ( last >= first ) && ( last < gene_trees.length ) ) {
-            my_gene_trees = new Phylogeny[ ( 1 + last ) - first ];
-            int c = 0;
-            for( int i = first; i <= last; ++i ) {
-                my_gene_trees[ c++ ] = gene_trees[ i ];
-            }
-        }
-        else {
-            my_gene_trees = gene_trees;
-        }
-        if ( log() ) {
-            preLog( gene_trees.length, species_tree, algorithm, outgroup );
-        }
-        if ( _verbose && ( my_gene_trees.length >= 4 ) ) {
-            System.out.println();
-        }
-        _analyzed_gene_trees = new Phylogeny[ my_gene_trees.length ];
-        int gene_tree_ext_nodes = 0;
-        for( int i = 0; i < my_gene_trees.length; ++i ) {
-            final Phylogeny gt = my_gene_trees[ i ];
-            if ( _verbose && ( my_gene_trees.length > 4 ) ) {
-                ForesterUtil.updateProgress( ( ( double ) i ) / my_gene_trees.length );
-            }
-            if ( i == 0 ) {
-                gene_tree_ext_nodes = gt.getNumberOfExternalNodes();
-            }
-            else if ( gene_tree_ext_nodes != gt.getNumberOfExternalNodes() ) {
-                throw new RIOException( "gene tree #" + i + " has a different number of external nodes ("
-                        + gt.getNumberOfExternalNodes() + ") than the preceding gene tree(s) (" + gene_tree_ext_nodes
-                        + ")" );
-            }
-            if ( algorithm == ALGORITHM.SDIR ) {
-                // Removes from gene_tree all species not found in species_tree.
-                PhylogenyMethods.taxonomyBasedDeletionOfExternalNodes( species_tree, gt );
-                if ( gt.isEmpty() ) {
-                    throw new RIOException( "failed to establish species based mapping between gene and species trees" );
-                }
-            }
-            _analyzed_gene_trees[ i ] = performOrthologInference( gt, species_tree, algorithm, outgroup, i );
-        }
-        if ( log() ) {
-            postLog( species_tree, first, last );
-        }
-        if ( _verbose && ( my_gene_trees.length > 4 ) ) {
-            System.out.println();
-            System.out.println();
-        }
+    public final Phylogeny getSpeciesTree() {
+        return _species_tree;
     }
 
     private final void inferOrthologs( final IteratingPhylogenyParser parser,
@@ -250,8 +204,9 @@ public final class RIO {
                                        final ALGORITHM algorithm,
                                        final String outgroup,
                                        int first,
-                                       final int last ) throws SDIException, RIOException, FileNotFoundException,
-            IOException {
+                                       final int last,
+                                       final boolean transfer_taxonomy ) throws SDIException, RIOException,
+            FileNotFoundException, IOException {
         if ( !parser.hasNext() ) {
             throw new RIOException( "no gene trees to analyze" );
         }
@@ -269,6 +224,12 @@ public final class RIO {
         while ( parser.hasNext() ) {
             final Phylogeny gt = parser.next();
             if ( no_range || ( ( i >= first ) && ( i <= last ) ) ) {
+                if ( gt.isEmpty() ) {
+                    throw new RIOException( "gene tree #" + i + " is empty" );
+                }
+                if ( gt.getNumberOfExternalNodes() == 1 ) {
+                    throw new RIOException( "gene tree #" + i + " has only one external node" );
+                }
                 if ( _verbose ) {
                     ForesterUtil.updateProgress( i, pf );
                 }
@@ -294,7 +255,12 @@ public final class RIO {
                         throw new RIOException( "failed to establish species based mapping between gene and species trees" );
                     }
                 }
-                final Phylogeny analyzed_gt = performOrthologInference( gt, species_tree, algorithm, outgroup, counter );
+                final Phylogeny analyzed_gt = performOrthologInference( gt,
+                                                                        species_tree,
+                                                                        algorithm,
+                                                                        outgroup,
+                                                                        counter,
+                                                                        transfer_taxonomy );
                 RIO.calculateOrthologTable( analyzed_gt, true, counter );
                 ++counter;
             }
@@ -307,7 +273,7 @@ public final class RIO {
             first = 0;
         }
         if ( log() ) {
-            postLog( species_tree, first, first + counter - 1 );
+            postLog( species_tree, first, ( first + counter ) - 1 );
         }
         if ( _verbose ) {
             System.out.println();
@@ -315,6 +281,82 @@ public final class RIO {
         }
     }
 
+    private final void inferOrthologs( final Phylogeny[] gene_trees,
+                                       final Phylogeny species_tree,
+                                       final ALGORITHM algorithm,
+                                       final String outgroup,
+                                       final int first,
+                                       final int last,
+                                       final boolean transfer_taxonomy ) throws SDIException, RIOException,
+            FileNotFoundException, IOException {
+        if ( algorithm == ALGORITHM.SDIR ) {
+            // Removes from species_tree all species not found in gene_tree.
+            PhylogenyMethods.taxonomyBasedDeletionOfExternalNodes( gene_trees[ 0 ], species_tree );
+            if ( species_tree.isEmpty() ) {
+                throw new RIOException( "failed to establish species based mapping between gene and species trees" );
+            }
+        }
+        final Phylogeny[] my_gene_trees;
+        if ( ( first >= 0 ) && ( last >= first ) && ( last < gene_trees.length ) ) {
+            my_gene_trees = new Phylogeny[ ( 1 + last ) - first ];
+            int c = 0;
+            for( int i = first; i <= last; ++i ) {
+                my_gene_trees[ c++ ] = gene_trees[ i ];
+            }
+        }
+        else {
+            my_gene_trees = gene_trees;
+        }
+        if ( log() ) {
+            preLog( gene_trees.length, species_tree, algorithm, outgroup );
+        }
+        if ( _verbose && ( my_gene_trees.length >= 4 ) ) {
+            System.out.println();
+        }
+        _analyzed_gene_trees = new Phylogeny[ my_gene_trees.length ];
+        int gene_tree_ext_nodes = 0;
+        for( int i = 0; i < my_gene_trees.length; ++i ) {
+            final Phylogeny gt = my_gene_trees[ i ];
+            if ( gt.isEmpty() ) {
+                throw new RIOException( "gene tree #" + i + " is empty" );
+            }
+            if ( gt.getNumberOfExternalNodes() == 1 ) {
+                throw new RIOException( "gene tree #" + i + " has only one external node" );
+            }
+            if ( _verbose && ( my_gene_trees.length > 4 ) ) {
+                ForesterUtil.updateProgress( ( ( double ) i ) / my_gene_trees.length );
+            }
+            if ( i == 0 ) {
+                gene_tree_ext_nodes = gt.getNumberOfExternalNodes();
+            }
+            else if ( gene_tree_ext_nodes != gt.getNumberOfExternalNodes() ) {
+                throw new RIOException( "gene tree #" + i + " has a different number of external nodes ("
+                        + gt.getNumberOfExternalNodes() + ") than the preceding gene tree(s) (" + gene_tree_ext_nodes
+                        + ")" );
+            }
+            if ( algorithm == ALGORITHM.SDIR ) {
+                // Removes from gene_tree all species not found in species_tree.
+                PhylogenyMethods.taxonomyBasedDeletionOfExternalNodes( species_tree, gt );
+                if ( gt.isEmpty() ) {
+                    throw new RIOException( "failed to establish species based mapping between gene and species trees" );
+                }
+            }
+            _analyzed_gene_trees[ i ] = performOrthologInference( gt,
+                                                                  species_tree,
+                                                                  algorithm,
+                                                                  outgroup,
+                                                                  i,
+                                                                  transfer_taxonomy );
+        }
+        if ( log() ) {
+            postLog( species_tree, first, last );
+        }
+        if ( _verbose && ( my_gene_trees.length > 4 ) ) {
+            System.out.println();
+            System.out.println();
+        }
+    }
+
     private final boolean log() {
         return _produce_log;
     }
@@ -354,7 +396,9 @@ public final class RIO {
                                                       final Phylogeny species_tree,
                                                       final ALGORITHM algorithm,
                                                       final String outgroup,
-                                                      final int i ) throws SDIException, RIOException {
+                                                      final int i,
+                                                      final boolean transfer_taxonomy ) throws SDIException,
+            RIOException {
         final Phylogeny assigned_tree;
         switch ( algorithm ) {
             case SDIR: {
@@ -362,7 +406,7 @@ public final class RIO {
                 break;
             }
             case GSDIR: {
-                assigned_tree = performOrthologInferenceByGSDI( gene_tree, species_tree, outgroup, i );
+                assigned_tree = performOrthologInferenceByGSDI( gene_tree, species_tree, outgroup, i, transfer_taxonomy );
                 break;
             }
             default: {
@@ -384,10 +428,13 @@ public final class RIO {
     private final Phylogeny performOrthologInferenceByGSDI( final Phylogeny gene_tree,
                                                             final Phylogeny species_tree,
                                                             final String outgroup,
-                                                            final int i ) throws SDIException, RIOException {
+                                                            final int i,
+                                                            final boolean transfer_taxonomy ) throws SDIException,
+            RIOException {
         final Phylogeny assigned_tree;
+        final int dups;
         if ( _rerooting == REROOTING.BY_ALGORITHM ) {
-            final GSDIR gsdir = new GSDIR( gene_tree, species_tree, true, i == 0 );
+            final GSDIR gsdir = new GSDIR( gene_tree, species_tree, true, i == 0, transfer_taxonomy );
             assigned_tree = gsdir.getMinDuplicationsSumGeneTree();
             if ( i == 0 ) {
                 _removed_gene_tree_nodes = gsdir.getStrippedExternalGeneTreeNodes();
@@ -401,7 +448,7 @@ public final class RIO {
             if ( i == 0 ) {
                 _gsdir_tax_comp_base = gsdir.getTaxCompBase();
             }
-            _duplications_stats.addValue( gsdir.getMinDuplicationsSum() );
+            dups = gsdir.getMinDuplicationsSum();
         }
         else {
             if ( _rerooting == REROOTING.MIDPOINT ) {
@@ -411,7 +458,7 @@ public final class RIO {
                 final PhylogenyNode n = gene_tree.getNode( outgroup );
                 gene_tree.reRoot( n );
             }
-            final GSDI gsdi = new GSDI( gene_tree, species_tree, true, true, true );
+            final GSDI gsdi = new GSDI( gene_tree, species_tree, true, true, true, transfer_taxonomy );
             _removed_gene_tree_nodes = gsdi.getStrippedExternalGeneTreeNodes();
             for( final PhylogenyNode r : _removed_gene_tree_nodes ) {
                 if ( !r.getNodeData().isHasTaxonomy() ) {
@@ -423,8 +470,12 @@ public final class RIO {
             if ( i == 0 ) {
                 _gsdir_tax_comp_base = gsdi.getTaxCompBase();
             }
-            _duplications_stats.addValue( gsdi.getDuplicationsSum() );
+            dups = gsdi.getDuplicationsSum();
+        }
+        if ( ( i == 0 ) || ( dups < _duplications_stats.getMin() ) ) {
+            _min_dub_gene_tree = assigned_tree;
         }
+        _duplications_stats.addValue( dups );
         return assigned_tree;
     }
 
@@ -498,73 +549,6 @@ public final class RIO {
         log( "Re-rooting                                      : " + rs );
     }
 
-    public final IntMatrix getOrthologTable() {
-        return _m;
-    }
-
-    private final static void calculateOrthologTable( final Phylogeny g, final boolean sort, final int counter )
-            throws RIOException {
-        final List<String> labels = new ArrayList<String>();
-        final Set<String> labels_set = new HashSet<String>();
-        if ( counter == 0 ) {
-            for( final PhylogenyNode n : g.getExternalNodes() ) {
-                final String label = obtainLabel( labels_set, n );
-                labels_set.add( label );
-                labels.add( label );
-            }
-            if ( sort ) {
-                Collections.sort( labels );
-            }
-            _m = new IntMatrix( labels );
-        }
-        updateCounts( _m, counter, g );
-    }
-
-    private final static String obtainLabel( final Set<String> labels_set, final PhylogenyNode n ) throws RIOException {
-        String label;
-        if ( n.getNodeData().isHasSequence() && !ForesterUtil.isEmpty( n.getNodeData().getSequence().getName() ) ) {
-            label = n.getNodeData().getSequence().getName();
-        }
-        else if ( n.getNodeData().isHasSequence() && !ForesterUtil.isEmpty( n.getNodeData().getSequence().getSymbol() ) ) {
-            label = n.getNodeData().getSequence().getSymbol();
-        }
-        else if ( !ForesterUtil.isEmpty( n.getName() ) ) {
-            label = n.getName();
-        }
-        else {
-            throw new RIOException( "node " + n + " has no appropriate label" );
-        }
-        if ( labels_set.contains( label ) ) {
-            throw new RIOException( "label " + label + " is not unique" );
-        }
-        return label;
-    }
-
-    private final static void updateCounts( final IntMatrix m, final int counter, final Phylogeny g )
-            throws RIOException {
-        PhylogenyMethods.preOrderReId( g );
-        final HashMap<String, PhylogenyNode> map = PhylogenyMethods.createNameToExtNodeMap( g );
-        for( int x = 0; x < m.size(); ++x ) {
-            final String mx = m.getLabel( x );
-            final PhylogenyNode nx = map.get( mx );
-            if ( nx == null ) {
-                throw new RIOException( "node \"" + mx + "\" not present in gene tree #" + counter );
-            }
-            String my;
-            PhylogenyNode ny;
-            for( int y = 0; y < m.size(); ++y ) {
-                my = m.getLabel( y );
-                ny = map.get( my );
-                if ( ny == null ) {
-                    throw new RIOException( "node \"" + my + "\" not present in gene tree #" + counter );
-                }
-                if ( !PhylogenyMethods.calculateLCAonTreeWithIdsInPreOrder( nx, ny ).isDuplication() ) {
-                    m.inreaseByOne( x, y );
-                }
-            }
-        }
-    }
-
     public final static IntMatrix calculateOrthologTable( final Phylogeny[] analyzed_gene_trees, final boolean sort )
             throws RIOException {
         final List<String> labels = new ArrayList<String>();
@@ -594,7 +578,9 @@ public final class RIO {
                                              final int first,
                                              final int last,
                                              final boolean produce_log,
-                                             final boolean verbose ) throws IOException, SDIException, RIOException {
+                                             final boolean verbose,
+                                             final boolean transfer_taxonomy ) throws IOException, SDIException,
+            RIOException {
         final Phylogeny[] gene_trees = parseGeneTrees( gene_trees_file );
         if ( gene_trees.length < 1 ) {
             throw new RIOException( "\"" + gene_trees_file + "\" is devoid of appropriate gene trees" );
@@ -604,7 +590,60 @@ public final class RIO {
                                                                  false,
                                                                  true,
                                                                  TAXONOMY_EXTRACTION.NO );
-        return new RIO( gene_trees, species_tree, algorithm, rerooting, outgroup, first, last, produce_log, verbose );
+        return new RIO( gene_trees,
+                        species_tree,
+                        algorithm,
+                        rerooting,
+                        outgroup,
+                        first,
+                        last,
+                        produce_log,
+                        verbose,
+                        transfer_taxonomy );
+    }
+
+    public final static RIO executeAnalysis( final File gene_trees_file,
+                                             final Phylogeny species_tree,
+                                             final ALGORITHM algorithm,
+                                             final REROOTING rerooting,
+                                             final String outgroup,
+                                             final boolean produce_log,
+                                             final boolean verbose,
+                                             final boolean transfer_taxonomy ) throws IOException, SDIException,
+            RIOException {
+        return new RIO( parseGeneTrees( gene_trees_file ),
+                        species_tree,
+                        algorithm,
+                        rerooting,
+                        outgroup,
+                        DEFAULT_RANGE,
+                        DEFAULT_RANGE,
+                        produce_log,
+                        verbose,
+                        transfer_taxonomy );
+    }
+
+    public final static RIO executeAnalysis( final File gene_trees_file,
+                                             final Phylogeny species_tree,
+                                             final ALGORITHM algorithm,
+                                             final REROOTING rerooting,
+                                             final String outgroup,
+                                             final int first,
+                                             final int last,
+                                             final boolean produce_log,
+                                             final boolean verbose,
+                                             final boolean transfer_taxonomy ) throws IOException, SDIException,
+            RIOException {
+        return new RIO( parseGeneTrees( gene_trees_file ),
+                        species_tree,
+                        algorithm,
+                        rerooting,
+                        outgroup,
+                        first,
+                        last,
+                        produce_log,
+                        verbose,
+                        transfer_taxonomy );
     }
 
     public final static RIO executeAnalysis( final IteratingPhylogenyParser p,
@@ -615,7 +654,9 @@ public final class RIO {
                                              final int first,
                                              final int last,
                                              final boolean produce_log,
-                                             final boolean verbose ) throws IOException, SDIException, RIOException {
+                                             final boolean verbose,
+                                             final boolean transfer_taxonomy ) throws IOException, SDIException,
+            RIOException {
         final Phylogeny g0 = p.next();
         if ( ( g0 == null ) || g0.isEmpty() || ( g0.getNumberOfExternalNodes() < 2 ) ) {
             throw new RIOException( "input file does not seem to contain any gene trees" );
@@ -626,25 +667,16 @@ public final class RIO {
                                                                  true,
                                                                  TAXONOMY_EXTRACTION.NO );
         p.reset();
-        return new RIO( p, species_tree, algorithm, rerooting, outgroup, first, last, produce_log, verbose );
-    }
-
-    public final static RIO executeAnalysis( final File gene_trees_file,
-                                             final Phylogeny species_tree,
-                                             final ALGORITHM algorithm,
-                                             final REROOTING rerooting,
-                                             final String outgroup,
-                                             final boolean produce_log,
-                                             final boolean verbose ) throws IOException, SDIException, RIOException {
-        return new RIO( parseGeneTrees( gene_trees_file ),
+        return new RIO( p,
                         species_tree,
                         algorithm,
                         rerooting,
                         outgroup,
-                        DEFAULT_RANGE,
-                        DEFAULT_RANGE,
+                        first,
+                        last,
                         produce_log,
-                        verbose );
+                        verbose,
+                        transfer_taxonomy );
     }
 
     public final static RIO executeAnalysis( final IteratingPhylogenyParser p,
@@ -653,7 +685,9 @@ public final class RIO {
                                              final REROOTING rerooting,
                                              final String outgroup,
                                              final boolean produce_log,
-                                             final boolean verbose ) throws IOException, SDIException, RIOException {
+                                             final boolean verbose,
+                                             final boolean transfer_taxonomy ) throws IOException, SDIException,
+            RIOException {
         return new RIO( p,
                         species_tree,
                         algorithm,
@@ -662,10 +696,11 @@ public final class RIO {
                         DEFAULT_RANGE,
                         DEFAULT_RANGE,
                         produce_log,
-                        verbose );
+                        verbose,
+                        transfer_taxonomy );
     }
 
-    public final static RIO executeAnalysis( final File gene_trees_file,
+    public final static RIO executeAnalysis( final IteratingPhylogenyParser p,
                                              final Phylogeny species_tree,
                                              final ALGORITHM algorithm,
                                              final REROOTING rerooting,
@@ -673,8 +708,10 @@ public final class RIO {
                                              final int first,
                                              final int last,
                                              final boolean produce_log,
-                                             final boolean verbose ) throws IOException, SDIException, RIOException {
-        return new RIO( parseGeneTrees( gene_trees_file ),
+                                             final boolean verbose,
+                                             final boolean transfer_taxonomy ) throws IOException, SDIException,
+            RIOException {
+        return new RIO( p,
                         species_tree,
                         algorithm,
                         rerooting,
@@ -682,7 +719,8 @@ public final class RIO {
                         first,
                         last,
                         produce_log,
-                        verbose );
+                        verbose,
+                        transfer_taxonomy );
     }
 
     public final static RIO executeAnalysis( final Phylogeny[] gene_trees, final Phylogeny species_tree )
@@ -695,6 +733,7 @@ public final class RIO {
                         DEFAULT_RANGE,
                         DEFAULT_RANGE,
                         false,
+                        false,
                         false );
     }
 
@@ -704,7 +743,9 @@ public final class RIO {
                                              final REROOTING rerooting,
                                              final String outgroup,
                                              final boolean produce_log,
-                                             final boolean verbose ) throws IOException, SDIException, RIOException {
+                                             final boolean verbose,
+                                             final boolean transfer_taxonomy ) throws IOException, SDIException,
+            RIOException {
         return new RIO( gene_trees,
                         species_tree,
                         algorithm,
@@ -713,7 +754,8 @@ public final class RIO {
                         DEFAULT_RANGE,
                         DEFAULT_RANGE,
                         produce_log,
-                        verbose );
+                        verbose,
+                        transfer_taxonomy );
     }
 
     public final static RIO executeAnalysis( final Phylogeny[] gene_trees,
@@ -724,35 +766,59 @@ public final class RIO {
                                              final int first,
                                              final int last,
                                              final boolean produce_log,
-                                             final boolean verbose ) throws IOException, SDIException, RIOException {
-        return new RIO( gene_trees, species_tree, algorithm, rerooting, outgroup, first, last, produce_log, verbose );
+                                             final boolean verbose,
+                                             final boolean transfer_taxonomy ) throws IOException, SDIException,
+            RIOException {
+        return new RIO( gene_trees,
+                        species_tree,
+                        algorithm,
+                        rerooting,
+                        outgroup,
+                        first,
+                        last,
+                        produce_log,
+                        verbose,
+                        transfer_taxonomy );
     }
 
-    public final static RIO executeAnalysis( final IteratingPhylogenyParser p,
-                                             final Phylogeny species_tree,
-                                             final ALGORITHM algorithm,
-                                             final REROOTING rerooting,
-                                             final String outgroup,
-                                             final int first,
-                                             final int last,
-                                             final boolean produce_log,
-                                             final boolean verbose ) throws IOException, SDIException, RIOException {
-        return new RIO( p, species_tree, algorithm, rerooting, outgroup, first, last, produce_log, verbose );
+    private final static void calculateOrthologTable( final Phylogeny g, final boolean sort, final int counter )
+            throws RIOException {
+        if ( counter == 0 ) {
+            final List<String> labels = new ArrayList<String>();
+            final Set<String> labels_set = new HashSet<String>();
+            for( final PhylogenyNode n : g.getExternalNodes() ) {
+                final String label = obtainLabel( labels_set, n );
+                labels_set.add( label );
+                labels.add( label );
+            }
+            if ( sort ) {
+                Collections.sort( labels );
+            }
+            _m = new IntMatrix( labels );
+        }
+        updateCounts( _m, counter, g );
     }
 
-    private final static void checkPreconditions( final Phylogeny[] gene_trees,
+    private final static void checkPreconditions( final IteratingPhylogenyParser p,
                                                   final Phylogeny species_tree,
                                                   final REROOTING rerooting,
                                                   final String outgroup,
                                                   final int first,
-                                                  final int last ) throws RIOException {
+                                                  final int last ) throws RIOException, IOException {
+        final Phylogeny g0 = p.next();
+        if ( ( g0 == null ) || g0.isEmpty() ) {
+            throw new RIOException( "input file does not seem to contain any gene trees" );
+        }
+        if ( g0.getNumberOfExternalNodes() < 2 ) {
+            throw new RIOException( "input file does not seem to contain any useable gene trees" );
+        }
         if ( !species_tree.isRooted() ) {
             throw new RIOException( "species tree is not rooted" );
         }
         if ( !( ( last == DEFAULT_RANGE ) && ( first == DEFAULT_RANGE ) )
-                && ( ( last < first ) || ( last >= gene_trees.length ) || ( last < 0 ) || ( first < 0 ) ) ) {
+                && ( ( last < first ) || ( last < 0 ) || ( first < 0 ) ) ) {
             throw new RIOException( "attempt to set range (0-based) of gene to analyze to: from " + first + " to "
-                    + last + " (out of " + gene_trees.length + ")" );
+                    + last );
         }
         if ( ( rerooting == REROOTING.OUTGROUP ) && ForesterUtil.isEmpty( outgroup ) ) {
             throw new RIOException( "outgroup not set for midpoint rooting" );
@@ -760,13 +826,12 @@ public final class RIO {
         if ( ( rerooting != REROOTING.OUTGROUP ) && !ForesterUtil.isEmpty( outgroup ) ) {
             throw new RIOException( "outgroup only used for midpoint rooting" );
         }
-        if ( ( rerooting == REROOTING.MIDPOINT )
-                && ( PhylogenyMethods.calculateMaxDistanceToRoot( gene_trees[ 0 ] ) <= 0 ) ) {
+        if ( ( rerooting == REROOTING.MIDPOINT ) && ( PhylogenyMethods.calculateMaxDistanceToRoot( g0 ) <= 0 ) ) {
             throw new RIOException( "attempt to use midpoint rooting on gene trees which seem to have no (positive) branch lengths (cladograms)" );
         }
         if ( rerooting == REROOTING.OUTGROUP ) {
             try {
-                gene_trees[ 0 ].getNode( outgroup );
+                g0.getNode( outgroup );
             }
             catch ( final IllegalArgumentException e ) {
                 throw new RIOException( "cannot perform re-rooting by outgroup: " + e.getLocalizedMessage() );
@@ -774,26 +839,19 @@ public final class RIO {
         }
     }
 
-    private final static void checkPreconditions( final IteratingPhylogenyParser p,
+    private final static void checkPreconditions( final Phylogeny[] gene_trees,
                                                   final Phylogeny species_tree,
                                                   final REROOTING rerooting,
                                                   final String outgroup,
                                                   final int first,
-                                                  final int last ) throws RIOException, IOException {
-        final Phylogeny g0 = p.next();
-        if ( ( g0 == null ) || g0.isEmpty() ) {
-            throw new RIOException( "input file does not seem to contain any gene trees" );
-        }
-        if ( g0.getNumberOfExternalNodes() < 2 ) {
-            throw new RIOException( "input file does not seem to contain any useable gene trees" );
-        }
+                                                  final int last ) throws RIOException {
         if ( !species_tree.isRooted() ) {
             throw new RIOException( "species tree is not rooted" );
         }
         if ( !( ( last == DEFAULT_RANGE ) && ( first == DEFAULT_RANGE ) )
-                && ( ( last < first ) || ( last < 0 ) || ( first < 0 ) ) ) {
+                && ( ( last < first ) || ( last >= gene_trees.length ) || ( last < 0 ) || ( first < 0 ) ) ) {
             throw new RIOException( "attempt to set range (0-based) of gene to analyze to: from " + first + " to "
-                    + last );
+                    + last + " (out of " + gene_trees.length + ")" );
         }
         if ( ( rerooting == REROOTING.OUTGROUP ) && ForesterUtil.isEmpty( outgroup ) ) {
             throw new RIOException( "outgroup not set for midpoint rooting" );
@@ -801,12 +859,13 @@ public final class RIO {
         if ( ( rerooting != REROOTING.OUTGROUP ) && !ForesterUtil.isEmpty( outgroup ) ) {
             throw new RIOException( "outgroup only used for midpoint rooting" );
         }
-        if ( ( rerooting == REROOTING.MIDPOINT ) && ( PhylogenyMethods.calculateMaxDistanceToRoot( g0 ) <= 0 ) ) {
+        if ( ( rerooting == REROOTING.MIDPOINT )
+                && ( PhylogenyMethods.calculateMaxDistanceToRoot( gene_trees[ 0 ] ) <= 0 ) ) {
             throw new RIOException( "attempt to use midpoint rooting on gene trees which seem to have no (positive) branch lengths (cladograms)" );
         }
         if ( rerooting == REROOTING.OUTGROUP ) {
             try {
-                g0.getNode( outgroup );
+                gene_trees[ 0 ].getNode( outgroup );
             }
             catch ( final IllegalArgumentException e ) {
                 throw new RIOException( "cannot perform re-rooting by outgroup: " + e.getLocalizedMessage() );
@@ -814,6 +873,30 @@ public final class RIO {
         }
     }
 
+    private final static String obtainLabel( final Set<String> labels_set, final PhylogenyNode n ) throws RIOException {
+        String label;
+        if ( n.getNodeData().isHasSequence() && !ForesterUtil.isEmpty( n.getNodeData().getSequence().getName() ) ) {
+            label = n.getNodeData().getSequence().getName();
+        }
+        else if ( n.getNodeData().isHasSequence() && !ForesterUtil.isEmpty( n.getNodeData().getSequence().getSymbol() ) ) {
+            label = n.getNodeData().getSequence().getSymbol();
+        }
+        else if ( n.getNodeData().isHasSequence()
+                && !ForesterUtil.isEmpty( n.getNodeData().getSequence().getGeneName() ) ) {
+            label = n.getNodeData().getSequence().getGeneName();
+        }
+        else if ( !ForesterUtil.isEmpty( n.getName() ) ) {
+            label = n.getName();
+        }
+        else {
+            throw new RIOException( "node " + n + " has no appropriate label" );
+        }
+        if ( labels_set.contains( label ) ) {
+            throw new RIOException( "label " + label + " is not unique" );
+        }
+        return label;
+    }
+
     private final static Phylogeny[] parseGeneTrees( final File gene_trees_file ) throws FileNotFoundException,
             IOException {
         final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
@@ -822,13 +905,13 @@ public final class RIO {
             final NHXParser nhx = ( NHXParser ) p;
             nhx.setReplaceUnderscores( false );
             nhx.setIgnoreQuotes( true );
-            nhx.setTaxonomyExtraction( NHXParser.TAXONOMY_EXTRACTION.YES );
+            nhx.setTaxonomyExtraction( TAXONOMY_EXTRACTION.AGGRESSIVE );
         }
         else if ( p instanceof NexusPhylogeniesParser ) {
             final NexusPhylogeniesParser nex = ( NexusPhylogeniesParser ) p;
             nex.setReplaceUnderscores( false );
             nex.setIgnoreQuotes( true );
-            nex.setTaxonomyExtraction( TAXONOMY_EXTRACTION.YES );
+            nex.setTaxonomyExtraction( TAXONOMY_EXTRACTION.AGGRESSIVE );
         }
         return factory.create( gene_trees_file, p );
     }
@@ -844,6 +927,31 @@ public final class RIO {
         }
     }
 
+    private final static void updateCounts( final IntMatrix m, final int counter, final Phylogeny g )
+            throws RIOException {
+        PhylogenyMethods.preOrderReId( g );
+        final HashMap<String, PhylogenyNode> map = PhylogenyMethods.createNameToExtNodeMap( g );
+        for( int x = 0; x < m.size(); ++x ) {
+            final String mx = m.getLabel( x );
+            final PhylogenyNode nx = map.get( mx );
+            if ( nx == null ) {
+                throw new RIOException( "node \"" + mx + "\" not present in gene tree #" + counter );
+            }
+            String my;
+            PhylogenyNode ny;
+            for( int y = 0; y < m.size(); ++y ) {
+                my = m.getLabel( y );
+                ny = map.get( my );
+                if ( ny == null ) {
+                    throw new RIOException( "node \"" + my + "\" not present in gene tree #" + counter );
+                }
+                if ( !PhylogenyMethods.calculateLCAonTreeWithIdsInPreOrder( nx, ny ).isDuplication() ) {
+                    m.inreaseByOne( x, y );
+                }
+            }
+        }
+    }
+
     public enum REROOTING {
         NONE, BY_ALGORITHM, MIDPOINT, OUTGROUP;
     }