Class: Bio::Ngs::Utils

Inherits:
Object
  • Object
show all
Defined in:
lib/bio/ngs/utils.rb

Defined Under Namespace

Classes: BinaryNotFound

Constant Summary collapse

@@skip_check_binaries =
false

Class Method Summary collapse

Class Method Details

.binary(name) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/bio/ngs/utils.rb', line 36

def binary(name)
  unless skip_check_binaries?
    begin
      if !(plugin_binaries_found = find_binary_files(name)).empty?
        return plugin_binaries_found.first
      elsif (os_binary = Bio::Command.query_command ["which", name]) != ""
        return os_binary.tr("\n","")
      else
        raise BinaryNotFound.new(:skip_task=>true), "No binary found with this name: #{name}"
      end
    rescue BinaryNotFound => e
      warn e.message
    end
  end
end

.compile_source(tool_name, tool_record, path_external, path_binary) ⇒ Object

uncompress



148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/bio/ngs/utils.rb', line 148

def compile_source(tool_name, tool_record, path_external, path_binary)
  puts "Uncompressing #{tool_name}..."
  tool_dir_name = uncompress_any(tool_name, tool_record)
  puts "Compiling #{tool_name}..."
  cd(tool_dir_name) do
    #system "#{tool_record["lib"]}='#{path_external}/bin/common/lib'" if tool_record["lib"]
    #system "#{tool_record["flags"]}='-O2'" if tool_record["flags"]
    system "PKG_CONFIG_PATH='#{path_external}/bin/common/lib/pkgconfig' ./configure --prefix=#{path_binary} --bindir=#{path_binary}"
    system "make"
    system "make install"
  end #cd
end

.download_and_uncompress(url, fileout) ⇒ Object



117
118
119
120
# File 'lib/bio/ngs/utils.rb', line 117

def download_and_uncompress(url,fileout)
  self.download_with_progress(:url => url,:mode => "b",:filename => fileout)
  self.uncompress_gz_file(fileout)
end

.download_with_progress(opts = {:url => nil, :mode => "", :filename => nil}) ⇒ Object

extend_system_path



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/bio/ngs/utils.rb', line 82

def download_with_progress(opts = {:url => nil, :mode => "", :filename => nil})
require "open-uri"
require "progressbar"
puts "Downloading from #{opts[:url]}"
filename = (opts[:filename]) ? opts[:filename] : opts[:url].split('/')[-1]
mode = (opts[:mode]) ? opts[:mode] : ""
pbar = nil
open(opts[:url],"r"+mode,
  :content_length_proc => lambda {|t|
    if t && 0 < t
      pbar = ProgressBar.new('', t)
      pbar.file_transfer_mode
    end
  },
  :progress_proc => lambda {|s|
    pbar.set s if pbar
  }) do |remote|
    open(filename,"w"+mode) {|file| file.write remote.read(16384) until remote.eof?}
  end
  puts "\nDone"
end

.extend_system_pathObject

tag_filename



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/bio/ngs/utils.rb', line 70

def extend_system_path
  path = File.expand_path(File.dirname(__FILE__))
  common_dir= File.join(path,"ext","bin","common")
  os_dir = File.join(path,"ext","bin",self.os_type)
  sub_dirs = Dir[os_dir+"/*"].select do |file|
    File.directory?(file)
  end.map do |dir|
    ":"+dir
  end.join
  ENV["PATH"]+=":"+common_dir+":"+os_dir + sub_dirs
end

.find_binary_files(binary_name) ⇒ Object

search in the current gem’s directory for installed binaries which the name binary_name it’s a recursive search in common and os specific directories return an array: empty if the binary can not be found otherwise full path to the binaries it is up to the user choose which binary to use, it’s suggested to use the first in the array to have a behavirou similar to the search PATH



189
190
191
192
193
194
# File 'lib/bio/ngs/utils.rb', line 189

def find_binary_files(binary_name)
  path = File.expand_path(File.dirname(__FILE__))
  Find.find(File.join(path,"ext","bin","common"),File.join(path,"ext","bin",self.os_type)).select do |f|
    File.file?(f) && File.basename(f) == binary_name
  end
