Class: Tool

Inherits:
Object
  • Object
show all
Defined in:
lib/protk/tool.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(option_support = []) ⇒ Tool

Creates an empty options object to hold commandline options Also creates an option_parser with default options common to all tools



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/protk/tool.rb', line 95

def initialize(option_support=[])
  @jobid_prefix = "x"
  @options = OpenStruct.new
  options.library = []
  options.inplace = false
  options.encoding = "utf8"
  options.transfer_type = :auto
  options.verbose = false

  @options_defined_by_user={}

  @option_parser=OptionParser.new do |opts|

    opts.on( '-h', '--help', 'Display this screen' ) do
      puts opts
      exit
    end
  end

  if ( option_support.include? :prefix)
    add_value_option(:output_prefix,"",['-b','--output-prefix pref', 'A string to prepend to the name of output files'])
  end

  if ( option_support.include? :over_write)
    add_boolean_option(:over_write,false,['-r', '--replace-output', 'Dont skip analyses for which the output file already exists'])
  end

  if ( option_support.include? :explicit_output )
    add_value_option(:explicit_output,nil,['-o', '--output out', 'An explicitly named output file.'])
  end

  if ( option_support.include? :threads )
    add_value_option(:threads,1,['-n','--threads num','Number of processing threads to use. Set to 0 to autodetect an appropriate value'])
  end

  if ( option_support.include? :database)
    add_value_option(:database,"sphuman",['-d', '--database dbname', 'Specify the database to use for this search. Can be a named protk database or the path to a fasta file'])
  end

  if (option_support.include? :debug)
    add_boolean_option(:debug,false,['--debug','Run in debug mode'])
  end

end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object

Provides direct access to options through methods of the same name



58
59
60
61
62
63
64
# File 'lib/protk/tool.rb', line 58

def method_missing(meth, *args, &block)
  if ( args.length==0 && block==nil)
    @options.send meth
  else
    super
  end
end

Instance Attribute Details

#option_parserObject (readonly)

The option parser used to parse command-line options.



32
33
34
# File 'lib/protk/tool.rb', line 32

def option_parser
  @option_parser
end

#optionsObject (readonly)

Options set from the command-line



28
29
30
# File 'lib/protk/tool.rb', line 28

def options
  @options
end

#options_defined_by_userObject (readonly)

Returns the value of attribute options_defined_by_user.



34
35
36
# File 'lib/protk/tool.rb', line 34

def options_defined_by_user
  @options_defined_by_user
end

Class Method Details

.default_output_path(input_path, newext, prefix, suffix) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/protk/tool.rb', line 154

def self.default_output_path(input_path,newext,prefix,suffix)
 path=""
 input_path=input_path[0] if (input_path.instance_of?(Array) && input_path.length==1)
 if input_path.instance_of?(Array)
   dir=Pathname.new(input_path[0]).dirname.realpath.to_s
   basename="collected_outputs"
   path="#{dir}/#{prefix}#{basename}#{suffix}#{newext}"
 else
   dir=Pathname.new(input_path).dirname.realpath.to_s
   basename=Pathname.new(input_path).basename.to_s
   oldext=Tool.extension_from_filename(input_path)
   basename=basename.gsub(/#{oldext}$/,"")
   path="#{dir}/#{prefix}#{basename}#{suffix}#{newext}"
 end
 path
end

.extension_from_filename(filename) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/protk/tool.rb', line 141

def self.extension_from_filename(filename)
 ext=""
 case filename.chomp
 when /\.pep\.xml/
   ext=".pep.xml"
 when /\.prot\.xml/
   ext=".prot.xml"
 else
   ext=Pathname.new(filename.chomp).extname
 end
 ext
end

Instance Method Details

#add_boolean_option(symbol, default_value, opts) ⇒ Object



82
83
84
85
86
87
88
89
# File 'lib/protk/tool.rb', line 82

def add_boolean_option(symbol,default_value,opts)
  @options[symbol]=default_value
  opts=add_default_to_help(default_value,opts)
  @option_parser.on(*opts) do
    @options[symbol]=!default_value
    @options_defined_by_user[symbol]=opts
  end
end

#add_default_to_help(default_value, opts) ⇒ Object



66
67
68
69
70
71
# File 'lib/protk/tool.rb', line 66

def add_default_to_help(default_value,opts)
  if default_value!=nil && default_value!=" " && default_value!=""
    opts[-1] = "#{opts.last} [#{default_value.to_s}]"
  end
  opts
end

#add_value_option(symbol, default_value, opts) ⇒ Object



73
74
75
76
77
78
79
80
# File 'lib/protk/tool.rb', line 73

def add_value_option(symbol,default_value,opts)
  @options[symbol]=default_value
  opts=add_default_to_help(default_value,opts)
  @option_parser.on(*opts) do |val|
    @options[symbol]=val
    @options_defined_by_user[symbol]=opts
  end
end

#check_options(require_input_file = false, mandatory = []) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/protk/tool.rb', line 171

def check_options(require_input_file=false,mandatory=[])
 # Checking for required options
 begin
   self.option_parser.parse!

   if has_override
     return true
   end
   missing = mandatory.select{ |param| self.send(param).nil? }
   if not missing.empty?
     puts "Missing options: #{missing.join(', ')}"
     puts self.option_parser
     return false
   end
 rescue OptionParser::InvalidOption, OptionParser::MissingArgument
   puts $!.to_s
   puts self.option_parser
   return false
 end

 if ( require_input_file && ARGV[0].nil? )
   puts "You must supply an input file"
   puts self.option_parser
   return false
 end

 return true
end

#database_infoObject



208
209
210
211
212
213
214
215
216
217
218
# File 'lib/protk/tool.rb', line 208

def database_info
  case
    when Pathname.new(@options.database).exist? # It's an explicitly named db
      db_path=Pathname.new(@options.database).expand_path.to_s
      db_name=Pathname.new(@options.database).basename.to_s
    else
      db_path=Constants.instance.current_database_for_name @options.database
      db_name=@options.database
  end
  FastaDatabase.new(db_name,db_path)
end

#jobid_prefixObject

Prefix for background jobs x = X!Tandem, o=OMSSA, p=“Phenyx”, m=“Mascot” Can’t use attr_accessor here because we want this available to subclasses



40
41
42
# File 'lib/protk/tool.rb', line 40

def jobid_prefix
  @jobid_prefix
end

#jobid_prefix=(p) ⇒ Object



44
45
46
# File 'lib/protk/tool.rb', line 44

def jobid_prefix=(p)
  @jobid_prefix=p
end

#run(cmd, genv, autodelete = true) ⇒ Object

Run the search tool using the given command string and global environment



202
203
204
205
# File 'lib/protk/tool.rb', line 202

def run(cmd,genv,autodelete=true)
 cmd_runner=CommandRunner.new(genv)
 cmd_runner.run_local(cmd)
end

#supported_optionsObject



48
49
50
51
52
53
54
# File 'lib/protk/tool.rb', line 48

def supported_options
  os_hash=@options.to_h
  # Remove entries entirely related to internal use
  internal_keys=[:library, :inplace, :encoding, :transfer_type, :verbose]
  os_hash.delete_if { |key,val| internal_keys.include? key }
  os_hash
end