[BioRuby-cvs] bioruby/lib/bio/util/restriction_enzyme/double_stranded aligned_strands.rb, NONE, 1.1 cut_location_pair.rb, NONE, 1.1 cut_location_pair_in_enzyme_notation.rb, NONE, 1.1 cut_locations.rb, NONE, 1.1 cut_locations_in_enzyme_notation.rb, NONE, 1.1

Trevor Wennblom trevor at pub.open-bio.org
Wed Feb 1 07:27:32 UTC 2006


Update of /home/repository/bioruby/bioruby/lib/bio/util/restriction_enzyme/double_stranded
In directory pub.open-bio.org:/tmp/cvs-serv28844/double_stranded

Added Files:
	aligned_strands.rb cut_location_pair.rb 
	cut_location_pair_in_enzyme_notation.rb cut_locations.rb 
	cut_locations_in_enzyme_notation.rb 
Log Message:
Bio::RestrictionEnzyme


--- NEW FILE: aligned_strands.rb ---
require 'pathname'
libpath = Pathname.new(File.join(File.dirname(__FILE__), ['..'] * 5, 'lib')).cleanpath.to_s
$:.unshift(libpath) unless $:.include?(libpath)

require 'bio/util/restriction_enzyme/single_strand'
require 'bio/util/restriction_enzyme/cut_symbol'
require 'bio/util/restriction_enzyme/string_formatting'

module Bio; end
class Bio::RestrictionEnzyme
class DoubleStranded

#
# bio/util/restriction_enzyme/double_stranded/aligned_strands.rb - 
#
# Copyright::  Copyright (C) 2006 Trevor Wennblom <trevor at corevx.com>
# License::    LGPL
#
#  $Id: aligned_strands.rb,v 1.1 2006/02/01 07:34:11 trevor Exp $
#
#
#--
#
#  This library is free software; you can redistribute it and/or
#  modify it under the terms of the GNU Lesser General Public
#  License as published by the Free Software Foundation; either
#  version 2 of the License, or (at your option) any later version.
#
#  This library 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
#  Lesser General Public License for more details.
#
#  You should have received a copy of the GNU Lesser General Public
#  License along with this library; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
#
#++
#

=begin rdoc
bio/util/restriction_enzyme/double_stranded/aligned_strands.rb - 

