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