in progress
[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.Pattern;
33
34 import org.forester.archaeopteryx.AptxUtil;
35 import org.forester.io.parsers.nhx.NHXFormatException;
36 import org.forester.io.parsers.phyloxml.PhyloXmlDataFormatException;
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;
48
49 public final class PhylogenyDecorator {
50
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;
70     private static final boolean CUT                                = true;
71
72     private PhylogenyDecorator() {
73         // Not needed.
74     }
75
76     public static void decorate( final Phylogeny phylogeny,
77                                  final Map<String, Map<String, String>> map,
78                                  final boolean picky,
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 );
87                     int x = 0;
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 ) );
91                         ++x;
92                     }
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 ) );
97                         }
98                         if ( new_values.containsKey( TP_TAXONOMY_ID )
99                                 && new_values.containsKey( TP_TAXONOMY_ID_PROVIDER ) ) {
100                             AptxUtil.ensurePresenceOfTaxonomy( node );
101                             node.getNodeData()
102                                     .getTaxonomy()
103                                     .setIdentifier( new Identifier( new_values.get( TP_TAXONOMY_ID ),
104                                                                     new_values.get( TP_TAXONOMY_ID_PROVIDER ) ) );
105                         }
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 ) ) );
110                         }
111                         if ( new_values.containsKey( TP_TAXONOMY_SN ) ) {
112                             AptxUtil.ensurePresenceOfTaxonomy( node );
113                             node.getNodeData().getTaxonomy().setScientificName( new_values.get( TP_TAXONOMY_SN ) );
114                         }
115                         if ( new_values.containsKey( TP_TAXONOMY_CN ) ) {
116                             AptxUtil.ensurePresenceOfTaxonomy( node );
117                             node.getNodeData().getTaxonomy().setCommonName( new_values.get( TP_TAXONOMY_CN ) );
118                         }
119                         if ( new_values.containsKey( TP_TAXONOMY_SYN ) ) {
120                             AptxUtil.ensurePresenceOfTaxonomy( node );
121                             node.getNodeData().getTaxonomy().getSynonyms().add( new_values.get( TP_TAXONOMY_SYN ) );
122                         }
123                         if ( new_values.containsKey( TP_SEQ_ACCESSION )
124                                 && new_values.containsKey( TP_SEQ_ACCESSION_SOURCE ) ) {
125                             AptxUtil.ensurePresenceOfSequence( node );
126                             node.getNodeData()
127                                     .getSequence()
128                                     .setAccession( new Accession( new_values.get( TP_SEQ_ACCESSION ),
129                                                                   new_values.get( TP_SEQ_ACCESSION_SOURCE ) ) );
130                         }
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 );
136                         }
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 );
141                         }
142                         if ( new_values.containsKey( TP_SEQ_SYMBOL ) ) {
143                             AptxUtil.ensurePresenceOfSequence( node );
144                             node.getNodeData().getSequence().setSymbol( new_values.get( TP_SEQ_SYMBOL ) );
145                         }
146                         if ( new_values.containsKey( TP_SEQ_NAME ) ) {
147                             AptxUtil.ensurePresenceOfSequence( node );
148                             node.getNodeData().getSequence().setName( new_values.get( TP_SEQ_NAME ) );
149                         }
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 ) );
153                         }
154                         if ( new_values.containsKey( TP_NODE_NAME ) ) {
155                             node.setName( new_values.get( TP_NODE_NAME ) );
156                         }
157                     } // if ( new_values != null ) 
158                 } // if ( map.containsKey( name ) || ( numbers_of_chars_allowed_to_remove_if_not_found_in_map > 0 ) )
159                 else if ( picky ) {
160                     throw new IllegalArgumentException( "\"" + name + "\" not found in name map" );
161                 }
162             }
163         }
164     }
165
166     /**
167      * 
168      * 
169      * 
170      * 
171      * 
172      * @param phylogeny
173      * @param map
174      *            maps names (in phylogeny) to new values
175      * @param field
176      * @param picky
177      * @throws IllegalArgumentException
178      * @throws NHXFormatException
179      * @throws PhyloXmlDataFormatException 
180      */
181     public static void decorate( final Phylogeny phylogeny,
182                                  final Map<String, String> map,
183                                  final FIELD field,
184                                  final boolean extract_bracketed_scientific_name,
185                                  final boolean extract_bracketed_tax_code,
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 trim_after_tilde ) throws IllegalArgumentException, NHXFormatException,
192             PhyloXmlDataFormatException {
193         PhylogenyDecorator.decorate( phylogeny,
194                                      map,
195                                      field,
196                                      extract_bracketed_scientific_name,
197                                      extract_bracketed_tax_code,
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                                      trim_after_tilde );
205     }
206
207     /**
208      * 
209      * 
210      * 
211      * @param phylogeny
212      * @param map
213      *            maps names (in phylogeny) to new values if intermediate_map is
214      *            null otherwise maps intermediate value to new value
215      * @param field
216      * @param picky
217      * @param intermediate_map
218      *            maps name (in phylogeny) to a intermediate value
219      * @throws IllegalArgumentException
220      * @throws PhyloXmlDataFormatException 
221      */
222     public static void decorate( final Phylogeny phylogeny,
223                                  final Map<String, String> map,
224                                  final FIELD field,
225                                  final boolean extract_bracketed_scientific_name,
226                                  final boolean extract_bracketed_tax_code,
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 trim_after_tilde ) throws IllegalArgumentException,
234             PhyloXmlDataFormatException {
235         if ( extract_bracketed_scientific_name && ( field == FIELD.TAXONOMY_SCIENTIFIC_NAME ) ) {
236             throw new IllegalArgumentException( "attempt to extract bracketed scientific name together with data field pointing to scientific name" );
237         }
238         for( final PhylogenyNodeIterator iter = phylogeny.iteratorPostorder(); iter.hasNext(); ) {
239             final PhylogenyNode node = iter.next();
240             String name = node.getName();
241             if ( trim_after_tilde && ( name.indexOf( '~' ) > 0 ) ) {
242                 name = name.substring( 0, name.indexOf( '~' ) );
243             }
244             if ( !ForesterUtil.isEmpty( name ) ) {
245                 if ( intermediate_map != null ) {
246                     name = PhylogenyDecorator.extractIntermediate( intermediate_map, name );
247                 }
248                 // int space_index = name.indexOf( " " );
249                 //                if ( CUT && space_index > 0 ) {
250                 //                    int y = name.lastIndexOf( "|" );
251                 //                    name = name.substring( y + 1, space_index );
252                 //                }
253                 //                String new_value = null;
254                 //                for( String key : map.keySet() ) {
255                 //                    if ( key.indexOf( name ) >= 0 ) {
256                 //                        if ( new_value == null ) {
257                 //                            new_value = map.get( key );
258                 //                        }
259                 //                        else {
260                 //                            System.out.println( name + " is not unique" );
261                 //                            System.exit( -1 );
262                 //                        }
263                 //                    }
264                 //                }
265                 // if ( new_value != null ) {
266                 if ( map.containsKey( name ) || ( numbers_of_chars_allowed_to_remove_if_not_found_in_map > 0 ) ) {
267                     String new_value = map.get( name );
268                     int x = 0;
269                     while ( ( new_value == null ) && ( numbers_of_chars_allowed_to_remove_if_not_found_in_map > 0 )
270                             && ( x <= numbers_of_chars_allowed_to_remove_if_not_found_in_map ) ) {
271                         new_value = map.get( name.substring( 0, name.length() - x ) );
272                         ++x;
273                     }
274                     if ( new_value != null ) {
275                         new_value = new_value.trim();
276                         new_value.replaceAll( "/\\s+/", " " );
277                         if ( extract_bracketed_scientific_name && new_value.endsWith( "]" ) ) {
278                             new_value = extractBracketedScientificNames( node, new_value );
279                         }
280                         else if ( extract_bracketed_tax_code && new_value.endsWith( "]" ) ) {
281                             new_value = extractBracketedTaxCodes( node, new_value );
282                         }
283                         switch ( field ) {
284                             case SEQUENCE_ANNOTATION_DESC:
285                                 if ( PhylogenyDecorator.VERBOSE ) {
286                                     System.out.println( name + ": " + new_value );
287                                 }
288                                 if ( !node.getNodeData().isHasSequence() ) {
289                                     node.getNodeData().setSequence( new Sequence() );
290                                 }
291                                 final Annotation annotation = new Annotation( "?" );
292                                 annotation.setDesc( new_value );
293                                 node.getNodeData().getSequence().addAnnotation( annotation );
294                                 break;
295                             case DOMAIN_STRUCTURE:
296                                 if ( PhylogenyDecorator.VERBOSE ) {
297                                     System.out.println( name + ": " + new_value );
298                                 }
299                                 if ( !node.getNodeData().isHasSequence() ) {
300                                     node.getNodeData().setSequence( new Sequence() );
301                                 }
302                                 node.getNodeData().getSequence()
303                                         .setDomainArchitecture( new DomainArchitecture( new_value ) );
304                                 break;
305                             case TAXONOMY_CODE:
306                                 if ( PhylogenyDecorator.VERBOSE ) {
307                                     System.out.println( name + ": " + new_value );
308                                 }
309                                 AptxUtil.ensurePresenceOfTaxonomy( node );
310                                 node.getNodeData().getTaxonomy().setTaxonomyCode( new_value );
311                                 break;
312                             case TAXONOMY_SCIENTIFIC_NAME:
313                                 if ( PhylogenyDecorator.VERBOSE ) {
314                                     System.out.println( name + ": " + new_value );
315                                 }
316                                 AptxUtil.ensurePresenceOfTaxonomy( node );
317                                 node.getNodeData().getTaxonomy().setScientificName( new_value );
318                                 break;
319                             case SEQUENCE_NAME:
320                                 if ( PhylogenyDecorator.VERBOSE ) {
321                                     System.out.println( name + ": " + new_value );
322                                 }
323                                 if ( !node.getNodeData().isHasSequence() ) {
324                                     node.getNodeData().setSequence( new Sequence() );
325                                 }
326                                 node.getNodeData().getSequence().setName( new_value );
327                                 break;
328                             case NODE_NAME:
329                                 if ( PhylogenyDecorator.VERBOSE ) {
330                                     System.out.print( name + " -> " );
331                                 }
332                                 if ( cut_name_after_space ) {
333                                     if ( PhylogenyDecorator.VERBOSE ) {
334                                         System.out.print( new_value + " -> " );
335                                     }
336                                     new_value = PhylogenyDecorator.deleteAtFirstSpace( new_value );
337                                 }
338                                 else if ( process_name_intelligently ) {
339                                     if ( PhylogenyDecorator.VERBOSE ) {
340                                         System.out.print( new_value + " -> " );
341                                     }
342                                     new_value = PhylogenyDecorator.processNameIntelligently( new_value );
343                                 }
344                                 else if ( process_similar_to ) {
345                                     if ( PhylogenyDecorator.VERBOSE ) {
346                                         System.out.print( new_value + " -> " );
347                                     }
348                                     new_value = PhylogenyDecorator.processSimilarTo( new_value );
349                                 }
350                                 if ( PhylogenyDecorator.SANITIZE ) {
351                                     new_value = PhylogenyDecorator.sanitize( new_value );
352                                 }
353                                 if ( PhylogenyDecorator.VERBOSE ) {
354                                     System.out.println( new_value );
355                                 }
356                                 node.setName( new_value );
357                                 break;
358                             default:
359                                 throw new RuntimeException( "unknown field \"" + field + "\"" );
360                         }
361                     }
362                 }
363                 else if ( picky ) {
364                     throw new IllegalArgumentException( "\"" + name + "\" not found in name map" );
365                 }
366             }
367         }
368     }
369
370     public static void decorate( final Phylogeny[] phylogenies,
371                                  final Map<String, Map<String, String>> map,
372                                  final boolean picky,
373                                  final int numbers_of_chars_allowed_to_remove_if_not_found_in_map )
374             throws IllegalArgumentException, NHXFormatException, PhyloXmlDataFormatException {
375         for( int i = 0; i < phylogenies.length; ++i ) {
376             PhylogenyDecorator.decorate( phylogenies[ i ],
377                                          map,
378                                          picky,
379                                          numbers_of_chars_allowed_to_remove_if_not_found_in_map );
380         }
381     }
382
383     public static void decorate( final Phylogeny[] phylogenies,
384                                  final Map<String, String> map,
385                                  final FIELD field,
386                                  final boolean extract_bracketed_scientific_name,
387                                  final boolean extract_bracketed_tax_code,
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 trim_after_tilde ) throws IllegalArgumentException, NHXFormatException,
394             PhyloXmlDataFormatException {
395         for( int i = 0; i < phylogenies.length; ++i ) {
396             PhylogenyDecorator.decorate( phylogenies[ i ],
397                                          map,
398                                          field,
399                                          extract_bracketed_scientific_name,
400                                          extract_bracketed_tax_code,
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                                          trim_after_tilde );
407         }
408     }
409
410     public static void decorate( final Phylogeny[] phylogenies,
411                                  final Map<String, String> map,
412                                  final FIELD field,
413                                  final boolean extract_bracketed_scientific_name,
414                                  final boolean extract_bracketed_tax_code,
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 trim_after_tilde ) throws IllegalArgumentException, NHXFormatException,
422             PhyloXmlDataFormatException {
423         for( int i = 0; i < phylogenies.length; ++i ) {
424             PhylogenyDecorator.decorate( phylogenies[ i ],
425                                          map,
426                                          field,
427                                          extract_bracketed_scientific_name,
428                                          extract_bracketed_tax_code,
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                                          trim_after_tilde );
436         }
437     }
438
439     private static String deleteAtFirstSpace( final String name ) {
440         final int first_space = name.indexOf( " " );
441         if ( first_space > 1 ) {
442             return name.substring( 0, first_space ).trim();
443         }
444         return name;
445     }
446
447     private static String extractBracketedScientificNames( final PhylogenyNode node, final String new_value ) {
448         final int i = new_value.lastIndexOf( "[" );
449         final String scientific_name = new_value.substring( i + 1, new_value.length() - 1 );
450         AptxUtil.ensurePresenceOfTaxonomy( node );
451         node.getNodeData().getTaxonomy().setScientificName( scientific_name );
452         return new_value.substring( 0, i - 1 ).trim();
453     }
454
455     private static String extractBracketedTaxCodes( final PhylogenyNode node, final String new_value ) {
456         final int i = new_value.lastIndexOf( "[" );
457         final String tc = new_value.substring( i + 1, new_value.length() - 1 );
458         AptxUtil.ensurePresenceOfTaxonomy( node );
459         try {
460             node.getNodeData().getTaxonomy().setTaxonomyCode( tc );
461         }
462         catch ( final PhyloXmlDataFormatException e ) {
463             throw new IllegalArgumentException( "illegal format for taxonomy code: " + tc );
464         }
465         return new_value.substring( 0, i - 1 ).trim();
466     }
467
468     private static String extractIntermediate( final Map<String, String> intermediate_map, final String name ) {
469         String new_name = null;
470         if ( PhylogenyDecorator.VERBOSE ) {
471             System.out.print( name + " => " );
472         }
473         if ( intermediate_map.containsKey( name ) ) {
474             new_name = intermediate_map.get( name );
475             if ( ForesterUtil.isEmpty( new_name ) ) {
476                 throw new IllegalArgumentException( "\"" + name + "\" maps to null or empty string in secondary map" );
477             }
478         }
479         else {
480             throw new IllegalArgumentException( "\"" + name + "\" not found in name secondary map" );
481         }
482         if ( PhylogenyDecorator.VERBOSE ) {
483             System.out.println( new_name + "  " );
484         }
485         return new_name;
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, 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 }