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/genome_counts_for_once_appearing_dcs.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.genome_counts_for_once_appearing_dcs
34 import java.util.HashMap;
35 import java.util.List;
38 import java.util.SortedSet;
39 import java.util.TreeSet;
41 import org.forester.phylogeny.Phylogeny;
42 import org.forester.phylogeny.PhylogenyNode;
43 import org.forester.phylogeny.factories.ParserBasedPhylogenyFactory;
44 import org.forester.phylogeny.factories.PhylogenyFactory;
45 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
46 import org.forester.util.BasicDescriptiveStatistics;
47 import org.forester.util.DescriptiveStatistics;
49 public class genome_counts_for_once_appearing_dcs {
51 public static void main( final String args[] ) {
52 if ( args.length != 1 ) {
54 System.err.println( "genome_counts_for_once_appearing_dcs: wrong number of arguments" );
55 System.err.println( "Usage: \"genome_counts_for_once_appearing_dcs <intree>" );
59 final File infile = new File( args[ 0 ] );
62 final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
63 phy = factory.create( infile, org.forester.io.parsers.util.ParserUtils
64 .createParserDependingOnFileType( infile, true ) )[ 0 ];
66 catch ( final Exception e ) {
67 System.err.println( e + "\nCould not read " + infile + "\n" );
70 final SortedSet<String> all_dcs = getAllExternalPresentAndGainedCharacters( phy.getRoot() );
71 final SortedSet<String> appearing_once_dcs = new TreeSet<String>();
72 System.out.println( "All DCs: " + all_dcs.size() );
73 for( final String dc : all_dcs ) {
74 int reappearing_count = 0;
75 for( final PhylogenyNodeIterator it = phy.iteratorPreorder(); it.hasNext(); ) {
76 final PhylogenyNode n = it.next();
77 SortedSet<String> n_gained_dcs = null;
79 n_gained_dcs = n.getNodeData().getBinaryCharacters().getPresentCharacters();
82 n_gained_dcs = n.getNodeData().getBinaryCharacters().getGainedCharacters();
84 if ( n_gained_dcs.contains( dc ) ) {
88 if ( reappearing_count < 1 ) {
89 System.out.println( "error: " + dc );
92 if ( reappearing_count == 1 ) {
93 appearing_once_dcs.add( dc );
96 System.out.println( "Appearing once DCs: " + appearing_once_dcs.size() );
97 final DescriptiveStatistics stats = new BasicDescriptiveStatistics();
98 final Map<String, Set<String>> node_to_chars = new HashMap<String, Set<String>>();
99 final SortedSet<String> appearing_in_all_dcs = new TreeSet<String>();
100 for( final String appearing_once_dc : appearing_once_dcs ) {
102 for( final PhylogenyNodeIterator ite = phy.iteratorExternalForward(); ite.hasNext(); ) {
103 final PhylogenyNode ext_node = ite.next();
104 if ( !node_to_chars.containsKey( ext_node.getName() ) ) {
105 node_to_chars.put( ext_node.getName(), getAllExternalPresentAndGainedCharacters( ext_node ) );
107 if ( node_to_chars.get( ext_node.getName() ).contains( appearing_once_dc ) ) {
112 System.out.println( "error, count is <1" );
115 if ( count == phy.getNumberOfExternalNodes() ) {
116 appearing_in_all_dcs.add( appearing_once_dc );
118 stats.addValue( count );
120 System.out.println();
121 System.out.println( stats.toString() );
122 System.out.println();
123 final int[] bins = BasicDescriptiveStatistics.performBinning( stats.getDataAsDoubleArray(), 1, 172, 172 );
124 for( int i = 0; i < bins.length; i++ ) {
125 System.out.println( ( i + 1 ) + "\t" + bins[ i ] );
127 System.out.println();
128 System.out.println( "appearing in all:" );
129 for( final String i : appearing_in_all_dcs ) {
130 System.out.println( i );
132 System.out.println();
133 for( final String dc : appearing_once_dcs ) {
134 System.out.println( "1\t" + dc );
138 private static SortedSet<String> getAllExternalPresentAndGainedCharacters( final PhylogenyNode node ) {
139 final SortedSet<String> chars = new TreeSet<String>();
140 final List<PhylogenyNode> descs = node.getAllExternalDescendants();
141 for( final PhylogenyNode desc : descs ) {
142 chars.addAll( desc.getNodeData().getBinaryCharacters().getGainedCharacters() );
143 chars.addAll( desc.getNodeData().getBinaryCharacters().getPresentCharacters() );