class Bio::Bam::AlignmentIterator

Class for iterating through alignments

Attributes

chromosome[RW]
command[RW]
region[RW]

Public Class Methods

new(command, references) click to toggle source

Creates a new AlignmentIterator object which will parse JSON outputted by a specified command. Names of reference sequences must be provided as well.

# File lib/bio-sambamba/alignmentiterator.rb, line 12
def initialize(command, references)
  @command = command
  @references = references
end

Public Instance Methods

[](reg) click to toggle source
# File lib/bio-sambamba/alignmentiterator.rb, line 114
def [](reg)
  overlapping(reg)
end
clone() click to toggle source
# File lib/bio-sambamba/alignmentiterator.rb, line 118
def clone
  iter = AlignmentIterator.new @command, @references
  iter.chromosome = chromosome
  iter.region = region
  iter
end
count() click to toggle source
# File lib/bio-sambamba/alignmentiterator.rb, line 93
def count
  command = get_command
  command.push('-c')
  Bio::Command.call_command_open3(command) do |pin, pout, perr|
    raise_exception_if_stderr_is_not_empty(perr)
    pout.readline.to_i
  end
end
each() { |alignment| ... } click to toggle source

Iterate through all alignments skipping validation checks

# File lib/bio-sambamba/alignmentiterator.rb, line 56
def each
  return enum_for(:each) if not block_given?

  command = get_command

  Bio::Command.call_command_open3(command) do |pin, pout, perr|

    counter = 0 # for triggering garbage collection manually
    unpacker = MessagePack::Unpacker.new pout

    begin
      unpacker.each do |obj|
        counter += 1
        yield Bio::Bam::Alignment.new(obj, @references)
        if (counter & 0xFFF) == 0 then
          ObjectSpace.garbage_collect
        end
      end
    rescue EOFError
    end

    raise_exception_if_stderr_is_not_empty(perr)
  end
end
each_valid() { |read| ... } click to toggle source

Iterate only through valid alignments

# File lib/bio-sambamba/alignmentiterator.rb, line 18
def each_valid

  return enum_for(:each_valid) if not block_given?

  command = get_command
  if command.index('--valid').nil?
    command.push '--valid'
  end

  iter = self.clone
  iter.command = command
  iter.each do |read|
    yield read
  end
end
overlapping(reg) click to toggle source
# File lib/bio-sambamba/alignmentiterator.rb, line 108
def overlapping(reg)
  iter = self.clone
  iter.region = reg
  iter
end
referencing(chr) click to toggle source
# File lib/bio-sambamba/alignmentiterator.rb, line 102
def referencing(chr)
  iter = self.clone
  iter.chromosome = chr
  iter
end
select(&block) click to toggle source
# File lib/bio-sambamba/alignmentiterator.rb, line 89
def select(&block)
  with_filter (Bio::Bam::filter &block)
end
with_filter(filter) click to toggle source

Set filter for alignments

# File lib/bio-sambamba/alignmentiterator.rb, line 82
def with_filter(filter)
  iter = self.clone
  iter.command.push('-F')
  iter.command.push(filter.to_s)
  iter
end

Private Instance Methods

get_command() click to toggle source
# File lib/bio-sambamba/alignmentiterator.rb, line 36
def get_command
  command = @command

  if not @chromosome.nil? then
    if not @region.nil? then
      command.push "#{@chromosome}:#{@region.min}-#{@region.max}"       
    else
      command.push "#{@chromosome}"
    end
  elsif not @region.nil? then
    raise 'must specify a reference when doing a region query'
  end

  command
end