Class: GeneValidator::Output

Inherits:
Object
  • Object
show all
Defined in:
lib/genevalidator/output.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mutex, mutex_yaml, mutex_html, filename, html_path, yaml_path, idx = 0, start_idx = 0) ⇒ Output

Initilizes the object Params: mutex: Mutex for exclusive access to the console mutex_yaml: Mutex for exclusive access to the YAML file mutex_html: Mutex for exclusive access to the HTML file filename: name of the fasta input file html_path: path of the html folder yaml_path: path where the yaml output wil be saved idx: idnex of the current query start_idx: number of the sequence from the file to start with



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/genevalidator/output.rb', line 40

def initialize(mutex, mutex_yaml, mutex_html, filename, html_path,
               yaml_path, idx = 0, start_idx = 0)
  @prediction_len = 0
  @prediction_def = 'no_definition'
  @nr_hits        = 0

  @filename       = filename
  @html_path      = html_path
  @yaml_path      = yaml_path
  @idx            = idx
  @start_idx      = start_idx

  @mutex          = mutex
  @mutex_yaml     = mutex_yaml
  @mutex_html     = mutex_html
end

Instance Attribute Details

#failsObject

Returns the value of attribute fails.



22
23
24
# File 'lib/genevalidator/output.rb', line 22

def fails
  @fails
end

#filenameObject

Returns the value of attribute filename.



15
16
17
# File 'lib/genevalidator/output.rb', line 15

def filename
  @filename
end

#html_pathObject

Returns the value of attribute html_path.



16
17
18
# File 'lib/genevalidator/output.rb', line 16

def html_path
  @html_path
end

#idxObject

Returns the value of attribute idx.



18
19
20
# File 'lib/genevalidator/output.rb', line 18

def idx
  @idx
end

#mutexObject

Returns the value of attribute mutex.



25
26
27
# File 'lib/genevalidator/output.rb', line 25

def mutex
  @mutex
end

#mutex_htmlObject

Returns the value of attribute mutex_html.



27
28
29
# File 'lib/genevalidator/output.rb', line 27

def mutex_html
  @mutex_html
end

#mutex_yamlObject

Returns the value of attribute mutex_yaml.



26
27
28
# File 'lib/genevalidator/output.rb', line 26

def mutex_yaml
  @mutex_yaml
end

#nr_hitsObject

Returns the value of attribute nr_hits.



10
11
12
# File 'lib/genevalidator/output.rb', line 10

def nr_hits
  @nr_hits
end

#overall_scoreObject

Returns the value of attribute overall_score.



21
22
23
# File 'lib/genevalidator/output.rb', line 21

def overall_score
  @overall_score
end

#prediction_defObject

Returns the value of attribute prediction_def.



9
10
11
# File 'lib/genevalidator/output.rb', line 9

def prediction_def
  @prediction_def
end

#prediction_lenObject

Returns the value of attribute prediction_len.



8
9
10
# File 'lib/genevalidator/output.rb', line 8

def prediction_len
  @prediction_len
end

#start_idxObject

Returns the value of attribute start_idx.



19
20
21
# File 'lib/genevalidator/output.rb', line 19

def start_idx
  @start_idx
end

#successesObject

Returns the value of attribute successes.



23
24
25
# File 'lib/genevalidator/output.rb', line 23

def successes
  @successes
end

#validationsObject

list of ValidationReport objects



13
14
15
# File 'lib/genevalidator/output.rb', line 13

def validations
  @validations
end

#yaml_pathObject

Returns the value of attribute yaml_path.



17
18
19
# File 'lib/genevalidator/output.rb', line 17

def yaml_path
  @yaml_path
end

Class Method Details

.overall_evaluation(no_queries, good_scores, bad_scores, no_evidence, no_mafft, no_internet, map_errors, running_times) ⇒ Object

Calculates an overall evaluation of the output Params: all_query_outputs: Array of ValidationTest objects Output Array of Strigs with the reports



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/genevalidator/output.rb', line 228

def self.overall_evaluation(no_queries, good_scores, bad_scores,
                            no_evidence, no_mafft, no_internet, map_errors,
                            running_times)
  good_pred = (good_scores == 1) ? 'One' : "#{good_scores} are"
  bad_pred  = (bad_scores == 1) ? 'One' : "#{bad_scores} are"

  eval = "Overall Query Score Evaluation:\n" \
         "#{no_queries} predictions were validated, from which there" \
         " were:\n" \
         "#{good_pred} good prediction(s),\n" \
         "#{bad_pred} possibly weak prediction(s).\n"

  if no_evidence != 0
    eval << "#{no_evidence} could not be evaluated due to the lack of" \
            ' evidence.'
  end

  # errors per validation
  error_eval = ''
  map_errors.each do |k, v|
    error_eval << "\nWe couldn't run #{k} Validation for #{v} queries"
  end

  if no_mafft >= (no_queries - no_evidence)
    error_eval << "\nWe couldn't run MAFFT multiple alignment"
  end
  if no_internet >= (no_queries - no_evidence)
    error_eval << "\nWe couldn't make use of your internet connection"
  end

  time_eval = ''
  running_times.each do |key, value|
    average_time = value.x / (value.y + 0.0)
    time_eval << "\nAverage running time for #{key} Validation:" \
                 " #{average_time.round(3)}s per validation"
  end

  overall_evaluation = [eval, error_eval, time_eval]
  overall_evaluation.select { |e| e != '' }
