2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
5 // Copyright (C) 2008-2009 Christian M. Zmasek
6 // Copyright (C) 2008-2009 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;
29 import java.io.IOException;
31 import org.forester.io.parsers.PhylogenyParser;
32 import org.forester.io.parsers.util.ParserUtils;
33 import org.forester.io.writers.PhylogenyWriter;
34 import org.forester.phylogeny.Phylogeny;
35 import org.forester.phylogeny.PhylogenyMethods;
36 import org.forester.phylogeny.factories.ParserBasedPhylogenyFactory;
37 import org.forester.phylogeny.factories.PhylogenyFactory;
41 public static void main( final String args[] ) {
42 if ( args.length < 4 ) {
43 System.out.println( "\nstrip: Wrong number of arguments.\n" );
45 .println( "Usage: \"strip <infile> <outfile> <options> [name1] [name2] ... OR [phylogenyfile]\"\n" );
46 System.out.println( " Options: -k to keep listed nodes" );
47 System.out.println( " -r to remove listed nodes" );
48 System.out.println( " -kp to keep nodes found in [phylogenyfile]" );
49 System.out.println( " -rp to remove nodes found in [phylogenyfile]\n" );
52 final File infile = new File( args[ 0 ] );
53 final File outfile = new File( args[ 1 ] );
54 final String options = args[ 2 ];
57 final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
58 final PhylogenyParser pp = ParserUtils.createParserDependingOnFileType( infile, true );
59 p = factory.create( infile, pp )[ 0 ];
61 catch ( final Exception e ) {
62 System.out.println( "\nCould not read \"" + infile + "\" [" + e.getMessage() + "]\n" );
66 boolean from_p0 = false;
67 if ( options.trim().toLowerCase().equals( "-k" ) ) {
70 else if ( options.trim().toLowerCase().equals( "-kp" ) ) {
74 else if ( options.trim().toLowerCase().equals( "-rp" ) ) {
77 else if ( !options.trim().toLowerCase().equals( "-r" ) ) {
78 System.out.println( "\nUnknown option \"" + options + "\"\n" );
81 String[] names = null;
83 names = strip.readInNamesFromPhylogeny( args[ 3 ] );
86 names = new String[ args.length - 3 ];
87 for( int i = 0; i < ( args.length - 3 ); ++i ) {
88 names[ i ] = args[ i + 3 ];
92 PhylogenyMethods.deleteExternalNodesPositiveSelection( names, p );
95 PhylogenyMethods.deleteExternalNodesNegativeSelection( names, p );
98 final PhylogenyWriter w = new PhylogenyWriter();
99 w.toPhyloXML( outfile, p, 1 );
101 catch ( final IOException e ) {
102 System.out.println( "\nFailure to write output [" + e.getMessage() + "]\n" );
107 private static String[] readInNamesFromPhylogeny( final String file ) {
110 final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
111 final File f = new File( file );
112 final PhylogenyParser pp = ParserUtils.createParserDependingOnFileType( f, true );
113 p0 = factory.create( f, pp )[ 0 ];
115 catch ( final Exception e ) {
116 System.out.println( "\nCould not read \"" + file + "\" [" + e.getMessage() + "]\n" );
119 return p0.getAllExternalNodeNames();