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



128
129
130
131
132
133
134
135
136
137
138
139
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
# File 'lib/protk/constants.rb', line 128

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



111
112
113
# File 'lib/protk/constants.rb', line 111

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

#blastdbcmdObject



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

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



247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/protk/constants.rb', line 247

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



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

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

#dbexist?(dbname) ⇒ Boolean

Returns:

  • (Boolean)


236
237
238
# File 'lib/protk/constants.rb', line 236

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

#import_fasta_database(dbroot, path_to_fasta_file) ⇒ Object



283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/protk/constants.rb', line 283

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



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/protk/constants.rb', line 195

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



222
223
224
225
226
227
228
229
# File 'lib/protk/constants.rb', line 222

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



115
116
117
118
119
120
121
122
# File 'lib/protk/constants.rb', line 115

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

#makeblastdbObject



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

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

#mascot2xmlObject



93
94
95
96
# File 'lib/protk/constants.rb', line 93

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



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

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



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

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

#path_for_builtin_database(dbname) ⇒ Object



231
232
233
# File 'lib/protk/constants.rb', line 231

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

#protein_database_rootObject



98
99
100
101
102
103
104
105
# File 'lib/protk/constants.rb', line 98

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



71
72
73
# File 'lib/protk/constants.rb', line 71

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

#run_local(command_string) ⇒ Object

Runs the given command in a local shell



264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/protk/constants.rb', line 264

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_rootObject



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

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



184
185
186
187
188
189
190
191
192
193
# File 'lib/protk/constants.rb', line 184

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