X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=forester%2Fjava%2Fsrc%2Forg%2Fforester%2Fapplication%2Fgsdi.java;h=a77ac2ddf2b830a46af82d06f0df6434402d9f69;hb=0b49b8e750b34d28a5989facdd8a7959870de996;hp=253bfcc048225724c6d2722040de7522a86f77b4;hpb=9956a18ae590422c0500a1770ff4cdaf8d787f82;p=jalview.git diff --git a/forester/java/src/org/forester/application/gsdi.java b/forester/java/src/org/forester/application/gsdi.java index 253bfcc..a77ac2d 100644 --- a/forester/java/src/org/forester/application/gsdi.java +++ b/forester/java/src/org/forester/application/gsdi.java @@ -21,7 +21,7 @@ // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA // // Contact: phylosoft @ gmail . com -// WWW: www.phylosoft.org/forester +// WWW: https://sites.google.com/site/cmzmasek/home/software/forester package org.forester.application; @@ -36,11 +36,9 @@ import java.util.SortedSet; import java.util.TreeMap; import java.util.TreeSet; -import org.forester.io.parsers.PhylogenyParser; -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; import org.forester.io.writers.PhylogenyWriter; import org.forester.phylogeny.Phylogeny; import org.forester.phylogeny.PhylogenyMethods; @@ -53,7 +51,6 @@ import org.forester.sdi.GSDIR; import org.forester.sdi.SDIException; import org.forester.sdi.SDIutil; import org.forester.sdi.SDIutil.ALGORITHM; -import org.forester.sdi.SDIutil.TaxonomyComparisonBase; import org.forester.util.CommandLineArguments; import org.forester.util.EasyWriter; import org.forester.util.ForesterConstants; @@ -66,17 +63,18 @@ public final class gsdi { final static private String GSDIR_OPTION = "r"; final static private String MOST_PARSIMONIOUS_OPTION = "m"; final static private String GUESS_FORMAT_OF_SPECIES_TREE = "q"; + final static private String TRANSFER_TAXONOMY_OPTION = "t"; final static private String HELP_OPTION_1 = "help"; final static private String HELP_OPTION_2 = "h"; final static private String SUFFIX_FOR_SPECIES_TREE_USED = "_species_tree_used.xml"; final static private String LOGFILE_SUFFIX = "_gsdi_log.txt"; final static private String REMAPPED_SUFFIX = "_gsdi_remapped.txt"; final static private String PRG_NAME = "gsdi"; - final static private String PRG_VERSION = "1.000"; - final static private String PRG_DATE = "120629"; + final static private String PRG_VERSION = "1.001"; + final static private String PRG_DATE = "130325"; final static private String PRG_DESC = "general speciation duplication inference"; final static private String E_MAIL = "phylosoft@gmail.com"; - final static private String WWW = "www.phylosoft.org/forester"; + final static private String WWW = "https://sites.google.com/site/cmzmasek/home/software/forester"; public static void main( final String args[] ) { try { @@ -107,11 +105,11 @@ public final class gsdi { System.exit( -1 ); } final List allowed_options = new ArrayList(); - allowed_options.add( gsdi.GSDIR_OPTION ); allowed_options.add( gsdi.GUESS_FORMAT_OF_SPECIES_TREE ); allowed_options.add( gsdi.MOST_PARSIMONIOUS_OPTION ); allowed_options.add( gsdi.ALLOW_STRIPPING_OF_GENE_TREE_OPTION ); + allowed_options.add( TRANSFER_TAXONOMY_OPTION ); final String dissallowed_options = cla.validateAllowedOptionsAsString( allowed_options ); if ( dissallowed_options.length() > 0 ) { ForesterUtil.fatalError( gsdi.PRG_NAME, "unknown option(s): " + dissallowed_options ); @@ -130,7 +128,6 @@ public final class gsdi { if ( cla.isOptionSet( gsdi.GSDIR_OPTION ) ) { base_algorithm = ALGORITHM.GSDIR; } - if ( cla.isOptionSet( gsdi.MOST_PARSIMONIOUS_OPTION ) ) { if ( base_algorithm == ALGORITHM.SDI ) { ForesterUtil.fatalError( gsdi.PRG_NAME, "Cannot use most parsimonious duplication mode with SDI" ); @@ -143,6 +140,10 @@ public final class gsdi { } allow_stripping_of_gene_tree = true; } + boolean transfer_taxonomy = false; + if ( cla.isOptionSet( TRANSFER_TAXONOMY_OPTION ) ) { + transfer_taxonomy = true; + } Phylogeny species_tree = null; Phylogeny gene_tree = null; File gene_tree_file = null; @@ -179,68 +180,30 @@ public final class gsdi { } try { final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance(); - gene_tree = factory.create( gene_tree_file, new PhyloXmlParser() )[ 0 ]; + gene_tree = factory.create( gene_tree_file, PhyloXmlParser.createPhyloXmlParserXsdValidating() )[ 0 ]; } catch ( final IOException e ) { - fatalError( "ERROR", - "Failed to read gene tree from [" + gene_tree_file + "]: " + e.getMessage(), + fatalError( "error", + "failed to read gene tree from [" + gene_tree_file + "]: " + e.getMessage(), log_writer ); } try { - final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance(); - final PhylogenyParser p = ParserUtils.createParserDependingOnFileType( species_tree_file, true ); - if ( p instanceof PhyloXmlParser ) { - species_tree = factory.create( species_tree_file, p )[ 0 ]; - } - else { - if ( REPLACE_UNDERSCORES_IN_NH_SPECIES_TREE && ( p instanceof NHXParser ) ) { - ( ( NHXParser ) p ).setReplaceUnderscores( true ); - } - species_tree = factory.create( species_tree_file, p )[ 0 ]; - final TaxonomyComparisonBase comp_base = SDIutil.determineTaxonomyComparisonBase( gene_tree ); - switch ( comp_base ) { - case SCIENTIFIC_NAME: - try { - PhylogenyMethods - .transferNodeNameToField( species_tree, - PhylogenyMethods.PhylogenyNodeField.TAXONOMY_SCIENTIFIC_NAME, - true ); - } - catch ( final PhyloXmlDataFormatException e ) { - fatalError( "USER ERROR", "Failed to transfer general node name to scientific name, in [" - + species_tree_file + "]: " + e.getMessage(), log_writer ); - } - break; - case CODE: - try { - PhylogenyMethods - .transferNodeNameToField( species_tree, - PhylogenyMethods.PhylogenyNodeField.TAXONOMY_CODE, - true ); - } - catch ( final PhyloXmlDataFormatException e ) { - fatalError( "USER ERROR", "Failed to transfer general node name to taxonomy code, in [" - + species_tree_file + "]: " + e.getMessage(), log_writer ); - } - break; - case ID: - try { - PhylogenyMethods.transferNodeNameToField( species_tree, - PhylogenyMethods.PhylogenyNodeField.TAXONOMY_ID, - true ); - } - catch ( final PhyloXmlDataFormatException e ) { - fatalError( "USER ERROR", "Failed to transfer general node name to taxonomy id, in [" - + species_tree_file + "]: " + e.getMessage(), log_writer ); - } - break; - default: - fatalError( "UNEXPECTED ERROR", "unable to determine comparison base", log_writer ); - } - } + species_tree = SDIutil.parseSpeciesTree( gene_tree, + species_tree_file, + REPLACE_UNDERSCORES_IN_NH_SPECIES_TREE, + true, + TAXONOMY_EXTRACTION.NO ); + } + catch ( final PhyloXmlDataFormatException e ) { + fatalError( "user error", + "failed to transfer general node name, in [" + species_tree_file + "]: " + e.getMessage(), + log_writer ); + } + catch ( final SDIException e ) { + fatalError( "user error", e.getMessage(), log_writer ); } catch ( final IOException e ) { - fatalError( "ERROR", + fatalError( "error", "Failed to read species tree from [" + species_tree_file + "]: " + e.getMessage(), log_writer ); } @@ -277,16 +240,17 @@ public final class gsdi { + ( ForesterUtil.isEmpty( species_tree.getName() ) ? "" : gene_tree.getName() ) ); System.out.println( "Species tree name : " + ( ForesterUtil.isEmpty( species_tree.getName() ) ? "" : gene_tree.getName() ) ); + System.out.println( "Transfer taxonomy : " + transfer_taxonomy ); GSDII gsdii = null; final long start_time = new Date().getTime(); try { if ( base_algorithm == ALGORITHM.GSDI ) { - System.out.println( "Algorithm : GSDI" ); - log_writer.println( "Algorithm : GSDI" ); + System.out.println( "Algorithm : GSDI" ); + log_writer.println( "Algorithm : GSDI" ); } else if ( base_algorithm == ALGORITHM.GSDIR ) { - System.out.println( "Algorithm : GSDIR" ); - log_writer.println( "Algorithm : GSDIR" ); + System.out.println( "Algorithm : GSDIR" ); + log_writer.println( "Algorithm : GSDIR" ); } System.out.println( "Use most parsimonous duplication model : " + most_parsimonous_duplication_model ); System.out.println( "Allow stripping of gene tree nodes : " + allow_stripping_of_gene_tree ); @@ -295,13 +259,14 @@ public final class gsdi { log_writer.flush(); if ( base_algorithm == ALGORITHM.GSDI ) { gsdii = new GSDI( gene_tree, - species_tree, - most_parsimonous_duplication_model, - allow_stripping_of_gene_tree, - true ); + species_tree, + most_parsimonous_duplication_model, + allow_stripping_of_gene_tree, + true, + transfer_taxonomy ); } else if ( base_algorithm == ALGORITHM.GSDIR ) { - gsdii = new GSDIR( gene_tree, species_tree, allow_stripping_of_gene_tree, true ); + gsdii = new GSDIR( gene_tree, species_tree, allow_stripping_of_gene_tree, true, transfer_taxonomy ); } } catch ( final SDIException e ) { @@ -310,28 +275,23 @@ public final class gsdi { catch ( final IOException e ) { fatalError( "error", e.toString(), log_writer ); } + catch ( final OutOfMemoryError e ) { + ForesterUtil.outOfMemoryError( e ); + } catch ( final Exception e ) { e.printStackTrace(); fatalError( "unexpected error", e.toString(), log_writer ); } System.out.println( "Running time (excluding I/O) : " + ( new Date().getTime() - start_time ) - + "ms" ); + + "ms" ); log_writer.println( "Running time (excluding I/O) : " + ( new Date().getTime() - start_time ) - + "ms" ); + + "ms" ); System.out.println( "Mapping based on : " + gsdii.getTaxCompBase() ); log_writer.println( "Mapping based on : " + gsdii.getTaxCompBase() ); - if ( ( base_algorithm == ALGORITHM.GSDIR ) ) { - final GSDIR gsdir = ( GSDIR ) gsdii; - System.out.println( "Duplications sum statistics : " + gsdir.getMinDuplicationsSum() ); - log_writer.println( "Duplications sum statistics : " + gsdir.getMinDuplicationsSum() ); - } try { final PhylogenyWriter writer = new PhylogenyWriter(); if ( base_algorithm == ALGORITHM.GSDIR ) { - writer.toPhyloXML( out_file, - ( ( GSDIR ) gsdii ).getMinDuplicationsSumGeneTrees(), - 0, - ForesterUtil.LINE_SEPARATOR ); + writer.toPhyloXML( out_file, ( ( GSDIR ) gsdii ).getMinDuplicationsSumGeneTree(), 0 ); } else { writer.toPhyloXML( out_file, gene_tree, 0 ); @@ -344,14 +304,14 @@ public final class gsdi { System.out.println( "Wrote resulting gene tree to : " + out_file.getCanonicalPath() ); log_writer.println( "Wrote resulting gene tree to : " + out_file.getCanonicalPath() ); final File species_tree_used_file = new File( ForesterUtil.removeSuffix( out_file.toString() ) - + SUFFIX_FOR_SPECIES_TREE_USED ); + + SUFFIX_FOR_SPECIES_TREE_USED ); try { final PhylogenyWriter writer = new PhylogenyWriter(); writer.toPhyloXML( species_tree_used_file, species_tree, 0 ); } catch ( final IOException e ) { ForesterUtil.fatalError( PRG_NAME, "Failed to write to [" + species_tree_used_file.getCanonicalPath() - + "]: " + e.getMessage() ); + + "]: " + e.getMessage() ); } System.out.println( "Wrote (stripped) species tree to : " + species_tree_used_file.getCanonicalPath() ); log_writer.println( "Wrote (stripped) species tree to : " + species_tree_used_file.getCanonicalPath() ); @@ -393,7 +353,6 @@ public final class gsdi { System.out.println( "Number of potential duplications : " + u ); log_writer.println( "Number of potential duplications : " + u ); } - } log_writer.println(); printMappedNodesToLog( log_writer, gsdii ); @@ -405,30 +364,6 @@ public final class gsdi { log_writer.close(); } - private static void writeToRemappedFile( final File out_file, - final SortedSet remapped, - final EasyWriter log_writer ) throws IOException { - final File file = new File( ForesterUtil.removeSuffix( out_file.toString() ) + REMAPPED_SUFFIX ); - final EasyWriter remapped_writer = ForesterUtil.createEasyWriter( file ); - for( final String s : remapped ) { - remapped_writer.println( s ); - } - remapped_writer.close(); - System.out.println( "Wrote remapped gene tree species to : " + file.getCanonicalPath() ); - log_writer.println( "Wrote remapped gene tree species to : " + file.getCanonicalPath() ); - } - - private static void printMappedNodesToLog( final EasyWriter log_writer, final GSDII gsdi ) throws IOException { - final SortedSet ss = new TreeSet(); - for( final PhylogenyNode n : gsdi.getMappedExternalSpeciesTreeNodes() ) { - ss.add( n.toString() ); - } - log_writer.println( "The following " + ss.size() + " species were used: " ); - for( final String s : ss ) { - log_writer.println( " " + s ); - } - } - private static void fatalError( final String type, final String msg, final EasyWriter log_writer ) { try { log_writer.flush(); @@ -443,6 +378,46 @@ public final class gsdi { ForesterUtil.fatalError( gsdi.PRG_NAME, msg ); } + private static void print_help() { + System.out.println( "Usage: " + gsdi.PRG_NAME + + " [-options] " ); + System.out.println(); + System.out.println( "Options:" ); + System.out.println( " -" + gsdi.ALLOW_STRIPPING_OF_GENE_TREE_OPTION + + ": to allow stripping of gene tree nodes without a matching species" ); + System.out.println( " -" + gsdi.MOST_PARSIMONIOUS_OPTION + + ": use most parimonious duplication model for GSDI: " ); + System.out.println( " assign nodes as speciations which would otherwise be assiged" ); + System.out.println( " as potential duplications due to polytomies in the species tree" ); + System.out.println( " -" + gsdi.GUESS_FORMAT_OF_SPECIES_TREE + + ": to allow species tree in other formats than phyloXML (i.e. Newick, NHX, Nexus)" ); + System.out.println( " -" + gsdi.GSDIR_OPTION + + ": to use GSDIR algorithm instead of GSDI algorithm (re-rooting)" ); + System.out.println( " -" + TRANSFER_TAXONOMY_OPTION + + ": to transfer taxonomic data from species tree to gene tree\n" ); + System.out.println(); + System.out.println( "Gene tree:" ); + System.out.println( " in phyloXM format, with taxonomy and sequence data in appropriate fields" ); + System.out.println(); + System.out.println( "Species tree:" ); + System.out.println( " in phyloXML format (unless option -" + gsdi.GUESS_FORMAT_OF_SPECIES_TREE + " is used)" ); + System.out.println(); + System.out.println( "Example: gsdi -" + ALLOW_STRIPPING_OF_GENE_TREE_OPTION + + " gene_tree.xml tree_of_life.xml out.xml" ); + System.out.println(); + } + + private static void printMappedNodesToLog( final EasyWriter log_writer, final GSDII gsdi ) throws IOException { + final SortedSet ss = new TreeSet(); + for( final PhylogenyNode n : gsdi.getMappedExternalSpeciesTreeNodes() ) { + ss.add( n.toString() ); + } + log_writer.println( "The following " + ss.size() + " species were used: " ); + for( final String s : ss ) { + log_writer.println( " " + s ); + } + } + private static void printStrippedGeneTreeNodesToLog( final EasyWriter log_writer, final GSDII gsdi ) throws IOException { final SortedMap sm = new TreeMap(); @@ -467,30 +442,16 @@ public final class gsdi { } } - private static void print_help() { - System.out.println( "Usage: " + gsdi.PRG_NAME - + " [-options] " ); - System.out.println(); - System.out.println( "Options:" ); - System.out.println( " -" + gsdi.ALLOW_STRIPPING_OF_GENE_TREE_OPTION - + ": to allow stripping of gene tree nodes without a matching species" ); - System.out.println( " -" + gsdi.MOST_PARSIMONIOUS_OPTION - + ": use most parimonious duplication model for GSDI: " ); - System.out.println( " assign nodes as speciations which would otherwise be assiged" ); - System.out.println( " as potential duplications due to polytomies in the species tree" ); - System.out.println( " -" + gsdi.GUESS_FORMAT_OF_SPECIES_TREE - + ": to allow species tree in other formats than phyloXML (i.e. Newick, NHX, Nexus)" ); - System.out.println( " -" + gsdi.GSDIR_OPTION - + ": to use GSDIR algorithm instead of GSDI algorithm (re-rooting)" ); - System.out.println(); - System.out.println( "Gene tree:" ); - System.out.println( " in phyloXM format, with taxonomy and sequence data in appropriate fields" ); - System.out.println(); - System.out.println( "Species tree:" ); - System.out.println( " in phyloXML format (unless option -" + gsdi.GUESS_FORMAT_OF_SPECIES_TREE + " is used)" ); - System.out.println(); - System.out.println( "Example: gsdi -" + ALLOW_STRIPPING_OF_GENE_TREE_OPTION - + " gene_tree.xml tree_of_life.xml out.xml" ); - System.out.println(); + private static void writeToRemappedFile( final File out_file, + final SortedSet remapped, + final EasyWriter log_writer ) throws IOException { + final File file = new File( ForesterUtil.removeSuffix( out_file.toString() ) + REMAPPED_SUFFIX ); + final EasyWriter remapped_writer = ForesterUtil.createEasyWriter( file ); + for( final String s : remapped ) { + remapped_writer.println( s ); + } + remapped_writer.close(); + System.out.println( "Wrote remapped gene tree species to : " + file.getCanonicalPath() ); + log_writer.println( "Wrote remapped gene tree species to : " + file.getCanonicalPath() ); } }