Class: Tool

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

Direct Known Subclasses

ManageDBTool, SearchTool, SetupTool

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(option_support = {:help=>true}) ⇒ Tool

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



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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
# File 'lib/protk/tool.rb', line 52

def initialize(option_support={:help=>true})
  @jobid_prefix = "x"
  @options = OpenStruct.new
  options.library = []
  options.inplace = false
  options.encoding = "utf8"
  options.transfer_type = :auto
  options.verbose = false
  
  @option_parser=OptionParser.new do |opts|
    
    if ( option_support[:prefix_suffix]==true)
    
      @options.output_prefix = ""
      opts.on( '-b', '--output-prefix pref', 'A string to prepend to the name of output files' ) do |prefix|
        @options.output_prefix = prefix
      end

      @options.output_suffix = ""
      opts.on( '-e', '--output-suffix suff', 'A string to append to the name of output files' ) do |suffix|
        @options.output_suffix = suffix
      end
      
    end
    
    if ( option_support[:explicit_output]==true)
      @options.explicit_output = nil
      opts.on( '-o', '--output out', 'An explicitly named output file.' ) do |out|
        @options.explicit_output = out
      end
    end
       
    if ( option_support[:over_write]==true)
          
      @options.over_write=false
      opts.on( '-r', '--replace-output', 'Dont skip analyses for which the output file already exists' ) do  
        @options.over_write = true
      end
      
    end

    if ( option_support[:background]==true)

      @options.background = false
      opts.on( '-z', '--background', 'Run jobs in the background using pbs' ) do  
        @options.background = true
      end
      
    end
    
    if ( option_support[:help]==true)
     
      opts.on( '-h', '--help', 'Display this screen' ) do
        puts opts
        exit
      end
    end
     
  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



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

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

Instance Method Details

#input_base_path(input_file, ext = nil) ⇒ Object

Create a full base path (without extension) representing the input file for this analysis Optionally provide the extension to be removed (if not provided it will be inferred)



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

def input_base_path(input_file,ext=nil)
  input_path=Pathname.new(input_file)
  throw "Error: Input directory #{input_path.dirname} does not exist" unless input_path.dirname.exist?
  dir=input_path.dirname.realpath.to_s
  if ( ext==nil)
    ext=input_path.extname    
  end
  base_name=input_path.basename.to_s.gsub(/#{ext}$/,"")
  "#{dir}/#{base_name}"
end

#jobid_from_filename(filename) ⇒ Object



173
174
175
176
177
178
179
180
# File 'lib/protk/tool.rb', line 173

def jobid_from_filename(filename)
  jobid="protk"
  jobnum_match=filename.match(/(.{1,10})\.d/)
  if (jobnum_match!=nil)
    jobid="#{self.jobid_prefix}#{jobnum_match[1]}"
  end
  return jobid
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



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

def jobid_prefix
  @jobid_prefix
end

#jobid_prefix=(p) ⇒ Object



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

def jobid_prefix=(p)
  @jobid_prefix=p
end

#output_base_path(output_file, ext = nil) ⇒ Object

Create and return a full base path (without extension) representing the output file for this analysis Optionally provide the extension to be removed (if not provided it will be inferred)



121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/protk/tool.rb', line 121

def output_base_path(output_file,ext=nil)

  output_path=Pathname.new(output_file)
  throw "Error: Output directory #{output_path.dirname} does not exist" unless output_path.dirname.exist?
  dir=output_path.dirname.realpath.to_s
  basename=output_path.basename.to_s
  if ( ext==nil)
    ext=output_path.extname    
  end
  base_name=basename.gsub(/#{ext}$/,"")

  "#{dir}/#{@options.output_prefix}#{base_name}#{@options.output_suffix}"
end

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

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



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

def run(cmd,genv,job_params=nil,jobscript_path=nil,autodelete=true)
  if ( @options.background )
    throw "Error: Background option was selected but this host does not support background jobs" unless genv.has_pbs
    # Send this job off to be run in a batch queuer
    
    cmd_runner=CommandRunner.new(genv)
    
 
    
    cmd_runner.run_batch(cmd,job_params,jobscript_path,autodelete)
    
  else 
    cmd_runner=CommandRunner.new(genv)
    cmd_runner.run_local(cmd)
  end
end