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