class MgNu::Alignment

Constants

StrongConservationGroups
WeakConservationGroups

Attributes

length[R]
order[RW]
sequences[RW]

Public Class Methods

new(sequences, order = nil) click to toggle source

create a new Alignment object

# File lib/mgnu/alignment.rb, line 11
def initialize(sequences, order = nil)
  @sequences = sequences
  @order = order
  @length = sequences[sequences.keys[0]].length
end

Public Instance Methods

[](range = nil) click to toggle source
# File lib/mgnu/alignment.rb, line 101
def [](range = nil)
  each_position(range)
end
each() { |seq| ... } click to toggle source

override each

# File lib/mgnu/alignment.rb, line 18
def each
  if @order.nil?
    @sequences.each do |name, seq|
      yield seq
    end
  else
    @order.each do |name|
      yield @sequences[name]
    end
  end
end
each_position(range = nil) { |position| ... } click to toggle source

Returns an array of arrays containing the sequences at the position indicated. Can take a range

# File lib/mgnu/alignment.rb, line 32
def each_position(range = nil)
  matrix = []
  if @order.nil?
    @sequences.each do |name, seq|
      if range.class == Range
        matrix.push(seq[range].split(//))
      elsif range.class == Integer
        matrix.push(seq[range])
      else
        matrix.push(seq.split(//))
      end
    end
  else
    @order.each do |name|
      if range.class == Range
        # correct for 0 indexed arrays
        matrix.push(@sequences[name][(range.begin - 1..range.end - 1)].split(//))
      elsif range.class == Integer
        matrix.push(@sequences[name][range - 1].chr)
      else
        matrix.push(@sequences[name].split(//))
      end
    end
  end

  positions = []
  if range.class == Range
    range.each do |pos|
      position = []
      matrix.each do |seq|
        position.push(seq[(pos - 1) - (range.begin - 1)])
      end
      positions << position
      if block_given?
        yield position
      end
    end
    unless block_given?
      positions
    end
  elsif range.class == Integer
    position = []
    matrix.each do |seq|
      position.push(seq)
    end
    positions << position
    if block_given?
      yield position
    end
    unless block_given?
      positions
    end
  else
    0.upto(@length-1) do |pos|
      position = []
      matrix.each do |seq|
        position.push(seq[pos])
      end
      positions << position
      if block_given?
        yield position
      end
    end
    unless block_given?
      positions
    end
  end
end
match(range = nil) click to toggle source
# File lib/mgnu/alignment.rb, line 105
def match(range = nil)
  # get the matrix for the whole alignment, or a portion if a
  # range is given
  m = each_position(range) 
  str = ""

  # go through every row (position) in the array from
  # each_position and compute the match symbol.  Concat to str
  m.each do |pos|
    # if there's a gap in the alignment at this pos, return a space
    if pos.index("-") != nil
      str += " "
    else
      # no gaps, so determine strength of column
      p = pos.collect { |c| c.upcase }.sort.uniq
      if p.length == 1
        str += "*"
      elsif StrongConservationGroups.find { |x| (p - x).empty? }
        str += ":"
      elsif WeakConservationGroups.find { |x| (p - x).empty? }
        str += "."
      else
        str += " "
      end
    end
  end
  str
end
to_s() click to toggle source
# File lib/mgnu/alignment.rb, line 134
def to_s
  str = ""
  self.order.each do |name|
    str += "#{name}: #{self.sequences[name]}\n"
  end
  str += self.match + "\n"
  str
end