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: www.phylosoft.org/
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.Point;
54 import org.forester.phylogeny.data.PropertiesMap;
55 import org.forester.phylogeny.data.Property;
56 import org.forester.phylogeny.data.Reference;
57 import org.forester.phylogeny.data.Sequence;
58 import org.forester.phylogeny.data.Taxonomy;
59 import org.forester.phylogeny.data.Uri;
60 import org.forester.util.ForesterUtil;
62 class NodePanel extends JPanel implements TreeSelectionListener {
64 static final String DIST_ALTITUDE = "Altitude";
65 static final String DIST_ALT_UNIT = "Altitude unit";
66 static final String DIST_LONGITUDE = "Longitude";
67 static final String DIST_LATITUDE = "Latitude";
68 static final String DIST_GEODETIC_DATUM = "Geodetic datum";
69 static final String DIST_DESCRIPTION = "Description";
70 static final String DATE_UNIT = "Unit";
71 static final String DATE_MAX = "Max";
72 static final String DATE_MIN = "Min";
73 static final String DATE_VALUE = "Value";
74 static final String DATE_DESCRIPTION = "Description";
75 static final String TAXONOMY_IDENTIFIER = "Identifier";
76 static final String SEQ_ACCESSION = "Accession";
77 static final String CONFIDENCE = "Confidence";
78 static final String PROP = "Properties";
79 static final String BINARY_CHARACTERS = "Binary characters";
80 static final String REFERENCE = "Reference";
81 static final String LIT_REFERENCE = "Reference";
82 static final String LIT_REFERENCE_DESC = "Description";
83 static final String LIT_REFERENCE_DOI = "DOI";
84 static final String DISTRIBUTION = "Distribution";
85 static final String DATE = "Date";
86 static final String EVENTS = "Events";
87 static final String SEQUENCE = "Sequence";
88 static final String TAXONOMY = "Taxonomy";
89 static final String BASIC = "Basic";
90 static final String TAXONOMY_SCIENTIFIC_NAME = "Scientific name";
91 static final String SEQ_MOL_SEQ = "Mol seq";
92 static final String SEQ_TYPE = "Type";
93 static final String SEQ_LOCATION = "Location";
94 static final String SEQ_SYMBOL = "Symbol";
95 static final String SEQ_URI = "URI";
96 static final String NODE_BRANCH_LENGTH = "Branch length";
97 static final String NODE_BRANCH_WIDTH = "Branch width";
98 static final String NODE_BRANCH_COLOR = "Branch color";
99 static final String NODE_NAME = "Name";
100 static final String TAXONOMY_URI = "URI";
101 static final String TAXONOMY_RANK = "Rank";
102 static final String TAXONOMY_SYNONYM = "Synonym";
103 static final String TAXONOMY_COMMON_NAME = "Common name";
104 static final String TAXONOMY_AUTHORITY = "Authority";
105 static final String TAXONOMY_CODE = "Code";
106 static final String SEQ_NAME = "Name";
107 static final String EVENTS_GENE_LOSSES = "Gene losses";
108 static final String EVENTS_SPECIATIONS = "Speciations";
109 static final String EVENTS_DUPLICATIONS = "Duplications";
110 private static final long serialVersionUID = 5120159904388100771L;
111 static final String CONFIDENCE_TYPE = "type";
112 private final JTree _tree;
113 private final JEditorPane _pane;
115 public NodePanel( final PhylogenyNode phylogeny_node ) {
116 String node_name = "";
117 if ( !ForesterUtil.isEmpty( phylogeny_node.getName() ) ) {
118 node_name = phylogeny_node.getName() + " ";
120 final DefaultMutableTreeNode top = new DefaultMutableTreeNode( "Node " + node_name );
121 createNodes( top, phylogeny_node );
122 _tree = new JTree( top );
123 _tree.setEditable( false );
124 getJTree().setToggleClickCount( 1 );
126 expandPath( TAXONOMY );
127 expandPath( SEQUENCE );
128 expandPath( EVENTS );
129 final JScrollPane tree_view = new JScrollPane( getJTree() );
130 _pane = new JEditorPane();
131 _pane.setEditable( false );
132 final JScrollPane data_view = new JScrollPane( _pane );
133 final JSplitPane split_pane = new JSplitPane( JSplitPane.VERTICAL_SPLIT );
134 split_pane.setTopComponent( tree_view );
135 split_pane.setBottomComponent( data_view );
136 data_view.setMinimumSize( Constants.NODE_PANEL_SPLIT_MINIMUM_SIZE );
137 tree_view.setMinimumSize( Constants.NODE_PANEL_SPLIT_MINIMUM_SIZE );
138 split_pane.setDividerLocation( 400 );
139 split_pane.setPreferredSize( Constants.NODE_PANEL_SIZE );
143 private void expandPath( final String name ) {
144 final TreePath tp = getJTree().getNextMatch( name, 0, Position.Bias.Forward );
146 getJTree().expandPath( tp );
150 private JTree getJTree() {
155 public void valueChanged( final TreeSelectionEvent e ) {
159 private static void addAnnotation( final DefaultMutableTreeNode top, final Annotation ann, final String name ) {
160 DefaultMutableTreeNode category;
161 category = new DefaultMutableTreeNode( name );
163 addSubelement( category, REFERENCE, ann.getRef() );
164 addSubelement( category, "Description", ann.getDesc() );
165 addSubelement( category, "Source", ann.getSource() );
166 addSubelement( category, "Type", ann.getType() );
167 addSubelement( category, "Evidence", ann.getEvidence() );
168 if ( ann.getConfidence() != null ) {
169 addSubelement( category, CONFIDENCE, ann.getConfidence().asText().toString() );
171 if ( ann.getProperties() != null ) {
172 addProperties( category, ann.getProperties(), PROP );
176 private static void addUri( final DefaultMutableTreeNode top, final Uri uri, final String name ) {
177 DefaultMutableTreeNode category;
178 category = new DefaultMutableTreeNode( name );
180 addSubelement( category, "Description", uri.getDescription() );
181 addSubelement( category, "Type", uri.getType() );
182 addSubelement( category, "URI", uri.getValue().toString() );
185 private static void addAnnotations( final DefaultMutableTreeNode top,
186 final SortedSet<Annotation> annotations,
187 final DefaultMutableTreeNode category ) {
188 if ( ( annotations != null ) && ( annotations.size() > 0 ) ) {
189 category.add( new DefaultMutableTreeNode( "Annotations" ) );
190 final DefaultMutableTreeNode last = top.getLastLeaf();
192 for( final PhylogenyData ann : annotations ) {
193 addAnnotation( last, ( Annotation ) ann, "Annotation " + ( i++ ) );
198 private static void addUris( final DefaultMutableTreeNode top,
199 final List<Uri> uris,
200 final DefaultMutableTreeNode category ) {
201 if ( ( uris != null ) && ( uris.size() > 0 ) ) {
202 category.add( new DefaultMutableTreeNode( "URIs" ) );
203 final DefaultMutableTreeNode last = top.getLastLeaf();
205 for( final Uri uri : uris ) {
207 addUri( last, uri, "URI " + ( i++ ) );
213 private static void addLineage( final DefaultMutableTreeNode top,
214 final List<String> lineage,
215 final DefaultMutableTreeNode category ) {
216 if ( ( lineage != null ) && ( lineage.size() > 0 ) ) {
217 final StringBuilder sb = new StringBuilder();
218 for( final String lin : lineage ) {
219 if ( !ForesterUtil.isEmpty( lin ) ) {
225 if ( sb.length() > 1 ) {
226 str = sb.substring( 0, sb.length() - 3 );
228 if ( !ForesterUtil.isEmpty( str ) ) {
229 addSubelement( category, "Lineage", str );
234 private static void addBasics( final DefaultMutableTreeNode top,
235 final PhylogenyNode phylogeny_node,
236 final String name ) {
237 final DefaultMutableTreeNode category = new DefaultMutableTreeNode( name );
239 addSubelement( category, NODE_NAME, phylogeny_node.getName() );
240 if ( phylogeny_node.getDistanceToParent() != PhylogenyNode.DISTANCE_DEFAULT ) {
241 addSubelement( category,
243 ForesterUtil.FORMATTER_6.format( phylogeny_node.getDistanceToParent() ) );
245 if ( phylogeny_node.getBranchData().isHasConfidences() ) {
246 for( final PhylogenyData conf : phylogeny_node.getBranchData().getConfidences() ) {
247 addSubelement( category, CONFIDENCE, conf.asText().toString() );
250 if ( !phylogeny_node.isExternal() ) {
251 addSubelement( category, "Children", String.valueOf( phylogeny_node.getNumberOfDescendants() ) );
252 addSubelement( category,
254 String.valueOf( phylogeny_node.getAllExternalDescendants().size() ) );
255 final SortedMap<Taxonomy, Integer> distinct_tax = PhylogenyMethods
256 .obtainDistinctTaxonomyCounts( phylogeny_node );
257 if ( distinct_tax != null ) {
258 final int no_tax = PhylogenyMethods.calculateNumberOfExternalNodesWithoutTaxonomy( phylogeny_node );
259 final int tax_count = distinct_tax.size();
260 addSubelement( category, "Distinct external taxonomies", String.valueOf( tax_count ) );
262 addSubelement( category, "External nodes without taxonomy", String.valueOf( no_tax ) );
264 //TODO remove me... maybe make me into a method?
265 //for( final Taxonomy taxonomy : distinct_tax.keySet() ) {
266 // System.out.println( taxonomy + ": " + distinct_tax.get( taxonomy ) );
270 if ( !phylogeny_node.isRoot() ) {
271 addSubelement( category, "Depth", String.valueOf( PhylogenyMethods.calculateDepth( phylogeny_node ) ) );
272 final double d = PhylogenyMethods.calculateDistanceToRoot( phylogeny_node );
274 addSubelement( category, "Distance to root", String.valueOf( ForesterUtil.FORMATTER_6.format( d ) ) );
277 if ( ( phylogeny_node.getBranchData().getBranchWidth() != null )
278 && ( phylogeny_node.getBranchData().getBranchWidth().getValue() != BranchWidth.BRANCH_WIDTH_DEFAULT_VALUE ) ) {
279 addSubelement( category,
281 ForesterUtil.FORMATTER_3.format( phylogeny_node.getBranchData().getBranchWidth().getValue() ) );
283 if ( ( phylogeny_node.getBranchData().getBranchColor() != null ) ) {
284 final Color c = phylogeny_node.getBranchData().getBranchColor().getValue();
285 addSubelement( category, NODE_BRANCH_COLOR, c.getRed() + ", " + c.getGreen() + ", " + c.getBlue() );
289 private static void addBinaryCharacters( final DefaultMutableTreeNode top,
290 final BinaryCharacters bc,
291 final String name ) {
292 DefaultMutableTreeNode category;
293 category = new DefaultMutableTreeNode( name );
295 addSubelement( category, "Gained", String.valueOf( bc.getGainedCount() ) );
296 addSubelement( category, "Lost", String.valueOf( bc.getLostCount() ) );
297 addSubelement( category, "Present", String.valueOf( bc.getPresentCount() ) );
298 final DefaultMutableTreeNode chars = new DefaultMutableTreeNode( "Lists" );
299 category.add( chars );
300 addSubelement( chars, "Gained", bc.getGainedCharactersAsStringBuffer().toString() );
301 addSubelement( chars, "Lost", bc.getLostCharactersAsStringBuffer().toString() );
302 addSubelement( chars, "Present", bc.getPresentCharactersAsStringBuffer().toString() );
305 private static void addDate( final DefaultMutableTreeNode top, final Date date, final String name ) {
306 DefaultMutableTreeNode category;
307 category = new DefaultMutableTreeNode( name );
309 addSubelement( category, DATE_DESCRIPTION, date.getDesc() );
310 addSubelement( category, DATE_VALUE, String.valueOf( date.getValue() ) );
311 addSubelement( category, DATE_MIN, String.valueOf( date.getMin() ) );
312 addSubelement( category, DATE_MAX, String.valueOf( date.getMax() ) );
313 addSubelement( category, DATE_UNIT, date.getUnit() );
316 private static void addDistribution( final DefaultMutableTreeNode top, final Distribution dist, final String name ) {
317 DefaultMutableTreeNode category;
318 category = new DefaultMutableTreeNode( name );
320 addSubelement( category, DIST_DESCRIPTION, dist.getDesc() );
321 if ( ( dist.getPoints() != null ) && ( dist.getPoints().size() > 0 ) ) {
322 final Point p0 = dist.getPoints().get( 0 );
324 addSubelement( category, DIST_GEODETIC_DATUM, p0.getGeodeticDatum() );
325 addSubelement( category, DIST_LATITUDE, String.valueOf( p0.getLatitude() ) );
326 addSubelement( category, DIST_LONGITUDE, String.valueOf( p0.getLongitude() ) );
327 String alt_unit = p0.getAltiudeUnit();
328 if ( ForesterUtil.isEmpty( alt_unit ) ) {
331 addSubelement( category, DIST_ALTITUDE, String.valueOf( p0.getAltitude() ) + alt_unit );
336 private static void addEvents( final DefaultMutableTreeNode top, final Event events, final String name ) {
337 DefaultMutableTreeNode category;
338 category = new DefaultMutableTreeNode( name );
340 if ( events.getNumberOfDuplications() > 0 ) {
341 addSubelement( category, EVENTS_DUPLICATIONS, String.valueOf( events.getNumberOfDuplications() ) );
343 if ( events.getNumberOfSpeciations() > 0 ) {
344 addSubelement( category, EVENTS_SPECIATIONS, String.valueOf( events.getNumberOfSpeciations() ) );
346 if ( events.getNumberOfGeneLosses() > 0 ) {
347 addSubelement( category, EVENTS_GENE_LOSSES, String.valueOf( events.getNumberOfGeneLosses() ) );
349 addSubelement( category, "Type", events.getEventType().toString() );
350 if ( events.getConfidence() != null ) {
351 addSubelement( category, CONFIDENCE, events.getConfidence().asText().toString() );
355 private static void addProperties( final DefaultMutableTreeNode top,
356 final PropertiesMap properties,
357 final String string ) {
358 final SortedMap<String, Property> properties_map = properties.getProperties();
359 final DefaultMutableTreeNode category = new DefaultMutableTreeNode( "Properties " );
361 for( final String key : properties_map.keySet() ) {
362 final Property prop = properties_map.get( key );
363 category.add( new DefaultMutableTreeNode( prop.getRef() + " " + prop.getValue() + " " + prop.getUnit()
364 + " [" + prop.getAppliesTo().toString() + "]" ) );
368 private static void addReference( final DefaultMutableTreeNode top, final Reference ref, final String name ) {
369 final DefaultMutableTreeNode category = new DefaultMutableTreeNode( name );
371 addSubelement( category, LIT_REFERENCE_DOI, ref.getDoi() );
372 addSubelement( category, LIT_REFERENCE_DESC, ref.getDescription() );
375 private static void addSequence( final DefaultMutableTreeNode top, final Sequence seq, final String name ) {
376 final DefaultMutableTreeNode category = new DefaultMutableTreeNode( name );
378 addSubelement( category, SEQ_NAME, seq.getName() );
379 addSubelement( category, SEQ_SYMBOL, seq.getSymbol() );
380 if ( seq.getAccession() != null ) {
381 addSubelement( category, SEQ_ACCESSION, seq.getAccession().asText().toString() );
383 addSubelement( category, SEQ_LOCATION, seq.getLocation() );
384 addSubelement( category, SEQ_TYPE, seq.getType() );
385 addSubelement( category, SEQ_MOL_SEQ, seq.getMolecularSequence() );
386 if ( ( seq.getUris() != null ) && !seq.getUris().isEmpty() ) {
387 addUris( top, seq.getUris(), category );
389 addAnnotations( top, seq.getAnnotations(), category );
392 private static void addSubelement( final DefaultMutableTreeNode node, final String name, final String value ) {
393 if ( !ForesterUtil.isEmpty( value ) ) {
394 node.add( new DefaultMutableTreeNode( name + ": " + value ) );
398 private static void addTaxonomy( final DefaultMutableTreeNode top, final Taxonomy tax, final String name ) {
399 final DefaultMutableTreeNode category = new DefaultMutableTreeNode( name );
401 if ( tax.getIdentifier() != null ) {
402 addSubelement( category, TAXONOMY_IDENTIFIER, tax.getIdentifier().asText().toString() );
404 addSubelement( category, TAXONOMY_CODE, tax.getTaxonomyCode() );
405 addSubelement( category, TAXONOMY_SCIENTIFIC_NAME, tax.getScientificName() );
406 addSubelement( category, TAXONOMY_AUTHORITY, tax.getAuthority() );
407 addSubelement( category, TAXONOMY_COMMON_NAME, tax.getCommonName() );
408 for( final String syn : tax.getSynonyms() ) {
409 addSubelement( category, TAXONOMY_SYNONYM, syn );
411 addSubelement( category, TAXONOMY_RANK, tax.getRank() );
412 if ( ( tax.getUris() != null ) && !tax.getUris().isEmpty() ) {
413 addUris( top, tax.getUris(), category );
415 if ( ( tax.getLineage() != null ) && !tax.getLineage().isEmpty() ) {
416 addLineage( top, tax.getLineage(), category );
420 private static void createNodes( final DefaultMutableTreeNode top, final PhylogenyNode phylogeny_node ) {
421 addBasics( top, phylogeny_node, BASIC );
423 if ( phylogeny_node.getNodeData().isHasTaxonomy() ) {
424 addTaxonomy( top, phylogeny_node.getNodeData().getTaxonomy(), TAXONOMY );
427 if ( phylogeny_node.getNodeData().isHasSequence() ) {
428 addSequence( top, phylogeny_node.getNodeData().getSequence(), SEQUENCE );
431 if ( phylogeny_node.getNodeData().isHasEvent() ) {
432 addEvents( top, phylogeny_node.getNodeData().getEvent(), EVENTS );
435 if ( phylogeny_node.getNodeData().isHasDate() ) {
436 addDate( top, phylogeny_node.getNodeData().getDate(), DATE );
439 if ( phylogeny_node.getNodeData().isHasDistribution() ) {
440 addDistribution( top, phylogeny_node.getNodeData().getDistribution(), DISTRIBUTION );
443 if ( phylogeny_node.getNodeData().isHasReference() ) {
444 addReference( top, phylogeny_node.getNodeData().getReference(), LIT_REFERENCE );
447 if ( phylogeny_node.getNodeData().isHasBinaryCharacters() ) {
448 addBinaryCharacters( top, phylogeny_node.getNodeData().getBinaryCharacters(), BINARY_CHARACTERS );
451 if ( phylogeny_node.getNodeData().isHasProperties() ) {
452 addProperties( top, phylogeny_node.getNodeData().getProperties(), PROP );