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.io.parsers.nhx.NHXFormatException;
36 import org.forester.phylogeny.Phylogeny;
37 import org.forester.phylogeny.PhylogenyNode;
38 import org.forester.phylogeny.data.Accession;
39 import org.forester.phylogeny.data.Annotation;
40 import org.forester.phylogeny.data.DomainArchitecture;
41 import org.forester.phylogeny.data.Identifier;
42 import org.forester.phylogeny.data.Sequence;
43 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
44 import org.forester.util.BasicTable;
45 import org.forester.util.BasicTableParser;
46 import org.forester.util.ForesterUtil;
48 public final class PhylogenyDecorator {
50 // From evoruby/lib/evo/apps/tseq_taxonomy_processor.rb:
51 final private static String TP_TAXONOMY_CODE = "TAXONOMY_CODE";
52 final private static String TP_TAXONOMY_ID = "TAXONOMY_ID";
53 final private static String TP_TAXONOMY_ID_PROVIDER = "TAXONOMY_ID_PROVIDER";
54 final private static String TP_TAXONOMY_SN = "TAXONOMY_SN";
55 final private static String TP_TAXONOMY_CN = "TAXONOMY_CN";
56 final private static String TP_TAXONOMY_SYN = "TAXONOMY_SYN";
57 final private static String TP_SEQ_SYMBOL = "SEQ_SYMBOL";
58 final private static String TP_SEQ_ACCESSION = "SEQ_ACCESSION";
59 final private static String TP_SEQ_ACCESSION_SOURCE = "SEQ_ACCESSION_SOURCE";
60 final private static String TP_SEQ_ANNOTATION_DESC = "SEQ_ANNOTATION_DESC";
61 final private static String TP_SEQ_ANNOTATION_REF = "SEQ_ANNOTATION_REF";
62 final private static String TP_SEQ_MOL_SEQ = "SEQ_MOL_SEQ";
63 final private static String TP_SEQ_NAME = "SEQ_NAME";
64 final private static String TP_NODE_NAME = "NODE_NAME";
65 final private static Pattern NODENAME_SEQNUMBER_TAXDOMAINNUMBER = Pattern
66 .compile( "^([a-fA-Z0-9]{1,5})_([A-Z0-9]{2,4}[A-Z])(\\d{1,4})$" );
67 public final static boolean SANITIZE = false;
68 public final static boolean VERBOSE = true;
70 private PhylogenyDecorator() {
74 public static void decorate( final Phylogeny phylogeny,
75 final Map<String, Map<String, String>> map,
77 final int numbers_of_chars_allowed_to_remove_if_not_found_in_map )
78 throws IllegalArgumentException {
79 for( final PhylogenyNodeIterator iter = phylogeny.iteratorPostorder(); iter.hasNext(); ) {
80 final PhylogenyNode node = iter.next();
81 final String name = node.getName();
82 if ( !ForesterUtil.isEmpty( name ) ) {
83 if ( map.containsKey( name ) || ( numbers_of_chars_allowed_to_remove_if_not_found_in_map > 0 ) ) {
84 Map<String, String> new_values = map.get( name );
86 while ( ( new_values == null ) && ( numbers_of_chars_allowed_to_remove_if_not_found_in_map > 0 )
87 && ( x <= numbers_of_chars_allowed_to_remove_if_not_found_in_map ) ) {
88 new_values = map.get( name.substring( 0, name.length() - x ) );
91 if ( new_values != null ) {
92 if ( new_values.containsKey( TP_TAXONOMY_CODE ) ) {
93 ForesterUtil.ensurePresenceOfTaxonomy( node );
94 node.getNodeData().getTaxonomy().setTaxonomyCode( new_values.get( TP_TAXONOMY_CODE ) );
96 if ( new_values.containsKey( TP_TAXONOMY_ID )
97 && new_values.containsKey( TP_TAXONOMY_ID_PROVIDER ) ) {
98 ForesterUtil.ensurePresenceOfTaxonomy( node );
101 .setIdentifier( new Identifier( new_values.get( TP_TAXONOMY_ID ),
102 new_values.get( TP_TAXONOMY_ID_PROVIDER ) ) );
104 else if ( new_values.containsKey( TP_TAXONOMY_ID ) ) {
105 ForesterUtil.ensurePresenceOfTaxonomy( node );
106 node.getNodeData().getTaxonomy()
107 .setIdentifier( new Identifier( new_values.get( TP_TAXONOMY_ID ) ) );
109 if ( new_values.containsKey( TP_TAXONOMY_SN ) ) {
110 ForesterUtil.ensurePresenceOfTaxonomy( node );
111 node.getNodeData().getTaxonomy().setScientificName( new_values.get( TP_TAXONOMY_SN ) );
113 if ( new_values.containsKey( TP_TAXONOMY_CN ) ) {
114 ForesterUtil.ensurePresenceOfTaxonomy( node );
115 node.getNodeData().getTaxonomy().setCommonName( new_values.get( TP_TAXONOMY_CN ) );
117 if ( new_values.containsKey( TP_TAXONOMY_SYN ) ) {
118 ForesterUtil.ensurePresenceOfTaxonomy( node );
119 node.getNodeData().getTaxonomy().getSynonyms().add( new_values.get( TP_TAXONOMY_SYN ) );
121 if ( new_values.containsKey( TP_SEQ_ACCESSION )
122 && new_values.containsKey( TP_SEQ_ACCESSION_SOURCE ) ) {
123 ForesterUtil.ensurePresenceOfSequence( node );
126 .setAccession( new Accession( new_values.get( TP_SEQ_ACCESSION ),
127 new_values.get( TP_SEQ_ACCESSION_SOURCE ) ) );
129 if ( new_values.containsKey( TP_SEQ_ANNOTATION_DESC ) ) {
130 ForesterUtil.ensurePresenceOfSequence( node );
131 final Annotation ann = new Annotation( "?" );
132 ann.setDesc( new_values.get( TP_SEQ_ANNOTATION_DESC ) );
133 node.getNodeData().getSequence().addAnnotation( ann );
135 if ( new_values.containsKey( TP_SEQ_ANNOTATION_REF ) ) {
136 ForesterUtil.ensurePresenceOfSequence( node );
137 final Annotation ann = new Annotation( new_values.get( TP_SEQ_ANNOTATION_REF ) );
138 node.getNodeData().getSequence().addAnnotation( ann );
140 if ( new_values.containsKey( TP_SEQ_SYMBOL ) ) {
141 ForesterUtil.ensurePresenceOfSequence( node );
142 node.getNodeData().getSequence().setSymbol( new_values.get( TP_SEQ_SYMBOL ) );
144 if ( new_values.containsKey( TP_SEQ_NAME ) ) {
145 ForesterUtil.ensurePresenceOfSequence( node );
146 node.getNodeData().getSequence().setName( new_values.get( TP_SEQ_NAME ) );
148 if ( new_values.containsKey( TP_SEQ_MOL_SEQ ) ) {
149 ForesterUtil.ensurePresenceOfSequence( node );
150 node.getNodeData().getSequence().setMolecularSequence( new_values.get( TP_SEQ_MOL_SEQ ) );
152 if ( new_values.containsKey( TP_NODE_NAME ) ) {
153 node.setName( new_values.get( TP_NODE_NAME ) );
158 throw new IllegalArgumentException( "\"" + name + "\" not found in name map" );
172 * maps names (in phylogeny) to new values
175 * @throws IllegalArgumentException
176 * @throws NHXFormatException
178 public static void decorate( final Phylogeny phylogeny,
179 final Map<String, String> map,
181 final boolean extract_bracketed_scientific_name,
183 final boolean cut_name_after_space,
184 final boolean process_name_intelligently,
185 final boolean process_similar_to,
186 final int numbers_of_chars_allowed_to_remove_if_not_found_in_map,
187 final boolean move_domain_numbers_at_end_to_middle ) throws IllegalArgumentException,
189 PhylogenyDecorator.decorate( phylogeny,
192 extract_bracketed_scientific_name,
195 cut_name_after_space,
196 process_name_intelligently,
198 numbers_of_chars_allowed_to_remove_if_not_found_in_map,
199 move_domain_numbers_at_end_to_middle );
208 * maps names (in phylogeny) to new values if intermediate_map is
209 * null otherwise maps intermediate value to new value
212 * @param intermediate_map
213 * maps name (in phylogeny) to a intermediate value
214 * @throws IllegalArgumentException
216 public static void decorate( final Phylogeny phylogeny,
217 final Map<String, String> map,
219 final boolean extract_bracketed_scientific_name,
221 final Map<String, String> intermediate_map,
222 final boolean cut_name_after_space,
223 final boolean process_name_intelligently,
224 final boolean process_similar_to,
225 final int numbers_of_chars_allowed_to_remove_if_not_found_in_map,
226 final boolean move_domain_numbers_at_end_to_middle ) throws IllegalArgumentException {
227 if ( extract_bracketed_scientific_name && ( field == FIELD.TAXONOMY_SCIENTIFIC_NAME ) ) {
228 throw new IllegalArgumentException( "Attempt to extract bracketed scientific name together with data field pointing to scientific name" );
230 for( final PhylogenyNodeIterator iter = phylogeny.iteratorPostorder(); iter.hasNext(); ) {
231 final PhylogenyNode node = iter.next();
232 String name = node.getName();
233 if ( !ForesterUtil.isEmpty( name ) ) {
234 if ( intermediate_map != null ) {
235 name = PhylogenyDecorator.extractIntermediate( intermediate_map, name );
237 if ( map.containsKey( name ) || ( numbers_of_chars_allowed_to_remove_if_not_found_in_map > 0 ) ) {
238 String new_value = map.get( name );
240 while ( ( new_value == null ) && ( numbers_of_chars_allowed_to_remove_if_not_found_in_map > 0 )
241 && ( x <= numbers_of_chars_allowed_to_remove_if_not_found_in_map ) ) {
242 new_value = map.get( name.substring( 0, name.length() - x ) );
245 if ( new_value != null ) {
246 new_value = new_value.trim();
247 new_value.replaceAll( "/\\s+/", " " );
248 if ( extract_bracketed_scientific_name && new_value.endsWith( "]" ) ) {
249 extractBracketedScientificNames( 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 ( PhylogenyDecorator.VERBOSE ) {
289 System.out.println( name + ": " + new_value );
291 if ( !node.getNodeData().isHasSequence() ) {
292 node.getNodeData().setSequence( new Sequence() );
294 node.getNodeData().getSequence().setName( new_value );
297 if ( PhylogenyDecorator.VERBOSE ) {
298 System.out.print( name + " -> " );
300 if ( cut_name_after_space ) {
301 if ( PhylogenyDecorator.VERBOSE ) {
302 System.out.print( new_value + " -> " );
304 new_value = PhylogenyDecorator.deleteAtFirstSpace( new_value );
306 else if ( process_name_intelligently ) {
307 if ( PhylogenyDecorator.VERBOSE ) {
308 System.out.print( new_value + " -> " );
310 new_value = PhylogenyDecorator.processNameIntelligently( new_value );
312 else if ( process_similar_to ) {
313 if ( PhylogenyDecorator.VERBOSE ) {
314 System.out.print( new_value + " -> " );
316 new_value = PhylogenyDecorator.processSimilarTo( new_value );
318 if ( PhylogenyDecorator.SANITIZE ) {
319 new_value = PhylogenyDecorator.sanitize( new_value );
321 if ( PhylogenyDecorator.VERBOSE ) {
322 System.out.println( new_value );
324 node.setName( new_value );
327 throw new RuntimeException( "unknown field \"" + field + "\"" );
329 if ( move_domain_numbers_at_end_to_middle && ( field != FIELD.NODE_NAME ) ) {
330 node.setName( moveDomainNumbersAtEnd( node.getName() ) );
335 throw new IllegalArgumentException( "\"" + name + "\" not found in name map" );
341 public static void decorate( final Phylogeny[] phylogenies,
342 final Map<String, Map<String, String>> map,
344 final int numbers_of_chars_allowed_to_remove_if_not_found_in_map )
345 throws IllegalArgumentException, NHXFormatException {
346 for( int i = 0; i < phylogenies.length; ++i ) {
347 PhylogenyDecorator.decorate( phylogenies[ i ],
350 numbers_of_chars_allowed_to_remove_if_not_found_in_map );
354 public static void decorate( final Phylogeny[] phylogenies,
355 final Map<String, String> map,
357 final boolean extract_bracketed_scientific_name,
359 final boolean cut_name_after_space,
360 final boolean process_name_intelligently,
361 final boolean process_similar_to,
362 final int numbers_of_chars_allowed_to_remove_if_not_found_in_map,
363 final boolean move_domain_numbers_at_end_to_middle ) throws IllegalArgumentException,
365 for( int i = 0; i < phylogenies.length; ++i ) {
366 PhylogenyDecorator.decorate( phylogenies[ i ],
369 extract_bracketed_scientific_name,
371 cut_name_after_space,
372 process_name_intelligently,
374 numbers_of_chars_allowed_to_remove_if_not_found_in_map,
375 move_domain_numbers_at_end_to_middle );
379 public static void decorate( final Phylogeny[] phylogenies,
380 final Map<String, String> map,
382 final boolean extract_bracketed_scientific_name,
384 final Map<String, String> intermediate_map,
385 final boolean cut_name_after_space,
386 final boolean process_name_intelligently,
387 final boolean process_similar_to,
388 final int numbers_of_chars_allowed_to_remove_if_not_found_in_map,
389 final boolean move_domain_numbers_at_end_to_middle ) throws IllegalArgumentException,
391 for( int i = 0; i < phylogenies.length; ++i ) {
392 PhylogenyDecorator.decorate( phylogenies[ i ],
395 extract_bracketed_scientific_name,
398 cut_name_after_space,
399 process_name_intelligently,
401 numbers_of_chars_allowed_to_remove_if_not_found_in_map,
402 move_domain_numbers_at_end_to_middle );
406 private static String deleteAtFirstSpace( final String name ) {
407 final int first_space = name.indexOf( " " );
408 if ( first_space > 1 ) {
409 return name.substring( 0, first_space ).trim();
414 private static void extractBracketedScientificNames( final PhylogenyNode node, final String new_value ) {
415 final int i = new_value.lastIndexOf( "[" );
416 final String scientific_name = new_value.substring( i + 1, new_value.length() - 1 );
417 ForesterUtil.ensurePresenceOfTaxonomy( node );
418 node.getNodeData().getTaxonomy().setScientificName( scientific_name );
421 private static String extractIntermediate( final Map<String, String> intermediate_map, final String name ) {
422 String new_name = null;
423 if ( PhylogenyDecorator.VERBOSE ) {
424 System.out.print( name + " => " );
426 if ( intermediate_map.containsKey( name ) ) {
427 new_name = intermediate_map.get( name );
428 if ( ForesterUtil.isEmpty( new_name ) ) {
429 throw new IllegalArgumentException( "\"" + name + "\" maps to null or empty string in secondary map" );
433 throw new IllegalArgumentException( "\"" + name + "\" not found in name secondary map" );
435 if ( PhylogenyDecorator.VERBOSE ) {
436 System.out.println( new_name + " " );
441 private static String moveDomainNumbersAtEnd( final String node_name ) {
442 final Matcher m = NODENAME_SEQNUMBER_TAXDOMAINNUMBER.matcher( node_name );
444 final String seq_number = m.group( 1 );
445 final String tax = m.group( 2 );
446 final String domain_number = m.group( 3 );
447 return seq_number + "_[" + domain_number + "]_" + tax;
454 public static Map<String, Map<String, String>> parseMappingTable( final File mapping_table_file )
456 final Map<String, Map<String, String>> map = new HashMap<String, Map<String, String>>();
457 BasicTable<String> mapping_table = null;
458 mapping_table = BasicTableParser.parse( mapping_table_file, "\t", false );
459 for( int row = 0; row < mapping_table.getNumberOfRows(); ++row ) {
460 final Map<String, String> row_map = new HashMap<String, String>();
462 for( int col = 0; col < mapping_table.getNumberOfColumns(); ++col ) {
463 final String table_cell = mapping_table.getValue( col, row );
467 else if ( table_cell != null ) {
468 final String key = table_cell.substring( 0, table_cell.indexOf( ':' ) );
469 final String val = table_cell.substring( table_cell.indexOf( ':' ) + 1, table_cell.length() );
470 row_map.put( key, val );
473 map.put( name, row_map );
478 private static String processNameIntelligently( final String name ) {
479 final String[] s = name.split( " " );
480 if ( s.length < 2 ) {
483 else if ( ( s[ 0 ].indexOf( "_" ) > 0 ) && ( s[ 0 ].indexOf( "|" ) > 0 ) ) {
486 else if ( ( s[ 1 ].indexOf( "_" ) > 0 ) && ( s[ 1 ].indexOf( "|" ) > 0 ) ) {
489 else if ( ( s[ 0 ].indexOf( "_" ) > 0 ) && ( s[ 0 ].indexOf( "." ) > 0 ) ) {
492 else if ( ( s[ 1 ].indexOf( "_" ) > 0 ) && ( s[ 1 ].indexOf( "." ) > 0 ) ) {
495 else if ( s[ 0 ].indexOf( "_" ) > 0 ) {
498 else if ( s[ 1 ].indexOf( "_" ) > 0 ) {
506 private static String processSimilarTo( final String name ) {
507 final int i = name.toLowerCase().indexOf( "similar to" );
508 String similar_to = "";
510 similar_to = " similarity=" + name.substring( i + 10 ).trim();
512 final String pi = processNameIntelligently( name );
513 return pi + similar_to;
516 private static String sanitize( String s ) {
517 s = s.replace( ' ', '_' );
518 s = s.replace( '(', '{' );
519 s = s.replace( ')', '}' );
520 s = s.replace( '[', '{' );
521 s = s.replace( ']', '}' );
522 s = s.replace( ',', '_' );
526 public static enum FIELD {
527 NODE_NAME, SEQUENCE_ANNOTATION_DESC, DOMAIN_STRUCTURE, TAXONOMY_CODE, TAXONOMY_SCIENTIFIC_NAME, SEQUENCE_NAME;