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.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 );
86             for( final String remove_me : not_found ) {
87                 //PhylogenyMethods.removeNode( phy.getNode( remove_me ), phy );
88                 phy.deleteSubtree( phy.getNode( remove_me ), true );
89             }
90             if ( phy.getNumberOfExternalNodes() < 2 ) {
91                 ForesterUtil.fatalError( PRG_NAME,
92                                          "after removal of unresolvable external nodes, phylogeny has "
93                                                  + phy.getNumberOfExternalNodes() + " external node(s), aborting" );
94             }
95             try {
96                 final PhylogenyWriter writer = new PhylogenyWriter();
97                 writer.toPhyloXML( phy, 0, outtree );
98             }
99             catch ( final IOException e ) {
100                 ForesterUtil.fatalError( PRG_NAME, "failed to write to [" + outtree + "]: " + e.getLocalizedMessage() );
101             }
102             ForesterUtil.programMessage( PRG_NAME, "wrote output phylogeny to: " + outtree );
103             final SortedSet<String> species_set = new TreeSet<String>();
104             for( final PhylogenyNodeIterator iter = phy.iteratorExternalForward(); iter.hasNext(); ) {
105                 final PhylogenyNode node = iter.next();
106                 if ( node.getNodeData().isHasTaxonomy() ) {
107                     final String sn = node.getNodeData().getTaxonomy().getScientificName();
108                     if ( !ForesterUtil.isEmpty( sn ) ) {
109                         species_set.add( sn );
110                     }
111                 }
112             }
113             try {
114                 final BufferedWriter out = new BufferedWriter( new FileWriter( present_species ) );
115                 for( final String species : species_set ) {
116                     out.write( species );
117                     out.newLine();
118                 }
119                 out.close();
120             }
121             catch ( final IOException e ) {
122                 ForesterUtil.fatalError( PRG_NAME,
123                                          "failed to write to [" + present_species + "]: " + e.getLocalizedMessage() );
124             }
125             ForesterUtil.programMessage( PRG_NAME, "wrote present species to: " + present_species );
126             try {
127                 final BufferedWriter out = new BufferedWriter( new FileWriter( removed_nodes ) );
128                 for( final String remove_me : not_found ) {
129                     out.write( remove_me );
130                     out.newLine();
131                 }
132                 out.close();
133             }
134             catch ( final IOException e ) {
135                 ForesterUtil.fatalError( PRG_NAME,
136                                          "failed to write to [" + removed_nodes + "]: " + e.getLocalizedMessage() );
137             }
138             ForesterUtil.programMessage( PRG_NAME, "wrote removed external nodes labels to: " + removed_nodes );
139             ForesterUtil.programMessage( PRG_NAME, "OK" );
140         }
141         catch ( final Exception e ) {
142             ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
143         }
144     }
145
146     public static void checkForOutputFileWriteability( final File outfile ) {
147         final String error = ForesterUtil.isWritableFile( outfile );
148         if ( !ForesterUtil.isEmpty( error ) ) {
149             ForesterUtil.fatalError( PRG_NAME, error );
150         }
151     }
152
153     private static void printHelp() {
154         ForesterUtil.printProgramInformation( PRG_NAME,
155                                               PRG_DESC,
156                                               PRG_VERSION,
157                                               PRG_DATE,
158                                               E_MAIL,
159                                               WWW,
160                                               ForesterUtil.getForesterLibraryInformation() );
161         System.out.print( "Usage: " );
162         System.out.println( PRG_NAME + " <input phylogeny file>" );
163         System.out.println();
164     }
165 }