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: www.phylosoft.org/forester
26 package org.forester.tools;
29 import java.io.IOException;
30 import java.util.HashMap;
32 import java.util.regex.Matcher;
33 import java.util.regex.Pattern;
35 import org.forester.archaeopteryx.Util;
36 import org.forester.io.parsers.nhx.NHXFormatException;
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 final private static Pattern NODENAME_SEQNUMBER_TAXDOMAINNUMBER = Pattern
67 .compile( "^([a-fA-Z0-9]{1,5})_([A-Z0-9]{2,4}[A-Z])(\\d{1,4})$" );
68 public final static boolean SANITIZE = false;
69 public final static boolean VERBOSE = true;
71 private PhylogenyDecorator() {
75 public static void decorate( final Phylogeny phylogeny,
76 final Map<String, Map<String, String>> map,
78 final int numbers_of_chars_allowed_to_remove_if_not_found_in_map )
79 throws IllegalArgumentException {
80 for( final PhylogenyNodeIterator iter = phylogeny.iteratorPostorder(); iter.hasNext(); ) {
81 final PhylogenyNode node = iter.next();
82 final String name = node.getName();
83 if ( !ForesterUtil.isEmpty( name ) ) {
84 if ( map.containsKey( name ) || ( numbers_of_chars_allowed_to_remove_if_not_found_in_map > 0 ) ) {
85 Map<String, String> new_values = map.get( name );
87 while ( ( new_values == null ) && ( numbers_of_chars_allowed_to_remove_if_not_found_in_map > 0 )
88 && ( x <= numbers_of_chars_allowed_to_remove_if_not_found_in_map ) ) {
89 new_values = map.get( name.substring( 0, name.length() - x ) );
92 if ( new_values != null ) {
93 if ( new_values.containsKey( TP_TAXONOMY_CODE ) ) {
94 Util.ensurePresenceOfTaxonomy( node );
95 node.getNodeData().getTaxonomy().setTaxonomyCode( new_values.get( TP_TAXONOMY_CODE ) );
97 if ( new_values.containsKey( TP_TAXONOMY_ID )
98 && new_values.containsKey( TP_TAXONOMY_ID_PROVIDER ) ) {
99 Util.ensurePresenceOfTaxonomy( node );
102 .setIdentifier( new Identifier( new_values.get( TP_TAXONOMY_ID ),
103 new_values.get( TP_TAXONOMY_ID_PROVIDER ) ) );
105 else if ( new_values.containsKey( TP_TAXONOMY_ID ) ) {
106 Util.ensurePresenceOfTaxonomy( node );
107 node.getNodeData().getTaxonomy()
108 .setIdentifier( new Identifier( new_values.get( TP_TAXONOMY_ID ) ) );
110 if ( new_values.containsKey( TP_TAXONOMY_SN ) ) {
111 Util.ensurePresenceOfTaxonomy( node );
112 node.getNodeData().getTaxonomy().setScientificName( new_values.get( TP_TAXONOMY_SN ) );
114 if ( new_values.containsKey( TP_TAXONOMY_CN ) ) {
115 Util.ensurePresenceOfTaxonomy( node );
116 node.getNodeData().getTaxonomy().setCommonName( new_values.get( TP_TAXONOMY_CN ) );
118 if ( new_values.containsKey( TP_TAXONOMY_SYN ) ) {
119 Util.ensurePresenceOfTaxonomy( node );
120 node.getNodeData().getTaxonomy().getSynonyms().add( new_values.get( TP_TAXONOMY_SYN ) );
122 if ( new_values.containsKey( TP_SEQ_ACCESSION )
123 && new_values.containsKey( TP_SEQ_ACCESSION_SOURCE ) ) {
124 Util.ensurePresenceOfSequence( node );
127 .setAccession( new Accession( new_values.get( TP_SEQ_ACCESSION ),
128 new_values.get( TP_SEQ_ACCESSION_SOURCE ) ) );
130 if ( new_values.containsKey( TP_SEQ_ANNOTATION_DESC ) ) {
131 Util.ensurePresenceOfSequence( node );
132 final Annotation ann = new Annotation( "?" );
133 ann.setDesc( new_values.get( TP_SEQ_ANNOTATION_DESC ) );
134 node.getNodeData().getSequence().addAnnotation( ann );
136 if ( new_values.containsKey( TP_SEQ_ANNOTATION_REF ) ) {
137 Util.ensurePresenceOfSequence( node );
138 final Annotation ann = new Annotation( new_values.get( TP_SEQ_ANNOTATION_REF ) );
139 node.getNodeData().getSequence().addAnnotation( ann );
141 if ( new_values.containsKey( TP_SEQ_SYMBOL ) ) {
142 Util.ensurePresenceOfSequence( node );
143 node.getNodeData().getSequence().setSymbol( new_values.get( TP_SEQ_SYMBOL ) );
145 if ( new_values.containsKey( TP_SEQ_NAME ) ) {
146 Util.ensurePresenceOfSequence( node );
147 node.getNodeData().getSequence().setName( new_values.get( TP_SEQ_NAME ) );
149 if ( new_values.containsKey( TP_SEQ_MOL_SEQ ) ) {
150 Util.ensurePresenceOfSequence( node );
151 node.getNodeData().getSequence().setMolecularSequence( new_values.get( TP_SEQ_MOL_SEQ ) );
153 if ( new_values.containsKey( TP_NODE_NAME ) ) {
154 node.setName( new_values.get( TP_NODE_NAME ) );
159 throw new IllegalArgumentException( "\"" + name + "\" not found in name map" );
173 * maps names (in phylogeny) to new values
176 * @throws IllegalArgumentException
177 * @throws NHXFormatException
179 public static void decorate( final Phylogeny phylogeny,
180 final Map<String, String> map,
182 final boolean extract_bracketed_scientific_name,
184 final boolean cut_name_after_space,
185 final boolean process_name_intelligently,
186 final boolean process_similar_to,
187 final int numbers_of_chars_allowed_to_remove_if_not_found_in_map,
188 final boolean move_domain_numbers_at_end_to_middle ) throws IllegalArgumentException,
190 PhylogenyDecorator.decorate( phylogeny,
193 extract_bracketed_scientific_name,
196 cut_name_after_space,
197 process_name_intelligently,
199 numbers_of_chars_allowed_to_remove_if_not_found_in_map,
200 move_domain_numbers_at_end_to_middle );
209 * maps names (in phylogeny) to new values if intermediate_map is
210 * null otherwise maps intermediate value to new value
213 * @param intermediate_map
214 * maps name (in phylogeny) to a intermediate value
215 * @throws IllegalArgumentException
217 public static void decorate( final Phylogeny phylogeny,
218 final Map<String, String> map,
220 final boolean extract_bracketed_scientific_name,
222 final Map<String, String> intermediate_map,
223 final boolean cut_name_after_space,
224 final boolean process_name_intelligently,
225 final boolean process_similar_to,
226 final int numbers_of_chars_allowed_to_remove_if_not_found_in_map,
227 final boolean move_domain_numbers_at_end_to_middle ) throws IllegalArgumentException {
228 if ( extract_bracketed_scientific_name && ( field == FIELD.TAXONOMY_SCIENTIFIC_NAME ) ) {
229 throw new IllegalArgumentException( "Attempt to extract bracketed scientific name together with data field pointing to scientific name" );
231 for( final PhylogenyNodeIterator iter = phylogeny.iteratorPostorder(); iter.hasNext(); ) {
232 final PhylogenyNode node = iter.next();
233 String name = node.getName();
234 if ( !ForesterUtil.isEmpty( name ) ) {
235 if ( intermediate_map != null ) {
236 name = PhylogenyDecorator.extractIntermediate( intermediate_map, name );
238 if ( map.containsKey( name ) || ( numbers_of_chars_allowed_to_remove_if_not_found_in_map > 0 ) ) {
239 String new_value = map.get( name );
241 while ( ( new_value == null ) && ( numbers_of_chars_allowed_to_remove_if_not_found_in_map > 0 )
242 && ( x <= numbers_of_chars_allowed_to_remove_if_not_found_in_map ) ) {
243 new_value = map.get( name.substring( 0, name.length() - x ) );
246 if ( new_value != null ) {
247 new_value = new_value.trim();
248 new_value.replaceAll( "/\\s+/", " " );
249 if ( extract_bracketed_scientific_name && new_value.endsWith( "]" ) ) {
250 extractBracketedScientificNames( node, new_value );
253 case SEQUENCE_ANNOTATION_DESC:
254 if ( PhylogenyDecorator.VERBOSE ) {
255 System.out.println( name + ": " + new_value );
257 if ( !node.getNodeData().isHasSequence() ) {
258 node.getNodeData().setSequence( new Sequence() );
260 final Annotation annotation = new Annotation( "?" );
261 annotation.setDesc( new_value );
262 node.getNodeData().getSequence().addAnnotation( annotation );
264 case DOMAIN_STRUCTURE:
265 if ( PhylogenyDecorator.VERBOSE ) {
266 System.out.println( name + ": " + new_value );
268 if ( !node.getNodeData().isHasSequence() ) {
269 node.getNodeData().setSequence( new Sequence() );
271 node.getNodeData().getSequence()
272 .setDomainArchitecture( new DomainArchitecture( new_value ) );
275 if ( PhylogenyDecorator.VERBOSE ) {
276 System.out.println( name + ": " + new_value );
278 Util.ensurePresenceOfTaxonomy( node );
279 node.getNodeData().getTaxonomy().setTaxonomyCode( new_value );
281 case TAXONOMY_SCIENTIFIC_NAME:
282 if ( PhylogenyDecorator.VERBOSE ) {
283 System.out.println( name + ": " + new_value );
285 Util.ensurePresenceOfTaxonomy( node );
286 node.getNodeData().getTaxonomy().setScientificName( new_value );
289 if ( PhylogenyDecorator.VERBOSE ) {
290 System.out.println( name + ": " + new_value );
292 if ( !node.getNodeData().isHasSequence() ) {
293 node.getNodeData().setSequence( new Sequence() );
295 node.getNodeData().getSequence().setName( new_value );
298 if ( PhylogenyDecorator.VERBOSE ) {
299 System.out.print( name + " -> " );
301 if ( cut_name_after_space ) {
302 if ( PhylogenyDecorator.VERBOSE ) {
303 System.out.print( new_value + " -> " );
305 new_value = PhylogenyDecorator.deleteAtFirstSpace( new_value );
307 else if ( process_name_intelligently ) {
308 if ( PhylogenyDecorator.VERBOSE ) {
309 System.out.print( new_value + " -> " );
311 new_value = PhylogenyDecorator.processNameIntelligently( new_value );
313 else if ( process_similar_to ) {
314 if ( PhylogenyDecorator.VERBOSE ) {
315 System.out.print( new_value + " -> " );
317 new_value = PhylogenyDecorator.processSimilarTo( new_value );
319 if ( PhylogenyDecorator.SANITIZE ) {
320 new_value = PhylogenyDecorator.sanitize( new_value );
322 if ( PhylogenyDecorator.VERBOSE ) {
323 System.out.println( new_value );
325 node.setName( new_value );
328 throw new RuntimeException( "unknown field \"" + field + "\"" );
330 if ( move_domain_numbers_at_end_to_middle && ( field != FIELD.NODE_NAME ) ) {
331 node.setName( moveDomainNumbersAtEnd( node.getName() ) );
336 throw new IllegalArgumentException( "\"" + name + "\" not found in name map" );
342 public static void decorate( final Phylogeny[] phylogenies,
343 final Map<String, Map<String, String>> map,
345 final int numbers_of_chars_allowed_to_remove_if_not_found_in_map )
346 throws IllegalArgumentException, NHXFormatException {
347 for( int i = 0; i < phylogenies.length; ++i ) {
348 PhylogenyDecorator.decorate( phylogenies[ i ],
351 numbers_of_chars_allowed_to_remove_if_not_found_in_map );
355 public static void decorate( final Phylogeny[] phylogenies,
356 final Map<String, String> map,
358 final boolean extract_bracketed_scientific_name,
360 final boolean cut_name_after_space,
361 final boolean process_name_intelligently,
362 final boolean process_similar_to,
363 final int numbers_of_chars_allowed_to_remove_if_not_found_in_map,
364 final boolean move_domain_numbers_at_end_to_middle ) throws IllegalArgumentException,
366 for( int i = 0; i < phylogenies.length; ++i ) {
367 PhylogenyDecorator.decorate( phylogenies[ i ],
370 extract_bracketed_scientific_name,
372 cut_name_after_space,
373 process_name_intelligently,
375 numbers_of_chars_allowed_to_remove_if_not_found_in_map,
376 move_domain_numbers_at_end_to_middle );
380 public static void decorate( final Phylogeny[] phylogenies,
381 final Map<String, String> map,
383 final boolean extract_bracketed_scientific_name,
385 final Map<String, String> intermediate_map,
386 final boolean cut_name_after_space,
387 final boolean process_name_intelligently,
388 final boolean process_similar_to,
389 final int numbers_of_chars_allowed_to_remove_if_not_found_in_map,
390 final boolean move_domain_numbers_at_end_to_middle ) throws IllegalArgumentException,
392 for( int i = 0; i < phylogenies.length; ++i ) {
393 PhylogenyDecorator.decorate( phylogenies[ i ],
396 extract_bracketed_scientific_name,
399 cut_name_after_space,
400 process_name_intelligently,
402 numbers_of_chars_allowed_to_remove_if_not_found_in_map,
403 move_domain_numbers_at_end_to_middle );
407 private static String deleteAtFirstSpace( final String name ) {
408 final int first_space = name.indexOf( " " );
409 if ( first_space > 1 ) {
410 return name.substring( 0, first_space ).trim();
415 private static void extractBracketedScientificNames( final PhylogenyNode node, final String new_value ) {
416 final int i = new_value.lastIndexOf( "[" );
417 final String scientific_name = new_value.substring( i + 1, new_value.length() - 1 );
418 Util.ensurePresenceOfTaxonomy( node );
419 node.getNodeData().getTaxonomy().setScientificName( scientific_name );
422 private static String extractIntermediate( final Map<String, String> intermediate_map, final String name ) {
423 String new_name = null;
424 if ( PhylogenyDecorator.VERBOSE ) {
425 System.out.print( name + " => " );
427 if ( intermediate_map.containsKey( name ) ) {
428 new_name = intermediate_map.get( name );
429 if ( ForesterUtil.isEmpty( new_name ) ) {
430 throw new IllegalArgumentException( "\"" + name + "\" maps to null or empty string in secondary map" );
434 throw new IllegalArgumentException( "\"" + name + "\" not found in name secondary map" );
436 if ( PhylogenyDecorator.VERBOSE ) {
437 System.out.println( new_name + " " );
442 private static String moveDomainNumbersAtEnd( final String node_name ) {
443 final Matcher m = NODENAME_SEQNUMBER_TAXDOMAINNUMBER.matcher( node_name );
445 final String seq_number = m.group( 1 );
446 final String tax = m.group( 2 );
447 final String domain_number = m.group( 3 );
448 return seq_number + "_[" + domain_number + "]_" + tax;
455 public static Map<String, Map<String, String>> parseMappingTable( final File mapping_table_file )
457 final Map<String, Map<String, String>> map = new HashMap<String, Map<String, String>>();
458 BasicTable<String> mapping_table = null;
459 mapping_table = BasicTableParser.parse( mapping_table_file, "\t", false );
460 for( int row = 0; row < mapping_table.getNumberOfRows(); ++row ) {
461 final Map<String, String> row_map = new HashMap<String, String>();
463 for( int col = 0; col < mapping_table.getNumberOfColumns(); ++col ) {
464 final String table_cell = mapping_table.getValue( col, row );
468 else if ( table_cell != null ) {
469 final String key = table_cell.substring( 0, table_cell.indexOf( ':' ) );
470 final String val = table_cell.substring( table_cell.indexOf( ':' ) + 1, table_cell.length() );
471 row_map.put( key, val );
474 map.put( name, row_map );
479 private static String processNameIntelligently( final String name ) {
480 final String[] s = name.split( " " );
481 if ( s.length < 2 ) {
484 else if ( ( s[ 0 ].indexOf( "_" ) > 0 ) && ( s[ 0 ].indexOf( "|" ) > 0 ) ) {
487 else if ( ( s[ 1 ].indexOf( "_" ) > 0 ) && ( s[ 1 ].indexOf( "|" ) > 0 ) ) {
490 else if ( ( s[ 0 ].indexOf( "_" ) > 0 ) && ( s[ 0 ].indexOf( "." ) > 0 ) ) {
493 else if ( ( s[ 1 ].indexOf( "_" ) > 0 ) && ( s[ 1 ].indexOf( "." ) > 0 ) ) {
496 else if ( s[ 0 ].indexOf( "_" ) > 0 ) {
499 else if ( s[ 1 ].indexOf( "_" ) > 0 ) {
507 private static String processSimilarTo( final String name ) {
508 final int i = name.toLowerCase().indexOf( "similar to" );
509 String similar_to = "";
511 similar_to = " similarity=" + name.substring( i + 10 ).trim();
513 final String pi = processNameIntelligently( name );
514 return pi + similar_to;
517 private static String sanitize( String s ) {
518 s = s.replace( ' ', '_' );
519 s = s.replace( '(', '{' );
520 s = s.replace( ')', '}' );
521 s = s.replace( '[', '{' );
522 s = s.replace( ']', '}' );
523 s = s.replace( ',', '_' );
527 public static enum FIELD {
528 NODE_NAME, SEQUENCE_ANNOTATION_DESC, DOMAIN_STRUCTURE, TAXONOMY_CODE, TAXONOMY_SCIENTIFIC_NAME, SEQUENCE_NAME;