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.AptxUtil;
36 import org.forester.io.parsers.nhx.NHXFormatException;
37 import org.forester.io.parsers.phyloxml.PhyloXmlDataFormatException;
38 import org.forester.phylogeny.Phylogeny;
39 import org.forester.phylogeny.PhylogenyNode;
40 import org.forester.phylogeny.data.Accession;
41 import org.forester.phylogeny.data.Annotation;
42 import org.forester.phylogeny.data.DomainArchitecture;
43 import org.forester.phylogeny.data.Identifier;
44 import org.forester.phylogeny.data.Sequence;
45 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
46 import org.forester.util.BasicTable;
47 import org.forester.util.BasicTableParser;
48 import org.forester.util.ForesterUtil;
50 public final class PhylogenyDecorator {
52 // From evoruby/lib/evo/apps/tseq_taxonomy_processor.rb:
53 final private static String TP_TAXONOMY_CODE = "TAXONOMY_CODE";
54 final private static String TP_TAXONOMY_ID = "TAXONOMY_ID";
55 final private static String TP_TAXONOMY_ID_PROVIDER = "TAXONOMY_ID_PROVIDER";
56 final private static String TP_TAXONOMY_SN = "TAXONOMY_SN";
57 final private static String TP_TAXONOMY_CN = "TAXONOMY_CN";
58 final private static String TP_TAXONOMY_SYN = "TAXONOMY_SYN";
59 final private static String TP_SEQ_SYMBOL = "SEQ_SYMBOL";
60 final private static String TP_SEQ_ACCESSION = "SEQ_ACCESSION";
61 final private static String TP_SEQ_ACCESSION_SOURCE = "SEQ_ACCESSION_SOURCE";
62 final private static String TP_SEQ_ANNOTATION_DESC = "SEQ_ANNOTATION_DESC";
63 final private static String TP_SEQ_ANNOTATION_REF = "SEQ_ANNOTATION_REF";
64 final private static String TP_SEQ_MOL_SEQ = "SEQ_MOL_SEQ";
65 final private static String TP_SEQ_NAME = "SEQ_NAME";
66 final private static String TP_NODE_NAME = "NODE_NAME";
67 final private static Pattern NODENAME_SEQNUMBER_TAXDOMAINNUMBER = Pattern
68 .compile( "^([a-fA-Z0-9]{1,5})_([A-Z0-9]{2,4}[A-Z])(\\d{1,4})$" );
69 public final static boolean SANITIZE = false;
70 public final static boolean VERBOSE = true;
72 private PhylogenyDecorator() {
76 public static void decorate( final Phylogeny phylogeny,
77 final Map<String, Map<String, String>> map,
79 final int numbers_of_chars_allowed_to_remove_if_not_found_in_map )
80 throws IllegalArgumentException, PhyloXmlDataFormatException {
81 for( final PhylogenyNodeIterator iter = phylogeny.iteratorPostorder(); iter.hasNext(); ) {
82 final PhylogenyNode node = iter.next();
83 final String name = node.getName();
84 if ( !ForesterUtil.isEmpty( name ) ) {
85 if ( map.containsKey( name ) || ( numbers_of_chars_allowed_to_remove_if_not_found_in_map > 0 ) ) {
86 Map<String, String> new_values = map.get( name );
88 while ( ( new_values == null ) && ( numbers_of_chars_allowed_to_remove_if_not_found_in_map > 0 )
89 && ( x <= numbers_of_chars_allowed_to_remove_if_not_found_in_map ) ) {
90 new_values = map.get( name.substring( 0, name.length() - x ) );
93 if ( new_values != null ) {
94 if ( new_values.containsKey( TP_TAXONOMY_CODE ) ) {
95 AptxUtil.ensurePresenceOfTaxonomy( node );
96 node.getNodeData().getTaxonomy().setTaxonomyCode( new_values.get( TP_TAXONOMY_CODE ) );
98 if ( new_values.containsKey( TP_TAXONOMY_ID )
99 && new_values.containsKey( TP_TAXONOMY_ID_PROVIDER ) ) {
100 AptxUtil.ensurePresenceOfTaxonomy( node );
103 .setIdentifier( new Identifier( new_values.get( TP_TAXONOMY_ID ),
104 new_values.get( TP_TAXONOMY_ID_PROVIDER ) ) );
106 else if ( new_values.containsKey( TP_TAXONOMY_ID ) ) {
107 AptxUtil.ensurePresenceOfTaxonomy( node );
108 node.getNodeData().getTaxonomy()
109 .setIdentifier( new Identifier( new_values.get( TP_TAXONOMY_ID ) ) );
111 if ( new_values.containsKey( TP_TAXONOMY_SN ) ) {
112 AptxUtil.ensurePresenceOfTaxonomy( node );
113 node.getNodeData().getTaxonomy().setScientificName( new_values.get( TP_TAXONOMY_SN ) );
115 if ( new_values.containsKey( TP_TAXONOMY_CN ) ) {
116 AptxUtil.ensurePresenceOfTaxonomy( node );
117 node.getNodeData().getTaxonomy().setCommonName( new_values.get( TP_TAXONOMY_CN ) );
119 if ( new_values.containsKey( TP_TAXONOMY_SYN ) ) {
120 AptxUtil.ensurePresenceOfTaxonomy( node );
121 node.getNodeData().getTaxonomy().getSynonyms().add( new_values.get( TP_TAXONOMY_SYN ) );
123 if ( new_values.containsKey( TP_SEQ_ACCESSION )
124 && new_values.containsKey( TP_SEQ_ACCESSION_SOURCE ) ) {
125 AptxUtil.ensurePresenceOfSequence( node );
128 .setAccession( new Accession( new_values.get( TP_SEQ_ACCESSION ),
129 new_values.get( TP_SEQ_ACCESSION_SOURCE ) ) );
131 if ( new_values.containsKey( TP_SEQ_ANNOTATION_DESC ) ) {
132 AptxUtil.ensurePresenceOfSequence( node );
133 final Annotation ann = new Annotation( "?" );
134 ann.setDesc( new_values.get( TP_SEQ_ANNOTATION_DESC ) );
135 node.getNodeData().getSequence().addAnnotation( ann );
137 if ( new_values.containsKey( TP_SEQ_ANNOTATION_REF ) ) {
138 AptxUtil.ensurePresenceOfSequence( node );
139 final Annotation ann = new Annotation( new_values.get( TP_SEQ_ANNOTATION_REF ) );
140 node.getNodeData().getSequence().addAnnotation( ann );
142 if ( new_values.containsKey( TP_SEQ_SYMBOL ) ) {
143 AptxUtil.ensurePresenceOfSequence( node );
144 node.getNodeData().getSequence().setSymbol( new_values.get( TP_SEQ_SYMBOL ) );
146 if ( new_values.containsKey( TP_SEQ_NAME ) ) {
147 AptxUtil.ensurePresenceOfSequence( node );
148 node.getNodeData().getSequence().setName( new_values.get( TP_SEQ_NAME ) );
150 if ( new_values.containsKey( TP_SEQ_MOL_SEQ ) ) {
151 AptxUtil.ensurePresenceOfSequence( node );
152 node.getNodeData().getSequence().setMolecularSequence( new_values.get( TP_SEQ_MOL_SEQ ) );
154 if ( new_values.containsKey( TP_NODE_NAME ) ) {
155 node.setName( new_values.get( TP_NODE_NAME ) );
157 } // if ( new_values != null )
158 } // if ( map.containsKey( name ) || ( numbers_of_chars_allowed_to_remove_if_not_found_in_map > 0 ) )
160 System.out.println( map.toString() );
161 throw new IllegalArgumentException( "\"" + name + "\" not found in name map" );
175 * maps names (in phylogeny) to new values
178 * @throws IllegalArgumentException
179 * @throws NHXFormatException
180 * @throws PhyloXmlDataFormatException
182 public static void decorate( final Phylogeny phylogeny,
183 final Map<String, String> map,
185 final boolean extract_bracketed_scientific_name,
187 final boolean cut_name_after_space,
188 final boolean process_name_intelligently,
189 final boolean process_similar_to,
190 final int numbers_of_chars_allowed_to_remove_if_not_found_in_map,
191 final boolean move_domain_numbers_at_end_to_middle ) throws IllegalArgumentException,
192 NHXFormatException, PhyloXmlDataFormatException {
193 PhylogenyDecorator.decorate( phylogeny,
196 extract_bracketed_scientific_name,
199 cut_name_after_space,
200 process_name_intelligently,
202 numbers_of_chars_allowed_to_remove_if_not_found_in_map,
203 move_domain_numbers_at_end_to_middle );
212 * maps names (in phylogeny) to new values if intermediate_map is
213 * null otherwise maps intermediate value to new value
216 * @param intermediate_map
217 * maps name (in phylogeny) to a intermediate value
218 * @throws IllegalArgumentException
219 * @throws PhyloXmlDataFormatException
221 public static void decorate( final Phylogeny phylogeny,
222 final Map<String, String> map,
224 final boolean extract_bracketed_scientific_name,
226 final Map<String, String> intermediate_map,
227 final boolean cut_name_after_space,
228 final boolean process_name_intelligently,
229 final boolean process_similar_to,
230 final int numbers_of_chars_allowed_to_remove_if_not_found_in_map,
231 final boolean move_domain_numbers_at_end_to_middle ) throws IllegalArgumentException,
232 PhyloXmlDataFormatException {
233 if ( extract_bracketed_scientific_name && ( field == FIELD.TAXONOMY_SCIENTIFIC_NAME ) ) {
234 throw new IllegalArgumentException( "Attempt to extract bracketed scientific name together with data field pointing to scientific name" );
236 for( final PhylogenyNodeIterator iter = phylogeny.iteratorPostorder(); iter.hasNext(); ) {
237 final PhylogenyNode node = iter.next();
238 String name = node.getName();
239 if ( !ForesterUtil.isEmpty( name ) ) {
240 if ( intermediate_map != null ) {
241 name = PhylogenyDecorator.extractIntermediate( intermediate_map, name );
243 if ( map.containsKey( name ) || ( numbers_of_chars_allowed_to_remove_if_not_found_in_map > 0 ) ) {
244 String new_value = map.get( name );
246 while ( ( new_value == null ) && ( numbers_of_chars_allowed_to_remove_if_not_found_in_map > 0 )
247 && ( x <= numbers_of_chars_allowed_to_remove_if_not_found_in_map ) ) {
248 new_value = map.get( name.substring( 0, name.length() - x ) );
251 if ( new_value != null ) {
252 new_value = new_value.trim();
253 new_value.replaceAll( "/\\s+/", " " );
254 if ( extract_bracketed_scientific_name && new_value.endsWith( "]" ) ) {
255 extractBracketedScientificNames( node, new_value );
258 case SEQUENCE_ANNOTATION_DESC:
259 if ( PhylogenyDecorator.VERBOSE ) {
260 System.out.println( name + ": " + new_value );
262 if ( !node.getNodeData().isHasSequence() ) {
263 node.getNodeData().setSequence( new Sequence() );
265 final Annotation annotation = new Annotation( "?" );
266 annotation.setDesc( new_value );
267 node.getNodeData().getSequence().addAnnotation( annotation );
269 case DOMAIN_STRUCTURE:
270 if ( PhylogenyDecorator.VERBOSE ) {
271 System.out.println( name + ": " + new_value );
273 if ( !node.getNodeData().isHasSequence() ) {
274 node.getNodeData().setSequence( new Sequence() );
276 node.getNodeData().getSequence()
277 .setDomainArchitecture( new DomainArchitecture( new_value ) );
280 if ( PhylogenyDecorator.VERBOSE ) {
281 System.out.println( name + ": " + new_value );
283 AptxUtil.ensurePresenceOfTaxonomy( node );
284 node.getNodeData().getTaxonomy().setTaxonomyCode( new_value );
286 case TAXONOMY_SCIENTIFIC_NAME:
287 if ( PhylogenyDecorator.VERBOSE ) {
288 System.out.println( name + ": " + new_value );
290 AptxUtil.ensurePresenceOfTaxonomy( node );
291 node.getNodeData().getTaxonomy().setScientificName( new_value );
294 if ( PhylogenyDecorator.VERBOSE ) {
295 System.out.println( name + ": " + new_value );
297 if ( !node.getNodeData().isHasSequence() ) {
298 node.getNodeData().setSequence( new Sequence() );
300 node.getNodeData().getSequence().setName( new_value );
303 if ( PhylogenyDecorator.VERBOSE ) {
304 System.out.print( name + " -> " );
306 if ( cut_name_after_space ) {
307 if ( PhylogenyDecorator.VERBOSE ) {
308 System.out.print( new_value + " -> " );
310 new_value = PhylogenyDecorator.deleteAtFirstSpace( new_value );
312 else if ( process_name_intelligently ) {
313 if ( PhylogenyDecorator.VERBOSE ) {
314 System.out.print( new_value + " -> " );
316 new_value = PhylogenyDecorator.processNameIntelligently( new_value );
318 else if ( process_similar_to ) {
319 if ( PhylogenyDecorator.VERBOSE ) {
320 System.out.print( new_value + " -> " );
322 new_value = PhylogenyDecorator.processSimilarTo( new_value );
324 if ( PhylogenyDecorator.SANITIZE ) {
325 new_value = PhylogenyDecorator.sanitize( new_value );
327 if ( PhylogenyDecorator.VERBOSE ) {
328 System.out.println( new_value );
330 node.setName( new_value );
333 throw new RuntimeException( "unknown field \"" + field + "\"" );
335 if ( move_domain_numbers_at_end_to_middle && ( field != FIELD.NODE_NAME ) ) {
336 node.setName( moveDomainNumbersAtEnd( node.getName() ) );
341 throw new IllegalArgumentException( "\"" + name + "\" not found in name map" );
347 public static void decorate( final Phylogeny[] phylogenies,
348 final Map<String, Map<String, String>> map,
350 final int numbers_of_chars_allowed_to_remove_if_not_found_in_map )
351 throws IllegalArgumentException, NHXFormatException, PhyloXmlDataFormatException {
352 for( int i = 0; i < phylogenies.length; ++i ) {
353 PhylogenyDecorator.decorate( phylogenies[ i ],
356 numbers_of_chars_allowed_to_remove_if_not_found_in_map );
360 public static void decorate( final Phylogeny[] phylogenies,
361 final Map<String, String> map,
363 final boolean extract_bracketed_scientific_name,
365 final boolean cut_name_after_space,
366 final boolean process_name_intelligently,
367 final boolean process_similar_to,
368 final int numbers_of_chars_allowed_to_remove_if_not_found_in_map,
369 final boolean move_domain_numbers_at_end_to_middle ) throws IllegalArgumentException,
370 NHXFormatException, PhyloXmlDataFormatException {
371 for( int i = 0; i < phylogenies.length; ++i ) {
372 PhylogenyDecorator.decorate( phylogenies[ i ],
375 extract_bracketed_scientific_name,
377 cut_name_after_space,
378 process_name_intelligently,
380 numbers_of_chars_allowed_to_remove_if_not_found_in_map,
381 move_domain_numbers_at_end_to_middle );
385 public static void decorate( final Phylogeny[] phylogenies,
386 final Map<String, String> map,
388 final boolean extract_bracketed_scientific_name,
390 final Map<String, String> intermediate_map,
391 final boolean cut_name_after_space,
392 final boolean process_name_intelligently,
393 final boolean process_similar_to,
394 final int numbers_of_chars_allowed_to_remove_if_not_found_in_map,
395 final boolean move_domain_numbers_at_end_to_middle ) throws IllegalArgumentException,
396 NHXFormatException, PhyloXmlDataFormatException {
397 for( int i = 0; i < phylogenies.length; ++i ) {
398 PhylogenyDecorator.decorate( phylogenies[ i ],
401 extract_bracketed_scientific_name,
404 cut_name_after_space,
405 process_name_intelligently,
407 numbers_of_chars_allowed_to_remove_if_not_found_in_map,
408 move_domain_numbers_at_end_to_middle );
412 private static String deleteAtFirstSpace( final String name ) {
413 final int first_space = name.indexOf( " " );
414 if ( first_space > 1 ) {
415 return name.substring( 0, first_space ).trim();
420 private static void extractBracketedScientificNames( final PhylogenyNode node, final String new_value ) {
421 final int i = new_value.lastIndexOf( "[" );
422 final String scientific_name = new_value.substring( i + 1, new_value.length() - 1 );
423 AptxUtil.ensurePresenceOfTaxonomy( node );
424 node.getNodeData().getTaxonomy().setScientificName( scientific_name );
427 private static String extractIntermediate( final Map<String, String> intermediate_map, final String name ) {
428 String new_name = null;
429 if ( PhylogenyDecorator.VERBOSE ) {
430 System.out.print( name + " => " );
432 if ( intermediate_map.containsKey( name ) ) {
433 new_name = intermediate_map.get( name );
434 if ( ForesterUtil.isEmpty( new_name ) ) {
435 throw new IllegalArgumentException( "\"" + name + "\" maps to null or empty string in secondary map" );
439 throw new IllegalArgumentException( "\"" + name + "\" not found in name secondary map" );
441 if ( PhylogenyDecorator.VERBOSE ) {
442 System.out.println( new_name + " " );
447 private static String moveDomainNumbersAtEnd( final String node_name ) {
448 final Matcher m = NODENAME_SEQNUMBER_TAXDOMAINNUMBER.matcher( node_name );
450 final String seq_number = m.group( 1 );
451 final String tax = m.group( 2 );
452 final String domain_number = m.group( 3 );
453 return seq_number + "_[" + domain_number + "]_" + tax;
460 public static Map<String, Map<String, String>> parseMappingTable( final File mapping_table_file )
462 final Map<String, Map<String, String>> map = new HashMap<String, Map<String, String>>();
463 BasicTable<String> mapping_table = null;
464 mapping_table = BasicTableParser.parse( mapping_table_file, "\t", false );
465 for( int row = 0; row < mapping_table.getNumberOfRows(); ++row ) {
466 final Map<String, String> row_map = new HashMap<String, String>();
468 for( int col = 0; col < mapping_table.getNumberOfColumns(); ++col ) {
469 final String table_cell = mapping_table.getValue( col, row );
473 else if ( table_cell != null ) {
474 final String key = table_cell.substring( 0, table_cell.indexOf( ':' ) );
475 final String val = table_cell.substring( table_cell.indexOf( ':' ) + 1, table_cell.length() );
476 row_map.put( key, val );
479 map.put( name, row_map );
484 private static String processNameIntelligently( final String name ) {
485 final String[] s = name.split( " " );
486 if ( s.length < 2 ) {
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 ) && ( s[ 0 ].indexOf( "." ) > 0 ) ) {
498 else if ( ( s[ 1 ].indexOf( "_" ) > 0 ) && ( s[ 1 ].indexOf( "." ) > 0 ) ) {
501 else if ( s[ 0 ].indexOf( "_" ) > 0 ) {
504 else if ( s[ 1 ].indexOf( "_" ) > 0 ) {
512 private static String processSimilarTo( final String name ) {
513 final int i = name.toLowerCase().indexOf( "similar to" );
514 String similar_to = "";
516 similar_to = " similarity=" + name.substring( i + 10 ).trim();
518 final String pi = processNameIntelligently( name );
519 return pi + similar_to;
522 private static String sanitize( String s ) {
523 s = s.replace( ' ', '_' );
524 s = s.replace( '(', '{' );
525 s = s.replace( ')', '}' );
526 s = s.replace( '[', '{' );
527 s = s.replace( ']', '}' );
528 s = s.replace( ',', '_' );
532 public static enum FIELD {
533 NODE_NAME, SEQUENCE_ANNOTATION_DESC, DOMAIN_STRUCTURE, TAXONOMY_CODE, TAXONOMY_SCIENTIFIC_NAME, SEQUENCE_NAME;