Skip to content

Commit

Permalink
Rescue start and versions on result overwrites
Browse files Browse the repository at this point in the history
  • Loading branch information
lmrodriguezr committed Mar 23, 2024
1 parent 2c6a718 commit 5d36c3c
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 37 deletions.
4 changes: 3 additions & 1 deletion lib/miga/cli/action/doctor/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ def saved_targets(dataset)

##
# Reads all the distance estimates in +a+ -> * for +metric+ and
# returns them as a hash +{"b_name" => [val, sd, ...], ...}+
# returns them as a hash +{"b_name" => [val, sd, ...], ...}+ for
# rows with values other than the metric, or +{"b_name" => val}+ for
# rows with the metric only
def read_bidirectional(a, metric)
db_file = a.result(:distances)&.file_path("#{metric}_db") or return {}
sql = "select seq2, #{metric}, sd, n, omega from #{metric}"
Expand Down
14 changes: 7 additions & 7 deletions lib/miga/cli/action/doctor/distances.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ def check_dist(cli)
#---- Auxuliary functions -----

##
# Make a temporal directory holding partial bidirectionality reports (one per thread)
# in a custom multi-JSON format. Requires a MiGA::Project +project+ and the iterator of
# the reference datasets +ref_ds+. Returns the path to the temporal directory created.
# Used by +check_bidir+
# Make a temporal directory holding partial bidirectionality reports (one per
# thread) in a custom multi-JSON format. Requires a MiGA::Project +project+
# and the iterator of the reference datasets +ref_ds+. Returns the path to the
# temporal directory created. Used by +check_bidir+
def partial_bidir_tmp(project, ref_ds)
n = ref_ds.size

Expand Down Expand Up @@ -106,9 +106,9 @@ def partial_bidir_tmp(project, ref_ds)
end

##
# Read partial temporal reports of bidirectionality (located in +tmp+), and return
# a two-deep hash with the final missingness report by metric (first key) and
# dataset name (second key). Used by +check_bidir+
# Read partial temporal reports of bidirectionality (located in +tmp+), and
# return a two-deep hash with the final missingness report by metric (first
# key) and dataset name (second key). Used by +check_bidir+
def merge_bidir_tmp(tmp)
dist = { aai: {}, ani: {} }
cli[:threads].times do |i|
Expand Down
2 changes: 1 addition & 1 deletion lib/miga/cli/action/doctor/operations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def check_files(cli)
unless ok
cli.say " > Registering again #{d.name}:#{r_k} "
d.add_result(r_k, true, force: true)
sr = d.result(:stats) and sr.remove!
d.result(:stats)&.remove!
end
end
end
Expand Down
22 changes: 17 additions & 5 deletions lib/miga/result.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,26 @@ def load(path)
##
# Check if +path+ describes a result and otherwise create
# it using the passed block. If +force+, ignore existing
# JSON in +path+ if any.
# JSON in +path+ if any, but it can rescue the versions
# field from the old one if it exists and the new one doesn't
# contain such field.
def create(path, force = false)
FileUtils.rm(path) if force && File.exist?(path)
r_pre = load(path)
return r_pre unless r_pre.nil?
# Deal with old results first
r_old = load(path)
return r_old if r_old && !force

# Create the new result using the block passed
FileUtils.rm(path) if r_old
yield
load(path)

# Load and return
load(path).tap do |r_new|
# Rescue versions and start (if any and if necessary)
if r_old
%i[versions started].each { |i| r_new[i] ||= r_old[i] }
r_new[:versions] = (r_old[:versions] || {}).merge(r_new[:versions])
end
end
end
end

Expand Down
4 changes: 2 additions & 2 deletions lib/miga/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ module MiGA
# - String indicating release status:
# - rc* release candidate, not released as gem
# - [0-9]+ stable release, released as gem
VERSION = [1.3, 13, 7].freeze
VERSION = [1.3, 13, 8].freeze

##
# Nickname for the current major.minor version.
VERSION_NAME = 'mezzotint'

##
# Date of the current gem relese.
VERSION_DATE = Date.new(2024, 3, 22)
VERSION_DATE = Date.new(2024, 3, 23)

