a60d22f8c1beef0f7ce66151252ca82871877cc5
[jalview.git] / forester / java / src / org / forester / application / surf_paup.java
1 // $Id:
2 //
3 // FORESTER -- software libraries and applications
4 // for evolutionary biology research and applications.
5 //
6 // Copyright (C) 2008-2009 Christian M. Zmasek
7 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
8 // All rights reserved
9 //
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.
14 //
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.
19 //
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
23 //
24 // Contact: phylosoft @ gmail . com
25 // WWW: www.phylosoft.org
26
27 package org.forester.application;
28
29 import java.io.File;
30 import java.io.IOException;
31 import java.util.ArrayList;
32 import java.util.List;
33
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;
47
48 public class surf_paup {
49
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";
57
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 ) ) {
64             printHelp();
65             System.exit( -1 );
66         }
67         CommandLineArguments cla = null;
68         try {
69             cla = new CommandLineArguments( args );
70         }
71         catch ( final Exception e ) {
72             ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
73         }
74         if ( cla.isOptionSet( HELP_OPTION_1 ) || cla.isOptionSet( HELP_OPTION_2 ) ) {
75             printHelp();
76             System.exit( 0 );
77         }
78         if ( cla.getNumberOfNames() != 3 ) {
79             printHelp();
80             System.exit( -1 );
81         }
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();
86         try {
87             nex_char_parser.setSource( surfacing_nexus_outfile );
88             nex_char_parser.parse();
89         }
90         catch ( final IOException e ) {
91             ForesterUtil.fatalError( PRG_NAME, "problem with parsing character labels from  ["
92                     + surfacing_nexus_outfile + "]: " + e.getMessage() );
93             e.printStackTrace();
94         }
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;
100         try {
101             phylogenies = factory.create( surfacing_nexus_outfile, phylogeny_parser );
102         }
103         catch ( final IOException e ) {
104             ForesterUtil.fatalError( PRG_NAME,
105                                      "problem with parsing phylogeny [" + surfacing_nexus_outfile + "]: "
106                                              + e.getMessage() );
107             e.printStackTrace();
108         }
109         if ( phylogenies.length != 1 ) {
110             ForesterUtil.fatalError( PRG_NAME, "failed to parse one phylogeny from [" + surfacing_nexus_outfile
111                     + "], got " + phylogenies.length + " instead" );
112         }
113         final Phylogeny phylogeny = phylogenies[ 0 ];
114         if ( !phylogeny.isRooted() ) {
115             ForesterUtil.fatalError( PRG_NAME, "phylogeny from [" + surfacing_nexus_outfile + "] is not rooted" );
116         }
117         ForesterUtil.postOrderRelabelInternalNodes( phylogeny, phylogeny.getNumberOfExternalNodes() + 1 );
118         CharacterStateMatrix<BinaryStates> matrix = null;
119         final PaupLogParser paup_log_parser = new PaupLogParser();
120         try {
121             paup_log_parser.setSource( paup_log_file );
122             matrix = paup_log_parser.parse();
123         }
124         catch ( final IOException e ) {
125             ForesterUtil.fatalError( PRG_NAME,
126                                      "failed to parse matrix from  [" + paup_log_file + "]: " + e.getMessage() );
127         }
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",
136                                          Format.FORESTER );
137         SurfacingUtil.writeMatrixToFile( domain_parsimony.getGainLossCountsMatrix(),
138                                          outfile_name + "_paup_glc",
139                                          Format.FORESTER );
140         SurfacingUtil.writeBinaryStatesMatrixAsListToFile( domain_parsimony.getGainLossMatrix(),
141                                                            CharacterStateMatrix.GainLossStates.GAIN,
142                                                            outfile_name + "_paup_gains",
143                                                            sep,
144                                                            ForesterUtil.LINE_SEPARATOR,
145                                                            null );
146         SurfacingUtil.writeBinaryStatesMatrixAsListToFile( domain_parsimony.getGainLossMatrix(),
147                                                            CharacterStateMatrix.GainLossStates.LOSS,
148                                                            outfile_name + "_paup_losses",
149                                                            sep,
150                                                            ForesterUtil.LINE_SEPARATOR,
151                                                            null );
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" );
159     }
160
161     private static void printHelp() {
162         System.out.println();
163         System.out.println( "Usage:" );
164         System.out.println();
165         System.out
166                 .println( "% java  -cp forester.jar org.forester.applications."
167                         + PRG_NAME
168                         + " <surfacing nexus outfile with character labels and tree> <paup log file with reconstructed states matrix> <outfile name base>" );
169         System.out.println();
170     }
171 }