Class: Constants

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/protk/constants.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConstants

Read the global constants file and initialize our class @env variable Initialize loggers



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/protk/constants.rb', line 140

def initialize() 

  @data_lib_dir="#{File.dirname(__FILE__)}/data"
  @protk_dir="#{Dir.home}/.protk"

  if ( ENV['PROTK_INSTALL_DIR']!=nil )
    p "Using protk install dir from shell"
    @protk_dir=ENV['PROTK_INSTALL_DIR']
  end

  # Load Protk Defaults
  #
  default_config_yml = YAML.load_file "#{File.dirname(__FILE__)}/data/default_config.yml"
  throw "Unable to read the config file at #{File.dirname(__FILE__)}/data/default_config.yml" unless default_config_yml!=nil

  # User-defined defaults override protk defaults
  #
  user_config_yml = nil
  user_config_yml = YAML.load_file "#{@protk_dir}/config.yml" if File.exist? "#{@protk_dir}/config.yml"
  if ( user_config_yml !=nil )
    @env = default_config_yml.merge user_config_yml
  else
    @env=default_config_yml
  end

  protk_tool_dirs=["tpp/bin","omssa","openms/bin","msgfplus","blast/bin","pwiz","tandem/bin"]

  # Construct the PATH variable by prepending our preferred paths
  #
  protk_paths=[]

  # Add PATHs if PROTK_XXX_ROOT is defined
  #
  protk_tool_dirs.each do |tooldir|  
    env_value = ENV["PROTK_#{tooldir.upcase}_ROOT"]
    if ( env_value!=nil)
      protk_paths<<env_value
    end
    protk_paths<<"#{@protk_dir}/tools/#{tooldir}"
  end

  original_path=ENV['PATH']
  protk_paths<<original_path


  ENV['PATH']=protk_paths.join(":")

  # puts "Path #{ENV['PATH']}"
  throw "No data found in config file" unless @env!=nil

  @info_level="fatal"
  @info_level=default_config_yml['message_level'] unless default_config_yml['message_level'].nil?

end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method) ⇒ Object

Provides direct access to constants through methods of the same name This will be used for all constants other than paths



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

def method_missing(method)

  from_env = @env[method.to_s]
  throw "#{method} is undefined" unless from_env!=nil
  from_env
end

Instance Attribute Details

#data_lib_dirObject (readonly)

Returns the value of attribute data_lib_dir.



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

def data_lib_dir
  @data_lib_dir
end

#info_levelObject

Returns the value of attribute info_level.



30
31
32
# File 'lib/protk/constants.rb', line 30

def info_level
  @info_level
end

#protk_dirObject (readonly)

Returns the value of attribute protk_dir.



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

def protk_dir
  @protk_dir
end

Instance Method Details

#binObject

Some constants are paths. They need to be translated into real paths before being returned



49
50
51
# File 'lib/protk/constants.rb', line 49

def bin
  return "#{@protk_dir}/bin"
end

#blast_rootObject



123
124
125
# File 'lib/protk/constants.rb', line 123

def blast_root
    "#{@protk_dir}/tools/blast"   
end

#blastdbcmdObject



100
101
102
103
# File 'lib/protk/constants.rb', line 100

def blastdbcmd
  path=%x[which blastdbcmd]
  path.chomp
end

#current_database_for_name(dbname) ⇒ Object

Based on the database shortname and global database path, find the most current version of the required database If dbname corresponds to a folder in the dbroot this function returns the path of the database with an extension appropriate to the database type

If dbname is a full path to a file this tool will first import the file as a temporary database and will then return its full path



259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/protk/constants.rb', line 259

