X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=forester%2Fjava%2Fsrc%2Forg%2Fforester%2Fapplication%2Fgsdi.java;h=eab52df09d22bdf1d1ea05a4ec5df41ee039438a;hb=6005bacbd8aecc0d320b70568a24d321a30bc85c;hp=0f77e5cd52e3e17eb00ad8f6936fe8bfbe73e516;hpb=c933c93e4553a4f5979390d47b7d2763603a60d0;p=jalview.git diff --git a/forester/java/src/org/forester/application/gsdi.java b/forester/java/src/org/forester/application/gsdi.java index 0f77e5c..eab52df 100644 --- a/forester/java/src/org/forester/application/gsdi.java +++ b/forester/java/src/org/forester/application/gsdi.java @@ -48,6 +48,7 @@ import org.forester.phylogeny.PhylogenyNode; import org.forester.phylogeny.factories.ParserBasedPhylogenyFactory; import org.forester.phylogeny.factories.PhylogenyFactory; import org.forester.sdi.GSDI; +import org.forester.sdi.GSDIR; import org.forester.sdi.SDI; import org.forester.sdi.SDI.TaxonomyComparisonBase; import org.forester.sdi.SDIException; @@ -60,21 +61,22 @@ import org.forester.util.ForesterUtil; public final class gsdi { private enum BASE_ALGORITHM { - GSDI, SDI + GSDIR, GSDI, SDI } final static public boolean REPLACE_UNDERSCORES_IN_NH_SPECIES_TREE = true; final static private String ALLOW_STRIPPING_OF_GENE_TREE_OPTION = "g"; final static private String SDISE_OPTION = "b"; + 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 HELP_OPTION_1 = "help"; final static private String HELP_OPTION_2 = "h"; - final static private String DEFAULT_OUTFILE_SUFFIX = "_gsdi_out.xml"; 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 = "120608"; + final static private String PRG_DATE = "120629"; 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"; @@ -100,7 +102,7 @@ public final class gsdi { gsdi.print_help(); System.exit( 0 ); } - else if ( ( args.length < 2 ) || ( cla.getNumberOfNames() < 2 ) || ( cla.getNumberOfNames() > 3 ) ) { + else if ( ( args.length < 2 ) || ( cla.getNumberOfNames() != 3 ) ) { System.out.println(); System.out.println( "Wrong number of arguments." ); System.out.println(); @@ -109,6 +111,7 @@ public final class gsdi { } final List allowed_options = new ArrayList(); allowed_options.add( gsdi.SDISE_OPTION ); + 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 ); @@ -127,18 +130,21 @@ public final class gsdi { BASE_ALGORITHM base_algorithm = BASE_ALGORITHM.GSDI; boolean most_parsimonous_duplication_model = false; boolean allow_stripping_of_gene_tree = false; - if ( cla.isOptionSet( gsdi.SDISE_OPTION ) ) { + if ( cla.isOptionSet( gsdi.GSDIR_OPTION ) ) { + base_algorithm = BASE_ALGORITHM.GSDIR; + } + else if ( cla.isOptionSet( gsdi.SDISE_OPTION ) ) { base_algorithm = BASE_ALGORITHM.SDI; } if ( cla.isOptionSet( gsdi.MOST_PARSIMONIOUS_OPTION ) ) { - if ( base_algorithm != BASE_ALGORITHM.GSDI ) { - ForesterUtil.fatalError( gsdi.PRG_NAME, "Can only use most parsimonious duplication mode with GSDI" ); + if ( base_algorithm == BASE_ALGORITHM.SDI ) { + ForesterUtil.fatalError( gsdi.PRG_NAME, "Cannot use most parsimonious duplication mode with SDI" ); } most_parsimonous_duplication_model = true; } if ( cla.isOptionSet( gsdi.ALLOW_STRIPPING_OF_GENE_TREE_OPTION ) ) { - if ( base_algorithm != BASE_ALGORITHM.GSDI ) { - ForesterUtil.fatalError( gsdi.PRG_NAME, "Can only allow stripping of gene tree with GSDI" ); + if ( base_algorithm == BASE_ALGORITHM.SDI ) { + ForesterUtil.fatalError( gsdi.PRG_NAME, "Cannot allow stripping of gene tree with SDI" ); } allow_stripping_of_gene_tree = true; } @@ -152,12 +158,7 @@ public final class gsdi { try { gene_tree_file = cla.getFile( 0 ); species_tree_file = cla.getFile( 1 ); - if ( cla.getNumberOfNames() == 3 ) { - out_file = cla.getFile( 2 ); - } - else { - out_file = new File( ForesterUtil.removeSuffix( gene_tree_file.toString() ) + DEFAULT_OUTFILE_SUFFIX ); - } + out_file = cla.getFile( 2 ); log_file = new File( ForesterUtil.removeSuffix( out_file.toString() ) + LOGFILE_SUFFIX ); } catch ( final IllegalArgumentException e ) { @@ -186,8 +187,9 @@ public final class gsdi { gene_tree = factory.create( gene_tree_file, new PhyloXmlParser() )[ 0 ]; } catch ( final IOException e ) { - ForesterUtil.fatalError( gsdi.PRG_NAME, - "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(); @@ -210,9 +212,8 @@ public final class gsdi { true ); } catch ( final PhyloXmlDataFormatException e ) { - ForesterUtil.fatalError( gsdi.PRG_NAME, - "Failed to transfer general node name to scientific name, in [" - + species_tree_file + "]: " + e.getMessage() ); + fatalError( "USER ERROR", "Failed to transfer general node name to scientific name, in [" + + species_tree_file + "]: " + e.getMessage(), log_writer ); } break; case CODE: @@ -223,9 +224,8 @@ public final class gsdi { true ); } catch ( final PhyloXmlDataFormatException e ) { - ForesterUtil.fatalError( gsdi.PRG_NAME, - "Failed to transfer general node name to taxonomy code, in [" - + species_tree_file + "]: " + e.getMessage() ); + fatalError( "USER ERROR", "Failed to transfer general node name to taxonomy code, in [" + + species_tree_file + "]: " + e.getMessage(), log_writer ); } break; case ID: @@ -235,32 +235,30 @@ public final class gsdi { true ); } catch ( final PhyloXmlDataFormatException e ) { - ForesterUtil.fatalError( gsdi.PRG_NAME, - "Failed to transfer general node name to taxonomy id, in [" - + species_tree_file + "]: " + e.getMessage() ); + fatalError( "USER ERROR", "Failed to transfer general node name to taxonomy id, in [" + + species_tree_file + "]: " + e.getMessage(), log_writer ); } break; default: - ForesterUtil.fatalError( gsdi.PRG_NAME, "unable to determine comparison base" ); + fatalError( "UNEXPECTED ERROR", "unable to determine comparison base", log_writer ); } } } catch ( final IOException e ) { - ForesterUtil.fatalError( gsdi.PRG_NAME, "Failed to read species tree from [" + species_tree_file + "]: " - + e.getMessage() ); + fatalError( "ERROR", + "Failed to read species tree from [" + species_tree_file + "]: " + e.getMessage(), + log_writer ); } gene_tree.setRooted( true ); species_tree.setRooted( true ); if ( !gene_tree.isCompletelyBinary() ) { - log_writer.println( "User Error: gene tree is not completely binary" ); - log_writer.close(); - ForesterUtil.fatalError( gsdi.PRG_NAME, "gene tree is not completely binary" ); + fatalError( "user error", "gene tree is not completely binary", log_writer ); } - if ( base_algorithm != BASE_ALGORITHM.GSDI ) { + if ( base_algorithm == BASE_ALGORITHM.SDI ) { if ( !species_tree.isCompletelyBinary() ) { - log_writer.println( "User Error: species tree is not completely binary, use GSDI instead" ); - log_writer.close(); - ForesterUtil.fatalError( gsdi.PRG_NAME, "species tree is not completely binary, use GSDI instead" ); + fatalError( "user error", + "species tree is not completely binary, use GSDI or GSDIR instead", + log_writer ); } } log_writer.println( PRG_NAME + " - " + PRG_DESC ); @@ -287,41 +285,48 @@ public final class gsdi { SDI sdi = null; final long start_time = new Date().getTime(); try { - if ( base_algorithm == BASE_ALGORITHM.GSDI ) { + if ( ( base_algorithm == BASE_ALGORITHM.GSDI ) || ( base_algorithm == BASE_ALGORITHM.GSDIR ) ) { + if ( base_algorithm == BASE_ALGORITHM.GSDI ) { + System.out.println( "Algorithm : GSDI" ); + log_writer.println( "Algorithm : GSDI" ); + } + else if ( base_algorithm == BASE_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 ); log_writer.println( "Use most parsimonous duplication model : " + most_parsimonous_duplication_model ); log_writer.println( "Allow stripping of gene tree nodes : " + allow_stripping_of_gene_tree ); log_writer.flush(); - sdi = new GSDI( gene_tree, - species_tree, - most_parsimonous_duplication_model, - allow_stripping_of_gene_tree, - true ); + if ( base_algorithm == BASE_ALGORITHM.GSDI ) { + sdi = new GSDI( gene_tree, + species_tree, + most_parsimonous_duplication_model, + allow_stripping_of_gene_tree, + true ); + } + else if ( base_algorithm == BASE_ALGORITHM.GSDIR ) { + sdi = new GSDIR( gene_tree, species_tree, allow_stripping_of_gene_tree, 1 ); + } } else { System.out.println(); - System.out.println( "Using SDIse algorithm" ); - log_writer.println( "Using SDIse algorithm" ); + System.out.println( "Algorithm : SDI" ); + log_writer.println( "Algorithm : SDI" ); log_writer.flush(); sdi = new SDIse( gene_tree, species_tree ); } } catch ( final SDIException e ) { - log_writer.println( "User Error: " + e.getLocalizedMessage() ); - log_writer.close(); - ForesterUtil.fatalError( PRG_NAME, e.getLocalizedMessage() ); + fatalError( "user error", e.getLocalizedMessage(), log_writer ); } catch ( final IOException e ) { - log_writer.println( "Error: " + e ); - log_writer.close(); - ForesterUtil.fatalError( PRG_NAME, e.toString() ); + fatalError( "error", e.toString(), log_writer ); } catch ( final Exception e ) { - log_writer.println( "Error: " + e ); - log_writer.close(); e.printStackTrace(); - System.exit( -1 ); + fatalError( "unexpected error", e.toString(), log_writer ); } System.out.println( "Running time (excluding I/O) : " + ( new Date().getTime() - start_time ) + "ms" ); @@ -332,9 +337,26 @@ public final class gsdi { System.out.println( "Mapping based on : " + gsdi.getTaxCompBase() ); log_writer.println( "Mapping based on : " + gsdi.getTaxCompBase() ); } + if ( ( base_algorithm == BASE_ALGORITHM.GSDIR ) ) { + final GSDIR gsdir = ( GSDIR ) sdi; + System.out.println( "Mapping based on : " + gsdir.getTaxCompBase() ); + log_writer.println( "Mapping based on : " + gsdir.getTaxCompBase() ); + System.out.println( "Minimal duplications sum : " + gsdir.getMinDuplicationsSum() ); + log_writer.println( "Minimal duplications sum : " + gsdir.getMinDuplicationsSum() ); + System.out.println( "Duplications sum statistics : " + gsdir.getMinDuplicationsSum() ); + log_writer.println( "Duplications sum statistics : " + gsdir.getMinDuplicationsSum() ); + } try { final PhylogenyWriter writer = new PhylogenyWriter(); - writer.toPhyloXML( out_file, gene_tree, 0 ); + if ( base_algorithm == BASE_ALGORITHM.GSDIR ) { + writer.toPhyloXML( out_file, + ( ( GSDIR ) sdi ).getMinDuplicationsSumGeneTrees(), + 0, + ForesterUtil.LINE_SEPARATOR ); + } + else { + writer.toPhyloXML( out_file, gene_tree, 0 ); + } } catch ( final IOException e ) { ForesterUtil.fatalError( PRG_NAME, @@ -347,7 +369,7 @@ public final class gsdi { System.out.println( "Mapping cost : " + sdi.computeMappingCostL() ); log_writer.println( "Mapping cost : " + sdi.computeMappingCostL() ); } - else if ( ( base_algorithm == BASE_ALGORITHM.GSDI ) ) { + else if ( ( base_algorithm == BASE_ALGORITHM.GSDI ) || ( base_algorithm == BASE_ALGORITHM.GSDIR ) ) { final GSDI gsdi = ( GSDI ) sdi; final File species_tree_used_file = new File( ForesterUtil.removeSuffix( out_file.toString() ) + SUFFIX_FOR_SPECIES_TREE_USED ); @@ -363,6 +385,14 @@ public final class gsdi { + species_tree_used_file.getCanonicalPath() ); log_writer.println( "Wrote (stripped) species tree to : " + species_tree_used_file.getCanonicalPath() ); + if ( ( gsdi.getReMappedScientificNamesFromGeneTree() != null ) + && !gsdi.getReMappedScientificNamesFromGeneTree().isEmpty() ) { + System.out.println( "Number of gene tree species remapped : " + + gsdi.getReMappedScientificNamesFromGeneTree().size() ); + log_writer.println( "Number of gene tree species remapped : " + + gsdi.getReMappedScientificNamesFromGeneTree().size() ); + writeToRemappedFile( out_file, gsdi.getReMappedScientificNamesFromGeneTree(), log_writer ); + } } System.out.println( "Number of external nodes in gene tree : " + gene_tree.getNumberOfExternalNodes() ); log_writer.println( "Number of external nodes in gene tree : " + gene_tree.getNumberOfExternalNodes() ); @@ -400,8 +430,6 @@ public final class gsdi { printMappedNodesToLog( log_writer, gsdi ); log_writer.println(); printStrippedGeneTreeNodesToLog( log_writer, gsdi ); - log_writer.println(); - printStrippedSpeciesTreeNodesToLog( log_writer, gsdi ); } System.out.println(); System.out.println( "Wrote log to : " + log_file.getCanonicalPath() ); @@ -409,6 +437,19 @@ 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 GSDI gsdi ) throws IOException { final SortedSet ss = new TreeSet(); for( final PhylogenyNode n : gsdi.getMappedExternalSpeciesTreeNodes() ) { @@ -420,6 +461,20 @@ public final class gsdi { } } + private static void fatalError( final String type, final String msg, final EasyWriter log_writer ) { + try { + log_writer.flush(); + log_writer.println(); + log_writer.print( type.toUpperCase() + ": " ); + log_writer.println( msg ); + log_writer.close(); + } + catch ( final IOException e ) { + e.printStackTrace(); + } + ForesterUtil.fatalError( gsdi.PRG_NAME, msg ); + } + private static void printStrippedGeneTreeNodesToLog( final EasyWriter log_writer, final GSDI gsdi ) throws IOException { final SortedMap sm = new TreeMap(); @@ -444,21 +499,9 @@ public final class gsdi { } } - private static void printStrippedSpeciesTreeNodesToLog( final EasyWriter log_writer, final GSDI gsdi ) - throws IOException { - final SortedSet ss = new TreeSet(); - for( final PhylogenyNode n : gsdi.getStrippedSpeciesTreeNodes() ) { - ss.add( n.toString() ); - } - log_writer.println( "The following " + ss.size() + " nodes were stripped from the species tree: " ); - for( final String n : ss ) { - log_writer.println( " " + n ); - } - } - private static void print_help() { System.out.println( "Usage: " + gsdi.PRG_NAME - + " [-options] [outfile]" ); + + " [-options] " ); System.out.println(); System.out.println( "Options:" ); System.out.println( " -" + gsdi.ALLOW_STRIPPING_OF_GENE_TREE_OPTION @@ -466,11 +509,13 @@ public final class gsdi { 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 dueof polytomies in the species tree" ); + 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.SDISE_OPTION + ": to use SDIse algorithm instead of GSDI algorithm (for binary species trees)" ); + 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" ); @@ -479,7 +524,7 @@ public final class gsdi { 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" ); + + " gene_tree.xml tree_of_life.xml out.xml" ); System.out.println(); } }