This is a parser class for phylip distance matrix data created by dnadist, protdist, or restdist commands.
Generates a new phylip distance matrix formatted text as a string.
# File lib/bio/appl/phylip/distance_matrix.rb, line 69 def self.generate(matrix, otu_names = nil, options = {}) if matrix.row_size != matrix.column_size then raise "must be a square matrix" end otus = matrix.row_size names = (0...otus).collect do |i| name = ((otu_names and otu_names[i]) or "OTU#{i.to_s}") name end data = (0...otus).collect do |i| x = (0...otus).collect { |j| sprintf("%9.6f", matrix[i, j]) } x.unshift(sprintf("%-10s", names[i])[0, 10]) str = x[0, 7].join(' ') + "\n" 7.step(otus + 1, 7) do |k| str << ' ' + x[k, 7].join(' ') + "\n" end str end sprintf("%5d\n", otus) + data.join('') end
creates a new distance matrix object
# File lib/bio/appl/phylip/distance_matrix.rb, line 27 def initialize(str) data = str.strip.split(/(?:\r\n|\r|\n)/) @otus = data.shift.to_s.strip.to_i prev = nil data.collect! do |x| if /\A +/ =~ x and prev then prev.concat x.strip.split(/\s+/) nil else prev = x.strip.split(/\s+/) prev end end data.compact! if data.size != @otus then raise "inconsistent data (OTUs=#{@otus} but #{data.size} rows)" end @otu_names = data.collect { |x| x.shift } mat = data.collect do |x| if x.size != @otus then raise "inconsistent data (OTUs=#{@otus} but #{x.size} columns)" end x.collect { |y| y.to_f } end @matrix = Matrix.rows(mat, false) @original_matrix = Matrix.rows(data, false) end
Generated with the Darkfish Rdoc Generator 2.