From 5c4e24e304f57058fa1c3a3f1256a573b37d89f6 Mon Sep 17 00:00:00 2001 From: "cmzmasek@gmail.com" Date: Tue, 24 Sep 2013 02:20:40 +0000 Subject: [PATCH] inprogress --- .../src/org/forester/application/decoratorX.java | 32 ++-- .../src/org/forester/archaeopteryx/AptxUtil.java | 116 +++++++++++--- .../src/org/forester/archaeopteryx/TreePanel.java | 30 +--- .../org/forester/phylogeny/data/Annotation.java | 168 ++++++++++++-------- .../src/org/forester/tools/PhylogenyDecorator.java | 2 +- 5 files changed, 217 insertions(+), 131 deletions(-) diff --git a/forester/java/src/org/forester/application/decoratorX.java b/forester/java/src/org/forester/application/decoratorX.java index c05ce86..916a4b7 100644 --- a/forester/java/src/org/forester/application/decoratorX.java +++ b/forester/java/src/org/forester/application/decoratorX.java @@ -1,6 +1,6 @@ // java -Xmx2048m -cp // ~/SOFTWARE_DEV/ECLIPSE_WORKSPACE/forester/java/forester.jar -// org.forester.application.decorator2 +// org.forester.application.decoratorX // RRMa_ALL_plus_RRMa_ee3_50_hmmalign_05_40_fme_with_seqs_2.phylo.xml // nature12311-s3_cz_4.txt x @@ -43,16 +43,19 @@ public class decoratorX { final PhyloXmlParser xml_parser = new PhyloXmlParser(); final Phylogeny phy = factory.create( intree, xml_parser )[ 0 ]; final BasicTable t = BasicTableParser.parse( intable, '\t' ); - System.out.println( t.toString() ); - final PhylogenyNodeIterator it = phy.iteratorPostorder(); + // System.out.println( t.toString() ); + final PhylogenyNodeIterator it = phy.iteratorExternalForward(); int i = 0; while ( it.hasNext() ) { final PhylogenyNode node = it.next(); - if ( node.isExternal() ) { - processNode( node, t ); - } + processNode( node, t ); i++; } + final PhylogenyNodeIterator it2 = phy.iteratorExternalForward(); + while ( it2.hasNext() ) { + final PhylogenyNode node = it2.next(); + processNode2( node, phy ); + } final PhylogenyWriter writer = new PhylogenyWriter(); writer.toPhyloXML( outtree, phy, 0 ); } @@ -63,17 +66,17 @@ public class decoratorX { } } - private static void processNode( final PhylogenyNode node, BasicTable t ) throws Exception { - String node_seq = node.getNodeData().getSequence().getMolecularSequence().toUpperCase(); + private static void processNode( final PhylogenyNode node, final BasicTable t ) throws Exception { + final String node_seq = node.getNodeData().getSequence().getMolecularSequence().toUpperCase(); boolean found = false; for( int col = 0; col < t.getNumberOfRows(); ++col ) { - String table_seq = t.getValueAsString( SEQ_COLUMN, col ).toUpperCase(); + final String table_seq = t.getValueAsString( SEQ_COLUMN, col ).toUpperCase(); if ( table_seq.contains( node_seq ) ) { if ( found ) { - throw new Exception( "Sequence from node " + node + " is not unique: " + node_seq ); + // throw new Exception( "Sequence from node " + node + " is not unique: " + node_seq ); } found = true; - Annotation annotation = new Annotation( "target:" + t.getValueAsString( TARGET_COLUMN, col ) ); + final Annotation annotation = new Annotation( "target", t.getValueAsString( TARGET_COLUMN, col ) ); node.getNodeData().getSequence().addAnnotation( annotation ); System.out.println( node + "->" + annotation ); } @@ -82,4 +85,11 @@ public class decoratorX { // throw new Exception( "Sequence from node " + node + " not found: " + node_seq ); // } } + + private static void processNode2( final PhylogenyNode node, final Phylogeny t ) { + if ( ( node.getNodeData().getSequence().getAnnotations() == null ) + || node.getNodeData().getSequence().getAnnotations().isEmpty() ) { + t.deleteSubtree( node, true ); + } + } } diff --git a/forester/java/src/org/forester/archaeopteryx/AptxUtil.java b/forester/java/src/org/forester/archaeopteryx/AptxUtil.java index 3ba5102..8ceb398 100644 --- a/forester/java/src/org/forester/archaeopteryx/AptxUtil.java +++ b/forester/java/src/org/forester/archaeopteryx/AptxUtil.java @@ -43,6 +43,7 @@ import java.net.URI; import java.net.URL; import java.net.URLEncoder; import java.text.ParseException; +import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; @@ -50,8 +51,11 @@ import java.util.Iterator; import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.Map.Entry; import java.util.Set; +import java.util.SortedMap; import java.util.SortedSet; +import java.util.TreeMap; import java.util.TreeSet; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -78,6 +82,7 @@ import org.forester.phylogeny.PhylogenyMethods; import org.forester.phylogeny.PhylogenyMethods.DESCENDANT_SORT_PRIORITY; import org.forester.phylogeny.PhylogenyNode; import org.forester.phylogeny.data.Accession; +import org.forester.phylogeny.data.Annotation; import org.forester.phylogeny.data.BranchColor; import org.forester.phylogeny.data.Taxonomy; import org.forester.phylogeny.factories.ParserBasedPhylogenyFactory; @@ -93,16 +98,27 @@ import org.forester.ws.seqdb.UniProtTaxonomy; public final class AptxUtil { + private final static String[] AVAILABLE_FONT_FAMILIES_SORTED = GraphicsEnvironment.getLocalGraphicsEnvironment() + .getAvailableFontFamilyNames(); private final static Pattern seq_identifier_pattern_1 = Pattern .compile( "^([A-Za-z]{2,5})[|=:]([0-9A-Za-z_\\.]{5,40})\\s*$" ); private final static Pattern seq_identifier_pattern_2 = Pattern .compile( "^([A-Za-z]{2,5})[|=:]([0-9A-Za-z_\\.]{5,40})[|,; ].*$" ); - private final static String[] AVAILABLE_FONT_FAMILIES_SORTED = GraphicsEnvironment.getLocalGraphicsEnvironment() - .getAvailableFontFamilyNames(); static { Arrays.sort( AVAILABLE_FONT_FAMILIES_SORTED ); } + public static MaskFormatter createMaskFormatter( final String s ) { + MaskFormatter formatter = null; + try { + formatter = new MaskFormatter( s ); + } + catch ( final ParseException e ) { + throw new IllegalArgumentException( e ); + } + return formatter; + } + public final static String createUriForSeqWeb( final PhylogenyNode node, final Configuration conf, final TreePanel tp ) { @@ -166,17 +182,6 @@ public final class AptxUtil { return uri_str; } - public static MaskFormatter createMaskFormatter( final String s ) { - MaskFormatter formatter = null; - try { - formatter = new MaskFormatter( s ); - } - catch ( final ParseException e ) { - throw new IllegalArgumentException( e ); - } - return formatter; - } - final static public boolean isHasAtLeastNodeWithEvent( final Phylogeny phy ) { final PhylogenyNodeIterator it = phy.iteratorPostorder(); while ( it.hasNext() ) { @@ -538,25 +543,41 @@ public final class AptxUtil { lookAtSomeTreePropertiesForAptxControlSettings( phy, main_panel.getControlPanel(), configuration ); } - final static Color calculateColorFromString( final String str ) { - final String species_uc = str.toUpperCase(); - char first = species_uc.charAt( 0 ); + final static Color calculateColorFromString( final String str, final boolean is_taxonomy ) { + final String my_str = str.toUpperCase(); + char first = my_str.charAt( 0 ); char second = ' '; char third = ' '; - if ( species_uc.length() > 1 ) { - second = species_uc.charAt( 1 ); - if ( species_uc.length() > 2 ) { - if ( species_uc.indexOf( " " ) > 0 ) { - third = species_uc.charAt( species_uc.indexOf( " " ) + 1 ); - } - else { - third = species_uc.charAt( 2 ); + if ( my_str.length() > 1 ) { + if ( is_taxonomy ) { + second = my_str.charAt( 1 ); + } + else { + second = my_str.charAt( my_str.length() - 1 ); + } + if ( is_taxonomy ) { + if ( my_str.length() > 2 ) { + if ( my_str.indexOf( " " ) > 0 ) { + third = my_str.charAt( my_str.indexOf( " " ) + 1 ); + } + else { + third = my_str.charAt( 2 ); + } } } + else if ( my_str.length() > 2 ) { + third = ( char ) ( Math.abs( str.hashCode() / 16909320 ) ); + System.out.println( str.hashCode() ); + } } first = AptxUtil.normalizeCharForRGB( first ); second = AptxUtil.normalizeCharForRGB( second ); - third = AptxUtil.normalizeCharForRGB( third ); + if ( is_taxonomy ) { + third = AptxUtil.normalizeCharForRGB( third ); + } + else { + third = third > 255 ? 255 : third; + } if ( ( first > 235 ) && ( second > 235 ) && ( third > 235 ) ) { first = 0; } @@ -729,6 +750,49 @@ public final class AptxUtil { return colorizations; } + final static String createAnnotationString( final SortedSet annotations ) { + final SortedMap> m = new TreeMap>(); + for( final Annotation an : annotations ) { + final String ref_source = ForesterUtil.isEmpty( an.getRefSource() ) ? "?" : an.getRefSource(); + if ( !m.containsKey( ref_source ) ) { + m.put( ref_source, new ArrayList() ); + } + m.get( ref_source ).add( an ); + } + final StringBuilder sb = new StringBuilder(); + for( final Entry> e : m.entrySet() ) { + final String ref_source = e.getKey(); + final List ans = e.getValue(); + if ( m.size() > 1 ) { + sb.append( "[" ); + } + if ( !ref_source.equals( "?" ) ) { + sb.append( ref_source ); + sb.append( ": " ); + } + for( int i = 0; i < ans.size(); ++i ) { + final Annotation an = ans.get( i ); + if ( !ForesterUtil.isEmpty( an.getRefValue() ) ) { + sb.append( an.getRefValue() ); + sb.append( " " ); + } + if ( !ForesterUtil.isEmpty( an.getDesc() ) ) { + sb.append( an.getDesc() ); + } + if ( sb.charAt( sb.length() - 1 ) == ' ' ) { + sb.deleteCharAt( sb.length() - 1 ); + } + if ( i < ans.size() - 1 ) { + sb.append( ", " ); + } + } + if ( m.size() > 1 ) { + sb.append( "] " ); + } + } + return sb.toString(); + } + final static String createBasicInformation( final Phylogeny phy ) { final StringBuilder desc = new StringBuilder(); if ( ( phy != null ) && !phy.isEmpty() ) { @@ -1329,7 +1393,7 @@ public final class AptxUtil { // br.close(); // } public static enum GraphicsExportType { - GIF( "gif" ), JPG( "jpg" ), PDF( "pdf" ), PNG( "png" ), TIFF( "tif" ), BMP( "bmp" ); + BMP( "bmp" ), GIF( "gif" ), JPG( "jpg" ), PDF( "pdf" ), PNG( "png" ), TIFF( "tif" ); private final String _suffix; diff --git a/forester/java/src/org/forester/archaeopteryx/TreePanel.java b/forester/java/src/org/forester/archaeopteryx/TreePanel.java index 151a3e5..1eaadd3 100644 --- a/forester/java/src/org/forester/archaeopteryx/TreePanel.java +++ b/forester/java/src/org/forester/archaeopteryx/TreePanel.java @@ -620,8 +620,8 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee if ( getControlPanel().isShowAnnotation() && ( node.getNodeData().getSequence().getAnnotations() != null ) && !node.getNodeData().getSequence().getAnnotations().isEmpty() ) { - sum += getTreeFontSet()._fm_large.stringWidth( node.getNodeData().getSequence().getAnnotation( 0 ) - .asSimpleText() + sum += getTreeFontSet()._fm_large.stringWidth( AptxUtil.createAnnotationString( node.getNodeData() + .getSequence().getAnnotations() ) + " " ); } if ( getControlPanel().isShowDomainArchitectures() @@ -711,11 +711,11 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee } if ( c == null ) { if ( !ForesterUtil.isEmpty( tax.getTaxonomyCode() ) ) { - c = AptxUtil.calculateColorFromString( tax.getTaxonomyCode() ); + c = AptxUtil.calculateColorFromString( tax.getTaxonomyCode(), true ); getControlPanel().getSpeciesColors().put( tax.getTaxonomyCode(), c ); } else { - c = AptxUtil.calculateColorFromString( tax.getScientificName() ); + c = AptxUtil.calculateColorFromString( tax.getScientificName(), true ); getControlPanel().getSpeciesColors().put( tax.getScientificName(), c ); } } @@ -2334,13 +2334,13 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee if ( getControlPanel().isColorAccordingToAnnotation() && ( getControlPanel().getAnnotationColors() != null ) ) { final StringBuilder sb = new StringBuilder(); for( final Annotation a : ann ) { - sb.append( !ForesterUtil.isEmpty( a.getRef() ) ? a.getRef() : a.getDesc() ); + sb.append( !ForesterUtil.isEmpty( a.getRefValue() ) ? a.getRefValue() : a.getDesc() ); } final String ann_str = sb.toString(); if ( !ForesterUtil.isEmpty( ann_str ) ) { c = getControlPanel().getAnnotationColors().get( ann_str ); if ( c == null ) { - c = AptxUtil.calculateColorFromString( ann_str ); + c = AptxUtil.calculateColorFromString( ann_str, false ); getControlPanel().getAnnotationColors().put( ann_str, c ); } if ( c == null ) { @@ -2429,22 +2429,6 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee repaint(); } - private String createAnnotationString( final SortedSet ann ) { - final StringBuilder sb = new StringBuilder(); - boolean first = true; - for( final Annotation a : ann ) { - if ( !first ) { - sb.append( "+" ); - } - else { - first = false; - } - sb.append( a.asSimpleText() ); - } - final String ann_str = sb.toString(); - return ann_str; - } - final private String createASimpleTextRepresentationOfANode( final PhylogenyNode node ) { final String tax = PhylogenyMethods.getSpecies( node ); String label = node.getName(); @@ -4111,7 +4095,7 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee else if ( getControlPanel().isColorAccordingToAnnotation() ) { g.setColor( calculateColorForAnnotation( ann ) ); } - final String ann_str = createAnnotationString( ann ); + final String ann_str = AptxUtil.createAnnotationString( ann ); TreePanel.drawString( ann_str, node.getXcoord() + x + 3 + half_box_size, node.getYcoord() + ( getTreeFontSet()._fm_large.getAscent() / down_shift_factor ), g ); _sb.setLength( 0 ); diff --git a/forester/java/src/org/forester/phylogeny/data/Annotation.java b/forester/java/src/org/forester/phylogeny/data/Annotation.java index 260c055..99f9691 100644 --- a/forester/java/src/org/forester/phylogeny/data/Annotation.java +++ b/forester/java/src/org/forester/phylogeny/data/Annotation.java @@ -36,32 +36,53 @@ import org.forester.util.ForesterUtil; public class Annotation implements PhylogenyData, MultipleUris, Comparable { + private Confidence _confidence; private String _desc; - private String _type; - private String _source; - private final String _ref; private String _evidence; - private Confidence _confidence; private PropertiesMap _properties; + private final String _ref_source; + private final String _ref_value; + private String _source; + private String _type; private List _uris; + public Annotation() { + _ref_value = ""; + _ref_source = ""; + init(); + } + public Annotation( final String ref ) { if ( ForesterUtil.isEmpty( ref ) ) { throw new IllegalArgumentException( "annotation reference is empty or null" ); } - if ( ( ref.indexOf( ':' ) < 1 ) || ( ref.indexOf( ':' ) > ref.length() - 2 ) || ( ref.length() < 3 ) ) { + final String s[] = ref.split( ":" ); + if ( ( s.length != 2 ) || ForesterUtil.isEmpty( s[ 0 ] ) || ForesterUtil.isEmpty( s[ 1 ] ) ) { throw new IllegalArgumentException( "illegal format for annotation reference: [" + ref + "]" ); } - _ref = ref; + _ref_source = s[ 0 ]; + _ref_value = s[ 1 ]; init(); } - public Annotation() { - _ref = ""; + public Annotation( final String ref_source, final String ref_value ) { + if ( ForesterUtil.isEmpty( ref_source ) || ForesterUtil.isEmpty( ref_value ) ) { + throw new IllegalArgumentException( "illegal format for annotation reference" ); + } + _ref_source = ref_source; + _ref_value = ref_value; init(); } @Override + public void addUri( final Uri uri ) { + if ( getUris() == null ) { + setUris( new ArrayList() ); + } + getUris().add( uri ); + } + + @Override public StringBuffer asSimpleText() { return new StringBuffer( !ForesterUtil.isEmpty( getRef() ) ? getRef() : getDesc() ); } @@ -72,17 +93,28 @@ public class Annotation implements PhylogenyData, MultipleUris, Comparable getUris() { + return _uris; } @Override @@ -174,6 +238,11 @@ public class Annotation implements PhylogenyData, MultipleUris, Comparable uris ) { + _uris = uris; + } + + @Override public StringBuffer toNHX() { throw new UnsupportedOperationException(); } @@ -234,54 +303,13 @@ public class Annotation implements PhylogenyData, MultipleUris, Comparable() ); - } - getUris().add( uri ); - } - - @Override - public Uri getUri( final int index ) { - return getUris().get( index ); - } - - @Override - public List getUris() { - return _uris; - } - - @Override - public void setUris( final List uris ) { - _uris = uris; - } - - @Override - public boolean equals( final Object o ) { - if ( this == o ) { - return true; - } - else if ( o == null ) { - return false; - } - else if ( o.getClass() != this.getClass() ) { - throw new IllegalArgumentException( "attempt to check [" + this.getClass() + "] equality to " + o + " [" - + o.getClass() + "]" ); - } - else { - return isEqual( ( Annotation ) o ); - } - } - - @Override - public int compareTo( final Annotation o ) { - if ( equals( o ) ) { - return 0; - } - if ( getRef().equals( o.getRef() ) ) { - return getDesc().compareTo( o.getDesc() ); - } - return getRef().compareTo( o.getRef() ); + private void init() { + _desc = ""; + _type = ""; + _source = ""; + _evidence = ""; + _confidence = null; + _properties = null; + setUris( null ); } } diff --git a/forester/java/src/org/forester/tools/PhylogenyDecorator.java b/forester/java/src/org/forester/tools/PhylogenyDecorator.java index 8da1094..57ef9f0 100644 --- a/forester/java/src/org/forester/tools/PhylogenyDecorator.java +++ b/forester/java/src/org/forester/tools/PhylogenyDecorator.java @@ -267,7 +267,7 @@ public final class PhylogenyDecorator { if ( !node.getNodeData().isHasSequence() ) { node.getNodeData().setSequence( new Sequence() ); } - final Annotation annotation = new Annotation( "?" ); + final Annotation annotation = new Annotation(); annotation.setDesc( new_value ); node.getNodeData().getSequence().addAnnotation( annotation ); break; -- 1.7.10.2