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 final boolean SIMPLE = false;
65 for( final PhylogenyNodeIterator iter = phy.iteratorPostorder(); iter.hasNext(); ) {
66 final PhylogenyNode node = iter.next();
67 if ( !SIMPLE && node.isExternal() ) {
70 if ( !node.isRoot() ) {
72 if ( node.getNodeData().isHasTaxonomy()
73 && !ForesterUtil.isEmpty( node.getNodeData().getTaxonomy().getScientificName() ) ) {
74 System.out.print( node.getName() + " " + node.getNodeData().getTaxonomy().getScientificName() );
77 System.out.print( node.getName() );
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 ) ) );
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 ) ) {
92 unique_chars.add( node_char );
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 ) ) {
106 unique_chars.add( node_char );
111 System.out.println( "\tSUM:\t" + unique_chars.size() );
113 for( final String unique_char : unique_chars ) {
114 System.out.println( "\t" + counter + ":\t" + unique_char );
121 private static SortedSet<Integer> copy( final SortedSet<Integer> set ) {
122 final SortedSet<Integer> copy = new TreeSet<Integer>();
123 for( final Integer i : set ) {
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() );
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() );