Class: Bio::Blast::NCBIOptions

Inherits:
Object
  • Object
show all
Defined in:
lib/bio/appl/blast/ncbioptions.rb

Overview

A class to parse and store NCBI-tools style command-line options. It is internally used in Bio::Blast and some other classes.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = []) ⇒ NCBIOptions

creates a new object from an array


27
28
29
30
# File 'lib/bio/appl/blast/ncbioptions.rb', line 27

def initialize(options = [])
  #@option_pairs = []
  @option_pairs = _parse_options(options)
end

Class Method Details

.parse(str) ⇒ Object

parses a string and returns a new object


96
97
98
99
# File 'lib/bio/appl/blast/ncbioptions.rb', line 96

def self.parse(str)
  options = Shellwords.shellwords(str)
  self.new(options)
end

Instance Method Details

#==(other) ⇒ Object

If self == other, returns true. Otherwise, returns false.


195
196
197
198
199
200
201
202
203
# File 'lib/bio/appl/blast/ncbioptions.rb', line 195

def ==(other)
  return true if super(other)
  begin
    oopts = other.options
  rescue
    return false
  end
  return self.options == oopts 
end

#add_options(options) ⇒ Object

Adds options from given array. Note that existing options will also be normalized.


Arguments:

  • options: options as an Array of String objects.

Returns

self


188
189
190
191
192
# File 'lib/bio/appl/blast/ncbioptions.rb', line 188

def add_options(options)
  @option_pairs.concat _parse_options(options)
  self.normalize!
  self
end

#delete(key) ⇒ Object

Delete the given option.


Arguments:

  • key: option name as a string, e.g. 'm', 'p', or '-m', '-p'.

Returns

String or nil


132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/bio/appl/blast/ncbioptions.rb', line 132

def delete(key)
  re = _key_to_regexp(key)

  # Note: the last option is used for return value
  # when two or more same option exist.
  oldvalue = nil
  @option_pairs = @option_pairs.delete_if do |pair|
    if re =~ pair[0] then
      oldvalue = pair[1]
      true
    else
      false
    end
  end
  return oldvalue
end

#get(key) ⇒ Object

Return the option.


Arguments:

  • key: option name as a string, e.g. 'm', 'p', or '-m', '-p'.

Returns

String or nil


113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/bio/appl/blast/ncbioptions.rb', line 113

def get(key)
  re = _key_to_regexp(key)

  # Note: the last option is used when two or more same option exist.
  value = nil
  @option_pairs.reverse_each do |pair|
    if re =~ pair[0] then
      value = pair[1]
      break
    end
  end
  return value
end

#make_command_line_options(prior_options = []) ⇒ Object

Returns an array for command-line options. prior_options are preferred to be used.


207
208
209
210
211
212
213
214
215
216
# File 'lib/bio/appl/blast/ncbioptions.rb', line 207

def make_command_line_options(prior_options = [])
  newopts = self.class.new(self.options)
  #newopts.normalize!
  prior_pairs = _parse_options(prior_options)
  prior_pairs.each do |pair|
    newopts.delete(pair[0])
  end
  newopts.option_pairs[0, 0] = prior_pairs
  newopts.options
end

#normalize!Object

Normalize options. For two or more same options (e.g. '-p blastn -p blastp'), only the last option is used. (e.g. '-p blastp' for above example).

Note that completely illegal options are left untouched.


Returns

self


71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/bio/appl/blast/ncbioptions.rb', line 71

def normalize!
  hash = {}
  newpairs = []
  @option_pairs.reverse_each do |pair|
    if pair.size == 2 then
      key = pair[0]
      unless hash[key] then
        newpairs.push pair
        hash[key] = pair
      end
    else
      newpairs.push pair
    end
  end
  newpairs.reverse!
  @option_pairs = newpairs
  self
end

#optionsObject

current options as an array of strings


91
92
93
# File 'lib/bio/appl/blast/ncbioptions.rb', line 91

def options
  @option_pairs.flatten
end

#set(key, value) ⇒ Object

Sets the option to given value.

For example, if you want to set '-p blastall' option,

obj.set('p', 'blastall')

or

obj.set('-p', 'blastall')

(above two are equivalent).


Arguments:

  • key: option name as a string, e.g. 'm', 'p'.

  • value: value as a string, e.g. '7', 'blastp'.

Returns

previous value; String or nil


162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/bio/appl/blast/ncbioptions.rb', line 162

def set(key, value)
  re = _key_to_regexp(key)
  oldvalue = nil
  flag = false
  # Note: only the last options is modified for multiple same options.
  @option_pairs.reverse_each do |pair|
    if re =~ pair[0] then
      oldvalue = pair[1]
      pair[1] = value
      flag = true
      break
    end
  end
  unless flag then
    key = "-#{key}" unless key[0, 1] == '-'
    @option_pairs.push([ key, value ])
  end
  oldvalue
end