Class: Bio::Bam::AlignmentIterator

Inherits:
Object
  • Object
show all
Includes:
SambambaStderrParser, Enumerable
Defined in:
lib/bio-sambamba/alignmentiterator.rb

Overview

Class for iterating through alignments

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command, references) ⇒ AlignmentIterator

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



12
13
14
15
# File 'lib/bio-sambamba/alignmentiterator.rb', line 12

def initialize(command, references)
  @command = command
  @references = references
end

Instance Attribute Details

#chromosomeObject

Returns the value of attribute chromosome.



125
126
127
# File 'lib/bio-sambamba/alignmentiterator.rb', line 125

def chromosome
  @chromosome
end

#commandObject

Returns the value of attribute command.



127
128
129
# File 'lib/bio-sambamba/alignmentiterator.rb', line 127

def command
  @command
end

#regionObject

Returns the value of attribute region.



126
127
128
# File 'lib/bio-sambamba/alignmentiterator.rb', line 126

def region
  @region
end

Instance Method Details

#[](reg) ⇒ Object



114
115
116
# File 'lib/bio-sambamba/alignmentiterator.rb', line 114

def [](reg)
  overlapping(reg)
end

#cloneObject



118
119
120
121
122
123
# File 'lib/bio-sambamba/alignmentiterator.rb', line 118

def clone
  iter = AlignmentIterator.new @command, @references
  iter.chromosome = chromosome
  iter.region = region
  iter
end

#countObject



93
94
95
96
97
98
99
100
# 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

#eachObject

Iterate through all alignments skipping validation checks



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# 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_validObject

Iterate only through valid alignments



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 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) ⇒ Object



108
109
110
111
112
# File 'lib/bio-sambamba/alignmentiterator.rb', line 108

def overlapping(reg)
  iter = self.clone
  iter.region = reg
  iter
end

#referencing(chr) ⇒ Object



102
103
104
105
106
# File 'lib/bio-sambamba/alignmentiterator.rb', line 102

def referencing(chr)
  iter = self.clone
  iter.chromosome = chr
  iter
end

#select(&block) ⇒ Object



89
90
91
# File 'lib/bio-sambamba/alignmentiterator.rb', line 89

def select(&block)
  with_filter (Bio::Bam::filter &block)
end

#with_filter(filter) ⇒ Object

Set filter for alignments



82
83
84
85
86
87
# 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