2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
5 // Copyright (C) 2008-2009 Christian M. Zmasek
6 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
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.
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.
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
23 // Contact: phylosoft @ gmail . com
24 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
26 package org.forester.tools;
29 import java.io.IOException;
30 import java.util.HashMap;
32 import java.util.regex.Matcher;
34 import org.forester.io.parsers.nhx.NHXFormatException;
35 import org.forester.io.parsers.phyloxml.PhyloXmlDataFormatException;
36 import org.forester.io.parsers.util.ParserUtils;
37 import org.forester.phylogeny.Phylogeny;
38 import org.forester.phylogeny.PhylogenyNode;
39 import org.forester.phylogeny.data.Accession;
40 import org.forester.phylogeny.data.Annotation;
41 import org.forester.phylogeny.data.DomainArchitecture;
42 import org.forester.phylogeny.data.Identifier;
43 import org.forester.phylogeny.data.Sequence;
44 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
45 import org.forester.util.BasicTable;
46 import org.forester.util.BasicTableParser;
47 import org.forester.util.ForesterUtil;
49 public final class PhylogenyDecorator {
51 // From evoruby/lib/evo/apps/tseq_taxonomy_processor.rb:
52 final private static String TP_TAXONOMY_CODE = "TAXONOMY_CODE";
53 final private static String TP_TAXONOMY_ID = "TAXONOMY_ID";
54 final private static String TP_TAXONOMY_ID_PROVIDER = "TAXONOMY_ID_PROVIDER";
55 final private static String TP_TAXONOMY_SN = "TAXONOMY_SN";
56 final private static String TP_TAXONOMY_CN = "TAXONOMY_CN";
57 final private static String TP_TAXONOMY_SYN = "TAXONOMY_SYN";
58 final private static String TP_SEQ_SYMBOL = "SEQ_SYMBOL";
59 final private static String TP_SEQ_ACCESSION = "SEQ_ACCESSION";
60 final private static String TP_SEQ_ACCESSION_SOURCE = "SEQ_ACCESSION_SOURCE";
61 final private static String TP_SEQ_ANNOTATION_DESC = "SEQ_ANNOTATION_DESC";
62 final private static String TP_SEQ_ANNOTATION_REF = "SEQ_ANNOTATION_REF";
63 final private static String TP_SEQ_MOL_SEQ = "SEQ_MOL_SEQ";
64 final private static String TP_SEQ_NAME = "SEQ_NAME";
65 final private static String TP_NODE_NAME = "NODE_NAME";
66 public final static boolean SANITIZE = false;
67 public final static boolean VERBOSE = true;
69 private PhylogenyDecorator() {
73 public static void decorate( final Phylogeny phylogeny,
74 final Map<String, Map<String, String>> map,
76 final int numbers_of_chars_allowed_to_remove_if_not_found_in_map )
77 throws IllegalArgumentException, PhyloXmlDataFormatException {
78 for( final PhylogenyNodeIterator iter = phylogeny.iteratorPostorder(); iter.hasNext(); ) {
79 final PhylogenyNode node = iter.next();
80 final String name = node.getName();
81 if ( !ForesterUtil.isEmpty( name ) ) {
82 if ( map.containsKey( name ) || ( numbers_of_chars_allowed_to_remove_if_not_found_in_map > 0 ) ) {
83 Map<String, String> new_values = map.get( name );
85 while ( ( new_values == null ) && ( numbers_of_chars_allowed_to_remove_if_not_found_in_map > 0 )
86 && ( x <= numbers_of_chars_allowed_to_remove_if_not_found_in_map ) ) {
87 new_values = map.get( name.substring( 0, name.length() - x ) );
90 if ( new_values != null ) {
91 if ( new_values.containsKey( TP_TAXONOMY_CODE ) ) {
92 ForesterUtil.ensurePresenceOfTaxonomy( node );
93 node.getNodeData().getTaxonomy().setTaxonomyCode( new_values.get( TP_TAXONOMY_CODE ) );
95 if ( new_values.containsKey( TP_TAXONOMY_ID )
96 && new_values.containsKey( TP_TAXONOMY_ID_PROVIDER ) ) {
97 ForesterUtil.ensurePresenceOfTaxonomy( node );
100 .setIdentifier( new Identifier( new_values.get( TP_TAXONOMY_ID ),
101 new_values.get( TP_TAXONOMY_ID_PROVIDER ) ) );
103 else if ( new_values.containsKey( TP_TAXONOMY_ID ) ) {
104 ForesterUtil.ensurePresenceOfTaxonomy( node );
105 node.getNodeData().getTaxonomy()
106 .setIdentifier( new Identifier( new_values.get( TP_TAXONOMY_ID ) ) );
108 if ( new_values.containsKey( TP_TAXONOMY_SN ) ) {
109 ForesterUtil.ensurePresenceOfTaxonomy( node );
110 node.getNodeData().getTaxonomy().setScientificName( new_values.get( TP_TAXONOMY_SN ) );
112 if ( new_values.containsKey( TP_TAXONOMY_CN ) ) {
113 ForesterUtil.ensurePresenceOfTaxonomy( node );
114 node.getNodeData().getTaxonomy().setCommonName( new_values.get( TP_TAXONOMY_CN ) );
116 if ( new_values.containsKey( TP_TAXONOMY_SYN ) ) {
117 ForesterUtil.ensurePresenceOfTaxonomy( node );
118 node.getNodeData().getTaxonomy().getSynonyms().add( new_values.get( TP_TAXONOMY_SYN ) );
120 if ( new_values.containsKey( TP_SEQ_ACCESSION )
121 && new_values.containsKey( TP_SEQ_ACCESSION_SOURCE ) ) {
122 ForesterUtil.ensurePresenceOfSequence( node );
125 .setAccession( new Accession( new_values.get( TP_SEQ_ACCESSION ),
126 new_values.get( TP_SEQ_ACCESSION_SOURCE ) ) );
128 if ( new_values.containsKey( TP_SEQ_ANNOTATION_DESC ) ) {
129 ForesterUtil.ensurePresenceOfSequence( node );
130 final Annotation ann = new Annotation();
131 ann.setDesc( new_values.get( TP_SEQ_ANNOTATION_DESC ) );
132 node.getNodeData().getSequence().addAnnotation( ann );
134 if ( new_values.containsKey( TP_SEQ_ANNOTATION_REF ) ) {
135 ForesterUtil.ensurePresenceOfSequence( node );
136 final Annotation ann = new Annotation( new_values.get( TP_SEQ_ANNOTATION_REF ) );
137 node.getNodeData().getSequence().addAnnotation( ann );
139 if ( new_values.containsKey( TP_SEQ_SYMBOL ) ) {
140 ForesterUtil.ensurePresenceOfSequence( node );
141 node.getNodeData().getSequence().setSymbol( new_values.get( TP_SEQ_SYMBOL ) );
143 if ( new_values.containsKey( TP_SEQ_NAME ) ) {
144 ForesterUtil.ensurePresenceOfSequence( node );
145 node.getNodeData().getSequence().setName( new_values.get( TP_SEQ_NAME ) );
147 if ( new_values.containsKey( TP_SEQ_MOL_SEQ ) ) {
148 ForesterUtil.ensurePresenceOfSequence( node );
149 node.getNodeData().getSequence().setMolecularSequence( new_values.get( TP_SEQ_MOL_SEQ ) );
151 if ( new_values.containsKey( TP_NODE_NAME ) ) {
152 node.setName( new_values.get( TP_NODE_NAME ) );
154 } // if ( new_values != null )
155 } // if ( map.containsKey( name ) || ( numbers_of_chars_allowed_to_remove_if_not_found_in_map > 0 ) )
157 throw new IllegalArgumentException( "\"" + name + "\" not found in name map" );
163 public static void decorate( final Phylogeny phylogeny,
164 final Map<String, String> map,
166 final boolean extract_bracketed_scientific_name,
167 final boolean extract_bracketed_tax_code,
169 final boolean cut_name_after_space,
170 final boolean process_name_intelligently,
171 final boolean process_similar_to,
172 final int numbers_of_chars_allowed_to_remove_if_not_found_in_map,
173 final boolean trim_after_tilde ) throws IllegalArgumentException, NHXFormatException,
174 PhyloXmlDataFormatException {
175 PhylogenyDecorator.decorate( phylogeny,
178 extract_bracketed_scientific_name,
179 extract_bracketed_tax_code,
182 cut_name_after_space,
183 process_name_intelligently,
185 numbers_of_chars_allowed_to_remove_if_not_found_in_map,
195 * maps names (in phylogeny) to new values if intermediate_map is
196 * null otherwise maps intermediate value to new value
199 * @param intermediate_map
200 * maps name (in phylogeny) to a intermediate value
201 * @throws IllegalArgumentException
202 * @throws PhyloXmlDataFormatException
204 public static void decorate( final Phylogeny phylogeny,
205 final Map<String, String> map,
207 final boolean extract_bracketed_scientific_name,
208 final boolean extract_bracketed_tax_code,
210 final Map<String, String> intermediate_map,
211 final boolean cut_name_after_space,
212 final boolean process_name_intelligently,
213 final boolean process_similar_to,
214 final int numbers_of_chars_allowed_to_remove_if_not_found_in_map,
215 final boolean trim_after_tilde ) throws IllegalArgumentException,
216 PhyloXmlDataFormatException {
217 if ( extract_bracketed_scientific_name && ( field == FIELD.TAXONOMY_SCIENTIFIC_NAME ) ) {
218 throw new IllegalArgumentException( "attempt to extract bracketed scientific name together with data field pointing to scientific name" );
220 for( final PhylogenyNodeIterator iter = phylogeny.iteratorPostorder(); iter.hasNext(); ) {
221 final PhylogenyNode node = iter.next();
222 String name = node.getName();
223 String tilde_annotation = null;
224 if ( trim_after_tilde && ( name.indexOf( '~' ) > 0 ) ) {
225 final int ti = name.indexOf( '~' );
226 tilde_annotation = name.substring( ti );
227 name = name.substring( 0, ti );
229 if ( !ForesterUtil.isEmpty( name ) ) {
230 if ( intermediate_map != null ) {
231 name = PhylogenyDecorator.extractIntermediate( intermediate_map, name );
233 if ( map.containsKey( name ) || ( numbers_of_chars_allowed_to_remove_if_not_found_in_map > 0 ) ) {
234 String new_value = map.get( name );
236 while ( ( new_value == null ) && ( numbers_of_chars_allowed_to_remove_if_not_found_in_map > 0 )
237 && ( x <= numbers_of_chars_allowed_to_remove_if_not_found_in_map ) ) {
238 new_value = map.get( name.substring( 0, name.length() - x ) );
241 if ( new_value != null ) {
242 new_value = new_value.trim();
243 new_value.replaceAll( "/\\s+/", " " );
244 if ( extract_bracketed_scientific_name && new_value.endsWith( "]" ) ) {
245 new_value = extractBracketedScientificNames( node, new_value );
247 else if ( extract_bracketed_tax_code
248 && ParserUtils.TAXOMONY_CODE_PATTERN_4.matcher( new_value ).matches() ) {
249 new_value = extractBracketedTaxCodes( node, new_value );
252 case SEQUENCE_ANNOTATION_DESC:
253 if ( PhylogenyDecorator.VERBOSE ) {
254 System.out.println( name + ": " + new_value );
256 if ( !node.getNodeData().isHasSequence() ) {
257 node.getNodeData().setSequence( new Sequence() );
259 final Annotation annotation = new Annotation( "?" );
260 annotation.setDesc( new_value );
261 node.getNodeData().getSequence().addAnnotation( annotation );
263 case DOMAIN_STRUCTURE:
264 if ( PhylogenyDecorator.VERBOSE ) {
265 System.out.println( name + ": " + new_value );
267 if ( !node.getNodeData().isHasSequence() ) {
268 node.getNodeData().setSequence( new Sequence() );
270 node.getNodeData().getSequence()
271 .setDomainArchitecture( new DomainArchitecture( new_value ) );
274 if ( PhylogenyDecorator.VERBOSE ) {
275 System.out.println( name + ": " + new_value );
277 ForesterUtil.ensurePresenceOfTaxonomy( node );
278 node.getNodeData().getTaxonomy().setTaxonomyCode( new_value );
280 case TAXONOMY_SCIENTIFIC_NAME:
281 if ( PhylogenyDecorator.VERBOSE ) {
282 System.out.println( name + ": " + new_value );
284 ForesterUtil.ensurePresenceOfTaxonomy( node );
285 node.getNodeData().getTaxonomy().setScientificName( new_value );
288 if ( trim_after_tilde ) {
289 new_value = addTildeAnnotation( tilde_annotation, new_value );
291 if ( PhylogenyDecorator.VERBOSE ) {
292 System.out.println( name + ": " + new_value );
294 if ( !node.getNodeData().isHasSequence() ) {
295 node.getNodeData().setSequence( new Sequence() );
297 node.getNodeData().getSequence().setName( new_value );
300 if ( PhylogenyDecorator.VERBOSE ) {
301 System.out.print( name + " -> " );
303 if ( cut_name_after_space ) {
304 if ( PhylogenyDecorator.VERBOSE ) {
305 System.out.print( new_value + " -> " );
307 new_value = PhylogenyDecorator.deleteAtFirstSpace( new_value );
309 else if ( process_name_intelligently ) {
310 if ( PhylogenyDecorator.VERBOSE ) {
311 System.out.print( new_value + " -> " );
313 new_value = PhylogenyDecorator.processNameIntelligently( new_value );
315 else if ( process_similar_to ) {
316 if ( PhylogenyDecorator.VERBOSE ) {
317 System.out.print( new_value + " -> " );
319 new_value = PhylogenyDecorator.processSimilarTo( new_value );
321 if ( PhylogenyDecorator.SANITIZE ) {
322 new_value = PhylogenyDecorator.sanitize( new_value );
324 if ( trim_after_tilde ) {
325 new_value = addTildeAnnotation( tilde_annotation, new_value );
327 if ( PhylogenyDecorator.VERBOSE ) {
328 System.out.println( new_value );
330 node.setName( new_value );
333 throw new RuntimeException( "unknown field \"" + field + "\"" );
338 throw new IllegalArgumentException( "\"" + name + "\" not found in name map" );
344 private final static String addTildeAnnotation( final String tilde_annotation, final String new_value ) {
345 if ( ForesterUtil.isEmpty( tilde_annotation ) ) {
348 return new_value + tilde_annotation;
351 public static void decorate( final Phylogeny[] phylogenies,
352 final Map<String, Map<String, String>> map,
354 final int numbers_of_chars_allowed_to_remove_if_not_found_in_map )
355 throws IllegalArgumentException, NHXFormatException, PhyloXmlDataFormatException {
356 for( final Phylogeny phylogenie : phylogenies ) {
358 .decorate( phylogenie, map, picky, numbers_of_chars_allowed_to_remove_if_not_found_in_map );
362 public static void decorate( final Phylogeny[] phylogenies,
363 final Map<String, String> map,
365 final boolean extract_bracketed_scientific_name,
366 final boolean extract_bracketed_tax_code,
368 final boolean cut_name_after_space,
369 final boolean process_name_intelligently,
370 final boolean process_similar_to,
371 final int numbers_of_chars_allowed_to_remove_if_not_found_in_map,
372 final boolean trim_after_tilde ) throws IllegalArgumentException, NHXFormatException,
373 PhyloXmlDataFormatException {
374 for( final Phylogeny phylogenie : phylogenies ) {
375 PhylogenyDecorator.decorate( phylogenie,
378 extract_bracketed_scientific_name,
379 extract_bracketed_tax_code,
381 cut_name_after_space,
382 process_name_intelligently,
384 numbers_of_chars_allowed_to_remove_if_not_found_in_map,
389 public static void decorate( final Phylogeny[] phylogenies,
390 final Map<String, String> map,
392 final boolean extract_bracketed_scientific_name,
393 final boolean extract_bracketed_tax_code,
395 final Map<String, String> intermediate_map,
396 final boolean cut_name_after_space,
397 final boolean process_name_intelligently,
398 final boolean process_similar_to,
399 final int numbers_of_chars_allowed_to_remove_if_not_found_in_map,
400 final boolean trim_after_tilde ) throws IllegalArgumentException, NHXFormatException,
401 PhyloXmlDataFormatException {
402 for( final Phylogeny phylogenie : phylogenies ) {
403 PhylogenyDecorator.decorate( phylogenie,
406 extract_bracketed_scientific_name,
407 extract_bracketed_tax_code,
410 cut_name_after_space,
411 process_name_intelligently,
413 numbers_of_chars_allowed_to_remove_if_not_found_in_map,
418 public static Map<String, Map<String, String>> parseMappingTable( final File mapping_table_file )
420 final Map<String, Map<String, String>> map = new HashMap<String, Map<String, String>>();
421 BasicTable<String> mapping_table = null;
422 mapping_table = BasicTableParser.parse( mapping_table_file, "\t", false, false );
423 for( int row = 0; row < mapping_table.getNumberOfRows(); ++row ) {
424 final Map<String, String> row_map = new HashMap<String, String>();
426 for( int col = 0; col < mapping_table.getNumberOfColumns(); ++col ) {
427 final String table_cell = mapping_table.getValue( col, row );
431 else if ( table_cell != null ) {
432 final String key = table_cell.substring( 0, table_cell.indexOf( ':' ) );
433 final String val = table_cell.substring( table_cell.indexOf( ':' ) + 1, table_cell.length() );
434 row_map.put( key, val );
437 map.put( name, row_map );
442 private static String deleteAtFirstSpace( final String name ) {
443 final int first_space = name.indexOf( " " );
444 if ( first_space > 1 ) {
445 return name.substring( 0, first_space ).trim();
450 private static String extractBracketedScientificNames( final PhylogenyNode node, final String new_value ) {
451 final int i = new_value.lastIndexOf( "[" );
452 final String scientific_name = new_value.substring( i + 1, new_value.length() - 1 );
453 ForesterUtil.ensurePresenceOfTaxonomy( node );
454 node.getNodeData().getTaxonomy().setScientificName( scientific_name );
455 return new_value.substring( 0, i - 1 ).trim();
458 private static String extractBracketedTaxCodes( final PhylogenyNode node, final String new_value ) {
459 final Matcher m = ParserUtils.TAXOMONY_CODE_PATTERN_4.matcher( new_value );
464 ForesterUtil.ensurePresenceOfTaxonomy( node );
466 node.getNodeData().getTaxonomy().setTaxonomyCode( tc );
468 catch ( final PhyloXmlDataFormatException e ) {
469 throw new IllegalArgumentException( "illegal format for taxonomy code: " + tc );
471 return new_value; //TODO //FIXME
474 private static String extractIntermediate( final Map<String, String> intermediate_map, final String name ) {
475 String new_name = null;
476 if ( PhylogenyDecorator.VERBOSE ) {
477 System.out.print( name + " => " );
479 if ( intermediate_map.containsKey( name ) ) {
480 new_name = intermediate_map.get( name );
481 if ( ForesterUtil.isEmpty( new_name ) ) {
482 throw new IllegalArgumentException( "\"" + name + "\" maps to null or empty string in secondary map" );
486 throw new IllegalArgumentException( "\"" + name + "\" not found in name secondary map" );
488 if ( PhylogenyDecorator.VERBOSE ) {
489 System.out.println( new_name + " " );
494 private static String processNameIntelligently( final String name ) {
495 final String[] s = name.split( " " );
496 if ( s.length < 2 ) {
499 else if ( ( s[ 0 ].indexOf( "_" ) > 0 ) && ( s[ 0 ].indexOf( "|" ) > 0 ) ) {
502 else if ( ( s[ 1 ].indexOf( "_" ) > 0 ) && ( s[ 1 ].indexOf( "|" ) > 0 ) ) {
505 else if ( ( s[ 0 ].indexOf( "_" ) > 0 ) && ( s[ 0 ].indexOf( "." ) > 0 ) ) {
508 else if ( ( s[ 1 ].indexOf( "_" ) > 0 ) && ( s[ 1 ].indexOf( "." ) > 0 ) ) {
511 else if ( s[ 0 ].indexOf( "_" ) > 0 ) {
514 else if ( s[ 1 ].indexOf( "_" ) > 0 ) {
522 private static String processSimilarTo( final String name ) {
523 final int i = name.toLowerCase().indexOf( "similar to" );
524 String similar_to = "";
526 similar_to = " similarity=" + name.substring( i + 10 ).trim();
528 final String pi = processNameIntelligently( name );
529 return pi + similar_to;
532 private static String sanitize( String s ) {
533 s = s.replace( ' ', '_' );
534 s = s.replace( '(', '{' );
535 s = s.replace( ')', '}' );
536 s = s.replace( '[', '{' );
537 s = s.replace( ']', '}' );
538 s = s.replace( ',', '_' );
542 public static enum FIELD {
543 NODE_NAME, SEQUENCE_ANNOTATION_DESC, DOMAIN_STRUCTURE, TAXONOMY_CODE, TAXONOMY_SCIENTIFIC_NAME, SEQUENCE_NAME;