class Bio::Phylip::DistanceMatrix
This is a parser class for phylip distance matrix data created by dnadist, protdist, or restdist commands.
Attributes
matrix[R]
distance matrix (returns Ruby’s Matrix object)
original_matrix[R]
matrix contains values as original strings. Use it when you doubt precision of floating-point numbers.
otu_names[R]
names of OTUs
otus[R]
number of OTUs
Public Class Methods
generate(matrix, otu_names = nil, options = {})
click to toggle source
Generates a new phylip distance matrix formatted text as a string.
# File lib/bio/appl/phylip/distance_matrix.rb 69 def self.generate(matrix, otu_names = nil, options = {}) 70 if matrix.row_size != matrix.column_size then 71 raise "must be a square matrix" 72 end 73 otus = matrix.row_size 74 names = (0...otus).collect do |i| 75 name = ((otu_names and otu_names[i]) or "OTU#{i.to_s}") 76 name 77 end 78 data = (0...otus).collect do |i| 79 x = (0...otus).collect { |j| sprintf("%9.6f", matrix[i, j]) } 80 x.unshift(sprintf("%-10s", names[i])[0, 10]) 81 82 str = x[0, 7].join(' ') + "\n" 83 7.step(otus + 1, 7) do |k| 84 str << ' ' + x[k, 7].join(' ') + "\n" 85 end 86 str 87 end 88 sprintf("%5d\n", otus) + data.join('') 89 end
new(str)
click to toggle source
creates a new distance matrix object
# File lib/bio/appl/phylip/distance_matrix.rb 27 def initialize(str) 28 data = str.strip.split(/(?:\r\n|\r|\n)/) 29 @otus = data.shift.to_s.strip.to_i 30 prev = nil 31 data.collect! do |x| 32 if /\A +/ =~ x and prev then 33 prev.concat x.strip.split(/\s+/) 34 nil 35 else 36 prev = x.strip.split(/\s+/) 37 prev 38 end 39 end 40 data.compact! 41 if data.size != @otus then 42 raise "inconsistent data (OTUs=#{@otus} but #{data.size} rows)" 43 end 44 @otu_names = data.collect { |x| x.shift } 45 mat = data.collect do |x| 46 if x.size != @otus then 47 raise "inconsistent data (OTUs=#{@otus} but #{x.size} columns)" 48 end 49 x.collect { |y| y.to_f } 50 end 51 @matrix = Matrix.rows(mat, false) 52 @original_matrix = Matrix.rows(data, false) 53 end