Skip to content
Snippets Groups Projects
SelectionInfoDisplay.java 13.1 KiB
Newer Older
  • Learn to ignore specific revisions
  • tjc's avatar
    tjc committed
    /* SelectionInfoDisplay.java
     *
     * created: Tue Dec 15 1998
     *
     * This file is part of Artemis
     *
     * Copyright (C) 1998,1999,2000,2002  Genome Research Limited
     *
     * 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.
     *
    
    tjc's avatar
    tjc committed
     * $Header: //tmp/pathsoft/artemis/uk/ac/sanger/artemis/components/SelectionInfoDisplay.java,v 1.7 2005-01-06 11:21:06 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.io.StreamQualifier;
    import uk.ac.sanger.artemis.io.QualifierInfo;
    import uk.ac.sanger.artemis.io.EntryInformation;
    import uk.ac.sanger.artemis.io.Qualifier;
    import uk.ac.sanger.artemis.io.QualifierVector;
    import uk.ac.sanger.artemis.io.Range;
    import uk.ac.sanger.artemis.util.OutOfRangeException;
    
    import java.awt.*;
    import java.awt.event.*;
    import java.util.Vector;
    
    import javax.swing.*;
    
    /**
     *  This class displays information about the selection in a Label.
     *
     *  @author Kim Rutherford
    
    tjc's avatar
    tjc committed
     *  @version $Id: SelectionInfoDisplay.java,v 1.7 2005-01-06 11:21:06 tjc Exp $
    
    tjc's avatar
    tjc committed
     **/
    public class SelectionInfoDisplay extends CanvasPanel
    
    tjc's avatar
    tjc committed
        implements SelectionChangeListener 
    {
    
    tjc's avatar
    tjc committed
      /**
       *  Create a new SelectionInfoDisplay component.
       *  @param entry_group The EntryGroup that this component will display.
       *  @param selection The Selection that this component will display.
       **/
    
    tjc's avatar
    tjc committed
      public SelectionInfoDisplay(final EntryGroup entry_group,
                                  final Selection selection) 
      {
    
    tjc's avatar
    tjc committed
        this.entry_group = entry_group;
        this.selection = selection;
    
    
    tjc's avatar
    tjc committed
        getSelection().addSelectionChangeListener(this);
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
        addComponentListener(new ComponentAdapter() 
        {
          public void componentResized(ComponentEvent e) 
          {
            setSize(getSize().width,
    
    tjc's avatar
    tjc committed
                    getFontHeight()+1);
    
    tjc's avatar
    tjc committed
            repaint();
    
    tjc's avatar
    tjc committed
          }
    
    tjc's avatar
    tjc committed
          public void componentShown(ComponentEvent e) 
    
    tjc's avatar
    tjc committed
          {
    
    tjc's avatar
    tjc committed
            setSize(getSize().width,
                    getFontHeight()+1);
    
    tjc's avatar
    tjc committed
            repaint();
    
    tjc's avatar
    tjc committed
          }
        });
    
    
    tjc's avatar
    tjc committed
        setBackground(new Color(230,230,230));
    
    tjc's avatar
    tjc committed
        setSize(80, getFontHeight());
    
    tjc's avatar
    tjc committed
      }
    
      /**
       *
       **/
    
    tjc's avatar
    tjc committed
      public Dimension getPreferredSize() 
      {
        return new Dimension(10, getFontHeight());
    
    tjc's avatar
    tjc committed
      }
    
      /**
       *
       **/
    
    tjc's avatar
    tjc committed
      public Dimension getMinimumSize() 
      {
        return new Dimension(10, getFontHeight());
    
    tjc's avatar
    tjc committed
      }
    
      /**
       *  Implementation of the SelectionChangeListener interface.  We listen to
       *  SelectionChange events so that we can update the list to reflect the
       *  current selection.
       **/
    
    tjc's avatar
    tjc committed
      public void selectionChanged(SelectionChangeEvent event) 
      {
    
    tjc's avatar
    tjc committed
        repaint();
    
    tjc's avatar
    tjc committed
      }
    
      /**
       *  Draw the label.
       **/
    
    tjc's avatar
    tjc committed
      public void paintComponent(final Graphics g) 
    
    tjc's avatar
    tjc committed
      {
        super.paintComponent(g);
        if(!isVisible ()) 
    
    tjc's avatar
    tjc committed
          return;
    
    
    tjc's avatar
    tjc committed
        final FeatureVector features = getSelection().getAllFeatures();
        final StringBuffer new_text  = new StringBuffer();
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
        new_text.append(markerRangeText(getSelection(), entry_group));
    
    tjc's avatar
    tjc committed
    
        int base_total = 0;
        int aa_total = 0;
        boolean saw_a_non_cds = false;
    
    
    tjc's avatar
    tjc committed
        if(features.size () > 0) 
        {
    
    tjc's avatar
    tjc committed
          final StringBuffer feature_names = new StringBuffer ();
    
    
    tjc's avatar
    tjc committed
          if(features.size () < 100) 
          {
            if(features.size () > 1)
    
    tjc's avatar
    tjc committed
              feature_names.append ("  (");
    
            // show up to 10 features
    
    tjc's avatar
    tjc committed
            for(int i = 0 ; i < features.size () ; ++i) 
            {
    
    tjc's avatar
    tjc committed
              final Feature current_feature = features.elementAt (i);
    
              base_total += current_feature.getBaseCount ();
    
    tjc's avatar
    tjc committed
              aa_total   += current_feature.getAACount ();
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
              if(!current_feature.getKey().equals ("CDS")) 
    
    tjc's avatar
    tjc committed
                saw_a_non_cds = true;
    
    
    tjc's avatar
    tjc committed
              if(i < 10) 
              {
                if(i != 0) 
    
    tjc's avatar
    tjc committed
                  feature_names.append(' ');
    
    tjc's avatar
    tjc committed
                
    
    tjc's avatar
    tjc committed
                feature_names.append(current_feature.getIDString ());
    
    tjc's avatar
    tjc committed
              }
            }
    
    
    tjc's avatar
    tjc committed
            if(features.size() > 10) 
    
    tjc's avatar
    tjc committed
              feature_names.append ("...");
    
    
    tjc's avatar
    tjc committed
            if(features.size() == 1) 
    
    tjc's avatar
    tjc committed
            {
    
    tjc's avatar
    tjc committed
              // only one feature so append some qualifiers
    
    tjc's avatar
    tjc committed
              feature_names.append("  (");
              feature_names.append(getQualifierString(features.elementAt (0)));
    
    tjc's avatar
    tjc committed
            }
    
    tjc's avatar
    tjc committed
            feature_names.append(")");
    
    tjc's avatar
    tjc committed
          }
    
    
    tjc's avatar
    tjc committed
          if(features.size() == 1) 
    
    tjc's avatar
    tjc committed
            new_text.append ("Selected feature:  ");
    
    tjc's avatar
    tjc committed
          else 
    
    tjc's avatar
    tjc committed
            new_text.append (features.size () + " selected features  ");
    
    
    tjc's avatar
    tjc committed
          if(features.size() < 100) 
    
    tjc's avatar
    tjc committed
          {
    
    tjc's avatar
    tjc committed
            // show a count of the number of bases and amino acids in the selected
            // feature
    
    tjc's avatar
    tjc committed
            if(features.size() > 1) 
    
    tjc's avatar
    tjc committed
            {
    
    tjc's avatar
    tjc committed
              new_text.append ("total bases " + base_total);
    
    
    tjc's avatar
    tjc committed
              if(!saw_a_non_cds) 
    
    tjc's avatar
    tjc committed
                new_text.append ("  total amino acids " + aa_total);
    
    tjc's avatar
    tjc committed
            }
            else
            {
    
    tjc's avatar
    tjc committed
              if(features.size() == 1) 
    
    tjc's avatar
    tjc committed
              {
    
    tjc's avatar
    tjc committed
                new_text.append ("bases " + base_total);
    
    tjc's avatar
    tjc committed
                if(!saw_a_non_cds) 
    
    tjc's avatar
    tjc committed
                  new_text.append ("  amino acids " + aa_total);
              }
            }
          }
    
    
    tjc's avatar
    tjc committed
          new_text.append("  ");
          new_text.append(feature_names.toString());
    
    tjc's avatar
    tjc committed
        }
    
    
    
    tjc's avatar
    tjc committed
        String text = new_text.toString();
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
        if(text.length () > 0) 
        {
          if (text.length () > 150) 
          {
    
    tjc's avatar
    tjc committed
            // call substring () just to keep the String a sensible length
            text = text.substring (0, 150);
          }
    
    tjc's avatar
    tjc committed
        } 
        else 
    
    tjc's avatar
    tjc committed
          text = "Nothing selected";
    
    
    tjc's avatar
    tjc committed
    //  g.setColor(new Color (230, 230, 230));
    //  g.fillRect(0, 0, getCanvasWidth(), getCanvasHeight());
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
        g.setColor(Color.black);
        g.drawString(text, 2, getFontMaxAscent() + 1);
    
    tjc's avatar
    tjc committed
      }
    
      /**
       *  Return a String containing the qualifiers (except the /note) of given
       *  feature.
       **/
    
    tjc's avatar
    tjc committed
      private String getQualifierString(final Feature feature) 
      {
        final QualifierVector qualifiers = feature.getQualifiers();
    
    tjc's avatar
    tjc committed
    
        // if we see a /note or /fasta_file it gets saved for later
    
    tjc's avatar
    tjc committed
        final Vector saved_qualifiers = new Vector();
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
        final StringBuffer string_buffer = new StringBuffer();
    
    tjc's avatar
    tjc committed
    
        final EntryInformation entry_information =
    
    tjc's avatar
    tjc committed
          feature.getEntry().getEntryInformation();
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
        for (int i = 0 ; i < qualifiers.size () ; ++i) 
        {
    
    tjc's avatar
    tjc committed
          final Qualifier this_qualifier = (Qualifier)qualifiers.elementAt (i);
    
    tjc's avatar
    tjc committed
    
          if (this_qualifier.getName ().equals ("note") ||
    
    tjc's avatar
    tjc committed
              this_qualifier.getName ().endsWith ("_file")) 
          {
    
    tjc's avatar
    tjc committed
            // save the qualifier and put it last
            saved_qualifiers.addElement (this_qualifier);
            continue;
          }
    
    
    tjc's avatar
    tjc committed
          if (string_buffer.length () > 0)
          {
    
    tjc's avatar
    tjc committed
            // put spaces between the qualifiers
            string_buffer.append (' ');
          }
    
          final QualifierInfo qualifier_info =
            entry_information.getQualifierInfo (this_qualifier.getName ());
    
          string_buffer.append (StreamQualifier.toString (qualifier_info,
                                                          this_qualifier));
        }
    
    
    tjc's avatar
    tjc committed
        for(int i = 0 ; i < saved_qualifiers.size () ; ++i) 
        {
          if(string_buffer.length () > 0) 
          {
    
    tjc's avatar
    tjc committed
            // put spaces between the qualifiers
            string_buffer.append (' ');
          }
    
          final Qualifier this_qualifier =
            (Qualifier) saved_qualifiers.elementAt (i);
    
          final QualifierInfo qualifier_info =
            entry_information.getQualifierInfo (this_qualifier.getName ());
    
          string_buffer.append (StreamQualifier.toString (qualifier_info,
                                                          this_qualifier));
        }
    
        return string_buffer.toString ();
      }
    
      /**
       *  If the selection contains a MarkerRange (a range of bases) then return a
       *  String in this form: "Selected Bases on forward strand: <start>..<end> ",
       *  otherwise return "".  This method is also used by the SelectionViewer
       *  class.
       *  @param current_selection The selection to summarize.
       *  @param entry_group The EntryGroup that the selection refers to.
       **/
    
    tjc's avatar
    tjc committed
      static String markerRangeText(final Selection current_selection,
                                    final EntryGroup entry_group) 
      {
    
    tjc's avatar
    tjc committed
        final MarkerRange marker_range = current_selection.getMarkerRange ();
    
    
    tjc's avatar
    tjc committed
        if (marker_range == null) 
    
    tjc's avatar
    tjc committed
          return "";
    
    tjc's avatar
    tjc committed
        else
        {
    
    tjc's avatar
    tjc committed
          final StringBuffer buffer = new StringBuffer ();
    
          final int start_pos = marker_range.getStart ().getPosition ();
          final int end_pos = marker_range.getEnd ().getPosition ();
    
    
    tjc's avatar
    tjc committed
          if(marker_range.getStrand ().isForwardStrand ()) 
          {
            if(start_pos == end_pos) 
            {
    
    tjc's avatar
    tjc committed
              buffer.append ("One selected base on forward strand: " +
                             start_pos + "  ");
    
    tjc's avatar
    tjc committed
            }
            else 
            {
    
    tjc's avatar
    tjc committed
              buffer.append ((end_pos - start_pos + 1) +
                             " selected bases on forward strand: " +
                             start_pos + ".." + end_pos + " ");
            }
    
    tjc's avatar
    tjc committed
          }
          else 
          {
            if(start_pos == end_pos) 
            {
    
    tjc's avatar
    tjc committed
              buffer.append ("One selected base on reverse strand: " +
                             marker_range.getStart ().getPosition () +
                             "  = complement (" +
                             marker_range.getEnd ().getRawPosition () + ") ");
    
    tjc's avatar
    tjc committed
            }
            else 
            {
    
    tjc's avatar
    tjc committed
              buffer.append ((end_pos - start_pos + 1) +
                             " selected bases on reverse strand: " +
                             marker_range.getStart ().getPosition () + ".." +
                             marker_range.getEnd ().getPosition () +
                             "  = complement (" +
                             marker_range.getEnd ().getRawPosition () + ".." +
                             marker_range.getStart ().getRawPosition () + ") ");
            }
          }
    
    
    tjc's avatar
    tjc committed
          if(marker_range.getCount () >= 3) 
          {
    
    tjc's avatar
    tjc committed
            // check if this codon is in a feature
    
            final Range raw_range = marker_range.getRawRange ();
    
            final FeatureVector features;
    
    
    tjc's avatar
    tjc committed
            try 
            {
    
    tjc's avatar
    tjc committed
              features = entry_group.getFeaturesInRange (raw_range);
    
    tjc's avatar
    tjc committed
            } 
            catch (OutOfRangeException e) 
            {
    
    tjc's avatar
    tjc committed
              return "illegal marker range";
            }
    
    
    tjc's avatar
    tjc committed
            for(int i = 0 ; i < features.size () ; ++i)
            {
    
    tjc's avatar
    tjc committed
              final Feature this_feature = features.elementAt (i);
    
    
    tjc's avatar
    tjc committed
              if(!this_feature.isProteinFeature ()) 
              {
    
    tjc's avatar
    tjc committed
                // only display protein features
                continue;
              }
    
    
    tjc's avatar
    tjc committed
              if(marker_range.isForwardMarker () !=
                 this_feature.isForwardFeature ()) 
              {
    
    tjc's avatar
    tjc committed
                // only display feature positions in features on the same strand
                // as the selected bases
                continue;
              }
    
              final String this_feature_id = this_feature.getIDString ();
    
              final Marker start_marker = marker_range.getStart ();
    
              final Marker end_marker = marker_range.getEnd ();
    
              final int start_position_in_feature =
                this_feature.getFeaturePositionFromMarker (start_marker);
    
              final int start_codon_position =
                start_position_in_feature - this_feature.getCodonStart () + 1;
    
              String start_string = null;
    
    
    tjc's avatar
    tjc committed
              if(start_position_in_feature != -1) 
              {
                if (start_codon_position % 3 == 0) 
                {
    
    tjc's avatar
    tjc committed
                  start_string = "codon " + (start_codon_position / 3 + 1);
                }
              }
    
              String end_string = null;
    
              final int end_position_in_feature =
                this_feature.getFeaturePositionFromMarker (end_marker);
    
              final int end_codon_position =
                end_position_in_feature - this_feature.getCodonStart () - 1;
    
    
    tjc's avatar
    tjc committed
              if (end_position_in_feature != -1) 
              {
    
    tjc's avatar
    tjc committed
                if (start_codon_position != end_codon_position &&
    
    tjc's avatar
    tjc committed
                    end_codon_position % 3 == 0)
                {
    
    tjc's avatar
    tjc committed
                  end_string = "codon " + (end_codon_position / 3 + 1);
                }
              }
    
    
    tjc's avatar
    tjc committed
              if(!(start_string == null && end_string == null)) 
              {
    
    tjc's avatar
    tjc committed
                buffer.append (" (");
    
    
    tjc's avatar
    tjc committed
                if(start_string != null) 
    
    tjc's avatar
    tjc committed
                  buffer.append (start_string);
    
    
    tjc's avatar
    tjc committed
                if(start_string != null && end_string != null) 
    
    tjc's avatar
    tjc committed
                  buffer.append (" to ");
    
    
    tjc's avatar
    tjc committed
                if(end_string != null) 
    
    tjc's avatar
    tjc committed
                  buffer.append (end_string);
    
    
    tjc's avatar
    tjc committed
                buffer.append(" in feature " + this_feature.getIDString () +
                              ") ");
    
    tjc's avatar
    tjc committed
              }
            }
          }
    
    
    tjc's avatar
    tjc committed
          return buffer.append(" ").toString();
    
    tjc's avatar
    tjc committed
        }
      }
    
      /**
       *  Return the Selection reference that was passed to the constructor.
       **/
    
    tjc's avatar
    tjc committed
      private Selection getSelection () 
      {
    
    tjc's avatar
    tjc committed
        return selection;
      }
    
      /**
       *  Return the Selection object that was passed to the constructor.
       **/
    
    tjc's avatar
    tjc committed
      private EntryGroup getEntryGroup () 
      {
    
    tjc's avatar
    tjc committed
        return entry_group;
      }
    
      /**
       *  The reference of the EntryGroup object that was passed to the
       *  constructor.
       **/
      private EntryGroup entry_group;
    
      /**
       *  This is a reference to the Selection object that created this
       *  component.
       **/
      private final Selection selection;
    }