3 // FORESTER -- software libraries and applications
4 // for evolutionary biology research and applications.
6 // Copyright (C) 2008-2009 Christian M. Zmasek
7 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
10 // This library is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU Lesser General Public
12 // License as published by the Free Software Foundation; either
13 // version 2.1 of the License, or (at your option) any later version.
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 // Contact: phylosoft @ gmail . com
25 // WWW: www.phylosoft.org
27 package org.forester.application;
30 import java.io.IOException;
31 import java.util.ArrayList;
32 import java.util.List;
34 import org.forester.evoinference.matrix.character.CharacterStateMatrix;
35 import org.forester.evoinference.matrix.character.CharacterStateMatrix.BinaryStates;
36 import org.forester.evoinference.matrix.character.CharacterStateMatrix.Format;
37 import org.forester.io.parsers.nexus.NexusCharactersParser;
38 import org.forester.io.parsers.nexus.NexusPhylogeniesParser;
39 import org.forester.io.parsers.nexus.PaupLogParser;
40 import org.forester.phylogeny.Phylogeny;
41 import org.forester.phylogeny.factories.ParserBasedPhylogenyFactory;
42 import org.forester.phylogeny.factories.PhylogenyFactory;
43 import org.forester.surfacing.DomainParsimonyCalculator;
44 import org.forester.surfacing.SurfacingUtil;
45 import org.forester.util.CommandLineArguments;
46 import org.forester.util.ForesterUtil;
48 public class surf_paup {
50 final static private String PRG_VERSION = "0.90";
51 final static private String PRG_DATE = "2008.03.28";
52 final static private String E_MAIL = "czmasek@burnham.org";
53 final static private String WWW = "www.phylosoft.org/forester/applications/surfacing";
54 final static private String HELP_OPTION_1 = "help";
55 final static private String HELP_OPTION_2 = "h";
56 private static final String PRG_NAME = "surf_paup";
58 public static void main( final String args[] ) {
59 ForesterUtil.printProgramInformation( PRG_NAME, PRG_VERSION, PRG_DATE, E_MAIL, WWW );
60 final List<String> allowed_options = new ArrayList<String>();
61 allowed_options.add( HELP_OPTION_1 );
62 allowed_options.add( HELP_OPTION_2 );
63 if ( ( args.length < 2 ) ) {
67 CommandLineArguments cla = null;
69 cla = new CommandLineArguments( args );
71 catch ( final Exception e ) {
72 ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
74 if ( cla.isOptionSet( HELP_OPTION_1 ) || cla.isOptionSet( HELP_OPTION_2 ) ) {
78 if ( cla.getNumberOfNames() != 3 ) {
82 final File surfacing_nexus_outfile = cla.getFile( 0 );
83 final File paup_log_file = cla.getFile( 1 );
84 final String outfile_name = cla.getFile( 2 ).toString();
85 final NexusCharactersParser nex_char_parser = new NexusCharactersParser();
87 nex_char_parser.setSource( surfacing_nexus_outfile );
88 nex_char_parser.parse();
90 catch ( final IOException e ) {
91 ForesterUtil.fatalError( PRG_NAME, "problem with parsing character labels from ["
92 + surfacing_nexus_outfile + "]: " + e.getMessage() );
95 final String[] labels = nex_char_parser.getCharStateLabels();
96 ForesterUtil.programMessage( PRG_NAME, "read in " + labels.length + " character labels" );
97 final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
98 final NexusPhylogeniesParser phylogeny_parser = new NexusPhylogeniesParser();
99 Phylogeny[] phylogenies = null;
101 phylogenies = factory.create( surfacing_nexus_outfile, phylogeny_parser );
103 catch ( final IOException e ) {
104 ForesterUtil.fatalError( PRG_NAME,
105 "problem with parsing phylogeny [" + surfacing_nexus_outfile + "]: "
109 if ( phylogenies.length != 1 ) {
110 ForesterUtil.fatalError( PRG_NAME, "failed to parse one phylogeny from [" + surfacing_nexus_outfile
111 + "], got " + phylogenies.length + " instead" );
113 final Phylogeny phylogeny = phylogenies[ 0 ];
114 if ( !phylogeny.isRooted() ) {
115 ForesterUtil.fatalError( PRG_NAME, "phylogeny from [" + surfacing_nexus_outfile + "] is not rooted" );
117 ForesterUtil.postOrderRelabelInternalNodes( phylogeny, phylogeny.getNumberOfExternalNodes() + 1 );
118 CharacterStateMatrix<BinaryStates> matrix = null;
119 final PaupLogParser paup_log_parser = new PaupLogParser();
121 paup_log_parser.setSource( paup_log_file );
122 matrix = paup_log_parser.parse();
124 catch ( final IOException e ) {
125 ForesterUtil.fatalError( PRG_NAME,
126 "failed to parse matrix from [" + paup_log_file + "]: " + e.getMessage() );
128 ForesterUtil.programMessage( PRG_NAME,
129 "read in character state matrix of size " + matrix.getNumberOfIdentifiers() + "x"
130 + matrix.getNumberOfCharacters() );
131 final DomainParsimonyCalculator domain_parsimony = DomainParsimonyCalculator.createInstance( phylogeny );
132 domain_parsimony.executeOnGivenBinaryStatesMatrix( matrix, labels );
133 final String sep = ForesterUtil.LINE_SEPARATOR + "###################" + ForesterUtil.LINE_SEPARATOR;
134 SurfacingUtil.writeMatrixToFile( domain_parsimony.getGainLossMatrix(),
135 outfile_name + "_paup_gl",
137 SurfacingUtil.writeMatrixToFile( domain_parsimony.getGainLossCountsMatrix(),
138 outfile_name + "_paup_glc",
140 SurfacingUtil.writeBinaryStatesMatrixAsListToFile( domain_parsimony.getGainLossMatrix(),
141 CharacterStateMatrix.GainLossStates.GAIN,
142 outfile_name + "_paup_gains",
144 ForesterUtil.LINE_SEPARATOR,
146 SurfacingUtil.writeBinaryStatesMatrixAsListToFile( domain_parsimony.getGainLossMatrix(),
147 CharacterStateMatrix.GainLossStates.LOSS,
148 outfile_name + "_paup_losses",
150 ForesterUtil.LINE_SEPARATOR,
152 SurfacingUtil.writeBinaryStatesMatrixAsListToFile( domain_parsimony.getGainLossMatrix(), null, outfile_name
153 + "_paup_present", sep, ForesterUtil.LINE_SEPARATOR, null );
154 final String date_time = ForesterUtil.getCurrentDateTime();
155 SurfacingUtil.preparePhylogeny( phylogeny, domain_parsimony, date_time, "parsimony (paup)", "paup_"
156 + outfile_name, "" );
157 SurfacingUtil.writePhylogenyToFile( phylogeny, outfile_name + "_paup.xml" );
158 ForesterUtil.programMessage( PRG_NAME, "OK" );
161 private static void printHelp() {
162 System.out.println();
163 System.out.println( "Usage:" );
164 System.out.println();
166 .println( "% java -cp forester.jar org.forester.applications."
168 + " <surfacing nexus outfile with character labels and tree> <paup log file with reconstructed states matrix> <outfile name base>" );
169 System.out.println();