inprogress
[jalview.git] / forester / java / src / org / forester / msa_compactor / Chart.java
1 // $Id:
2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
4 //
5 // Copyright (C) 2014 Christian M. Zmasek
6 // Copyright (C) 2014 Sanford-Burnham Medical Research Institute
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 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
24
25 package org.forester.msa_compactor;
26
27 import java.awt.BorderLayout;
28 import java.awt.event.ActionListener;
29 import java.util.ArrayList;
30 import java.util.List;
31
32 import javax.swing.JDialog;
33 import javax.swing.JMenu;
34 import javax.swing.JMenuBar;
35 import javax.swing.JMenuItem;
36 import javax.swing.JPanel;
37 import javax.swing.UIManager;
38 import javax.swing.WindowConstants;
39
40 import org.forester.util.ForesterUtil;
41
42 import com.approximatrix.charting.coordsystem.BoxCoordSystem;
43 import com.approximatrix.charting.model.MultiScatterDataModel;
44 import com.approximatrix.charting.render.MultiScatterChartRenderer;
45 import com.approximatrix.charting.swing.ChartPanel;
46
47 public final class Chart extends JDialog implements ActionListener {
48
49     private static final long   serialVersionUID = -5292420246132943515L;
50     private ChartPanel          _chart_panel     = null;
51     private final JMenuItem     _m_exit          = new JMenuItem();
52     private List<MsaProperties> _msa_props;
53
54     private Chart( final List<MsaProperties> msa_props ) {
55         super();
56         _msa_props = msa_props;
57         setTitle( "msa compactor" );
58         setSize( 500, 400 );
59         setResizable( true );
60         final JPanel content_pane = new JPanel();
61         content_pane.setLayout( new BorderLayout() );
62         setContentPane( content_pane );
63         final JMenuBar menu_bar = new JMenuBar();
64         final JMenu file_menu = new JMenu();
65         file_menu.setText( "File" );
66         _m_exit.setText( "Exit" );
67         file_menu.add( _m_exit );
68         menu_bar.add( file_menu );
69         setJMenuBar( menu_bar );
70         setDefaultCloseOperation( WindowConstants.DISPOSE_ON_CLOSE );
71         _m_exit.addActionListener( this );
72         content_pane.add( obtainChartPanel(), BorderLayout.CENTER );
73     }
74
75     @Override
76     public void actionPerformed( final java.awt.event.ActionEvent e ) {
77         if ( e.getSource() == _m_exit ) {
78             dispose();
79         }
80     }
81
82     private ChartPanel obtainChartPanel() {
83         if ( _chart_panel == null ) {
84             final MultiScatterDataModel model = new MultiScatterDataModel();
85             if ( ( _msa_props == null ) || _msa_props.isEmpty() ) {
86                 _msa_props = new ArrayList<MsaProperties>();
87                 final MsaProperties p0 = new MsaProperties( 10, 200, 0.5, 0.1 );
88                 final MsaProperties p1 = new MsaProperties( 9, 190, 0.49, 0.2 );
89                 final MsaProperties p2 = new MsaProperties( 8, 150, 0.2, 0.3 );
90                 final MsaProperties p3 = new MsaProperties( 7, 145, 0.2, 0.4 );
91                 _msa_props.add( p0 );
92                 _msa_props.add( p1 );
93                 _msa_props.add( p2 );
94                 _msa_props.add( p3 );
95             }
96             final double[][] seqs_length = new double[ _msa_props.size() ][ 2 ];
97             for( int i = 0; i < _msa_props.size(); ++i ) {
98                 seqs_length[ i ][ 0 ] = _msa_props.get( i ).getNumberOfSequences();
99                 seqs_length[ i ][ 1 ] = _msa_props.get( i ).getLength();
100             }
101             model.addData( seqs_length, "Length" );
102             model.setSeriesLine( "Series " + "Length", true );
103             model.setSeriesMarker( "Series " + "Length", false );
104             final double[][] seqs_gaps = new double[ _msa_props.size() ][ 2 ];
105             for( int i = 0; i < _msa_props.size(); ++i ) {
106                 seqs_gaps[ i ][ 0 ] = _msa_props.get( i ).getNumberOfSequences();
107                 seqs_gaps[ i ][ 1 ] = ForesterUtil.roundToInt( _msa_props.get( i ).getGapRatio() * 100 );
108             }
109             model.addData( seqs_gaps, "Gaps" );
110             model.setSeriesLine( "Series " + "Gaps", true );
111             model.setSeriesMarker( "Series " + "Gaps", false );
112             final double[][] seqs_identity = new double[ _msa_props.size() ][ 2 ];
113             for( int i = 0; i < _msa_props.size(); ++i ) {
114                 seqs_identity[ i ][ 0 ] = _msa_props.get( i ).getNumberOfSequences();
115                 seqs_identity[ i ][ 1 ] = ForesterUtil.roundToInt( _msa_props.get( i ).getAverageIdentityRatio() * 100 );
116             }
117             model.addData( seqs_identity, "Id" );
118             model.setSeriesLine( "Series " + "Id", true );
119             model.setSeriesMarker( "Series " + "Id", false );
120             final BoxCoordSystem coord = new BoxCoordSystem( model );
121             coord.setUnitFont( coord.getUnitFont().deriveFont( 20.0f ) );
122             coord.setXAxisUnit( "Number of Sequences" );
123             coord.setPaintGrid( true );
124             coord.setYAxisUnit( "MSA Length" );
125             _chart_panel = new ChartPanel( model, "msa compactor" );
126             _chart_panel.setCoordSystem( coord );
127             final MultiScatterChartRenderer renderer = new MultiScatterChartRenderer( coord, model );
128             renderer.setAllowBuffer( false );
129             _chart_panel.addChartRenderer( renderer, 0 );
130         }
131         return _chart_panel;
132     }
133
134     public static void display( final List<MsaProperties> msa_props ) {
135         try {
136             UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName() );
137         }
138         catch ( final Exception e ) {
139             e.printStackTrace();
140         }
141         final Chart chart = new Chart( msa_props );
142         chart.setVisible( true );
143     }
144
145     public static void main( final String[] args ) {
146         try {
147             UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName() );
148         }
149         catch ( final Exception e ) {
150             e.printStackTrace();
151         }
152         final Chart temp = new Chart( null );
153         temp.setVisible( true );
154     }
155 }