fixed issue with UTF8 encoding.
[jalview.git] / forester_applications / src / org / forester / applications / get_loss_nodes.java
1 // $Id:
2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
4 //
5 // Copyright (C) 2008-2009 Christian M. Zmasek
6 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
7 // All rights reserved
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 //
23 // Contact: phylosoft @ gmail . com
24 // WWW: www.phylosoft.org/forester
25 // javac -cp ~/SOFTWARE_DEV/ECLIPSE_WORKSPACE/forester/java/forester.jar
26 // ~/SOFTWARE_DEV/ECLIPSE_WORKSPACE/forester_applications/src/org/forester/applications/get_loss_nodes.java
27 // java -Xmx2048m -cp
28 // /home/czmasek/SOFTWARE_DEV/ECLIPSE_WORKSPACE/forester_applications/src/:/home/czmasek/SOFTWARE_DEV/ECLIPSE_WORKSPACE/forester/java/forester.jar
29 // org.forester.applications.get_loss_nodes
30
31 package org.forester.applications;
32
33 import java.io.File;
34 import java.io.IOException;
35 import java.util.List;
36
37 import org.forester.io.parsers.PhylogenyParser;
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 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
43 import org.forester.util.ForesterUtil;
44
45 public class get_loss_nodes {
46
47     public static void main( final String args[] ) {
48         if ( args.length != 2 ) {
49             System.out.println( "get_loss_nodes: Wrong number of arguments" );
50             System.out.println( "Usage: \"get_loss_nodes <phylogeny file> <file with characters>\"" );
51             System.exit( -1 );
52         }
53         final File phylogeny_infile = new File( args[ 0 ] );
54         Phylogeny p = null;
55         try {
56             final PhylogenyParser pp = org.forester.io.parsers.util.ParserUtils
57                     .createParserDependingOnFileType( phylogeny_infile, true );
58             final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
59             p = factory.create( phylogeny_infile, pp )[ 0 ];
60         }
61         catch ( final Exception e ) {
62             e.printStackTrace();
63             System.exit( -1 );
64         }
65         List<String> chars = null;
66         try {
67             chars = ForesterUtil.file2list( new File( args[ 1 ] ) );
68         }
69         catch ( final IOException e ) {
70             e.printStackTrace();
71             System.exit( -1 );
72         }
73         for( final String c : chars ) {
74             boolean found = false;
75             for( final PhylogenyNodeIterator it = p.iteratorPostorder(); it.hasNext(); ) {
76                 final PhylogenyNode n = it.next();
77                 if ( n.getNodeData().getBinaryCharacters().getLostCharacters().contains( c ) ) {
78                     if ( n.getNodeData().isHasTaxonomy()
79                             && !ForesterUtil.isEmpty( n.getNodeData().getTaxonomy().getScientificName() ) ) {
80                         System.out.println( c + "\t" + n.getNodeData().getTaxonomy().getScientificName() );
81                     }
82                     else {
83                         System.out.println( c + "\t" + n.getName() );
84                     }
85                     found = true;
86                 }
87             }
88             if ( !found ) {
89                 System.out.println( c + "\t" + "never lost" );
90             }
91         }
92     }
93 }