Fixed issue with reading from TreeBase
[jalview.git] / forester / java / src / org / forester / archaeopteryx / NodeEditPanel.java
index 9780d25..85476b1 100644 (file)
@@ -21,7 +21,7 @@
 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 //
 // Contact: phylosoft @ gmail . com
-// WWW: www.phylosoft.org/
+// WWW: https://sites.google.com/site/cmzmasek/home/software/forester
 
 package org.forester.archaeopteryx;
 
@@ -48,9 +48,11 @@ import javax.swing.tree.DefaultMutableTreeNode;
 import javax.swing.tree.TreePath;
 import javax.swing.tree.TreeSelectionModel;
 
+import org.forester.archaeopteryx.tools.ImageLoader;
 import org.forester.io.parsers.phyloxml.PhyloXmlDataFormatException;
 import org.forester.phylogeny.PhylogenyNode;
 import org.forester.phylogeny.data.Accession;
+import org.forester.phylogeny.data.BranchWidth;
 import org.forester.phylogeny.data.Confidence;
 import org.forester.phylogeny.data.Date;
 import org.forester.phylogeny.data.Distribution;
@@ -58,6 +60,7 @@ import org.forester.phylogeny.data.Event;
 import org.forester.phylogeny.data.Identifier;
 import org.forester.phylogeny.data.MultipleUris;
 import org.forester.phylogeny.data.PhylogenyData;
+import org.forester.phylogeny.data.PhylogenyDataUtil;
 import org.forester.phylogeny.data.Point;
 import org.forester.phylogeny.data.Reference;
 import org.forester.phylogeny.data.Sequence;
