Class: Bio::Krona
- Inherits:
-
Object
- Object
- Bio::Krona
- Defined in:
- lib/bio-krona/krona.rb
Class Method Summary collapse
-
.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.
-
.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) ** :multiple_samples: count_hash is a hash of sample names to hash of Array => Numeric, when multiple samples are to be included [default false].
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
77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/bio-krona/krona.rb', line 77 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) ** :multiple_samples: count_hash is a hash of sample names to hash of Array => Numeric, when multiple samples are to be included [default false]
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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/bio-krona/krona.rb', line 12 def self.html(count_hash, ={}) raise unless count_hash.kind_of?(Hash) [:krona_path] ||= %(ktImportText) get_tempfile_from_count_hash = lambda do |ch| tempfile = Tempfile.new('krona') ch.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 end input_tempfiles = [] input_names = [] if [:multiple_samples] input_names = count_hash.keys input_tempfiles = count_hash.values.collect{|ch| get_tempfile_from_count_hash.call(ch)} else input_names = [nil] input_tempfiles = [get_tempfile_from_count_hash.call(count_hash)] end pairs = [] input_names.each_with_index do |name, i| tmpname = input_tempfiles[i].path if name.nil? pairs.push tmpname else pairs.push "#{tmpname},#{name}" end end Tempfile.open('krona_out') do |output| output.close command = [ [:krona_path], '-o', output.path, pairs, ].flatten if [:resources_url] command.push '-u' command.push [:resources_url] end Bio::Command.call_command_open3(command) do |stdin, stdout, stderr| err = stderr.read raise err unless err=='' end input_tempfiles.each{|t| t.close} return File.open(output.path).read end end |