我正在使用其中包含“非规范化”键的文件,并将其读入散列。
哈希部分看起来像这样:
:primer_left_0_sequence=>"TCCTTCCTGCAGCCATGAAG", :primer_right_0_sequence=>"GGCCTCGGCACTTACTTCAT", :primer_left_0=>"90,20", :primer_right_0=>"195,20", :primer_left_0_tm=>"60.035", :primer_right_0_tm=>"60.108", :primer_left_0_gc_percent=>"55.000", :primer_right_0_gc_percent=>"55.000"
后来重复了相同的键,但键中嵌入了不同的数字:
:primer_left_2_sequence=>"TTCTCTCCCTCCTTCCTGCA", :primer_right_2_sequence=>"GGCCTCGGCACTTACTTCAT", :primer_left_2=>"81,20", :primer_right_2=>"195,20", :primer_left_2_tm=>"59.883", :primer_right_2_tm=>"60.108", :primer_left_2_gc_percent=>"55.000",
它重复了几次,每次嵌入在密钥中的整数可能不同。我想将此数据加载到表中,其中列名与键相同,但没有嵌入整数。每次有一组新的重复信息时,我都会插入一个新行。
像这样的东西,但我不知道如何处理嵌入到密钥中的整数:
rawfile_hash.each do |key, value|
# if a new key integer, then create a new record
# ie: primer3_output = Primer3Output.new
if primer3_output.class.accessible_attributes.include?(key)
primer3_output.send("#{key}=", value)
end
end
编辑
为了清楚起见,我正在寻找代码来做这样的事情:
primer3_output = Primer3Output.new
primer3_output.primer_left_sequence = rawfile_hash[:primer_left_0_sequence]
primer3_output.primer_right_sequence = rawfile_hash[:primer_right_0_sequence]
... many entries later ...
primer3_output.save
primer3_output = Primer3Output.new
primer3_output.primer_left_sequence = rawfile_hash[:primer_left_2_sequence]
primer3_output.primer_right_sequence = rawfile_hash[:primer_right_2_sequence]
... etc ...
解决方案
如果重新打开它,我会将其添加为答案。
作为 rake 任务
namespace :db do
task load_table: :environment do
results = []
row = 0
File.open(Rails.root.join("lib", "assets", "P3_INPUT_577_8328_1197.raw"), "r").each_line do |line|
key, value = line.strip.split("=")
id = key.scan(/\d+/).first.to_i
column_name = key.gsub(/_\d+/, "").downcase
results[id] = {"primer3_parameter_id" => id} unless results[id]
results[id][column_name] = value
if id > row
PrimerPair.create!(results[row])
results[row] = nil
row = id
end
end
PrimerPair.create!(results.last)
end
结尾