Align two SingleStrand::Pattern objects and return a Result
object with +primary+ and +complement+ accessors.
=end
class AlignedStrands
  extend CutSymbol
  extend StringFormatting

  # The object returned for alignments
  Result = Struct.new(:primary, :complement)

  # Pad and align two String objects.
  #
  # +a+:: First String
  # +b+:: Second String
  #
  # Example invocation:
  #   AlignedStrands.align('nngattacannnnn', 'nnnnnctaatgtnn')
  #
  # Example return value:
  #   #<struct Bio::RestrictionEnzyme::DoubleStranded::AlignedStrands::Result
  #    primary="nnnnngattacannnnn",
  #    complement="nnnnnctaatgtnnnnn">
  #
  def self.align(a, b)
    a = a.to_s
    b = b.to_s
    validate_input( strip_padding(a), strip_padding(b) )
    left = [left_padding(a), left_padding(b)].sort.last
    right = [right_padding(a), right_padding(b)].sort.last

    p = left + strip_padding(a) + right
    c = left + strip_padding(b) + right
    Result.new(p,c)
  end

  # Pad and align two String objects with cut symbols.
  #
  # +a+:: First String
  # +b+:: Second String
  # +a_cuts+:: First strand cut locations in 0-based index notation
  # +b_cuts+:: Second strand cut locations in 0-based index notation
  #
  # Example invocation:
  #   AlignedStrands.with_cuts('nngattacannnnn', 'nnnnnctaatgtnn', [0, 10, 12], [0, 2, 12])
  #
  # Example return value:
  #   #<struct Bio::RestrictionEnzyme::DoubleStranded::AlignedStrands::Result
  #    primary="n n n n^n g a t t a c a n n^n n^n",
  #    complement="n^n n^n n c t a a t g t n^n n n n">
  #
  # Notes:
  # * To make room for the cut symbols each nucleotide is spaced out.
  # * This is meant to be able to handle multiple cuts and completely
  #   unrelated cutsites on the two strands, therefore no biological
  #   shortcuts are made.
  #
  def self.align_with_cuts(a,b,a_cuts,b_cuts)
    a = a.to_s
    b = b.to_s
    validate_input( strip_padding(a), strip_padding(b) )

    a_left, a_right = left_padding(a), right_padding(a)
    b_left, b_right = left_padding(b), right_padding(b)

    left_diff = a_left.length - b_left.length
    right_diff = a_right.length - b_right.length

    (right_diff > 0) ? (b_right += 'n' * right_diff) : (a_right += 'n' * right_diff.abs)

    a_adjust = b_adjust = 0

    if left_diff > 0
      b_left += 'n' * left_diff
      b_adjust = left_diff
    else
      a_left += 'n' * left_diff.abs
      a_adjust = left_diff.abs
    end

    a = a_left + strip_padding(a) + a_right
    b = b_left + strip_padding(b) + b_right

    a_cuts.sort.reverse.each { |c| a.insert(c+1+a_adjust, cut_symbol) }
    b_cuts.sort.reverse.each { |c| b.insert(c+1+b_adjust, cut_symbol) }

    Result.new( add_spacing(a), add_spacing(b) )
  end

  #########
  protected
  #########

  def self.validate_input(a,b)
    unless a.size == b.size
      err = "Result sequences are not the same size.  Does not align sequences with differing lengths after strip_padding.\n"
      err += "#{a.size}, #{a.inspect}\n"
      err += "#{b.size}, #{b.inspect}"
      raise ArgumentError, err
    end
  end

end

end
end

--- NEW FILE: cut_locations.rb ---
require 'pathname'
libpath = Pathname.new(File.join(File.dirname(__FILE__), ['..'] * 5, 'lib')).cleanpath.to_s
$:.unshift(libpath) unless $:.include?(libpath)

require 'bio/util/restriction_enzyme/double_stranded/cut_location_pair'

module Bio; end
class Bio::RestrictionEnzyme
class DoubleStranded

#
# bio/util/restriction_enzyme/double_stranded/cut_locations.rb - 
#
# Copyright::  Copyright (C) 2006 Trevor Wennblom <trevor at corevx.com>
# License::    LGPL
#
#  $Id: cut_locations.rb,v 1.1 2006/02/01 07:34:11 trevor Exp $
#
#
#--
#
#  This library is free software; you can redistribute it and/or
#  modify it under the terms of the GNU Lesser General Public
#  License as published by the Free Software Foundation; either
#  version 2 of the License, or (at your option) any later version.
#
#  This library 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
#  Lesser General Public License for more details.
#
#  You should have received a copy of the GNU Lesser General Public
#  License along with this library; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
#
#++
#

=begin rdoc
bio/util/restriction_enzyme/double_stranded/cut_locations.rb - 
=end
class CutLocations < Array

  def initialize(*args)
    validate_args(args)
    super(args)
  end

  def primary
    self.collect {|a| a[0]}
  end

  def complement
    self.collect {|a| a[1]}
  end

  #########
  protected
  #########

  def validate_args(args)
    args.each do |a|
      unless a.class == Bio::RestrictionEnzyme::DoubleStranded::CutLocationPair
        err = "Not a CutLocationPair\n"
        err += "class: #{a.class}\n"
        err += "inspect: #{a.inspect}"
        raise ArgumentError, err
      end
    end
  end


end

end
end

--- NEW FILE: cut_locations_in_enzyme_notation.rb ---
require 'pathname'
libpath = Pathname.new(File.join(File.dirname(__FILE__), ['..'] * 5, 'lib')).cleanpath.to_s
$:.unshift(libpath) unless $:.include?(libpath)

