Class | Bio::Blast::NCBIOptions
In: | lib/bio/appl/blast/ncbioptions.rb
Parent: | Object

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

### Methods

== add_options delete get make_command_line_options new normalize! options parse set

### Attributes

option_pairs | [R] | (protected) option pairs. internal use only.

### Public Class methods

new(options = [])

creates a new object from an array

[Source]

        # File lib/bio/appl/blast/ncbioptions.rb, line 30
    30:     def initialize(options = [])
    31:       #@option_pairs = []
    32:       @option_pairs = _parse_options(options)
    33:     end

parse(str)

parses a string and returns a new object

[Source]

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

### Public Instance methods

==(other)

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

[Source]

         # File lib/bio/appl/blast/ncbioptions.rb, line 198
    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)

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

Arguments:

- options: options as an Array of String objects.

Returns: | self

[Source]

         # File lib/bio/appl/blast/ncbioptions.rb, line 191
    191:     def add_options(options)
    192:       @option_pairs.concat _parse_options(options)
    193:       self.normalize!
    194:       self
    195:     end

delete(key)

Delete the given option.

Arguments:

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

Returns: | String or nil

[Source]

         # File lib/bio/appl/blast/ncbioptions.rb, line 135
    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)

Return the option.

Arguments:

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

Returns: | String or nil

[Source]

         # File lib/bio/appl/blast/ncbioptions.rb, line 116
    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 = [])

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

[Source]

         # File lib/bio/appl/blast/ncbioptions.rb, line 210
    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!()

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

[Source]

        # File lib/bio/appl/blast/ncbioptions.rb, line 74
    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()

current options as an array of strings

[Source]

        # File lib/bio/appl/blast/ncbioptions.rb, line 94
    94:     def options
    95:       @option_pairs.flatten
    96:     end

set(key, value)

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

[Source]

         # File lib/bio/appl/blast/ncbioptions.rb, line 165
    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

[Validate]