Class: Params

Inherits:
Object
  • Object
show all
Defined in:
lib/seqtrimnext/classes/params.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Params

Creates the structure and start the reading of parameter’s file



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/seqtrimnext/classes/params.rb', line 10

def initialize(path)
  @params = {}
  @comments = {}
  # @param_order={}
  @mids = {}
  @ab_adapters={}
  @adapters={}
  @linkers = {}
  @clusters = {}

  @plugin_comments = {}

  read_file(path)
end

Class Method Details

.generate_sample_paramsObject



393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
# File 'lib/seqtrimnext/classes/params.rb', line 393

def self.generate_sample_params

  filename = 'sample_params.txt'
  x=1
  while File.exists?(filename)
    filename = "sample_params#{x}.txt"
    x+=1
  end

  f=File.open(filename,'w')
  f.puts "SAMPLE_PARAMS"
  f.close

  puts "Sample params file generated: #{filename}"

end

Instance Method Details

#check_db_param(errors, db_param_name) ⇒ Object

def split_databases(db_param_name)



345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
# File 'lib/seqtrimnext/classes/params.rb', line 345

def check_db_param(errors,db_param_name)
  if !get_param(db_param_name).empty?
    # expand database paths
    dbs= get_param(db_param_name).gsub('"','').split(/\s+/)
    # puts "ALGO"*20
    # puts "INPUT DATABASES:\n"+dbs.join(',')

    procesed_dbs=[]
    #
    # TODO - chequear aqui que la db no esta vacia y que esta formateada.
    dbs.reverse_each {|db_p|
      db=File.expand_path(db_p)

      if !File.exists?(db)
        path=File.join($FORMATTED_DB_PATH,db_p)
      else
        path=db
      end


      if Dir.glob(path+'*.n*').entries.empty?
        puts "DB file #{path} not formatted"

        if File.writable_real?(path)
          cmd = "makeblastdb -in #{path} -parse_seqids -dbtype nucl"
          system(cmd)
        else
          raise "Can't format database. We don't have write permissions in: #{path}"
        end
      end

      procesed_dbs << path

      if !File.exists?(path)
        raise "DB File #{path} does not exists"
        # exit
      end
    }

    db_paths = '"'+procesed_dbs.join(' ')+'"'

    set_param(db_param_name,db_paths)

    puts "USED DATABASES\n"+db_paths
  end
end

#check_param(errors, param, param_class, default_value = nil, comment = nil) ⇒ Object



410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
# File 'lib/seqtrimnext/classes/params.rb', line 410

def check_param(errors,param,param_class,default_value=nil, comment=nil)

  if !exists?(param)
    if default_value.nil? #|| (default_value.is_a?(String) && default_value.empty?)
      errors.push "The param #{param} is required and no default value is available"
    else
      set_param(param,default_value,comment)
    end
  end

  s = get_param(param)


  set_comment(get_plugin,param,comment)

  # check_class=Object.const_get(param_class)
  begin

    case param_class
    when 'Integer'
      r = Integer(s)
    when 'Float'
      r = Float(s)
    when 'String'
      r = String(s)
    when 'DB'
      # it is a string
      r = String(s)
      # and must be a valid db

      r = check_db_param(errors,param)

    when 'PluginList'
      r=String(s)
      r= check_plugin_list_param(errors,param)
    end

  rescue Exception => e
    message="Current value is ##{s}#. "
    if param_class=='DB'
      message += e.message
    end

    errors.push "Param #{param} is not a valid #{param_class}. #{message}"
  end
  # end

end

#check_plugin_list_param(errors, param_name) ⇒ Object



318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'lib/seqtrimnext/classes/params.rb', line 318

def check_plugin_list_param(errors,param_name)
  # get plugin list
  pl_list=get_param(param_name)

  # puts pl_list,param_name
  list=pl_list.split(',')

  list.map!{|e| e.strip}

  # puts "Lista:",list.join(',')


  # always the pluginExtractInserts at the end
  list.delete('PluginExtractInserts')
  list << 'PluginExtractInserts'

  set_param(param_name,list.join(','))
  # if !list.include?('PluginExtractInserts')
  #   raise "PluginExtractInserts do not exists"
  #
  # end



end

#exists?(param_name) ⇒ Boolean

Returns true if exists the parameter and nil if don’t

Returns:

  • (Boolean)


314
315
316
# File 'lib/seqtrimnext/classes/params.rb', line 314

def exists?(param_name)
  return !@params[param_name].nil?
end

#get_ab_adapter(adapter) ⇒ Object

Return the ab of param



226
227
228
229
# File 'lib/seqtrimnext/classes/params.rb', line 226

def get_ab_adapter(adapter)
  # return @ab_adapters[adapter]
  return get_fasta(@ab_adapters,adapter,"ab_adapter")
