"rio" work
authorcmzmasek@gmail.com <cmzmasek@gmail.com@ca865154-3058-d1c3-3e42-d8f55a55bdbd>
Wed, 19 Dec 2012 22:15:16 +0000 (22:15 +0000)
committercmzmasek@gmail.com <cmzmasek@gmail.com@ca865154-3058-d1c3-3e42-d8f55a55bdbd>
Wed, 19 Dec 2012 22:15:16 +0000 (22:15 +0000)
forester/java/src/org/forester/application/rio.java
forester/java/src/org/forester/io/parsers/nexus/NexusPhylogeniesParser.java
forester/java/src/org/forester/rio/RIO.java
forester/java/src/org/forester/sdi/SDIutil.java

index 065b676..d1a317a 100644 (file)
@@ -47,7 +47,7 @@ public class rio {
 
     final static private String PRG_NAME      = "rio";
     final static private String PRG_VERSION   = "4.000 beta 3";
-    final static private String PRG_DATE      = "2012.12.18";
+    final static private String PRG_DATE      = "2012.12.19";
     final static private String E_MAIL        = "czmasek@burnham.org";
     final static private String WWW           = "www.phylosoft.org/forester/";
     final static private String HELP_OPTION_1 = "help";
index 6a51d8a..ef4b4c3 100644 (file)
@@ -38,6 +38,7 @@ import org.forester.archaeopteryx.Constants;
 import org.forester.io.parsers.PhylogenyParser;
 import org.forester.io.parsers.nhx.NHXFormatException;
 import org.forester.io.parsers.nhx.NHXParser;
+import org.forester.io.parsers.nhx.NHXParser.TAXONOMY_EXTRACTION;
 import org.forester.io.parsers.util.ParserUtils;
 import org.forester.io.parsers.util.PhylogenyParserException;
 import org.forester.phylogeny.Phylogeny;
@@ -65,79 +66,7 @@ public class NexusPhylogeniesParser implements PhylogenyParser {
     private Map<String, String>  _translate_map;
     private boolean              _replace_underscores      = NHXParser.REPLACE_UNDERSCORES_DEFAULT;
     private boolean              _ignore_quotes_in_nh_data = Constants.NH_PARSING_IGNORE_QUOTES_DEFAULT;
-
-    private void createPhylogeny( final String name,
-                                  final StringBuffer nhx,
-                                  final boolean rooted_info_present,
-                                  final boolean is_rooted ) throws IOException {
-        final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
-        final NHXParser pars = new NHXParser();
-        pars.setTaxonomyExtraction( NHXParser.TAXONOMY_EXTRACTION.NO );
-        pars.setReplaceUnderscores( isReplaceUnderscores() );
-        pars.setIgnoreQuotes( isIgnoreQuotes() );
-        if ( rooted_info_present ) {
-            pars.setGuessRootedness( false );
-        }
-        final Phylogeny p = factory.create( nhx, pars )[ 0 ];
-        p.setName( name );
-        if ( rooted_info_present ) {
-            p.setRooted( is_rooted );
-        }
-        if ( ( getTaxlabels().size() > 0 ) || ( getTranslateMap().size() > 0 ) ) {
-            final PhylogenyNodeIterator it = p.iteratorExternalForward();
-            while ( it.hasNext() ) {
-                final PhylogenyNode node = it.next();
-                if ( ( getTranslateMap().size() > 0 ) && getTranslateMap().containsKey( node.getName() ) ) {
-                    node.setName( getTranslateMap().get( node.getName() ).replaceAll( "['\"]+", "" ) );
-                }
-                else if ( getTaxlabels().size() > 0 ) {
-                    int i = -1;
-                    try {
-                        i = Integer.parseInt( node.getName() );
-                    }
-                    catch ( final NumberFormatException e ) {
-                        // Ignore.
-                    }
-                    if ( i > 0 ) {
-                        node.setName( getTaxlabels().get( i - 1 ).replaceAll( "['\"]+", "" ) );
-                    }
-                }
-            }
-        }
-        getPhylogenies().add( p );
-    }
-
-    private Object getNexusSource() {
-        return _nexus_source;
-    }
-
-    private List<Phylogeny> getPhylogenies() {
-        return _phylogenies;
-    }
-
-    private Phylogeny[] getPhylogeniesAsArray() {
-        final Phylogeny[] p = new Phylogeny[ getPhylogenies().size() ];
-        for( int i = 0; i < getPhylogenies().size(); ++i ) {
-            p[ i ] = getPhylogenies().get( i );
-        }
-        return p;
-    }
-
-    private List<String> getTaxlabels() {
-        return _taxlabels;
-    }
-
-    private Map<String, String> getTranslateMap() {
-        return _translate_map;
-    }
-
-    private boolean isIgnoreQuotes() {
-        return _ignore_quotes_in_nh_data;
-    }
-
-    private boolean isReplaceUnderscores() {
-        return _replace_underscores;
-    }
+    private TAXONOMY_EXTRACTION  _taxonomy_extraction      = NHXParser.TAXONOMY_EXTRACTION_DEFAULT;
 
     @Override
     public Phylogeny[] parse() throws IOException, NHXFormatException {
@@ -150,7 +79,6 @@ public class NexusPhylogeniesParser implements PhylogenyParser {
         boolean in_trees_block = false;
         boolean in_taxalabels = false;
         boolean in_translate = false;
-        final boolean in_comment = false;
         boolean in_tree = false;
         boolean rooted_info_present = false;
         boolean is_rooted = false;
@@ -270,20 +198,10 @@ public class NexusPhylogeniesParser implements PhylogenyParser {
         return getPhylogeniesAsArray();
     }
 
-    private void reset() {
-        setPhylogenies( new ArrayList<Phylogeny>() );
-        setTaxlabels( new ArrayList<String>() );
-        setTranslateMap( new HashMap<String, String>() );
-    }
-
     public void setIgnoreQuotes( final boolean ignore_quotes_in_nh_data ) {
         _ignore_quotes_in_nh_data = ignore_quotes_in_nh_data;
     }
 
-    private void setPhylogenies( final ArrayList<Phylogeny> phylogenies ) {
-        _phylogenies = phylogenies;
-    }
-
     public void setReplaceUnderscores( final boolean replace_underscores ) {
         _replace_underscores = replace_underscores;
     }
@@ -296,6 +214,97 @@ public class NexusPhylogeniesParser implements PhylogenyParser {
         _nexus_source = nexus_source;
     }
 
+    public void setTaxonomyExtraction( final TAXONOMY_EXTRACTION taxonomy_extraction ) {
+        _taxonomy_extraction = taxonomy_extraction;
+    }
+
+    private void createPhylogeny( final String name,
+                                  final StringBuffer nhx,
+                                  final boolean rooted_info_present,
+                                  final boolean is_rooted ) throws IOException {
+        final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
+        final NHXParser pars = new NHXParser();
+        pars.setTaxonomyExtraction( getTaxonomyExtraction() );
+        pars.setReplaceUnderscores( isReplaceUnderscores() );
+        pars.setIgnoreQuotes( isIgnoreQuotes() );
+        if ( rooted_info_present ) {
+            pars.setGuessRootedness( false );
+        }
+        final Phylogeny p = factory.create( nhx, pars )[ 0 ];
+        p.setName( name );
+        if ( rooted_info_present ) {
+            p.setRooted( is_rooted );
+        }
+        if ( ( getTaxlabels().size() > 0 ) || ( getTranslateMap().size() > 0 ) ) {
+            final PhylogenyNodeIterator it = p.iteratorExternalForward();
+            while ( it.hasNext() ) {
+                final PhylogenyNode node = it.next();
+                if ( ( getTranslateMap().size() > 0 ) && getTranslateMap().containsKey( node.getName() ) ) {
+                    node.setName( getTranslateMap().get( node.getName() ).replaceAll( "['\"]+", "" ) );
+                }
+                else if ( getTaxlabels().size() > 0 ) {
+                    int i = -1;
+                    try {
+                        i = Integer.parseInt( node.getName() );
+                    }
+                    catch ( final NumberFormatException e ) {
+                        // Ignore.
+                    }
+                    if ( i > 0 ) {
+                        node.setName( getTaxlabels().get( i - 1 ).replaceAll( "['\"]+", "" ) );
+                    }
+                }
+            }
+        }
+        getPhylogenies().add( p );
+    }
+
+    private Object getNexusSource() {
+        return _nexus_source;
+    }
+
+    private List<Phylogeny> getPhylogenies() {
+        return _phylogenies;
+    }
+
+    private Phylogeny[] getPhylogeniesAsArray() {
+        final Phylogeny[] p = new Phylogeny[ getPhylogenies().size() ];
+        for( int i = 0; i < getPhylogenies().size(); ++i ) {
+            p[ i ] = getPhylogenies().get( i );
+        }
+        return p;
+    }
+
+    private List<String> getTaxlabels() {
+        return _taxlabels;
+    }
+
+    private TAXONOMY_EXTRACTION getTaxonomyExtraction() {
+        return _taxonomy_extraction;
+    }
+
+    private Map<String, String> getTranslateMap() {
+        return _translate_map;
+    }
+
+    private boolean isIgnoreQuotes() {
+        return _ignore_quotes_in_nh_data;
+    }
+
+    private boolean isReplaceUnderscores() {
+        return _replace_underscores;
+    }
+
+    private void reset() {
+        setPhylogenies( new ArrayList<Phylogeny>() );
+        setTaxlabels( new ArrayList<String>() );
+        setTranslateMap( new HashMap<String, String>() );
+    }
+
+    private void setPhylogenies( final ArrayList<Phylogeny> phylogenies ) {
+        _phylogenies = phylogenies;
+    }
+
     private void setTaxlabels( final List<String> taxlabels ) {
         _taxlabels = taxlabels;
     }
index 38efa35..0ecb248 100644 (file)
@@ -41,6 +41,7 @@ import java.util.TreeSet;
 
 import org.forester.datastructures.IntMatrix;
 import org.forester.io.parsers.PhylogenyParser;
+import org.forester.io.parsers.nexus.NexusPhylogeniesParser;
 import org.forester.io.parsers.nhx.NHXParser;
 import org.forester.io.parsers.nhx.NHXParser.TAXONOMY_EXTRACTION;
 import org.forester.io.parsers.util.ParserUtils;
@@ -476,15 +477,7 @@ public final class RIO {
                                              final int last,
                                              final boolean produce_log,
                                              final boolean verbose ) throws IOException, SDIException, RIOException {
-        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 );
-        }
-        final Phylogeny[] gene_trees = factory.create( gene_trees_file, p );
+        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" );
         }
@@ -503,16 +496,7 @@ public final class RIO {
                                              final String outgroup,
                                              final boolean produce_log,
                                              final boolean verbose ) throws IOException, SDIException, RIOException {
-        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 );
-        }
-        final Phylogeny[] gene_trees = factory.create( gene_trees_file, p );
-        return new RIO( gene_trees,
+        return new RIO( parseGeneTrees( gene_trees_file ),
                         species_tree,
                         algorithm,
                         rerooting,
@@ -532,19 +516,15 @@ public final class RIO {
                                              final int last,
                                              final boolean produce_log,
                                              final boolean verbose ) throws IOException, SDIException, RIOException {
-        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 );
-        }
-        final Phylogeny[] gene_trees = factory.create( gene_trees_file, p );
-        if ( gene_trees.length < 1 ) {
-            throw new RIOException( "\"" + gene_trees_file + "\" is devoid of appropriate gene trees" );
-        }
-        return new RIO( gene_trees, species_tree, algorithm, rerooting, outgroup, first, last, produce_log, verbose );
+        return new RIO( parseGeneTrees( gene_trees_file ),
+                        species_tree,
+                        algorithm,
+                        rerooting,
+                        outgroup,
+                        first,
+                        last,
+                        produce_log,
+                        verbose );
     }
 
     public final static RIO executeAnalysis( final Phylogeny[] gene_trees, final Phylogeny species_tree )
@@ -624,6 +604,25 @@ public final class RIO {
         }
     }
 