end

Method that closes the gas in the html file and writes the overall evaluation Param: all_query_outputs: array with ValidationTest objects html_path: path of the html folder filemane: name of the fasta input file def self.print_footer(all_query_outputs, html_path, filename)



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/genevalidator/output.rb', line 162

def self.print_footer(no_queries, scores, good_predictions, bad_predictions,
                      nee, no_mafft, no_internet, map_errors, running_times,
                      html_path, filename)
  # compute the statistics
  # overall_evaluation = overall_evaluation(all_query_outputs, filename)
  overall_evaluation = overall_evaluation(no_queries, good_predictions,
                                          bad_predictions, nee, no_mafft,
                                          no_internet, map_errors,
                                          running_times)

  less = overall_evaluation[0]
  less = less.gsub("\n", '<br>').gsub("'", %q(\\\'))

  # print to console
  evaluation = ''
  overall_evaluation.each { |e| evaluation << "\n#{e}" }
  puts evaluation
  puts ''

  # print to html
  # make the historgram with the resulted scores
  statistics_filename = "#{html_path}/files/json/#{filename}_statistics.json"
  f = File.open(statistics_filename, 'w')

  f.write(
    [scores.group_by { |a| a }.map { |k, vs| { 'key' => k,
                                               'value' => vs.length,
                                               'main' => false } }].to_json)
  f.close

  plot_statistics = Plot.new("files/json/#{filename}_statistics.json",
                             :simplebars,
                             'Overall evaluation',
                             '',
                             'validation score',
                             'number of queries',
                             10)

  evaluation = evaluation.gsub("\n", '<br>').gsub("'", %q(\\\'))

  index_file = "#{html_path}/results.html"
  table_file = "#{html_path}/files/table.html"
  aux_dir = File.join(File.dirname(File.expand_path(__FILE__)), '../../aux')

  template_footer     = File.join(aux_dir, 'template_footer.erb')
  app_template_footer = File.join(aux_dir, 'app_template_footer.erb')

  template_file = File.open(template_footer, 'r').read
  erb = ERB.new(template_file, 0, '>')
  File.open(index_file, 'a+') do |file|
    file.write(erb.result(binding))
  end

  table_footer_template = File.open(app_template_footer, 'r').read
  table_erb = ERB.new(table_footer_template, 0, '>')
  File.open(table_file, 'a+') do |file|
    file.write(table_erb.result(binding))
  end
end

Instance Method Details

#generate_htmlObject



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/genevalidator/output.rb', line 103

def generate_html
  if @fails == 0
    bg_icon = 'success'
  else
    bg_icon = 'danger'
  end

  index_file = "#{@html_path}/results.html"
  table_file = "#{@html_path}/files/table.html"

  aux_dir = File.join(File.dirname(File.expand_path(__FILE__)), '../../aux')

  # if it's the first time I write in the html file
  if @idx == @start_idx
    @mutex_html.synchronize do
      template_header     = File.join(aux_dir, 'template_header.erb')
      template_file       = File.open(template_header, 'r').read
      erb                 = ERB.new(template_file, 0, '>')

      #  Creating a Separate output file for the web app
      app_template_header = File.join(aux_dir, 'app_template_header.erb')
      table_template_file = File.open(app_template_header, 'r').read
      erb_table           = ERB.new(table_template_file, 0, '>')

      File.open(index_file, 'w+') do |file|
        file.write(erb.result(binding))
      end

      File.open(table_file, 'w+') do |file|
        file.write(erb_table.result(binding))
      end
    end
  end

  toggle = "toggle#{@idx}"

  @mutex_yaml.synchronize do
    template_query = File.join(aux_dir, 'template_query.erb')
    template_file = File.open(template_query, 'r').read
    erb = ERB.new(template_file, 0, '>')

    File.open(index_file, 'a') do |file|
      file.write(erb.result(binding))
    end

    File.open(table_file, 'a') do |file|
      file.write(erb.result(binding))
    end
  end
end


57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/genevalidator/output.rb', line 57

def print_output_console
  if @idx == @start_idx
    header = sprintf('%3s|%s|%20s|%5s', 'No', 'Score', 'Identifier',
                     'No_Hits')
    validations.map do |v|
      header << "|#{v.short_header}"
    end
    puts header
  end

  short_def          = @prediction_def.scan(/([^ ]+)/)[0][0]
  validation_outputs = validations.map(&:print)

  output             = sprintf('%3s|%d|%20s|%5s|', @idx, @overall_score,
                               short_def, @nr_hits)
  validation_outputs.each do |item|
    output << item
    output << '|'
  end

  @mutex.synchronize do
    puts output.gsub('&nbsp;', ' ')
  end
end


82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/genevalidator/output.rb', line 82

def print_output_file_yaml
  file_yaml = "#{@yaml_path}/#{@filename}.yaml"
  report = validations
  if @idx == @start_idx
    @mutex_yaml.synchronize do
      File.open(file_yaml, 'w') do |f|
        YAML.dump({ @prediction_def.scan(/([^ ]+)/)[0][0] => report }, f)
      end
    end
  else
    @mutex_yaml.synchronize do
      hash = {} # YAML.load_file(file_yaml)
      hash[@prediction_def.scan(/([^ ]+)/)[0][0]] = report
      File.open(file_yaml, 'a') do |f|
        new_report =  hash.to_yaml
        f.write(new_report[4..new_report.length - 1])
      end
    end
  end
end