Skip to content
Snippets Groups Projects
GCFrameAlgorithm.java 9.4 KiB
Newer Older
  • Learn to ignore specific revisions
  • tjc's avatar
    tjc committed
    /* GCFrameAlgorithm.java
     *
     * created: Tue Dec 15 1998
     *
     * This file is part of Artemis
     * 
     * Copyright (C) 1998,1999,2000  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/plot/GCFrameAlgorithm.java,v 1.3 2004-12-02 16:52:55 tjc Exp $
    
    tjc's avatar
    tjc committed
     */
    
    package uk.ac.sanger.artemis.plot;
    
    import uk.ac.sanger.artemis.sequence.*;
    import uk.ac.sanger.artemis.util.*;
    import uk.ac.sanger.artemis.io.Range;
    
    
    tjc's avatar
    tjc committed
    import java.awt.*;
    
    
    tjc's avatar
    tjc committed
    /**
     *  Objects of this class have one useful method - getValues (), which takes a
     *  range of bases and returns three floating point numbers, which are the
     *  percent GC content in each frame.  The Strand to use is set in the
     *  constructor.
     *
     *  @author Kim Rutherford
    
    tjc's avatar
    tjc committed
     *  @version $Id: GCFrameAlgorithm.java,v 1.3 2004-12-02 16:52:55 tjc Exp $
    
    tjc's avatar
    tjc committed
     **/
    
    tjc's avatar
    tjc committed
    public class GCFrameAlgorithm extends BaseAlgorithm 
    {
    
    tjc's avatar
    tjc committed
      /**
       *  Create a new GCFrameAlgorithm object.
       *  @param strand The Strand to do the calculation on.
       **/
    
    tjc's avatar
    tjc committed
      public GCFrameAlgorithm(final Strand strand) 
      {
        super(strand, makeName(strand), "gc_frame");
        setScalingFlag(true);
    
    tjc's avatar
    tjc committed
      }
    
      /**
    
    tjc's avatar
    tjc committed
       *  This is used as temporary storage by getValues().
    
    tjc's avatar
    tjc committed
       **/
    
    tjc's avatar
    tjc committed
      private int gc_counts [] = new int [getValueCount()];
    
    tjc's avatar
    tjc committed
    
      /**
       *  Return the percent gc between a pair of bases in each of the three
       *  frames.
       *  @param start The start base (included in the range).
       *  @param end The end base (included in the range).  If the start/end pair
       *    doesn't give a multiple of three bases end is moved down so that it is
       *    a multiple of three.
       *  @param values The three results are returned in this array, hence it
       *    should be three long.  The first value is the gc content of positions
       *    start, start+3, start+6, etc, the second value is start+1, start+4,
       *    etc.
       **/
    
    tjc's avatar
    tjc committed
      public void getValues(int start, int end, final float [] values) 
      {
        if(isRevCompDisplay())
        {
    
    tjc's avatar
    tjc committed
          final int new_end =
    
    tjc's avatar
    tjc committed
            getStrand().getBases().getComplementPosition(start);
    
    tjc's avatar
    tjc committed
          final int new_start =
    
    tjc's avatar
    tjc committed
            getStrand().getBases().getComplementPosition(end);
    
    tjc's avatar
    tjc committed
    
          end = new_end;
          start = new_start;
        }
    
        // add 1 or 2 if necessary to make the range a multiple of 3
    
    tjc's avatar
    tjc committed
        if(getStrand().isForwardStrand())
    
    tjc's avatar
    tjc committed
          end -= (end - start + 1) % 3;
    
    tjc's avatar
    tjc committed
        else
    
    tjc's avatar
    tjc committed
          start += (end - start + 1) % 3;
    
    
    tjc's avatar
    tjc committed
        for(int i = 0; i < getValueCount(); ++i)
    
    tjc's avatar
    tjc committed
          gc_counts[i] = 0;
        
        final String sub_sequence;
    
    
    tjc's avatar
    tjc committed
        try 
        {
          sub_sequence = getStrand().getRawSubSequence(new Range(start, end));
        } 
        catch(OutOfRangeException e) 
        {
          throw new Error("internal error - unexpected exception: " + e);
    
    tjc's avatar
    tjc committed
        }
    
    
    tjc's avatar
    tjc committed
        final int sub_sequence_length = sub_sequence.length();
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
        if(getStrand().isForwardStrand()) 
        {
          for(int i = 0 ; i < sub_sequence_length ; i += 3) 
          {
            for(int frame = 0 ; frame < 3 ; ++frame) 
            {
              final char this_char = sub_sequence.charAt(i + frame);
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
              if(this_char == 'g' || this_char == 'c') 
    
    tjc's avatar
    tjc committed
                ++gc_counts[(frame + start) % 3];
            }
          }
    
    tjc's avatar
    tjc committed
        } 
        else
        {
          final int whole_sequence_length = getStrand().getSequenceLength();
    
    tjc's avatar
    tjc committed
          final int whole_sequence_length_mod3 = whole_sequence_length % 3;
    
    
    tjc's avatar
    tjc committed
          for(int i = 0; i < sub_sequence_length; i += 3)
          {
            for(int frame = 0; frame < 3; ++frame) 
            {
              final char this_char = sub_sequence.charAt(i + frame);
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
              if(this_char == 'g' || this_char == 'c') 
    
    tjc's avatar
    tjc committed
                ++gc_counts[(frame + start + 3 - whole_sequence_length_mod3) % 3];
            }
          }
        }
    
    
    tjc's avatar
    tjc committed
        // multiply by 3 because we are taking every third base
        for(int frame = 0 ; frame < 3 ; ++frame) 
    
    tjc's avatar
    tjc committed
          values[frame] = 1.0F * gc_counts[frame]/sub_sequence_length * 3 * 100;
      }
    
    
    tjc's avatar
    tjc committed
      /**
      *
      *  Override drawLegend() 
      *
      */
      public void drawLegend(Graphics g, int font_height,
                             int font_width, Color[] frameColour)
      {
        final Strand strand = getStrand();
        if(strand.isForwardStrand())
          super.drawLegend(g,font_height,font_width,frameColour);
        else
        {
          Graphics2D g2d = (Graphics2D)g;
    
          FontMetrics fm = g2d.getFontMetrics();
          int lineHgt    = 3 * font_height/4;
          int frame = strand.getSequenceLength() % 3;
    
    //    System.out.println("MOD "+frame);
          g2d.setColor(Color.black);
          g2d.drawString("4",0,font_height);
          g2d.drawString("5",font_width*5,font_height);
          g2d.drawString("6",font_width*10,font_height);
    
          BasicStroke stroke = (BasicStroke)g2d.getStroke();
          g2d.setStroke(new BasicStroke(3.f));
    
          switch(frame)
          {
            case 0:
              g2d.setColor(frameColour[0]);
              g2d.drawLine(font_width*2, lineHgt, font_width*4, lineHgt);
     
              g2d.setColor(frameColour[1]);
              g2d.drawLine(font_width*7, lineHgt, font_width*9, lineHgt);
    
              g2d.setColor(frameColour[2]);
              g2d.drawLine(font_width*12, lineHgt, font_width*14, lineHgt);
              g2d.setStroke(stroke);
              return;
            case 1:
              g2d.setColor(frameColour[2]);
              g2d.drawLine(font_width*2, lineHgt, font_width*4, lineHgt);
    
              g2d.setColor(frameColour[0]);
              g2d.drawLine(font_width*7, lineHgt, font_width*9, lineHgt);
    
              g2d.setColor(frameColour[1]);
              g2d.drawLine(font_width*12, lineHgt, font_width*14, lineHgt);
              g2d.setStroke(stroke);
              return;
            case 2:
              g2d.setColor(frameColour[1]);
              g2d.drawLine(font_width*2, lineHgt, font_width*4, lineHgt);
    
              g2d.setColor(frameColour[2]);
              g2d.drawLine(font_width*7, lineHgt, font_width*9, lineHgt);
    
              g2d.setColor(frameColour[0]);
              g2d.drawLine(font_width*12, lineHgt, font_width*14, lineHgt);
              g2d.setStroke(stroke);
              return;
          } 
        }
      }
    
    
    
    tjc's avatar
    tjc committed
      /**
    
    tjc's avatar
    tjc committed
       *  Return the number of values a call to getValues() will return - three
    
    tjc's avatar
    tjc committed
       *  in this case.
       **/
    
    tjc's avatar
    tjc committed
      public int getValueCount()
      {
    
    tjc's avatar
    tjc committed
        return 3;
      }
    
      /**
       *  Return the default or optimal window size.
       *  @return null is returned if this algorithm doesn't have optimal window
       *    size.
       **/
    
    tjc's avatar
    tjc committed
      public Integer getDefaultWindowSize()
      {
        final Integer super_window_size = super.getDefaultWindowSize();
        if(super_window_size != null) 
        {
          // the superclass version of getDefaultWindowSize() returns non-null
    
    tjc's avatar
    tjc committed
          // iff the user has set the window size in the options file
          return super_window_size;
        }
    
    tjc's avatar
    tjc committed
        return new Integer(120);
    
    tjc's avatar
    tjc committed
      }
    
      /**
       *  Return the default maximum window size for this algorithm.
       *  @return null is returned if this algorithm doesn't have maximum window
       *    size.
       **/
    
    tjc's avatar
    tjc committed
      public Integer getDefaultMaxWindowSize()
      {
        final Integer super_max_window_size = super.getDefaultMaxWindowSize();
        if(super_max_window_size != null) 
        {
          // the superclass version of getDefaultMaxWindowSize() returns non-null
    
    tjc's avatar
    tjc committed
          // iff the user has set the max window size in the options file
          return super_max_window_size;
        }
    
    tjc's avatar
    tjc committed
        return new Integer(500);
    
    tjc's avatar
    tjc committed
      }
    
      /**
       *  Return the default minimum window size for this algorithm.
       *  @return null is returned if this algorithm doesn't have minimum window
       *    size.
       **/
    
    tjc's avatar
    tjc committed
      public Integer getDefaultMinWindowSize()
      {
        final Integer super_min_window_size = super.getDefaultMinWindowSize();
        if(super_min_window_size != null) 
        {
          // the superclass version of getDefaultMinWindowSize() returns non-null
    
    tjc's avatar
    tjc committed
          // iff the user has set the minimum window size in the options file
          return super_min_window_size;
        }
    
    tjc's avatar
    tjc committed
        return new Integer(24);
    
    tjc's avatar
    tjc committed
      }
    
      /**
       *  Return the default or optimal step size.
       *  @return null is returned if this algorithm doesn't have optimal step
       *    size.
       **/
    
    tjc's avatar
    tjc committed
      public Integer getDefaultStepSize(int window_size) 
      {
        if(window_size > 8)
          return new Integer(window_size / 8);
        else
    
    tjc's avatar
    tjc committed
          return null;
      }
    
      /**
       *  Return the maximum value of this algorithm.
       *  @return The maximum is 100.
       **/
    
    tjc's avatar
    tjc committed
      protected Float getMaximumInternal()
      {
        return new Float(100);
    
    tjc's avatar
    tjc committed
      }
    
      /**
       *  Return the minimum value of this algorithm.
       *  @return The minimum is 0.
       **/
    
    tjc's avatar
    tjc committed
      protected Float getMinimumInternal() 
      {
        return new Float(0);
    
    tjc's avatar
    tjc committed
      }
    
      /**
       *  Return the average value of function over the whole strand.
       *  @return null is returned if this algorithm doesn't have an average or if
       *    the average can't be calculated.
       **/
    
    tjc's avatar
    tjc committed
      public Float getAverage() 
      {
        return new Float(getStrand().getBases().getAverageGCPercent());
    
    tjc's avatar
    tjc committed
      }
    
      /**
       *  Returns "GC Frame Plot" if the given strand is a forward strand
       *  otherwise returns "Reverse GC Frame Plot".
       **/
    
    tjc's avatar
    tjc committed
      private static String makeName(final Strand strand) 
      {
        if(strand.isForwardStrand())
    
    tjc's avatar
    tjc committed
          return "GC Frame Plot";
    
    tjc's avatar
    tjc committed
        else
    
    tjc's avatar
    tjc committed
          return "Reverse GC Frame Plot";
      }
    }