fixed issue with UTF8 encoding.
[jalview.git] / forester_applications / src / org / forester / applications / core_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/core_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.core_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.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 core_chars {
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( "core_chars: 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> a = getAllExternalPresentAndGainedCharacters( phy.getNode( "Opisthokonta" ) );
67         final SortedSet<String> b = getAllExternalPresentAndGainedCharacters( phy.getNode( "THETRA" ) );
68         final SortedSet<String> c = getAllExternalPresentAndGainedCharacters( phy.getNode( "Amoebozoa" ) );
69         final SortedSet<String> d = getAllExternalPresentAndGainedCharacters( phy.getNode( "Archaeplastida" ) );
70         final SortedSet<String> e = getAllExternalPresentAndGainedCharacters( phy.getNode( "Chromalveolate" ) );
71         final SortedSet<String> f = getAllExternalPresentAndGainedCharacters( phy.getNode( "Excavata" ) );
72         a.retainAll( b );
73         a.retainAll( c );
74         a.retainAll( d );
75         a.retainAll( e );
76         a.retainAll( f );
77         System.out.println( a.size() );
78         for( final String s : a ) {
79             System.out.println( s );
80         }
81     }
82
83     private static SortedSet<String> getAllExternalPresentAndGainedCharacters( final PhylogenyNode node ) {
84         final SortedSet<String> chars = new TreeSet<String>();
85         final List<PhylogenyNode> descs = node.getAllExternalDescendants();
86         for( final PhylogenyNode desc : descs ) {
87             chars.addAll( desc.getNodeData().getBinaryCharacters().getGainedCharacters() );
88             chars.addAll( desc.getNodeData().getBinaryCharacters().getPresentCharacters() );
89         }
90         return chars;
91     }
92 }