cleanup
[jalview.git] / forester / java / src / org / forester / archaeopteryx / NodePanel.java
1 // $Id:
2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
4 //
5 // Copyright (C) 2008-2009 Christian M. Zmasek
6 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
7 // All rights reserved
8 //
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.
13 //
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.
18 //
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
22 //
23 // Contact: phylosoft @ gmail . com
24 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
25
26 package org.forester.archaeopteryx;
27
28 import java.awt.Color;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.SortedMap;
32 import java.util.SortedSet;
33
34 import javax.swing.JEditorPane;
35 import javax.swing.JPanel;
36 import javax.swing.JScrollPane;
37 import javax.swing.JSplitPane;
38 import javax.swing.JTree;
39 import javax.swing.event.TreeSelectionEvent;
40 import javax.swing.event.TreeSelectionListener;
41 import javax.swing.text.Position;
42 import javax.swing.tree.DefaultMutableTreeNode;
43 import javax.swing.tree.TreePath;
44
45 import org.forester.phylogeny.PhylogenyMethods;
46 import org.forester.phylogeny.PhylogenyNode;
47 import org.forester.phylogeny.data.Accession;
48 import org.forester.phylogeny.data.Annotation;
49 import org.forester.phylogeny.data.BinaryCharacters;
50 import org.forester.phylogeny.data.BranchWidth;
51 import org.forester.phylogeny.data.Date;
52 import org.forester.phylogeny.data.Distribution;
53 import org.forester.phylogeny.data.Event;
54 import org.forester.phylogeny.data.PhylogenyData;
55 import org.forester.phylogeny.data.PhylogenyDataUtil;
56 import org.forester.phylogeny.data.Point;
57 import org.forester.phylogeny.data.PropertiesMap;
58 import org.forester.phylogeny.data.Property;
59 import org.forester.phylogeny.data.Reference;
60 import org.forester.phylogeny.data.Sequence;
61 import org.forester.phylogeny.data.Taxonomy;
62 import org.forester.phylogeny.data.Uri;
63 import org.forester.util.ForesterUtil;
64
65 class NodePanel extends JPanel implements TreeSelectionListener {
66
67     static final String       BASIC                    = "Basic";
68     static final String       BINARY_CHARACTERS        = "Binary characters";
69     static final String       CONFIDENCE               = "Confidence";
70     static final String       CONFIDENCE_TYPE          = "type";
71     static final String       DATE                     = "Date";
72     static final String       DATE_DESCRIPTION         = "Description";
73     static final String       DATE_MAX                 = "Max";
74     static final String       DATE_MIN                 = "Min";
75     static final String       DATE_UNIT                = "Unit";
76     static final String       DATE_VALUE               = "Value";
77     static final String       DIST_ALT_UNIT            = "Altitude unit";
78     static final String       DIST_ALTITUDE            = "Altitude";
79     static final String       DIST_DESCRIPTION         = "Description";
80     static final String       DIST_GEODETIC_DATUM      = "Geodetic datum";
81     static final String       DIST_LATITUDE            = "Latitude";
82     static final String       DIST_LONGITUDE           = "Longitude";
83     static final String       DISTRIBUTION             = "Distribution";
84     static final String       EVENTS                   = "Events";
85     static final String       EVENTS_DUPLICATIONS      = "Duplications";
86     static final String       EVENTS_GENE_LOSSES       = "Gene losses";
87     static final String       EVENTS_SPECIATIONS       = "Speciations";
88     static final String       LIT_REFERENCE            = "Reference";
89     static final String       LIT_REFERENCE_DESC       = "Description";
90     static final String       LIT_REFERENCE_DOI        = "DOI";
91     static final String       NODE_BRANCH_COLOR        = "Branch color";
92     static final String       NODE_BRANCH_LENGTH       = "Branch length";
93     static final String       NODE_BRANCH_WIDTH        = "Branch width";
94     static final String       NODE_NAME                = "Name";
95     static final String       PROP                     = "Properties";
96     static final String       REFERENCE                = "Reference";
97     static final String       SEQ_ACCESSION            = "Accession";
98     static final String       SEQ_LOCATION             = "Location";
99     static final String       SEQ_MOL_SEQ              = "Mol seq";
100     static final String       SEQ_NAME                 = "Name";
101     static final String       SEQ_SYMBOL               = "Symbol";
102     static final String       SEQ_GENE_NAME            = "Gene name";
103     static final String       SEQ_TYPE                 = "Type";
104     static final String       SEQ_URI                  = "URI";
105     static final String       SEQUENCE                 = "Sequence";
106     static final String       TAXONOMY                 = "Taxonomy";
107     static final String       TAXONOMY_AUTHORITY       = "Authority";
108     static final String       TAXONOMY_CODE            = "Code";
109     static final String       TAXONOMY_COMMON_NAME     = "Common name";
110     static final String       TAXONOMY_IDENTIFIER      = "Identifier";
111     static final String       TAXONOMY_RANK            = "Rank";
112     static final String       TAXONOMY_SCIENTIFIC_NAME = "Scientific name";
113     static final String       TAXONOMY_SYNONYM         = "Synonym";
114     static final String       TAXONOMY_URI             = "URI";
115     private static final long serialVersionUID         = 5120159904388100771L;
116     private final JEditorPane _pane;
117     private final JTree       _tree;
118
119     public NodePanel( final PhylogenyNode phylogeny_node ) {
120         String node_name = "";
121         if ( !ForesterUtil.isEmpty( phylogeny_node.getName() ) ) {
122             node_name = phylogeny_node.getName() + " ";
123         }
124         final DefaultMutableTreeNode top = new DefaultMutableTreeNode( "Node " + node_name );
125         createNodes( top, phylogeny_node );
126         _tree = new JTree( top );
127         _tree.setEditable( false );
128         getJTree().setToggleClickCount( 1 );
129         expandPath( BASIC );
130         expandPath( TAXONOMY );
131         expandPath( SEQUENCE );
132         expandPath( EVENTS );
133         final JScrollPane tree_view = new JScrollPane( getJTree() );
134         _pane = new JEditorPane();
135         _pane.setEditable( false );
136         final JScrollPane data_view = new JScrollPane( _pane );
137         final JSplitPane split_pane = new JSplitPane( JSplitPane.VERTICAL_SPLIT );
138         split_pane.setTopComponent( tree_view );
139         split_pane.setBottomComponent( data_view );
140         data_view.setMinimumSize( Constants.NODE_PANEL_SPLIT_MINIMUM_SIZE );
141         tree_view.setMinimumSize( Constants.NODE_PANEL_SPLIT_MINIMUM_SIZE );
142         split_pane.setDividerLocation( 400 );
143         split_pane.setPreferredSize( Constants.NODE_PANEL_SIZE );
144         add( split_pane );
145     }
146
147     @Override
148     public void valueChanged( final TreeSelectionEvent e ) {
149         // Do nothing.
150     }
151
152     private void expandPath( final String name ) {
153         final TreePath tp = getJTree().getNextMatch( name, 0, Position.Bias.Forward );
154         if ( tp != null ) {
155             getJTree().expandPath( tp );
156         }
157     }
158
159     private JTree getJTree() {
160         return _tree;
161     }
162
163     private static void addAnnotation( final DefaultMutableTreeNode top, final Annotation ann, final String name ) {
164         DefaultMutableTreeNode category;
165         category = new DefaultMutableTreeNode( name );
166         top.add( category );
167         addSubelement( category, "Source", ann.getSource() );
168         addSubelement( category, "Type", ann.getType() );
169         addSubelement( category, "Evidence", ann.getEvidence() );
170         if ( ann.getConfidence() != null ) {
171             addSubelement( category, CONFIDENCE, ann.getConfidence().asText().toString() );
172         }
173         if ( ann.getProperties() != null ) {
174             addProperties( category, ann.getProperties(), PROP );
175         }
176     }
177
178     private static void addAnnotations( final DefaultMutableTreeNode top,
179                                         final SortedSet<Annotation> annotations,
180                                         final DefaultMutableTreeNode category ) {
181         if ( ( annotations != null ) && ( annotations.size() > 0 ) ) {
182             category.add( new DefaultMutableTreeNode( "Annotations" ) );
183             final DefaultMutableTreeNode last = top.getLastLeaf();
184             for( final Annotation ann : annotations ) {
185                 addAnnotation( last, ann, ann.asText().toString() );
186             }
187         }
188     }
189
190     private static void addBasics( final DefaultMutableTreeNode top,
191                                    final PhylogenyNode phylogeny_node,
192                                    final String name ) {
193         final DefaultMutableTreeNode category = new DefaultMutableTreeNode( name );
194         top.add( category );
195         addSubelement( category, NODE_NAME, phylogeny_node.getName() );
196         if ( phylogeny_node.getDistanceToParent() != PhylogenyDataUtil.BRANCH_LENGTH_DEFAULT ) {
197             addSubelement( category,
198                            NODE_BRANCH_LENGTH,
199                            ForesterUtil.FORMATTER_6.format( phylogeny_node.getDistanceToParent() ) );
200         }
201         if ( phylogeny_node.getBranchData().isHasConfidences() ) {
202             for( final PhylogenyData conf : phylogeny_node.getBranchData().getConfidences() ) {
203                 addSubelement( category, CONFIDENCE, conf.asText().toString() );
204             }
205         }
206         if ( !phylogeny_node.isExternal() ) {
207             addSubelement( category, "Children", String.valueOf( phylogeny_node.getNumberOfDescendants() ) );
208             addSubelement( category,
209                            "External children",
210                            String.valueOf( phylogeny_node.getAllExternalDescendants().size() ) );
211             final Map<Taxonomy, Integer> distinct_tax = PhylogenyMethods.obtainDistinctTaxonomyCounts( phylogeny_node );
212             if ( distinct_tax != null ) {
213                 final int no_tax = PhylogenyMethods.calculateNumberOfExternalNodesWithoutTaxonomy( phylogeny_node );
214                 final int tax_count = distinct_tax.size();
215                 addSubelement( category, "Distinct external taxonomies", String.valueOf( tax_count ) );
216                 if ( no_tax > 0 ) {
217                     addSubelement( category, "External nodes without taxonomy", String.valueOf( no_tax ) );
218                 }
219             }
220         }
221         if ( !phylogeny_node.isRoot() ) {
222             addSubelement( category, "Depth", String.valueOf( phylogeny_node.calculateDepth() ) );
223             final double d = phylogeny_node.calculateDistanceToRoot();
224             if ( d > 0 ) {
225                 addSubelement( category, "Distance to root", String.valueOf( ForesterUtil.FORMATTER_6.format( d ) ) );
226             }
227         }
228         if ( ( phylogeny_node.getBranchData().getBranchWidth() != null )
229                 && ( phylogeny_node.getBranchData().getBranchWidth().getValue() != BranchWidth.BRANCH_WIDTH_DEFAULT_VALUE ) ) {
230             addSubelement( category,
231                            NODE_BRANCH_WIDTH,
232                            ForesterUtil.FORMATTER_3.format( phylogeny_node.getBranchData().getBranchWidth().getValue() ) );
233         }
234         if ( ( phylogeny_node.getBranchData().getBranchColor() != null ) ) {
235             final Color c = phylogeny_node.getBranchData().getBranchColor().getValue();
236             addSubelement( category, NODE_BRANCH_COLOR, c.getRed() + ", " + c.getGreen() + ", " + c.getBlue() );
237         }
238     }
239
240     private static void addBinaryCharacters( final DefaultMutableTreeNode top,
241                                              final BinaryCharacters bc,
242                                              final String name ) {
243         DefaultMutableTreeNode category;
244         category = new DefaultMutableTreeNode( name );
245         top.add( category );
246         addSubelement( category, "Gained", String.valueOf( bc.getGainedCount() ) );
247         addSubelement( category, "Lost", String.valueOf( bc.getLostCount() ) );
248         addSubelement( category, "Present", String.valueOf( bc.getPresentCount() ) );
249         final DefaultMutableTreeNode chars = new DefaultMutableTreeNode( "Lists" );
250         category.add( chars );
251         addSubelement( chars, "Gained", bc.getGainedCharactersAsStringBuffer().toString() );
252         addSubelement( chars, "Lost", bc.getLostCharactersAsStringBuffer().toString() );
253         addSubelement( chars, "Present", bc.getPresentCharactersAsStringBuffer().toString() );
254     }
255
256     private static void addCrossReference( final DefaultMutableTreeNode top, final Accession x, final String name ) {
257         DefaultMutableTreeNode category;
258         category = new DefaultMutableTreeNode( name );
259         top.add( category );
260     }
261
262     private static void addCrossReferences( final DefaultMutableTreeNode top,
263                                             final SortedSet<Accession> xs,
264                                             final DefaultMutableTreeNode category ) {
265         if ( ( xs != null ) && ( xs.size() > 0 ) ) {
266             category.add( new DefaultMutableTreeNode( "Cross references" ) );
267             final DefaultMutableTreeNode last = top.getLastLeaf();
268             for( final Accession x : xs ) {
269                 addCrossReference( last, x, x.asText().toString() );
270             }
271         }
272     }
273
274     private static void addDate( final DefaultMutableTreeNode top, final Date date, final String name ) {
275         DefaultMutableTreeNode category;
276         category = new DefaultMutableTreeNode( name );
277         top.add( category );
278         addSubelement( category, DATE_DESCRIPTION, date.getDesc() );
279         addSubelement( category, DATE_VALUE, String.valueOf( date.getValue() ) );
280         addSubelement( category, DATE_MIN, String.valueOf( date.getMin() ) );
281         addSubelement( category, DATE_MAX, String.valueOf( date.getMax() ) );
282         addSubelement( category, DATE_UNIT, date.getUnit() );
283     }
284
285     private static void addDistribution( final DefaultMutableTreeNode top, final Distribution dist, final String name ) {
286         DefaultMutableTreeNode category;
287         category = new DefaultMutableTreeNode( name );
288         top.add( category );
289         addSubelement( category, DIST_DESCRIPTION, dist.getDesc() );
290         if ( ( dist.getPoints() != null ) && ( dist.getPoints().size() > 0 ) ) {
291             final Point p0 = dist.getPoints().get( 0 );
292             if ( ( p0 != null ) && !Point.isSeemsEmpty( p0 ) ) {
293                 addSubelement( category, DIST_GEODETIC_DATUM, p0.getGeodeticDatum() );
294                 addSubelement( category, DIST_LATITUDE, String.valueOf( p0.getLatitude() ) );
295                 addSubelement( category, DIST_LONGITUDE, String.valueOf( p0.getLongitude() ) );
296                 String alt_unit = p0.getAltiudeUnit();
297                 if ( ForesterUtil.isEmpty( alt_unit ) ) {
298                     alt_unit = "?";
299                 }
300                 addSubelement( category, DIST_ALTITUDE, String.valueOf( p0.getAltitude() ) + alt_unit );
301             }
302         }
303     }
304
305     private static void addEvents( final DefaultMutableTreeNode top, final Event events, final String name ) {
306         DefaultMutableTreeNode category;
307         category = new DefaultMutableTreeNode( name );
308         top.add( category );
309         if ( events.getNumberOfDuplications() > 0 ) {
310             addSubelement( category, EVENTS_DUPLICATIONS, String.valueOf( events.getNumberOfDuplications() ) );
311         }
312         if ( events.getNumberOfSpeciations() > 0 ) {
313             addSubelement( category, EVENTS_SPECIATIONS, String.valueOf( events.getNumberOfSpeciations() ) );
314         }
315         if ( events.getNumberOfGeneLosses() > 0 ) {
316             addSubelement( category, EVENTS_GENE_LOSSES, String.valueOf( events.getNumberOfGeneLosses() ) );
317         }
318         addSubelement( category, "Type", events.getEventType().toString() );
319         if ( events.getConfidence() != null ) {
320             addSubelement( category, CONFIDENCE, events.getConfidence().asText().toString() );
321         }
322     }
323
324     private static void addLineage( final DefaultMutableTreeNode top,
325                                     final List<String> lineage,
326                                     final DefaultMutableTreeNode category ) {
327         if ( ( lineage != null ) && ( lineage.size() > 0 ) ) {
328             final StringBuilder sb = new StringBuilder();
329             for( final String lin : lineage ) {
330                 if ( !ForesterUtil.isEmpty( lin ) ) {
331                     sb.append( lin );
332                     sb.append( " > " );
333                 }
334             }
335             String str = null;
336             if ( sb.length() > 1 ) {
337                 str = sb.substring( 0, sb.length() - 3 );
338             }
339             if ( !ForesterUtil.isEmpty( str ) ) {
340                 addSubelement( category, "Lineage", str );
341             }
342         }
343     }
344
345     private static void addProperties( final DefaultMutableTreeNode top,
346                                        final PropertiesMap properties,
347                                        final String string ) {
348         final SortedMap<String, Property> properties_map = properties.getProperties();
349         final DefaultMutableTreeNode category = new DefaultMutableTreeNode( "Properties " );
350         top.add( category );
351         for( final String key : properties_map.keySet() ) {
352             final Property prop = properties_map.get( key );
353             category.add( new DefaultMutableTreeNode( prop.getRef() + "=" + prop.getValue() + " " + prop.getUnit()
354                     + " [" + prop.getAppliesTo().toString() + "]" ) );
355         }
356     }
357
358     private static void addReference( final DefaultMutableTreeNode top, final Reference ref, final String name ) {
359         final DefaultMutableTreeNode category = new DefaultMutableTreeNode( name );
360         top.add( category );
361         addSubelement( category, LIT_REFERENCE_DOI, ref.getDoi() );
362         addSubelement( category, LIT_REFERENCE_DESC, ref.getDescription() );
363     }
364
365     private static void addSequence( final DefaultMutableTreeNode top, final Sequence seq, final String name ) {
366         final DefaultMutableTreeNode category = new DefaultMutableTreeNode( name );
367         top.add( category );
368         addSubelement( category, SEQ_NAME, seq.getName() );
369         addSubelement( category, SEQ_SYMBOL, seq.getSymbol() );
370         addSubelement( category, SEQ_GENE_NAME, seq.getGeneName() );
371         if ( seq.getAccession() != null ) {
372             addSubelement( category, SEQ_ACCESSION, seq.getAccession().asText().toString() );
373         }
374         addSubelement( category, SEQ_LOCATION, seq.getLocation() );
375         addSubelement( category, SEQ_TYPE, seq.getType() );
376         addSubelement( category, SEQ_MOL_SEQ, seq.getMolecularSequence() );
377         if ( ( seq.getAnnotations() != null ) && !seq.getAnnotations().isEmpty() ) {
378             addAnnotations( top, seq.getAnnotations(), category );
379         }
380         if ( ( seq.getCrossReferences() != null ) && !seq.getCrossReferences().isEmpty() ) {
381             addCrossReferences( top, seq.getCrossReferences(), category );
382         }
383         if ( ( seq.getUris() != null ) && !seq.getUris().isEmpty() ) {
384             addUris( top, seq.getUris(), category );
385         }
386     }
387
388     private static void addSubelement( final DefaultMutableTreeNode node, final String name, final String value ) {
389         if ( !ForesterUtil.isEmpty( value ) ) {
390             node.add( new DefaultMutableTreeNode( name + ": " + value ) );
391         }
392     }
393
394     private static void addTaxonomy( final DefaultMutableTreeNode top, final Taxonomy tax, final String name ) {
395         final DefaultMutableTreeNode category = new DefaultMutableTreeNode( name );
396         top.add( category );
397         if ( tax.getIdentifier() != null ) {
398             addSubelement( category, TAXONOMY_IDENTIFIER, tax.getIdentifier().asText().toString() );
399         }
400         addSubelement( category, TAXONOMY_CODE, tax.getTaxonomyCode() );
401         addSubelement( category, TAXONOMY_SCIENTIFIC_NAME, tax.getScientificName() );
402         addSubelement( category, TAXONOMY_AUTHORITY, tax.getAuthority() );
403         addSubelement( category, TAXONOMY_COMMON_NAME, tax.getCommonName() );
404         for( final String syn : tax.getSynonyms() ) {
405             addSubelement( category, TAXONOMY_SYNONYM, syn );
406         }
407         addSubelement( category, TAXONOMY_RANK, tax.getRank() );
408         if ( ( tax.getUris() != null ) && !tax.getUris().isEmpty() ) {
409             addUris( top, tax.getUris(), category );
410         }
411         if ( ( tax.getLineage() != null ) && !tax.getLineage().isEmpty() ) {
412             addLineage( top, tax.getLineage(), category );
413         }
414     }
415
416     private static void addUri( final DefaultMutableTreeNode top, final Uri uri, final String name ) {
417         DefaultMutableTreeNode category;
418         category = new DefaultMutableTreeNode( name );
419         top.add( category );
420         addSubelement( category, "Description", uri.getDescription() );
421         addSubelement( category, "Type", uri.getType() );
422         addSubelement( category, "URI", uri.getValue().toString() );
423     }
424
425     private static void addUris( final DefaultMutableTreeNode top,
426                                  final List<Uri> uris,
427                                  final DefaultMutableTreeNode category ) {
428         if ( ( uris != null ) && ( uris.size() > 0 ) ) {
429             category.add( new DefaultMutableTreeNode( "URIs" ) );
430             final DefaultMutableTreeNode last = top.getLastLeaf();
431             int i = 0;
432             for( final Uri uri : uris ) {
433                 if ( uri != null ) {
434                     addUri( last, uri, "URI " + ( i++ ) );
435                 }
436             }
437         }
438     }
439
440     private static void createNodes( final DefaultMutableTreeNode top, final PhylogenyNode phylogeny_node ) {
441         addBasics( top, phylogeny_node, BASIC );
442         // Taxonomy
443         if ( phylogeny_node.getNodeData().isHasTaxonomy() ) {
444             addTaxonomy( top, phylogeny_node.getNodeData().getTaxonomy(), TAXONOMY );
445         }
446         // Sequence
447         if ( phylogeny_node.getNodeData().isHasSequence() ) {
448             addSequence( top, phylogeny_node.getNodeData().getSequence(), SEQUENCE );
449         }
450         // Events
451         if ( phylogeny_node.getNodeData().isHasEvent() ) {
452             addEvents( top, phylogeny_node.getNodeData().getEvent(), EVENTS );
453         }
454         // Date
455         if ( phylogeny_node.getNodeData().isHasDate() ) {
456             addDate( top, phylogeny_node.getNodeData().getDate(), DATE );
457         }
458         // Distribution
459         if ( phylogeny_node.getNodeData().isHasDistribution() ) {
460             addDistribution( top, phylogeny_node.getNodeData().getDistribution(), DISTRIBUTION );
461         }
462         // Reference
463         if ( phylogeny_node.getNodeData().isHasReference() ) {
464             addReference( top, phylogeny_node.getNodeData().getReference(), LIT_REFERENCE );
465         }
466         // BinaryCharacters
467         if ( phylogeny_node.getNodeData().isHasBinaryCharacters() ) {
468             addBinaryCharacters( top, phylogeny_node.getNodeData().getBinaryCharacters(), BINARY_CHARACTERS );
469         }
470         // Properties
471         if ( phylogeny_node.getNodeData().isHasProperties() ) {
472             addProperties( top, phylogeny_node.getNodeData().getProperties(), PROP );
473         }
474     }
475 }