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