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 27
    27:     def initialize(options = [])
    28:       #@option_pairs = []
    29:       @option_pairs = _parse_options(options)
    30:     end

parse(str)

parses a string and returns a new object

[Source]

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

### Public Instance methods

==(other)

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

[Source]

         # File lib/bio/appl/blast/ncbioptions.rb, line 195
    195:     def ==(other)
    196:       return true if super(other)
    197:       begin
    198:         oopts = other.options
    199:       rescue
    200:         return false
    201:       end
    202:       return self.options == oopts
    203:     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 188
    188:     def add_options(options)
    189:       @option_pairs.concat _parse_options(options)
    190:       self.normalize!
    191:       self
    192:     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 132
    132:     def delete(key)
    133:       re = _key_to_regexp(key)
    134:
    135:       # Note: the last option is used for return value
    136:       # when two or more same option exist.
    137:       oldvalue = nil
    138:       @option_pairs = @option_pairs.delete_if do |pair|
    139:         if re =~ pair[0] then
    140:           oldvalue = pair[1]
    141:           true
    142:         else
    143:           false
    144:         end
    145:       end
    146:       return oldvalue
    147:     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 113
    113:     def get(key)
    114:       re = _key_to_regexp(key)
    115:
    116:       # Note: the last option is used when two or more same option exist.
    117:       value = nil
    118:       @option_pairs.reverse_each do |pair|
    119:         if re =~ pair[0] then
    120:           value = pair[1]
    121:           break
    122:         end
    123:       end
    124:       return value
    125:     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 207
    207:     def make_command_line_options(prior_options = [])
    208:       newopts = self.class.new(self.options)
    209:       #newopts.normalize!
    210:       prior_pairs = _parse_options(prior_options)
    211:       prior_pairs.each do |pair|
    212:         newopts.delete(pair[0])
    213:       end
    214:       newopts.option_pairs[0, 0] = prior_pairs
    215:       newopts.options
    216:     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 71
    71:     def normalize!
    72:       hash = {}
    73:       newpairs = []
    74:       @option_pairs.reverse_each do |pair|
    75:         if pair.size == 2 then
    76:           key = pair[0]
    77:           unless hash[key] then
    78:             newpairs.push pair
    79:             hash[key] = pair
    80:           end
    81:         else
    82:           newpairs.push pair
    83:         end
    84:       end
    85:       newpairs.reverse!
    86:       @option_pairs = newpairs
    87:       self
    88:     end

options()

current options as an array of strings

[Source]

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

[Validate]