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/reinv_count.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.reinv_count
34 import java.util.List;
35 import java.util.SortedSet;
36 import java.util.TreeSet;
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;
44 public class reinv_count {
46 public static void main( final String args[] ) {
47 if ( args.length != 2 ) {
49 System.err.println( "reinv_count: wrong number of arguments" );
50 System.err.println( "Usage: \"reinv_count <intree> <name>" );
54 final File infile = new File( args[ 0 ] );
55 final String node_name = args[ 1 ];
58 final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
59 phy = factory.create( infile, org.forester.io.parsers.util.ParserUtils
60 .createParserDependingOnFileType( infile, true ) )[ 0 ];
62 catch ( final Exception e ) {
63 System.err.println( e + "\nCould not read " + infile + "\n" );
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() );
71 final SortedSet<String> reinv = new TreeSet<String>();
72 for( final String target_dc : target_dcs ) {
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 ) ) {
83 reinv.add( target_dc );
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 );
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() );