initial commit
[jalview.git] / forester_applications / src / org / forester / applications / shared_chars_in_ext_nodes.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/shared_chars_in_ext_nodes.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.shared_chars_in_ext_nodes
33 import java.io.File;
34 import java.util.SortedSet;
35 import java.util.TreeSet;
36
37 import org.forester.phylogeny.Phylogeny;
38 import org.forester.phylogeny.PhylogenyNode;
39 import org.forester.phylogeny.factories.ParserBasedPhylogenyFactory;
40 import org.forester.phylogeny.factories.PhylogenyFactory;
41 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
42
43 public class shared_chars_in_ext_nodes {
44
45     final static boolean SIMPLE = true;
46
47     public static void main( final String args[] ) {
48         if ( args.length != 2 ) {
49             System.err.println();
50             System.err.println( "shared_chars_in_ext_nodes: wrong number of arguments" );
51             System.err.println( "Usage: \"shared_chars_in_ext_nodes <intree> <node name>" );
52             System.err.println();
53             System.exit( -1 );
54         }
55         final File infile = new File( args[ 0 ] );
56         final String node_name = args[ 1 ];
57         Phylogeny phy = null;
58         try {
59             final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
60             phy = factory.create( infile, org.forester.io.parsers.util.ParserUtils
61                     .createParserDependingOnFileType( infile, true ) )[ 0 ];
62         }
63         catch ( final Exception e ) {
64             System.err.println( e + "\nCould not read " + infile + "\n" );
65             System.exit( -1 );
66         }
67         final SortedSet<String> a = phy.getNode( node_name ).getNodeData().getBinaryCharacters().getGainedCharacters();
68         for( final PhylogenyNodeIterator it = phy.iteratorExternalForward(); it.hasNext(); ) {
69             final PhylogenyNode n = it.next();
70             final SortedSet<String> b = n.getNodeData().getBinaryCharacters().getGainedCharacters();
71             final SortedSet<String> a_copy = copy( a );
72             a_copy.retainAll( b );
73             final double ratio = ( double ) a_copy.size() / b.size();
74             System.out.println( n.getName() + "\t\"" + a_copy.size() + "/" + b.size() + "\"\t" + ratio );
75         }
76     }
77
78     private static SortedSet<String> copy( final SortedSet<String> set ) {
79         final SortedSet<String> copy = new TreeSet<String>();
80         for( final String i : set ) {
81             copy.add( i );
82         }
83         return copy;
84     }
85 }