in progress
[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.phylogeny.Phylogeny;
35 import org.forester.phylogeny.factories.ParserBasedPhylogenyFactory;
36 import org.forester.util.ForesterUtil;
37
38 //
39 // java -javaagent:shiftone-jrat.jar -cp
40 // $HOME/SOFTWARE_DEV/ECLIPSE_WORKSPACE/forester-atv/java/forester.jar:.
41 // org.forester.archaeopteryx.Archaeopteryx
42 // -c $HOME/SOFTWARE_DEV/ECLIPSE_WORKSPACE/forester-atv/_aptx_configuration_file
43 //
44 public final class Archaeopteryx {
45
46     private final static boolean TEST = false; //TODO remove me!
47
48     public static MainFrame createApplication( final Phylogeny phylogeny ) {
49         final Phylogeny[] phylogenies = new Phylogeny[ 1 ];
50         phylogenies[ 0 ] = phylogeny;
51         return createApplication( phylogenies, "", "" );
52     }
53
54     public static MainFrame createApplication( final Phylogeny[] phylogenies ) {
55         return createApplication( phylogenies, "", "" );
56     }
57
58     public static MainFrame createApplication( final Phylogeny[] phylogenies,
59                                                final String config_file_name,
60                                                final String title ) {
61         return MainFrameApplication.createInstance( phylogenies, config_file_name, title );
62     }
63
64     public static void main( final String args[] ) {
65         Phylogeny[] phylogenies = null;
66         String config_filename = null;
67         Configuration conf = null;
68         File f = null;
69         try {
70             int filename_index = 0;
71             if ( args.length == 0 ) {
72                 conf = new Configuration( null, false, false );
73             }
74             else if ( args.length > 0 ) {
75                 // check for a config file
76                 if ( args[ 0 ].startsWith( "-c" ) ) {
77                     config_filename = args[ 1 ];
78                     filename_index += 2;
79                 }
80                 if ( args[ 0 ].startsWith( "-open" ) ) {
81                     filename_index += 1;
82                 }
83                 conf = new Configuration( config_filename, false, false );
84                 if ( args.length > filename_index ) {
85                     f = new File( args[ filename_index ] );
86                     final String err = ForesterUtil.isReadableFile( f );
87                     if ( !ForesterUtil.isEmpty( err ) ) {
88                         ForesterUtil.fatalError( Constants.PRG_NAME, err );
89                     }
90                     boolean nhx_or_nexus = false;
91                     final PhylogenyParser p = ForesterUtil.createParserDependingOnFileType( f, conf
92                             .isValidatePhyloXmlAgainstSchema() );
93                     if ( p instanceof NHXParser ) {
94                         nhx_or_nexus = true;
95                         final NHXParser nhx = ( NHXParser ) p;
96                         nhx.setReplaceUnderscores( conf.isReplaceUnderscoresInNhParsing() );
97                         nhx.setIgnoreQuotes( false );
98                         ForesterUtil.TAXONOMY_EXTRACTION te = ForesterUtil.TAXONOMY_EXTRACTION.NO;
99                         if ( conf.isExtractPfamTaxonomyCodesInNhParsing() ) {
100                             te = ForesterUtil.TAXONOMY_EXTRACTION.PFAM_STYLE_ONLY;
101                         }
102                         nhx.setTaxonomyExtraction( te );
103                     }
104                     else if ( p instanceof NexusPhylogeniesParser ) {
105                         nhx_or_nexus = true;
106                         final NexusPhylogeniesParser nex = ( NexusPhylogeniesParser ) p;
107                         nex.setReplaceUnderscores( conf.isReplaceUnderscoresInNhParsing() );
108                         nex.setIgnoreQuotes( false );
109                     }
110                     else if ( p instanceof PhyloXmlParser ) {
111                         MainFrameApplication.warnIfNotPhyloXmlValidation( conf );
112                     }
113                     phylogenies = ForesterUtil.readPhylogenies( p, f );
114                     if ( nhx_or_nexus && conf.isInternalNumberAreConfidenceForNhParsing() ) {
115                         for( final Phylogeny phy : phylogenies ) {
116                             ForesterUtil.transferInternalNodeNamesToConfidence( phy );
117                         }
118                     }
119                     //
120                     //                                        Phylogeny py = phylogenies[ 0 ];
121                     //                                        for( final PhylogenyNodeIterator iter = py.iteratorExternalForward(); iter.hasNext(); ) {
122                     //                                            final PhylogenyNode node = iter.next();
123                     //                                            System.out.println( node.getNodeData().getTaxonomy().getScientificName() + "\t"
124                     //                                                    + node.getNodeData().getBinaryCharacters().getPresentCount() );
125                     //                                        }
126                     //                                        for( final PhylogenyNodeIterator iter = py.iteratorPreorder(); iter.hasNext(); ) {
127                     //                                            final PhylogenyNode node = iter.next();
128                     //                                            if ( !node.isExternal() ) {
129                     //                                                System.out.println( node.getNodeData().getTaxonomy().getScientificName() + "\t"
130                     //                                                        + node.getNodeData().getBinaryCharacters().getPresentCount() );
131                     //                                            }
132                     //                                        }
133                     //
134                 }
135             }
136         }
137         catch ( final Exception e ) {
138             ForesterUtil.fatalError( Constants.PRG_NAME, "failed to start: " + e.getLocalizedMessage() );
139         }
140         String title = "";
141         if ( f != null ) {
142             title = f.getName();
143         }
144         try {
145             String s = "";
146             if ( TEST ) {
147                 s = "/home/czmasek/888.xml";
148                 if ( ForesterUtil.isReadableFile( s ) != null ) {
149                     s = "/Users/zma/888.xml";
150                     if ( ForesterUtil.isReadableFile( s ) != null ) {
151                         s = "C:\\888.xml";
152                         if ( ForesterUtil.isReadableFile( s ) != null ) {
153                             s = "C:\\Documents and Settings\\czmasek\\";
154                         }
155                     }
156                 }
157             }
158             if ( !TEST ) {
159                 MainFrameApplication.createInstance( phylogenies, conf, title );
160             }
161             else {
162                 MainFrameApplication.createInstance( ParserBasedPhylogenyFactory.getInstance()
163                                                              .create( s, new PhyloXmlParser() ),
164                                                      conf,
165                                                      title );
166             }
167         }
168         // catch ( final IOException ex ) {
169         //     ForesterUtil.fatalError( Constants.PRG_NAME, "failed to start: " + ex.getLocalizedMessage() );
170         // }
171         catch ( final Exception ex ) {
172             Util.unexpectedException( ex );
173         }
174         catch ( final Error err ) {
175             Util.unexpectedError( err );
176         }
177     }
178 }