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: https://sites.google.com/site/cmzmasek/home/software/forester
26 package org.forester.application;
29 import java.io.IOException;
30 import java.util.ArrayList;
31 import java.util.List;
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;
43 public class phylostrip {
45 public static void main( final String args[] ) {
46 if ( args.length < 4 ) {
47 System.out.println( "\nstrip: Wrong number of arguments.\n" );
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" );
57 final File infile = new File( args[ 0 ] );
58 final File outfile = new File( args[ 1 ] );
59 final String options = args[ 2 ];
62 final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
63 final PhylogenyParser pp = ParserUtils.createParserDependingOnFileType( infile, true );
64 p = factory.create( infile, pp )[ 0 ];
66 catch ( final Exception e ) {
67 System.out.println( "\nCould not read \"" + infile + "\" [" + e.getMessage() + "]\n" );
71 boolean from_p0 = false;
73 if ( options.trim().toLowerCase().equals( "-knn" ) ) {
76 else if ( options.trim().toLowerCase().equals( "-knnp" ) ) {
80 else if ( options.trim().toLowerCase().equals( "-rnnp" ) ) {
83 else if ( options.trim().toLowerCase().equals( "-ktc" ) ) {
86 else if ( !options.trim().toLowerCase().equals( "-rnn" ) ) {
87 System.out.println( "\nUnknown option \"" + options + "\"\n" );
90 String[] names = null;
92 names = phylostrip.readInNamesFromPhylogeny( args[ 3 ] );
95 names = new String[ args.length - 3 ];
96 for( int i = 0; i < ( args.length - 3 ); ++i ) {
97 names[ i ] = args[ i + 3 ];
101 final List<Taxonomy> taxonomies_to_keep = new ArrayList<Taxonomy>();
102 for( final String n : names ) {
103 final Taxonomy t = new Taxonomy();
105 t.setTaxonomyCode( n );
107 catch ( final PhyloXmlDataFormatException e ) {
108 System.out.println( e.getMessage() );
111 taxonomies_to_keep.add( t );
113 PhylogenyMethods.deleteExternalNodesPositiveSelectionT( taxonomies_to_keep, p );
116 PhylogenyMethods.deleteExternalNodesPositiveSelection( names, p );
119 PhylogenyMethods.deleteExternalNodesNegativeSelection( names, p );
122 final PhylogenyWriter w = new PhylogenyWriter();
123 w.toPhyloXML( outfile, p, 0 );
125 catch ( final IOException e ) {
126 System.out.println( "\nFailure to write output [" + e.getMessage() + "]\n" );
131 private static String[] readInNamesFromPhylogeny( final String file ) {
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 ];
139 catch ( final Exception e ) {
140 System.out.println( "\nCould not read \"" + file + "\" [" + e.getMessage() + "]\n" );
143 return p0.getAllExternalNodeNames();