Class: Tool

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

Direct Known Subclasses

ManageDBTool, ProtXMLToGFFTool, SearchTool, SetupTool

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



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/protk/tool.rb', line 76

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



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



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

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.



21
22
23
# File 'lib/protk/tool.rb', line 21

def option_parser
  @option_parser
end

#optionsObject (readonly)

Options set from the command-line



17
18
19
# File 'lib/protk/tool.rb', line 17

def options
  @options
end

#options_defined_by_userObject (readonly)

Returns the value of attribute options_defined_by_user.



23
24
25
# File 'lib/protk/tool.rb', line 23

def options_defined_by_user
  @options_defined_by_user
end

Class Method Details

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



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/protk/tool.rb', line 129

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



116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/protk/tool.rb', line 116

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



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

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

#add_value_option(symbol, default_value, opts) ⇒ Object



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

def add_value_option(symbol,default_value,opts)    
  @options[symbol]=default_value
  @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



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/protk/tool.rb', line 146

def check_options(require_input_file=false,mandatory=[])
 # Checking for required options
 begin
   self.option_parser.parse!
   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

#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



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

def jobid_prefix
  @jobid_prefix
end

#jobid_prefix=(p) ⇒ Object



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

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



173
174
175
176
# File 'lib/protk/tool.rb', line 173

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

#supported_optionsObject



37
38
39
40
41
42
43
# File 'lib/protk/tool.rb', line 37

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