2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
5 // Copyright (C) 2008-2012 Christian M. Zmasek
6 // Copyright (C) 2008-2012 Burnham Institute for Medical Research
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.
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.
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
23 // Contact: phylosoft @ gmail . com
24 // WWW: www.phylosoft.org/forester
26 package org.forester.application;
28 import java.io.BufferedWriter;
30 import java.io.FileWriter;
31 import java.io.IOException;
32 import java.util.SortedSet;
33 import java.util.TreeSet;
35 import org.forester.io.parsers.util.ParserUtils;
36 import org.forester.io.writers.PhylogenyWriter;
37 import org.forester.phylogeny.Phylogeny;
38 import org.forester.phylogeny.PhylogenyNode;
39 import org.forester.phylogeny.factories.ParserBasedPhylogenyFactory;
40 import org.forester.phylogeny.factories.PhylogenyFactory;
41 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
42 import org.forester.util.CommandLineArguments;
43 import org.forester.util.ForesterUtil;
44 import org.forester.ws.seqdb.SequenceDbWsTools;
46 public class gene_tree_preprocess {
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 private final static int DEFAULT_LINES_TO_RETURN = 50;
58 public static void main( final String[] args ) {
60 final CommandLineArguments cla = new CommandLineArguments( args );
61 if ( cla.isOptionSet( HELP_OPTION_1 ) || cla.isOptionSet( HELP_OPTION_2 ) || ( args.length != 1 ) ) {
65 final File in = cla.getFile( 0 );
67 final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
69 phy = factory.create( in, ParserUtils.createParserDependingOnFileType( in, true ) )[ 0 ];
71 catch ( final IOException e ) {
72 ForesterUtil.fatalError( PRG_NAME,
73 "failed to read phylogeny from [" + in + "]: " + e.getLocalizedMessage() );
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 final File present_species = new File( ForesterUtil.removeSuffix( in.toString() ) + "_species_present.txt" );
79 checkForOutputFileWriteability( outtree );
80 checkForOutputFileWriteability( removed_nodes );
81 checkForOutputFileWriteability( present_species );
82 if ( phy.getNumberOfExternalNodes() < 2 ) {
83 ForesterUtil.fatalError( PRG_NAME, "phylogeny has " + phy.getNumberOfExternalNodes()
84 + " external node(s), aborting" );
86 final SortedSet<String> not_found = SequenceDbWsTools.obtainSeqInformation( phy,
89 DEFAULT_LINES_TO_RETURN );
90 for( final String remove_me : not_found ) {
91 phy.deleteSubtree( phy.getNode( remove_me ), true );
93 phy.clearHashIdToNodeMap();
94 phy.externalNodesHaveChanged();
95 if ( phy.getNumberOfExternalNodes() < 2 ) {
96 ForesterUtil.fatalError( PRG_NAME,
97 "after removal of unresolvable external nodes, phylogeny has "
98 + phy.getNumberOfExternalNodes() + " external node(s), aborting" );
101 final PhylogenyWriter writer = new PhylogenyWriter();
102 writer.toPhyloXML( phy, 0, outtree );
104 catch ( final IOException e ) {
105 ForesterUtil.fatalError( PRG_NAME, "failed to write to [" + outtree + "]: " + e.getLocalizedMessage() );
107 ForesterUtil.programMessage( PRG_NAME, "wrote output phylogeny to: " + outtree );
108 final SortedSet<String> species_set = new TreeSet<String>();
109 for( final PhylogenyNodeIterator iter = phy.iteratorExternalForward(); iter.hasNext(); ) {
110 final PhylogenyNode node = iter.next();
111 if ( node.getNodeData().isHasTaxonomy() ) {
112 final String sn = node.getNodeData().getTaxonomy().getScientificName();
113 if ( !ForesterUtil.isEmpty( sn ) ) {
114 species_set.add( sn );
119 final BufferedWriter out = new BufferedWriter( new FileWriter( present_species ) );
120 for( final String species : species_set ) {
121 out.write( species );
126 catch ( final IOException e ) {
127 ForesterUtil.fatalError( PRG_NAME,
128 "failed to write to [" + present_species + "]: " + e.getLocalizedMessage() );
130 ForesterUtil.programMessage( PRG_NAME, "wrote present species to: " + present_species );
132 final BufferedWriter out = new BufferedWriter( new FileWriter( removed_nodes ) );
133 for( final String remove_me : not_found ) {
134 out.write( remove_me );
139 catch ( final IOException e ) {
140 ForesterUtil.fatalError( PRG_NAME,
141 "failed to write to [" + removed_nodes + "]: " + e.getLocalizedMessage() );
143 ForesterUtil.programMessage( PRG_NAME, "wrote removed external nodes labels to: " + removed_nodes );
144 ForesterUtil.programMessage( PRG_NAME, "OK" );
146 catch ( final Exception e ) {
147 ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
151 private static void checkForOutputFileWriteability( final File outfile ) {
152 final String error = ForesterUtil.isWritableFile( outfile );
153 if ( !ForesterUtil.isEmpty( error ) ) {
154 ForesterUtil.fatalError( PRG_NAME, error );
158 private static void printHelp() {
159 ForesterUtil.printProgramInformation( PRG_NAME,
165 ForesterUtil.getForesterLibraryInformation() );
166 System.out.print( "Usage: " );
167 System.out.println( PRG_NAME + " <input phylogeny file>" );
168 System.out.println();