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.Collections;
32 import java.util.List;
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;
45 public final class annotator {
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";
51 public static void main( final String args[] ) {
52 ForesterUtil.printProgramInformation( annotator.PRG_NAME, annotator.PRG_VERSION, annotator.PRG_DATE );
54 if ( ( args.length != 2 ) ) {
55 annotator.argumentsError();
57 CommandLineArguments cla = null;
59 cla = new CommandLineArguments( args );
61 catch ( final Exception e ) {
62 ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
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" );
69 if ( !outdir.isDirectory() ) {
70 ForesterUtil.fatalError( PRG_NAME, outdir + " is not a directory" );
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" ) ) {
79 Collections.sort( infiles );
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" );
90 final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
91 final Phylogeny[] phylogenies = factory.create( infile,
92 PhyloXmlParser.createPhyloXmlParserXsdValidating() );
93 phy = phylogenies[ 0 ];
95 catch ( final Exception e ) {
97 .fatalError( PRG_NAME, "failed to read phylgenies from [" + infile + "] [" + e.getMessage()
101 obtainSeqInformation( phy );
103 catch ( final IOException e ) {
104 ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
107 // inferTaxonomyFromDescendents( phy );
109 // catch ( final IOException e ) {
110 // ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
112 // catch ( final AncestralTaxonomyInferenceException e ) {
113 // ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
115 //phy.setRerootable( false );
117 final PhylogenyWriter w = new PhylogenyWriter();
118 w.toPhyloXML( phy, 0, outfile );
120 catch ( final IOException e ) {
121 ForesterUtil.fatalError( PRG_NAME, "failed to write output [" + e.getMessage() + "]" );
127 private static void obtainSeqInformation( final Phylogeny phy ) throws IOException {
128 SequenceDbWsTools.obtainSeqInformation( phy, true, true, SequenceDbWsTools.DEFAULT_LINES_TO_RETURN );
131 private static void inferTaxonomyFromDescendents( final Phylogeny phy ) throws IOException,
132 AncestralTaxonomyInferenceException {
133 AncestralTaxonomyInference.inferTaxonomyFromDescendents( phy );
136 private static void argumentsError() {
137 System.out.println( annotator.PRG_NAME + " <indir> <outdir>" );
138 System.out.println();