2 package org.forester.archaeopteryx;
5 import java.awt.Component;
6 import java.io.UnsupportedEncodingException;
7 import java.net.URLEncoder;
8 import java.util.ArrayList;
9 import java.util.Collections;
10 import java.util.HashMap;
11 import java.util.HashSet;
12 import java.util.List;
14 import java.util.Map.Entry;
16 import java.util.SortedMap;
17 import java.util.SortedSet;
18 import java.util.TreeMap;
20 import javax.swing.JOptionPane;
22 import org.forester.analysis.TaxonomyDataManager;
23 import org.forester.phylogeny.Phylogeny;
24 import org.forester.phylogeny.PhylogenyMethods;
25 import org.forester.phylogeny.PhylogenyNode;
26 import org.forester.phylogeny.data.Accession;
27 import org.forester.phylogeny.data.Annotation;
28 import org.forester.phylogeny.data.BranchColor;
29 import org.forester.phylogeny.data.NodeDataField;
30 import org.forester.phylogeny.data.Sequence;
31 import org.forester.phylogeny.data.Taxonomy;
32 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
33 import org.forester.phylogeny.iterators.PreorderTreeIterator;
34 import org.forester.util.ForesterConstants;
35 import org.forester.util.ForesterUtil;
36 import org.forester.util.SequenceAccessionTools;
37 import org.forester.util.StringInt;
38 import org.forester.ws.seqdb.UniProtTaxonomy;
40 public class TreePanelUtil {
42 public final static String createUriForSeqWeb( final PhylogenyNode node,
43 final Configuration conf,
44 final TreePanel tp ) {
45 String uri_str = null;
46 final String upkb = SequenceAccessionTools.obtainUniProtAccessorFromDataFields( node );
47 if ( !ForesterUtil.isEmpty( upkb ) ) {
49 uri_str = ForesterUtil.UNIPROT_KB + URLEncoder.encode( upkb, ForesterConstants.UTF8 );
51 catch ( final UnsupportedEncodingException e ) {
52 AptxUtil.showErrorMessage( tp, e.toString() );
56 if ( ForesterUtil.isEmpty( uri_str ) ) {
57 final String v = SequenceAccessionTools.obtainGenbankAccessorFromDataFields( node );
58 if ( !ForesterUtil.isEmpty( v ) ) {
60 if ( SequenceAccessionTools.isProteinDbQuery( v ) ) {
61 uri_str = ForesterUtil.NCBI_PROTEIN + URLEncoder.encode( v, ForesterConstants.UTF8 );
64 uri_str = ForesterUtil.NCBI_NUCCORE + URLEncoder.encode( v, ForesterConstants.UTF8 );
67 catch ( final UnsupportedEncodingException e ) {
68 AptxUtil.showErrorMessage( tp, e.toString() );
73 if ( ForesterUtil.isEmpty( uri_str ) ) {
74 final String v = SequenceAccessionTools.obtainRefSeqAccessorFromDataFields( node );
75 if ( !ForesterUtil.isEmpty( v ) ) {
77 if ( SequenceAccessionTools.isProteinDbQuery( v ) ) {
78 uri_str = ForesterUtil.NCBI_PROTEIN + URLEncoder.encode( v, ForesterConstants.UTF8 );
81 uri_str = ForesterUtil.NCBI_NUCCORE + URLEncoder.encode( v, ForesterConstants.UTF8 );
84 catch ( final UnsupportedEncodingException e ) {
85 AptxUtil.showErrorMessage( tp, e.toString() );
90 if ( ForesterUtil.isEmpty( uri_str ) ) {
91 final String v = SequenceAccessionTools.obtainGiNumberFromDataFields( node );
92 if ( !ForesterUtil.isEmpty( v ) ) {
94 uri_str = ForesterUtil.NCBI_GI + URLEncoder.encode( v, ForesterConstants.UTF8 );
96 catch ( final UnsupportedEncodingException e ) {
97 AptxUtil.showErrorMessage( tp, e.toString() );
105 public static List<String> createUrisForPdbWeb( final PhylogenyNode node,
106 final List<Accession> pdb_accs,
107 final Configuration configuration,
108 final TreePanel treePanel ) {
109 final List<String> uris = new ArrayList<String>();
110 if ( !ForesterUtil.isEmpty( pdb_accs ) ) {
111 for( final Accession pdb_acc : pdb_accs ) {
112 if ( !ForesterUtil.isEmpty( pdb_acc.getValue() ) ) {
113 uris.add( ForesterUtil.PDB + pdb_acc.getValue() );
121 * Returns the set of distinct taxonomies of
122 * all external nodes of node.
123 * If at least one the external nodes has no taxonomy,
127 public static Set<Taxonomy> obtainDistinctTaxonomies( final PhylogenyNode node ) {
128 final List<PhylogenyNode> descs = node.getAllExternalDescendants();
129 final Set<Taxonomy> tax_set = new HashSet<Taxonomy>();
130 for( final PhylogenyNode n : descs ) {
131 if ( !n.getNodeData().isHasTaxonomy() || n.getNodeData().getTaxonomy().isEmpty() ) {
134 tax_set.add( n.getNodeData().getTaxonomy() );
139 public final static void showExtDescNodeDataUserSelectedHelper( final ControlPanel cp,
140 final PhylogenyNode node,
141 final List<String> data ) {
142 final StringBuilder sb = new StringBuilder();
143 if ( cp.isShowNodeNames() && !ForesterUtil.isEmpty( node.getName() ) ) {
144 TreePanelUtil.showExtDescNodeDataUserSelectedHelperHelper( node.getName(), sb );
146 if ( cp.isShowSeqNames() && node.getNodeData().isHasSequence()
147 && !ForesterUtil.isEmpty( node.getNodeData().getSequence().getName() ) ) {
148 TreePanelUtil.showExtDescNodeDataUserSelectedHelperHelper( node.getNodeData().getSequence().getName(), sb );
150 if ( cp.isShowSeqSymbols() && node.getNodeData().isHasSequence()
151 && !ForesterUtil.isEmpty( node.getNodeData().getSequence().getSymbol() ) ) {
153 .showExtDescNodeDataUserSelectedHelperHelper( node.getNodeData().getSequence().getSymbol(), sb );
155 if ( cp.isShowGeneNames() && node.getNodeData().isHasSequence()
156 && !ForesterUtil.isEmpty( node.getNodeData().getSequence().getGeneName() ) ) {
157 TreePanelUtil.showExtDescNodeDataUserSelectedHelperHelper( node.getNodeData().getSequence().getGeneName(),
160 if ( cp.isShowSequenceAcc() && node.getNodeData().isHasSequence()
161 && ( node.getNodeData().getSequence().getAccession() != null )
162 && !ForesterUtil.isEmpty( node.getNodeData().getSequence().getAccession().toString() ) ) {
163 TreePanelUtil.showExtDescNodeDataUserSelectedHelperHelper( node.getNodeData().getSequence().getAccession()
166 if ( cp.isShowTaxonomyCode() && node.getNodeData().isHasTaxonomy()
167 && !ForesterUtil.isEmpty( node.getNodeData().getTaxonomy().getTaxonomyCode() ) ) {
168 TreePanelUtil.showExtDescNodeDataUserSelectedHelperHelper( node.getNodeData().getTaxonomy()
169 .getTaxonomyCode(), sb );
171 if ( cp.isShowTaxonomyScientificNames() && node.getNodeData().isHasTaxonomy()
172 && !ForesterUtil.isEmpty( node.getNodeData().getTaxonomy().getScientificName() ) ) {
173 TreePanelUtil.showExtDescNodeDataUserSelectedHelperHelper( node.getNodeData().getTaxonomy()
174 .getScientificName(), sb );
176 if ( cp.isShowTaxonomyCommonNames() && node.getNodeData().isHasTaxonomy()
177 && !ForesterUtil.isEmpty( node.getNodeData().getTaxonomy().getCommonName() ) ) {
179 .showExtDescNodeDataUserSelectedHelperHelper( node.getNodeData().getTaxonomy().getCommonName(), sb );
181 // if ( ( cp.isShowSeqNames() || cp.isShowSeqSymbols() || cp.isShowSequenceAcc() )
182 // && node.getNodeData().isHasSequence()
183 // && !ForesterUtil.isEmpty( node.getNodeData().getSequence().getMolecularSequence() ) ) {
184 // TreePanelUtil.showExtDescNodeDataUserSelectedHelperHelper( node.getNodeData().getSequence()
185 // .getMolecularSequence(), sb );
187 final String s = sb.toString().trim();
188 if ( !ForesterUtil.isEmpty( s ) ) {
193 public final static void showExtDescNodeDataUserSelectedHelperHelper( final String s, final StringBuilder sb ) {
194 if ( sb.length() > 0 ) {
200 final public static void showInformationMessage( final Component parent, final String title, final String msg ) {
201 JOptionPane.showMessageDialog( parent, msg, title, JOptionPane.INFORMATION_MESSAGE );
204 final static void collapseSpeciesSpecificSubtrees( final Phylogeny phy ) {
205 boolean inferred = false;
206 for( final PhylogenyNodeIterator it = phy.iteratorPreorder(); it.hasNext(); ) {
207 final PhylogenyNode n = it.next();
208 if ( !n.isExternal() && !n.isCollapse() && ( n.getNumberOfDescendants() > 1 ) ) {
209 final Set<Taxonomy> taxs = TreePanelUtil.obtainDistinctTaxonomies( n );
210 if ( ( taxs != null ) && ( taxs.size() == 1 ) ) {
211 TreePanelUtil.collapseSubtree( n, true );
212 if ( !n.getNodeData().isHasTaxonomy() ) {
213 n.getNodeData().setTaxonomy( ( Taxonomy ) n.getAllExternalDescendants().get( 0 ).getNodeData()
214 .getTaxonomy().copy() );
219 n.setCollapse( false );
224 phy.setRerootable( false );
228 final static void collapseSubtree( final PhylogenyNode node, final boolean collapse ) {
229 node.setCollapse( collapse );
230 if ( node.isExternal() ) {
233 final PhylogenyNodeIterator it = new PreorderTreeIterator( node );
234 while ( it.hasNext() ) {
235 it.next().setCollapse( collapse );
239 static void colorizeSubtree( final PhylogenyNode node, final BranchColor c ) {
240 node.getBranchData().setBranchColor( c );
241 final List<PhylogenyNode> descs = PhylogenyMethods.getAllDescendants( node );
242 for( final PhylogenyNode desc : descs ) {
243 desc.getBranchData().setBranchColor( c );
247 final static void colorPhylogenyAccordingToConfidenceValues( final Phylogeny tree, final TreePanel tree_panel ) {
248 double max_conf = 0.0;
249 for( final PhylogenyNodeIterator it = tree.iteratorPreorder(); it.hasNext(); ) {
250 final PhylogenyNode n = it.next();
251 n.getBranchData().setBranchColor( null );
252 if ( n.getBranchData().isHasConfidences() ) {
253 final double conf = PhylogenyMethods.getConfidenceValue( n );
254 if ( conf > max_conf ) {
259 if ( max_conf > 0.0 ) {
260 final Color bg = tree_panel.getTreeColorSet().getBackgroundColor();
261 final Color br = tree_panel.getTreeColorSet().getBranchColor();
262 for( final PhylogenyNodeIterator it = tree.iteratorPreorder(); it.hasNext(); ) {
263 final PhylogenyNode n = it.next();
264 if ( n.getBranchData().isHasConfidences() ) {
265 final double conf = PhylogenyMethods.getConfidenceValue( n );
266 final BranchColor c = new BranchColor( ForesterUtil.calcColor( conf, 0.0, max_conf, bg, br ) );
267 TreePanelUtil.colorizeSubtree( n, c );
273 final static void colorPhylogenyAccordingToExternalTaxonomy( final Phylogeny tree, final TreePanel tree_panel ) {
274 for( final PhylogenyNodeIterator it = tree.iteratorPreorder(); it.hasNext(); ) {
275 it.next().getBranchData().setBranchColor( null );
277 for( final PhylogenyNodeIterator it = tree.iteratorPreorder(); it.hasNext(); ) {
278 final PhylogenyNode n = it.next();
279 if ( !n.getBranchData().isHasBranchColor() ) {
280 final Taxonomy tax = PhylogenyMethods.getExternalDescendantsTaxonomy( n );
282 n.getBranchData().setBranchColor( new BranchColor( tree_panel.calculateTaxonomyBasedColor( tax ) ) );
283 final List<PhylogenyNode> descs = PhylogenyMethods.getAllDescendants( n );
284 for( final PhylogenyNode desc : descs ) {
286 .setBranchColor( new BranchColor( tree_panel.calculateTaxonomyBasedColor( tax ) ) );
293 final static int colorPhylogenyAccordingToRanks( final Phylogeny tree, final String rank, final TreePanel tree_panel ) {
294 final Map<String, Color> true_lineage_to_color_map = new HashMap<String, Color>();
295 int colorizations = 0;
296 for( final PhylogenyNodeIterator it = tree.iteratorPostorder(); it.hasNext(); ) {
297 final PhylogenyNode n = it.next();
298 if ( n.getNodeData().isHasTaxonomy()
299 && ( !ForesterUtil.isEmpty( n.getNodeData().getTaxonomy().getScientificName() )
300 || !ForesterUtil.isEmpty( n.getNodeData().getTaxonomy().getCommonName() ) || !ForesterUtil
301 .isEmpty( n.getNodeData().getTaxonomy().getTaxonomyCode() ) ) ) {
302 if ( !ForesterUtil.isEmpty( n.getNodeData().getTaxonomy().getRank() )
303 && n.getNodeData().getTaxonomy().getRank().equalsIgnoreCase( rank ) ) {
304 final BranchColor c = new BranchColor( tree_panel.calculateTaxonomyBasedColor( n.getNodeData()
306 TreePanelUtil.colorizeSubtree( n, c );
308 if ( !ForesterUtil.isEmpty( n.getNodeData().getTaxonomy().getScientificName() ) ) {
309 true_lineage_to_color_map.put( n.getNodeData().getTaxonomy().getScientificName(), c.getValue() );
314 for( final PhylogenyNodeIterator it = tree.iteratorPostorder(); it.hasNext(); ) {
315 final PhylogenyNode node = it.next();
316 if ( ( node.getBranchData().getBranchColor() == null ) && node.getNodeData().isHasTaxonomy()
317 && !ForesterUtil.isEmpty( node.getNodeData().getTaxonomy().getLineage() ) ) {
318 boolean success = false;
319 if ( !true_lineage_to_color_map.isEmpty() ) {
320 for( final String lin : node.getNodeData().getTaxonomy().getLineage() ) {
321 if ( true_lineage_to_color_map.containsKey( lin ) ) {
323 .colorizeSubtree( node, new BranchColor( true_lineage_to_color_map.get( lin ) ) );
331 final Map<String, String> lineage_to_rank_map = MainPanel.getLineageToRankMap();
332 for( final String lin : node.getNodeData().getTaxonomy().getLineage() ) {
333 final Taxonomy temp_tax = new Taxonomy();
334 temp_tax.setScientificName( lin );
335 if ( lineage_to_rank_map.containsKey( lin )
336 && !ForesterUtil.isEmpty( lineage_to_rank_map.get( lin ) )
337 && lineage_to_rank_map.get( lin ).equalsIgnoreCase( rank ) ) {
338 final BranchColor c = new BranchColor( tree_panel.calculateTaxonomyBasedColor( temp_tax ) );
339 TreePanelUtil.colorizeSubtree( node, c );
341 true_lineage_to_color_map.put( lin, c.getValue() );
345 UniProtTaxonomy up = null;
347 up = TaxonomyDataManager.obtainUniProtTaxonomy( temp_tax, null, null );
349 catch ( final Exception e ) {
352 if ( ( up != null ) && !ForesterUtil.isEmpty( up.getRank() ) ) {
353 lineage_to_rank_map.put( lin, up.getRank() );
354 if ( up.getRank().equalsIgnoreCase( rank ) ) {
355 final BranchColor c = new BranchColor( tree_panel.calculateTaxonomyBasedColor( temp_tax ) );
356 TreePanelUtil.colorizeSubtree( node, c );
358 true_lineage_to_color_map.put( lin, c.getValue() );
367 return colorizations;
370 final static String createAnnotationString( final SortedSet<Annotation> annotations, final boolean show_ref_sources ) {
371 final SortedMap<String, List<Annotation>> m = new TreeMap<String, List<Annotation>>();
372 for( final Annotation an : annotations ) {
373 final String ref_source = ForesterUtil.isEmpty( an.getRefSource() ) ? "?" : an.getRefSource();
374 if ( !m.containsKey( ref_source ) ) {
375 m.put( ref_source, new ArrayList<Annotation>() );
377 m.get( ref_source ).add( an );
379 final StringBuilder sb = new StringBuilder();
380 for( final Entry<String, List<Annotation>> e : m.entrySet() ) {
381 final String ref_source = e.getKey();
382 final List<Annotation> ans = e.getValue();
383 if ( m.size() > 1 ) {
386 if ( show_ref_sources && !ref_source.equals( "?" ) ) {
387 sb.append( ref_source );
390 for( int i = 0; i < ans.size(); ++i ) {
391 final Annotation an = ans.get( i );
392 if ( !ForesterUtil.isEmpty( an.getRefValue() ) ) {
393 sb.append( an.getRefValue() );
396 if ( !ForesterUtil.isEmpty( an.getDesc() ) ) {
397 sb.append( an.getDesc() );
399 if ( sb.charAt( sb.length() - 1 ) == ' ' ) {
400 sb.deleteCharAt( sb.length() - 1 );
402 if ( i < ( ans.size() - 1 ) ) {
406 if ( m.size() > 1 ) {
410 return sb.toString();
413 final static String getPartAfterColon( final String s ) {
414 final int i = s.indexOf( ':' );
415 if ( ( i < 1 ) || ( i == ( s.length() - 1 ) ) ) {
418 return s.substring( i + 1, s.length() );
421 final static boolean isHasAssignedEvent( final PhylogenyNode node ) {
422 if ( !node.getNodeData().isHasEvent() ) {
425 if ( ( node.getNodeData().getEvent() ).isUnassigned() ) {
431 final static boolean isSequenceEmpty( final Sequence seq ) {
432 return ( seq.getAccession() == null ) && ForesterUtil.isEmpty( seq.getName() )
433 && ForesterUtil.isEmpty( seq.getGeneName() ) && ForesterUtil.isEmpty( seq.getSymbol() );
436 final static boolean isTaxonomyEmpty( final Taxonomy tax ) {
437 return ( ( tax.getIdentifier() == null ) && ForesterUtil.isEmpty( tax.getTaxonomyCode() )
438 && ForesterUtil.isEmpty( tax.getCommonName() ) && ForesterUtil.isEmpty( tax.getScientificName() ) && tax
439 .getSynonyms().isEmpty() );
442 static final int nodeDataIntoStringBuffer( final List<String> data, final Options optz, final StringBuilder sb ) {
443 final SortedMap<String, Integer> map = new TreeMap<String, Integer>();
445 if ( ( optz.getExtDescNodeDataToReturn() != NodeDataField.SEQUENCE_MOL_SEQ_FASTA )
446 && ( optz.getExtDescNodeDataToReturn() != NodeDataField.GO_TERM_IDS ) ) {
447 for( final String d : data ) {
448 if ( !ForesterUtil.isEmpty( d ) ) {
449 if ( map.containsKey( d ) ) {
450 map.put( d, map.get( d ) + 1 );
457 if ( ( optz.getExtDescNodeDataToReturn() == NodeDataField.DOMAINS_ALL )
458 || ( optz.getExtDescNodeDataToReturn() == NodeDataField.DOMAINS_COLLAPSED_PER_PROTEIN )
459 || ( optz.getExtDescNodeDataToReturn() == NodeDataField.SEQ_ANNOTATIONS ) ) {
460 final ArrayList<StringInt> sis = new ArrayList<StringInt>();
461 for( final Entry<String, Integer> e : map.entrySet() ) {
462 sis.add( new StringInt( e.getKey(), e.getValue() ) );
464 Collections.sort( sis, new StringInt.DescendingIntComparator() );
465 for( final StringInt si : sis ) {
466 sb.append( si.getString() );
468 sb.append( si.getInt() );
469 sb.append( ForesterUtil.LINE_SEPARATOR );
473 for( final Entry<String, Integer> e : map.entrySet() ) {
474 final String v = e.getKey();
475 final Object c = e.getValue();
479 sb.append( ForesterUtil.LINE_SEPARATOR );
485 for( final String d : data ) {
486 if ( !ForesterUtil.isEmpty( d ) ) {
488 sb.append( ForesterUtil.LINE_SEPARATOR );
496 final static String pdbAccToString( final List<Accession> accs, final int i ) {
497 if ( ForesterUtil.isEmpty( accs.get( i ).getComment() ) ) {
498 return accs.get( i ).getValue();
500 return accs.get( i ).getValue() + " (" + accs.get( i ).getComment().toLowerCase() + ")";
503 final static Phylogeny subTree( final PhylogenyNode new_root, final Phylogeny source_phy ) {
504 final Phylogeny new_phy = new Phylogeny();
505 new_phy.setRooted( true );
506 new_phy.setName( source_phy.getName() );
507 new_phy.setDescription( source_phy.getDescription() );
508 new_phy.setType( source_phy.getType() );
509 new_phy.setDistanceUnit( source_phy.getDistanceUnit() );
510 new_phy.setConfidence( source_phy.getConfidence() );
511 new_phy.setIdentifier( source_phy.getIdentifier() );
512 new_phy.setRoot( new_root.copyNodeDataShallow() );
514 for( final PhylogenyNode n : new_root.getDescendants() ) {
515 new_phy.getRoot().setChildNode( i++, n );