891864bacd2eae200861f458259f7e2010e67c52
[jalview.git] / forester / java / src / org / forester / tools / PhylogenyDecorator.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.tools;
27
28 import java.io.File;
29 import java.io.IOException;
30 import java.util.HashMap;
31 import java.util.Map;
32 import java.util.regex.Matcher;
33 import java.util.regex.Pattern;
34
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;
49
50 public final class PhylogenyDecorator {
51
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;
71     private static final boolean CUT                                = true;
72
73     private PhylogenyDecorator() {
74         // Not needed.
75     }
76
77     public static void decorate( final Phylogeny phylogeny,
78                                  final Map<String, Map<String, String>> map,
79                                  final boolean picky,
80                                  final int numbers_of_chars_allowed_to_remove_if_not_found_in_map )
81             throws IllegalArgumentException, PhyloXmlDataFormatException {
82         for( final PhylogenyNodeIterator iter = phylogeny.iteratorPostorder(); iter.hasNext(); ) {
83             final PhylogenyNode node = iter.next();
84             final String name = node.getName();
85             if ( !ForesterUtil.isEmpty( name ) ) {
86                 if ( map.containsKey( name ) || ( numbers_of_chars_allowed_to_remove_if_not_found_in_map > 0 ) ) {
87                     Map<String, String> new_values = map.get( name );
88                     int x = 0;
89                     while ( ( new_values == null ) && ( numbers_of_chars_allowed_to_remove_if_not_found_in_map > 0 )
90                             && ( x <= numbers_of_chars_allowed_to_remove_if_not_found_in_map ) ) {
91                         new_values = map.get( name.substring( 0, name.length() - x ) );
92                         ++x;
93                     }
94                     if ( new_values != null ) {
95                         if ( new_values.containsKey( TP_TAXONOMY_CODE ) ) {
96                             AptxUtil.ensurePresenceOfTaxonomy( node );
97                             node.getNodeData().getTaxonomy().setTaxonomyCode( new_values.get( TP_TAXONOMY_CODE ) );
98                         }
99                         if ( new_values.containsKey( TP_TAXONOMY_ID )
100                                 && new_values.containsKey( TP_TAXONOMY_ID_PROVIDER ) ) {
101                             AptxUtil.ensurePresenceOfTaxonomy( node );
102                             node.getNodeData()
103                                     .getTaxonomy()
104                                     .setIdentifier( new Identifier( new_values.get( TP_TAXONOMY_ID ),
105                                                                     new_values.get( TP_TAXONOMY_ID_PROVIDER ) ) );
106                         }
107                         else if ( new_values.containsKey( TP_TAXONOMY_ID ) ) {
108                             AptxUtil.ensurePresenceOfTaxonomy( node );
109                             node.getNodeData().getTaxonomy()
110                                     .setIdentifier( new Identifier( new_values.get( TP_TAXONOMY_ID ) ) );
111                         }
112                         if ( new_values.containsKey( TP_TAXONOMY_SN ) ) {
113                             AptxUtil.ensurePresenceOfTaxonomy( node );
114                             node.getNodeData().getTaxonomy().setScientificName( new_values.get( TP_TAXONOMY_SN ) );
115                         }
116                         if ( new_values.containsKey( TP_TAXONOMY_CN ) ) {
117                             AptxUtil.ensurePresenceOfTaxonomy( node );
118                             node.getNodeData().getTaxonomy().setCommonName( new_values.get( TP_TAXONOMY_CN ) );
119                         }
120                         if ( new_values.containsKey( TP_TAXONOMY_SYN ) ) {
121                             AptxUtil.ensurePresenceOfTaxonomy( node );
122                             node.getNodeData().getTaxonomy().getSynonyms().add( new_values.get( TP_TAXONOMY_SYN ) );
123                         }
124                         if ( new_values.containsKey( TP_SEQ_ACCESSION )
125                                 && new_values.containsKey( TP_SEQ_ACCESSION_SOURCE ) ) {
126                             AptxUtil.ensurePresenceOfSequence( node );
127                             node.getNodeData()
128                                     .getSequence()
129                                     .setAccession( new Accession( new_values.get( TP_SEQ_ACCESSION ),
130                                                                   new_values.get( TP_SEQ_ACCESSION_SOURCE ) ) );
131                         }
132                         if ( new_values.containsKey( TP_SEQ_ANNOTATION_DESC ) ) {
133                             AptxUtil.ensurePresenceOfSequence( node );
134                             final Annotation ann = new Annotation( "?" );
135                             ann.setDesc( new_values.get( TP_SEQ_ANNOTATION_DESC ) );
136                             node.getNodeData().getSequence().addAnnotation( ann );
137                         }
138                         if ( new_values.containsKey( TP_SEQ_ANNOTATION_REF ) ) {
139                             AptxUtil.ensurePresenceOfSequence( node );
140                             final Annotation ann = new Annotation( new_values.get( TP_SEQ_ANNOTATION_REF ) );
141                             node.getNodeData().getSequence().addAnnotation( ann );
142                         }
143                         if ( new_values.containsKey( TP_SEQ_SYMBOL ) ) {
144                             AptxUtil.ensurePresenceOfSequence( node );
145                             node.getNodeData().getSequence().setSymbol( new_values.get( TP_SEQ_SYMBOL ) );
146                         }
147                         if ( new_values.containsKey( TP_SEQ_NAME ) ) {
148                             AptxUtil.ensurePresenceOfSequence( node );
149                             node.getNodeData().getSequence().setName( new_values.get( TP_SEQ_NAME ) );
150                         }
151                         if ( new_values.containsKey( TP_SEQ_MOL_SEQ ) ) {
152                             AptxUtil.ensurePresenceOfSequence( node );
153                             node.getNodeData().getSequence().setMolecularSequence( new_values.get( TP_SEQ_MOL_SEQ ) );
154                         }
155                         if ( new_values.containsKey( TP_NODE_NAME ) ) {
156                             node.setName( new_values.get( TP_NODE_NAME ) );
157                         }
158                     } // if ( new_values != null ) 
159                 } // if ( map.containsKey( name ) || ( numbers_of_chars_allowed_to_remove_if_not_found_in_map > 0 ) )
160                 else if ( picky ) {
161                     throw new IllegalArgumentException( "\"" + name + "\" not found in name map" );
162                 }
163             }
164         }
165     }
166
167     /**
168      * 
169      * 
170      * 
171      * 
172      * 
173      * @param phylogeny
174      * @param map
175      *            maps names (in phylogeny) to new values
176      * @param field
177      * @param picky
178      * @throws IllegalArgumentException
179      * @throws NHXFormatException
180      * @throws PhyloXmlDataFormatException 
181      */
182     public static void decorate( final Phylogeny phylogeny,
183                                  final Map<String, String> map,
184                                  final FIELD field,
185                                  final boolean extract_bracketed_scientific_name,
186                                  final boolean picky,
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,
192                                  final boolean trim_after_tilde ) throws IllegalArgumentException, NHXFormatException,
193             PhyloXmlDataFormatException {
194         PhylogenyDecorator.decorate( phylogeny,
195                                      map,
196                                      field,
197                                      extract_bracketed_scientific_name,
198                                      picky,
199                                      null,
200                                      cut_name_after_space,
201                                      process_name_intelligently,
202                                      process_similar_to,
203                                      numbers_of_chars_allowed_to_remove_if_not_found_in_map,
204                                      move_domain_numbers_at_end_to_middle,
205                                      trim_after_tilde );
206     }
207
208     /**
209      * 
210      * 
211      * 
212      * @param phylogeny
213      * @param map
214      *            maps names (in phylogeny) to new values if intermediate_map is
215      *            null otherwise maps intermediate value to new value
216      * @param field
217      * @param picky
218      * @param intermediate_map
219      *            maps name (in phylogeny) to a intermediate value
220      * @throws IllegalArgumentException
221      * @throws PhyloXmlDataFormatException 
222      */
223     public static void decorate( final Phylogeny phylogeny,
224                                  final Map<String, String> map,
225                                  final FIELD field,
226                                  final boolean extract_bracketed_scientific_name,
227                                  final boolean picky,
228                                  final Map<String, String> intermediate_map,
229                                  final boolean cut_name_after_space,
230                                  final boolean process_name_intelligently,
231                                  final boolean process_similar_to,
232                                  final int numbers_of_chars_allowed_to_remove_if_not_found_in_map,
233                                  final boolean move_domain_numbers_at_end_to_middle,
234                                  final boolean trim_after_tilde ) throws IllegalArgumentException,
235             PhyloXmlDataFormatException {
236         if ( extract_bracketed_scientific_name && ( field == FIELD.TAXONOMY_SCIENTIFIC_NAME ) ) {
237             throw new IllegalArgumentException( "Attempt to extract bracketed scientific name together with data field pointing to scientific name" );
238         }
239         for( final PhylogenyNodeIterator iter = phylogeny.iteratorPostorder(); iter.hasNext(); ) {
240             final PhylogenyNode node = iter.next();
241             String name = node.getName();
242             if ( trim_after_tilde && ( name.indexOf( '~' ) > 0 ) ) {
243                 name = name.substring( 0, name.indexOf( '~' ) );
244             }
245             if ( !ForesterUtil.isEmpty( name ) ) {
246                 if ( intermediate_map != null ) {
247                     name = PhylogenyDecorator.extractIntermediate( intermediate_map, name );
248                 }
249                 // int space_index = name.indexOf( " " );
250                 //                if ( CUT && space_index > 0 ) {
251                 //                    int y = name.lastIndexOf( "|" );
252                 //                    name = name.substring( y + 1, space_index );
253                 //                }
254                 //                String new_value = null;
255                 //                for( String key : map.keySet() ) {
256                 //                    if ( key.indexOf( name ) >= 0 ) {
257                 //                        if ( new_value == null ) {
258                 //                            new_value = map.get( key );
259                 //                        }
260                 //                        else {
261                 //                            System.out.println( name + " is not unique" );
262                 //                            System.exit( -1 );
263                 //                        }
264                 //                    }
265                 //                }
266                 // if ( new_value != null ) {
267                 if ( map.containsKey( name ) || ( numbers_of_chars_allowed_to_remove_if_not_found_in_map > 0 ) ) {
268                     String new_value = map.get( name );
269                     int x = 0;
270                     while ( ( new_value == null ) && ( numbers_of_chars_allowed_to_remove_if_not_found_in_map > 0 )
271                             && ( x <= numbers_of_chars_allowed_to_remove_if_not_found_in_map ) ) {
272                         new_value = map.get( name.substring( 0, name.length() - x ) );
273                         ++x;
274                     }
275                     if ( new_value != null ) {
276                         new_value = new_value.trim();
277                         new_value.replaceAll( "/\\s+/", " " );
278                         if ( extract_bracketed_scientific_name && new_value.endsWith( "]" ) ) {
279                             extractBracketedScientificNames( node, new_value );
280                         }
281                         switch ( field ) {
282                             case SEQUENCE_ANNOTATION_DESC:
283                                 if ( PhylogenyDecorator.VERBOSE ) {
284                                     System.out.println( name + ": " + new_value );
285                                 }
286                                 if ( !node.getNodeData().isHasSequence() ) {
287                                     node.getNodeData().setSequence( new Sequence() );
288                                 }
289                                 final Annotation annotation = new Annotation( "?" );
290                                 annotation.setDesc( new_value );
291                                 node.getNodeData().getSequence().addAnnotation( annotation );
292                                 break;
293                             case DOMAIN_STRUCTURE:
294                                 if ( PhylogenyDecorator.VERBOSE ) {
295                                     System.out.println( name + ": " + new_value );
296                                 }
297                                 if ( !node.getNodeData().isHasSequence() ) {
298                                     node.getNodeData().setSequence( new Sequence() );
299                                 }
300                                 node.getNodeData().getSequence()
301                                         .setDomainArchitecture( new DomainArchitecture( new_value ) );
302                                 break;
303                             case TAXONOMY_CODE:
304                                 if ( PhylogenyDecorator.VERBOSE ) {
305                                     System.out.println( name + ": " + new_value );
306                                 }
307                                 AptxUtil.ensurePresenceOfTaxonomy( node );
308                                 node.getNodeData().getTaxonomy().setTaxonomyCode( new_value );
309                                 break;
310                             case TAXONOMY_SCIENTIFIC_NAME:
311                                 if ( PhylogenyDecorator.VERBOSE ) {
312                                     System.out.println( name + ": " + new_value );
313                                 }
314                                 AptxUtil.ensurePresenceOfTaxonomy( node );
315                                 node.getNodeData().getTaxonomy().setScientificName( new_value );
316                                 break;
317                             case SEQUENCE_NAME:
318                                 if ( PhylogenyDecorator.VERBOSE ) {
319                                     System.out.println( name + ": " + new_value );
320                                 }
321                                 if ( !node.getNodeData().isHasSequence() ) {
322                                     node.getNodeData().setSequence( new Sequence() );
323                                 }
324                                 node.getNodeData().getSequence().setName( new_value );
325                                 break;
326                             case NODE_NAME:
327                                 if ( PhylogenyDecorator.VERBOSE ) {
328                                     System.out.print( name + " -> " );
329                                 }
330                                 if ( cut_name_after_space ) {
331                                     if ( PhylogenyDecorator.VERBOSE ) {
332                                         System.out.print( new_value + " -> " );
333                                     }
334                                     new_value = PhylogenyDecorator.deleteAtFirstSpace( new_value );
335                                 }
336                                 else if ( process_name_intelligently ) {
337                                     if ( PhylogenyDecorator.VERBOSE ) {
338                                         System.out.print( new_value + " -> " );
339                                     }
340                                     new_value = PhylogenyDecorator.processNameIntelligently( new_value );
341                                 }
342                                 else if ( process_similar_to ) {
343                                     if ( PhylogenyDecorator.VERBOSE ) {
344                                         System.out.print( new_value + " -> " );
345                                     }
346                                     new_value = PhylogenyDecorator.processSimilarTo( new_value );
347                                 }
348                                 if ( PhylogenyDecorator.SANITIZE ) {
349                                     new_value = PhylogenyDecorator.sanitize( new_value );
350                                 }
351                                 if ( PhylogenyDecorator.VERBOSE ) {
352                                     System.out.println( new_value );
353                                 }
354                                 node.setName( new_value );
355                                 break;
356                             default:
357                                 throw new RuntimeException( "unknown field \"" + field + "\"" );
358                         }
359                         if ( move_domain_numbers_at_end_to_middle && ( field != FIELD.NODE_NAME ) ) {
360                             node.setName( moveDomainNumbersAtEnd( node.getName() ) );
361                         }
362                     }
363                 }
364                 else if ( picky ) {
365                     throw new IllegalArgumentException( "\"" + name + "\" not found in name map" );
366                 }
367             }
368         }
369     }
370
371     public static void decorate( final Phylogeny[] phylogenies,
372                                  final Map<String, Map<String, String>> map,
373                                  final boolean picky,
374                                  final int numbers_of_chars_allowed_to_remove_if_not_found_in_map )
375             throws IllegalArgumentException, NHXFormatException, PhyloXmlDataFormatException {
376         for( int i = 0; i < phylogenies.length; ++i ) {
377             PhylogenyDecorator.decorate( phylogenies[ i ],
378                                          map,
379                                          picky,
380                                          numbers_of_chars_allowed_to_remove_if_not_found_in_map );
381         }
382     }
383
384     public static void decorate( final Phylogeny[] phylogenies,
385                                  final Map<String, String> map,
386                                  final FIELD field,
387                                  final boolean extract_bracketed_scientific_name,
388                                  final boolean picky,
389                                  final boolean cut_name_after_space,
390                                  final boolean process_name_intelligently,
391                                  final boolean process_similar_to,
392                                  final int numbers_of_chars_allowed_to_remove_if_not_found_in_map,
393                                  final boolean move_domain_numbers_at_end_to_middle,
394                                  final boolean trim_after_tilde ) throws IllegalArgumentException, NHXFormatException,
395             PhyloXmlDataFormatException {
396         for( int i = 0; i < phylogenies.length; ++i ) {
397             PhylogenyDecorator.decorate( phylogenies[ i ],
398                                          map,
399                                          field,
400                                          extract_bracketed_scientific_name,
401                                          picky,
402                                          cut_name_after_space,
403                                          process_name_intelligently,
404                                          process_similar_to,
405                                          numbers_of_chars_allowed_to_remove_if_not_found_in_map,
406                                          move_domain_numbers_at_end_to_middle,
407                                          trim_after_tilde );
408         }
409     }
410
411     public static void decorate( final Phylogeny[] phylogenies,
412                                  final Map<String, String> map,
413                                  final FIELD field,
414                                  final boolean extract_bracketed_scientific_name,
415                                  final boolean picky,
416                                  final Map<String, String> intermediate_map,
417                                  final boolean cut_name_after_space,
418                                  final boolean process_name_intelligently,
419                                  final boolean process_similar_to,
420                                  final int numbers_of_chars_allowed_to_remove_if_not_found_in_map,
421                                  final boolean move_domain_numbers_at_end_to_middle,
422                                  final boolean trim_after_tilde ) throws IllegalArgumentException, NHXFormatException,
423             PhyloXmlDataFormatException {
424         for( int i = 0; i < phylogenies.length; ++i ) {
425             PhylogenyDecorator.decorate( phylogenies[ i ],
426                                          map,
427                                          field,
428                                          extract_bracketed_scientific_name,
429                                          picky,
430                                          intermediate_map,
431                                          cut_name_after_space,
432                                          process_name_intelligently,
433                                          process_similar_to,
434                                          numbers_of_chars_allowed_to_remove_if_not_found_in_map,
435                                          move_domain_numbers_at_end_to_middle,
436                                          trim_after_tilde );
437         }
438     }
439
440     private static String deleteAtFirstSpace( final String name ) {
441         final int first_space = name.indexOf( " " );
442         if ( first_space > 1 ) {
443             return name.substring( 0, first_space ).trim();
444         }
445         return name;
446     }
447
448     private static void extractBracketedScientificNames( final PhylogenyNode node, final String new_value ) {
449         final int i = new_value.lastIndexOf( "[" );
450         final String scientific_name = new_value.substring( i + 1, new_value.length() - 1 );
451         AptxUtil.ensurePresenceOfTaxonomy( node );
452         node.getNodeData().getTaxonomy().setScientificName( scientific_name );
453     }
454
455     private static String extractIntermediate( final Map<String, String> intermediate_map, final String name ) {
456         String new_name = null;
457         if ( PhylogenyDecorator.VERBOSE ) {
458             System.out.print( name + " => " );
459         }
460         if ( intermediate_map.containsKey( name ) ) {
461             new_name = intermediate_map.get( name );
462             if ( ForesterUtil.isEmpty( new_name ) ) {
463                 throw new IllegalArgumentException( "\"" + name + "\" maps to null or empty string in secondary map" );
464             }
465         }
466         else {
467             throw new IllegalArgumentException( "\"" + name + "\" not found in name secondary map" );
468         }
469         if ( PhylogenyDecorator.VERBOSE ) {
470             System.out.println( new_name + "  " );
471         }
472         return new_name;
473     }
474
475     private static String moveDomainNumbersAtEnd( final String node_name ) {
476         final Matcher m = NODENAME_SEQNUMBER_TAXDOMAINNUMBER.matcher( node_name );
477         if ( m.matches() ) {
478             final String seq_number = m.group( 1 );
479             final String tax = m.group( 2 );
480             final String domain_number = m.group( 3 );
481             return seq_number + "_[" + domain_number + "]_" + tax;
482         }
483         else {
484             return node_name;
485         }
486     }
487
488     public static Map<String, Map<String, String>> parseMappingTable( final File mapping_table_file )
489             throws IOException {
490         final Map<String, Map<String, String>> map = new HashMap<String, Map<String, String>>();
491         BasicTable<String> mapping_table = null;
492         mapping_table = BasicTableParser.parse( mapping_table_file, "\t", false );
493         for( int row = 0; row < mapping_table.getNumberOfRows(); ++row ) {
494             final Map<String, String> row_map = new HashMap<String, String>();
495             String name = null;
496             for( int col = 0; col < mapping_table.getNumberOfColumns(); ++col ) {
497                 final String table_cell = mapping_table.getValue( col, row );
498                 if ( col == 0 ) {
499                     name = table_cell;
500                 }
501                 else if ( table_cell != null ) {
502                     final String key = table_cell.substring( 0, table_cell.indexOf( ':' ) );
503                     final String val = table_cell.substring( table_cell.indexOf( ':' ) + 1, table_cell.length() );
504                     row_map.put( key, val );
505                 }
506             }
507             map.put( name, row_map );
508         }
509         return map;
510     }
511
512     private static String processNameIntelligently( final String name ) {
513         final String[] s = name.split( " " );
514         if ( s.length < 2 ) {
515             return name;
516         }
517         else if ( ( s[ 0 ].indexOf( "_" ) > 0 ) && ( s[ 0 ].indexOf( "|" ) > 0 ) ) {
518             return s[ 0 ];
519         }
520         else if ( ( s[ 1 ].indexOf( "_" ) > 0 ) && ( s[ 1 ].indexOf( "|" ) > 0 ) ) {
521             return s[ 1 ];
522         }
523         else if ( ( s[ 0 ].indexOf( "_" ) > 0 ) && ( s[ 0 ].indexOf( "." ) > 0 ) ) {
524             return s[ 0 ];
525         }
526         else if ( ( s[ 1 ].indexOf( "_" ) > 0 ) && ( s[ 1 ].indexOf( "." ) > 0 ) ) {
527             return s[ 1 ];
528         }
529         else if ( s[ 0 ].indexOf( "_" ) > 0 ) {
530             return s[ 0 ];
531         }
532         else if ( s[ 1 ].indexOf( "_" ) > 0 ) {
533             return s[ 1 ];
534         }
535         else {
536             return s[ 0 ];
537         }
538     }
539
540     private static String processSimilarTo( final String name ) {
541         final int i = name.toLowerCase().indexOf( "similar to" );
542         String similar_to = "";
543         if ( i >= 0 ) {
544             similar_to = " similarity=" + name.substring( i + 10 ).trim();
545         }
546         final String pi = processNameIntelligently( name );
547         return pi + similar_to;
548     }
549
550     private static String sanitize( String s ) {
551         s = s.replace( ' ', '_' );
552         s = s.replace( '(', '{' );
553         s = s.replace( ')', '}' );
554         s = s.replace( '[', '{' );
555         s = s.replace( ']', '}' );
556         s = s.replace( ',', '_' );
557         return s;
558     }
559
560     public static enum FIELD {
561         NODE_NAME, SEQUENCE_ANNOTATION_DESC, DOMAIN_STRUCTURE, TAXONOMY_CODE, TAXONOMY_SCIENTIFIC_NAME, SEQUENCE_NAME;
562     }
563 }