end

#get_adapter(adapter) ⇒ Object



231
232
233
234
# File 'lib/seqtrimnext/classes/params.rb', line 231

def get_adapter(adapter)
  # return @adapters[adapter]
  return get_fasta(@adapters,adapter,"adapter")
end

#get_comment(plugin, param) ⇒ Object



263
264
265
266
267
268
269
# File 'lib/seqtrimnext/classes/params.rb', line 263

def get_comment(plugin,param)
  res = nil
  if @plugin_comments[plugin]
    res =@plugin_comments[plugin][param]
  end
  return res
end

#get_fasta(list, name, type) ⇒ Object



202
203
204
205
206
207
208
209
210
211
# File 'lib/seqtrimnext/classes/params.rb', line 202

def get_fasta(list,name,type)
  res = list[name]

  if res.nil?
    $LOG.error("Error. The #{type}: #{name} was not correctly loaded")
    raise "Error. The #{type}: #{name} was not found in loaded #{name}s: #{list.map{|k,v| k}}."
  end

  return res
end

#get_linker(linker) ⇒ Object

Return the linker of param



220
221
222
223
# File 'lib/seqtrimnext/classes/params.rb', line 220

def get_linker(linker)
  # return @linkers[linker]
  return get_fasta(@linkers,linker,"linker")
end

#get_mid(mid) ⇒ Object

Return the mid’s size of param



214
215
216
217
# File 'lib/seqtrimnext/classes/params.rb', line 214

def get_mid(mid)
  # return @mids[mid]
  return get_fasta(@mids,mid,"mid")
end

#get_param(param) ⇒ Object

Return the parameter’s list in an array



197
198
199
200
# File 'lib/seqtrimnext/classes/params.rb', line 197

def get_param(param)
  #$LOG.debug "Get Param:  #{@params[param]}"
  return @params[param]
end

#get_pluginObject



237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/seqtrimnext/classes/params.rb', line 237

