in pprogress...
[jalview.git] / forester_applications / src / org / forester / applications / get_shared_chars.java
1
2 package org.forester.applications;
3
4 // $Id:
5 // FORESTER -- software libraries and applications
6 // for evolutionary biology research and applications.
7 //
8 // Copyright (C) 2008-2011 Christian M. Zmasek
9 // Copyright (C) 2008-2011 Burnham Institute for Medical Research
10 // All rights reserved
11 //
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.
16 //
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.
21 //
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
25 //
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
30 // java -Xmx2048m -cp
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
33 import java.io.File;
34 import java.util.List;
35 import java.util.SortedSet;
36 import java.util.TreeSet;
37
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;
43
44 public class get_shared_chars {
45
46     public final static boolean DEBUG = true;
47
48     public static void main( final String args[] ) {
49         if ( args.length < 2 ) {
50             System.err.println();
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>" );
53             System.err.println();
54             System.exit( -1 );
55         }
56         final File infile = new File( args[ 0 ] );
57         Phylogeny phy = null;
58         try {
59             final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
60             phy = factory.create( infile, ParserUtils.createParserDependingOnFileType( infile, true ) )[ 0 ];
61         }
62         catch ( final Exception e ) {
63             System.err.println( e + "\nCould not read " + infile + "\n" );
64             System.exit( -1 );
65         }
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 ] );
73             if ( i == 1 ) {
74                 all_shared_chars.addAll( getAllExternalPresentAndGainedCharacters( current_node ) );
75             }
76             else {
77                 all_shared_chars.retainAll( getAllExternalPresentAndGainedCharacters( current_node ) );
78             }
79             outside_external_ids.removeAll( getAllExternalDescendantsNodeIds( current_node ) );
80         }
81         System.out.println();
82         if ( DEBUG ) {
83             System.out.println( "Number of outside nodes: " + outside_external_ids.size() );
84         }
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 ) ) );
89         }
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() );
93         System.out.println();
94         System.out.println( "Unique shared characters:" );
95         for( final String unique_shared_char : unique_shared_chars ) {
96             System.out.println( unique_shared_char );
97         }
98     }
99
100     private static SortedSet<String> copy( final SortedSet<String> set ) {
101         final SortedSet<String> copy = new TreeSet<String>();
102         for( final String s : set ) {
103             copy.add( s );
104         }
105         return copy;
106     }
107
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() );
113         }
114         return ids;
115     }
116
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() );
123         }
124         return chars;
125     }
126 }