require 'bio/util/restriction_enzyme/double_stranded/cut_locations'
require 'bio/util/restriction_enzyme/double_stranded/cut_location_pair_in_enzyme_notation'

module Bio; end
class Bio::RestrictionEnzyme
class DoubleStranded

#
# bio/util/restriction_enzyme/double_stranded/cut_locations_in_enzyme_notation.rb - 
#
# Copyright::  Copyright (C) 2006 Trevor Wennblom <trevor at corevx.com>
# License::    LGPL
#
#  $Id: cut_locations_in_enzyme_notation.rb,v 1.1 2006/02/01 07:34:11 trevor Exp $
#
#
#--
#
#  This library is free software; you can redistribute it and/or
#  modify it under the terms of the GNU Lesser General Public
#  License as published by the Free Software Foundation; either
#  version 2 of the License, or (at your option) any later version.
#
#  This library 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
#  Lesser General Public License for more details.
#
#  You should have received a copy of the GNU Lesser General Public
#  License along with this library; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
#
#++
#

=begin rdoc
bio/util/restriction_enzyme/double_stranded/cut_locations_in_enzyme_notation.rb - 
=end
class CutLocationsInEnzymeNotation < CutLocations

  def primary_to_array_index
    helper_for_to_array_index(self.primary)
  end

  def complement_to_array_index
    helper_for_to_array_index(self.complement)
  end

  def to_array_index
    unless self.primary_to_array_index.size == self.complement_to_array_index.size
      err = "Primary and complement strand cut locations are not available in equal numbers.\n"
      err += "primary: #{self.primary_to_array_index.inspect}\n"
      err += "primary.size: #{self.primary_to_array_index.size}\n"
      err += "complement: #{self.complement_to_array_index.inspect}\n"
      err += "complement.size: #{self.complement_to_array_index.size}"
      raise IndexError, err
    end
    a = self.primary_to_array_index.zip(self.complement_to_array_index)
    CutLocations.new( *a.collect {|cl| CutLocationPair.new(cl)} )
  end

  #########
  protected
  #########

  def helper_for_to_array_index(a)
    minimum = (self.primary + self.complement).flatten
    minimum.delete(nil)
    minimum = minimum.sort.first

    return [] if minimum == nil  # no elements

    if minimum.negative?
      calc = lambda do |n|
        unless n == nil
          n -= 1 unless n.negative?
          n += minimum.abs
        end
        n
      end
    else
      calc = lambda do |n| 
        n -= 1 unless n == nil
        n
      end
    end

    a.collect(&calc)
  end

  def validate_args(args)
    args.each do |a|
      unless a.class == Bio::RestrictionEnzyme::DoubleStranded::CutLocationPairInEnzymeNotation
        err = "Not a CutLocationPairInEnzymeNotation\n"
        err += "class: #{a.class}\n"
        err += "inspect: #{a.inspect}"
        raise TypeError, err
      end
    end
  end

end

end
end

--- NEW FILE: cut_location_pair_in_enzyme_notation.rb ---
require 'pathname'
libpath = Pathname.new(File.join(File.dirname(__FILE__), ['..'] * 5, 'lib')).cleanpath.to_s
$:.unshift(libpath) unless $:.include?(libpath)

require 'bio/util/restriction_enzyme/double_stranded/cut_location_pair'
require 'bio/util/restriction_enzyme/integer'

module Bio; end
class Bio::RestrictionEnzyme
class DoubleStranded

#
# bio/util/restriction_enzyme/double_stranded/cut_location_pair_in_enzyme_notation.rb - 
#
# Copyright::  Copyright (C) 2006 Trevor Wennblom <trevor at corevx.com>
# License::    LGPL
#
#  $Id: cut_location_pair_in_enzyme_notation.rb,v 1.1 2006/02/01 07:34:11 trevor Exp $
#
#
#--
#
#  This library is free software; you can redistribute it and/or
#  modify it under the terms of the GNU Lesser General Public
#  License as published by the Free Software Foundation; either
#  version 2 of the License, or (at your option) any later version.
#
#  This library 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
#  Lesser General Public License for more details.
#
#  You should have received a copy of the GNU Lesser General Public
#  License along with this library; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
#
#++
#

