class Bio::Blast::NCBIOptions

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

Attributes

option_pairs[R]

(protected) option pairs. internal use only.

Public Class Methods

new(options = []) click to toggle source

creates a new object from an array

   # File lib/bio/appl/blast/ncbioptions.rb
30 def initialize(options = [])
31   #@option_pairs = []
32   @option_pairs = _parse_options(options)
33 end
parse(str) click to toggle source

parses a string and returns a new object

    # File lib/bio/appl/blast/ncbioptions.rb
 99 def self.parse(str)
100   options = Shellwords.shellwords(str)
101   self.new(options)
102 end

Public Instance Methods

==(other) click to toggle source

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

Calls superclass method
    # File lib/bio/appl/blast/ncbioptions.rb
198 def ==(other)
199   return true if super(other)
200   begin
201     oopts = other.options
202   rescue
203     return false
204   end
205   return self.options == oopts 
206 end
add_options(options) click to toggle source

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


Arguments:

  • options: options as an Array of String objects.

Returns

self

    # File lib/bio/appl/blast/ncbioptions.rb
191 def add_options(options)
192   @option_pairs.concat _parse_options(options)
193   self.normalize!
194   self
195 end
delete(key) click to toggle source

Delete the given option.


Arguments:

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

Returns

String or nil

    # File lib/bio/appl/blast/ncbioptions.rb
135 def delete(key)
136   re = _key_to_regexp(key)
137 
138   # Note: the last option is used for return value
139   # when two or more same option exist.
140   oldvalue = nil
141   @option_pairs = @option_pairs.delete_if do |pair|
142     if re =~ pair[0] then
143       oldvalue = pair[1]
144       true
145     else
146       false
147     end
148   end
149   return oldvalue
150 end
get(key) click to toggle source

Return the option.


Arguments:

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

Returns

String or nil

    # File lib/bio/appl/blast/ncbioptions.rb
116 def get(key)
117   re = _key_to_regexp(key)
118 
119   # Note: the last option is used when two or more same option exist.
120   value = nil
121   @option_pairs.reverse_each do |pair|
122     if re =~ pair[0] then
123       value = pair[1]
124       break
125     end
126   end
127   return value
128 end
make_command_line_options(prior_options = []) click to toggle source

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

    # File lib/bio/appl/blast/ncbioptions.rb
210 def make_command_line_options(prior_options = [])
211   newopts = self.class.new(self.options)
212   #newopts.normalize!
213   prior_pairs = _parse_options(prior_options)
214   prior_pairs.each do |pair|
215     newopts.delete(pair[0])
216   end
217   newopts.option_pairs[0, 0] = prior_pairs
218   newopts.options
219 end
normalize!() click to toggle source

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

   # File lib/bio/appl/blast/ncbioptions.rb
74 def normalize!
75   hash = {}
76   newpairs = []
77   @option_pairs.reverse_each do |pair|
78     if pair.size == 2 then
79       key = pair[0]
80       unless hash[key] then
81         newpairs.push pair
82         hash[key] = pair
83       end
84     else
85       newpairs.push pair
86     end
87   end
88   newpairs.reverse!
89   @option_pairs = newpairs
90   self
91 end
options() click to toggle source

current options as an array of strings

   # File lib/bio/appl/blast/ncbioptions.rb
94 def options
95   @option_pairs.flatten
96 end
set(key, value) click to toggle source

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

    # File lib/bio/appl/blast/ncbioptions.rb
165 def set(key, value)
166   re = _key_to_regexp(key)
167   oldvalue = nil
168   flag = false
169   # Note: only the last options is modified for multiple same options.
170   @option_pairs.reverse_each do |pair|
171     if re =~ pair[0] then
172       oldvalue = pair[1]
173       pair[1] = value
174       flag = true
175       break
176     end
177   end
178   unless flag then
179     key = "-#{key}" unless key[0, 1] == '-'
180     @option_pairs.push([ key, value ])
181   end
182   oldvalue
183 end