initial commit
[jalview.git] / forester_applications / src / org / forester / applications / inverted_dcs.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/inverted_dcs.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.inverted_dcs
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
43 public class inverted_dcs {
44
45     final static boolean SIMPLE = true;
46
47     public static void main( final String args[] ) {
48         if ( args.length != 1 ) {
49             System.err.println();
50             System.err.println( "inverted_dcs: wrong number of arguments" );
51             System.err.println( "Usage: \"get_subtree_specific_chars <intree>" );
52             System.err.println();
53             System.exit( -1 );
54         }
55         final File infile = new File( args[ 0 ] );
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         final SortedSet<String> orig = getAllExternalPresentAndGainedCharacters( phy.getRoot() );
67         System.out.println( "total=" + orig.size() );
68         for( final String dc : orig ) {
69             final String split[] = dc.split( "=" );
70             final String inv = split[ 1 ] + "=" + split[ 0 ];
71             if ( orig.contains( inv ) ) {
72                 System.out.println( dc );
73             }
74         }
75     }
76
77     private static SortedSet<String> getAllExternalPresentAndGainedCharacters( final PhylogenyNode node ) {
78         final SortedSet<String> chars = new TreeSet<String>();
79         final List<PhylogenyNode> descs = node.getAllExternalDescendants();
80         for( final PhylogenyNode desc : descs ) {
81             chars.addAll( desc.getNodeData().getBinaryCharacters().getGainedCharacters() );
82             chars.addAll( desc.getNodeData().getBinaryCharacters().getPresentCharacters() );
83         }
84         return chars;
85     }
86 }