I would like to use ruby to process the file with the following coordinates, but I can't think about what to do, so I'm at a standstill.Could you give me an idea?
Before Processing
group start end id distance value1 value2
A58177756 id1 193903
A58177754 id2 1937-21
A58177752 id3 1935-41
A58177747 id4 1930-91
A5817 10699 id5 1938-501
A58197756 id6 1937-21
A58197755 id7 1936-31
A 5828 7756 id8 1928-111
A59838180 id9 2197 12
A59838175 id102192-52
A59838174 id112191-61
A59848175 id122191-61
For coordinates with overlap as above, for example,
(id2; end – id1; end) – (id2; start – id1; start) = id2; value1
(id3; end – id1; end) – (id3; start – id1; start) = id3; value1
If these conditions are met, id1 and id2 and id3 are considered to belong to the same group, and I would like to add the count to id1 value2 with the largest value1 in the group.
After Processing
group start end id length value1 value2
A58177756 id1 193909
A5817 10699 id5 1938-501
A59838180 id9 2197 15
Thank you very much for your information.
ruby
require'pp'
list = [ ]
open('/tmp/hoge.txt') .read.split("\n").select.with_index{|a,i|i!=0}.map {|a|a.split("")}.map do|a|
list<<{
group: a[0],
start: a[1].to_i,
end —a[2].to_i,
id: a[3],
distance —a[4].to_i,
value1: a[5].to_i,
value2 —a[6].to_i
}
end
id_comb_list=list.map {|a|a[:id]}.combination(3).to_a
id_comb_list.each do|ic|
id1 = list.find { | a | a [:id] == ic[0]}
id2 = list.find { | a | a [:id] == ic[1]}
id3 = list.find { | a | a [:id] == ic[2]}
if((id2[:end]-id1[:end])-(id2[:start]-id1[:start])==id2[:value1])&
((id3[:end]-id1[:end])-(id3[:start]-id1[:start])==id3[:value1])
target = [id1, id2, id3].sort_by {|c|c[:value1]}.last
target [:value2] + = target [:value1]
end
end
ppl list
© 2024 OneMinuteCode. All rights reserved.