@@ -68,6 +71,71 @@ import org.forester.util.ForesterUtil;
 
 class NodeEditPanel extends JPanel {
 
+    private enum PHYLOXML_TAG {
+        NODE_NAME,
+        NODE_BRANCH_LENGTH,
+        NODE_BRANCH_WIDTH,
+        TAXONOMY_CODE,
+        TAXONOMY_SCIENTIFIC_NAME,
+        TAXONOMY_AUTHORITY,
+        TAXONOMY_COMMON_NAME,
+        TAXONOMY_SYNONYM,
+        TAXONOMY_RANK,
+        TAXONOMY_URI,
+        SEQ_SYMBOL,
+        SEQ_NAME,
+        SEQ_GENE_NAME,
+        SEQ_LOCATION,
+        SEQ_TYPE,
+        SEQ_MOL_SEQ,
+        SEQ_URI,
+        DATE_DESCRIPTION,
+        DATE_VALUE,
+        DATE_MIN,
+        DATE_MAX,
+        DATE_UNIT,
+        TAXONOMY_ID_VALUE,
+        TAXONOMY_ID_PROVIDER,
+        SEQ_ACC_VALUE,
+        SEQ_ACC_SOURCE,
+        CONFIDENCE_VALUE,
+        CONFIDENCE_TYPE,
+        LIT_REFERENCE_DESC,
+        LIT_REFERENCE_DOI,
+        EVENTS_DUPLICATIONS,
+        EVENTS_SPECIATIONS,
+        EVENTS_GENE_LOSSES,
+        DIST_DESC,
+        DIST_GEODETIC,
+        DIST_LAT,
+        DIST_LONG,
+        DIST_ALT,
+        DIST_ALT_UNIT
+    }
+
+    private class TagNumber {
+
+        final private PHYLOXML_TAG _tag;
+        final private int          _number;
+
+        TagNumber( final PHYLOXML_TAG tag, final int number ) {
+            _tag = tag;
+            _number = number;
+        }
+
+        @Override
+        public String toString() {
+            return getTag() + "_" + getNumber();
+        }
+
+        int getNumber() {
+            return _number;
+        }
+
+        PHYLOXML_TAG getTag() {
+            return _tag;
+        }
+    }
     private static final long                            serialVersionUID = 5120159904388100771L;
     private final JTree                                  _tree;
     private final JEditorPane                            _pane;
@@ -97,10 +165,10 @@ class NodeEditPanel extends JPanel {
         final JSplitPane split_pane = new JSplitPane( JSplitPane.VERTICAL_SPLIT );
         split_pane.setTopComponent( tree_view );
         // split_pane.setBottomComponent( data_view );
-        data_view.setMinimumSize( Constants.NODE_PANEL_SPLIT_MINIMUM_SIZE );
-        tree_view.setMinimumSize( Constants.NODE_PANEL_SPLIT_MINIMUM_SIZE );
+        data_view.setMinimumSize( AptxConstants.NODE_PANEL_SPLIT_MINIMUM_SIZE );
+        tree_view.setMinimumSize( AptxConstants.NODE_PANEL_SPLIT_MINIMUM_SIZE );
         // split_pane.setDividerLocation( 400 );
-        split_pane.setPreferredSize( Constants.NODE_PANEL_SIZE );
+        split_pane.setPreferredSize( AptxConstants.NODE_PANEL_SIZE );
         add( split_pane );
         getJTree().getSelectionModel().setSelectionMode( TreeSelectionModel.SINGLE_TREE_SELECTION );
         getJTree().addKeyListener( new KeyListener() {
@@ -151,7 +219,7 @@ class NodeEditPanel extends JPanel {
         top.add( category );
         addSubelementEditable( category, NodePanel.NODE_NAME, phylogeny_node.getName(), PHYLOXML_TAG.NODE_NAME );
         String bl = "";
-        if ( phylogeny_node.getDistanceToParent() != PhylogenyNode.DISTANCE_DEFAULT ) {
+        if ( phylogeny_node.getDistanceToParent() != PhylogenyDataUtil.BRANCH_LENGTH_DEFAULT ) {
             bl = ForesterUtil.FORMATTER_6.format( phylogeny_node.getDistanceToParent() );
         }
         addSubelementEditable( category, NodePanel.NODE_BRANCH_LENGTH, bl, PHYLOXML_TAG.NODE_BRANCH_LENGTH );
@@ -182,6 +250,12 @@ class NodeEditPanel extends JPanel {
                                "",
                                PHYLOXML_TAG.CONFIDENCE_TYPE,
                                counter );
+        String bw = "1";
+        if ( ( phylogeny_node.getBranchData().getBranchWidth() != null )
+                && ( phylogeny_node.getBranchData().getBranchWidth().getValue() != BranchWidth.BRANCH_WIDTH_DEFAULT_VALUE ) ) {
+            bw = ForesterUtil.FORMATTER_3.format( phylogeny_node.getBranchData().getBranchWidth().getValue() );
+        }
+        addSubelementEditable( category, NodePanel.NODE_BRANCH_WIDTH, bw, PHYLOXML_TAG.NODE_BRANCH_WIDTH );
     }
 
     //    private void addAnnotation( final DefaultMutableTreeNode top, final Annotation ann, final String name ) {
@@ -328,6 +402,7 @@ class NodeEditPanel extends JPanel {
         }
         addSubelementEditable( category, NodePanel.SEQ_NAME, seq.getName(), PHYLOXML_TAG.SEQ_NAME );
         addSubelementEditable( category, NodePanel.SEQ_SYMBOL, seq.getSymbol(), PHYLOXML_TAG.SEQ_SYMBOL );
+        addSubelementEditable( category, NodePanel.SEQ_GENE_NAME, seq.getGeneName(), PHYLOXML_TAG.SEQ_GENE_NAME );
         addSubelementEditable( category,
                                NodePanel.SEQ_ACCESSION,
                                acc.getValue(),
@@ -481,6 +556,25 @@ class NodeEditPanel extends JPanel {
                                uri_counter );
     }
 
+    private void addUri( final DefaultMutableTreeNode mtn, final Uri uri, final int number, final MultipleUris mu ) {
+        if ( uri != null ) {
+            if ( mu.getUris() == null ) {
+                mu.setUris( new ArrayList<Uri>() );
+            }
+        }
+        if ( ( uri != null ) && ( mu.getUris() == null ) ) {
+            mu.setUris( new ArrayList<Uri>() );
+        }
+        if ( ( uri != null ) && ( mu.getUris().size() == number ) ) {
+            mu.getUris().add( uri );
+        }
+        if ( ( mu.getUris() != null ) && ( mu.getUris().size() != number ) ) {
+            mu.getUris().set( number, uri );
+        }
+        final ImageLoader il = new ImageLoader( getTreePanel() );
+        new Thread( il ).start();
+    }
+
     private void collapsePath( final String name ) {
         final TreePath tp = getJTree().getNextMatch( name, 0, Position.Bias.Forward );
         if ( tp != null ) {
@@ -531,10 +625,6 @@ class NodeEditPanel extends JPanel {
         return getMap().get( mtn );
     }
 
-    PhylogenyNode getMyNode() {
-        return _my_node;
-    }
-
     private DefaultMutableTreeNode getSelectedTreeNode() {
         final TreePath selectionPath = getJTree().getSelectionPath();
         if ( selectionPath != null ) {
@@ -607,13 +697,6 @@ class NodeEditPanel extends JPanel {
         return i;
     }
 
-    void writeAll() {
-        for( int i = 0; i < getJTree().getRowCount(); i++ ) {
-            final TreePath p = getJTree().getPathForRow( i );
-            writeBack( ( DefaultMutableTreeNode ) p.getLastPathComponent() );
-        }
-    }
-
     private void writeBack( final DefaultMutableTreeNode mtn ) {
         if ( !getMap().containsKey( mtn ) ) {
             final DefaultMutableTreeNode parent = ( DefaultMutableTreeNode ) mtn.getParent();
@@ -643,7 +726,7 @@ class NodeEditPanel extends JPanel {
                 break;
             case NODE_BRANCH_LENGTH:
                 if ( ForesterUtil.isEmpty( value ) ) {
-                    getMyNode().setDistanceToParent( PhylogenyNode.DISTANCE_DEFAULT );
+                    getMyNode().setDistanceToParent( PhylogenyDataUtil.BRANCH_LENGTH_DEFAULT );
                 }
                 else {
                     try {
@@ -658,6 +741,28 @@ class NodeEditPanel extends JPanel {
                     }
                 }
                 break;
+            case NODE_BRANCH_WIDTH:
+                if ( ForesterUtil.isEmpty( value ) || value.equals( "1" ) ) {
+                    if ( getMyNode().getBranchData().getBranchWidth() != null ) {
+                        getMyNode().getBranchData().setBranchWidth( new BranchWidth() );
+                    }
+                }
+                else {
+                    try {
+                        final double bw = ForesterUtil.parseDouble( value );
+                        if ( bw >= 0 ) {
+                            getMyNode().getBranchData().setBranchWidth( new BranchWidth( bw ) );
+                        }
+                    }
+                    catch ( final ParseException e ) {
+                        JOptionPane.showMessageDialog( this,
+                                                       "failed to parse branch width from: " + value,
+                                                       "Error",
+                                                       JOptionPane.ERROR_MESSAGE );
+                        mtn.setUserObject( "" );
+                    }
+                }
+                break;
             case CONFIDENCE_VALUE:
                 double confidence = Confidence.CONFIDENCE_DEFAULT_VALUE;
                 if ( !ForesterUtil.isEmpty( value ) ) {
@@ -683,7 +788,8 @@ class NodeEditPanel extends JPanel {
                 }
                 else {
                     final String type = getMyNode().getBranchData().getConfidences().get( number ).getType();
-                    getMyNode().getBranchData().getConfidences().set( number, new Confidence( confidence, type ) );
+                    final double sd = getMyNode().getBranchData().getConfidences().get( number ).getStandardDeviation();
+                    getMyNode().getBranchData().getConfidences().set( number, new Confidence( confidence, type, sd ) );
                 }
                 break;
             case CONFIDENCE_TYPE:
@@ -697,7 +803,8 @@ class NodeEditPanel extends JPanel {
                 }
                 else {
                     final double v = getMyNode().getBranchData().getConfidences().get( number ).getValue();
-                    getMyNode().getBranchData().getConfidences().set( number, new Confidence( v, value ) );
+                    final double sd = getMyNode().getBranchData().getConfidences().get( number ).getStandardDeviation();
+                    getMyNode().getBranchData().getConfidences().set( number, new Confidence( v, value, sd ) );
                 }
                 break;
             case TAXONOMY_CODE:
@@ -792,7 +899,7 @@ class NodeEditPanel extends JPanel {
                 break;
             case SEQ_MOL_SEQ:
                 ForesterUtil.ensurePresenceOfSequence( getMyNode() );
-                getMyNode().getNodeData().getSequence().setMolecularSequence( value );
+                getMyNode().getNodeData().getSequence().setMolecularSequence( value.replaceAll( "[^a-zA-Z-]", "" ) );
                 break;
             case SEQ_NAME:
                 ForesterUtil.ensurePresenceOfSequence( getMyNode() );
@@ -808,6 +915,10 @@ class NodeEditPanel extends JPanel {
                     break;
                 }
                 break;
+            case SEQ_GENE_NAME:
+                ForesterUtil.ensurePresenceOfSequence( getMyNode() );
+                getMyNode().getNodeData().getSequence().setGeneName( value );
+                break;
             case SEQ_TYPE:
                 ForesterUtil.ensurePresenceOfSequence( getMyNode() );
                 try {
@@ -997,86 +1108,14 @@ class NodeEditPanel extends JPanel {
         getTreePanel().repaint();
     }
 
-    private void addUri( final DefaultMutableTreeNode mtn, final Uri uri, final int number, final MultipleUris mu ) {
-        if ( uri != null ) {
-            if ( mu.getUris() == null ) {
-                mu.setUris( new ArrayList<Uri>() );
-            }
-        }
-        if ( ( uri != null ) && ( mu.getUris() == null ) ) {
-            mu.setUris( new ArrayList<Uri>() );
-        }
-        if ( ( uri != null ) && ( mu.getUris().size() == number ) ) {
-            mu.getUris().add( uri );
-        }
-        if ( ( mu.getUris() != null ) && ( mu.getUris().size() != number ) ) {
-            mu.getUris().set( number, uri );
-        }
-        final ImageLoader il = new ImageLoader( getTreePanel() );
-        new Thread( il ).start();
-    }
-
-    private enum PHYLOXML_TAG {
-        NODE_NAME,
-        NODE_BRANCH_LENGTH,
-        TAXONOMY_CODE,
-        TAXONOMY_SCIENTIFIC_NAME,
-        TAXONOMY_AUTHORITY,
-        TAXONOMY_COMMON_NAME,
-        TAXONOMY_SYNONYM,
-        TAXONOMY_RANK,
-        TAXONOMY_URI,
-        SEQ_SYMBOL,
-        SEQ_NAME,
-        SEQ_LOCATION,
-        SEQ_TYPE,
-        SEQ_MOL_SEQ,
-        SEQ_URI,
-        DATE_DESCRIPTION,
-        DATE_VALUE,
-        DATE_MIN,
-        DATE_MAX,
-        DATE_UNIT,
-        TAXONOMY_ID_VALUE,
-        TAXONOMY_ID_PROVIDER,
-        SEQ_ACC_VALUE,
-        SEQ_ACC_SOURCE,
-        CONFIDENCE_VALUE,
-        CONFIDENCE_TYPE,
-        LIT_REFERENCE_DESC,
-        LIT_REFERENCE_DOI,
-        EVENTS_DUPLICATIONS,
-        EVENTS_SPECIATIONS,
-        EVENTS_GENE_LOSSES,
-        DIST_DESC,
-        DIST_GEODETIC,
-        DIST_LAT,
-        DIST_LONG,
-        DIST_ALT,
-        DIST_ALT_UNIT
+    PhylogenyNode getMyNode() {
+        return _my_node;
     }
 
-    private class TagNumber {
-
-        final private PHYLOXML_TAG _tag;
-        final private int          _number;
-
-        TagNumber( final PHYLOXML_TAG tag, final int number ) {
-            _tag = tag;
-            _number = number;
-        }
-
-        int getNumber() {
-            return _number;
-        }
-
-        PHYLOXML_TAG getTag() {
-            return _tag;
-        }
-
-        @Override
-        public String toString() {
-            return getTag() + "_" + getNumber();
+    void writeAll() {
+        for( int i = 0; i < getJTree().getRowCount(); i++ ) {
+            final TreePath p = getJTree().getPathForRow( i );
+            writeBack( ( DefaultMutableTreeNode ) p.getLastPathComponent() );
         }
     }
 }