Skip to content

Instantly share code, notes, and snippets.

@drio
Created July 1, 2009 18:20
Show Gist options
  • Save drio/138951 to your computer and use it in GitHub Desktop.
Save drio/138951 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
#
# vim: tw=80 ts=2 sw=2
#
# merge_eg_output.rb: Merges multiple egenotype outputs
#
# Usage:
#
# 1. Concat all your eg outputs in 1 file:
#
# $ for i in *.eg; do cat $i >> big.txt ; done
#
# 2. Run this script against that file:
#
# $ merge_eg_output.rb big.txt > all.txt
#
# 3. all.txt will have all the probe hits but theh collisions
# fixed
#
class EgOutput
attr_accessor :chrm, :pos, :id, :al_cs, :al_seq, :counters
def initialize(others, counters)
@chrm, @pos, @id, @ref, @var = others
@counters = counters.map{|c| c.to_i }
end
def +(ec)
new_obj = self.clone
([email protected]).each do |i|
new_obj.counters[i] = @counters[i] + ec.counters[i]
end
new_obj
end
def to_s
"#{@chrm},#{@pos},#{@id},#{@ref},#{@var}," +
@counters.inject("") {|all, c| all << c.to_s + ","}.gsub(/,$/, '')
end
end
entries = {}
File.open(ARGV[0]).each do |i|
#$stderr.puts "--> " + i
eo = EgOutput.new(i.split(',')[0,5], i.split(",")[-15..-1])
entries[eo.id] = entries.key?(eo.id) ? entries[eo.id] + eo : eo
end
entries.each_value { |e| puts e }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment