inprogress
[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         for( final PhylogenyNodeIterator iter = phylogeny.iteratorPostorder(); iter.hasNext(); ) {
221             final PhylogenyNode node = iter.next();
222             String name = node.getName();
223             String tilde_annotation = null;
224             if ( trim_after_tilde && ( name.indexOf( '~' ) > 0 ) ) {
225                 final int ti = name.indexOf( '~' );
226                 tilde_annotation = name.substring( ti );
227                 name = name.substring( 0, ti );
228             }
229             if ( !ForesterUtil.isEmpty( name ) ) {
230                 if ( intermediate_map != null ) {
231                     name = PhylogenyDecorator.extractIntermediate( intermediate_map, name );
232                 }
233                 if ( map.containsKey( name ) || ( numbers_of_chars_allowed_to_remove_if_not_found_in_map > 0 ) ) {
234                     String new_value = map.get( name );
235                     int x = 0;
236                     while ( ( new_value == null ) && ( numbers_of_chars_allowed_to_remove_if_not_found_in_map > 0 )
237                             && ( x <= numbers_of_chars_allowed_to_remove_if_not_found_in_map ) ) {
238                         new_value = map.get( name.substring( 0, name.length() - x ) );
239                         ++x;
240                     }
241                     if ( new_value != null ) {
242                         new_value = new_value.trim();
243                         new_value.replaceAll( "/\\s+/", " " );
244                         if ( extract_bracketed_scientific_name && new_value.endsWith( "]" ) ) {
245                             new_value = extractBracketedScientificNames( node, new_value );
246                         }
247                         else if ( extract_bracketed_tax_code
248                                 && ParserUtils.TAXOMONY_CODE_PATTERN_4.matcher( new_value ).matches() ) {
249                             new_value = extractBracketedTaxCodes( node, new_value );
250                         }
251                         switch ( field ) {
252                             case SEQUENCE_ANNOTATION_DESC:
253                                 if ( PhylogenyDecorator.VERBOSE ) {
254                                     System.out.println( name + ": " + new_value );
255                                 }
256                                 if ( !node.getNodeData().isHasSequence() ) {
257                                     node.getNodeData().setSequence( new Sequence() );
258                                 }
259                                 final Annotation annotation = new Annotation( "?" );
260                                 annotation.setDesc( new_value );
261                                 node.getNodeData().getSequence().addAnnotation( annotation );
262                                 break;
263                             case DOMAIN_STRUCTURE:
264                                 if ( PhylogenyDecorator.VERBOSE ) {
265                                     System.out.println( name + ": " + new_value );
266                                 }
267                                 if ( !node.getNodeData().isHasSequence() ) {
268                                     node.getNodeData().setSequence( new Sequence() );
269                                 }
270                                 node.getNodeData().getSequence()
271                                         .setDomainArchitecture( new DomainArchitecture( new_value ) );
272                                 break;
273                             case TAXONOMY_CODE:
274                                 if ( PhylogenyDecorator.VERBOSE ) {
275                                     System.out.println( name + ": " + new_value );
276                                 }
277                                 ForesterUtil.ensurePresenceOfTaxonomy( node );
278                                 node.getNodeData().getTaxonomy().setTaxonomyCode( new_value );
279                                 break;
280                             case TAXONOMY_SCIENTIFIC_NAME:
281                                 if ( PhylogenyDecorator.VERBOSE ) {
282                                     System.out.println( name + ": " + new_value );
283                                 }
284                                 ForesterUtil.ensurePresenceOfTaxonomy( node );
285                                 node.getNodeData().getTaxonomy().setScientificName( new_value );
286                                 break;
287                             case SEQUENCE_NAME:
288                                 if ( trim_after_tilde ) {
289                                     new_value = addTildeAnnotation( tilde_annotation, new_value );
290                                 }
291                                 if ( PhylogenyDecorator.VERBOSE ) {
292                                     System.out.println( name + ": " + new_value );
293                                 }
294                                 if ( !node.getNodeData().isHasSequence() ) {
295                                     node.getNodeData().setSequence( new Sequence() );
296                                 }
297                                 node.getNodeData().getSequence().setName( new_value );
298                                 break;
299                             case NODE_NAME:
300                                 if ( PhylogenyDecorator.VERBOSE ) {
301                                     System.out.print( name + " -> " );
302                                 }
303                                 if ( cut_name_after_space ) {
304                                     if ( PhylogenyDecorator.VERBOSE ) {
305                                         System.out.print( new_value + " -> " );
306                                     }
307                                     new_value = PhylogenyDecorator.deleteAtFirstSpace( new_value );
308                                 }
309                                 else if ( process_name_intelligently ) {
310                                     if ( PhylogenyDecorator.VERBOSE ) {
311                                         System.out.print( new_value + " -> " );
312                                     }
313                                     new_value = PhylogenyDecorator.processNameIntelligently( new_value );
314                                 }
315                                 else if ( process_similar_to ) {
316                                     if ( PhylogenyDecorator.VERBOSE ) {
317                                         System.out.print( new_value + " -> " );
318                                     }
319                                     new_value = PhylogenyDecorator.processSimilarTo( new_value );
320                                 }
321                                 if ( PhylogenyDecorator.SANITIZE ) {
322                                     new_value = PhylogenyDecorator.sanitize( new_value );
323                                 }
324                                 if ( trim_after_tilde ) {
325                                     new_value = addTildeAnnotation( tilde_annotation, new_value );
326                                 }
327                                 if ( PhylogenyDecorator.VERBOSE ) {
328                                     System.out.println( new_value );
329                                 }
330                                 node.setName( new_value );
331                                 break;
332                             default:
333                                 throw new RuntimeException( "unknown field \"" + field + "\"" );
334                         }
335                     }
336                 }
337                 else if ( picky ) {
338                     throw new IllegalArgumentException( "\"" + name + "\" not found in name map" );
339                 }
340             }
341         }
342     }
343
344     private final static String addTildeAnnotation( final String tilde_annotation, final String new_value ) {
345         if ( ForesterUtil.isEmpty( tilde_annotation ) ) {
346             return new_value;
347         }
348         return new_value + tilde_annotation;
349     }
350
351     public static void decorate( final Phylogeny[] phylogenies,
352                                  final Map<String, Map<String, String>> map,
353                                  final boolean picky,
354                                  final int numbers_of_chars_allowed_to_remove_if_not_found_in_map )
355             throws IllegalArgumentException, NHXFormatException, PhyloXmlDataFormatException {
356         for( final Phylogeny phylogenie : phylogenies ) {
357             PhylogenyDecorator
358                     .decorate( phylogenie, map, picky, numbers_of_chars_allowed_to_remove_if_not_found_in_map );
359         }
360     }
361
362     public static void decorate( final Phylogeny[] phylogenies,
363                                  final Map<String, String> map,
364                                  final FIELD field,
365                                  final boolean extract_bracketed_scientific_name,
366                                  final boolean extract_bracketed_tax_code,
367                                  final boolean picky,
368                                  final boolean cut_name_after_space,
369                                  final boolean process_name_intelligently,
370                                  final boolean process_similar_to,
371                                  final int numbers_of_chars_allowed_to_remove_if_not_found_in_map,
372                                  final boolean trim_after_tilde ) throws IllegalArgumentException, NHXFormatException,
373             PhyloXmlDataFormatException {
374         for( final Phylogeny phylogenie : phylogenies ) {
375             PhylogenyDecorator.decorate( phylogenie,
376                                          map,
377                                          field,
378                                          extract_bracketed_scientific_name,
379                                          extract_bracketed_tax_code,
380                                          picky,
381                                          cut_name_after_space,
382                                          process_name_intelligently,
383                                          process_similar_to,
384                                          numbers_of_chars_allowed_to_remove_if_not_found_in_map,
385                                          trim_after_tilde );
386         }
387     }
388
389     public static void decorate( final Phylogeny[] phylogenies,
390                                  final Map<String, String> map,
391                                  final FIELD field,
392                                  final boolean extract_bracketed_scientific_name,
393                                  final boolean extract_bracketed_tax_code,
394                                  final boolean picky,
395                                  final Map<String, String> intermediate_map,
396                                  final boolean cut_name_after_space,
397                                  final boolean process_name_intelligently,
398                                  final boolean process_similar_to,
399                                  final int numbers_of_chars_allowed_to_remove_if_not_found_in_map,
400                                  final boolean trim_after_tilde ) throws IllegalArgumentException, NHXFormatException,
401             PhyloXmlDataFormatException {
402         for( final Phylogeny phylogenie : phylogenies ) {
403             PhylogenyDecorator.decorate( phylogenie,
404                                          map,
405                                          field,
406                                          extract_bracketed_scientific_name,
407                                          extract_bracketed_tax_code,
408                                          picky,
409                                          intermediate_map,
410                                          cut_name_after_space,
411                                          process_name_intelligently,
412                                          process_similar_to,
413                                          numbers_of_chars_allowed_to_remove_if_not_found_in_map,
414                                          trim_after_tilde );
415         }
416     }
417
418     public static Map<String, Map<String, String>> parseMappingTable( final File mapping_table_file )
419             throws IOException {
420         final Map<String, Map<String, String>> map = new HashMap<String, Map<String, String>>();
421         BasicTable<String> mapping_table = null;
422         mapping_table = BasicTableParser.parse( mapping_table_file, "\t", false, false );
423         for( int row = 0; row < mapping_table.getNumberOfRows(); ++row ) {
424             final Map<String, String> row_map = new HashMap<String, String>();
425             String name = null;
426             for( int col = 0; col < mapping_table.getNumberOfColumns(); ++col ) {
427                 final String table_cell = mapping_table.getValue( col, row );
428                 if ( col == 0 ) {
429                     name = table_cell;
430                 }
431                 else if ( table_cell != null ) {
432                     final String key = table_cell.substring( 0, table_cell.indexOf( ':' ) );
433                     final String val = table_cell.substring( table_cell.indexOf( ':' ) + 1, table_cell.length() );
434                     row_map.put( key, val );
435                 }
436             }
437             map.put( name, row_map );
438         }
439         return map;
440     }
441
442     private static String deleteAtFirstSpace( final String name ) {
443         final int first_space = name.indexOf( " " );
444         if ( first_space > 1 ) {
445             return name.substring( 0, first_space ).trim();
446         }
447         return name;
448     }
449
450     private static String extractBracketedScientificNames( final PhylogenyNode node, final String new_value ) {
451         final int i = new_value.lastIndexOf( "[" );
452         final String scientific_name = new_value.substring( i + 1, new_value.length() - 1 );
453         ForesterUtil.ensurePresenceOfTaxonomy( node );
454         node.getNodeData().getTaxonomy().setScientificName( scientific_name );
455         return new_value.substring( 0, i - 1 ).trim();
456     }
457
458     private static String extractBracketedTaxCodes( final PhylogenyNode node, final String new_value ) {
459         final Matcher m = ParserUtils.TAXOMONY_CODE_PATTERN_4.matcher( new_value );
460         String tc = null;
461         if ( m.matches() ) {
462             tc = m.group( 1 );
463         }
464         ForesterUtil.ensurePresenceOfTaxonomy( node );
465         try {
466             node.getNodeData().getTaxonomy().setTaxonomyCode( tc );
467         }
468         catch ( final PhyloXmlDataFormatException e ) {
469             throw new IllegalArgumentException( "illegal format for taxonomy code: " + tc );
470         }
471         return new_value; //TODO //FIXME
472     }
473
474     private static String extractIntermediate( final Map<String, String> intermediate_map, final String name ) {
475         String new_name = null;
476         if ( PhylogenyDecorator.VERBOSE ) {
477             System.out.print( name + " => " );
478         }
479         if ( intermediate_map.containsKey( name ) ) {
480             new_name = intermediate_map.get( name );
481             if ( ForesterUtil.isEmpty( new_name ) ) {
482                 throw new IllegalArgumentException( "\"" + name + "\" maps to null or empty string in secondary map" );
483             }
484         }
485         else {
486             throw new IllegalArgumentException( "\"" + name + "\" not found in name secondary map" );
487         }
488         if ( PhylogenyDecorator.VERBOSE ) {
489             System.out.println( new_name + "  " );
490         }
491         return new_name;
492     }
493
494     private static String processNameIntelligently( final String name ) {
495         final String[] s = name.split( " " );
496         if ( s.length < 2 ) {
497             return name;
498         }
499         else if ( ( s[ 0 ].indexOf( "_" ) > 0 ) && ( s[ 0 ].indexOf( "|" ) > 0 ) ) {
500             return s[ 0 ];
501         }
502         else if ( ( s[ 1 ].indexOf( "_" ) > 0 ) && ( s[ 1 ].indexOf( "|" ) > 0 ) ) {
503             return s[ 1 ];
504         }
505         else if ( ( s[ 0 ].indexOf( "_" ) > 0 ) && ( s[ 0 ].indexOf( "." ) > 0 ) ) {
506             return s[ 0 ];
507         }
508         else if ( ( s[ 1 ].indexOf( "_" ) > 0 ) && ( s[ 1 ].indexOf( "." ) > 0 ) ) {
509             return s[ 1 ];
510         }
511         else if ( s[ 0 ].indexOf( "_" ) > 0 ) {
512             return s[ 0 ];
513         }
514         else if ( s[ 1 ].indexOf( "_" ) > 0 ) {
515             return s[ 1 ];
516         }
517         else {
518             return s[ 0 ];
519         }
520     }
521
522     private static String processSimilarTo( final String name ) {
523         final int i = name.toLowerCase().indexOf( "similar to" );
524         String similar_to = "";
525         if ( i >= 0 ) {
526             similar_to = " similarity=" + name.substring( i + 10 ).trim();
527         }
528         final String pi = processNameIntelligently( name );
529         return pi + similar_to;
530     }
531
532     private static String sanitize( String s ) {
533         s = s.replace( ' ', '_' );
534         s = s.replace( '(', '{' );
535         s = s.replace( ')', '}' );
536         s = s.replace( '[', '{' );
537         s = s.replace( ']', '}' );
538         s = s.replace( ',', '_' );
539         return s;
540     }
541
542     public static enum FIELD {
543         NODE_NAME, SEQUENCE_ANNOTATION_DESC, DOMAIN_STRUCTURE, TAXONOMY_CODE, TAXONOMY_SCIENTIFIC_NAME, SEQUENCE_NAME;
544     }
545 }