<commit_before>#!/usr/bin/env ruby

# @package MiGA
# @license Artistic-2.0

o = {q:true}
opts = OptionParser.new do |opt|
  opt_banner(opt)
  opt_object(opt, o, [:project, :dataset_opt, :result])
  opt.on("--compute-and-save",
    "Computes and saves the statistics."){ |v| o[:compute] = v }
  opt_common(opt, o)
end.parse!

##=> Main <=
opts.parse!
opt_require(o, project:"-P", name:"-r")

$stderr.puts "Loading project." unless o[:q]
p = MiGA::Project.load(o[:project])
raise "Impossible to load project: #{o[:project]}" if p.nil?

$stderr.puts "Loading result." unless o[:q]
if o[:dataset].nil?
  r = p.add_result(o[:name], false)
else
  d = p.dataset(o[:dataset])
  r = d.add_result(o[:name], false)
end
raise "Cannot load result." if r.nil?

if o[:compute]
  $stderr.puts "Computing statistics." unless o[:q]
  stats = {}
  case o[:name]
  when :raw_reads
    scr = "awk 'NR%4==2{L+=length($0)} END{print NR/4, L*4/NR}'"
    if r[:files][:pair1].nil?
      s = `#{scr} '#{r.file_path :single}'`.chomp.split(" ")
      stats = {reads: s[0].to_i, average_length: [s[1].to_f, "bp"]}
    else
      s1 = `#{scr} '#{r.file_path :pair1}'`.chomp.split(" ")
      s2 = `#{scr} '#{r.file_path :pair2}'`.chomp.split(" ")
      stats = {read_pairs: s1[0].to_i,
        average_length_forward: [s1[1].to_f, "bp"],
        average_length_reverse: [s2[1].to_f, "bp"]}
    end
  when :trimmed_fasta
    scr = "awk '{L+=$2} END{print NR, L/NR}'"<commit_msg>New option --try-load in CLI `miga result_stats`<commit_after>#!/usr/bin/env ruby

# @package MiGA
# @license Artistic-2.0

o = {q:true, try_load:false}
opts = OptionParser.new do |opt|
  opt_banner(opt)
  opt_object(opt, o, [:project, :dataset_opt, :result])
  opt.on("--compute-and-save",
    "Computes and saves the statistics."){ |v| o[:compute] = v }
  opt.on("--try-load",
    "Checks if stat exists instead of computing on --compute-and-save."
    ){ |v| o[:try_load] = v }
  opt_common(opt, o)
end.parse!

##=> Main <=
opts.parse!
opt_require(o, project:"-P", name:"-r")

$stderr.puts "Loading project." unless o[:q]
p = MiGA::Project.load(o[:project])
raise "Impossible to load project: #{o[:project]}" if p.nil?

$stderr.puts "Loading result." unless o[:q]
if o[:dataset].nil?
  r = p.add_result(o[:name], false)
else
  d = p.dataset(o[:dataset])
  r = d.add_result(o[:name], false)
end
raise "Cannot load result." if r.nil?

o[:compute] = false if o[:try_load] and
  (not r[:stats].nil?) and (not r[:stats].empty?)

if o[:compute]
  $stderr.puts "Computing statistics." unless o[:q]
  stats = {}
  case o[:name]
  when :raw_reads
    scr = "awk 'NR%4==2{L+=length($0)} END{print NR/4, L*4/NR}'"
    if r[:files][:pair1].nil?
      s = `#{scr} '#{r.file_path :single}'`.chomp.split(" ")
      stats = {reads: s[0].to_i, average_length: [s[1].to_f, "bp"]}
    else
      s1 = `#{scr} '#{r.file_path :pair1}'`.chomp.split(" ")
      s2 = `#{scr} '#{r.file_path :pair2}'`.chomp.split(" ")
      stats = {read_pairs: s1[0].to_i,
        average_length_forward: [s1[1].to_f, "bp"],
        average_length_reverse: [s2[1].to_f, "bp"]}
    end
  when :trimmed_fasta
    scr = "awk '{L+=$2} END{print NR, L/NR}'"
{'fraction_non_alphanumeric': 0.21448908573236317, 'fraction_numerical': 0.013286934514394179, 'mean_word_length': 3.7620481927710845, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2846728', 'n_tokens_mistral': 1302, 'n_tokens_neox': 1294, 'n_words': 291}