in progress
[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         final boolean SIMPLE = false;
65         for( final PhylogenyNodeIterator iter = phy.iteratorPostorder(); iter.hasNext(); ) {
66             final PhylogenyNode node = iter.next();
67             if ( !SIMPLE && node.isExternal() ) {
68                 continue;
69             }
70             if ( !node.isRoot() ) {
71                 System.out.println();
72                 if ( node.getNodeData().isHasTaxonomy()
73                         && !ForesterUtil.isEmpty( node.getNodeData().getTaxonomy().getScientificName() ) ) {
74                     System.out.print( node.getName() + " " + node.getNodeData().getTaxonomy().getScientificName() );
75                 }
76                 else {
77                     System.out.print( node.getName() );
78                 }
79                 System.out.println( ":" );
80                 final SortedSet<Integer> external_ids = getAllExternalDescendantsNodeIds( node );
81                 final SortedSet<Integer> not_external_ids = copy( all_external_ids );
82                 not_external_ids.removeAll( external_ids );
83                 final SortedSet<String> not_node_chars = new TreeSet<String>();
84                 for( final Integer id : not_external_ids ) {
85                     not_node_chars.addAll( getAllExternalPresentAndGainedCharacters( phy.getNode( id ) ) );
86                 }
87                 final SortedSet<String> node_chars = getAllExternalPresentAndGainedCharacters( node );
88                 final SortedSet<String> unique_chars = new TreeSet<String>();
89                 for( final String node_char : node_chars ) {
90                     if ( !not_node_chars.contains( node_char ) ) {
91                         if ( SIMPLE ) {
92                             unique_chars.add( node_char );
93                         }
94                         else {
95                             boolean found = true;
96                             for( final int external_id : external_ids ) {
97                                 if ( !phy.getNode( external_id ).getNodeData().getBinaryCharacters()
98                                         .getGainedCharacters().contains( node_char )
99                                         && !phy.getNode( external_id ).getNodeData().getBinaryCharacters()
100                                                 .getPresentCharacters().contains( node_char ) ) {
101                                     found = false;
102                                     break;
103                                 }
104                             }
105                             if ( found ) {
106                                 unique_chars.add( node_char );
107                             }
108                         }
109                     }
110                 }
111                 System.out.println( "\tSUM:\t" + unique_chars.size() );
112                 int counter = 1;
113                 for( final String unique_char : unique_chars ) {
114                     System.out.println( "\t" + counter + ":\t" + unique_char );
115                     ++counter;
116                 }
117             }
118         }
119     }
120
121     private static SortedSet<Integer> copy( final SortedSet<Integer> set ) {
122         final SortedSet<Integer> copy = new TreeSet<Integer>();
123         for( final Integer i : set ) {
124             copy.add( i );
125         }
126         return copy;
127     }
128
129     private static SortedSet<Integer> getAllExternalDescendantsNodeIds( final PhylogenyNode node ) {
130         final SortedSet<Integer> ids = new TreeSet<Integer>();
131         final List<PhylogenyNode> descs = node.getAllExternalDescendants();
132         for( final PhylogenyNode desc : descs ) {
133             ids.add( desc.getId() );
134         }
135         return ids;
136     }
137
138     private static SortedSet<String> getAllExternalPresentAndGainedCharacters( final PhylogenyNode node ) {
139         final SortedSet<String> chars = new TreeSet<String>();
140         final List<PhylogenyNode> descs = node.getAllExternalDescendants();
141         for( final PhylogenyNode desc : descs ) {
142             chars.addAll( desc.getNodeData().getBinaryCharacters().getGainedCharacters() );
143             chars.addAll( desc.getNodeData().getBinaryCharacters().getPresentCharacters() );
144         }
145         return chars;
146     }
147 }