def current_database_for_name(dbname)
  dbroot=self.protein_database_root
  
  throw "Protein database directory not specified" unless dbroot!=nil
  throw "Protein database directory #{dbroot} does not exist" unless Pathname(dbroot).exist?
  
  # Remove any trailing slashes or spaces from the end of dbroot if present
  #
  dbroot.sub!(/(\/*\s*)$/,"")
  
  return path_for_builtin_database(dbname)

end

#database_downloadsObject



119
120
121
# File 'lib/protk/constants.rb', line 119

def database_downloads
  return "#{self.protein_database_root}/downloads"
end

#dbexist?(dbname) ⇒ Boolean

Returns:

  • (Boolean)


248
249
250
# File 'lib/protk/constants.rb', line 248

def dbexist?(dbname)
  Pathname.new("#{self.protein_database_root}/#{dbname}").exist?
end

#get_path_for_executable(exec_name_list) ⇒ Object



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

def get_path_for_executable(exec_name_list)
  exec_name_list.each do |exec_name| 
    exec_path=%x[which #{exec_name}].chomp
    return exec_path unless !exec_path || exec_path.length==0
  end
  throw "Unable to locate #{exec_name_list}"
end

#import_fasta_database(dbroot, path_to_fasta_file) ⇒ Object



295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# File 'lib/protk/constants.rb', line 295

def import_fasta_database(dbroot,path_to_fasta_file)
  
  tmp_dbroot=Pathname.new("#{dbroot}/tmp/")

  dest_fasta_file_name=Pathname.new(path_to_fasta_file).basename
  dest_fasta_file_path=Pathname.new("#{tmp_dbroot}#{dest_fasta_file_name}")

  if ( !dest_fasta_file_path.exist? )

    Dir.mkdir(tmp_dbroot) unless tmp_dbroot.exist? && tmp_dbroot.directory?

    throw "Unable to make temporary database directory #{tmp_dbroot}" unless tmp_dbroot.exist?
    
    link_cmd = "ln -s #{path_to_fasta_file} #{dest_fasta_file_path}"
    
    result= %x[#{link_cmd}]
    p result
  end

  check_cmd="#{self.ncbi_tools_bin}/blastdbcmd -info -db #{dest_fasta_file_path}"
  result = %x[#{check_cmd}]

  if ( result=="")
    
    throw "Unable to create temporary database #{dest_fasta_file_path}" unless dest_fasta_file_path.exist?
    cmd="#{self.makeblastdb} -in #{dest_fasta_file_path} -parse_seqids"
    p cmd
    self.run_local(cmd)
    
  end

  return dest_fasta_file_path.to_s
  
end

#initialize_loggersObject



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/protk/constants.rb', line 207

def initialize_loggers
  log_dir = Pathname.new(self.log_file).dirname
  log_dir.mkpath unless log_dir.exist?

  @stdout_logger=Logger.new(STDOUT)
  @file_logger=Logger.new(self.log_file)

  throw "Unable to create file logger at path #{self.log_file}" unless @file_logger!=nil
  throw "Unable to create stdout logger " unless @stdout_logger!=nil

  case @info_level
  when /info/i
    @stdout_logger.level=Logger::INFO
  when /debug/i    
    @stdout_logger.level=Logger::DEBUG
  when /warn/i
    @stdout_logger.level=Logger::WARN
  when /fatal/i
    @stdout_logger.level=Logger::FATAL
  else
    throw "Unknown log level #{@info_level}"
  end

end

#log(message, level) ⇒ Object

Write a message to all logger objects



234
235
236
237
238
239
240
241
# File 'lib/protk/constants.rb', line 234

def log(message,level)
  if ( @stdout_logger == nil || @file_logger == nil)
    initialize_loggers
  end

 @stdout_logger.send(level,message)
 @file_logger.send(level,message)        
end

#log_fileObject



127
128
129
130
131
132
133
134
# File 'lib/protk/constants.rb', line 127

def log_file
  path=@env['log_file']
  if ( path =~ /^\// )
    return path
  else
    return "#{@protk_dir}/#{@env['log_file']}"
  end
end

#makeblastdbObject



95
96
97
98
# File 'lib/protk/constants.rb', line 95

def makeblastdb
  makeblastdbpath=%x[which makeblastdb]
  makeblastdbpath.chomp
end

#mascot2xmlObject



105
106
107
108
# File 'lib/protk/constants.rb', line 105

def mascot2xml
  path=%x[which Mascot2XML]
  path.chomp
end

#msgfplus_rootObject



62
63
64
# File 'lib/protk/constants.rb', line 62

def msgfplus_root
  "#{@protk_dir}/tools/msgfplus"
end

#msgfplusjarObject



78
79
80
81
# File 'lib/protk/constants.rb', line 78

def msgfplusjar
  msgfplus_path=%x[which MSGFPlus.jar]
  msgfplus_path.chomp
end

#omssa_rootObject



58
59
60
# File 'lib/protk/constants.rb', line 58

def omssa_root
    "#{@protk_dir}/tools/omssa"
end

#openms_rootObject



87
88
89
# File 'lib/protk/constants.rb', line 87

def openms_root
    "#{@protk_dir}/tools/openms"
end

#path_for_builtin_database(dbname) ⇒ Object



243
244
245
# File 'lib/protk/constants.rb', line 243

def path_for_builtin_database(dbname)
  "#{self.protein_database_root}/#{dbname}/current.fasta"
end

#protein_database_rootObject



110
111
112
113
114
115
116
117
# File 'lib/protk/constants.rb', line 110

def protein_database_root
  path=@env['protein_database_root']
  if ( path =~ /^\// )
    return path
  else
    return "#{@protk_dir}/#{@env['protein_database_root']}"
  end
end

#pwiz_rootObject



83
84
85
# File 'lib/protk/constants.rb', line 83

def pwiz_root
    return "#{@protk_dir}/tools/pwiz"
end

#run_local(command_string) ⇒ Object

Runs the given command in a local shell



276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/protk/constants.rb', line 276

def run_local(command_string)
  self.log("Command: #{command_string} started",:info)
  status = Open4::popen4("#{command_string} ") do |pid, stdin, stdout, stderr|
    puts "PID #{pid}" 

    stdout.each { |line| self.log(line.chomp,:info) }

    stderr.each { |line| self.log(line.chomp,:warn) }

  end
  if ( status!=0 )
    # We terminated with some error code so log as an error
    self.log( "Command: #{command_string} exited with status #{status.to_s}",:error)
  else
    self.log( "Command: #{command_string} exited with status #{status.to_s}",:info)      
  end
  status     
end

#tandem_binObject



74
75
76
# File 'lib/protk/constants.rb', line 74

def tandem_bin
  get_path_for_executable ["tandem","tandem.exe"]
end

#tandem_rootObject



91
92
93
# File 'lib/protk/constants.rb', line 91

def tandem_root
    "#{@protk_dir}/tools/tandem"
end

#tpp_rootObject



53
54
55
# File 'lib/protk/constants.rb', line 53

def tpp_root
    "#{@protk_dir}/tools/tpp"
end

#update_user_config(dict) ⇒ Object



196
197
198
199
200
201
202
203
204
205
# File 'lib/protk/constants.rb', line 196

def update_user_config(dict)
  user_config_yml = YAML.load_file "#{self.protk_dir}/config.yml" if File.exist? "#{self.protk_dir}/config.yml"

  if ( user_config_yml !=nil )
    dict = user_config_yml.merge dict 
  end

  File.open("#{self.protk_dir}/config.yml", "w") {|file| file.puts(dict.to_yaml) }

end