in progress
[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.BufferedWriter;
29 import java.io.File;
30 import java.io.FileWriter;
31 import java.io.IOException;
32 import java.util.SortedSet;
33 import java.util.TreeSet;
34
35 import org.forester.archaeopteryx.tools.SequenceDataRetriver;
36 import org.forester.io.parsers.util.ParserUtils;
37 import org.forester.io.writers.PhylogenyWriter;
38 import org.forester.phylogeny.Phylogeny;
39 import org.forester.phylogeny.PhylogenyNode;
40 import org.forester.phylogeny.factories.ParserBasedPhylogenyFactory;
41 import org.forester.phylogeny.factories.PhylogenyFactory;
42 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
43 import org.forester.util.CommandLineArguments;
44 import org.forester.util.ForesterUtil;
45
46 public class gene_tree_preprocess {
47
48     final static private String HELP_OPTION_1 = "help";
49     final static private String HELP_OPTION_2 = "h";
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.01";
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 phylogeny from [" + in + "]: " + e.getLocalizedMessage() );
73             }
74             final File outtree = new File( ForesterUtil.removeSuffix( in.toString() )
75                     + "_preprocessed_gene_tree.phylo.xml" );
76             final File removed_nodes = new File( ForesterUtil.removeSuffix( in.toString() ) + "_removed_nodes.txt" );
77             final File present_species = new File( ForesterUtil.removeSuffix( in.toString() ) + "_species_present.txt" );
78             checkForOutputFileWriteability( outtree );
79             checkForOutputFileWriteability( removed_nodes );
80             checkForOutputFileWriteability( present_species );
81             if ( phy.getNumberOfExternalNodes() < 2 ) {
82                 ForesterUtil.fatalError( PRG_NAME, "phylogeny has " + phy.getNumberOfExternalNodes()
83                         + " external node(s), aborting" );
84             }
85             final SortedSet<String> not_found = SequenceDataRetriver.obtainSeqInformation( phy, true, false );
86             for( final String remove_me : not_found ) {
87                 phy.deleteSubtree( phy.getNode( remove_me ), true );
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 phylogeny to: " + outtree );
102             final SortedSet<String> species_set = new TreeSet<String>();
103             for( final PhylogenyNodeIterator iter = phy.iteratorExternalForward(); iter.hasNext(); ) {
104                 final PhylogenyNode node = iter.next();
105                 if ( node.getNodeData().isHasTaxonomy() ) {
106                     final String sn = node.getNodeData().getTaxonomy().getScientificName();
107                     if ( !ForesterUtil.isEmpty( sn ) ) {
108                         species_set.add( sn );
109                     }
110                 }
111             }
112             try {
113                 final BufferedWriter out = new BufferedWriter( new FileWriter( present_species ) );
114                 for( final String species : species_set ) {
115                     out.write( species );
116                     out.newLine();
117                 }
118                 out.close();
119             }
120             catch ( final IOException e ) {
121                 ForesterUtil.fatalError( PRG_NAME,
122                                          "failed to write to [" + present_species + "]: " + e.getLocalizedMessage() );
123             }
124             ForesterUtil.programMessage( PRG_NAME, "wrote present species to: " + present_species );
125             try {
126                 final BufferedWriter out = new BufferedWriter( new FileWriter( removed_nodes ) );
127                 for( final String remove_me : not_found ) {
128                     out.write( remove_me );
129                     out.newLine();
130                 }
131                 out.close();
132             }
133             catch ( final IOException e ) {
134                 ForesterUtil.fatalError( PRG_NAME,
135                                          "failed to write to [" + removed_nodes + "]: " + e.getLocalizedMessage() );
136             }
137             ForesterUtil.programMessage( PRG_NAME, "wrote removed external nodes labels to: " + removed_nodes );
138             ForesterUtil.programMessage( PRG_NAME, "OK" );
139         }
140         catch ( final Exception e ) {
141             ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
142         }
143     }
144
145     public static void checkForOutputFileWriteability( final File outfile ) {
146         final String error = ForesterUtil.isWritableFile( outfile );
147         if ( !ForesterUtil.isEmpty( error ) ) {
148             ForesterUtil.fatalError( PRG_NAME, error );
149         }
150     }
151
152     private static void printHelp() {
153         ForesterUtil.printProgramInformation( PRG_NAME,
154                                               PRG_DESC,
155                                               PRG_VERSION,
156                                               PRG_DATE,
157                                               E_MAIL,
158                                               WWW,
159                                               ForesterUtil.getForesterLibraryInformation() );
160         System.out.print( "Usage: " );
161         System.out.println( PRG_NAME + " <input phylogeny file>" );
162         System.out.println();
163     }
164 }