Advertisement
Guest User

Untitled

a guest
Apr 26th, 2015
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. def mean_std_dev(arr,n)
  2. sum=0
  3. mean = arr.inject{|sum,x| sum + x } / n.to_f
  4. total=0
  5. [mean, Math.sqrt(arr.inject{|total,x| total + ((mean-x)*(mean-x)) } / n.to_f) ]
  6. end
  7.  
  8. def percentile_depths(arr,n,pct)
  9. samples_in_percentile = (n * pct).floor
  10. (arr.sort.reverse.slice(0,samples_in_percentile)).min
  11. end
  12.  
  13. def depth_percentages(arr,n)
  14. more_than_n = 0
  15. arr.each do |depth|
  16. more_than_n = more_than_n + 1 if depth >= n
  17. end
  18. more_than_n.to_f / arr.size
  19. end
  20.  
  21. prev=nil
  22. File.open("depths.tsv") do |file|
  23. file.each do |line|
  24. #puts line.slice(0,100)
  25. next if line =~ /^#.*/
  26.  
  27. fields = line.chomp.split("\t")
  28.  
  29. next if [ fields[0], fields[1] ] == prev
  30.  
  31. depths = []
  32. 8.upto(fields.size-1) do |i|
  33. depths << ((fields[i].split(/:/))[1]).to_i
  34. end
  35. depth_mean = mean_std_dev(depths,depths.size)
  36. puts "#{fields[0]} #{fields[1]} #{fields[2]} #{depth_mean[0]} #{depth_mean[1]} " +
  37. percentile_depths(depths,depths.size,0.99).inspect + " " +
  38. percentile_depths(depths,depths.size,0.95).inspect + " " +
  39. depth_percentages(depths,20).inspect + " " +
  40. depth_percentages(depths,1).inspect
  41.  
  42. prev = [fields[0], fields[1]]
  43. end
  44. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement