phylotastic hackathon at NESCENT 120607
[jalview.git] / forester / java / src / org / forester / application / gene_tree_preprocess.java
1 // $Id:
2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
4 //
5 // Copyright (C) 2008-2012 Christian M. Zmasek
6 // Copyright (C) 2008-2012 Burnham Institute for Medical Research
7 // All rights reserved
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 //
23 // Contact: phylosoft @ gmail . com
24 // WWW: www.phylosoft.org/forester
25
26 package org.forester.application;
27
28 import java.io.File;
29 import java.io.IOException;
30 import java.util.SortedSet;
31
32 import org.forester.archaeopteryx.tools.SequenceDataRetriver;
33 import org.forester.io.parsers.util.ParserUtils;
34 import org.forester.io.writers.PhylogenyWriter;
35 import org.forester.phylogeny.Phylogeny;
36 import org.forester.phylogeny.PhylogenyMethods;
37 import org.forester.phylogeny.factories.ParserBasedPhylogenyFactory;
38 import org.forester.phylogeny.factories.PhylogenyFactory;
39 import org.forester.util.CommandLineArguments;
40 import org.forester.util.ForesterUtil;
41
42 public class gene_tree_preprocess {
43
44     final static private String HELP_OPTION_1 = "help";
45     final static private String HELP_OPTION_2 = "h";
46     final static private String FROM_OPTION   = "f";
47     final static private String TO_OPTION     = "t";
48     final static private String STEP_OPTION   = "s";
49     final static private String WINDOW_OPTION = "w";
50     final static private String PRG_NAME      = "gene_tree_preprocess";
51     final static private String PRG_DESC      = "gene tree preprocessing for SDI analysis";
52     final static private String PRG_VERSION   = "1.00";
53     final static private String PRG_DATE      = "2012.06.07";
54     final static private String E_MAIL        = "phylosoft@gmail.com";
55     final static private String WWW           = "www.phylosoft.org/forester/";
56
57     public static void main( final String[] args ) {
58         try {
59             final CommandLineArguments cla = new CommandLineArguments( args );
60             if ( cla.isOptionSet( HELP_OPTION_1 ) || cla.isOptionSet( HELP_OPTION_2 ) || ( args.length != 1 ) ) {
61                 printHelp();
62                 System.exit( 0 );
63             }
64             final File in = cla.getFile( 0 );
65             Phylogeny phy = null;
66             final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
67             try {
68                 phy = factory.create( in, ParserUtils.createParserDependingOnFileType( in, true ) )[ 0 ];
69             }
70             catch ( final IOException e ) {
71                 ForesterUtil.fatalError( PRG_NAME,
72                                          "failed to read target phylogenies from [" + in + "]: "
73                                                  + e.getLocalizedMessage() );
74             }
75             final File outtree = new File( ForesterUtil.removeSuffix( in.toString() )
76                     + "_preprocessed_gene_tree.phylo.xml" );
77             final File removed_nodes = new File( ForesterUtil.removeSuffix( in.toString() ) + "_removed_nodes.txt" );
78             checkForOutputFileWriteability( outtree );
79             checkForOutputFileWriteability( removed_nodes );
80             if ( phy.getNumberOfExternalNodes() < 2 ) {
81                 ForesterUtil.fatalError( PRG_NAME, "phylogeny has " + phy.getNumberOfExternalNodes()
82                         + " external node(s), aborting" );
83             }
84             final SortedSet<String> not_found = SequenceDataRetriver.obtainSeqInformation( phy );
85             for( final String remove_me : not_found ) {
86                 System.out.println( " not found: " + not_found );
87                 PhylogenyMethods.removeNode( phy.getNode( remove_me ), phy );
88             }
89             if ( phy.getNumberOfExternalNodes() < 2 ) {
90                 ForesterUtil.fatalError( PRG_NAME,
91                                          "after removal of unresolvable external nodes, phylogeny has "
92                                                  + phy.getNumberOfExternalNodes() + " external node(s), aborting" );
93             }
94             try {
95                 final PhylogenyWriter writer = new PhylogenyWriter();
96                 writer.toPhyloXML( phy, 0, outtree );
97             }
98             catch ( final IOException e ) {
99                 ForesterUtil.fatalError( PRG_NAME, "failed to write to [" + outtree + "]: " + e.getLocalizedMessage() );
100             }
101             //  ForesterUtil.programMessage( PRG_NAME, "wrote output to: [" + outfile + "]" );
102             ForesterUtil.programMessage( PRG_NAME, "OK" );
103             System.out.println();
104         }
105         catch ( final Exception e ) {
106             ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
107         }
108     }
109
110     public static void checkForOutputFileWriteability( final File outfile ) {
111         final String error = ForesterUtil.isWritableFile( outfile );
112         if ( !ForesterUtil.isEmpty( error ) ) {
113             ForesterUtil.fatalError( PRG_NAME, error );
114         }
115     }
116
117     private static void printHelp() {
118         ForesterUtil.printProgramInformation( PRG_NAME,
119                                               PRG_DESC,
120                                               PRG_VERSION,
121                                               PRG_DATE,
122                                               E_MAIL,
123                                               WWW,
124                                               ForesterUtil.getForesterLibraryInformation() );
125         System.out.println( "Usage:" );
126         System.out.println();
127         System.out.println( PRG_NAME + " <options> <msa input file>" );
128         System.out.println();
129         System.out.println( " options: " );
130         System.out.println();
131         System.out.println( "   -" + FROM_OPTION + "=<integer>: from (msa column)" );
132         System.out.println( "   -" + TO_OPTION + "=<integer>: to (msa column)" );
133         System.out.println( "    or" );
134         System.out.println( "   -" + WINDOW_OPTION + "=<integer>: window size (msa columns)" );
135         System.out.println( "   -" + STEP_OPTION + "=<integer>: step size (msa columns)" );
136         System.out.println();
137         System.out.println();
138         System.out.println();
139     }
140 }