Module: ViennaRna::Utils

Defined in:
lib/vienna_rna/modules/utils.rb

Class Method Summary collapse

Class Method Details

.fastas_from_file(path) ⇒ Object



4
5
6
7
# File 'lib/vienna_rna/modules/utils.rb', line 4

def fastas_from_file(path)
  # Force it to not be lazy.
  Bio::FlatFile.auto(path).to_enum.map { |fasta| fasta }
end

.regress(x, y, degree) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/vienna_rna/modules/utils.rb', line 25

def regress(x, y, degree)
  x_data   = x.map { |i| (0..degree).map { |power| i ** power.to_f } }
  x_matrix = Matrix[*x_data]
  y_matrix = Matrix.column_vector(y)

  ((x_matrix.transpose * x_matrix).inverse * x_matrix.transpose * y_matrix).transpose.to_a[0]
end

.write_fastas!(fastas, directory, base_name, group_size = 10) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/vienna_rna/modules/utils.rb', line 9

def write_fastas!(fastas, directory, base_name, group_size = 10)
  fastas.each_slice(group_size).each_with_index do |fasta_group, i|
    path = File.join(directory, base_name + "_#{i}.fa")
    
    unless File.exists?(path)
      File.open(path, "w") do |file|
        fasta_group.each do |folding|
          file.write(">%s\n%s\n" % [folding.fasta.definition, folding.fasta.seq])
        end
      end
    else
      puts "Warning: file '#{path}' exists. Skipping."
    end
  end
end