in progress
[jalview.git] / forester_applications / src / org / forester / applications / get_genome_counts_per_char.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/get_genome_counts_per_char.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.get_genome_counts_per_char
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.PhylogenyMethods;
40 import org.forester.phylogeny.PhylogenyNode;
41 import org.forester.phylogeny.factories.ParserBasedPhylogenyFactory;
42 import org.forester.phylogeny.factories.PhylogenyFactory;
43
44 public class get_genome_counts_per_char {
45
46     final static boolean SIMPLE = true;
47
48     public static void main( final String args[] ) {
49         if ( args.length != 1 ) {
50             System.err.println();
51             System.err.println( "get_genome_counts_per_char: wrong number of arguments" );
52             System.err.println( "Usage: \"get_subtree_specific_chars <intree>" );
53             System.err.println();
54             System.exit( -1 );
55         }
56         final File infile = new File( args[ 0 ] );
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> all_chars = getAllExternalPresentAndGainedCharacters( phy.getRoot() );
68         final SortedSet<String> human = getAllExternalPresentAndGainedCharacters( phy.getNode( "HUMAN" ) );
69         final SortedSet<String> primates = getAllExternalPresentAndGainedCharacters( phy.getNode( find( "Primates", phy ) ) );
70         final SortedSet<String> mammalia = getAllExternalPresentAndGainedCharacters( phy.getNode(find( "Mammalia", phy ) ) );
71         final SortedSet<String> metazoa = getAllExternalPresentAndGainedCharacters( phy.getNode(find( "Metazoa", phy ) ) );
72         final SortedSet<String> fungi = getAllExternalPresentAndGainedCharacters( phy.getNode(find( "Fungi", phy ) ) );
73         final SortedSet<String> plants = getAllExternalPresentAndGainedCharacters( phy.getNode(find( "Viridiplantae", phy ) ) );
74         System.out.println( "Sum of all external characters:\t" + all_chars.size() );
75         System.out.println();
76         final List<PhylogenyNode> ext = phy.getRoot().getAllExternalDescendants();
77         System.out.println( "genomes" + "\t" + ext.size() );
78         for( final String c : all_chars ) {
79             int count = 0;
80             for( final PhylogenyNode e : ext ) {
81                 if ( e.getNodeData().getBinaryCharacters().getGainedCharacters().contains( c )
82                         || e.getNodeData().getBinaryCharacters().getPresentCharacters().contains( c ) ) {
83                     count++;
84                 }
85             }
86             if ( count < 1 ) {
87                 System.err.println( "error" );
88                 System.exit( -1 );
89             }
90             System.out.print( c + "\t" + count + "\t" );
91             if ( human.contains( c ) ) {
92                 System.out.print( "HUMAN" + "\t" );
93             }
94             else {
95                 System.out.print( "" + "\t" );
96             }
97             if ( primates.contains( c ) ) {
98                 System.out.print( "PRIMATES" + "\t" );
99             }
100             else {
101                 System.out.print( "" + "\t" );
102             }
103             if ( mammalia.contains( c ) ) {
104                 System.out.print( "MAMMALS" + "\t" );
105             }
106             else {
107                 System.out.print( "" + "\t" );
108             }
109             if ( metazoa.contains( c ) ) {
110                 System.out.print( "METAZOA" + "\t" );
111             }
112             else {
113                 System.out.print( "" + "\t" );
114             }
115             if ( fungi.contains( c ) ) {
116                 System.out.print( "FUNGI" + "\t" );
117             }
118             else {
119                 System.out.print( "" + "\t" );
120             }
121             if ( plants.contains( c ) ) {
122                 System.out.print( "PLANTS" + "\t" );
123             }
124             else {
125                 System.out.print( "" + "\t" );
126             }
127             System.out.println();
128         }
129     }
130
131     private static Long find( final String s, final Phylogeny phy ) {
132         final List<Long> l = PhylogenyMethods.searchData( s, phy, true, false, false, false, 0 );
133         if ( l.size() != 1 ) {
134             System.err.println( "error: " + s );
135             System.exit( -1 );
136         }
137         return l.get( 0 );
138     }
139
140     private static SortedSet<String> getAllExternalPresentAndGainedCharacters( final PhylogenyNode node ) {
141         final SortedSet<String> chars = new TreeSet<String>();
142         final List<PhylogenyNode> descs = node.getAllExternalDescendants();
143         for( final PhylogenyNode desc : descs ) {
144             chars.addAll( desc.getNodeData().getBinaryCharacters().getGainedCharacters() );
145             chars.addAll( desc.getNodeData().getBinaryCharacters().getPresentCharacters() );
146         }
147         return chars;
148     }
149 }