2 package org.forester.applications;
5 // FORESTER -- software libraries and applications
6 // for evolutionary biology research and applications.
8 // Copyright (C) 2008-2011 Christian M. Zmasek
9 // Copyright (C) 2008-2011 Burnham Institute for Medical Research
10 // All rights reserved
12 // This library is free software; you can redistribute it and/or
13 // modify it under the terms of the GNU Lesser General Public
14 // License as published by the Free Software Foundation; either
15 // version 2.1 of the License, or (at your option) any later version.
17 // This library is distributed in the hope that it will be useful,
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 // Lesser General Public License for more details.
22 // You should have received a copy of the GNU Lesser General Public
23 // License along with this library; if not, write to the Free Software
24 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
26 // Contact: phylosoft @ gmail . com
27 // WWW: www.phylosoft.org/forester
28 // javac -cp ~/SOFTWARE_DEV/ECLIPSE_WORKSPACE/forester/java/forester.jar
29 // ~/SOFTWARE_DEV/ECLIPSE_WORKSPACE/forester_applications/src/org/forester/applications/get_shared_chars.java
31 // /home/czmasek/SOFTWARE_DEV/ECLIPSE_WORKSPACE/forester_applications/src/:/home/czmasek/SOFTWARE_DEV/ECLIPSE_WORKSPACE/forester/java/forester.jar
32 // org.forester.applications.get_shared_chars
34 import java.util.List;
35 import java.util.SortedSet;
36 import java.util.TreeSet;
38 import org.forester.io.parsers.util.ParserUtils;
39 import org.forester.phylogeny.Phylogeny;
40 import org.forester.phylogeny.PhylogenyNode;
41 import org.forester.phylogeny.factories.ParserBasedPhylogenyFactory;
42 import org.forester.phylogeny.factories.PhylogenyFactory;
44 public class get_shared_chars {
46 public final static boolean DEBUG = true;
48 public static void main( final String args[] ) {
49 if ( args.length < 2 ) {
51 System.err.println( "get_subtree_specific_chars: wrong number of arguments" );
52 System.err.println( "Usage: \"get_shared_chars <intree> <subtree 1> <subtree 2> ... <subtree n>" );
56 final File infile = new File( args[ 0 ] );
59 final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
60 phy = factory.create( infile, ParserUtils.createParserDependingOnFileType( infile, true ) )[ 0 ];
62 catch ( final Exception e ) {
63 System.err.println( e + "\nCould not read " + infile + "\n" );
66 final SortedSet<Long> outside_external_ids = getAllExternalDescendantsNodeIds( phy.getRoot() );
67 final SortedSet<String> all_chars = getAllExternalPresentAndGainedCharacters( phy.getRoot() );
68 System.out.println( "Sum of all external characters:\t" + all_chars.size() );
69 final SortedSet<String> all_shared_chars = new TreeSet<String>();
70 for( int i = 1; i < args.length; ++i ) {
71 System.out.print( args[ i ] + "\t" );
72 final PhylogenyNode current_node = phy.getNode( args[ i ] );
74 all_shared_chars.addAll( getAllExternalPresentAndGainedCharacters( current_node ) );
77 all_shared_chars.retainAll( getAllExternalPresentAndGainedCharacters( current_node ) );
79 outside_external_ids.removeAll( getAllExternalDescendantsNodeIds( current_node ) );
83 System.out.println( "Number of outside nodes: " + outside_external_ids.size() );
85 final SortedSet<String> outside_chars = new TreeSet<String>();
86 System.out.println( "All shared characters\t" + all_shared_chars.size() );
87 for( final Long id : outside_external_ids ) {
88 outside_chars.addAll( getAllExternalPresentAndGainedCharacters( phy.getNode( id ) ) );
90 final SortedSet<String> unique_shared_chars = copy( all_shared_chars );
91 unique_shared_chars.removeAll( outside_chars );
92 System.out.println( "Unique shared characters\t" + unique_shared_chars.size() );
94 System.out.println( "Unique shared characters:" );
95 for( final String unique_shared_char : unique_shared_chars ) {
96 System.out.println( unique_shared_char );
100 private static SortedSet<String> copy( final SortedSet<String> set ) {
101 final SortedSet<String> copy = new TreeSet<String>();
102 for( final String s : set ) {
108 private static SortedSet<Long> getAllExternalDescendantsNodeIds( final PhylogenyNode node ) {
109 final SortedSet<Long> ids = new TreeSet<Long>();
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() );