class BioDSL::Filesys

Class for handling filesystem manipulations.

Attributes

io[R]

Public Class Methods

new(ios) click to toggle source
# File lib/BioDSL/filesys.rb, line 111
def initialize(ios)
  @io = ios
end
open(*args) { |new(ios)| ... } click to toggle source

Open a file which may be compressed with gzip og bzip2.

# File lib/BioDSL/filesys.rb, line 67
def self.open(*args)
  file    = args.shift
  mode    = args.shift
  options = args.shift || {}

  if mode == 'w'
    case options[:compress]
    when :gzip
      ios, = Open3.pipeline_w('gzip -f', out: file)
    when :bzip, :bzip2
      ios, = Open3.pipeline_w('bzip2 -c', out: file)
    else
      ios = File.open(file, mode, options)
    end
  else
    type = if file.respond_to? :path
             `file -Lk #{file.path}`
           else
             `file -Lk #{file}`
           end

    ios = case type
          when /gzip/
            IO.popen("gzip -cd #{file}")
          when /bzip/
            IO.popen("bzcat #{file}")
          else
            File.open(file, mode, options)
          end
  end

  if block_given?
    begin
      yield new(ios)
    ensure
      ios.close
    end
  else
    return new(ios)
  end
end
tmpfile(tmp_dir = ENV['TMPDIR']) click to toggle source

Class method that returns a path to a unique temporary file. If no directory is specified reverts to the systems tmp directory.

# File lib/BioDSL/filesys.rb, line 58
def self.tmpfile(tmp_dir = ENV['TMPDIR'])
  time = Time.now.to_i
  user = ENV['USER']
  pid  = $PID
  path = tmp_dir + [user, time + pid, pid].join('_') + '.tmp'
  path
end
which(cmd) click to toggle source

Cross-platform way of finding an executable in the $PATH.

which('ruby') #=> /usr/bin/ruby
# File lib/BioDSL/filesys.rb, line 43
def self.which(cmd)
  exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']

  ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
    exts.each do |ext|
      exe = File.join(path, "#{cmd}#{ext}")
      return exe if File.executable?(exe) && !File.directory?(exe)
    end
  end

  nil
end

Public Instance Methods

close() click to toggle source
# File lib/BioDSL/filesys.rb, line 131
def close
  @io.close
end
each() { |line| ... } click to toggle source

Iterator method for parsing entries.

# File lib/BioDSL/filesys.rb, line 140
def each
  return to_enum :each unless block_given?

  @io.each { |line| yield line }
end
eof?() click to toggle source
# File lib/BioDSL/filesys.rb, line 135
def eof?
  @io.eof?
end
gets() click to toggle source
# File lib/BioDSL/filesys.rb, line 115
def gets
  @io.gets
end
puts(*args) click to toggle source
# File lib/BioDSL/filesys.rb, line 119
def puts(*args)
  @io.puts(*args)
end
read() click to toggle source
# File lib/BioDSL/filesys.rb, line 123
def read
  @io.read
end
write(arg) click to toggle source
# File lib/BioDSL/filesys.rb, line 127
def write(arg)
  @io.write arg
end