Class: GeneValidator::JsonToGVResults

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/genevalidator/json_to_gv_results.rb

Overview

produce GV results from a JSON previously produced from GV

Class Method Summary collapse

Class Method Details

.calculate_no_quries_with_no_evidenceObject

calculate number of queries that had warnings for all validations.



138
139
140
141
142
143
144
145
146
147
# File 'lib/genevalidator/json_to_gv_results.rb', line 138

def calculate_no_quries_with_no_evidence
  all_warnings = 0
  @json_array.each do |row|
    status = row['validations'].map { |_, h| h['status'] }
    if status.count { |r| r == 'warning' } == status.length
      all_warnings += 1
    end
  end
  all_warnings
end

.calculate_overall_scoreObject



110
111
112
113
114
115
116
# File 'lib/genevalidator/json_to_gv_results.rb', line 110

def calculate_overall_score
  scores = []
  @json_array.each { |row| scores << row['overall_score'] }
  plot_dir = File.join(@config[:html_path], 'files/json')
  less     =  generate_evaluation(scores)
  Output.create_overview_json(scores, plot_dir, less, less)
end

.create_json_file(row) ⇒ Object



49
50
51
52
53
# File 'lib/genevalidator/json_to_gv_results.rb', line 49

def create_json_file(row)
  @json_file = File.join(@config[:plot_dir],
                         "#{@config[:filename]}_#{row['idx']}.json")
  File.open(@json_file, 'w') { |f| f.write(row.to_json) }
end

.generate_evaluation(scores) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/genevalidator/json_to_gv_results.rb', line 118

def generate_evaluation(scores)
  no_of_queries = scores.length
  good_scores = scores.count { |s| s >= 75 }
  bad_scores  = scores.count { |s| s < 75 }
  nee         = calculate_no_quries_with_no_evidence # nee = no evidence

  good_pred = (good_scores == 1) ? 'One' : "#{good_scores} are"
  bad_pred  = (bad_scores == 1) ? 'One' : "#{bad_scores} are"
  eval = 'Overall Query Score Evaluation:<br>' \
         "#{no_of_queries} predictions were validated, from which there" \
         ' were:<br>' \
         "#{good_pred} good prediction(s),<br>" \
         "#{bad_pred} possibly weak prediction(s).<br>"
  return eval if nee == 0
  eval << "#{nee} could not be evaluated due to the lack of" \
          ' evidence.<br>'
  eval
end

.generate_html_header(output_html) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/genevalidator/json_to_gv_results.rb', line 60

def generate_html_header(output_html)
  return if File.exist?(output_html)
  json_header_template = File.join(@config[:aux], 'json_header.erb')
  template_contents    = File.open(json_header_template, 'r').read
  erb                  = ERB.new(template_contents, 0, '>')
  File.open(output_html, 'w+') { |f| f.write(erb.result(binding)) }
end

.generate_html_query(output_html, row) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/genevalidator/json_to_gv_results.rb', line 68

def generate_html_query(output_html, row)
  @row = row
  json_query_template = File.join(@config[:aux], 'json_query.erb')
  template_contents   = File.open(json_query_template, 'r').read
  erb                 = ERB.new(template_contents, 0, '>')
  File.open(output_html, 'a') { |f| f.write(erb.result(binding)) }
end

Add footer to all output files



77
78
79
80
81
82
83
84
# File 'lib/genevalidator/json_to_gv_results.rb', line 77

def html_footer
  no_of_output_files = (@config[:run_no].to_f / @config[:output_max]).ceil

  output_files = []
  (1..no_of_output_files).each { |i| output_files << "results#{i}.html" }

  write_html_footer(no_of_output_files, output_files)
end

.initObject



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/genevalidator/json_to_gv_results.rb', line 17

def init
  @opt    = opt
  @config = {
    html_path: "#{@opt[:json_file]}.html",
    plot_dir: "#{@opt[:json_file]}.html/files/json",
    aux: File.expand_path(File.join(File.dirname(__FILE__), '../../aux')),
    filename: File.basename(@opt[:json_file]),
    output_max: 2500,
    run_no: 0
  }
  @json_array = load_json_file
end

.load_json_fileObject



44
45
46
47
# File 'lib/genevalidator/json_to_gv_results.rb', line 44

def load_json_file
  json_contents = File.read(File.expand_path(@opt[:json_file]))
  JSON.load(json_contents)
end

.output_filenameObject



55
56
57
58
# File 'lib/genevalidator/json_to_gv_results.rb', line 55

def output_filename
  i = (@config[:run_no].to_f / @config[:output_max]).ceil
  File.join(@config[:html_path], "results#{i}.html")
end

.runObject



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/genevalidator/json_to_gv_results.rb', line 30

def run
  init
  GeneValidator.create_output_folder(@config[:html_path], @config[:aux])
  @json_array.each do |row|
    @config[:run_no] += 1
    create_json_file(row)
    output_html = output_filename
    generate_html_header(output_html) unless File.exist?(output_html)
    generate_html_query(output_html, row)
  end
  html_footer
  calculate_overall_score
end

.turn_off_automated_sortingObject

By default, on page load, the results are automatically sorted by the index. However since the whole idea is that users would sort by JSON, this is not wanted here.



100
101
102
103
104
105
106
107
108
# File 'lib/genevalidator/json_to_gv_results.rb', line 100

def turn_off_automated_sorting
  script_file = File.join(@config[:html_path],
                          'files/js/genevalidator.compiled.min.js')
  original_content = File.read(script_file)
  # removes the automatic sort on page load
  updated_content = original_content.gsub(',sortList:[[0,0]]', '')
  File.open("#{script_file}.tmp", 'w') { |f| f.puts updated_content }
  FileUtils.mv("#{script_file}.tmp", script_file)
end


86
87
88
89
90
91
92
93
94
95
# File 'lib/genevalidator/json_to_gv_results.rb', line 86

def write_html_footer(no_of_output_files, output_files)
  turn_off_automated_sorting
  json_footer_template = File.join(@config[:aux], 'json_footer.erb')
  template_contents    = File.open(json_footer_template, 'r').read
  erb                  = ERB.new(template_contents, 0, '>')
  (1..no_of_output_files).each do |i|
    results_html = File.join(@config[:html_path], "results#{i}.html")
    File.open(results_html, 'a+') { |f| f.write(erb.result(binding)) }
  end
end