Class: SeqtrimWorkManager

Inherits:
ScbiMapreduce::WorkManager
  • Object
show all
Defined in:
lib/seqtrimnext/classes/em_classes/seqtrim_work_manager.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.end_work_managerObject



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
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
153
154
155
156
# File 'lib/seqtrimnext/classes/em_classes/seqtrim_work_manager.rb', line 88

def self.end_work_manager

  puts "FULL STATS:\n" +JSON.pretty_generate(@@full_stats)

  f = File.open(STATS_PATH,'w')
  f.puts JSON.pretty_generate(@@full_stats)
  f.close

  r=File.read(STATS_PATH)

  stats=JSON::parse(r)


  gs=GraphStats.new(stats)

  #gs=GraphStats.new(@@full_stats)


  #close all files

  # @@fqr.close
  if @@use_json
    @@json_output.close
  end
  @@errors_file.close
  # @@rejected_output_file.close

  # @@paired_output_files.each do |k,file|
  #   file.close
  # end

  @@files.each do |k,file|
    file.close
  end

  # @@paired_qual_output_files.each do |k,file|
  #            file.close
  # end

  # @@sequences_output_files.each do |k,file|
  #   file.close
  # end
  #
  # @@low_complexity_output_files.each do |k,file|
  #   file.close
  # end
  #
  # @@sffinfo_files.each do |k,file|
  #   file.close
  # end
  #
  # @@low_sffinfo_files.each do |k,file|
  #   file.close
  # end


  # @@qual_output_files.each do |k,file|
  #  file.close
  # end

  # more than one MID found
  # if @@full_stats['mid_id'] && @@full_stats['mid_id'].count>1
  #
  # end

  if File.exists?('scbi_drb_checkpoint')
    File.delete('scbi_drb_checkpoint')
  end
end

.init_work_manager(sequence_reader, params, chunk_size = 100, use_json = false) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/seqtrimnext/classes/em_classes/seqtrim_work_manager.rb', line 16

def self.init_work_manager(sequence_reader, params, chunk_size = 100, use_json=false)
  @@full_stats={}
  @@params= params
  @@exit = false

  @@chunk_size = chunk_size


  # puts "CHECKPOINT: #{self.checkpoint}\n"*20

  checkpoint_exists=File.exists?('scbi_drb_checkpoint')

  # @@use_qual = !qual_path.nil? and File.exists?(qual_path)
  @@open_mode='w'
  if checkpoint_exists
    @@open_mode = 'a'
    if File.exists?(STATS_PATH)
      # load stats
      text = File.read(STATS_PATH)

      # wipe text
      # text=text.grep(/^\s*[^#]/).to_s

      # decode json
      @@full_stats = JSON.parse(text)
    end
  end

  #open input file
  @@fqr=sequence_reader

  # @@use_qual = @@fqr.with_qual?
  # @@use_json = use_json

  @@params.set_param('use_qual',@@fqr.with_qual?)
  @@params.set_param('use_json',use_json)

  @@use_json=use_json

  @@fqr.rewind

  # open output files

  if !Dir.exists?(OUTPUT_PATH)
    Dir.mkdir(OUTPUT_PATH)
  end

  @@files={}

  # @@rejected_output_file=File.open(File.join(OUTPUT_PATH,'rejected.txt'),@@open_mode)

  # seqs_with_errors
  @@errors_file=File.open('errors.txt',@@open_mode)

  if @@use_json
    @@json_output=File.open('results.json',@@open_mode)
  end

  @@json_separator=''

  @@paired_output_files={}

  @@sequences_output_files={}

  @@low_complexity_output_files={}

  @@sffinfo_files={}

  @@low_sffinfo_files={}

end

Instance Method Details

#error_received(worker_error, obj) ⇒ Object



159
160
161
162
# File 'lib/seqtrimnext/classes/em_classes/seqtrim_work_manager.rb', line 159

def error_received(worker_error, obj)
  @@errors_file.puts "Error while processing object #{obj.inspect}\n" + worker_error.original_exception.message + ":\n" +worker_error.original_exception.backtrace.join("\n")
  @@errors_file.puts "="*60
end

#get_file(file_name) ⇒ Object



313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/seqtrimnext/classes/em_classes/seqtrim_work_manager.rb', line 313

def get_file(file_name)
  res_file = @@files[file_name]

  # if file is not already open, create it
  if res_file.nil?
    # create dir if necessary
    dir = File.dirname(file_name)
    if !File.exists?(dir)
      FileUtils.mkdir_p(dir)
    end

    # open file
    res_file=File.open(file_name,@@open_mode)

    # save it in hash for next use
    @@files[file_name]=res_file
  end

  return res_file
