analyze pplacer output...
[jalview.git] / forester / java / src / org / forester / application / annotator.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: https://sites.google.com/site/cmzmasek/home/software/forester
25
26 package org.forester.application;
27
28 import java.io.File;
29 import java.io.IOException;
30 import java.util.ArrayList;
31 import java.util.Collections;
32 import java.util.List;
33
34 import org.forester.analysis.AncestralTaxonomyInference;
35 import org.forester.analysis.AncestralTaxonomyInferenceException;
36 import org.forester.io.parsers.phyloxml.PhyloXmlParser;
37 import org.forester.io.writers.PhylogenyWriter;
38 import org.forester.phylogeny.Phylogeny;
39 import org.forester.phylogeny.factories.ParserBasedPhylogenyFactory;
40 import org.forester.phylogeny.factories.PhylogenyFactory;
41 import org.forester.util.CommandLineArguments;
42 import org.forester.util.ForesterUtil;
43 import org.forester.ws.seqdb.SequenceDbWsTools;
44
45 public final class annotator {
46
47     final static private String PRG_NAME    = "annotator";
48     final static private String PRG_VERSION = "1.00";
49     final static private String PRG_DATE    = "131122";
50
51     public static void main( final String args[] ) {
52         ForesterUtil.printProgramInformation( annotator.PRG_NAME, annotator.PRG_VERSION, annotator.PRG_DATE );
53         System.out.println();
54         if ( ( args.length != 2 ) ) {
55             annotator.argumentsError();
56         }
57         CommandLineArguments cla = null;
58         try {
59             cla = new CommandLineArguments( args );
60         }
61         catch ( final Exception e ) {
62             ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
63         }
64         final File indir = cla.getFile( 0 );
65         final File outdir = cla.getFile( 1 );
66         if ( !indir.isDirectory() ) {
67             ForesterUtil.fatalError( PRG_NAME, indir + " is not a directory" );
68         }
69         if ( !outdir.isDirectory() ) {
70             ForesterUtil.fatalError( PRG_NAME, outdir + " is not a directory" );
71         }
72         final File[] list_of_files = indir.listFiles();
73         final List<File> infiles = new ArrayList<File>();
74         for( final File file : list_of_files ) {
75             if ( file.isFile() && file.canRead() && file.toString().toLowerCase().endsWith( ".xml" ) ) {
76                 infiles.add( file );
77             }
78         }
79         Collections.sort( infiles );
80         int c = 0;
81         for( final File infile : infiles ) {
82             System.out.println( ++c + "/" + infiles.size() + ": " + infile );
83             final File outfile = new File( outdir.getAbsolutePath().toString() + "/" + infile.getName() );
84             if ( outfile.exists() ) {
85                 System.out.println( outfile + " already exists" );
86             }
87             else {
88                 Phylogeny phy = null;
89                 try {
90                     final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
91                     final Phylogeny[] phylogenies = factory.create( infile,
92                                                                     PhyloXmlParser.createPhyloXmlParserXsdValidating() );
93                     phy = phylogenies[ 0 ];
94                 }
95                 catch ( final Exception e ) {
96                     ForesterUtil
97                     .fatalError( PRG_NAME, "failed to read phylgenies from [" + infile + "] [" + e.getMessage()
98                                  + "]" );
99                 }
100                 try {
101                     obtainSeqInformation( phy );
102                 }
103                 catch ( final IOException e ) {
104                     ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
105                 }
106                 //                try {
107                 //                    inferTaxonomyFromDescendents( phy );
108                 //                }
109                 //                catch ( final IOException e ) {
110                 //                    ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
111                 //                }
112                 //                catch ( final AncestralTaxonomyInferenceException e ) {
113                 //                    ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
114                 //                }
115                 //phy.setRerootable( false );
116                 try {
117                     final PhylogenyWriter w = new PhylogenyWriter();
118                     w.toPhyloXML( phy, 0, outfile );
119                 }
120                 catch ( final IOException e ) {
121                     ForesterUtil.fatalError( PRG_NAME, "failed to write output [" + e.getMessage() + "]" );
122                 }
123             }
124         }
125     }
126
127     private static void obtainSeqInformation( final Phylogeny phy ) throws IOException {
128         SequenceDbWsTools.obtainSeqInformation( phy, true, true, SequenceDbWsTools.DEFAULT_LINES_TO_RETURN );
129     }
130
131     private static void inferTaxonomyFromDescendents( final Phylogeny phy ) throws IOException,
132     AncestralTaxonomyInferenceException {
133         AncestralTaxonomyInference.inferTaxonomyFromDescendents( phy );
134     }
135
136     private static void argumentsError() {
137         System.out.println( annotator.PRG_NAME + " <indir> <outdir>" );
138         System.out.println();
139         System.exit( -1 );
140     }
141 }