X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=forester%2Fjava%2Fsrc%2Forg%2Fforester%2Ftools%2FPhylogenyDecorator.java;h=9d865e1ef6630955b9b1f286aea650b7c8b389ff;hb=01d681ced8f186561a3dd76715d950bd0eabd82d;hp=2bf5ad232cbd0edf5b58d308b4ec52a050d11fe7;hpb=03e51d179caedf757b09e2872f9500318bd85a53;p=jalview.git diff --git a/forester/java/src/org/forester/tools/PhylogenyDecorator.java b/forester/java/src/org/forester/tools/PhylogenyDecorator.java index 2bf5ad2..9d865e1 100644 --- a/forester/java/src/org/forester/tools/PhylogenyDecorator.java +++ b/forester/java/src/org/forester/tools/PhylogenyDecorator.java @@ -21,7 +21,7 @@ // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA // // Contact: phylosoft @ gmail . com -// WWW: www.phylosoft.org/forester +// WWW: https://sites.google.com/site/cmzmasek/home/software/forester package org.forester.tools; @@ -29,11 +29,11 @@ import java.io.File; import java.io.IOException; import java.util.HashMap; import java.util.Map; -import java.util.regex.Pattern; +import java.util.regex.Matcher; -import org.forester.archaeopteryx.AptxUtil; import org.forester.io.parsers.nhx.NHXFormatException; import org.forester.io.parsers.phyloxml.PhyloXmlDataFormatException; +import org.forester.io.parsers.util.ParserUtils; import org.forester.phylogeny.Phylogeny; import org.forester.phylogeny.PhylogenyNode; import org.forester.phylogeny.data.Accession; @@ -49,25 +49,22 @@ import org.forester.util.ForesterUtil; public final class PhylogenyDecorator { // From evoruby/lib/evo/apps/tseq_taxonomy_processor.rb: - final private static String TP_TAXONOMY_CODE = "TAXONOMY_CODE"; - final private static String TP_TAXONOMY_ID = "TAXONOMY_ID"; - final private static String TP_TAXONOMY_ID_PROVIDER = "TAXONOMY_ID_PROVIDER"; - final private static String TP_TAXONOMY_SN = "TAXONOMY_SN"; - final private static String TP_TAXONOMY_CN = "TAXONOMY_CN"; - final private static String TP_TAXONOMY_SYN = "TAXONOMY_SYN"; - final private static String TP_SEQ_SYMBOL = "SEQ_SYMBOL"; - final private static String TP_SEQ_ACCESSION = "SEQ_ACCESSION"; - final private static String TP_SEQ_ACCESSION_SOURCE = "SEQ_ACCESSION_SOURCE"; - final private static String TP_SEQ_ANNOTATION_DESC = "SEQ_ANNOTATION_DESC"; - final private static String TP_SEQ_ANNOTATION_REF = "SEQ_ANNOTATION_REF"; - final private static String TP_SEQ_MOL_SEQ = "SEQ_MOL_SEQ"; - final private static String TP_SEQ_NAME = "SEQ_NAME"; - final private static String TP_NODE_NAME = "NODE_NAME"; - final private static Pattern NODENAME_SEQNUMBER_TAXDOMAINNUMBER = Pattern - .compile( "^([a-fA-Z0-9]{1,5})_([A-Z0-9]{2,4}[A-Z])(\\d{1,4})$" ); - public final static boolean SANITIZE = false; - public final static boolean VERBOSE = true; - private static final boolean CUT = true; + final private static String TP_TAXONOMY_CODE = "TAXONOMY_CODE"; + final private static String TP_TAXONOMY_ID = "TAXONOMY_ID"; + final private static String TP_TAXONOMY_ID_PROVIDER = "TAXONOMY_ID_PROVIDER"; + final private static String TP_TAXONOMY_SN = "TAXONOMY_SN"; + final private static String TP_TAXONOMY_CN = "TAXONOMY_CN"; + final private static String TP_TAXONOMY_SYN = "TAXONOMY_SYN"; + final private static String TP_SEQ_SYMBOL = "SEQ_SYMBOL"; + final private static String TP_SEQ_ACCESSION = "SEQ_ACCESSION"; + final private static String TP_SEQ_ACCESSION_SOURCE = "SEQ_ACCESSION_SOURCE"; + final private static String TP_SEQ_ANNOTATION_DESC = "SEQ_ANNOTATION_DESC"; + final private static String TP_SEQ_ANNOTATION_REF = "SEQ_ANNOTATION_REF"; + final private static String TP_SEQ_MOL_SEQ = "SEQ_MOL_SEQ"; + final private static String TP_SEQ_NAME = "SEQ_NAME"; + final private static String TP_NODE_NAME = "NODE_NAME"; + public final static boolean SANITIZE = false; + public final static boolean VERBOSE = true; private PhylogenyDecorator() { // Not needed. @@ -92,63 +89,63 @@ public final class PhylogenyDecorator { } if ( new_values != null ) { if ( new_values.containsKey( TP_TAXONOMY_CODE ) ) { - AptxUtil.ensurePresenceOfTaxonomy( node ); + ForesterUtil.ensurePresenceOfTaxonomy( node ); node.getNodeData().getTaxonomy().setTaxonomyCode( new_values.get( TP_TAXONOMY_CODE ) ); } if ( new_values.containsKey( TP_TAXONOMY_ID ) && new_values.containsKey( TP_TAXONOMY_ID_PROVIDER ) ) { - AptxUtil.ensurePresenceOfTaxonomy( node ); + ForesterUtil.ensurePresenceOfTaxonomy( node ); node.getNodeData() .getTaxonomy() .setIdentifier( new Identifier( new_values.get( TP_TAXONOMY_ID ), new_values.get( TP_TAXONOMY_ID_PROVIDER ) ) ); } else if ( new_values.containsKey( TP_TAXONOMY_ID ) ) { - AptxUtil.ensurePresenceOfTaxonomy( node ); + ForesterUtil.ensurePresenceOfTaxonomy( node ); node.getNodeData().getTaxonomy() .setIdentifier( new Identifier( new_values.get( TP_TAXONOMY_ID ) ) ); } if ( new_values.containsKey( TP_TAXONOMY_SN ) ) { - AptxUtil.ensurePresenceOfTaxonomy( node ); + ForesterUtil.ensurePresenceOfTaxonomy( node ); node.getNodeData().getTaxonomy().setScientificName( new_values.get( TP_TAXONOMY_SN ) ); } if ( new_values.containsKey( TP_TAXONOMY_CN ) ) { - AptxUtil.ensurePresenceOfTaxonomy( node ); + ForesterUtil.ensurePresenceOfTaxonomy( node ); node.getNodeData().getTaxonomy().setCommonName( new_values.get( TP_TAXONOMY_CN ) ); } if ( new_values.containsKey( TP_TAXONOMY_SYN ) ) { - AptxUtil.ensurePresenceOfTaxonomy( node ); + ForesterUtil.ensurePresenceOfTaxonomy( node ); node.getNodeData().getTaxonomy().getSynonyms().add( new_values.get( TP_TAXONOMY_SYN ) ); } if ( new_values.containsKey( TP_SEQ_ACCESSION ) && new_values.containsKey( TP_SEQ_ACCESSION_SOURCE ) ) { - AptxUtil.ensurePresenceOfSequence( node ); + ForesterUtil.ensurePresenceOfSequence( node ); node.getNodeData() .getSequence() .setAccession( new Accession( new_values.get( TP_SEQ_ACCESSION ), new_values.get( TP_SEQ_ACCESSION_SOURCE ) ) ); } if ( new_values.containsKey( TP_SEQ_ANNOTATION_DESC ) ) { - AptxUtil.ensurePresenceOfSequence( node ); - final Annotation ann = new Annotation( "?" ); + ForesterUtil.ensurePresenceOfSequence( node ); + final Annotation ann = new Annotation(); ann.setDesc( new_values.get( TP_SEQ_ANNOTATION_DESC ) ); node.getNodeData().getSequence().addAnnotation( ann ); } if ( new_values.containsKey( TP_SEQ_ANNOTATION_REF ) ) { - AptxUtil.ensurePresenceOfSequence( node ); + ForesterUtil.ensurePresenceOfSequence( node ); final Annotation ann = new Annotation( new_values.get( TP_SEQ_ANNOTATION_REF ) ); node.getNodeData().getSequence().addAnnotation( ann ); } if ( new_values.containsKey( TP_SEQ_SYMBOL ) ) { - AptxUtil.ensurePresenceOfSequence( node ); + ForesterUtil.ensurePresenceOfSequence( node ); node.getNodeData().getSequence().setSymbol( new_values.get( TP_SEQ_SYMBOL ) ); } if ( new_values.containsKey( TP_SEQ_NAME ) ) { - AptxUtil.ensurePresenceOfSequence( node ); + ForesterUtil.ensurePresenceOfSequence( node ); node.getNodeData().getSequence().setName( new_values.get( TP_SEQ_NAME ) ); } if ( new_values.containsKey( TP_SEQ_MOL_SEQ ) ) { - AptxUtil.ensurePresenceOfSequence( node ); + ForesterUtil.ensurePresenceOfSequence( node ); node.getNodeData().getSequence().setMolecularSequence( new_values.get( TP_SEQ_MOL_SEQ ) ); } if ( new_values.containsKey( TP_NODE_NAME ) ) { @@ -163,21 +160,6 @@ public final class PhylogenyDecorator { } } - /** - * - * - * - * - * - * @param phylogeny - * @param map - * maps names (in phylogeny) to new values - * @param field - * @param picky - * @throws IllegalArgumentException - * @throws NHXFormatException - * @throws PhyloXmlDataFormatException - */ public static void decorate( final Phylogeny phylogeny, final Map map, final FIELD field, @@ -235,34 +217,22 @@ public final class PhylogenyDecorator { 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" ); + } for( final PhylogenyNodeIterator iter = phylogeny.iteratorPostorder(); iter.hasNext(); ) { final PhylogenyNode node = iter.next(); String name = node.getName(); + String tilde_annotation = null; if ( trim_after_tilde && ( name.indexOf( '~' ) > 0 ) ) { - name = name.substring( 0, name.indexOf( '~' ) ); + final int ti = name.indexOf( '~' ); + tilde_annotation = name.substring( ti ); + name = name.substring( 0, ti ); } if ( !ForesterUtil.isEmpty( name ) ) { if ( intermediate_map != null ) { name = PhylogenyDecorator.extractIntermediate( intermediate_map, name ); } - // int space_index = name.indexOf( " " ); - // if ( CUT && space_index > 0 ) { - // int y = name.lastIndexOf( "|" ); - // name = name.substring( y + 1, space_index ); - // } - // String new_value = null; - // for( String key : map.keySet() ) { - // if ( key.indexOf( name ) >= 0 ) { - // if ( new_value == null ) { - // new_value = map.get( key ); - // } - // else { - // System.out.println( name + " is not unique" ); - // System.exit( -1 ); - // } - // } - // } - // if ( new_value != null ) { if ( map.containsKey( name ) || ( numbers_of_chars_allowed_to_remove_if_not_found_in_map > 0 ) ) { String new_value = map.get( name ); int x = 0; @@ -277,8 +247,14 @@ public final class PhylogenyDecorator { if ( extract_bracketed_scientific_name && new_value.endsWith( "]" ) ) { new_value = extractBracketedScientificNames( node, new_value ); } - else if ( extract_bracketed_tax_code && new_value.endsWith( "]" ) ) { - new_value = extractBracketedTaxCodes( node, new_value ); + else if ( extract_bracketed_tax_code ) { + if ( ParserUtils.TAXOMONY_CODE_PATTERN_4.matcher( new_value ).find() ) { + new_value = extractBracketedTaxCodes( node, new_value ); + } + else if ( picky ) { + throw new IllegalArgumentException( " could not get taxonomy from \"" + new_value + + "\"" ); + } } switch ( field ) { case SEQUENCE_ANNOTATION_DESC: @@ -306,17 +282,20 @@ public final class PhylogenyDecorator { if ( PhylogenyDecorator.VERBOSE ) { System.out.println( name + ": " + new_value ); } - AptxUtil.ensurePresenceOfTaxonomy( node ); + ForesterUtil.ensurePresenceOfTaxonomy( node ); node.getNodeData().getTaxonomy().setTaxonomyCode( new_value ); break; case TAXONOMY_SCIENTIFIC_NAME: if ( PhylogenyDecorator.VERBOSE ) { System.out.println( name + ": " + new_value ); } - AptxUtil.ensurePresenceOfTaxonomy( node ); + ForesterUtil.ensurePresenceOfTaxonomy( node ); node.getNodeData().getTaxonomy().setScientificName( new_value ); break; case SEQUENCE_NAME: + if ( trim_after_tilde ) { + new_value = addTildeAnnotation( tilde_annotation, new_value ); + } if ( PhylogenyDecorator.VERBOSE ) { System.out.println( name + ": " + new_value ); } @@ -350,6 +329,9 @@ public final class PhylogenyDecorator { if ( PhylogenyDecorator.SANITIZE ) { new_value = PhylogenyDecorator.sanitize( new_value ); } + if ( trim_after_tilde ) { + new_value = addTildeAnnotation( tilde_annotation, new_value ); + } if ( PhylogenyDecorator.VERBOSE ) { System.out.println( new_value ); } @@ -367,6 +349,13 @@ public final class PhylogenyDecorator { } } + private final static String addTildeAnnotation( final String tilde_annotation, final String new_value ) { + if ( ForesterUtil.isEmpty( tilde_annotation ) ) { + return new_value; + } + return new_value + tilde_annotation; + } + public static void decorate( final Phylogeny[] phylogenies, final Map> map, final boolean picky, @@ -434,6 +423,30 @@ public final class PhylogenyDecorator { } } + public static Map> parseMappingTable( final File mapping_table_file ) + throws IOException { + final Map> map = new HashMap>(); + BasicTable mapping_table = null; + mapping_table = BasicTableParser.parse( mapping_table_file, '\t', false, false ); + for( int row = 0; row < mapping_table.getNumberOfRows(); ++row ) { + final Map row_map = new HashMap(); + String name = null; + for( int col = 0; col < mapping_table.getNumberOfColumns(); ++col ) { + final String table_cell = mapping_table.getValue( col, row ); + if ( col == 0 ) { + name = table_cell; + } + else if ( table_cell != null ) { + final String key = table_cell.substring( 0, table_cell.indexOf( ':' ) ); + final String val = table_cell.substring( table_cell.indexOf( ':' ) + 1, table_cell.length() ); + row_map.put( key, val ); + } + } + map.put( name, row_map ); + } + return map; + } + private static String deleteAtFirstSpace( final String name ) { final int first_space = name.indexOf( " " ); if ( first_space > 1 ) { @@ -445,22 +458,25 @@ public final class PhylogenyDecorator { private static String extractBracketedScientificNames( final PhylogenyNode node, final String new_value ) { final int i = new_value.lastIndexOf( "[" ); final String scientific_name = new_value.substring( i + 1, new_value.length() - 1 ); - AptxUtil.ensurePresenceOfTaxonomy( node ); + ForesterUtil.ensurePresenceOfTaxonomy( node ); node.getNodeData().getTaxonomy().setScientificName( scientific_name ); return new_value.substring( 0, i - 1 ).trim(); } private static String extractBracketedTaxCodes( final PhylogenyNode node, final String new_value ) { - final int i = new_value.lastIndexOf( "[" ); - final String tc = new_value.substring( i + 1, new_value.length() - 1 ); - AptxUtil.ensurePresenceOfTaxonomy( node ); + final Matcher m = ParserUtils.TAXOMONY_CODE_PATTERN_4.matcher( new_value ); + String tc = "?"; + if ( m.find() ) { + tc = m.group( 1 ); + } + ForesterUtil.ensurePresenceOfTaxonomy( node ); try { node.getNodeData().getTaxonomy().setTaxonomyCode( tc ); } catch ( final PhyloXmlDataFormatException e ) { throw new IllegalArgumentException( "illegal format for taxonomy code: " + tc ); } - return new_value.substring( 0, i - 1 ).trim(); + return new_value; //TODO //FIXME } private static String extractIntermediate( final Map intermediate_map, final String name ) { @@ -483,30 +499,6 @@ public final class PhylogenyDecorator { return new_name; } - public static Map> parseMappingTable( final File mapping_table_file ) - throws IOException { - final Map> map = new HashMap>(); - BasicTable mapping_table = null; - mapping_table = BasicTableParser.parse( mapping_table_file, "\t", false, false ); - for( int row = 0; row < mapping_table.getNumberOfRows(); ++row ) { - final Map row_map = new HashMap(); - String name = null; - for( int col = 0; col < mapping_table.getNumberOfColumns(); ++col ) { - final String table_cell = mapping_table.getValue( col, row ); - if ( col == 0 ) { - name = table_cell; - } - else if ( table_cell != null ) { - final String key = table_cell.substring( 0, table_cell.indexOf( ':' ) ); - final String val = table_cell.substring( table_cell.indexOf( ':' ) + 1, table_cell.length() ); - row_map.put( key, val ); - } - } - map.put( name, row_map ); - } - return map; - } - private static String processNameIntelligently( final String name ) { final String[] s = name.split( " " ); if ( s.length < 2 ) {