Class: Bio::Bam::File

Inherits:
Object
  • Object
show all
Includes:
FileExistenceChecker
Defined in:
lib/bio-sambamba/bamfile.rb

Overview

Class providing access to BAM files

Instance Method Summary collapse

Methods included from FileExistenceChecker

#check_file_existence

Constructor Details

#initialize(filename) ⇒ File

Creates an object for access to BAM file



20
21
22
23
# File 'lib/bio-sambamba/bamfile.rb', line 20

def initialize(filename)
  @filename = filename
  check_file_existence filename
end

Instance Method Details

#[](chr) ⇒ Object



62
63
64
# File 'lib/bio-sambamba/bamfile.rb', line 62

def [](chr)
  fetch(chr, nil)
end

#alignmentsObject

Returns an AlignmentIterator object for iterating over all alignments in the file



31
32
33
34
# File 'lib/bio-sambamba/bamfile.rb', line 31

def alignments
  cmdline = ['sambamba', 'view', '--format', 'msgpack', @filename]
  Bio::Bam::AlignmentIterator.new(cmdline, reference_sequence_names)
end

#fetch(chr, region) ⇒ Object

Fetches alignments overlapping a region. Returns an AlignmentIterator object.


Arguments:

  • chr: reference sequence

  • region: a Range representing an interval. Coordinates are 1-based.



50
51
52
53
54
55
56
# File 'lib/bio-sambamba/bamfile.rb', line 50

def fetch(chr, region)
  cmdline = ['sambamba', 'view', '--format=msgpack', @filename]
  iter = Bio::Bam::AlignmentIterator.new(cmdline, reference_sequence_names)
  iter.chromosome = chr
  iter.region = region
  iter
end

#has_index?Boolean

True if index file was found

Returns:

  • (Boolean)


37
38
39
40
41
# File 'lib/bio-sambamba/bamfile.rb', line 37

def has_index?
  fn1 = @filename + '.bai'
  fn2 = @filename.chomp(RubyFile.extname(@filename)) + '.bai'
  RubyFile.exists?(fn1) || RubyFile.exists?(fn2)
end

#headerObject

SAM header



26
27
28
# File 'lib/bio-sambamba/bamfile.rb', line 26

def header
  @header ||= Bio::Bam::SamHeader.new(@filename)
end

#reference_sequencesObject



58
59
60
# File 'lib/bio-sambamba/bamfile.rb', line 58

def reference_sequences
  @reference_sequences ||= JSON.parse(Bio::Command.query_command ['sambamba', 'view', '-I', @filename])
end