From: cmzmasek@gmail.com Date: Wed, 19 Dec 2012 22:15:16 +0000 (+0000) Subject: "rio" work X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=f0e9864f2a7aa03931cc4c243ca9819bd9bdb9a9;p=jalview.git "rio" work --- diff --git a/forester/java/src/org/forester/application/rio.java b/forester/java/src/org/forester/application/rio.java index 065b676..d1a317a 100644 --- a/forester/java/src/org/forester/application/rio.java +++ b/forester/java/src/org/forester/application/rio.java @@ -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"; diff --git a/forester/java/src/org/forester/io/parsers/nexus/NexusPhylogeniesParser.java b/forester/java/src/org/forester/io/parsers/nexus/NexusPhylogeniesParser.java index 6a51d8a..ef4b4c3 100644 --- a/forester/java/src/org/forester/io/parsers/nexus/NexusPhylogeniesParser.java +++ b/forester/java/src/org/forester/io/parsers/nexus/NexusPhylogeniesParser.java @@ -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 _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 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 getTaxlabels() { - return _taxlabels; - } - - private Map 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() ); - setTaxlabels( new ArrayList() ); - setTranslateMap( new HashMap() ); - } - 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 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 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 getTaxlabels() { + return _taxlabels; + } + + private TAXONOMY_EXTRACTION getTaxonomyExtraction() { + return _taxonomy_extraction; + } + + private Map 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() ); + setTaxlabels( new ArrayList() ); + setTranslateMap( new HashMap() ); + } + + private void setPhylogenies( final ArrayList phylogenies ) { + _phylogenies = phylogenies; + } + private void setTaxlabels( final List taxlabels ) { _taxlabels = taxlabels; } diff --git a/forester/java/src/org/forester/rio/RIO.java b/forester/java/src/org/forester/rio/RIO.java index 38efa35..0ecb248 100644 --- a/forester/java/src/org/forester/rio/RIO.java +++ b/forester/java/src/org/forester/rio/RIO.java @@ -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 ) { diff --git a/forester/java/src/org/forester/sdi/SDIutil.java b/forester/java/src/org/forester/sdi/SDIutil.java index 5526618..077f818 100644 --- a/forester/java/src/org/forester/sdi/SDIutil.java +++ b/forester/java/src/org/forester/sdi/SDIutil.java @@ -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 );