##
# References of MiGA
Expand Down
8 changes: 4 additions & 4 deletions manual/part2/requirements/conda.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ curl -Lso miga.yml \
conda env create -f miga.yml
rm miga.yml

# Fix conda-forge Ruby issue (see https://github.com/bio-miga/miga/issues/168)
mkdir -p "$GEM_HOME/bin"
ln -s "$CONDA_PREFIX/bin/ruby" "$GEM_HOME/bin/ruby"

# Tell MiGA to activate the proper conda environment
echo 'eval "$(conda shell.bash hook)" && conda activate miga' > ~/.miga_modules

# Activate the environment
. ~/.miga_modules

# Fix conda-forge Ruby issue (see https://github.com/bio-miga/miga/issues/168)
mkdir -p "$GEM_HOME/bin"
ln -s "$CONDA_PREFIX/bin/ruby" "$GEM_HOME/bin/ruby"
```

## MyTaxa utils
Expand Down
34 changes: 17 additions & 17 deletions test/result_stats_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ def test_single_raw_reads
File.open(fq, 'w') { |fh| fh.puts '@1', 'ACTAC', '+', '####' }
touch_done(dir)
r = dataset.add_result(:raw_reads)
assert_equal({}, r[:stats])
assert_equal({}, r.stats)
r.compute_stats
assert_not_empty(r[:stats])
assert_equal(Hash, r[:stats].class)
assert_equal(1, r[:stats][:reads])
assert_equal([40.0, '%'], r[:stats][:g_c_content])
assert_not_empty(r.stats)
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
Expand All @@ -40,20 +40,20 @@ def test_coupled_raw_reads
touch_done(dir)
r = dataset.add_result(:raw_reads)
r.compute_stats
assert_not_empty(r[:stats])
assert_nil(r[:stats][:reads])
assert_equal(1, r[:stats][:read_pairs])
assert_equal([40.0, '%'], r[:stats][:reverse_g_c_content])
assert_not_empty(r.stats)
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 = dataset.add_result(:trimmed_reads)
assert_equal({}, r[:stats])
assert_equal({}, r.stats)
r.compute_stats
assert_equal({}, r[:stats])
assert_equal({}, r.stats)
end

def test_read_quality
Expand All @@ -62,9 +62,9 @@ def test_read_quality
Dir.mkdir(file_path(dir, '.fastqc'))
touch_done(dir)
r = dataset.add_result(:read_quality)
assert_equal({}, r[:stats])
assert_equal({}, r.stats)
r.compute_stats
assert_equal({}, r[:stats])
assert_equal({}, r.stats)
end

def test_trimmed_fasta
Expand Down Expand Up @@ -133,10 +133,10 @@ def test_taxonomy
r = dataset.add_result(:taxonomy)

# Test assertions
assert_nil(r[:stats][:closest_relative])
assert_nil(r.stats[:closest_relative])
r.compute_stats
assert_equal('dad', r[:stats][:closest_relative])
assert_equal([100.0, '%'], r[:stats][:aai])
assert_equal(0.0, r[:stats][:phylum_pvalue])
assert_equal('dad', r.stats[:closest_relative])
assert_equal([100.0, '%'], r.stats[:aai])
assert_equal(0.0, r.stats[:phylum_pvalue])
end
end
24 changes: 24 additions & 0 deletions test/result_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,30 @@ def test_add_result
assert_instance_of(MiGA::Result, r)
end

def test_overwrite_result
r = dataset.add_result(:trimmed_reads)
r[:ephemeral] = '@'
r[:versions] = { you: 'best' }
r.save

# Before reloading
assert_equal('@', r[:ephemeral])
assert_equal('best', r[:versions][:you])
assert_nil(r[:versions][:MiGA])

# After reloading (without forcing)
r = dataset.add_result(:trimmed_reads, true, force: false)
assert_equal('@', r[:ephemeral])
assert_equal('best', r[:versions][:you])
assert_nil(r[:versions][:MiGA])

# After reloading (with forcing)
r = dataset.add_result(:trimmed_reads, true, force: true)
assert_nil(r[:ephemeral])
assert_equal('best', r[:versions][:you])
assert_not_nil(r[:versions][:MiGA])
end

def test_unlink
r = project.add_result(:clade_finding)
path = r.path
Expand Down

0 comments on commit 5d36c3c

Please sign in to comment.