in pprogress...
[jalview.git] / forester_applications / src / org / forester / applications / reinv_count.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/reinv_count.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.reinv_count
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.phylogeny.Phylogeny;
39 import org.forester.phylogeny.PhylogenyNode;
40 import org.forester.phylogeny.factories.ParserBasedPhylogenyFactory;
41 import org.forester.phylogeny.factories.PhylogenyFactory;
42 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
43
44 public class reinv_count {
45
46     public static void main( final String args[] ) {
47         if ( args.length != 2 ) {
48             System.err.println();
49             System.err.println( "reinv_count: wrong number of arguments" );
50             System.err.println( "Usage: \"reinv_count <intree> <name>" );
51             System.err.println();
52             System.exit( -1 );
53         }
54         final File infile = new File( args[ 0 ] );
55         final String node_name = args[ 1 ];
56         Phylogeny phy = null;
57         try {
58             final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
59             phy = factory.create( infile, org.forester.io.parsers.util.ParserUtils
60                                   .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         for( final PhylogenyNodeIterator ite = phy.iteratorExternalForward(); ite.hasNext(); ) {
67             final PhylogenyNode target_node = ite.next();
68             final SortedSet<String> target_dcs = getAllExternalPresentAndGainedCharacters( target_node );
69             //System.out.println( "Target DCs:" + target_dcs.size() );
70             int counter = 0;
71             final SortedSet<String> reinv = new TreeSet<String>();
72             for( final String target_dc : target_dcs ) {
73                 int c = 0;
74                 for( final PhylogenyNodeIterator it = phy.iteratorPreorder(); it.hasNext(); ) {
75                     final PhylogenyNode n = it.next();
76                     final SortedSet<String> n_gained_dcs = n.getNodeData().getBinaryCharacters().getGainedCharacters();
77                     if ( n_gained_dcs.contains( target_dc ) ) {
78                         c++;
79                     }
80                 }
81                 if ( c > 1 ) {
82                     counter++;
83                     reinv.add( target_dc );
84                 }
85             }
86             // System.out.println();
87             //System.out.println( "reinv:" + reinv );
88             //System.out.println();
89             // System.out.println( "Target DCs:" + target_dcs.size() );
90             // System.out.println( "reinv size:" + reinv.size() );
91             // System.out.println( ">1:" + counter );
92             final double ratio = ( double ) counter / target_dcs.size();
93             System.out.println( target_node.getName() + "\t" + counter + "/" + target_dcs.size() + "\t" + ratio );
94         }
95     }
96
97     private static SortedSet<String> getAllExternalPresentAndGainedCharacters( final PhylogenyNode node ) {
98         final SortedSet<String> chars = new TreeSet<String>();
99         final List<PhylogenyNode> descs = node.getAllExternalDescendants();
100         for( final PhylogenyNode desc : descs ) {
101             chars.addAll( desc.getNodeData().getBinaryCharacters().getGainedCharacters() );
102             chars.addAll( desc.getNodeData().getBinaryCharacters().getPresentCharacters() );
103         }
104         return chars;
105     }
106 }