end

#load_user_checkpoint(checkpoint) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/seqtrimnext/classes/em_classes/seqtrim_work_manager.rb', line 172

def load_user_checkpoint(checkpoint)

  # reset count stats since they are repeated by checkpointing

  # if @@full_stats['sequences'] && @@full_stats['repeated']
  #   @@full_stats['sequences']['count']['repeated']=0
  # end
  #
  # if @@full_stats['sequences'] && @@full_stats['processed']
  #   @@full_stats['sequences']['processed']['count']=0
  # end
  #
  # if @@full_stats['sequences'] && @@full_stats['total']
  #   @@full_stats['sequences']['total']['count']=0
  # end

  super
  # return checkpoint
end

#next_workObject



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
# File 'lib/seqtrimnext/classes/em_classes/seqtrim_work_manager.rb', line 216

def next_work

  if @@exit
    return nil
  end

  begin

    n,f,q,c = @@fqr.next_seq

    if !n.nil? && @@params.repeated_seq?(n)
      @@full_stats.add_stats({'sequences' => {'count' => {'rejected' => 1}}})
      @@full_stats.add_stats({'sequences' => {'rejected' => {'repeated' => 1}}})

      get_file(File.join(OUTPUT_PATH,'rejected.txt')).puts('>'+n+ ' repeated')
      
    end
    
    if !n.nil?
      @@full_stats.add_stats({'sequences' => {'count' => {'input_count' => 1}}})
    end
  end while (!n.nil? && @@params.repeated_seq?(n))

  if !n.nil?
    return SequenceWithAction.new(n,f.upcase,q,c)
  else
    return nil
  end


end

#save_files(obj) ⇒ Object



305
306
307
308
309
310
311
# File 'lib/seqtrimnext/classes/em_classes/seqtrim_work_manager.rb', line 305

def save_files(obj)
  files=obj.output_files
  files.each do |file_name,content|
    f=get_file(file_name)
    f.puts content
  end
end

#save_user_checkpointObject



192
193
194
195
196
197
198
# File 'lib/seqtrimnext/classes/em_classes/seqtrim_work_manager.rb', line 192

def save_user_checkpoint

  f = File.open(STATS_PATH,'w')
  f.puts JSON.pretty_generate(@@full_stats)
  f.close

end

#too_many_errors_receivedObject



164
165
166
# File 'lib/seqtrimnext/classes/em_classes/seqtrim_work_manager.rb', line 164

def too_many_errors_received
  $LOG.error "Too many errors: #{@@error_count} errors on #{@@count} executed sequences, exiting before finishing"
end

#trash_checkpointed_workObject

read a work that will not be processed, only to skip until checkpoint



202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/seqtrimnext/classes/em_classes/seqtrim_work_manager.rb', line 202

def trash_checkpointed_work

  @@chunk_size.times do
    begin
      n,f,q,c = @@fqr.next_seq
    end while (!n.nil? && @@params.repeated_seq?(n))

    if n.nil?
      break
    end
  end

end

#work_received(obj) ⇒ Object

def next_work

  if @@exit
    return nil
  end
  group = SequenceGroup.new

  @@chunk_size.times do
    begin

      n,f,q,c = @@fqr.next_seq

      if !n.nil? && @@params.repeated_seq?(n)
        @@full_stats.add_stats({'sequences' => {'count' => {'rejected' => 1}}})
        @@full_stats.add_stats({'sequences' => {'rejected' => {'repeated' => 1}}})

        get_file(File.join(OUTPUT_PATH,'rejected.txt')).puts('>'+n+ ' repeated')

      end
      if !n.nil?
        @@full_stats.add_stats({'sequences' => {'count' => {'input_count' => 1}}})
      end
    end while (!n.nil? && @@params.repeated_seq?(n))

    if !n.nil?
      # @@full_stats.add_stats({'sequences' => {'count' => {'processed' => 1}}})
      group.push SequenceWithAction.new(n,f.upcase,q,c)
    else
      break
    end
  end

  # puts "Processing #{group.inspect}"

  if group.empty?
    return nil
  else
    return group
  end

end


290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/seqtrimnext/classes/em_classes/seqtrim_work_manager.rb', line 290

def work_received(obj)

  res = obj

  # collect stats
  @@full_stats.add_stats(obj.stats)

  # print output in screen
  puts obj.output_text

  # save results to files
  save_files(obj)

end

#worker_initial_configObject



168
169
170
# File 'lib/seqtrimnext/classes/em_classes/seqtrim_work_manager.rb', line 168

def worker_initial_config
  return @@params
end