73afe58b3f3835c89c43a8c6197c9c7157a658e8
[jalview.git] / forester / java / src / org / forester / archaeopteryx / Archaeopteryx.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.archaeopteryx;
27
28 import java.io.File;
29
30 import org.forester.io.parsers.PhylogenyParser;
31 import org.forester.io.parsers.nexus.NexusPhylogeniesParser;
32 import org.forester.io.parsers.nhx.NHXParser;
33 import org.forester.io.parsers.phyloxml.PhyloXmlParser;
34 import org.forester.io.parsers.util.ParserUtils;
35 import org.forester.phylogeny.Phylogeny;
36 import org.forester.phylogeny.PhylogenyMethods;
37 import org.forester.util.ForesterUtil;
38
39
40 public final class Archaeopteryx {
41
42     public static MainFrame createApplication( final Phylogeny phylogeny ) {
43         final Phylogeny[] phylogenies = new Phylogeny[ 1 ];
44         phylogenies[ 0 ] = phylogeny;
45         return createApplication( phylogenies, "", "" );
46     }
47
48     public static MainFrame createApplication( final Phylogeny[] phylogenies ) {
49         return createApplication( phylogenies, "", "" );
50     }
51
52     public static MainFrame createApplication( final Phylogeny[] phylogenies,
53                                                final String config_file_name,
54                                                final String title ) {
55         return MainFrameApplication.createInstance( phylogenies, config_file_name, title );
56     }
57
58     public static void main( final String args[] ) {
59         Phylogeny[] phylogenies = null;
60         String config_filename = null;
61         Configuration conf = null;
62         File f = null;
63         try {
64             int filename_index = 0;
65             if ( args.length == 0 ) {
66                 conf = new Configuration( null, false, false, true );
67             }
68             else if ( args.length > 0 ) {
69                 // check for a config file
70                 if ( args[ 0 ].startsWith( "-c" ) ) {
71                     config_filename = args[ 1 ];
72                     filename_index += 2;
73                 }
74                 if ( args[ 0 ].startsWith( "-open" ) ) {
75                     filename_index += 1;
76                 }
77                 conf = new Configuration( config_filename, false, false, true );
78                 if ( args.length > filename_index ) {
79                     f = new File( args[ filename_index ] );
80                     final String err = ForesterUtil.isReadableFile( f );
81                     if ( !ForesterUtil.isEmpty( err ) ) {
82                         ForesterUtil.fatalError( Constants.PRG_NAME, err );
83                     }
84                     boolean nhx_or_nexus = false;
85                     final PhylogenyParser p = ParserUtils.createParserDependingOnFileType( f, conf
86                             .isValidatePhyloXmlAgainstSchema() );
87                     if ( p instanceof NHXParser ) {
88                         nhx_or_nexus = true;
89                         final NHXParser nhx = ( NHXParser ) p;
90                         nhx.setReplaceUnderscores( conf.isReplaceUnderscoresInNhParsing() );
91                         nhx.setIgnoreQuotes( false );
92                         nhx.setTaxonomyExtraction( conf.getTaxonomyExtraction() );
93                     }
94                     else if ( p instanceof NexusPhylogeniesParser ) {
95                         nhx_or_nexus = true;
96                         final NexusPhylogeniesParser nex = ( NexusPhylogeniesParser ) p;
97                         nex.setReplaceUnderscores( conf.isReplaceUnderscoresInNhParsing() );
98                         nex.setIgnoreQuotes( false );
99                     }
100                     else if ( p instanceof PhyloXmlParser ) {
101                         MainFrameApplication.warnIfNotPhyloXmlValidation( conf );
102                     }
103                     phylogenies = PhylogenyMethods.readPhylogenies( p, f );
104                     if ( nhx_or_nexus && conf.isInternalNumberAreConfidenceForNhParsing() ) {
105                         for( final Phylogeny phy : phylogenies ) {
106                             PhylogenyMethods.transferInternalNodeNamesToConfidence( phy );
107                         }
108                     }
109                 }
110             }
111         }
112         catch ( final Exception e ) {
113             ForesterUtil.fatalError( Constants.PRG_NAME, "failed to start: " + e.getLocalizedMessage() );
114         }
115         String title = "";
116         if ( f != null ) {
117             title = f.getName();
118         }
119         File current_dir = null;
120         if ( ( phylogenies != null ) && ( phylogenies.length > 0 ) ) {
121             current_dir = new File( "." );
122         }
123         try {
124             MainFrameApplication.createInstance( phylogenies, conf, title, current_dir );
125         }
126         catch ( final OutOfMemoryError e ) {
127             AptxUtil.outOfMemoryError( e );
128         }
129         catch ( final Exception e ) {
130             AptxUtil.unexpectedException( e );
131         }
132         catch ( final Error e ) {
133             AptxUtil.unexpectedError( e );
134         }
135     }
136 }