new tool
[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.List;
32
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;
43
44 public final class annotator {
45
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";
49
50     public static void main( final String args[] ) {
51         ForesterUtil.printProgramInformation( annotator.PRG_NAME, annotator.PRG_VERSION, annotator.PRG_DATE );
52         System.out.println();
53         if ( ( args.length != 2 ) ) {
54             annotator.argumentsError();
55         }
56         CommandLineArguments cla = null;
57         try {
58             cla = new CommandLineArguments( args );
59         }
60         catch ( final Exception e ) {
61             ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
62         }
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" );
67         }
68         if ( !outdir.isDirectory() ) {
69             ForesterUtil.fatalError( PRG_NAME, outdir + " is not a directory" );
70         }
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" ) ) {
75                 infiles.add( file );
76             }
77         }
78         int c = 0;
79         for( final File infile : infiles ) {
80             System.out.println( ++c + "/" + infiles.size() + ": " + infile );
81             Phylogeny phy = null;
82             try {
83                 final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
84                 final Phylogeny[] phylogenies = factory.create( infile,
85                                                                 PhyloXmlParser.createPhyloXmlParserXsdValidating() );
86                 phy = phylogenies[ 0 ];
87             }
88             catch ( final Exception e ) {
89                 ForesterUtil.fatalError( PRG_NAME, "failed to read phylgenies from [" + infile + "] [" + e.getMessage()
90                         + "]" );
91             }
92             try {
93                 obtainSeqInformation( phy );
94             }
95             catch ( final IOException e ) {
96                 ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
97             }
98             try {
99                 inferTaxonomyFromDescendents( phy );
100             }
101             catch ( final IOException e ) {
102                 ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
103             }
104             catch ( final AncestralTaxonomyInferenceException e ) {
105                 ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
106             }
107             final File outfile = new File( outdir.getAbsolutePath().toString() + "/" + infile.getName() );
108             try {
109                 final PhylogenyWriter w = new PhylogenyWriter();
110                 w.toPhyloXML( phy, 0, outfile );
111             }
112             catch ( final IOException e ) {
113                 ForesterUtil.fatalError( PRG_NAME, "failed to write output [" + e.getMessage() + "]" );
114             }
115         }
116     }
117
118     private static void obtainSeqInformation( final Phylogeny phy ) throws IOException {
119         SequenceDbWsTools.obtainSeqInformation( phy, true, true, SequenceDbWsTools.DEFAULT_LINES_TO_RETURN );
120     }
121
122     private static void inferTaxonomyFromDescendents( final Phylogeny phy ) throws IOException,
123             AncestralTaxonomyInferenceException {
124         AncestralTaxonomyInference.inferTaxonomyFromDescendents( phy );
125     }
126
127     private static void argumentsError() {
128         System.out.println( annotator.PRG_NAME + " <indir> <outdir>" );
129         System.out.println();
130         System.exit( -1 );
131     }
132 }