JAL-2805 made needed color set methods public
[jalview.git] / forester / java / src / org / forester / tools / PhylogenyDecorator.java
index 6ba9460..3a5ba40 100644 (file)
@@ -42,6 +42,7 @@ import org.forester.phylogeny.data.DomainArchitecture;
 import org.forester.phylogeny.data.Identifier;
 import org.forester.phylogeny.data.Sequence;
 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
+import org.forester.sequence.MolecularSequence.TYPE;
 import org.forester.util.BasicTable;
 import org.forester.util.BasicTableParser;
 import org.forester.util.ForesterUtil;
@@ -70,7 +71,8 @@ public final class PhylogenyDecorator {
 
     public static void decorate( final Phylogeny phylogeny,
                                  final Map<String, Map<String, String>> map,
-                                 final boolean picky ) throws IllegalArgumentException, PhyloXmlDataFormatException {
+                                 final boolean picky )
+            throws IllegalArgumentException, PhyloXmlDataFormatException {
         for( final PhylogenyNodeIterator iter = phylogeny.iteratorPostorder(); iter.hasNext(); ) {
             final PhylogenyNode node = iter.next();
             final String name = node.getName();
@@ -85,8 +87,7 @@ public final class PhylogenyDecorator {
                         if ( new_values.containsKey( TP_TAXONOMY_ID )
                                 && new_values.containsKey( TP_TAXONOMY_ID_PROVIDER ) ) {
                             ForesterUtil.ensurePresenceOfTaxonomy( node );
-                            node.getNodeData()
-                                    .getTaxonomy()
+                            node.getNodeData().getTaxonomy()
                                     .setIdentifier( new Identifier( new_values.get( TP_TAXONOMY_ID ),
                                                                     new_values.get( TP_TAXONOMY_ID_PROVIDER ) ) );
                         }
@@ -110,8 +111,7 @@ public final class PhylogenyDecorator {
                         if ( new_values.containsKey( TP_SEQ_ACCESSION )
                                 && new_values.containsKey( TP_SEQ_ACCESSION_SOURCE ) ) {
                             ForesterUtil.ensurePresenceOfSequence( node );
-                            node.getNodeData()
-                                    .getSequence()
+                            node.getNodeData().getSequence()
                                     .setAccession( new Accession( new_values.get( TP_SEQ_ACCESSION ),
                                                                   new_values.get( TP_SEQ_ACCESSION_SOURCE ) ) );
                         }
@@ -141,7 +141,7 @@ public final class PhylogenyDecorator {
                         if ( new_values.containsKey( TP_NODE_NAME ) ) {
                             node.setName( new_values.get( TP_NODE_NAME ) );
                         }
-                    } // if ( new_values != null ) 
+                    } // if ( new_values != null )
                 } // if ( map.containsKey( name ) || ( numbers_of_chars_allowed_to_remove_if_not_found_in_map > 0 ) )
                 else if ( picky ) {
                     throw new IllegalArgumentException( "\"" + name + "\" not found in name map" );
@@ -158,8 +158,8 @@ public final class PhylogenyDecorator {
                                    final boolean picky,
                                    final boolean cut_name_after_space,
                                    final boolean trim_after_tilde,
-                                   final boolean verbose ) throws IllegalArgumentException, NHXFormatException,
-            PhyloXmlDataFormatException {
+                                   final boolean verbose )
+            throws IllegalArgumentException, NHXFormatException, PhyloXmlDataFormatException {
         return PhylogenyDecorator.decorate( phylogeny,
                                             map,
                                             field,
@@ -173,9 +173,9 @@ public final class PhylogenyDecorator {
     }
 
     /**
-     * 
-     * 
-     * 
+     *
+     *
+     *
      * @param phylogeny
      * @param map
      *            maps names (in phylogeny) to new values if intermediate_map is
@@ -185,7 +185,7 @@ public final class PhylogenyDecorator {
      * @param intermediate_map
      *            maps name (in phylogeny) to a intermediate value
      * @throws IllegalArgumentException
-     * @throws PhyloXmlDataFormatException 
+     * @throws PhyloXmlDataFormatException
      */
     public static String decorate( final Phylogeny phylogeny,
                                    final Map<String, String> map,
@@ -196,16 +196,14 @@ public final class PhylogenyDecorator {
                                    final Map<String, String> intermediate_map,
                                    final boolean cut_name_after_space,
                                    final boolean trim_after_tilde,
-                                   final boolean verbose ) throws IllegalArgumentException, PhyloXmlDataFormatException {
+                                   final boolean verbose )
+            throws IllegalArgumentException, PhyloXmlDataFormatException {
         if ( extract_bracketed_scientific_name && ( field == FIELD.TAXONOMY_SCIENTIFIC_NAME ) ) {
             throw new IllegalArgumentException( "attempt to extract bracketed scientific name together with data field pointing to scientific name" );
         }
         if ( map.isEmpty() ) {
             throw new IllegalArgumentException( "map is empty" );
         }
-        if ( picky && ( map.size() < phylogeny.getNumberOfExternalNodes() ) ) {
-            throw new IllegalArgumentException( "map contains less entries than the tree has external nodes" );
-        }
         int ext_nodes = 0;
         int ext_nodes_updated = 0;
         int int_nodes = 0;
@@ -223,19 +221,22 @@ public final class PhylogenyDecorator {
                 throw new IllegalArgumentException( "external node with no name present" );
             }
             String tilde_annotation = null;
+            final String orig_name = name;
             if ( trim_after_tilde && ( name.indexOf( '~' ) > 0 ) ) {
                 final int ti = name.indexOf( '~' );
-                final String orig = name;
                 tilde_annotation = name.substring( ti );
                 name = name.substring( 0, ti );
                 if ( node.isExternal() && ForesterUtil.isEmpty( name ) ) {
-                    throw new IllegalArgumentException( "external node with illegal name: " + orig );
+                    throw new IllegalArgumentException( "external node with illegal name: " + orig_name );
                 }
             }
             if ( !ForesterUtil.isEmpty( name ) ) {
                 if ( intermediate_map != null ) {
                     name = PhylogenyDecorator.extractIntermediate( intermediate_map, name, verbose );
                 }
+                if ( ( field == FIELD.MOL_SEQ ) && !map.containsKey( name ) ) {
+                    name = orig_name;
+                }
                 if ( map.containsKey( name ) ) {
                     String new_value = map.get( name ).trim().replaceAll( "/\\s+/", " " );
                     if ( !ForesterUtil.isEmpty( new_value ) ) {
@@ -266,6 +267,18 @@ public final class PhylogenyDecorator {
                                     node.getNodeData().setSequence( new Sequence() );
                                 }
                                 node.getNodeData().getSequence().setMolecularSequence( new_value );
+                                final TYPE type = ForesterUtil.guessMolecularSequenceType( new_value );
+                                if ( type != null ) {
+                                    if ( type == TYPE.AA ) {
+                                        node.getNodeData().getSequence().setType( "protein" );
+                                    }
+                                    else if ( type == TYPE.DNA ) {
+                                        node.getNodeData().getSequence().setType( "dna" );
+                                    }
+                                    else if ( type == TYPE.RNA ) {
+                                        node.getNodeData().getSequence().setType( "rna" );
+                                    }
+                                }
                                 break;
                             case SEQUENCE_ANNOTATION_DESC:
                                 if ( verbose ) {
@@ -340,7 +353,7 @@ public final class PhylogenyDecorator {
                         throw new IllegalArgumentException( "node name \"" + name + "\" maps to empty value" );
                     }
                 }
-                else if ( picky ) {
+                else if ( picky && node.isExternal() ) {
                     throw new IllegalArgumentException( "node name \"" + name + "\" not found in map" );
                 }
             }
@@ -446,12 +459,12 @@ public final class PhylogenyDecorator {
     }
 
     public static enum FIELD {
-        DOMAIN_STRUCTURE,
-        MOL_SEQ,
-        NODE_NAME,
-        SEQUENCE_ANNOTATION_DESC,
-        SEQUENCE_NAME,
-        TAXONOMY_CODE,
-        TAXONOMY_SCIENTIFIC_NAME;
+                              DOMAIN_STRUCTURE,
+                              MOL_SEQ,
+                              NODE_NAME,
+                              SEQUENCE_ANNOTATION_DESC,
+                              SEQUENCE_NAME,
+                              TAXONOMY_CODE,
+                              TAXONOMY_SCIENTIFIC_NAME;
     }
 }