+    private final static Phylogeny[] parseGeneTrees( final File gene_trees_file ) throws FileNotFoundException,
+            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 );
+        }
+        else if ( p instanceof NexusPhylogeniesParser ) {
+            final NexusPhylogeniesParser nex = ( NexusPhylogeniesParser ) p;
+            nex.setReplaceUnderscores( false );
+            nex.setIgnoreQuotes( true );
+            nex.setTaxonomyExtraction( TAXONOMY_EXTRACTION.YES );
+        }
+        return factory.create( gene_trees_file, p );
+    }
+
     private final static void removeSingleDescendentsNodes( final Phylogeny species_tree, final boolean verbose ) {
         final int o = PhylogenyMethods.countNumberOfOneDescendantNodes( species_tree );
         if ( o > 0 ) {
index 5526618..077f818 100644 (file)
@@ -6,7 +6,9 @@ import java.io.FileNotFoundException;
 import java.io.IOException;
 
 import org.forester.io.parsers.PhylogenyParser;
+import org.forester.io.parsers.nexus.NexusPhylogeniesParser;
 import org.forester.io.parsers.nhx.NHXParser;
+import org.forester.io.parsers.nhx.NHXParser.TAXONOMY_EXTRACTION;
 import org.forester.io.parsers.phyloxml.PhyloXmlDataFormatException;
 import org.forester.io.parsers.phyloxml.PhyloXmlParser;
 import org.forester.io.parsers.util.ParserUtils;
@@ -70,7 +72,7 @@ public class SDIutil {
                                                     final File species_tree_file,
                                                     final boolean replace_undescores_in_nhx_trees,
                                                     final boolean ignore_quotes_in_nhx_trees,
-                                                    final NHXParser.TAXONOMY_EXTRACTION taxonomy_extraction_in_nhx_trees )
+                                                    final TAXONOMY_EXTRACTION taxonomy_extraction_in_nhx_trees )
             throws FileNotFoundException, PhyloXmlDataFormatException, IOException, SDIException {
         Phylogeny species_tree;
         final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
@@ -85,6 +87,12 @@ public class SDIutil {
                 nhx.setIgnoreQuotes( ignore_quotes_in_nhx_trees );
                 nhx.setTaxonomyExtraction( taxonomy_extraction_in_nhx_trees );
             }
+            else if ( p instanceof NexusPhylogeniesParser ) {
+                final NexusPhylogeniesParser nex = ( NexusPhylogeniesParser ) p;
+                nex.setReplaceUnderscores( replace_undescores_in_nhx_trees );
+                nex.setIgnoreQuotes( ignore_quotes_in_nhx_trees );
+                nex.setTaxonomyExtraction( taxonomy_extraction_in_nhx_trees );
+            }
             species_tree = factory.create( species_tree_file, p )[ 0 ];
             species_tree.setRooted( true );
             final TaxonomyComparisonBase comp_base = determineTaxonomyComparisonBase( gene_tree );