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