Skip to content

Instantly share code, notes, and snippets.

@cth
Created March 10, 2015 09:03
Show Gist options
  • Save cth/cb6a46004f5d94b965d8 to your computer and use it in GitHub Desktop.
Save cth/cb6a46004f5d94b965d8 to your computer and use it in GitHub Desktop.
def mean_std_dev(arr,n)
sum=0
mean = arr.inject{|sum,x| sum + x } / n.to_f
total=0
[mean, Math.sqrt(arr.inject{|total,x| total + ((mean-x)*(mean-x)) } / n.to_f) ]
end
def percentile_depths(arr,n,pct)
samples_in_percentile = (n * pct).floor
(arr.sort.reverse.slice(0,samples_in_percentile)).min
end
def depth_percentages(arr,n)
more_than_n = 0
arr.each do |depth|
more_than_n = more_than_n + 1 if depth >= n
end
more_than_n.to_f / arr.size
end
prev=nil
File.open("depths.tsv") do |file|
file.each do |line|
#puts line.slice(0,100)
next if line =~ /^#.*/
fields = line.chomp.split("\t")
next if [ fields[0], fields[1] ] == prev
depths = []
8.upto(fields.size-1) do |i|
depths << ((fields[i].split(/:/))[1]).to_i
end
depth_mean = mean_std_dev(depths,depths.size)
puts "#{fields[0]} #{fields[1]} #{fields[2]} #{depth_mean[0]} #{depth_mean[1]} " +
percentile_depths(depths,depths.size,0.99).inspect + " " +
percentile_depths(depths,depths.size,0.95).inspect + " " +
depth_percentages(depths,20).inspect + " " +
depth_percentages(depths,1).inspect
prev = [fields[0], fields[1]]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment