JAL-2805 made needed color set methods public
[jalview.git] / forester / java / src / org / forester / application / phylostrip.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: https://sites.google.com/site/cmzmasek/home/software/forester
25
26 package org.forester.application;
27
28 import java.io.File;
29 import java.io.IOException;
30 import java.util.ArrayList;
31 import java.util.List;
32
33 import org.forester.io.parsers.PhylogenyParser;
34 import org.forester.io.parsers.phyloxml.PhyloXmlDataFormatException;
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.PhylogenyMethods;
39 import org.forester.phylogeny.data.Taxonomy;
40 import org.forester.phylogeny.factories.ParserBasedPhylogenyFactory;
41 import org.forester.phylogeny.factories.PhylogenyFactory;
42
43 public class phylostrip {
44
45     public static void main( final String args[] ) {
46         if ( args.length < 4 ) {
47             System.out.println( "\nstrip: Wrong number of arguments.\n" );
48             System.out
49             .println( "Usage: \"phylostrip <in-tree> <out-tree> <options> [name1] [name2] ... OR [ref-tree]\"\n" );
50             System.out.println( " Options: -knn to keep listed nodes" );
51             System.out.println( "          -rnn to remove listed nodes" );
52             System.out.println( "          -knnp to keep nodes found in [ref-tree]" );
53             System.out.println( "          -rnnp to remove nodes found in [ref-tree]" );
54             System.out.println( "          -ktc to keep only nodes from listed taxonomy codes\n" );
55             System.exit( -1 );
56         }
57         final File infile = new File( args[ 0 ] );
58         final File outfile = new File( args[ 1 ] );
59         final String options = args[ 2 ];
60         Phylogeny p = null;
61         try {
62             final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
63             final PhylogenyParser pp = ParserUtils.createParserDependingOnFileType( infile, true );
64             p = factory.create( infile, pp )[ 0 ];
65         }
66         catch ( final Exception e ) {
67             System.out.println( "\nCould not read \"" + infile + "\" [" + e.getMessage() + "]\n" );
68             System.exit( -1 );
69         }
70         boolean keep = false;
71         boolean from_p0 = false;
72         boolean ktc = false;
73         if ( options.trim().toLowerCase().equals( "-knn" ) ) {
74             keep = true;
75         }
76         else if ( options.trim().toLowerCase().equals( "-knnp" ) ) {
77             keep = true;
78             from_p0 = true;
79         }
80         else if ( options.trim().toLowerCase().equals( "-rnnp" ) ) {
81             from_p0 = true;
82         }
83         else if ( options.trim().toLowerCase().equals( "-ktc" ) ) {
84             ktc = true;
85         }
86         else if ( !options.trim().toLowerCase().equals( "-rnn" ) ) {
87             System.out.println( "\nUnknown option \"" + options + "\"\n" );
88             System.exit( -1 );
89         }
90         String[] names = null;
91         if ( from_p0 ) {
92             names = phylostrip.readInNamesFromPhylogeny( args[ 3 ] );
93         }
94         else {
95             names = new String[ args.length - 3 ];
96             for( int i = 0; i < ( args.length - 3 ); ++i ) {
97                 names[ i ] = args[ i + 3 ];
98             }
99         }
100         if ( ktc ) {
101             final List<Taxonomy> taxonomies_to_keep = new ArrayList<Taxonomy>();
102             for( final String n : names ) {
103                 final Taxonomy t = new Taxonomy();
104                 try {
105                     t.setTaxonomyCode( n );
106                 }
107                 catch ( final PhyloXmlDataFormatException e ) {
108                     System.out.println( e.getMessage() );
109                     System.exit( -1 );
110                 }
111                 taxonomies_to_keep.add( t );
112             }
113             PhylogenyMethods.deleteExternalNodesPositiveSelectionT( taxonomies_to_keep, p );
114         }
115         else if ( keep ) {
116             PhylogenyMethods.deleteExternalNodesPositiveSelection( names, p );
117         }
118         else {
119             PhylogenyMethods.deleteExternalNodesNegativeSelection( names, p );
120         }
121         try {
122             final PhylogenyWriter w = new PhylogenyWriter();
123             w.toPhyloXML( outfile, p, 0 );
124         }
125         catch ( final IOException e ) {
126             System.out.println( "\nFailure to write output [" + e.getMessage() + "]\n" );
127             System.exit( -1 );
128         }
129     }
130
131     private static String[] readInNamesFromPhylogeny( final String file ) {
132         Phylogeny p0 = null;
133         try {
134             final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
135             final File f = new File( file );
136             final PhylogenyParser pp = ParserUtils.createParserDependingOnFileType( f, true );
137             p0 = factory.create( f, pp )[ 0 ];
138         }
139         catch ( final Exception e ) {
140             System.out.println( "\nCould not read \"" + file + "\" [" + e.getMessage() + "]\n" );
141             System.exit( -1 );
142         }
143         return p0.getAllExternalNodeNames();
144     }
145 }