new app
[jalview.git] / forester / java / src / org / forester / application / get_subtree_specific_chars.java
1 // $Id:
2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
4 //
5 // Copyright (C) 2008-2011 Christian M. Zmasek
6 // Copyright (C) 2008-2011 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.util.List;
30 import java.util.SortedSet;
31 import java.util.TreeSet;
32
33 import org.forester.phylogeny.Phylogeny;
34 import org.forester.phylogeny.PhylogenyNode;
35 import org.forester.phylogeny.factories.ParserBasedPhylogenyFactory;
36 import org.forester.phylogeny.factories.PhylogenyFactory;
37 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
38 import org.forester.util.ForesterUtil;
39
40 public class get_subtree_specific_chars {
41
42     public static void main( final String args[] ) {
43         if ( args.length != 1 ) {
44             System.err.println();
45             System.err.println( "get_subtree_specific_chars: wrong number of arguments" );
46             System.err.println( "Usage: \"get_subtree_specific_chars <intree>" );
47             System.err.println();
48             System.exit( -1 );
49         }
50         final File infile = new File( args[ 0 ] );
51         Phylogeny phy = null;
52         try {
53             final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
54             phy = factory.create( infile, ForesterUtil.createParserDependingOnFileType( infile, true ) )[ 0 ];
55         }
56         catch ( final Exception e ) {
57             System.err.println( e + "\nCould not read " + infile + "\n" );
58             System.exit( -1 );
59         }
60         final SortedSet<Integer> all_external_ids = getAllExternalDescendantsNodeIds( phy.getRoot() );
61         final SortedSet<String> all_chars = getAllExternalPresentAndGainedCharacters( phy.getRoot() );
62         System.out.println( "Sum of all external characters:\t" + all_chars.size() );
63         System.out.println();
64         for( final PhylogenyNodeIterator iter = phy.iteratorPostorder(); iter.hasNext(); ) {
65             final PhylogenyNode node = iter.next();
66             if ( !node.isRoot() ) {
67                 System.out.println();
68                 if ( node.getNodeData().isHasTaxonomy()
69                         && !ForesterUtil.isEmpty( node.getNodeData().getTaxonomy().getScientificName() ) ) {
70                     System.out.print( node.getName() + " " + node.getNodeData().getTaxonomy().getScientificName() );
71                 }
72                 else {
73                     System.out.print( node.getName() );
74                 }
75                 System.out.println( ":" );
76                 final SortedSet<Integer> external_ids = getAllExternalDescendantsNodeIds( node );
77                 final SortedSet<Integer> not_external_ids = copy( all_external_ids );
78                 not_external_ids.removeAll( external_ids );
79                 final SortedSet<String> not_node_chars = new TreeSet<String>();
80                 for( final Integer id : not_external_ids ) {
81                     not_node_chars.addAll( getAllExternalPresentAndGainedCharacters( phy.getNode( id ) ) );
82                 }
83                 final SortedSet<String> node_chars = getAllExternalPresentAndGainedCharacters( node );
84                 final SortedSet<String> unique_chars = new TreeSet<String>();
85                 for( final String node_char : node_chars ) {
86                     if ( !not_node_chars.contains( node_char ) ) {
87                         unique_chars.add( node_char );
88                     }
89                 }
90                 System.out.println( "\tSUM:\t" + unique_chars.size() );
91                 int counter = 1;
92                 for( final String unique_char : unique_chars ) {
93                     System.out.println( "\t" + counter + ":\t" + unique_char );
94                     ++counter;
95                 }
96             }
97         }
98     }
99
100     private static SortedSet<Integer> copy( final SortedSet<Integer> set ) {
101         final SortedSet<Integer> copy = new TreeSet<Integer>();
102         for( final Integer i : set ) {
103             copy.add( i );
104         }
105         return copy;
106     }
107
108     private static SortedSet<Integer> getAllExternalDescendantsNodeIds( final PhylogenyNode node ) {
109         final SortedSet<Integer> ids = new TreeSet<Integer>();
110         final List<PhylogenyNode> descs = node.getAllExternalDescendants();
111         for( final PhylogenyNode desc : descs ) {
112             ids.add( desc.getId() );
113         }
114         return ids;
115     }
116
117     private static SortedSet<String> getAllExternalPresentAndGainedCharacters( final PhylogenyNode node ) {
118         final SortedSet<String> chars = new TreeSet<String>();
119         final List<PhylogenyNode> descs = node.getAllExternalDescendants();
120         for( final PhylogenyNode desc : descs ) {
121             chars.addAll( desc.getNodeData().getBinaryCharacters().getGainedCharacters() );
122             chars.addAll( desc.getNodeData().getBinaryCharacters().getPresentCharacters() );
123         }
124         return chars;
125     }
126 }