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/get_genome_counts_per_char.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.get_genome_counts_per_char
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.PhylogenyMethods;
40 import org.forester.phylogeny.PhylogenyNode;
41 import org.forester.phylogeny.factories.ParserBasedPhylogenyFactory;
42 import org.forester.phylogeny.factories.PhylogenyFactory;
44 public class get_genome_counts_per_char {
46 final static boolean SIMPLE = true;
48 public static void main( final String args[] ) {
49 if ( args.length != 1 ) {
51 System.err.println( "get_genome_counts_per_char: wrong number of arguments" );
52 System.err.println( "Usage: \"get_subtree_specific_chars <intree>" );
56 final File infile = new File( args[ 0 ] );
59 final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
60 phy = factory.create( infile, org.forester.io.parsers.util.ParserUtils
61 .createParserDependingOnFileType( infile, true ) )[ 0 ];
63 catch ( final Exception e ) {
64 System.err.println( e + "\nCould not read " + infile + "\n" );
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() );
76 final List<PhylogenyNode> ext = phy.getRoot().getAllExternalDescendants();
77 System.out.println( "genomes" + "\t" + ext.size() );
78 for( final String c : all_chars ) {
80 for( final PhylogenyNode e : ext ) {
81 if ( e.getNodeData().getBinaryCharacters().getGainedCharacters().contains( c )
82 || e.getNodeData().getBinaryCharacters().getPresentCharacters().contains( c ) ) {
87 System.err.println( "error" );
90 System.out.print( c + "\t" + count + "\t" );
91 if ( human.contains( c ) ) {
92 System.out.print( "HUMAN" + "\t" );
95 System.out.print( "" + "\t" );
97 if ( primates.contains( c ) ) {
98 System.out.print( "PRIMATES" + "\t" );
101 System.out.print( "" + "\t" );
103 if ( mammalia.contains( c ) ) {
104 System.out.print( "MAMMALS" + "\t" );
107 System.out.print( "" + "\t" );
109 if ( metazoa.contains( c ) ) {
110 System.out.print( "METAZOA" + "\t" );
113 System.out.print( "" + "\t" );
115 if ( fungi.contains( c ) ) {
116 System.out.print( "FUNGI" + "\t" );
119 System.out.print( "" + "\t" );
121 if ( plants.contains( c ) ) {
122 System.out.print( "PLANTS" + "\t" );
125 System.out.print( "" + "\t" );
127 System.out.println();
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 );
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() );