Class: Constants

Inherits:
Object
  • Object
show all
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



124
125
126
127
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
# File 'lib/protk/constants.rb', line 124

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=default_config_yml['message_level']
  
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



36
37
38
39
40
# File 'lib/protk/constants.rb', line 36

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.



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

def data_lib_dir
  @data_lib_dir
end

#info_levelObject (readonly)

Returns the value of attribute info_level.



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

def info_level
  @info_level
end

#protk_dirObject (readonly)

Returns the value of attribute protk_dir.



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

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



45
46
47
# File 'lib/protk/constants.rb', line 45

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

#blast_rootObject



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

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

#blastdbcmdObject



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

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



238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/protk/constants.rb', line 238

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



103
104
105
# File 'lib/protk/constants.rb', line 103

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

#dbexist?(dbname) ⇒ Boolean

Returns:

  • (Boolean)


227
228
229
# File 'lib/protk/constants.rb', line 227

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

#import_fasta_database(dbroot, path_to_fasta_file) ⇒ Object



274
275
276
277
278
279
280
281
282
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
# File 'lib/protk/constants.rb', line 274

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



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/protk/constants.rb', line 189

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"
    @stdout_logger.level=Logger::INFO
  when "debug"    
    @stdout_logger.level=Logger::DEBUG
  when "warn"
    @stdout_logger.level=Logger::WARN      
  end

end

#log(message, level) ⇒ Object

Write a message to all logger objects



214
215
216
217
218
219
220
# File 'lib/protk/constants.rb', line 214

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



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

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

#makeblastdbObject



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

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

#mascot2xmlObject



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

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

#msgfplus_rootObject



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

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

#msgfplusjarObject



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

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

#omssa_rootObject



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

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

#openms_rootObject



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

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

#path_for_builtin_database(dbname) ⇒ Object



222
223
224
# File 'lib/protk/constants.rb', line 222

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

#protein_database_rootObject



94
95
96
97
98
99
100
101
# File 'lib/protk/constants.rb', line 94

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



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

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

#run_local(command_string) ⇒ Object

Runs the given command in a local shell



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

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



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

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

#tpp_rootObject



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

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

#update_user_config(dict) ⇒ Object



178
179
180
181
182
183
184
185
186
187
# File 'lib/protk/constants.rb', line 178

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