2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
5 // Copyright (C) 2008-2011 Christian M. Zmasek
6 // Copyright (C) 2008-2011 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: www.phylosoft.org/forester
26 package org.forester.application;
29 import java.util.List;
30 import java.util.SortedSet;
31 import java.util.TreeSet;
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;
40 public class get_subtree_specific_chars {
42 final static boolean SIMPLE = true;
44 public static void main( final String args[] ) {
45 if ( args.length != 1 ) {
47 System.err.println( "get_subtree_specific_chars: wrong number of arguments" );
48 System.err.println( "Usage: \"get_subtree_specific_chars <intree>" );
52 final File infile = new File( args[ 0 ] );
55 final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
56 phy = factory.create( infile, ForesterUtil.createParserDependingOnFileType( infile, true ) )[ 0 ];
58 catch ( final Exception e ) {
59 System.err.println( e + "\nCould not read " + infile + "\n" );
62 final SortedSet<Integer> all_external_ids = getAllExternalDescendantsNodeIds( phy.getRoot() );
63 final SortedSet<String> all_chars = getAllExternalPresentAndGainedCharacters( phy.getRoot() );
64 System.out.println( "Sum of all external characters:\t" + all_chars.size() );
66 for( final PhylogenyNodeIterator iter = phy.iteratorPostorder(); iter.hasNext(); ) {
67 final PhylogenyNode node = iter.next();
68 if ( !SIMPLE && node.isExternal() ) {
71 if ( !node.isRoot() ) {
73 if ( node.getNodeData().isHasTaxonomy()
74 && !ForesterUtil.isEmpty( node.getNodeData().getTaxonomy().getScientificName() ) ) {
75 System.out.print( node.getName() + " " + node.getNodeData().getTaxonomy().getScientificName() );
78 System.out.print( node.getName() );
80 System.out.println( ":" );
81 final SortedSet<Integer> external_ids = getAllExternalDescendantsNodeIds( node );
82 final SortedSet<Integer> not_external_ids = copy( all_external_ids );
83 not_external_ids.removeAll( external_ids );
84 final SortedSet<String> not_node_chars = new TreeSet<String>();
85 for( final Integer id : not_external_ids ) {
86 not_node_chars.addAll( getAllExternalPresentAndGainedCharacters( phy.getNode( id ) ) );
88 final SortedSet<String> node_chars = getAllExternalPresentAndGainedCharacters( node );
89 final SortedSet<String> unique_chars = new TreeSet<String>();
90 for( final String node_char : node_chars ) {
91 if ( !not_node_chars.contains( node_char ) ) {
93 unique_chars.add( node_char );
97 for( final int external_id : external_ids ) {
98 if ( !phy.getNode( external_id ).getNodeData().getBinaryCharacters()
99 .getGainedCharacters().contains( node_char )
100 && !phy.getNode( external_id ).getNodeData().getBinaryCharacters()
101 .getPresentCharacters().contains( node_char ) ) {
107 unique_chars.add( node_char );
112 System.out.println( "\tSUM:\t" + unique_chars.size() );
114 for( final String unique_char : unique_chars ) {
115 System.out.println( "\t" + counter + ":\t" + unique_char );
122 private static SortedSet<Integer> copy( final SortedSet<Integer> set ) {
123 final SortedSet<Integer> copy = new TreeSet<Integer>();
124 for( final Integer i : set ) {
130 private static SortedSet<Integer> getAllExternalDescendantsNodeIds( final PhylogenyNode node ) {
131 final SortedSet<Integer> ids = new TreeSet<Integer>();
132 final List<PhylogenyNode> descs = node.getAllExternalDescendants();
133 for( final PhylogenyNode desc : descs ) {
134 ids.add( desc.getId() );
139 private static SortedSet<String> getAllExternalPresentAndGainedCharacters( final PhylogenyNode node ) {
140 final SortedSet<String> chars = new TreeSet<String>();
141 final List<PhylogenyNode> descs = node.getAllExternalDescendants();
142 for( final PhylogenyNode desc : descs ) {
143 chars.addAll( desc.getNodeData().getBinaryCharacters().getGainedCharacters() );
144 chars.addAll( desc.getNodeData().getBinaryCharacters().getPresentCharacters() );