end

.install_binary(tool_name, tool_record, path_external, path_binary) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
# File 'lib/bio/ngs/utils.rb', line 171

def install_binary(tool_name, tool_record, path_external, path_binary)
  require 'fileutils'
  include FileUtils::Verbose
  puts "Uncompressing #{tool_name}"
  uncompressed_tool_dir_name = uncompress_any(tool_name, tool_record)
  puts "Installing #{tool_name}"
  path_binary_tool = File.join(path_binary,tool_name)
  FileUtils.remove_dir(path_binary_tool) if Dir.exists?(path_binary_tool)
  FileUtils.mkdir(path_binary_tool)
  FileUtils.cp_r "#{uncompressed_tool_dir_name}/.", path_binary_tool, :preserve=>true
end

.just_make(tool_name, tool_record, path_external, path_binary) ⇒ Object

uncompress_compile



161
162
163
164
165
166
167
168
169
# File 'lib/bio/ngs/utils.rb', line 161

def just_make(tool_name, tool_record, path_external, path_binary)
  puts "Uncompressing #{tool_name}..."
  tool_dir_name = uncompress_any(tool_name, tool_record)
  puts "Compiling #{tool_name}..."
  cd(tool_dir_name) do
    system "make"
    FileUtils.cp tool_name,path_binary
  end #cd
end

.os_typeObject

binary



52
53
54
55
56
57
58
59
# File 'lib/bio/ngs/utils.rb', line 52

def os_type
  require 'rbconfig'
  case RbConfig::CONFIG['host_os']
  when /darwin/ then return "osx"
  when /linux/ then return "linux"
  when /mswin|mingw/ then raise NotImplementedError, "This plugin does not run on Windows"
  end
end

.skip_check_binariesObject



28
29
30
# File 'lib/bio/ngs/utils.rb', line 28

def skip_check_binaries
  @@skip_check_binaries=true
end

.skip_check_binaries?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/bio/ngs/utils.rb', line 32

def skip_check_binaries?
  @@skip_check_binaries
end

.tag_filename(filename, tag, extension) ⇒ Object

Remove from filename the dot and the extension, adds the tag and the new extension



62
63
64
65
66
67
68
# File 'lib/bio/ngs/utils.rb', line 62

def tag_filename(filename, tag, extension)
  if filename=~/\..*/
    filename.gsub(/\..*/, "_#{tag}.#{extension}")
  else
    "#{filename}_#{tag}.#{extension}"
  end
end

.uncompress_any(tool_name, tool_record) ⇒ Object

uncompress_command



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/bio/ngs/utils.rb', line 132

def uncompress_any(tool_name, tool_record)
  tool_file_name = "#{tool_record["basename"]}.#{tool_record["suffix"]}"
  tool_dir_name = tool_record["basename"]
  uncompress = uncompress_command(tool_record["suffix"])
  STDERR.puts "#{uncompress} #{tool_file_name}"
  system "#{uncompress} #{tool_file_name}"
  STDERR.puts "completed."
  if Dir.exists?(tool_dir_name)
    tool_dir_name
  elsif Dir.exists?("#{tool_name}-#{tool_record['version']}")
    "#{tool_name}-#{tool_record['version']}"
  else
    raise "BioNGS can not identify the uncompressed destination folder"
  end
end

.uncompress_command(suffix) ⇒ Object



122
123
124
125
126
127
128
129
130
# File 'lib/bio/ngs/utils.rb', line 122

def uncompress_command(suffix)
  case suffix
  when "tar.bz2" then "tar xvfj"
  when "tar.gz" then "tar xvfz"
  when "zip" then "unzip"
  else
    raise "Unkonw suffix."
  end
end

.uncompress_gz_file(file_in) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
# File 'lib/bio/ngs/utils.rb', line 104

def uncompress_gz_file(file_in)
  require 'zlib'
  puts "Uncompressing file #{file_in}"
  file_out = file_in.gsub(/.gz/,"")
  Zlib::GzipReader.open(file_in) {|gz|
    open(file_out,"w") do |file|
      file.write gz.read
    end
  }
  puts "Done\n"
end