2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
5 // Copyright (C) 2008-2009 Christian M. Zmasek
6 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
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.
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.
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
23 // Contact: phylosoft @ gmail . com
24 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
26 package org.forester.application;
29 import java.io.IOException;
30 import java.util.ArrayList;
31 import java.util.List;
33 import org.forester.analysis.AncestralTaxonomyInference;
34 import org.forester.analysis.AncestralTaxonomyInferenceException;
35 import org.forester.io.parsers.phyloxml.PhyloXmlParser;
36 import org.forester.io.writers.PhylogenyWriter;
37 import org.forester.phylogeny.Phylogeny;
38 import org.forester.phylogeny.factories.ParserBasedPhylogenyFactory;
39 import org.forester.phylogeny.factories.PhylogenyFactory;
40 import org.forester.util.CommandLineArguments;
41 import org.forester.util.ForesterUtil;
42 import org.forester.ws.seqdb.SequenceDbWsTools;
44 public final class annotator {
46 final static private String PRG_NAME = "annotator";
47 final static private String PRG_VERSION = "1.00";
48 final static private String PRG_DATE = "131122";
50 public static void main( final String args[] ) {
51 ForesterUtil.printProgramInformation( annotator.PRG_NAME, annotator.PRG_VERSION, annotator.PRG_DATE );
53 if ( ( args.length != 2 ) ) {
54 annotator.argumentsError();
56 CommandLineArguments cla = null;
58 cla = new CommandLineArguments( args );
60 catch ( final Exception e ) {
61 ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
63 final File indir = cla.getFile( 0 );
64 final File outdir = cla.getFile( 1 );
65 if ( !indir.isDirectory() ) {
66 ForesterUtil.fatalError( PRG_NAME, indir + " is not a directory" );
68 if ( !outdir.isDirectory() ) {
69 ForesterUtil.fatalError( PRG_NAME, outdir + " is not a directory" );
71 final File[] list_of_files = indir.listFiles();
72 final List<File> infiles = new ArrayList<File>();
73 for( final File file : list_of_files ) {
74 if ( file.isFile() && file.canRead() && file.toString().toLowerCase().endsWith( ".xml" ) ) {
79 for( final File infile : infiles ) {
80 System.out.println( ++c + "/" + infiles.size() + ": " + infile );
83 final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
84 final Phylogeny[] phylogenies = factory.create( infile,
85 PhyloXmlParser.createPhyloXmlParserXsdValidating() );
86 phy = phylogenies[ 0 ];
88 catch ( final Exception e ) {
89 ForesterUtil.fatalError( PRG_NAME, "failed to read phylgenies from [" + infile + "] [" + e.getMessage()
93 obtainSeqInformation( phy );
95 catch ( final IOException e ) {
96 ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
99 inferTaxonomyFromDescendents( phy );
101 catch ( final IOException e ) {
102 ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
104 catch ( final AncestralTaxonomyInferenceException e ) {
105 ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
107 final File outfile = new File( outdir.getAbsolutePath().toString() + "/" + infile.getName() );
109 final PhylogenyWriter w = new PhylogenyWriter();
110 w.toPhyloXML( phy, 0, outfile );
112 catch ( final IOException e ) {
113 ForesterUtil.fatalError( PRG_NAME, "failed to write output [" + e.getMessage() + "]" );
118 private static void obtainSeqInformation( final Phylogeny phy ) throws IOException {
119 SequenceDbWsTools.obtainSeqInformation( phy, true, true, SequenceDbWsTools.DEFAULT_LINES_TO_RETURN );
122 private static void inferTaxonomyFromDescendents( final Phylogeny phy ) throws IOException,
123 AncestralTaxonomyInferenceException {
124 AncestralTaxonomyInference.inferTaxonomyFromDescendents( phy );
127 private static void argumentsError() {
128 System.out.println( annotator.PRG_NAME + " <indir> <outdir>" );
129 System.out.println();