8084ee258418f99f1374c0c051ee5bb618f43b58
[jalview.git] / forester / java / src / org / forester / application / decorator.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.application;
27
28 import java.io.File;
29 import java.io.IOException;
30 import java.util.ArrayList;
31 import java.util.List;
32 import java.util.Map;
33
34 import org.forester.io.parsers.PhylogenyParser;
35 import org.forester.io.parsers.util.ParserUtils;
36 import org.forester.io.writers.PhylogenyWriter;
37 import org.forester.phylogeny.Phylogeny;
38 import org.forester.phylogeny.data.Identifier;
39 import org.forester.phylogeny.factories.ParserBasedPhylogenyFactory;
40 import org.forester.phylogeny.factories.PhylogenyFactory;
41 import org.forester.tools.PhylogenyDecorator;
42 import org.forester.tools.PhylogenyDecorator.FIELD;
43 import org.forester.util.BasicTable;
44 import org.forester.util.BasicTableParser;
45 import org.forester.util.CommandLineArguments;
46 import org.forester.util.ForesterUtil;
47
48 public final class decorator {
49
50     private static final String SEQUENCE_NAME_FIELD                     = "s";
51     private static final String TAXONOMY_CODE_FIELD                     = "c";
52     private static final String TAXONOMY_SCIENTIFIC_NAME_FIELD          = "sn";
53     private static final String DS_FILED                                = "d";
54     private static final String SEQUENCE_ANNOTATION_DESC                = "a";
55     private static final String NODE_NAME_FIELD                         = "n";
56     final static private String PICKY_OPTION                            = "p";
57     final static private String FIELD_OPTION                            = "f";
58     final static private String TRIM_AFTER_TILDE_OPTION                 = "t";
59     final static private String TREE_NAME_OPTION                        = "pn";
60     final static private String TREE_ID_OPTION                          = "pi";
61     final static private String TREE_DESC_OPTION                        = "pd";
62     final static private String EXTRACT_BRACKETED_SCIENTIC_NAME_OPTION  = "sn";
63     final static private String EXTRACT_BRACKETED_TAXONOMIC_CODE_OPTION = "tc";
64     final static private String PROCESS_NAME_INTELLIGENTLY_OPTION       = "x";
65     final static private String PROCESS_SIMILAR_TO_OPTION               = "xs";
66     final static private String CUT_NAME_AFTER_FIRST_SPACE_OPTION       = "c";
67     final static private String ALLOW_REMOVAL_OF_CHARS_OPTION           = "r";
68     final static private String ADVANCED_TABLE_OPTION                   = "table";
69     final static private String KEY_COLUMN                              = "k";
70     final static private String VALUE_COLUMN                            = "v";
71     final static private String MAPPING_FILE_SEPARATOR_OPTION           = "s";
72     final static private String MAPPING_FILE_SEPARATOR_DEFAULT          = ": ";
73     final static private String PRG_NAME                                = "decorator";
74     final static private String PRG_VERSION                             = "1.12";
75     final static private String PRG_DATE                                = "2012.12.21";
76
77     public static void main( final String args[] ) {
78         ForesterUtil.printProgramInformation( decorator.PRG_NAME, decorator.PRG_VERSION, decorator.PRG_DATE );
79         if ( ( args.length < 4 ) || ( args.length > 12 ) ) {
80             decorator.argumentsError();
81         }
82         CommandLineArguments cla = null;
83         try {
84             cla = new CommandLineArguments( args );
85         }
86         catch ( final Exception e ) {
87             ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
88         }
89         if ( ( cla.getNumberOfNames() < 3 ) || ( cla.getNumberOfNames() > 4 ) ) {
90             decorator.argumentsError();
91         }
92         final File phylogenies_infile = cla.getFile( 0 );
93         final File mapping_infile = cla.getFile( 1 );
94         final File phylogenies_outfile = cla.getFile( 2 );
95         if ( phylogenies_outfile.exists() ) {
96             ForesterUtil.fatalError( PRG_NAME, "[" + phylogenies_outfile + "] already exists" );
97         }
98         final List<String> allowed_options = new ArrayList<String>();
99         allowed_options.add( decorator.ADVANCED_TABLE_OPTION );
100         allowed_options.add( decorator.PICKY_OPTION );
101         allowed_options.add( decorator.FIELD_OPTION );
102         allowed_options.add( decorator.PROCESS_NAME_INTELLIGENTLY_OPTION );
103         allowed_options.add( decorator.PROCESS_SIMILAR_TO_OPTION );
104         allowed_options.add( decorator.CUT_NAME_AFTER_FIRST_SPACE_OPTION );
105         allowed_options.add( decorator.ALLOW_REMOVAL_OF_CHARS_OPTION );
106         allowed_options.add( decorator.KEY_COLUMN );
107         allowed_options.add( decorator.VALUE_COLUMN );
108         allowed_options.add( decorator.MAPPING_FILE_SEPARATOR_OPTION );
109         allowed_options.add( decorator.EXTRACT_BRACKETED_SCIENTIC_NAME_OPTION );
110         allowed_options.add( decorator.EXTRACT_BRACKETED_TAXONOMIC_CODE_OPTION );
111         allowed_options.add( decorator.TREE_NAME_OPTION );
112         allowed_options.add( decorator.TREE_ID_OPTION );
113         allowed_options.add( decorator.TREE_DESC_OPTION );
114         allowed_options.add( decorator.TRIM_AFTER_TILDE_OPTION );
115         final String dissallowed_options = cla.validateAllowedOptionsAsString( allowed_options );
116         if ( dissallowed_options.length() > 0 ) {
117             ForesterUtil.fatalError( decorator.PRG_NAME, "unknown option(s): " + dissallowed_options );
118         }
119         final boolean advanced_table = cla.isOptionSet( decorator.ADVANCED_TABLE_OPTION );
120         if ( !advanced_table ) {
121             final List<String> mandatory_options = new ArrayList<String>();
122             mandatory_options.add( decorator.FIELD_OPTION );
123             final String missing_options = cla.validateMandatoryOptionsAsString( mandatory_options );
124             if ( missing_options.length() > 0 ) {
125                 ForesterUtil.fatalError( decorator.PRG_NAME, "missing option(s): " + missing_options );
126             }
127         }
128         final boolean picky = cla.isOptionSet( decorator.PICKY_OPTION );
129         String separator = decorator.MAPPING_FILE_SEPARATOR_DEFAULT;
130         if ( cla.isOptionSet( decorator.MAPPING_FILE_SEPARATOR_OPTION ) ) {
131             if ( advanced_table ) {
132                 argumentsError();
133             }
134             separator = cla.getOptionValue( decorator.MAPPING_FILE_SEPARATOR_OPTION );
135         }
136         int key_column = 0;
137         int value_column = 1;
138         String field_str = "";
139         FIELD field = FIELD.NODE_NAME;
140         int numbers_of_chars_allowed_to_remove_if_not_found_in_map = -1;
141         boolean cut_name_after_space = false;
142         boolean process_name_intelligently = false;
143         boolean process_similar_to = false;
144         boolean extract_bracketed_scientific_name = false;
145         boolean extract_bracketed_tax_code = false;
146         boolean trim_after_tilde = false;
147         String tree_name = "";
148         String tree_id = "";
149         String tree_desc = "";
150         try {
151             if ( cla.isOptionSet( decorator.TREE_NAME_OPTION ) ) {
152                 tree_name = cla.getOptionValueAsCleanString( decorator.TREE_NAME_OPTION );
153             }
154             if ( cla.isOptionSet( decorator.TREE_ID_OPTION ) ) {
155                 tree_id = cla.getOptionValueAsCleanString( decorator.TREE_ID_OPTION );
156             }
157             if ( cla.isOptionSet( decorator.TREE_DESC_OPTION ) ) {
158                 tree_desc = cla.getOptionValueAsCleanString( decorator.TREE_DESC_OPTION );
159             }
160             if ( cla.isOptionSet( decorator.EXTRACT_BRACKETED_SCIENTIC_NAME_OPTION ) ) {
161                 if ( advanced_table ) {
162                     argumentsError();
163                 }
164                 extract_bracketed_scientific_name = true;
165             }
166             if ( cla.isOptionSet( decorator.EXTRACT_BRACKETED_TAXONOMIC_CODE_OPTION ) ) {
167                 if ( advanced_table ) {
168                     argumentsError();
169                 }
170                 extract_bracketed_tax_code = true;
171             }
172             if ( cla.isOptionSet( decorator.KEY_COLUMN ) ) {
173                 if ( advanced_table ) {
174                     argumentsError();
175                 }
176                 key_column = cla.getOptionValueAsInt( decorator.KEY_COLUMN );
177             }
178             if ( cla.isOptionSet( decorator.VALUE_COLUMN ) ) {
179                 if ( advanced_table ) {
180                     argumentsError();
181                 }
182                 value_column = cla.getOptionValueAsInt( decorator.VALUE_COLUMN );
183             }
184             if ( cla.isOptionSet( decorator.CUT_NAME_AFTER_FIRST_SPACE_OPTION ) ) {
185                 if ( advanced_table ) {
186                     argumentsError();
187                 }
188                 cut_name_after_space = true;
189             }
190             if ( cla.isOptionSet( decorator.PROCESS_NAME_INTELLIGENTLY_OPTION ) ) {
191                 if ( advanced_table ) {
192                     argumentsError();
193                 }
194                 process_name_intelligently = true;
195             }
196             if ( cla.isOptionSet( decorator.PROCESS_SIMILAR_TO_OPTION ) ) {
197                 if ( advanced_table ) {
198                     argumentsError();
199                 }
200                 process_similar_to = true;
201             }
202             if ( cla.isOptionSet( decorator.TRIM_AFTER_TILDE_OPTION ) ) {
203                 if ( advanced_table ) {
204                     argumentsError();
205                 }
206                 trim_after_tilde = true;
207             }
208             if ( cla.isOptionSet( decorator.ALLOW_REMOVAL_OF_CHARS_OPTION ) ) {
209                 numbers_of_chars_allowed_to_remove_if_not_found_in_map = cla
210                         .getOptionValueAsInt( decorator.ALLOW_REMOVAL_OF_CHARS_OPTION );
211             }
212             if ( cla.isOptionSet( decorator.FIELD_OPTION ) ) {
213                 field_str = cla.getOptionValue( decorator.FIELD_OPTION );
214                 if ( field_str.equals( NODE_NAME_FIELD ) ) {
215                     field = FIELD.NODE_NAME;
216                 }
217                 else if ( field_str.equals( SEQUENCE_ANNOTATION_DESC ) ) {
218                     field = FIELD.SEQUENCE_ANNOTATION_DESC;
219                 }
220                 else if ( field_str.equals( DS_FILED ) ) {
221                     field = FIELD.DOMAIN_STRUCTURE;
222                     extract_bracketed_scientific_name = false;
223                     extract_bracketed_tax_code = false;
224                 }
225                 else if ( field_str.equals( TAXONOMY_CODE_FIELD ) ) {
226                     field = FIELD.TAXONOMY_CODE;
227                 }
228                 else if ( field_str.equals( SEQUENCE_NAME_FIELD ) ) {
229                     field = FIELD.SEQUENCE_NAME;
230                 }
231                 else if ( field_str.equals( TAXONOMY_SCIENTIFIC_NAME_FIELD ) ) {
232                     field = FIELD.TAXONOMY_SCIENTIFIC_NAME;
233                     extract_bracketed_scientific_name = false;
234                     extract_bracketed_tax_code = false;
235                 }
236                 else {
237                     ForesterUtil.fatalError( decorator.PRG_NAME, "unknown value for \"" + decorator.FIELD_OPTION
238                             + "\" option: \"" + field_str + "\"" );
239                 }
240             }
241         }
242         catch ( final Exception e ) {
243             ForesterUtil.fatalError( decorator.PRG_NAME, "error in command line: " + e.getMessage() );
244         }
245         if ( ( field != FIELD.NODE_NAME ) && ( cut_name_after_space || process_name_intelligently ) ) {
246             ForesterUtil.fatalError( decorator.PRG_NAME, "attempt to use -x or -c option without -f=n" );
247         }
248         if ( ( field != FIELD.NODE_NAME ) && process_similar_to ) {
249             ForesterUtil.fatalError( decorator.PRG_NAME, "attempt to use -" + decorator.PROCESS_SIMILAR_TO_OPTION
250                     + " option without -f=n" );
251         }
252         if ( cut_name_after_space && process_name_intelligently ) {
253             ForesterUtil.fatalError( decorator.PRG_NAME, "attempt to use -x and -c option together" );
254         }
255         if ( process_similar_to && process_name_intelligently ) {
256             ForesterUtil.fatalError( decorator.PRG_NAME, "attempt to use -" + decorator.PROCESS_SIMILAR_TO_OPTION
257                     + " and -x option together" );
258         }
259         if ( process_similar_to && cut_name_after_space ) {
260             ForesterUtil.fatalError( decorator.PRG_NAME, "attempt to use -" + decorator.PROCESS_SIMILAR_TO_OPTION
261                     + " and -c option together" );
262         }
263         if ( extract_bracketed_scientific_name && extract_bracketed_tax_code ) {
264             argumentsError();
265         }
266         Phylogeny[] phylogenies = null;
267         try {
268             final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
269             final PhylogenyParser pp = ParserUtils.createParserDependingOnFileType( phylogenies_infile, true );
270             phylogenies = factory.create( phylogenies_infile, pp );
271         }
272         catch ( final Exception e ) {
273             ForesterUtil.fatalError( decorator.PRG_NAME, "failed to read phylgenies from [" + phylogenies_infile
274                     + "] [" + e.getMessage() + "]" );
275         }
276         Map<String, String> map = null;
277         if ( !advanced_table ) {
278             BasicTable<String> mapping_table = null;
279             try {
280                 mapping_table = BasicTableParser.parse( mapping_infile, separator, false, true );
281             }
282             catch ( final Exception e ) {
283                 ForesterUtil.fatalError( decorator.PRG_NAME,
284                                          "failed to read [" + mapping_infile + "] [" + e.getMessage() + "]" );
285             }
286             if ( ( key_column < 0 ) || ( key_column >= mapping_table.getNumberOfColumns() ) ) {
287                 ForesterUtil.fatalError( decorator.PRG_NAME, "illegal value for key column" );
288             }
289             if ( ( value_column < 0 ) || ( value_column >= mapping_table.getNumberOfColumns() ) ) {
290                 ForesterUtil.fatalError( decorator.PRG_NAME, "illegal value for value column" );
291             }
292             map = mapping_table.getColumnsAsMap( key_column, value_column );
293         }
294         if ( !ForesterUtil.isEmpty( tree_name ) || !ForesterUtil.isEmpty( tree_id )
295                 || !ForesterUtil.isEmpty( tree_desc ) ) {
296             if ( ( phylogenies.length > 1 )
297                     && ( !ForesterUtil.isEmpty( tree_name ) || !ForesterUtil.isEmpty( tree_id ) ) ) {
298                 ForesterUtil.fatalError( decorator.PRG_NAME,
299                                          "attempt to set same name or id on more than one phylogeny" );
300             }
301             if ( !ForesterUtil.isEmpty( tree_name ) ) {
302                 phylogenies[ 0 ].setName( tree_name );
303             }
304             if ( !ForesterUtil.isEmpty( tree_id ) ) {
305                 final String[] s_ary = tree_id.split( ":" );
306                 phylogenies[ 0 ].setIdentifier( new Identifier( s_ary[ 1 ], s_ary[ 0 ] ) );
307             }
308             if ( !ForesterUtil.isEmpty( tree_desc ) ) {
309                 for( final Phylogeny phylogenie : phylogenies ) {
310                     phylogenie.setDescription( tree_desc );
311                 }
312             }
313         }
314         try {
315             if ( advanced_table ) {
316                 Map<String, Map<String, String>> table = null;
317                 try {
318                     table = PhylogenyDecorator.parseMappingTable( mapping_infile );
319                 }
320                 catch ( final IOException e ) {
321                     ForesterUtil.fatalError( decorator.PRG_NAME,
322                                              "failed to read \"" + mapping_infile + "\" [" + e.getMessage() + "]" );
323                 }
324                 PhylogenyDecorator.decorate( phylogenies,
325                                              table,
326                                              picky,
327                                              numbers_of_chars_allowed_to_remove_if_not_found_in_map );
328             }
329             else {
330                 PhylogenyDecorator.decorate( phylogenies,
331                                              map,
332                                              field,
333                                              extract_bracketed_scientific_name,
334                                              extract_bracketed_tax_code,
335                                              picky,
336                                              cut_name_after_space,
337                                              process_name_intelligently,
338                                              process_similar_to,
339                                              numbers_of_chars_allowed_to_remove_if_not_found_in_map,
340                                              trim_after_tilde );
341             }
342         }
343         catch ( final NullPointerException e ) {
344             ForesterUtil.unexpectedFatalError( decorator.PRG_NAME, e );
345         }
346         catch ( final Exception e ) {
347             ForesterUtil.fatalError( decorator.PRG_NAME, e.getLocalizedMessage() );
348         }
349         try {
350             final PhylogenyWriter w = new PhylogenyWriter();
351             w.toPhyloXML( phylogenies, 0, phylogenies_outfile, ForesterUtil.getLineSeparator() );
352         }
353         catch ( final IOException e ) {
354             ForesterUtil.fatalError( decorator.PRG_NAME, "failed to write output [" + e.getMessage() + "]" );
355         }
356         System.out.println();
357         ForesterUtil.programMessage( PRG_NAME, "wrote: " + phylogenies_outfile );
358         ForesterUtil.programMessage( PRG_NAME, "OK." );
359     }
360
361     private static void argumentsError() {
362         System.out.println();
363         System.out.println( decorator.PRG_NAME + " -" + ADVANCED_TABLE_OPTION + " | -f=<c> <phylogenies infile> "
364                 + "[mapping table file] <phylogenies outfile>" );
365         System.out.println();
366         System.out.println( "options:" );
367         System.out.println();
368         System.out.println( " -" + ADVANCED_TABLE_OPTION + " : table instead of one to one map (-f=<c>)" );
369         System.out.println( " -r=<n> : allow to remove up to n characters from the end of the names" );
370         System.out.println( "          in phylogenies infile if not found (in map) otherwise" );
371         System.out.println( " -p     : picky, fails if node name not found in mapping table" );
372         System.out.println( " -" + TREE_NAME_OPTION + "=<s>: name for the phylogeny" );
373         System.out.println( " -" + TREE_ID_OPTION + "=<s>: identifier for the phylogeny (in the form provider:value)" );
374         System.out.println( " -" + TREE_DESC_OPTION + "=<s>: description for phylogenies" );
375         System.out.println();
376         System.out.println();
377         System.out.println( "advanced options, only available if -" + ADVANCED_TABLE_OPTION + " is not used:" );
378         System.out.println();
379         System.out.println( " -f=<c> : field to be replaced: " + NODE_NAME_FIELD + " : node name" );
380         System.out.println( "                                " + SEQUENCE_ANNOTATION_DESC
381                 + " : sequence annotation description" );
382         System.out.println( "                                " + DS_FILED + " : domain structure" );
383         System.out.println( "                                " + TAXONOMY_CODE_FIELD + " : taxonomy code" );
384         System.out.println( "                                " + TAXONOMY_SCIENTIFIC_NAME_FIELD
385                 + ": taxonomy scientific name" );
386         System.out.println( "                                " + SEQUENCE_NAME_FIELD + " : sequence name" );
387         System.out.println( " -k=<n> : key column in mapping table (0 based)," );
388         System.out.println( "          names of the node to be decorated - default is 0" );
389         System.out.println( " -v=<n> : value column in mapping table (0 based)," );
390         System.out.println( "          data which with to decorate - default is 1" );
391         System.out.println( " -" + EXTRACT_BRACKETED_SCIENTIC_NAME_OPTION
392                 + "    : to extract bracketed scientific names, e.g. [Nematostella vectensis]" );
393         System.out.println( " -" + EXTRACT_BRACKETED_TAXONOMIC_CODE_OPTION
394                 + "    : to extract bracketed taxonomic codes, e.g. [NEMVE]" );
395         System.out.println( " -s=<c> : column separator in mapping file, default is \""
396                 + decorator.MAPPING_FILE_SEPARATOR_DEFAULT + "\"" );
397         System.out.println( " -x     : process name \"intelligently\" (only for -f=n)" );
398         System.out.println( " -" + decorator.PROCESS_SIMILAR_TO_OPTION
399                 + "    : process name \"intelligently\" and process information after \"similar to\" (only for -f=n)" );
400         System.out.println( " -c     : cut name after first space (only for -f=n)" );
401         System.out.println( " -" + decorator.TRIM_AFTER_TILDE_OPTION
402                 + "     : trim node name to be replaced after tilde" );
403         System.out.println();
404         System.exit( -1 );
405     }
406 }