Class: Bio::Krona

Inherits:
Object
  • Object
show all
Defined in:
lib/bio-krona/krona.rb

Class Method Summary collapse

Class Method Details

.collapse(count_hash, max_level) ⇒ Object

Take a count_hash (hash of Array => Numeric), and collapse the array down to some maximum number of levels. In krona, the collapsed output would now have less (max_level) rings.

Returns the collapsed count_hash



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/bio-krona/krona.rb', line 53

def self.collapse(count_hash, max_level)
  raise unless max_level.kind_of?(Integer) and max_level > 0
  new_count_hash = {}
  count_hash.each do |array, count|
    raise unless array.kind_of?(Array)
    raise unless count.kind_of?(Numeric)

    new_count_hash[array[0...max_level]] ||= 0
    new_count_hash[array[0...max_level]] += count
  end
  return new_count_hash
end

.html(count_hash, options = {}) ⇒ Object

Given a count_hash, return the html generated by krona

  • count_hash: hash of Array => Numeric, where the array is a list of descriptors (e.g. [Eukaryota, Metazoa, Chordata]), and the Numeric is a count for the number of observations of that description

  • options:

** :krona_path: path to the ktImportText script in the krona directory (default ‘ktImportText’ i.e. assuming it is already in the PATH) ** :resources_url: URL of krona resources (i.e. the -u option of ktImportText)



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/bio-krona/krona.rb', line 11

def self.html(count_hash, options={})
  raise unless count_hash.kind_of?(Hash)
  options[:krona_path] ||= %(ktImportText)

  Tempfile.open('krona') do |tempfile|
    count_hash.each do |array, count|
      raise unless array.kind_of?(Array)
      raise unless count.kind_of?(Numeric)

      tempfile.puts [
        count,
        array
      ].flatten.join("\t")
    end
    tempfile.close

    Tempfile.open('krona_out') do |output|
      output.close

      command = [
        options[:krona_path],
        '-o',
        output.path,
        tempfile.path,
      ].flatten
      if options[:resources_url]
        command.push '-u'
        command.push options[:resources_url]
      end
      Bio::Command.call_command_open3(command) do |stdin, stdout, stderr|
        err = stderr.read
        raise err unless err==''
      end

      return File.open(output.path).read
    end
  end
end