找到文件中每个基因的最长转录本的最佳方法是什么?
我的文件包含以下内容:
Gene Transcript Transcript's size
NONDMEG000107 NONDMET000231 329
NONDMEG000107 NONDMET000234 21
NONDMEG000107 NONDMET000235 131
NONDMEG000107 NONHSAT000018 283
NONHSAG000008 NONHSAT000019 322
NONHSAG000008 NONHSAT000020 1300
NONHSAG000008 NONHSAT000021 971
NONHSAG000008 NONHSAT000022 277
NONHSAG000008 NONHSAT000043 335
NONHSAG000008 NONHSAT000044 179
输出应为:
NONDMEG000107 NONDMET000231 329
NONHSAG000008 NONHSAT000020 1300
答案 0 :(得分:2)
目前还不清楚有哪些数据类型,但假设每个条目都是gen(字符串),transcript(字符串)和size(整数)的数组:
data = [
['NONDMEG000107', 'some transcript', 10],
['NONDMEG000107', 'some transcript', 3229],
['NONDMEG000107', 'some transcript', 312],
['NONDMEG000107', 'some transcript', 144],
['NONHSAG000008', 'some transcript', 1],
['NONHSAG000008', 'some transcript', 345],
['NONHSAG000008', 'some transcript', 100],
['NONHSAG000008', 'some transcript', 324]
]
data.group_by(&:first).map{|_,v| v.max_by(&:last)}
#=> [["NONDMEG000107", "some transcript", 3229], ["NONHSAG000008", "some transcript", 345]]
答案 1 :(得分:0)
您可以将文件读作CSV
,然后检查每一行并确定哪个基因具有最大转录大小。输入文件中需要进行一项更改,标题应为
Gene Transcript Transcript_size
这是代码:
require "csv"
require "pp"
hash = {}
CSV.foreach("/path/to/gene_data_file", :col_sep => " ", :headers => :first_row) do |row|
hash[row["Gene"]] = {} if not hash[row["Gene"]]
hash[row["Gene"]]["Transcript"] = "" if not hash[row["Gene"]]["Transcript"]
hash[row["Gene"]]["Transcript_size"] = 0 if not hash[row["Gene"]]["Transcript_size"]
if row["Transcript_size"].to_i > hash[row["Gene"]]["Transcript_size"]
hash[row["Gene"]]["Transcript"] = row["Transcript"]
hash[row["Gene"]]["Transcript_size"] = row["Transcript_size"].to_i
end
end
pp hash
#=> {"NONDMEG000107"=>{"Transcript"=>"NONDMET000231", "Transcript_size"=>329},
# "NONHSAG000008"=>{"Transcript"=>"NONHSAT000020", "Transcript_size"=>1300}}