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     private final int           _initial_number_of_seqs;
54
55     private Chart( final List<MsaProperties> msa_props, final int initial_number_of_seqs ) {
56         super();
57         _msa_props = msa_props;
58         _initial_number_of_seqs = initial_number_of_seqs;
59         setTitle( "msa compactor" );
60         setSize( 500, 400 );
61         setResizable( true );
62         final JPanel content_pane = new JPanel();
63         content_pane.setLayout( new BorderLayout() );
64         setContentPane( content_pane );
65         final JMenuBar menu_bar = new JMenuBar();
66         final JMenu file_menu = new JMenu();
67         file_menu.setText( "File" );
68         _m_exit.setText( "Exit" );
69         file_menu.add( _m_exit );
70         menu_bar.add( file_menu );
71         setJMenuBar( menu_bar );
72         setDefaultCloseOperation( WindowConstants.DISPOSE_ON_CLOSE );
73         _m_exit.addActionListener( this );
74         content_pane.add( obtainChartPanel(), BorderLayout.CENTER );
75     }
76
77     @Override
78     public void actionPerformed( final java.awt.event.ActionEvent e ) {
79         if ( e.getSource() == _m_exit ) {
80             dispose();
81         }
82     }
83
84     private ChartPanel obtainChartPanel() {
85         if ( _chart_panel == null ) {
86             final MultiScatterDataModel model = new MultiScatterDataModel();
87             if ( ( _msa_props == null ) || _msa_props.isEmpty() ) {
88                 _msa_props = new ArrayList<MsaProperties>();
89                 final MsaProperties p0 = new MsaProperties( 10, 200, 0.5, 0.1 );
90                 final MsaProperties p1 = new MsaProperties( 9, 190, 0.49, 0.2 );
91                 final MsaProperties p2 = new MsaProperties( 8, 150, 0.2, 0.3 );
92                 final MsaProperties p3 = new MsaProperties( 7, 145, 0.2, 0.4 );
93                 _msa_props.add( p0 );
94                 _msa_props.add( p1 );
95                 _msa_props.add( p2 );
96                 _msa_props.add( p3 );
97             }
98             final double[][] seqs_length = new double[ _msa_props.size() ][ 2 ];
99             for( int i = 0; i < _msa_props.size(); ++i ) {
100                 seqs_length[ i ][ 0 ] = _initial_number_of_seqs - _msa_props.get( i ).getNumberOfSequences();
101                 seqs_length[ i ][ 1 ] = _msa_props.get( i ).getLength();
102             }
103             model.addData( seqs_length, "Length" );
104             model.setSeriesLine( "Series " + "Length", true );
105             model.setSeriesMarker( "Series " + "Length", false );
106             final double[][] seqs_gaps = new double[ _msa_props.size() ][ 2 ];
107             for( int i = 0; i < _msa_props.size(); ++i ) {
108                 seqs_gaps[ i ][ 0 ] = _initial_number_of_seqs - _msa_props.get( i ).getNumberOfSequences();
109                 seqs_gaps[ i ][ 1 ] = ForesterUtil.roundToInt( _msa_props.get( i ).getGapRatio() * 100 );
110             }
111             model.addData( seqs_gaps, "Gaps" );
112             model.setSeriesLine( "Series " + "Gaps", true );
113             model.setSeriesMarker( "Series " + "Gaps", false );
114             final double[][] seqs_identity = new double[ _msa_props.size() ][ 2 ];
115             for( int i = 0; i < _msa_props.size(); ++i ) {
116                 seqs_identity[ i ][ 0 ] = _initial_number_of_seqs - _msa_props.get( i ).getNumberOfSequences();
117                 seqs_identity[ i ][ 1 ] = ForesterUtil.roundToInt( _msa_props.get( i ).getAverageIdentityRatio() * 100 );
118             }
119             model.addData( seqs_identity, "Id" );
120             model.setSeriesLine( "Series " + "Id", true );
121             model.setSeriesMarker( "Series " + "Id", false );
122             final BoxCoordSystem coord = new BoxCoordSystem( model );
123             coord.setUnitFont( coord.getUnitFont().deriveFont( 20.0f ) );
124             coord.setXAxisUnit( "Number of Sequences" );
125             coord.setPaintGrid( true );
126             coord.setYAxisUnit( "MSA Length" );
127             _chart_panel = new ChartPanel( model, "msa compactor" );
128             _chart_panel.setCoordSystem( coord );
129             final MultiScatterChartRenderer renderer = new MultiScatterChartRenderer( coord, model );
130             renderer.setAllowBuffer( false );
131             _chart_panel.addChartRenderer( renderer, 0 );
132         }
133         return _chart_panel;
134     }
135
136     public static void display( final List<MsaProperties> msa_props, final int initial_number_of_seqs ) {
137         try {
138             UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName() );
139         }
140         catch ( final Exception e ) {
141             e.printStackTrace();
142         }
143         final Chart chart = new Chart( msa_props, initial_number_of_seqs );
144         chart.setVisible( true );
145     }
146
147     public static void main( final String[] args ) {
148         try {
149             UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName() );
150         }
151         catch ( final Exception e ) {
152             e.printStackTrace();
153         }
154         final Chart temp = new Chart( null, 0 );
155         temp.setVisible( true );
156     }
157 }