Skip to content
Snippets Groups Projects
UserDataAlgorithm.java 6.8 KiB
Newer Older
tjc's avatar
tjc committed
/* UserDataAlgorithm.java
 *
 * created: Wed May 10 2000
 *
 * This file is part of Artemis
 *
 * Copyright (C) 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/UserDataAlgorithm.java,v 1.4 2008-05-15 09:57:31 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.ReadFormatException;

import java.io.*;

/**
 *  Objects of this class have one useful method - getValues (), which takes a
 *  range of bases and returns a single floating point number.  The number is
 *  calculated by averaging the values from a data file.  The Strand to use is
 *  set in the constructor.
 *
 *  @author Kim Rutherford <kmr@sanger.ac.uk>
tjc's avatar
tjc committed
 *  @version $Id: UserDataAlgorithm.java,v 1.4 2008-05-15 09:57:31 tjc Exp $
tjc's avatar
tjc committed
 **/

tjc's avatar
tjc committed
public class UserDataAlgorithm extends BaseAlgorithm
{
  /**
   *  The data that was read by the constructor.
   **/
  private float data[][] = null;

  /**
   *  The maximum value in the data array.
   **/
  private float data_max = -9999999;

  /**
   *  The minimum value in the data array.
   **/
  private float data_min = 9999999;

  /**
   *  The average calculated by readData ().
   **/
  private float average_value = 0;

  /**
   *  The value returned by getValueCount ().
   **/
  private int number_of_values;
  
  private boolean logTransform;
  
tjc's avatar
tjc committed
  /**
   *  Create a new UserDataAlgorithm object.
   *  @param strand The strand to do the calculation on.
   *  @param document The Document to read the data from.
   **/
tjc's avatar
tjc committed
  public UserDataAlgorithm (final Strand strand, final Document document, 
                            final boolean logTransform)
      throws IOException 
  {
tjc's avatar
tjc committed
    super (strand, "User algorithm from " + document.getName (), "user");

tjc's avatar
tjc committed
    this.logTransform = logTransform;
tjc's avatar
tjc committed
    final Reader document_reader = document.getReader ();

tjc's avatar
tjc committed
    LinePushBackReader pushback_reader = new LinePushBackReader (document_reader);
tjc's avatar
tjc committed

    final String first_line = pushback_reader.readLine ();
tjc's avatar
tjc committed
    final StringVector tokens = StringVector.getStrings (first_line, " ");
tjc's avatar
tjc committed

tjc's avatar
tjc committed
    if (tokens.size () < 1) 
tjc's avatar
tjc committed
      throw new ReadFormatException ("unknown file type");

    this.number_of_values = tokens.size ();
    pushback_reader.pushBack (first_line);
    data = new float [strand.getSequenceLength ()][tokens.size ()];

    readData (pushback_reader);
  }

  /**
   *  Read all from buffered_reader into data.
   **/
  private void readData (final LinePushBackReader pushback_reader)
tjc's avatar
tjc committed
      throws IOException
  {
tjc's avatar
tjc committed
    String line = null;
    int count = 0;

tjc's avatar
tjc committed
    while ((line = pushback_reader.readLine ()) != null)
    {
tjc's avatar
tjc committed

tjc's avatar
tjc committed
      if (count >= getStrand ().getSequenceLength ()) 
tjc's avatar
tjc committed
        throw new ReadFormatException ("too many values in input file");

tjc's avatar
tjc committed
      final StringVector tokens = StringVector.getStrings (line, " ");
tjc's avatar
tjc committed
      //final int read_base;

      if (tokens.size () == data[0].length) 
      {
        for (int i = 0 ; i < tokens.size () ; ++i)
        {
          try 
          {
            float value =
tjc's avatar
tjc committed
              Float.valueOf ((String)tokens.elementAt (i)).floatValue ();
tjc's avatar
tjc committed

tjc's avatar
tjc committed
            if(logTransform)
              value = (float) Math.log(value+1);
            
            if (value > data_max) 
tjc's avatar
tjc committed
              data_max = value;

tjc's avatar
tjc committed
            if (value < data_min)
tjc's avatar
tjc committed
              data_min = value;

            data[count][i] = value;

            average_value += value;
tjc's avatar
tjc committed
          } 
          catch (NumberFormatException e) 
          {
tjc's avatar
tjc committed
            throw new ReadFormatException ("cannot understand this number: " +
                                           tokens.elementAt (i) + " - " +
                                           e.getMessage ());
          }
        }
tjc's avatar
tjc committed
      } 
      else 
tjc's avatar
tjc committed
        throw new ReadFormatException ("line has the wrong number of fields");

      ++count;
    }

    average_value /= data[0].length * getStrand ().getSequenceLength ();
  }

  /**
   *  Return the value of the function between a pair of bases.
   *  @param start The start base (included in the range).
   *  @param end The end base (included in the range).
   *  @param values The one return value for this algorithm is returned in
   *    this array.
   **/
tjc's avatar
tjc committed
  public void getValues (int start, int end, final float [] values) 
  {
tjc's avatar
tjc committed
    final int value_count = getValueCount ();

tjc's avatar
tjc committed
    for (int i = 0 ; i < value_count ; ++i) 
    {
tjc's avatar
tjc committed
      values [i] = 0;
tjc's avatar
tjc committed
      for (int base = start ; base <= end ; ++base) 
tjc's avatar
tjc committed
        values [i] += data[base - 1][i] / (end - start + 1);
    }
  }

  /**
   *  Return the number of values a call to getValues () will return - one
   *  in this case.
   **/
tjc's avatar
tjc committed
  public int getValueCount () 
  {
tjc's avatar
tjc committed
    return number_of_values;
  }

  /**
   *  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 () 
  {
tjc's avatar
tjc committed
    return new Integer (3);
  }

  /**
   *  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 ()
  {
tjc's avatar
tjc committed
    return new Integer (100);
  }

  /**
   *  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 () 
  {
tjc's avatar
tjc committed
    return new Integer (1);
  }

  /**
   *  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 > 10) 
tjc's avatar
tjc committed
      return new Integer (window_size / 10);
tjc's avatar
tjc committed
    else 
tjc's avatar
tjc committed
      return null;
  }

  /**
   *  Return the maximum value of this algorithm.
   **/
tjc's avatar
tjc committed
  protected Float getMaximumInternal ()
  {
tjc's avatar
tjc committed
    return new Float (data_max);
  }

  /**
   *  Return the minimum value of this algorithm.
   **/
tjc's avatar
tjc committed
  protected Float getMinimumInternal () 
  {
tjc's avatar
tjc committed
    return new Float (data_min);
  }

  /**
   *  Return the average value of function over the whole strand.
   **/
tjc's avatar
tjc committed
  public Float getAverage () 
  {
tjc's avatar
tjc committed
    return new Float (average_value);
  }

}