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 public static void main( final String args[] ) {
43 if ( args.length != 1 ) {
45 System.err.println( "get_subtree_specific_chars: wrong number of arguments" );
46 System.err.println( "Usage: \"get_subtree_specific_chars <intree>" );
50 final File infile = new File( args[ 0 ] );
53 final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
54 phy = factory.create( infile, ForesterUtil.createParserDependingOnFileType( infile, true ) )[ 0 ];
56 catch ( final Exception e ) {
57 System.err.println( e + "\nCould not read " + infile + "\n" );
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() );
64 for( final PhylogenyNodeIterator iter = phy.iteratorPostorder(); iter.hasNext(); ) {
65 final PhylogenyNode node = iter.next();
66 if ( !node.isRoot() ) {
68 if ( node.getNodeData().isHasTaxonomy()
69 && !ForesterUtil.isEmpty( node.getNodeData().getTaxonomy().getScientificName() ) ) {
70 System.out.print( node.getName() + " " + node.getNodeData().getTaxonomy().getScientificName() );
73 System.out.print( node.getName() );
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 ) ) );
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 );
90 System.out.println( "\tSUM:\t" + unique_chars.size() );
92 for( final String unique_char : unique_chars ) {
93 System.out.println( "\t" + counter + ":\t" + unique_char );
100 private static SortedSet<Integer> copy( final SortedSet<Integer> set ) {
101 final SortedSet<Integer> copy = new TreeSet<Integer>();
102 for( final Integer i : set ) {
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() );
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() );