X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=forester%2Fjava%2Fsrc%2Forg%2Fforester%2Farchaeopteryx%2FTreePanelUtil.java;h=c764eae9c2e29490ef903251c8b4c56fb86eeb39;hb=c0439ed8b088887ffea2faf11bc7897333287cb3;hp=61db054456e1d2841040f3c6bcdb7428d94717ad;hpb=397137f4f105810096dc6034ccc1e1013ed11644;p=jalview.git diff --git a/forester/java/src/org/forester/archaeopteryx/TreePanelUtil.java b/forester/java/src/org/forester/archaeopteryx/TreePanelUtil.java index 61db054..c764eae 100644 --- a/forester/java/src/org/forester/archaeopteryx/TreePanelUtil.java +++ b/forester/java/src/org/forester/archaeopteryx/TreePanelUtil.java @@ -6,6 +6,7 @@ import java.awt.Component; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.util.ArrayList; +import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -22,9 +23,10 @@ import org.forester.analysis.TaxonomyDataManager; import org.forester.phylogeny.Phylogeny; import org.forester.phylogeny.PhylogenyMethods; 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.NodeData.NODE_DATA; +import org.forester.phylogeny.data.NodeDataField; import org.forester.phylogeny.data.Sequence; import org.forester.phylogeny.data.Taxonomy; import org.forester.phylogeny.iterators.PhylogenyNodeIterator; @@ -32,50 +34,11 @@ import org.forester.phylogeny.iterators.PreorderTreeIterator; import org.forester.util.ForesterConstants; import org.forester.util.ForesterUtil; import org.forester.util.SequenceAccessionTools; +import org.forester.util.StringInt; import org.forester.ws.seqdb.UniProtTaxonomy; public class TreePanelUtil { - static int makeSB( final List data, final Options optz, final StringBuilder sb ) { - final SortedMap map = new TreeMap(); - if ( ( optz.getExtDescNodeDataToReturn() != NODE_DATA.SEQUENCE_MOL_SEQ ) - && ( optz.getExtDescNodeDataToReturn() != NODE_DATA.SEQUENCE_MOL_SEQ_FASTA ) ) { - for( final String d : data ) { - if ( !ForesterUtil.isEmpty( d ) ) { - if ( map.containsKey( d ) ) { - map.put( d, map.get( d ) + 1 ); - } - else { - map.put( d, 1 ); - } - } - } - } - int size = 0; - if ( ( optz.getExtDescNodeDataToReturn() != NODE_DATA.SEQUENCE_MOL_SEQ ) - && ( optz.getExtDescNodeDataToReturn() != NODE_DATA.SEQUENCE_MOL_SEQ_FASTA ) ) { - for( final Entry e : map.entrySet() ) { - final String v = e.getKey(); - final Object c = e.getValue(); - sb.append( v ); - sb.append( "\t" ); - sb.append( c ); - sb.append( ForesterUtil.LINE_SEPARATOR ); - } - size = map.size(); - } - else { - for( final String d : data ) { - if ( !ForesterUtil.isEmpty( d ) ) { - sb.append( d ); - sb.append( ForesterUtil.LINE_SEPARATOR ); - } - } - size = data.size(); - } - return size; - } - public final static String createUriForSeqWeb( final PhylogenyNode node, final Configuration conf, final TreePanel tp ) { @@ -83,7 +46,7 @@ public class TreePanelUtil { final String upkb = SequenceAccessionTools.obtainUniProtAccessorFromDataFields( node ); if ( !ForesterUtil.isEmpty( upkb ) ) { try { - uri_str = ForesterUtil.UNIPROT_KB + URLEncoder.encode( upkb, ForesterConstants.UTF8 ); + uri_str = ForesterUtil.UNIPROT_KB + URLEncoder.encode( upkb, ForesterConstants.UTF_8 ); } catch ( final UnsupportedEncodingException e ) { AptxUtil.showErrorMessage( tp, e.toString() ); @@ -95,10 +58,10 @@ public class TreePanelUtil { if ( !ForesterUtil.isEmpty( v ) ) { try { if ( SequenceAccessionTools.isProteinDbQuery( v ) ) { - uri_str = ForesterUtil.NCBI_PROTEIN + URLEncoder.encode( v, ForesterConstants.UTF8 ); + uri_str = ForesterUtil.NCBI_PROTEIN + URLEncoder.encode( v, ForesterConstants.UTF_8 ); } else { - uri_str = ForesterUtil.NCBI_NUCCORE + URLEncoder.encode( v, ForesterConstants.UTF8 ); + uri_str = ForesterUtil.NCBI_NUCCORE + URLEncoder.encode( v, ForesterConstants.UTF_8 ); } } catch ( final UnsupportedEncodingException e ) { @@ -112,10 +75,10 @@ public class TreePanelUtil { if ( !ForesterUtil.isEmpty( v ) ) { try { if ( SequenceAccessionTools.isProteinDbQuery( v ) ) { - uri_str = ForesterUtil.NCBI_PROTEIN + URLEncoder.encode( v, ForesterConstants.UTF8 ); + uri_str = ForesterUtil.NCBI_PROTEIN + URLEncoder.encode( v, ForesterConstants.UTF_8 ); } else { - uri_str = ForesterUtil.NCBI_NUCCORE + URLEncoder.encode( v, ForesterConstants.UTF8 ); + uri_str = ForesterUtil.NCBI_NUCCORE + URLEncoder.encode( v, ForesterConstants.UTF_8 ); } } catch ( final UnsupportedEncodingException e ) { @@ -128,7 +91,7 @@ public class TreePanelUtil { final String v = SequenceAccessionTools.obtainGiNumberFromDataFields( node ); if ( !ForesterUtil.isEmpty( v ) ) { try { - uri_str = ForesterUtil.NCBI_GI + URLEncoder.encode( v, ForesterConstants.UTF8 ); + uri_str = ForesterUtil.NCBI_GI + URLEncoder.encode( v, ForesterConstants.UTF_8 ); } catch ( final UnsupportedEncodingException e ) { AptxUtil.showErrorMessage( tp, e.toString() ); @@ -139,12 +102,27 @@ public class TreePanelUtil { return uri_str; } + public static List createUrisForPdbWeb( final PhylogenyNode node, + final List pdb_accs, + final Configuration configuration, + final TreePanel treePanel ) { + final List uris = new ArrayList(); + if ( !ForesterUtil.isEmpty( pdb_accs ) ) { + for( final Accession pdb_acc : pdb_accs ) { + if ( !ForesterUtil.isEmpty( pdb_acc.getValue() ) ) { + uris.add( ForesterUtil.PDB + pdb_acc.getValue() ); + } + } + } + return uris; + } + /** * Returns the set of distinct taxonomies of * all external nodes of node. * If at least one the external nodes has no taxonomy, * null is returned. - * + * */ public static Set obtainDistinctTaxonomies( final PhylogenyNode node ) { final List descs = node.getAllExternalDescendants(); @@ -171,8 +149,8 @@ public class TreePanelUtil { } if ( cp.isShowSeqSymbols() && node.getNodeData().isHasSequence() && !ForesterUtil.isEmpty( node.getNodeData().getSequence().getSymbol() ) ) { - TreePanelUtil - .showExtDescNodeDataUserSelectedHelperHelper( node.getNodeData().getSequence().getSymbol(), sb ); + TreePanelUtil.showExtDescNodeDataUserSelectedHelperHelper( node.getNodeData().getSequence().getSymbol(), + sb ); } if ( cp.isShowGeneNames() && node.getNodeData().isHasSequence() && !ForesterUtil.isEmpty( node.getNodeData().getSequence().getGeneName() ) ) { @@ -187,25 +165,27 @@ public class TreePanelUtil { } if ( cp.isShowTaxonomyCode() && node.getNodeData().isHasTaxonomy() && !ForesterUtil.isEmpty( node.getNodeData().getTaxonomy().getTaxonomyCode() ) ) { - TreePanelUtil.showExtDescNodeDataUserSelectedHelperHelper( node.getNodeData().getTaxonomy() - .getTaxonomyCode(), sb ); + TreePanelUtil + .showExtDescNodeDataUserSelectedHelperHelper( node.getNodeData().getTaxonomy().getTaxonomyCode(), + sb ); } if ( cp.isShowTaxonomyScientificNames() && node.getNodeData().isHasTaxonomy() && !ForesterUtil.isEmpty( node.getNodeData().getTaxonomy().getScientificName() ) ) { - TreePanelUtil.showExtDescNodeDataUserSelectedHelperHelper( node.getNodeData().getTaxonomy() - .getScientificName(), sb ); + TreePanelUtil + .showExtDescNodeDataUserSelectedHelperHelper( node.getNodeData().getTaxonomy().getScientificName(), + sb ); } if ( cp.isShowTaxonomyCommonNames() && node.getNodeData().isHasTaxonomy() && !ForesterUtil.isEmpty( node.getNodeData().getTaxonomy().getCommonName() ) ) { - TreePanelUtil - .showExtDescNodeDataUserSelectedHelperHelper( node.getNodeData().getTaxonomy().getCommonName(), sb ); - } - if ( ( cp.isShowSeqNames() || cp.isShowSeqSymbols() || cp.isShowSequenceAcc() ) - && node.getNodeData().isHasSequence() - && !ForesterUtil.isEmpty( node.getNodeData().getSequence().getMolecularSequence() ) ) { - TreePanelUtil.showExtDescNodeDataUserSelectedHelperHelper( node.getNodeData().getSequence() - .getMolecularSequence(), sb ); + TreePanelUtil.showExtDescNodeDataUserSelectedHelperHelper( node.getNodeData().getTaxonomy().getCommonName(), + sb ); } + // if ( ( cp.isShowSeqNames() || cp.isShowSeqSymbols() || cp.isShowSequenceAcc() ) + // && node.getNodeData().isHasSequence() + // && !ForesterUtil.isEmpty( node.getNodeData().getSequence().getMolecularSequence() ) ) { + // TreePanelUtil.showExtDescNodeDataUserSelectedHelperHelper( node.getNodeData().getSequence() + // .getMolecularSequence(), sb ); + // } final String s = sb.toString().trim(); if ( !ForesterUtil.isEmpty( s ) ) { data.add( s ); @@ -223,46 +203,11 @@ public class TreePanelUtil { JOptionPane.showMessageDialog( parent, msg, title, JOptionPane.INFORMATION_MESSAGE ); } - 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 ( 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 = my_str.charAt( ( my_str.length() - 1 ) / 2 ); - } - } - first = TreePanelUtil.normalizeCharForRGB( first ); - second = TreePanelUtil.normalizeCharForRGB( second ); - third = TreePanelUtil.normalizeCharForRGB( third ); - if ( ( first > 235 ) && ( second > 235 ) && ( third > 235 ) ) { - first = 0; - } - else if ( ( first < 60 ) && ( second < 60 ) && ( third < 60 ) ) { - second = 255; - } - return new Color( first, second, third ); - } - final static void collapseSpeciesSpecificSubtrees( final Phylogeny phy ) { boolean inferred = false; + for( final PhylogenyNodeIterator iter = phy.iteratorPreorder(); iter.hasNext(); ) { + iter.next().setCollapse( false ); + } for( final PhylogenyNodeIterator it = phy.iteratorPreorder(); it.hasNext(); ) { final PhylogenyNode n = it.next(); if ( !n.isExternal() && !n.isCollapse() && ( n.getNumberOfDescendants() > 1 ) ) { @@ -296,6 +241,17 @@ public class TreePanelUtil { } } + final static void uncollapseSubtree( final PhylogenyNode node ) { + node.setCollapse( false ); + if ( node.isExternal() ) { + return; + } + final PhylogenyNodeIterator it = new PreorderTreeIterator( node ); + while ( it.hasNext() ) { + it.next().setCollapse( false ); + } + } + static void colorizeSubtree( final PhylogenyNode node, final BranchColor c ) { node.getBranchData().setBranchColor( c ); final List descs = PhylogenyMethods.getAllDescendants( node ); @@ -339,7 +295,8 @@ public class TreePanelUtil { if ( !n.getBranchData().isHasBranchColor() ) { final Taxonomy tax = PhylogenyMethods.getExternalDescendantsTaxonomy( n ); if ( tax != null ) { - n.getBranchData().setBranchColor( new BranchColor( tree_panel.calculateTaxonomyBasedColor( tax ) ) ); + n.getBranchData() + .setBranchColor( new BranchColor( tree_panel.calculateTaxonomyBasedColor( tax ) ) ); final List descs = PhylogenyMethods.getAllDescendants( n ); for( final PhylogenyNode desc : descs ) { desc.getBranchData() @@ -350,23 +307,97 @@ public class TreePanelUtil { } } - final static int colorPhylogenyAccordingToRanks( final Phylogeny tree, final String rank, final TreePanel tree_panel ) { +// final static int collapseByTaxonomicRank( final Phylogeny tree, final String rank, final TreePanel tree_panel ) { +// final Set true_lineage_set = new HashSet(); +// for( final PhylogenyNodeIterator iter = tree.iteratorPreorder(); iter.hasNext(); ) { +// iter.next().setCollapse( false ); +// } +// int collapsed = 0; +// for( final PhylogenyNodeIterator it = tree.iteratorPostorder(); it.hasNext(); ) { +// final PhylogenyNode n = it.next(); +// if ( !n.isExternal() && n.getNodeData().isHasTaxonomy() && !ForesterUtil.isEmpty( n.getNodeData().getTaxonomy().getRank() ) +// && n.getNodeData().getTaxonomy().getRank().equalsIgnoreCase( rank ) /*&& !n.isRoot()*/ ) { +// TreePanelUtil.collapseSubtree( n, true ); +// ++collapsed; +// if ( !ForesterUtil.isEmpty( n.getNodeData().getTaxonomy().getScientificName() ) ) { +// true_lineage_set.add( n.getNodeData().getTaxonomy().getScientificName() ); +// } +// } +// } +// for( final PhylogenyNodeIterator it = tree.iteratorPostorder(); it.hasNext(); ) { +// final PhylogenyNode node = it.next(); +// if ( ( !node.isExternal() && !node.isCollapse() ) && node.getNodeData().isHasTaxonomy() +// && !ForesterUtil.isEmpty( node.getNodeData().getTaxonomy().getLineage() ) /* && !node.isRoot()*/ ) { +// boolean success = false; +// if ( !true_lineage_set.isEmpty() ) { +// for( final String lin : node.getNodeData().getTaxonomy().getLineage() ) { +// if ( true_lineage_set.contains( lin ) ) { +// TreePanelUtil.collapseSubtree( node, true ); +// ++collapsed; +// success = true; +// break; +// } +// } +// } +// if ( !success ) { +// final Map lineage_to_rank_map = MainPanel.getLineageToRankMap(); +// for( final String lin : node.getNodeData().getTaxonomy().getLineage() ) { +// final Taxonomy temp_tax = new Taxonomy(); +// temp_tax.setScientificName( lin ); +// if ( lineage_to_rank_map.containsKey( lin ) +// && !ForesterUtil.isEmpty( lineage_to_rank_map.get( lin ) ) +// && lineage_to_rank_map.get( lin ).equalsIgnoreCase( rank ) ) { +// TreePanelUtil.collapseSubtree( node, true ); +// ++collapsed; +// true_lineage_set.add( lin ); +// break; +// } +// else { +// UniProtTaxonomy up = null; +// try { +// up = TaxonomyDataManager.obtainUniProtTaxonomy( temp_tax, null, null ); +// } +// catch ( final Exception e ) { +// e.printStackTrace(); +// } +// if ( ( up != null ) && !ForesterUtil.isEmpty( up.getRank() ) ) { +// lineage_to_rank_map.put( lin, up.getRank() ); +// System.out.println( lin + "->" + up.getRank() ); +// if ( up.getRank().equalsIgnoreCase( rank ) ) { +// TreePanelUtil.collapseSubtree( node, true ); +// ++collapsed; +// true_lineage_set.add( lin ); +// break; +// } +// } +// } +// } +// } +// } +// } +// return collapsed; +// } + + final static int colorPhylogenyAccordingToRanks( final Phylogeny tree, + final String rank, + final TreePanel tree_panel ) { final Map true_lineage_to_color_map = new HashMap(); int colorizations = 0; for( final PhylogenyNodeIterator it = tree.iteratorPostorder(); it.hasNext(); ) { final PhylogenyNode n = it.next(); if ( n.getNodeData().isHasTaxonomy() && ( !ForesterUtil.isEmpty( n.getNodeData().getTaxonomy().getScientificName() ) - || !ForesterUtil.isEmpty( n.getNodeData().getTaxonomy().getCommonName() ) || !ForesterUtil - .isEmpty( n.getNodeData().getTaxonomy().getTaxonomyCode() ) ) ) { + || !ForesterUtil.isEmpty( n.getNodeData().getTaxonomy().getCommonName() ) + || !ForesterUtil.isEmpty( n.getNodeData().getTaxonomy().getTaxonomyCode() ) ) ) { if ( !ForesterUtil.isEmpty( n.getNodeData().getTaxonomy().getRank() ) && n.getNodeData().getTaxonomy().getRank().equalsIgnoreCase( rank ) ) { - final BranchColor c = new BranchColor( tree_panel.calculateTaxonomyBasedColor( n.getNodeData() - .getTaxonomy() ) ); + final BranchColor c = new BranchColor( tree_panel + .calculateTaxonomyBasedColor( n.getNodeData().getTaxonomy() ) ); TreePanelUtil.colorizeSubtree( n, c ); ++colorizations; if ( !ForesterUtil.isEmpty( n.getNodeData().getTaxonomy().getScientificName() ) ) { - true_lineage_to_color_map.put( n.getNodeData().getTaxonomy().getScientificName(), c.getValue() ); + true_lineage_to_color_map.put( n.getNodeData().getTaxonomy().getScientificName(), + c.getValue() ); } } } @@ -379,8 +410,8 @@ public class TreePanelUtil { if ( !true_lineage_to_color_map.isEmpty() ) { for( final String lin : node.getNodeData().getTaxonomy().getLineage() ) { if ( true_lineage_to_color_map.containsKey( lin ) ) { - TreePanelUtil - .colorizeSubtree( node, new BranchColor( true_lineage_to_color_map.get( lin ) ) ); + TreePanelUtil.colorizeSubtree( node, + new BranchColor( true_lineage_to_color_map.get( lin ) ) ); ++colorizations; success = true; break; @@ -411,8 +442,10 @@ public class TreePanelUtil { } if ( ( up != null ) && !ForesterUtil.isEmpty( up.getRank() ) ) { lineage_to_rank_map.put( lin, up.getRank() ); + System.out.println( lin + "->" + up.getRank() ); if ( up.getRank().equalsIgnoreCase( rank ) ) { - final BranchColor c = new BranchColor( tree_panel.calculateTaxonomyBasedColor( temp_tax ) ); + final BranchColor c = new BranchColor( tree_panel + .calculateTaxonomyBasedColor( temp_tax ) ); TreePanelUtil.colorizeSubtree( node, c ); ++colorizations; true_lineage_to_color_map.put( lin, c.getValue() ); @@ -427,7 +460,8 @@ public class TreePanelUtil { return colorizations; } - final static String createAnnotationString( final SortedSet annotations, final boolean show_ref_sources ) { + final static String createAnnotationString( final SortedSet annotations, + final boolean show_ref_sources ) { final SortedMap> m = new TreeMap>(); for( final Annotation an : annotations ) { final String ref_source = ForesterUtil.isEmpty( an.getRefSource() ) ? "?" : an.getRefSource(); @@ -459,7 +493,7 @@ public class TreePanelUtil { if ( sb.charAt( sb.length() - 1 ) == ' ' ) { sb.deleteCharAt( sb.length() - 1 ); } - if ( i < ans.size() - 1 ) { + if ( i < ( ans.size() - 1 ) ) { sb.append( ", " ); } } @@ -495,16 +529,69 @@ public class TreePanelUtil { final static boolean isTaxonomyEmpty( final Taxonomy tax ) { return ( ( tax.getIdentifier() == null ) && ForesterUtil.isEmpty( tax.getTaxonomyCode() ) - && ForesterUtil.isEmpty( tax.getCommonName() ) && ForesterUtil.isEmpty( tax.getScientificName() ) && tax - .getSynonyms().isEmpty() ); + && ForesterUtil.isEmpty( tax.getCommonName() ) && ForesterUtil.isEmpty( tax.getScientificName() ) + && tax.getSynonyms().isEmpty() ); } - final static char normalizeCharForRGB( char c ) { - c -= 65; - c *= 10.2; - c = c > 255 ? 255 : c; - c = c < 0 ? 0 : c; - return c; + static final int nodeDataIntoStringBuffer( final List data, final Options optz, final StringBuilder sb ) { + final SortedMap map = new TreeMap(); + int size = 0; + if ( ( optz.getExtDescNodeDataToReturn() != NodeDataField.SEQUENCE_MOL_SEQ_FASTA ) + && ( optz.getExtDescNodeDataToReturn() != NodeDataField.GO_TERM_IDS ) ) { + for( final String d : data ) { + if ( !ForesterUtil.isEmpty( d ) ) { + if ( map.containsKey( d ) ) { + map.put( d, map.get( d ) + 1 ); + } + else { + map.put( d, 1 ); + } + } + } + if ( ( optz.getExtDescNodeDataToReturn() == NodeDataField.DOMAINS_ALL ) + || ( optz.getExtDescNodeDataToReturn() == NodeDataField.DOMAINS_COLLAPSED_PER_PROTEIN ) + || ( optz.getExtDescNodeDataToReturn() == NodeDataField.SEQ_ANNOTATIONS ) ) { + final ArrayList sis = new ArrayList(); + for( final Entry e : map.entrySet() ) { + sis.add( new StringInt( e.getKey(), e.getValue() ) ); + } + Collections.sort( sis, new StringInt.DescendingIntComparator() ); + for( final StringInt si : sis ) { + sb.append( si.getString() ); + sb.append( "\t" ); + sb.append( si.getInt() ); + sb.append( ForesterUtil.LINE_SEPARATOR ); + } + } + else { + for( final Entry e : map.entrySet() ) { + final String v = e.getKey(); + final Object c = e.getValue(); + sb.append( v ); + sb.append( "\t" ); + sb.append( c ); + sb.append( ForesterUtil.LINE_SEPARATOR ); + } + } + size = map.size(); + } + else { + for( final String d : data ) { + if ( !ForesterUtil.isEmpty( d ) ) { + sb.append( d ); + sb.append( ForesterUtil.LINE_SEPARATOR ); + } + } + size = data.size(); + } + return size; + } + + final static String pdbAccToString( final List accs, final int i ) { + if ( ForesterUtil.isEmpty( accs.get( i ).getComment() ) ) { + return accs.get( i ).getValue(); + } + return accs.get( i ).getValue() + " (" + accs.get( i ).getComment().toLowerCase() + ")"; } final static Phylogeny subTree( final PhylogenyNode new_root, final Phylogeny source_phy ) {