Skip to content
Permalink
Browse files

Additional test coverage

Also, minor code style 🆙
  • Loading branch information
lmrodriguezr committed Mar 25, 2020
1 parent a1663fa commit 4bf1909c03e9f38b20b5aa8c26ca5a63d5c85d0e
Showing with 151 additions and 18 deletions.
  1. +10 −10 lib/miga/result/dates.rb
  2. +119 −0 test/result_stats_test.rb
  3. +22 −8 test/result_test.rb
@@ -30,16 +30,16 @@ def running_time

private

##
# Internal function to detect start and end dates
def date_at(event)
date = self[event]
date ||= self[:started] if event == :start
if date.nil?
f = path event
date = File.read(f) if File.size? f
end
date.nil? ? nil : Time.parse(date)
##
# Internal function to detect start and end dates
def date_at(event)
date = self[event]
date ||= self[:started] if event == :start
if date.nil?
f = path event
date = File.read(f) if File.size? f
end
Time.parse(date) unless date.nil?
end
end

@@ -0,0 +1,119 @@
require 'test_helper'
require 'miga/project'

class ResultStatsTest < Test::Unit::TestCase

def setup
$tmp = Dir.mktmpdir
ENV['MIGA_HOME'] = $tmp
FileUtils.touch(File.expand_path('.miga_rc', ENV['MIGA_HOME']))
FileUtils.touch(File.expand_path('.miga_daemon.json', ENV['MIGA_HOME']))
$p = MiGA::Project.new(File.expand_path('project1', $tmp))
$d = $p.add_dataset('dataset1')
end

def teardown
FileUtils.rm_rf $tmp
ENV['MIGA_HOME'] = nil
end

def file_path(dir, ext)
File.join($p.path, dir, "#{$d.name}#{ext}")
end

def touch_done(dir)
FileUtils.touch(file_path(dir, '.done'))
end

def test_single_raw_reads
dir = 'data/01.raw_reads'
fq = file_path(dir, '.1.fastq')
File.open(fq, 'w') { |fh| fh.puts '@1','ACTAC','+','####' }
touch_done(dir)
r = $d.add_result(:raw_reads)
assert_equal({}, r[:stats])
r.compute_stats
assert(!r[:stats].empty?)
assert_equal(Hash, r[:stats].class)
assert_equal(1, r[:stats][:reads])
assert_equal([40.0, '%'], r[:stats][:g_c_content])
end

def test_coupled_raw_reads
dir = 'data/01.raw_reads'
fq = file_path(dir, '.1.fastq')
File.open(fq, 'w') { |fh| fh.puts '@1','ACTAC','+','####' }
fq = file_path(dir, '.2.fastq')
File.open(fq, 'w') { |fh| fh.puts '@1','ACTAC','+','####' }
touch_done(dir)
r = $d.add_result(:raw_reads)
r.compute_stats
assert(!r[:stats].empty?)
assert_nil(r[:stats][:reads])
assert_equal(1, r[:stats][:read_pairs])
assert_equal([40.0, '%'], r[:stats][:reverse_g_c_content])
end

def test_trimmed_reads
dir = 'data/02.trimmed_reads'
FileUtils.touch(file_path(dir, '.1.clipped.fastq'))
touch_done(dir)
r = $d.add_result(:trimmed_reads)
assert_equal({}, r[:stats])
r.compute_stats
assert_equal({}, r[:stats])
end

def test_read_quality
dir = 'data/03.read_quality'
Dir.mkdir(file_path(dir, '.solexaqa'))
Dir.mkdir(file_path(dir, '.fastqc'))
touch_done(dir)
r = $d.add_result(:read_quality)
assert_equal({}, r[:stats])
r.compute_stats
assert_equal({}, r[:stats])
end

def test_trimmed_fasta
dir = 'data/04.trimmed_fasta'
fa = file_path(dir, '.CoupledReads.fa')
File.open(fa, 'w') { |fh| fh.puts '>1','ACTAC' }
touch_done(dir)
r = $d.add_result(:trimmed_fasta)
assert_equal({}, r[:stats])
r.compute_stats
assert_equal(1, r[:stats][:reads])
assert_equal([40.0, '%'], r[:stats][:g_c_content])
end

def test_assembly
dir = 'data/05.assembly'
fa = file_path(dir, '.LargeContigs.fna')
File.open(fa, 'w') { |fh| fh.puts '>1','ACTAC' }
touch_done(dir)
r = $d.add_result(:assembly)
assert_equal({}, r[:stats])
r.compute_stats
assert_equal(1, r[:stats][:contigs])
assert_equal([5, 'bp'], r[:stats][:total_length])
assert_equal([40.0, '%'], r[:stats][:g_c_content])
end

def test_cds
dir = 'data/06.cds'
fa = file_path(dir, '.faa')
File.open(fa, 'w') { |fh| fh.puts '>1','M' }
touch_done(dir)
r = $d.add_result(:cds)
assert_equal({}, r[:stats])
r.compute_stats
assert_equal(1, r[:stats][:predicted_proteins])
assert_equal([1.0, 'aa'], r[:stats][:average_length])
assert_nil(r[:stats][:coding_density])
test_assembly
r.compute_stats
assert_equal([60.0, '%'], r[:stats][:coding_density])
end

end
@@ -10,14 +10,14 @@ def setup
FileUtils.touch(File.expand_path('.miga_daemon.json', ENV['MIGA_HOME']))
$p1 = MiGA::Project.new(File.expand_path('project1', $tmp))
$d1 = $p1.add_dataset('dataset1')
FileUtils.touch(File.expand_path(
"data/02.trimmed_reads/#{$d1.name}.1.clipped.fastq", $p1.path))
FileUtils.touch(File.expand_path(
"data/02.trimmed_reads/#{$d1.name}.done", $p1.path))
FileUtils.touch(File.expand_path(
'data/10.clades/01.find/miga-project.empty', $p1.path))
FileUtils.touch(File.expand_path(
'data/10.clades/01.find/miga-project.done', $p1.path))
FileUtils.touch(
File.join($p1.path, "data/02.trimmed_reads/#{$d1.name}.1.clipped.fastq"))
FileUtils.touch(
File.join($p1.path, "data/02.trimmed_reads/#{$d1.name}.done"))
FileUtils.touch(
File.join($p1.path, 'data/10.clades/01.find/miga-project.empty'))
FileUtils.touch(
File.join($p1.path, 'data/10.clades/01.find/miga-project.done'))
end

def teardown
@@ -46,4 +46,18 @@ def test_result_source
assert_equal($p1.path, r.source.path)
end

def test_dates
r = $d1.add_result(:trimmed_reads)
assert_nil(r.done_at)
assert_nil(r.started_at)
tf = File.join($p1.path, "data/02.trimmed_reads/#{$d1.name}.done")
File.open(tf, 'w') { |fh| fh.puts Time.new(1,2,3,4,5) }
assert_equal(Time, r.done_at.class)
assert_nil(r.running_time)
tf = File.join($p1.path, "data/02.trimmed_reads/#{$d1.name}.start")
File.open(tf, 'w') { |fh| fh.puts Time.new(1,2,3,4,0) }
r = $d1.add_result(:trimmed_reads)
assert_equal(5.0, r.running_time)
end

end

0 comments on commit 4bf1909

Please sign in to comment.
You can’t perform that action at this time.