Class: Bio::EBD::DistanceMatrix
- Inherits:
-
Object
- Object
- Bio::EBD::DistanceMatrix
- Defined in:
- lib/bio-express_beta_diversity/distance_matrix.rb
Overview
Distance matrix output from express beta diversity (Donovan Parks et al). Similar to phylip distance format, but not quite the same.
Instance Attribute Summary collapse
-
#distance_matrix ⇒ Object
Returns the value of attribute distance_matrix.
-
#sample_names ⇒ Object
Returns the value of attribute sample_names.
Class Method Summary collapse
Instance Method Summary collapse
-
#distance(sample1, sample2) ⇒ Object
Return the floating point distance between a pair of samples.
-
#initialize ⇒ DistanceMatrix
constructor
A new instance of DistanceMatrix.
- #number_of_samples ⇒ Object
Constructor Details
#initialize ⇒ DistanceMatrix
Returns a new instance of DistanceMatrix.
12 13 14 15 |
# File 'lib/bio-express_beta_diversity/distance_matrix.rb', line 12 def initialize @distance_matrix = [] @sample_names = [] end |
Instance Attribute Details
#distance_matrix ⇒ Object
Returns the value of attribute distance_matrix.
9 10 11 |
# File 'lib/bio-express_beta_diversity/distance_matrix.rb', line 9 def distance_matrix @distance_matrix end |
#sample_names ⇒ Object
Returns the value of attribute sample_names.
10 11 12 |
# File 'lib/bio-express_beta_diversity/distance_matrix.rb', line 10 def sample_names @sample_names end |
Class Method Details
.parse_from_file(filename) ⇒ Object
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 |
# File 'lib/bio-express_beta_diversity/distance_matrix.rb', line 17 def self.parse_from_file(filename) ebd = Bio::EBD::DistanceMatrix.new line = 1 expected_number_of_samples = nil CSV.foreach(filename, :col_sep => "\t") do |row| if line == 1 # First line is the number of samples raise "Parse exception at this row: #{row.inspect}, expected" unless row.length == 1 expected_number_of_samples = row[0].to_i else # all other lines are the sample names and then the lower # triangular distance matrix sample_index = line-2 raise "Parse exception at this row: #{row.inspect}" unless row.length == sample_index+1 ebd.sample_names.push row[0] distances = row[1...row.length].collect{|d| d.to_f} ebd.distance_matrix[sample_index] = distances end line += 1 end return ebd end |
Instance Method Details
#distance(sample1, sample2) ⇒ Object
Return the floating point distance between a pair of samples
48 49 50 51 52 53 54 55 56 57 |
# File 'lib/bio-express_beta_diversity/distance_matrix.rb', line 48 def distance(sample1, sample2) index1 = @sample_names.find_index{|n| n==sample1} index2 = @sample_names.find_index{|n| n==sample2} raise "error extracting the EBD distance between #{sample1.inspect} and #{sample2.inspect}" unless index1 and index2 and index2 != index1 if index1 > index2 return @distance_matrix[index1][index2] else return @distance_matrix[index2][index1] end end |
#number_of_samples ⇒ Object
43 44 45 |
# File 'lib/bio-express_beta_diversity/distance_matrix.rb', line 43 def number_of_samples @sample_names.length end |