Skip to content
Snippets Groups Projects
BasePlotGroup.java 10.5 KiB
Newer Older
tjc's avatar
tjc committed
/* BasePlotGroup.java
 *
 * created: Tue Dec 15 1998
 *
 * This file is part of Artemis
 *
 * Copyright (C) 1998-2008  Genome Research Limited
tjc's avatar
tjc committed
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Header: //tmp/pathsoft/artemis/uk/ac/sanger/artemis/components/BasePlotGroup.java,v 1.9 2008-06-16 12:11:01 tjc Exp $
tjc's avatar
tjc committed
 */

package uk.ac.sanger.artemis.components;

import uk.ac.sanger.artemis.*;
import uk.ac.sanger.artemis.sequence.*;
import uk.ac.sanger.artemis.plot.*;

import java.awt.*;
import java.util.Vector;

tjc's avatar
tjc committed
import javax.swing.JComponent;
import javax.swing.JPanel;
tjc's avatar
tjc committed

/**
 *  This is a super-component containing several BasePlot components, each of
 *  which can toggled off and on.
 *
 *  @author Kim Rutherford
 *  @version $Id: BasePlotGroup.java,v 1.9 2008-06-16 12:11:01 tjc Exp $
tjc's avatar
tjc committed
 **/

public class BasePlotGroup extends JPanel
tjc's avatar
tjc committed
                           implements DisplayAdjustmentListener 
{
  /**
   *  The EntryGroup that contains the sequence that this JComponent is
   *  displaying.
   **/
  private final EntryGroup entry_group;

  /**
   *  This array contains the Algorithm objects of the BasePlot components in
   *  this BasePlotGroup, as set by the constructor.
   **/
  private final Vector plot_value_producers = new Vector ();

  /**
   *  The layout object used by this component.
   **/
  private final GridBagLayout gridbag = new GridBagLayout();

  /**
   *  The constraints object used by this component.
   **/
  private final GridBagConstraints c = new GridBagConstraints();

  /**
   *  The Selection that was passed to the constructor.
   **/
  private Selection selection;

  /**
   *  The GotoEventSource that was passed to the constructor.
   **/
  private GotoEventSource goto_event_source;
  
tjc's avatar
tjc committed
  /**
   *  Create a new BasePlotGroup component for the given EntryGroup.
   *  @param selection Used to set and display the current selection in the
   *    BasePlot.
   *  @param goto_event_source The object the we will call gotoBase () on.
   *    This allows the user to double click on a base in a BasePlot and have
   *    the FeatureDisplay follow.
   **/
tjc's avatar
tjc committed
  public BasePlotGroup(final EntryGroup entry_group,
                       final Component owning_component,
                       final Selection selection,
                       final GotoEventSource goto_event_source) 
  {
tjc's avatar
tjc committed
    this.entry_group = entry_group;
    this.selection = selection;
    this.goto_event_source = goto_event_source;

    final Strand forward_strand =
tjc's avatar
tjc committed
      entry_group.getBases().getForwardStrand();
tjc's avatar
tjc committed

tjc's avatar
tjc committed
    // obtain the reverse complement
tjc's avatar
tjc committed
    final Strand reverse_strand =
tjc's avatar
tjc committed
      entry_group.getBases().getReverseStrand();
tjc's avatar
tjc committed

tjc's avatar
tjc committed
    setLayout(gridbag);
    setBackground(Color.WHITE);
tjc's avatar
tjc committed

    c.fill = GridBagConstraints.BOTH;
tjc's avatar
tjc committed
    c.anchor = GridBagConstraints.NORTH;
    c.gridwidth = GridBagConstraints.REMAINDER;
    c.weightx = 1;
    c.weighty = 1;
    c.insets = new Insets(0,0,2,0);
tjc's avatar
tjc committed

    addAlgorithm(new GCWindowAlgorithm(forward_strand));
    addAlgorithm(new GCSDWindowAlgorithm(forward_strand));
    addAlgorithm(new AGWindowAlgorithm(forward_strand));
    addAlgorithm(new GCFrameAlgorithm(forward_strand));
    addAlgorithm(new GCFrameAlgorithm(reverse_strand));
    addAlgorithm(new Codon12CorrelationAlgorithm(forward_strand));
    addAlgorithm(new Codon12CorrelationAlgorithm(reverse_strand));
    addAlgorithm(new GCDeviationAlgorithm(forward_strand));
    addAlgorithm(new ATDeviationAlgorithm(forward_strand));
    addAlgorithm(new KarlinSigAlgorithm(forward_strand));
tjc's avatar
tjc committed
    

    //CumulativeATSkewAlgorithm
    addAlgorithm(new CumulativeATSkewAlgorithm(forward_strand));
    addAlgorithm(new CumulativeGCSkewAlgorithm(forward_strand));

    //Positional Asymmetry
    addAlgorithm(new PositionalAsymmetryAlgorithm(forward_strand));

    //Informational Entropy
    addAlgorithm(new EntropyAlgorithm(forward_strand));

    //Scaled Chi
    addAlgorithm(new ScaledChiAlgorithm(forward_strand));
    addAlgorithm(new ScaledChiAlgorithm(reverse_strand));

    //Corrected Scaled Chi Square
    addAlgorithm(new CSCSAlgorithm(forward_strand));
    addAlgorithm(new CSCSAlgorithm(reverse_strand));

    //Mutational Response Index
    addAlgorithm(new MRIAlgorithm(forward_strand));
    addAlgorithm(new MRIAlgorithm(reverse_strand));

    //Effective Codon Number
    addAlgorithm(new NcAlgorithm(forward_strand));
    addAlgorithm(new NcAlgorithm(reverse_strand));

    //Intrinsic Codon Deviation Index
    addAlgorithm(new ICDIAlgorithm(forward_strand));
    addAlgorithm(new ICDIAlgorithm(reverse_strand));
tjc's avatar
tjc committed
  }

tjc's avatar
tjc committed
 
  /**
  * 
  * Routine for printing graphs in artemis
  *
  */
  protected void printComponent(Graphics g)
  {
    final Component[] children = getComponents();
    for(int i = 0 ; i<children.length ; ++i)
      if(children[i] instanceof BasePlot)
      {
        BasePlot bp = (BasePlot)children[i];
        if(!bp.isVisible())
          continue;
tjc's avatar
tjc committed
        bp.paintComponent(g);
tjc's avatar
tjc committed
        g.translate(0,bp.getHeight());
      }
  }


tjc's avatar
tjc committed
  /**
  *
  * Return the number of visible BasePlot objects
  *
  */
  protected int getNumberBasePlots()
  {
    final Component[] children = getComponents();
    int num = 0;

    for(int i = 0 ; i<children.length ; ++i)
      if(children[i] instanceof BasePlot)
      {
        BasePlot bp = (BasePlot)children[i];
        if(bp.isVisible())
          num++;
      }
    return num;
  }

tjc's avatar
tjc committed
  /**
   *  Implementation of the DisplayAdjustmentListener interface.  Invoked when
   *  a component (FeatureDisplay) scrolls or changes the scale.  Sends the
   *  event to all the BasePlot components in this BasePlotGroup.
   **/
tjc's avatar
tjc committed
  public void displayAdjustmentValueChanged(DisplayAdjustmentEvent event) 
  {
    final Component[] children = getComponents();

    for(int i = 0 ; i<children.length ; ++i)
    {
      if(children[i] instanceof BasePlot) 
        ((BasePlot)children[i]).displayAdjustmentValueChanged(event);
tjc's avatar
tjc committed
    }
  }

  /**
   *  Add a new BasePlot component for the given algorithm.
   *  @return The new BasePlot
   **/
tjc's avatar
tjc committed
  public BasePlot addAlgorithm(final BaseAlgorithm algorithm) 
  {
    plot_value_producers.addElement(algorithm);
tjc's avatar
tjc committed

tjc's avatar
tjc committed
    return makePlot(algorithm, gridbag, c);
tjc's avatar
tjc committed
  }

  /**
   *  Return the first CodonUsageAlgorithm or null if there isn't one in the
   *  group.
   **/
tjc's avatar
tjc committed
  public CodonUsageAlgorithm getCodonUsageAlgorithm() 
  {
    for(int i = 0 ; i < plot_value_producers.size() ; ++i) 
    {
tjc's avatar
tjc committed
      final BaseAlgorithm this_algorithm =
tjc's avatar
tjc committed
        (BaseAlgorithm) plot_value_producers.elementAt(i);
tjc's avatar
tjc committed

tjc's avatar
tjc committed
      if(this_algorithm instanceof CodonUsageAlgorithm) 
tjc's avatar
tjc committed
        return (CodonUsageAlgorithm) this_algorithm;
    }

    return null;
  }

  /**
   *  Return an array containing the Algorithm objects of the BasePlot
   *  components in this BasePlotGroup.
   **/
tjc's avatar
tjc committed
  public BaseAlgorithm[] getPlotAlgorithms() 
  {
    final BaseAlgorithm[] return_array =
      new BaseAlgorithm[plot_value_producers.size ()];
tjc's avatar
tjc committed

tjc's avatar
tjc committed
    for(int i = 0 ; i < plot_value_producers.size () ; ++i) 
    {
tjc's avatar
tjc committed
      final BaseAlgorithm this_algorithm =
        (BaseAlgorithm) plot_value_producers.elementAt (i);
      return_array[i] = this_algorithm;
    }

    return return_array;
  }

  /**
   *  Return true if and only if the BasePlot for the given Algorithm is
   *  visible.
   **/
tjc's avatar
tjc committed
  public boolean basePlotIsVisible(final Algorithm algorithm) 
  {
    final Component base_plot = findPlotByAlgorithm(algorithm);
tjc's avatar
tjc committed
    return base_plot.isVisible ();
  }

  /**
   *  Given an Algorithm, find and set the visibility of the corresponding
   *  BasePlot component.
   **/
tjc's avatar
tjc committed
  public void setVisibleByAlgorithm(final Algorithm algorithm,
                                    final boolean visible) 
  {
    final JComponent base_plot = findPlotByAlgorithm(algorithm);
tjc's avatar
tjc committed

tjc's avatar
tjc committed
    base_plot.setVisible(visible);
tjc's avatar
tjc committed
    if (getParent () != null) {
      // XXX change to revalidate().
      getParent ().validate ();
    }
  }

  /**
   *  Find the BasePlot component in this BasePlotGroup object that is
   *  plotting the given Algorithm or null if no such BasePlot object exists.
   **/
tjc's avatar
tjc committed
  private JComponent findPlotByAlgorithm(final Algorithm algorithm) 
  {
    final Component[] children = getComponents();

    for(int i = 0 ; i < children.length ; ++i) 
    {
      if(children[i] instanceof BasePlot) 
      {
tjc's avatar
tjc committed
        final Algorithm component_algorithm =
tjc's avatar
tjc committed
                           ((BasePlot)children[i]).getAlgorithm ();
        if(component_algorithm == algorithm)
tjc's avatar
tjc committed
          return (JComponent)children[i];
      }
    }

    return null;
  }

tjc's avatar
tjc committed

tjc's avatar
tjc committed
  /**
   *  Create a Plot component for the given Algorithm and then make a button
   *  for it so that it can be shown and hidden.  Note that button making is
   *  currently disabled
   *  @param algorithm The Algorithm to create a Plot of.
   *  @param gridbag The GridBagLayout to use to lay out the Plot components.
   *  @param constraints The GridBagConstraints object to use to lay out the
   *    Plot components.
   *  @return The new BasePlot
   **/
tjc's avatar
tjc committed
  private BasePlot makePlot(BaseAlgorithm algorithm,
                            GridBagLayout gridbag,
                            GridBagConstraints constraints) 
  {
tjc's avatar
tjc committed
    final BasePlot new_base_plot =
tjc's avatar
tjc committed
      new BasePlot(algorithm, getSelection(), getGotoEventSource(), entry_group);
tjc's avatar
tjc committed

tjc's avatar
tjc committed
    gridbag.setConstraints(new_base_plot, constraints);
    add(new_base_plot);
    new_base_plot.setVisible(false);
tjc's avatar
tjc committed

tjc's avatar
tjc committed
    getSelection().addSelectionChangeListener(new_base_plot);
tjc's avatar
tjc committed

    if (getParent () != null) {
      // XXX change to revalidate().
      getParent ().validate ();
    }

    return new_base_plot;
  }

  /**
   *  Return the Selection object that was passed to the constructor.
   **/
tjc's avatar
tjc committed
  private Selection getSelection() 
  {
tjc's avatar
tjc committed
    return selection;
  }

  /**
   *  Return the EntryGroup object that was passed to the constructor.
   **/
tjc's avatar
tjc committed
  private EntryGroup getEntryGroup() 
  {
tjc's avatar
tjc committed
    return entry_group;
  }

  /**
   *  Return the GotoEventSource object that was passed to the constructor.
   **/
tjc's avatar
tjc committed
  private GotoEventSource getGotoEventSource()
  {
tjc's avatar
tjc committed
    return goto_event_source;
  }
}