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
# 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)

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

  # if initial files doesn't exists, create it
  if !File.exists?(File.join(OUTPUT_PATH,'initial_stats.json'))
    File.open(File.join(OUTPUT_PATH,'initial_stats.json'),'w') do |f|
      f.puts JSON.pretty_generate(@@ongoing_stats)
    end
  end

  # load stats
  r=File.read(STATS_PATH)
  stats=JSON::parse(r)



  # make graphs
  gs=GraphStats.new(stats)

  #close all files
  if @@use_json
    @@json_output.close
  end
  @@errors_file.close

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

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

.init_work_manager(sequence_readers, params, chunk_size = 100, use_json = false, skip_output = false, write_in_gzip = true) ⇒ 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_readers, params, chunk_size = 100, use_json=false, skip_output=false, write_in_gzip=true)
  @@full_stats={}
  @@params= params
  @@exit = false

  @@ongoing_stats={}
  @@ongoing_stats[:sequence_count] = 0
  @@ongoing_stats[:smallest_sequence_size] = 900000000000000
  @@ongoing_stats[:biggest_sequence_size] = 0

  @@skip_output=skip_output
  @@write_in_gzip=write_in_gzip

  @@chunk_size = chunk_size

  checkpoint_exists=File.exists?(ScbiMapreduce::CHECKPOINT_FILE)

  # @@use_qual = !qual_path.nil? and File.exists?(qual_path)
  @@open_mode='w'
  if checkpoint_exists
    @@open_mode = 'a'
  end

  #open input file
  @@sequence_readers=sequence_readers

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

  @@params.set_param('use_qual',@@sequence_readers.first.with_qual?)
  @@params.set_param('use_json',use_json)
  @@params.set_param('tuple_size',@@sequence_readers.count)

  @@use_json=use_json

  @@sequence_readers.each do |sequence_reader|
    sequence_reader.rewind
  end

  # 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={}

  @@tuple_id=0

end

Instance Method Details

#error_received(worker_error, obj) ⇒ Object



129
130
131
132
# File 'lib/seqtrimnext/classes/em_classes/seqtrim_work_manager.rb', line 129

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



328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'lib/seqtrimnext/classes/em_classes/seqtrim_work_manager.rb', line 328

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
    if @@write_in_gzip && file_name.upcase.index('.FASTQ')
      file=File.open(file_name+'.gz',@@open_mode)
      res_file=Zlib::GzipWriter.new(file)
    else
      res_file=File.open(file_name,@@open_mode)
    end

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

  return res_file
end

#get_next_seq_from_file(file) ⇒ Object



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/seqtrimnext/classes/em_classes/seqtrim_work_manager.rb', line 226

def get_next_seq_from_file(file)
  # find a valid and no repeated sequence in file
  begin

    n,f,q,c = file.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?
      @@ongoing_stats[:sequence_count] += 1
      @@ongoing_stats[:smallest_sequence_size] = [f.size, @@ongoing_stats[:smallest_sequence_size]].min
      @@ongoing_stats[:biggest_sequence_size] = [f.size, @@ongoing_stats[:smallest_sequence_size]].max

      @@full_stats.add_stats({'sequences' => {'count' => {'input_count' => 1}}})
    end

  end while (!n.nil? && @@params.repeated_seq?(n))

  return n,f,q,c

end

#load_user_checkpoint(checkpoint) ⇒ Object



142
143
144
145
146
147
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
# File 'lib/seqtrimnext/classes/em_classes/seqtrim_work_manager.rb', line 142

def load_user_checkpoint(checkpoint)
  # load full_stats from file !!!!!!!!!!!!!

  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

  # reset count stats since they are repeated by checkpointing

  # {
  #   "sequences": {
  #   "count": {
  #     "input_count": 1600,
  #     "output_seqs": 933,
  #     "rejected": 67
  #   },
  #   "rejected": {
  #     "short insert": 39,
  #     "contaminated": 26,
  #     "unexpected vector": 2
  #   }
  # }
  # }

  if @@full_stats['sequences']
    if @@full_stats['sequences']['count']
      # set input count to 0
      @@full_stats['sequences']['count']['input_count']=0

      # do not remove outputseqs
      # @@full_stats['sequences']['count']['output_seqs']=0
    end

    # remove rejected due to repetitions from rejected count
    if @@full_stats['sequences']['rejected']

      # it there are repeated
      if (@@full_stats['sequences']['rejected']['repeated'])

        # if repeated count > 0 and there count exists
        if (@@full_stats['sequences']['rejected']['repeated'] > 0) and @@full_stats['sequences']['count']

          # discount repeated from rejected, since they are going to be added again by checkout process
          @@full_stats['sequences']['count']['rejected'] -= @@full_stats['sequences']['rejected']['repeated']
        end

        # set repeated to 0
        @@full_stats['sequences']['rejected']['repeated']=0
      end
    end
  end


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

  # TODO - remove sequences from rejected file that were added by cloned

  super
  # return checkpoint
end

#next_workObject



254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/seqtrimnext/classes/em_classes/seqtrim_work_manager.rb', line 254

def next_work

  if @@exit
    return nil
  end

  tuple=[]
  order_in_tuple=0

  @@tuple_id += 1
  tuple_size=@@sequence_readers.count

  @@sequence_readers.each do |sequence_reader|
    n,f,q,c = get_next_seq_from_file(sequence_reader)

    if !n.nil?
      seq=SequenceWithAction.new(n,f.upcase,q,c)
      seq.tuple_id=@@tuple_id
      seq.order_in_tuple=order_in_tuple
      seq.tuple_size=tuple_size
      tuple << seq
      order_in_tuple+=1
    end

  end

  if tuple_size>1
    # check duplicated names
    names = tuple.map{|s| s.seq_name}
    
    if names.uniq.count!=tuple_size
      # puts "NAMES EQUAL IN TUPLE"
      tuple.each_with_index do |seq,i|
        # puts seq.class # seq_name
        seq.seq_name = "#{seq.seq_name}/#{i+1}"
      end
    end
  end

  # tuple is complete
  if tuple.count==tuple_size
    return tuple
  else
    return nil
  end

end

#save_files(obj) ⇒ Object



320
321
322
323
324
325
326
# File 'lib/seqtrimnext/classes/em_classes/seqtrim_work_manager.rb', line 320

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



212
213
214
215
216
217
218
# File 'lib/seqtrimnext/classes/em_classes/seqtrim_work_manager.rb', line 212

def save_user_checkpoint

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

end

#too_many_errors_receivedObject



134
135
136
# File 'lib/seqtrimnext/classes/em_classes/seqtrim_work_manager.rb', line 134

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



222
223
224
# File 'lib/seqtrimnext/classes/em_classes/seqtrim_work_manager.rb', line 222

def trash_checkpointed_work
  warn "Deprecated: trash_checkpointed_work was deprecated, it is automatic now"
end

#work_received(obj) ⇒ Object



303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# File 'lib/seqtrimnext/classes/em_classes/seqtrim_work_manager.rb', line 303

def work_received(obj)

  res = obj

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

  # print output in screen
  if !@@skip_output
    puts obj.output_text
  end

  # save results to files
  save_files(obj)

end

#worker_initial_configObject



138
139
140
# File 'lib/seqtrimnext/classes/em_classes/seqtrim_work_manager.rb', line 138

def worker_initial_config
  return @@params
end