Class: Transrate::Salmon

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

Instance Method Summary collapse

Constructor Details

#initializeSalmon

Returns a new instance of Salmon.



8
9
10
11
12
13
14
15
# File 'lib/transrate/salmon.rb', line 8

def initialize
  which = Cmd.new('which salmon')
  which.run
  if !which.status.success?
    raise SalmonError.new("could not find salmon in the path")
  end
  @salmon = which.stdout.split("\n").first
end

Instance Method Details

#build_command(assembly, bamfile, threads = 4) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/transrate/salmon.rb', line 38

def build_command assembly, bamfile, threads=4
  cmd = "#{@salmon} quant"
  cmd << " --libType IU"
  cmd << " --alignments #{bamfile}"
  cmd << " --targets #{assembly}"
  cmd << " --threads #{threads}"
  cmd << " --sampleOut"
  cmd << " --sampleUnaligned" # thanks Rob!
  cmd << " --output ."
  cmd << " --useVBOpt"
  cmd << " --useErrorModel"
  cmd
end

#load_expression(file) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/transrate/salmon.rb', line 52

def load_expression file
  expression = {}
  File.open(file).each do |line|
    if line !~ /^#/
      line = line.chomp.split("\t")
      unless line.length == 4
        raise SalmonError.new("Salmon output file should have 4 columns " +
          "but it had #{line.length}\n" +
          "Please check you are using the correct version of Salmon")
      end
      target = line[0]
      effective_length = line[1]
      effective_count = line[3]
      tpm = line[2]
      expression[target] = {
        :eff_len => effective_length.to_i,
        :eff_count => effective_count.to_f,
        :tpm => tpm.to_f
      }
    end
  end
  expression
end

#run(assembly, bamfile, threads = 8) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/transrate/salmon.rb', line 17

def run assembly, bamfile, threads=8
  assembly = assembly.file if assembly.is_a? Assembly
  output = "quant.sf"
  sampled_bam = "postSample.bam"
  @fin_output = "#{File.basename assembly}_#{output}"
  unless File.exist? @fin_output
    salmon = Cmd.new build_command(assembly, bamfile, threads)
    salmon.run
    unless salmon.status.success?
      logger.error salmon.stderr
      raise SalmonError.new("Salmon failed")
    end
    unless File.exist?(sampled_bam)
      logger.error salmon.stderr
      raise SalmonError.new("#{sampled_bam} not created")
    end
    File.rename(output, @fin_output)
  end
  return sampled_bam
end