Class: Transrate::Samtools

Inherits:
Object
  • Object
show all
Defined in:
lib/transrate/samtools.rb

Class Method Summary collapse

Class Method Details

.coverage(bam) ⇒ Object

Calculate per-base coverage from a sorted, indexed bam file return the path to the coverage file



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/transrate/samtools.rb', line 58

def self.coverage bam
  outfile = File.expand_path "#{File.basename(bam.fasta)}.coverage"
  cmd = "mpileup"
  cmd += " -f #{File.expand_path bam.fasta}" # reference
  cmd += " -B" # don't calculate BAQ quality scores
  cmd += " -Q0" # include all reads ignoring quality
  cmd += " -I" # don't do genotype calculations
  cmd += " #{File.expand_path bam.bam}" # the bam file
  cmd += " > #{outfile}"
  Samtools.run cmd
  outfile
end

.index_bam(bamfile) ⇒ Object

Index a bamfile, returning the path to the index



40
41
42
43
44
45
# File 'lib/transrate/samtools.rb', line 40

def self.index_bam bamfile
  index = File.basename(bamfile, '.bam') + '.bai'
  index = File.expand_path index
  Samtools.run "index #{File.expand_path bamfile} #{index}"
  index
end

.pathObject

Get the path to the samtools binary built when bio-samtools was installed



9
10
11
12
# File 'lib/transrate/samtools.rb', line 9

def self.path
  gem_path = Gem.loaded_specs['bio-samtools'].full_gem_path
  return File.join(gem_path, 'lib/bio/db/sam/external/samtools')
end

.run(cmd) ⇒ Object

Run a samtools command



15
16
17
18
19
# File 'lib/transrate/samtools.rb', line 15

def self.run cmd
  runcmd = Cmd.new "#{Samtools.path} #{cmd}"
  runcmd.run
  runcmd.stdout
end

.sam_to_bam(samfile) ⇒ Object

Convert a sam file to a bam file, returning the path to the bamfile



22
23
24
25
26
27
# File 'lib/transrate/samtools.rb', line 22

def self.sam_to_bam samfile
  bamfile = File.basename(samfile, '.sam') + '.bam'
  bamfile = File.expand_path bamfile
  Samtools.run "view -bS #{File.expand_path samfile} > #{bamfile}"
  File.expand_path bamfile
end

.sam_to_sorted_indexed_bam(samfile) ⇒ Object

Convert a sam file to bam, sort and index the bam, returning an array of paths to the bamfile, sorted bamfile and index respectively



49
50
51
52
53
54
# File 'lib/transrate/samtools.rb', line 49

def self.sam_to_sorted_indexed_bam samfile
  bamfile = Samtools.sam_to_bam samfile
  sorted = Samtools.sort_bam bamfile
  index = Samtools.index_bam bamfile
  [bamfile, sorted, index]
end

.sort_bam(bamfile) ⇒ Object

Sort a bam file, returning the path to the sorted bamfile



30
31
32
33
34
35
36
37
# File 'lib/transrate/samtools.rb', line 30

def self.sort_bam bamfile
  # the sort command behaves inconsistently with the other commands:
  # it takes an output prefix rather than a filename
  # and automatically adds the .bam extension
  sorted = File.basename(bamfile, '.bam') + '.sorted'
  Samtools.run "sort #{File.expand_path bamfile} #{sorted}"
  File.expand_path(sorted + '.bam')
end