Class: 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

- (Output) initialize(filename, html_path, yaml_path, idx = 0, start_idx = 0)

Initilizes the object Params: 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



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/genevalidator/output.rb', line 28

def initialize(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

end

Instance Attribute Details

- (Object) filename

Returns the value of attribute filename



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

def filename
  @filename
end

- (Object) html_path

Returns the value of attribute html_path



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

def html_path
  @html_path
end

- (Object) idx

Returns the value of attribute idx



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

def idx
  @idx
end

- (Object) nr_hits

Returns the value of attribute nr_hits



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

def nr_hits
  @nr_hits
end

- (Object) prediction_def

Returns the value of attribute prediction_def



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

def prediction_def
  @prediction_def
end

- (Object) prediction_len

Returns the value of attribute prediction_len



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

def prediction_len
  @prediction_len
end

- (Object) start_idx

Returns the value of attribute start_idx



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

def start_idx
  @start_idx
end

- (Object) validations

list of ValidationReport objects



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

def validations
  @validations
end

- (Object) yaml_path

Returns the value of attribute yaml_path



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

def yaml_path
  @yaml_path
end

Class Method Details

+ (Object) overall_evaluation(all_query_outputs)

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



148
149
150
151
152
153
154
155
156
157
158
159
160
161
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
221
222
223
224
225
226
227
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
# File 'lib/genevalidator/output.rb', line 148

def self.overall_evaluation(all_query_outputs)
    score_evaluation = ""
    score_evaluation << "Query score evaluation:"
    
    # count the cases of "not enough evidence"
    no_evidence = all_query_outputs.count{|report|
      report.validations.count{|v| v.validation_report.result == :unapplicable or v.validation_report.result == :warning} == report.validations.length
    }  

    # print at the console
    scores = []
    no_mafft = 0
    no_internet = 0

    # how many genes are good
    all_query_outputs.each do |report| 
      successes = report.validations.map{|v| v.validation_report.result == v.validation_report.expected}.count(true)
      fails = report.validations.map{|v| v.validation_report.validation != :unapplicable and v.validation_report.validation != :error and
        v.validation_report.result != v.validation_report.expected}.count(true)
      overall_score = (successes*100/(successes + fails + 0.0)).round(0)
      scores.push overall_score

      report.validations.each do |v| 
        if v.validation_report.errors != nil
          no_mafft += v.validation_report.errors.select{|e| e == NoMafftInstallationError}.length
        end
      end

      report.validations.each do |v| 
        if v.validation_report.errors != nil
          no_internet += v.validation_report.errors.select{|e| e == NoInternetError}.length
        end
      end
    end

    good_scores = scores.count{|v| v > 75}
    bad_scores = scores.length - good_scores 

    score_evaluation << "\nThere were validated #{all_query_outputs.length} predictions from which:"
    if good_scores == 1
      score_evaluation << "\nOne good prediction"
    else
      score_evaluation << "\n#{good_scores} are good predictions"
    end
    if bad_scores == 1
      score_evaluation << "\nOne possibly weak prediction"
    else
      score_evaluation << "\n#{bad_scores} are possibly weak predictions"
    end

    if no_evidence != 0
      score_evaluation << "\n#{no_evidence} of them couldn't be evaluated because of low evidence"
    end

    error_evaluation = ""
    # errors per validation
    validations = all_query_outputs[0].validations
    validations.each_with_index do |v,i|
      no_errors = 0
      all_query_outputs.each do |report|
        if report.validations[i].validation_report.validation == :error
          no_errors += 1
        end
      end
      if no_errors != 0
        error_evaluation <<  "\nWe couldn't run #{v.short_header} Validation for #{no_errors} queries"
      end
    end

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

    # Running time statistics
    running_times = {}
    all_query_outputs[0].validations.each do |v|
      running_times[v.short_header] = []
    end

    all_query_outputs.each do |output|
      output.validations.each do |v|
        if v.running_time != 0 and 
           v.running_time != nil and 
           v.validation_report.validation != :unapplicable and
           v.validation_report.validation != :error
          running_times[v.short_header].push v.running_time
        end
      end
    end

    time_evaluation = ""
    time_evaluation << "\nRunning Time:"
    running_times = running_times.select{|k,v| v.length!=0}
    running_times.each do |key, array|
      average_time = array.inject{ |sum, el| sum + el }.to_f / array.size
      time_evaluation << "\nAverage running time for #{key} Validation: #{average_time.round(3)}s per validation"
    end

    overall_evaluation = [score_evaluation, error_evaluation, time_evaluation]
    overall_evaluation = overall_evaluation.select{|e| e!=""}
    return overall_evaluation
end

Class 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



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/genevalidator/output.rb', line 126

def self.print_footer(all_query_outputs, html_path)
  overall_evaluation = overall_evaluation(all_query_outputs)
  # print to console
  evaluation = ""
  overall_evaluation.each{|e| evaluation << "\n#{e}"}
  puts evaluation
  puts ""
  evaluation = evaluation.gsub("\n","<br>")

  # print to html
  index_file = "#{html_path}/index.html"
  template_file = File.open("aux/template_footer.htm.erb", 'r').read
  erb = ERB.new(template_file)
  File.open(index_file, 'a+') { |file| file.write(erb.result(binding)) }
end

Instance Method Details

- (Object) generate_html



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/genevalidator/output.rb', line 88

def generate_html

  successes = validations.map{|v| v.validation_report.result == 
    v.validation_report.expected}.count(true)
  fails = validations.map{|v| v.validation_report.validation != :unapplicable and
    v.validation_report.validation != :error and 
    v.validation_report.result != v.validation_report.expected}.count(true)
  unknown = validations.length - successes - fails
  overall_score = (successes*100/(successes + fails + 0.0)).round(0)

  if fails == 0
    bg_icon = "success"
  else
    bg_icon = "danger"
  end

  index_file = "#{@html_path}/index.html"

  # if it's the first time I write in the html file
  if @idx == @start_idx
    template_file = File.open("aux/template_header.htm.erb", 'r').read
    erb = ERB.new(template_file)
    File.open(index_file, 'w+') { |file| file.write(erb.result(binding)) }      
  end

  toggle = "toggle#{@idx}"

  template_file = File.open("aux/template_query.htm.erb", 'r').read
  erb = ERB.new(template_file)

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


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/genevalidator/output.rb', line 43

def print_output_console

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

  short_def = @prediction_def.scan(/([^ ]+)/)[0][0]
  short_def = short_def[0..[20,short_def.length].min]
  validation_outputs = validations.map{|v| v.validation_report.print}

  output = sprintf("%3s|%20s|%5s|", @idx, short_def, @nr_hits)
  validation_outputs.each do |item|
    item_padd = sprintf("%17s", item);
    output << item
    output << "|"
  end

  puts output

end


69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/genevalidator/output.rb', line 69

def print_output_file_yaml

  file_yaml = "#{@yaml_path}/#{@filename}.yaml"
  report = validations.map{|v| v.validation_report}
  unless @idx == @start_idx
    hsh = YAML.load_file(file_yaml)
    hsh[@prediction_def.scan(/([^ ]+)/)[0][0]] = report
    File.open(file_yaml, "w") do |f|
      YAML.dump(hsh, f)
    end
  else 
    File.open(file_yaml, "w") do |f|
      YAML.dump({@prediction_def.scan(/([^ ]+)/)[0][0] => report},f)
    end
  end

end