2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
5 // Copyright (C) 2008-2009 Christian M. Zmasek
6 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 // Contact: phylosoft @ gmail . com
24 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
26 package org.forester.archaeopteryx;
28 import java.awt.Color;
29 import java.util.List;
30 import java.util.SortedMap;
31 import java.util.SortedSet;
33 import javax.swing.JEditorPane;
34 import javax.swing.JPanel;
35 import javax.swing.JScrollPane;
36 import javax.swing.JSplitPane;
37 import javax.swing.JTree;
38 import javax.swing.event.TreeSelectionEvent;
39 import javax.swing.event.TreeSelectionListener;
40 import javax.swing.text.Position;
41 import javax.swing.tree.DefaultMutableTreeNode;
42 import javax.swing.tree.TreePath;
44 import org.forester.phylogeny.PhylogenyMethods;
45 import org.forester.phylogeny.PhylogenyNode;
46 import org.forester.phylogeny.data.Annotation;
47 import org.forester.phylogeny.data.BinaryCharacters;
48 import org.forester.phylogeny.data.BranchWidth;
49 import org.forester.phylogeny.data.Date;
50 import org.forester.phylogeny.data.Distribution;
51 import org.forester.phylogeny.data.Event;
52 import org.forester.phylogeny.data.PhylogenyData;
53 import org.forester.phylogeny.data.PhylogenyDataUtil;
54 import org.forester.phylogeny.data.Point;
55 import org.forester.phylogeny.data.PropertiesMap;
56 import org.forester.phylogeny.data.Property;
57 import org.forester.phylogeny.data.Reference;
58 import org.forester.phylogeny.data.Sequence;
59 import org.forester.phylogeny.data.Taxonomy;
60 import org.forester.phylogeny.data.Uri;
61 import org.forester.util.ForesterUtil;
63 class NodePanel extends JPanel implements TreeSelectionListener {
65 static final String DIST_ALTITUDE = "Altitude";
66 static final String DIST_ALT_UNIT = "Altitude unit";
67 static final String DIST_LONGITUDE = "Longitude";
68 static final String DIST_LATITUDE = "Latitude";
69 static final String DIST_GEODETIC_DATUM = "Geodetic datum";
70 static final String DIST_DESCRIPTION = "Description";
71 static final String DATE_UNIT = "Unit";
72 static final String DATE_MAX = "Max";
73 static final String DATE_MIN = "Min";
74 static final String DATE_VALUE = "Value";
75 static final String DATE_DESCRIPTION = "Description";
76 static final String TAXONOMY_IDENTIFIER = "Identifier";
77 static final String SEQ_ACCESSION = "Accession";
78 static final String CONFIDENCE = "Confidence";
79 static final String PROP = "Properties";
80 static final String BINARY_CHARACTERS = "Binary characters";
81 static final String REFERENCE = "Reference";
82 static final String LIT_REFERENCE = "Reference";
83 static final String LIT_REFERENCE_DESC = "Description";
84 static final String LIT_REFERENCE_DOI = "DOI";
85 static final String DISTRIBUTION = "Distribution";
86 static final String DATE = "Date";
87 static final String EVENTS = "Events";
88 static final String SEQUENCE = "Sequence";
89 static final String TAXONOMY = "Taxonomy";
90 static final String BASIC = "Basic";
91 static final String TAXONOMY_SCIENTIFIC_NAME = "Scientific name";
92 static final String SEQ_MOL_SEQ = "Mol seq";
93 static final String SEQ_TYPE = "Type";
94 static final String SEQ_LOCATION = "Location";
95 static final String SEQ_SYMBOL = "Symbol";
96 static final String SEQ_URI = "URI";
97 static final String NODE_BRANCH_LENGTH = "Branch length";
98 static final String NODE_BRANCH_WIDTH = "Branch width";
99 static final String NODE_BRANCH_COLOR = "Branch color";
100 static final String NODE_NAME = "Name";
101 static final String TAXONOMY_URI = "URI";
102 static final String TAXONOMY_RANK = "Rank";
103 static final String TAXONOMY_SYNONYM = "Synonym";
104 static final String TAXONOMY_COMMON_NAME = "Common name";
105 static final String TAXONOMY_AUTHORITY = "Authority";
106 static final String TAXONOMY_CODE = "Code";
107 static final String SEQ_NAME = "Name";
108 static final String EVENTS_GENE_LOSSES = "Gene losses";
109 static final String EVENTS_SPECIATIONS = "Speciations";
110 static final String EVENTS_DUPLICATIONS = "Duplications";
111 private static final long serialVersionUID = 5120159904388100771L;
112 static final String CONFIDENCE_TYPE = "type";
113 private final JTree _tree;
114 private final JEditorPane _pane;
116 public NodePanel( final PhylogenyNode phylogeny_node ) {
117 String node_name = "";
118 if ( !ForesterUtil.isEmpty( phylogeny_node.getName() ) ) {
119 node_name = phylogeny_node.getName() + " ";
121 final DefaultMutableTreeNode top = new DefaultMutableTreeNode( "Node " + node_name );
122 createNodes( top, phylogeny_node );
123 _tree = new JTree( top );
124 _tree.setEditable( false );
125 getJTree().setToggleClickCount( 1 );
127 expandPath( TAXONOMY );
128 expandPath( SEQUENCE );
129 expandPath( EVENTS );
130 final JScrollPane tree_view = new JScrollPane( getJTree() );
131 _pane = new JEditorPane();
132 _pane.setEditable( false );
133 final JScrollPane data_view = new JScrollPane( _pane );
134 final JSplitPane split_pane = new JSplitPane( JSplitPane.VERTICAL_SPLIT );
135 split_pane.setTopComponent( tree_view );
136 split_pane.setBottomComponent( data_view );
137 data_view.setMinimumSize( Constants.NODE_PANEL_SPLIT_MINIMUM_SIZE );
138 tree_view.setMinimumSize( Constants.NODE_PANEL_SPLIT_MINIMUM_SIZE );
139 split_pane.setDividerLocation( 400 );
140 split_pane.setPreferredSize( Constants.NODE_PANEL_SIZE );
144 private void expandPath( final String name ) {
145 final TreePath tp = getJTree().getNextMatch( name, 0, Position.Bias.Forward );
147 getJTree().expandPath( tp );
151 private JTree getJTree() {
156 public void valueChanged( final TreeSelectionEvent e ) {
160 private static void addAnnotation( final DefaultMutableTreeNode top, final Annotation ann, final String name ) {
161 DefaultMutableTreeNode category;
162 category = new DefaultMutableTreeNode( name );
164 addSubelement( category, REFERENCE, ann.getRef() );
165 addSubelement( category, "Description", ann.getDesc() );
166 addSubelement( category, "Source", ann.getSource() );
167 addSubelement( category, "Type", ann.getType() );
168 addSubelement( category, "Evidence", ann.getEvidence() );
169 if ( ann.getConfidence() != null ) {
170 addSubelement( category, CONFIDENCE, ann.getConfidence().asText().toString() );
172 if ( ann.getProperties() != null ) {
173 addProperties( category, ann.getProperties(), PROP );
177 private static void addUri( final DefaultMutableTreeNode top, final Uri uri, final String name ) {
178 DefaultMutableTreeNode category;
179 category = new DefaultMutableTreeNode( name );
181 addSubelement( category, "Description", uri.getDescription() );
182 addSubelement( category, "Type", uri.getType() );
183 addSubelement( category, "URI", uri.getValue().toString() );
186 private static void addAnnotations( final DefaultMutableTreeNode top,
187 final SortedSet<Annotation> annotations,
188 final DefaultMutableTreeNode category ) {
189 if ( ( annotations != null ) && ( annotations.size() > 0 ) ) {
190 category.add( new DefaultMutableTreeNode( "Annotations" ) );
191 final DefaultMutableTreeNode last = top.getLastLeaf();
193 for( final PhylogenyData ann : annotations ) {
194 addAnnotation( last, ( Annotation ) ann, "Annotation " + ( i++ ) );
199 private static void addUris( final DefaultMutableTreeNode top,
200 final List<Uri> uris,
201 final DefaultMutableTreeNode category ) {
202 if ( ( uris != null ) && ( uris.size() > 0 ) ) {
203 category.add( new DefaultMutableTreeNode( "URIs" ) );
204 final DefaultMutableTreeNode last = top.getLastLeaf();
206 for( final Uri uri : uris ) {
208 addUri( last, uri, "URI " + ( i++ ) );
214 private static void addLineage( final DefaultMutableTreeNode top,
215 final List<String> lineage,
216 final DefaultMutableTreeNode category ) {
217 if ( ( lineage != null ) && ( lineage.size() > 0 ) ) {
218 final StringBuilder sb = new StringBuilder();
219 for( final String lin : lineage ) {
220 if ( !ForesterUtil.isEmpty( lin ) ) {
226 if ( sb.length() > 1 ) {
227 str = sb.substring( 0, sb.length() - 3 );
229 if ( !ForesterUtil.isEmpty( str ) ) {
230 addSubelement( category, "Lineage", str );
235 private static void addBasics( final DefaultMutableTreeNode top,
236 final PhylogenyNode phylogeny_node,
237 final String name ) {
238 final DefaultMutableTreeNode category = new DefaultMutableTreeNode( name );
240 addSubelement( category, NODE_NAME, phylogeny_node.getName() );
241 if ( phylogeny_node.getDistanceToParent() != PhylogenyDataUtil.BRANCH_LENGTH_DEFAULT ) {
242 addSubelement( category,
244 ForesterUtil.FORMATTER_6.format( phylogeny_node.getDistanceToParent() ) );
246 if ( phylogeny_node.getBranchData().isHasConfidences() ) {
247 for( final PhylogenyData conf : phylogeny_node.getBranchData().getConfidences() ) {
248 addSubelement( category, CONFIDENCE, conf.asText().toString() );
251 if ( !phylogeny_node.isExternal() ) {
252 addSubelement( category, "Children", String.valueOf( phylogeny_node.getNumberOfDescendants() ) );
253 addSubelement( category,
255 String.valueOf( phylogeny_node.getAllExternalDescendants().size() ) );
256 final SortedMap<Taxonomy, Integer> distinct_tax = PhylogenyMethods
257 .obtainDistinctTaxonomyCounts( phylogeny_node );
258 if ( distinct_tax != null ) {
259 final int no_tax = PhylogenyMethods.calculateNumberOfExternalNodesWithoutTaxonomy( phylogeny_node );
260 final int tax_count = distinct_tax.size();
261 addSubelement( category, "Distinct external taxonomies", String.valueOf( tax_count ) );
263 addSubelement( category, "External nodes without taxonomy", String.valueOf( no_tax ) );
267 if ( !phylogeny_node.isRoot() ) {
268 addSubelement( category, "Depth", String.valueOf( phylogeny_node.calculateDepth() ) );
269 final double d = phylogeny_node.calculateDistanceToRoot();
271 addSubelement( category, "Distance to root", String.valueOf( ForesterUtil.FORMATTER_6.format( d ) ) );
274 if ( ( phylogeny_node.getBranchData().getBranchWidth() != null )
275 && ( phylogeny_node.getBranchData().getBranchWidth().getValue() != BranchWidth.BRANCH_WIDTH_DEFAULT_VALUE ) ) {
276 addSubelement( category,
278 ForesterUtil.FORMATTER_3.format( phylogeny_node.getBranchData().getBranchWidth().getValue() ) );
280 if ( ( phylogeny_node.getBranchData().getBranchColor() != null ) ) {
281 final Color c = phylogeny_node.getBranchData().getBranchColor().getValue();
282 addSubelement( category, NODE_BRANCH_COLOR, c.getRed() + ", " + c.getGreen() + ", " + c.getBlue() );
286 private static void addBinaryCharacters( final DefaultMutableTreeNode top,
287 final BinaryCharacters bc,
288 final String name ) {
289 DefaultMutableTreeNode category;
290 category = new DefaultMutableTreeNode( name );
292 addSubelement( category, "Gained", String.valueOf( bc.getGainedCount() ) );
293 addSubelement( category, "Lost", String.valueOf( bc.getLostCount() ) );
294 addSubelement( category, "Present", String.valueOf( bc.getPresentCount() ) );
295 final DefaultMutableTreeNode chars = new DefaultMutableTreeNode( "Lists" );
296 category.add( chars );
297 addSubelement( chars, "Gained", bc.getGainedCharactersAsStringBuffer().toString() );
298 addSubelement( chars, "Lost", bc.getLostCharactersAsStringBuffer().toString() );
299 addSubelement( chars, "Present", bc.getPresentCharactersAsStringBuffer().toString() );
302 private static void addDate( final DefaultMutableTreeNode top, final Date date, final String name ) {
303 DefaultMutableTreeNode category;
304 category = new DefaultMutableTreeNode( name );
306 addSubelement( category, DATE_DESCRIPTION, date.getDesc() );
307 addSubelement( category, DATE_VALUE, String.valueOf( date.getValue() ) );
308 addSubelement( category, DATE_MIN, String.valueOf( date.getMin() ) );
309 addSubelement( category, DATE_MAX, String.valueOf( date.getMax() ) );
310 addSubelement( category, DATE_UNIT, date.getUnit() );
313 private static void addDistribution( final DefaultMutableTreeNode top, final Distribution dist, final String name ) {
314 DefaultMutableTreeNode category;
315 category = new DefaultMutableTreeNode( name );
317 addSubelement( category, DIST_DESCRIPTION, dist.getDesc() );
318 if ( ( dist.getPoints() != null ) && ( dist.getPoints().size() > 0 ) ) {
319 final Point p0 = dist.getPoints().get( 0 );
320 if ( ( p0 != null ) && !Point.isSeemsEmpty( p0 ) ) {
321 addSubelement( category, DIST_GEODETIC_DATUM, p0.getGeodeticDatum() );
322 addSubelement( category, DIST_LATITUDE, String.valueOf( p0.getLatitude() ) );
323 addSubelement( category, DIST_LONGITUDE, String.valueOf( p0.getLongitude() ) );
324 String alt_unit = p0.getAltiudeUnit();
325 if ( ForesterUtil.isEmpty( alt_unit ) ) {
328 addSubelement( category, DIST_ALTITUDE, String.valueOf( p0.getAltitude() ) + alt_unit );
333 private static void addEvents( final DefaultMutableTreeNode top, final Event events, final String name ) {
334 DefaultMutableTreeNode category;
335 category = new DefaultMutableTreeNode( name );
337 if ( events.getNumberOfDuplications() > 0 ) {
338 addSubelement( category, EVENTS_DUPLICATIONS, String.valueOf( events.getNumberOfDuplications() ) );
340 if ( events.getNumberOfSpeciations() > 0 ) {
341 addSubelement( category, EVENTS_SPECIATIONS, String.valueOf( events.getNumberOfSpeciations() ) );
343 if ( events.getNumberOfGeneLosses() > 0 ) {
344 addSubelement( category, EVENTS_GENE_LOSSES, String.valueOf( events.getNumberOfGeneLosses() ) );
346 addSubelement( category, "Type", events.getEventType().toString() );
347 if ( events.getConfidence() != null ) {
348 addSubelement( category, CONFIDENCE, events.getConfidence().asText().toString() );
352 private static void addProperties( final DefaultMutableTreeNode top,
353 final PropertiesMap properties,
354 final String string ) {
355 final SortedMap<String, Property> properties_map = properties.getProperties();
356 final DefaultMutableTreeNode category = new DefaultMutableTreeNode( "Properties " );
358 for( final String key : properties_map.keySet() ) {
359 final Property prop = properties_map.get( key );
360 category.add( new DefaultMutableTreeNode( prop.getRef() + "=" + prop.getValue() + " " + prop.getUnit()
361 + " [" + prop.getAppliesTo().toString() + "]" ) );
365 private static void addReference( final DefaultMutableTreeNode top, final Reference ref, final String name ) {
366 final DefaultMutableTreeNode category = new DefaultMutableTreeNode( name );
368 addSubelement( category, LIT_REFERENCE_DOI, ref.getDoi() );
369 addSubelement( category, LIT_REFERENCE_DESC, ref.getDescription() );
372 private static void addSequence( final DefaultMutableTreeNode top, final Sequence seq, final String name ) {
373 final DefaultMutableTreeNode category = new DefaultMutableTreeNode( name );
375 addSubelement( category, SEQ_NAME, seq.getName() );
376 addSubelement( category, SEQ_SYMBOL, seq.getSymbol() );
377 if ( seq.getAccession() != null ) {
378 addSubelement( category, SEQ_ACCESSION, seq.getAccession().asText().toString() );
380 addSubelement( category, SEQ_LOCATION, seq.getLocation() );
381 addSubelement( category, SEQ_TYPE, seq.getType() );
382 addSubelement( category, SEQ_MOL_SEQ, seq.getMolecularSequence() );
383 if ( ( seq.getUris() != null ) && !seq.getUris().isEmpty() ) {
384 addUris( top, seq.getUris(), category );
386 addAnnotations( top, seq.getAnnotations(), category );
389 private static void addSubelement( final DefaultMutableTreeNode node, final String name, final String value ) {
390 if ( !ForesterUtil.isEmpty( value ) ) {
391 node.add( new DefaultMutableTreeNode( name + ": " + value ) );
395 private static void addTaxonomy( final DefaultMutableTreeNode top, final Taxonomy tax, final String name ) {
396 final DefaultMutableTreeNode category = new DefaultMutableTreeNode( name );
398 if ( tax.getIdentifier() != null ) {
399 addSubelement( category, TAXONOMY_IDENTIFIER, tax.getIdentifier().asText().toString() );
401 addSubelement( category, TAXONOMY_CODE, tax.getTaxonomyCode() );
402 addSubelement( category, TAXONOMY_SCIENTIFIC_NAME, tax.getScientificName() );
403 addSubelement( category, TAXONOMY_AUTHORITY, tax.getAuthority() );
404 addSubelement( category, TAXONOMY_COMMON_NAME, tax.getCommonName() );
405 for( final String syn : tax.getSynonyms() ) {
406 addSubelement( category, TAXONOMY_SYNONYM, syn );
408 addSubelement( category, TAXONOMY_RANK, tax.getRank() );
409 if ( ( tax.getUris() != null ) && !tax.getUris().isEmpty() ) {
410 addUris( top, tax.getUris(), category );
412 if ( ( tax.getLineage() != null ) && !tax.getLineage().isEmpty() ) {
413 addLineage( top, tax.getLineage(), category );
417 private static void createNodes( final DefaultMutableTreeNode top, final PhylogenyNode phylogeny_node ) {
418 addBasics( top, phylogeny_node, BASIC );
420 if ( phylogeny_node.getNodeData().isHasTaxonomy() ) {
421 addTaxonomy( top, phylogeny_node.getNodeData().getTaxonomy(), TAXONOMY );
424 if ( phylogeny_node.getNodeData().isHasSequence() ) {
425 addSequence( top, phylogeny_node.getNodeData().getSequence(), SEQUENCE );
428 if ( phylogeny_node.getNodeData().isHasEvent() ) {
429 addEvents( top, phylogeny_node.getNodeData().getEvent(), EVENTS );
432 if ( phylogeny_node.getNodeData().isHasDate() ) {
433 addDate( top, phylogeny_node.getNodeData().getDate(), DATE );
436 if ( phylogeny_node.getNodeData().isHasDistribution() ) {
437 addDistribution( top, phylogeny_node.getNodeData().getDistribution(), DISTRIBUTION );
440 if ( phylogeny_node.getNodeData().isHasReference() ) {
441 addReference( top, phylogeny_node.getNodeData().getReference(), LIT_REFERENCE );
444 if ( phylogeny_node.getNodeData().isHasBinaryCharacters() ) {
445 addBinaryCharacters( top, phylogeny_node.getNodeData().getBinaryCharacters(), BINARY_CHARACTERS );
448 if ( phylogeny_node.getNodeData().isHasProperties() ) {
449 addProperties( top, phylogeny_node.getNodeData().getProperties(), PROP );