Skip to content

Instantly share code, notes, and snippets.

@cth
Created September 22, 2014 10:51
Show Gist options
  • Save cth/d85f91c0526e2748b438 to your computer and use it in GitHub Desktop.
Save cth/d85f91c0526e2748b438 to your computer and use it in GitHub Desktop.
Script to swap alleles in imputed .gprobs files (genotype likelihoods), e.g., from IMPUTE2
# Script to swap alleles in imputed .gprobs files (genotype likelihoods), e.g., from IMPUTE2
# ruby swapgeno.rb genotypes.gprobs swap_snps.txt swapped.gprobs
# swap_snps.txt is a list of SNPs for which genotypes should be swapped (one per line in file)
# Read list of snps to swap
swap_snps = {}
File.open(ARGV[1]) { |sf| sf.each { |l| swap_snps[l.chomp] = true } }
File.open(ARGV[2],"w") do |outfile|
File.open(ARGV[0]) do |file|
file.each do |line|
fields = line.chomp.split(" ")
next unless swap_snps[fields[1]] == true
i=5
new_fields = fields[0..2]
new_fields << fields[4]
new_fields << fields[3]
begin
rgeno = [fields[i],fields[i+1],fields[i+2]].reverse
# Check that we are getting the right three fields
sum = rgeno.inject { |i,j| i.to_f+j.to_f }
throw "wrong sum=#{sum}" unless (sum-1).abs < 0.01
1.upto(3) { |i| new_fields << rgeno.shift }
i=i+3
end while i < fields.size
outfile.puts new_fields.join(" ")
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment