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;
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 {
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;
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;
}
_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;
}
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;
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" );
}
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,
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 )
}
}
+ 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 ) {
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;
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();
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 );