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

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

  @plugin_comments = {}

  read_file(path)
end

Class Method Details

.generate_sample_paramsObject



336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
# File 'lib/seqtrimnext/classes/params.rb', line 336

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)



299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# File 'lib/seqtrimnext/classes/params.rb', line 299

def check_db_param(errors,db_param_name)
  # expand database paths
  dbs= get_param(db_param_name).gsub('"','').split(/\s+/)
  # puts "ALGO"*20
  puts dbs.join(',')
  #
  # TODO - chequear aqui que la db no esta vacia y que esta formateada.
  dbs.reverse_each {|db|
    if !File.exists?(db)
      path=File.join($FORMATTED_DB_PATH,db)
    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

    if !File.exists?(path)
      raise "DB File #{path} does not exists"
      # exit
    end
  }
  db_paths = '"'+dbs.join(' ')+'"'

  set_param(db_param_name,db_paths)
  # puts "DATABASES"+db_paths
end

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



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
391
392
393
394
395
396
397
398
399
400
# File 'lib/seqtrimnext/classes/params.rb', line 353

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



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

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)


268
269
270
# File 'lib/seqtrimnext/classes/params.rb', line 268

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

#get_ab_adapter(adapter) ⇒ Object

Return the ab of param



170
171
172
# File 'lib/seqtrimnext/classes/params.rb', line 170

def get_ab_adapter(adapter)
  return @ab_adapters[adapter]
end

#get_comment(plugin, param) ⇒ Object



216
217
218
219
220
221
222
# File 'lib/seqtrimnext/classes/params.rb', line 216

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

#get_linker(linker) ⇒ Object

Return the linker of param



165
166
167
# File 'lib/seqtrimnext/classes/params.rb', line 165

def get_linker(linker)
  return @linkers[linker]
end

#get_mid(param) ⇒ Object

Return the mid’s size of param



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

def get_mid(param)
  return @mids[param]
end

#get_param(param) ⇒ Object

Return the parameter’s list in an array



154
155
156
157
# File 'lib/seqtrimnext/classes/params.rb', line 154

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

#get_pluginObject



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

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))
    
    # puts "CALLER: #{plugin}"
    # puts [file, line, method]

  end
  
end

#load_ab_adapters(path_file) ⇒ Object

Load ab_adapters file



79
80
81
82
# File 'lib/seqtrimnext/classes/params.rb', line 79

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

#load_db_fastas(path_file) ⇒ Object

end def



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/seqtrimnext/classes/params.rb', line 58

def load_db_fastas(path_file)
  res={}
  if File.exists?(path_file)
    ff = FastaFile.new(path_file)
    ff.each {|n,f|
      # @mid_sizes[n]=f.size
      res[n]=f
    }

    ff.close
  end
  return res
end

#load_linkers(path_file) ⇒ Object

Load mid’s file



85
86
87
88
# File 'lib/seqtrimnext/classes/params.rb', line 85

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

#load_mids(path_file) ⇒ Object

Load mid’s file



73
74
75
76
# File 'lib/seqtrimnext/classes/params.rb', line 73

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

#load_repeated_seqs(file_path) ⇒ Object



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

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])
        #puts line
        # puts line,line[0]
        if line =~ />([^\.]+)\.\.\.\s/
          #puts 'ok'
          @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



146
147
148
149
150
151
# File 'lib/seqtrimnext/classes/params.rb', line 146

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



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

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

          #$LOG.debug "read: #{params[1]}"
        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)


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

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

#save_file(path_file) ⇒ Object

Reads param’s file



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/seqtrimnext/classes/params.rb', line 122

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



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
256
257
258
259
# File 'lib/seqtrimnext/classes/params.rb', line 225

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



262
263
264
# File 'lib/seqtrimnext/classes/params.rb', line 262

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

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



192
193
194
195
196
197
198
199
200
201
202
# File 'lib/seqtrimnext/classes/params.rb', line 192

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