def get_plugin
  plugin='General'
  # puts caller(2)[1]
  at = caller(2)[1]
  if /^(.+?):(\d+)(?::in `(.*)')?/ =~ at
    file = Regexp.last_match[1]
    line = Regexp.last_match[2].to_i
    method = Regexp.last_match[3]
    plugin=File.basename(file,File.extname(file))

  end
  
end

#load_ab_adapters(path_file) ⇒ Object

Load ab_adapters file



111
112
113
114
# File 'lib/seqtrimnext/classes/params.rb', line 111

def load_ab_adapters(path_file)
  @ab_adapters=load_db_fastas(path_file)
  # puts @ab_adapters
end

#load_adapters(path_file) ⇒ Object

load normal adapters



117
118
119
# File 'lib/seqtrimnext/classes/params.rb', line 117

def load_adapters(path_file)
  @adapters=load_db_fastas(path_file)
end

#load_db_fastas(input_paths) ⇒ Object

end def



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/seqtrimnext/classes/params.rb', line 71

def load_db_fastas(input_paths)

  res={}

  if (!input_paths.nil?) & (input_paths!='')
    # remove quotes
    paths=input_paths.gsub(/\A['"]+|['"]+\Z/, "")

    # split paths by spaces
    # puts "PATHS:"
    # puts paths.split(' ')
    paths.split(' ').each do |path_file|

      if File.exists?(path_file)
        ff = FastaFile.new(path_file)
        ff.each {|n,f|
          res[n]=f
        }

        ff.close
      end
    end

  end

  # puts "LOADED_DB #{paths}:"
  # res.each do |k,v|
  #   puts k
  # end

  return res
end

#load_linkers(path_file) ⇒ Object

Load mid’s file



123
124
125
126
# File 'lib/seqtrimnext/classes/params.rb', line 123

def load_linkers(path_file)
  @linkers=load_db_fastas(path_file)
  # puts @linkers
end

#load_mids(path_file) ⇒ Object

Load mid’s file



105
106
107
108
# File 'lib/seqtrimnext/classes/params.rb', line 105

def load_mids(path_file)
  @mids=load_db_fastas(path_file)
  # puts @mids
end

#load_repeated_seqs(file_path) ⇒ Object



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
157
158
# File 'lib/seqtrimnext/classes/params.rb', line 128

def load_repeated_seqs(file_path)
  @clusters={}

  if File.exists?(file_path)
    # File.open(ARGV[0]).each_line do |line|
    $LOG.debug("Repeated file path:"+file_path)
    File.open(file_path).each_line do |line|
      #puts line,line[0]
      # en ruby19 line[0] da el caracter, no el chr
      #if (line[0]!=62) && (line[0]!=48)
      # if (line[0]!='>'[0]) && (line[0]!='0'[0])

      # line doesn't finish in *
      if (line[0]!='>'[0]) && (!(line =~ /\*$/))

        #puts line
        # puts line,line[0]
        if line =~ />([^\.]+)\.\.\.\s/
          #puts 'ok'
          # puts $1
          @clusters[$1]=1
        end
      end
    end
    $LOG.info("Repeated sequence count: #{@clusters.count}")
  else
    $LOG.error("Clustering file's doesn't exists: #{@clusters.count}")
  end


end

Prints the pair name/numeric-value for every parameter



189
190
191
192
193
194
# File 'lib/seqtrimnext/classes/params.rb', line 189

def print_parameters()
  @params.each do |clave, valor|
    #$LOG.debug  "The Parameter #{clave} have the value " +valor.to_s
    puts "#{clave} = #{valor} "
  end
end

#read_file(path_file) ⇒ Object

Reads param’s file



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
# File 'lib/seqtrimnext/classes/params.rb', line 26

def read_file(path_file)

  if path_file && File.exists?(path_file)
    comments= []
    File.open(path_file).each_line do |line|
      line.chomp! # delete end of line

      if !line.empty?
        if !(line =~ /^\s*#/)   # if line is not a comment
          # extract the parameter's name in params[0] and the parameter's value in params[1]
          #params = line.split(/\s*=\s*/)

          # store in the hash the pair key/value, in our case will be name/numeric-value ,
          # that are save in params[0] and params[1],  respectively
          #if (!params[0].nil?) && (!params[1].nil?)
          #  set_param(params[0].strip,params[1].strip,comments)
          #  comments=[]
          #end

          line =~ /^\s*([^=]*)\s*=\s*(.*)\s*$/
          params=[$1,$2]
            
          # store in the hash the pair key/value, in our case will be name/numeric-value ,
          # that are save in params[0] and params[1],  respectively
          if (!params[0].nil?) && (!params[1].nil?)
            set_param(params[0].strip,params[1].strip,comments)
            comments=[]
          end


          $LOG.debug "read: #{params[0]}=#{params[1]}" if !$LOG.nil?
          
        else
          comments << line.gsub(/^\s*#/,'')
        end # end if comentario
      end #end if line
    end #end each
    if @params.empty?
      puts "INVALID PARAMETER FILE: #{path_file}. No parameters defined"
      exit
    end

  end
end

#repeated_seq?(name) ⇒ Boolean

Returns:

  • (Boolean)


160
161
162
# File 'lib/seqtrimnext/classes/params.rb', line 160

def repeated_seq?(name)
  return !@clusters[name].nil?
end

#save_file(path_file) ⇒ Object

Reads param’s file



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/seqtrimnext/classes/params.rb', line 165

def save_file(path_file)

  f=File.open(path_file,'w')
  @plugin_comments.keys.sort.reverse.each do |plugin_name|
    f.puts "#"*50
    f.puts "# " + plugin_name
    f.puts "#"*50
    f.puts ''

    @plugin_comments[plugin_name].keys.each do |param|
      comment=get_comment(plugin_name,param)
      if !comment.nil? && !comment.empty? && comment!=''
        f.puts comment.map{|c| '# '+c if c!=''}
      end
      f.puts ''
      f.puts "#{param} = #{@params[param]}"
      f.puts ''
    end
  end
  f.close

end

#set_comment(plugin, param, comment) ⇒ Object



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
301
302
303
304
305
306
# File 'lib/seqtrimnext/classes/params.rb', line 272

def set_comment(plugin,param,comment)
  if !comment.is_a?(Array) && !comment.nil?
    comment=comment.split("\n").compact.map{|l| l.strip}
  end

  if @plugin_comments[plugin].nil?
    @plugin_comments[plugin]={}
  end

  old_comment=''
  # remove from other plugins
  @plugin_comments.each do |plugin_name,comments|
    if comments.keys.include?(param) && plugin_name!=plugin
      old_comment=comments[param]
      comments.delete(param)
    end
  end

  if comment.nil?
    comment=old_comment
  end

  # @comments[param]=(comment || [''])
  @plugin_comments[plugin][param]=(comment || [''])
  # puts @plugin_comments.keys.to_json

  # remove empty comments

  @plugin_comments.reverse_each do |plugin_name,comments|
    if comments.empty?
      @plugin_comments.delete(plugin_name)
    end
  end

end

#set_mid(param, value) ⇒ Object



309
310
311
# File 'lib/seqtrimnext/classes/params.rb', line 309

def set_mid(param,value)
  @mids[param] = value
end

#set_param(param, value, comment = nil) ⇒ Object



251
252
253
254
255
256
257
258
259
260
261
# File 'lib/seqtrimnext/classes/params.rb', line 251

def set_param(param,value,comment = nil)
  plugin=get_plugin

  @params[param] = value

  if get_comment(plugin,param).nil?
    set_comment(plugin,param,comment)
  end


end