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



82
83
84
85
86
87
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
# File 'lib/seqtrimnext/classes/em_classes/seqtrim_work_manager.rb', line 82

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_reader, params, chunk_size = 100, use_json = false, skip_output = 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
# 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, skip_output=false)
  @@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

  @@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
  @@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



124
125
126
127
# File 'lib/seqtrimnext/classes/em_classes/seqtrim_work_manager.rb', line 124

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



283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/seqtrimnext/classes/em_classes/seqtrim_work_manager.rb', line 283

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



137
138
139
140
141
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
# File 'lib/seqtrimnext/classes/em_classes/seqtrim_work_manager.rb', line 137

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



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

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?
      @@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))

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


end

#save_files(obj) ⇒ Object



275
276
277
278
279
280
281
# File 'lib/seqtrimnext/classes/em_classes/seqtrim_work_manager.rb', line 275

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



207
208
209
210
211
212
213
# File 'lib/seqtrimnext/classes/em_classes/seqtrim_work_manager.rb', line 207

def save_user_checkpoint

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

end

#too_many_errors_receivedObject



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

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



217
218
219
# File 'lib/seqtrimnext/classes/em_classes/seqtrim_work_manager.rb', line 217

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

#work_received(obj) ⇒ Object



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/seqtrimnext/classes/em_classes/seqtrim_work_manager.rb', line 258

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



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

def worker_initial_config
  return @@params
end