Rubyのハッシュからヒストグラム(単にASCIIの 'text'バージョンではなくmatplotlibに似た出力)を表示することに興味があります。各ハッシュキーは各ビンの 'ラベル'として表示され、各キーのそれぞれの値は周波数(y軸)で表示されます。私は 'rinruby'を使用しようとしましたが、複数回試行した後、プログラムがハングアップします。私はWindowsマシンとJRuby 9.1.0.0を使用しています。 gnuplotの宝石はむしろ混乱していたので、建設的な提案ができるかどうか疑問に思っていました。これはではなく、の宿題です。Rubyを使ってハッシュからヒストグラムを作成する
# Import the 'bioruby' package
require 'bio'
# Construct a new hash object referenced by the variable 'aaf'
aaf = Hash.new(0)
# Construct a new array object referenced by 'aa'
aa = []
# Prompt the user for a file name. Wrap the user input code in
# an exception handling block to create a more "robust" solution
begin
print 'Please enter a file name: '
# Read the keyboard input as a string and remove the newline character.
# The input is summarily referenced by the 'filename' variable
filename = gets.chomp()
# Display a blank line for formatting purposes
puts()
# Open and read the file name provided and remove the newlines from the
# sequence file.
seq = File.new(filename, 'r').read().chomp()
# Catch the exception thrown above, display the string, and return
# control to the 'begin' block for additional user input
rescue
puts "I'm sorry. I was unable to locate that file."
retry
end
# Create a new sequence object using the DNA sequence data obtained
# from the file name provided
seq = Bio::Sequence::NA.new(seq)
# Use the 'translate.names' method in the bioruby package to obtain
# the complete name of each amino acid in the 'seq' object and reference
# them via the 'aa' object
aa = seq.translate.names
# Iterate/Loop over each amino acid name in the 'aa' array object using
# 'name' as a temporary variable. The name of each amino acid is
# associated with the frequency of occurrence. Example: 'Lysine 110' would
# indicate that 110 Lysine residues were contained in the original
# sequence file.
aa.each { |name|
aaf[name] += 1
}
# Iterate/Loop over the newly populated hash structure while ignoring
# any line (key) that is a blank line (/^$/ is a regular expression anchor
# pattern that checks for the beginning, '^', and end, '$', of a line. The
# line (key) is blank if the 'if' statement returns a boolean result of true.
# A blank line in this instance would indicate the presence of a stop codon.
# Each amino acid and its respective frequency is displayed
aaf.each do |k, v|
next if "#{k}" =~ /^$/
print "#{k}", "->", "#{v}\n"
end
ありがとうございます。簡単なチャートについては
完了しました。上記を参照してください:) – CaitlinG
空白を心配する必要はありません - ラインやスペース; 'Bio :: Sequence'はそれらを無視します。投稿されたコードは正しいデータを表示するので、それをグラフ形式に変換したいだけですか? –
はい、正確です。 Bio :: Sequenceに関する情報をありがとう。 – CaitlinG