=begin rdoc
bio/util/restriction_enzyme/double_stranded/cut_location_pair_in_enzyme_notation.rb - 

See CutLocationPair
=end
class CutLocationPairInEnzymeNotation < CutLocationPair

  #########
  protected
  #########

  def validate_2( a, b )
    if a == 0
      raise ArgumentError, "Enzyme index notation only.  0 values are illegal."
    end

    if b == 0
      raise ArgumentError, "Enzyme index notation only.  0 values are illegal."
    end

    if a == nil and b == nil
      raise ArgumentError, "Neither strand has a cut.  Ambiguous."
    end
  end

end

end
end

--- NEW FILE: cut_location_pair.rb ---
require 'pathname'
libpath = Pathname.new(File.join(File.dirname(__FILE__), ['..'] * 5, 'lib')).cleanpath.to_s
$:.unshift(libpath) unless $:.include?(libpath)

require 'bio/util/restriction_enzyme/cut_symbol'
require 'bio/util/restriction_enzyme/integer'

module Bio; end
class Bio::RestrictionEnzyme
class DoubleStranded

#
# bio/util/restriction_enzyme/double_stranded/cut_location_pair.rb - 
#
# Copyright::  Copyright (C) 2006 Trevor Wennblom <trevor at corevx.com>
# License::    LGPL
#
#  $Id: cut_location_pair.rb,v 1.1 2006/02/01 07:34:11 trevor Exp $
#
#
#--
#
#  This library is free software; you can redistribute it and/or
#  modify it under the terms of the GNU Lesser General Public
#  License as published by the Free Software Foundation; either
#  version 2 of the License, or (at your option) any later version.
#
#  This library 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
#  Lesser General Public License for more details.
#
#  You should have received a copy of the GNU Lesser General Public
#  License along with this library; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
#
#++
#

=begin rdoc
bio/util/restriction_enzyme/double_stranded/cut_location_pair.rb - 

Stores a cut location pair in 0-based index notation

Input:
+pair+:: May be two values represented as an Array, a Range, or a
         combination of Integer and nil values.  The first value
         represents a cut on the primary strand, the second represents
         a cut on the complement strand.

Example:
  clp = CutLocationPair.new(3,2)
  clp.primary                    # 3
  clp.complement                 # 2

Notes:
* a value of +nil+ is an explicit representation of 'no cut'
=end
class CutLocationPair < Array
  attr_reader :primary, :complement

  def initialize( *pair )
    a = b = nil

    if pair[0].kind_of? Array
      a,b = init_with_array( pair[0] )

    elsif pair[0].kind_of? Range
      a,b = init_with_array( [pair[0].first, pair[0].last] )

    elsif pair[0].kind_of? Integer or pair[0].kind_of? NilClass
      a,b = init_with_array( [pair[0], pair[1]] )

    else
      raise ArgumentError, "#{pair[0].class} is an invalid class type."
    end

    super( [a,b] )
    @primary = a
    @complement = b
  end

  #########
  protected
  #########

  def init_with_array( ary )
    validate_1(ary)
    a = ary.shift
    ary.empty? ? b = nil : b = ary.shift
    validate_2(a,b)
    [a,b]
  end

  def validate_1( ary )
    unless ary.size == 1 or ary.size == 2
      raise ArgumentError, "Must be one or two elements."
    end
  end

  def validate_2( a, b )
    if a != nil and a.negative?
      raise ArgumentError, "0-based index notation only.  Negative values are illegal."
    end

    if b != nil and b.negative?
      raise ArgumentError, "0-based index notation only.  Negative values are illegal."
    end

    if a == nil and b == nil
      raise ArgumentError, "Neither strand has a cut.  Ambiguous."
    end
  end

end

end
end




More information about the bioruby-cvs mailing list