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