transrate 0.3.1 → 1.0.0.alpha.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,75 +0,0 @@
1
- module Transrate
2
-
3
- class Bowtie2Error < StandardError
4
- end
5
-
6
- class Bowtie2
7
-
8
- require 'which'
9
- include Which
10
-
11
- attr_reader :index_name, :sam
12
-
13
- def initialize
14
- bowtie2_path = which('bowtie2')
15
- if bowtie2_path.empty?
16
- raise Bowtie2Error.new("could not find bowtie2 in the path")
17
- end
18
- @bowtie2 = bowtie2_path.first
19
- bowtie2_build_path = which('bowtie2-build')
20
- if bowtie2_build_path.empty?
21
- raise Bowtie2Error.new("could not find bowtie2-build in the path")
22
- end
23
- @bowtie2_build = bowtie2_build_path.first
24
- @index_built = false
25
- @index_name = ""
26
- end
27
-
28
- def map_reads(file, left,
29
- right, insertsize: 200,
30
- insertsd: 50, outputname: nil,
31
- threads: 8)
32
- raise Bowtie2Error.new("Index not built") if !@index_built
33
- lbase = File.basename(left)
34
- rbase = File.basename(right)
35
- index = File.basename(@index_name)
36
- @sam = File.expand_path("#{lbase}.#{rbase}.#{index}.sam")
37
- realistic_dist = insertsize + (3 * insertsd)
38
- unless File.exists? @sam
39
- # construct bowtie command
40
- bowtiecmd = "#{@bowtie2} --very-sensitive"
41
- bowtiecmd += " -p #{threads} -X #{realistic_dist}"
42
- bowtiecmd += " --quiet --no-unal"
43
- bowtiecmd += " --seed 1337"
44
- bowtiecmd += " -x #{@index_name}"
45
- bowtiecmd += " -1 #{left}"
46
- # paired end?
47
- bowtiecmd += " -2 #{right}" if right
48
- bowtiecmd += " -S #{@sam}"
49
- # run bowtie
50
- runner = Cmd.new bowtiecmd
51
- runner.run
52
- if !runner.status.success?
53
- raise Bowtie2Error.new("Bowtie2 failed\n#{runner.stderr}")
54
- end
55
- end
56
- @sam
57
- end
58
-
59
- def build_index file
60
- @index_name = File.basename(file).split(".")[0..-2].join(".")
61
- unless File.exists?(@index_name + '.1.bt2')
62
- cmd = "#{@bowtie2_build} --quiet --offrate 1 #{file} #{@index_name}"
63
- runner = Cmd.new cmd
64
- runner.run
65
- if !runner.status.success?
66
- msg = "Failed to build Bowtie2 index\n#{runner.stderr}"
67
- raise Bowtie2Error.new(msg)
68
- end
69
- end
70
- @index_built = true
71
- end
72
-
73
- end # Bowtie2
74
-
75
- end # Transrate
@@ -1,18 +0,0 @@
1
- module Transrate
2
-
3
- class DimensionReduce
4
-
5
- def self.dimension_reduce(metrics)
6
- total = 0
7
- metrics.each do |metric|
8
- o = metric.origin
9
- w = metric.weighting
10
- a = metric.score
11
- total += w * ((o - a) ** 2)
12
- end
13
- Math.sqrt(total) / metrics.length
14
- end
15
-
16
- end # DimensionReduce
17
-
18
- end # Transrate
@@ -1,16 +0,0 @@
1
- module Transrate
2
-
3
- class Metric
4
-
5
- attr_reader :origin, :score, :name, :weighting
6
-
7
- def initialize(name, score, origin)
8
- @origin = origin
9
- @score = score ? score : (1 - origin)
10
- @name = name
11
- @weighting = 1
12
- end
13
-
14
- end # Metric
15
-
16
- end # Transrate
@@ -1,66 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require 'helper'
4
- require 'tmpdir'
5
-
6
- class TestBowtie < Test::Unit::TestCase
7
-
8
- context "bowtie" do
9
-
10
- setup do
11
- @reference = File.join(File.dirname(__FILE__), 'data',
12
- 'sorghum_transcript.fa')
13
- @left = File.join(File.dirname(__FILE__), 'data', '150uncovered.l.fq')
14
- @right = File.join(File.dirname(__FILE__), 'data', '150uncovered.r.fq')
15
- @mapper = Transrate::Bowtie2.new
16
- end
17
-
18
- should "build index" do
19
- Dir.mktmpdir do |tmpdir|
20
- Dir.chdir tmpdir do
21
- @mapper.build_index @reference
22
- assert File.exist?("sorghum_transcript.1.bt2")
23
- end
24
- end
25
- end
26
-
27
- should "build index and map reads" do
28
- Dir.mktmpdir do |tmpdir|
29
- Dir.chdir tmpdir do
30
- @mapper.build_index @reference
31
- left = File.basename(@left)
32
- right = File.basename(@right)
33
- index = File.basename(@mapper.index_name)
34
- @mapper.map_reads(@reference, @left, @right)
35
- sam = @mapper.sam
36
- assert File.exist?("#{sam}"), "sam file doesn't exist"
37
- cmd = "grep -v \"^@\" #{sam} | wc -l "
38
- line_in_sam_file = `#{cmd}`.chomp.to_i
39
- assert_equal 424, line_in_sam_file
40
- end
41
- end
42
- end
43
-
44
- should "raise error when no index built" do
45
- Dir.mktmpdir do |tmpdir|
46
- Dir.chdir tmpdir do
47
- assert_raise Transrate::Bowtie2Error do
48
- @mapper.map_reads(@reference, @left, @right)
49
- end
50
- end
51
- end
52
- end
53
-
54
- should "raise error when bowtie fails" do
55
- not_reads = File.join(File.dirname(__FILE__), 'data', 'not_a_file.fq')
56
- Dir.mktmpdir do |tmpdir|
57
- Dir.chdir tmpdir do
58
- assert_raise Transrate::Bowtie2Error do
59
- @mapper.build_index @reference
60
- @mapper.map_reads(@reference, @left, not_reads)
61
- end
62
- end
63
- end
64
- end
65
- end
66
- end