in progress
[jalview.git] / forester / java / src / org / forester / application / strip.java
1 // $Id:
2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
4 //
5 // Copyright (C) 2008-2009 Christian M. Zmasek
6 // Copyright (C) 2008-2009 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
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;
38
39 public class strip {
40
41     public static void main( final String args[] ) {
42         if ( args.length < 4 ) {
43             System.out.println( "\nstrip: Wrong number of arguments.\n" );
44             System.out
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" );
50             System.exit( -1 );
51         }
52         final File infile = new File( args[ 0 ] );
53         final File outfile = new File( args[ 1 ] );
54         final String options = args[ 2 ];
55         Phylogeny p = null;
56         try {
57             final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
58             final PhylogenyParser pp = ParserUtils.createParserDependingOnFileType( infile, true );
59             p = factory.create( infile, pp )[ 0 ];
60         }
61         catch ( final Exception e ) {
62             System.out.println( "\nCould not read \"" + infile + "\" [" + e.getMessage() + "]\n" );
63             System.exit( -1 );
64         }
65         boolean keep = false;
66         boolean from_p0 = false;
67         if ( options.trim().toLowerCase().equals( "-k" ) ) {
68             keep = true;
69         }
70         else if ( options.trim().toLowerCase().equals( "-kp" ) ) {
71             keep = true;
72             from_p0 = true;
73         }
74         else if ( options.trim().toLowerCase().equals( "-rp" ) ) {
75             from_p0 = true;
76         }
77         else if ( !options.trim().toLowerCase().equals( "-r" ) ) {
78             System.out.println( "\nUnknown option \"" + options + "\"\n" );
79             System.exit( -1 );
80         }
81         String[] names = null;
82         if ( from_p0 ) {
83             names = strip.readInNamesFromPhylogeny( args[ 3 ] );
84         }
85         else {
86             names = new String[ args.length - 3 ];
87             for( int i = 0; i < args.length - 3; ++i ) {
88                 names[ i ] = args[ i + 3 ];
89             }
90         }
91         if ( keep ) {
92             PhylogenyMethods.deleteExternalNodesPositiveSelection( names, p );
93         }
94         else {
95             PhylogenyMethods.deleteExternalNodesNegativeSelection( names, p );
96         }
97         try {
98             final PhylogenyWriter w = new PhylogenyWriter();
99             w.toPhyloXML( outfile, p, 1 );
100         }
101         catch ( final IOException e ) {
102             System.out.println( "\nFailure to write output [" + e.getMessage() + "]\n" );
103             System.exit( -1 );
104         }
105     }
106
107     private static String[] readInNamesFromPhylogeny( final String file ) {
108         Phylogeny p0 = null;
109         try {
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 ];
114         }
115         catch ( final Exception e ) {
116             System.out.println( "\nCould not read \"" + file + "\" [" + e.getMessage() + "]\n" );
117             System.exit( -1 );
118         }
119         return p0.getAllExternalNodeNames();
120     }
121 }