make PH, aPH, CL selection
[jalview.git] / forester / java / src / org / forester / rio / RIO.java
index d360a1f..4552ba4 100644 (file)
@@ -89,7 +89,8 @@ 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;
         }
@@ -110,7 +111,7 @@ public final class RIO {
         _removed_gene_tree_nodes = null;
         _duplications_stats = new BasicDescriptiveStatistics();
         p.reset();
-        inferOrthologs( p, species_tree, algorithm, outgroup, first, last );
+        inferOrthologs( p, species_tree, algorithm, outgroup, first, last, transfer_taxonomy );
         _species_tree = species_tree;
     }
 
@@ -122,7 +123,8 @@ 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;
         }
@@ -141,7 +143,7 @@ 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 );
+        inferOrthologs( gene_trees, species_tree, algorithm, outgroup, first, last, transfer_taxonomy );
         _species_tree = species_tree;
     }
 
@@ -156,7 +158,7 @@ public final class RIO {
     /**
      * Returns the numbers of number of ext nodes in gene trees analyzed (after
      * stripping).
-     * 
+     *
      * @return number of ext nodes in gene trees analyzed (after stripping)
      */
     public final int getExtNodesOfAnalyzedGeneTrees() {
@@ -170,7 +172,7 @@ public final class RIO {
     /**
      * Returns the numbers of number of int nodes in gene trees analyzed (after
      * stripping).
-     * 
+     *
      * @return number of int nodes in gene trees analyzed (after stripping)
      */
     public final int getIntNodesOfAnalyzedGeneTrees() {
@@ -202,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" );
         }
@@ -252,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;
             }
@@ -265,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();
@@ -278,8 +286,9 @@ public final class RIO {
                                        final ALGORITHM algorithm,
                                        final String outgroup,
                                        final int first,
-                                       final int last ) throws SDIException, RIOException, FileNotFoundException,
-            IOException {
+                                       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 );
@@ -332,7 +341,12 @@ public final class RIO {
                     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 );
+            _analyzed_gene_trees[ i ] = performOrthologInference( gt,
+                                                                  species_tree,
+                                                                  algorithm,
+                                                                  outgroup,
+                                                                  i,
+                                                                  transfer_taxonomy );
         }
         if ( log() ) {
             postLog( species_tree, first, last );
@@ -382,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: {
@@ -390,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: {
@@ -412,11 +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();
@@ -440,12 +458,12 @@ 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() ) {
                     throw new RIOException( "node with no (appropriate) taxonomic information found in gene tree #" + i
-                            + ": " + r.toString() );
+                                            + ": " + r.toString() );
                 }
             }
             assigned_tree = gene_tree;
@@ -482,13 +500,13 @@ public final class RIO {
         }
         log( "Gene trees analyzed                             : " + _duplications_stats.getN() );
         log( "Mean number of duplications                     : " + df.format( _duplications_stats.arithmeticMean() )
-                + " (sd: " + df.format( _duplications_stats.sampleStandardDeviation() ) + ")" + " ("
-                + df.format( ( 100.0 * _duplications_stats.arithmeticMean() ) / getIntNodesOfAnalyzedGeneTrees() )
-                + "%)" );
+             + " (sd: " + df.format( _duplications_stats.sampleStandardDeviation() ) + ")" + " ("
+             + df.format( ( 100.0 * _duplications_stats.arithmeticMean() ) / getIntNodesOfAnalyzedGeneTrees() )
+             + "%)" );
         if ( _duplications_stats.getN() > 3 ) {
             log( "Median number of duplications                   : " + df.format( _duplications_stats.median() )
-                    + " (" + df.format( ( 100.0 * _duplications_stats.median() ) / getIntNodesOfAnalyzedGeneTrees() )
-                    + "%)" );
+                 + " (" + df.format( ( 100.0 * _duplications_stats.median() ) / getIntNodesOfAnalyzedGeneTrees() )
+                 + "%)" );
         }
         log( "Minimum duplications                            : " + ( int ) _duplications_stats.getMin() + " ("
                 + df.format( ( 100.0 * _duplications_stats.getMin() ) / getIntNodesOfAnalyzedGeneTrees() ) + "%)" );
@@ -560,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" );
@@ -570,7 +590,16 @@ 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,
@@ -579,7 +608,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( parseGeneTrees( gene_trees_file ),
                         species_tree,
                         algorithm,
@@ -588,7 +619,8 @@ public final class RIO {
                         DEFAULT_RANGE,
                         DEFAULT_RANGE,
                         produce_log,
-                        verbose );
+                        verbose,
+                        transfer_taxonomy );
     }
 
     public final static RIO executeAnalysis( final File gene_trees_file,
@@ -599,7 +631,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 {
         return new RIO( parseGeneTrees( gene_trees_file ),
                         species_tree,
                         algorithm,
@@ -608,7 +642,8 @@ public final class RIO {
                         first,
                         last,
                         produce_log,
-                        verbose );
+                        verbose,
+                        transfer_taxonomy );
     }
 
     public final static RIO executeAnalysis( final IteratingPhylogenyParser p,
@@ -619,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" );
@@ -630,7 +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 );
+        return new RIO( p,
+                        species_tree,
+                        algorithm,
+                        rerooting,
+                        outgroup,
+                        first,
+                        last,
+                        produce_log,
+                        verbose,
+                        transfer_taxonomy );
     }
 
     public final static RIO executeAnalysis( final IteratingPhylogenyParser p,
@@ -639,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,
@@ -648,7 +696,8 @@ public final class RIO {
                         DEFAULT_RANGE,
                         DEFAULT_RANGE,
                         produce_log,
-                        verbose );
+                        verbose,
+                        transfer_taxonomy );
     }
 
     public final static RIO executeAnalysis( final IteratingPhylogenyParser p,
@@ -659,8 +708,19 @@ public final class RIO {
                                              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 );
+                                             final boolean verbose,
+                                             final boolean transfer_taxonomy ) throws IOException, SDIException,
+                                             RIOException {
+        return new RIO( p,
+                        species_tree,
+                        algorithm,
+                        rerooting,
+                        outgroup,
+                        first,
+                        last,
+                        produce_log,
+                        verbose,
+                        transfer_taxonomy );
     }
 
     public final static RIO executeAnalysis( final Phylogeny[] gene_trees, final Phylogeny species_tree )
@@ -673,6 +733,7 @@ public final class RIO {
                         DEFAULT_RANGE,
                         DEFAULT_RANGE,
                         false,
+                        false,
                         false );
     }
 
@@ -682,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,
@@ -691,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,
@@ -702,8 +766,19 @@ 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 );
     }
 
     private final static void calculateOrthologTable( final Phylogeny g, final boolean sort, final int counter )
@@ -806,6 +881,10 @@ public final class RIO {
         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();
         }
@@ -819,20 +898,20 @@ public final class RIO {
     }
 
     private final static Phylogeny[] parseGeneTrees( final File gene_trees_file ) throws FileNotFoundException,
-            IOException {
+    IOException {
         final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
         final PhylogenyParser p = ParserUtils.createParserDependingOnFileType( gene_trees_file, true );
         if ( p instanceof NHXParser ) {
             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 );
     }
@@ -842,7 +921,7 @@ public final class RIO {
         if ( o > 0 ) {
             if ( verbose ) {
                 System.out.println( "warning: species tree has " + o
-                        + " internal nodes with only one descendent which are therefore going to be removed" );
+                                    + " internal nodes with only one descendent which are therefore going to be removed" );
             }
             PhylogenyMethods.deleteInternalNodesWithOnlyOneDescendent( species_tree );
         }