Created
August 17, 2010 23:40
-
-
Save yagays/532647 to your computer and use it in GitHub Desktop.
tmapっぽい形式のファイルから全遺伝子IDそれぞれのwigファイル(bedgraph format)を作成します.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#/usr/bin/env ruby | |
require "pp" | |
require "optparse" | |
# Usage : ruby bedgraph.rb [-o output_dir] input_file | |
# Output_dir is optional. Default output_dir is "bedgraph". | |
# | |
# Input_file format must be tab delimited file and the following format below. | |
# gene_id gene_name fpkm chr start end | |
# | |
# Please change the number a[n] (line 22) if your input_file formtat is different form it. | |
def open_t_delimited_file(filename) | |
h = Hash.new([]) | |
id = [] | |
open(filename) {|f| | |
f.each_line do |line| | |
a = line.chomp.split("\t") | |
if a[0][0] != ?- | |
id << a[0] | |
id_sym = a[0].to_sym | |
h[id_sym] += [["chr" + a[3], a[4], a[5], a[2]]] # chr,start,end,fpkm | |
end | |
end | |
} | |
puts filename + " was loaded." | |
return h, id | |
end | |
if __FILE__ == $PROGRAM_NAME | |
output_dir = "bedgraph" | |
ARGV.options do |opt| | |
opt.on( '-o VAL' ) { |a| output_dir = a } | |
opt.parse! | |
end | |
tmap, gene_id_list = open_t_delimited_file(ARGV[0]) | |
gene_id_list_sorted = gene_id_list.uniq | |
if output_dir[-1] == ?/ | |
output_dir = output_dir[0..-2] | |
end | |
if File.exists?(output_dir) | |
puts %|ERROR : "#{output_dir}" directory already exists!| | |
exit 1 | |
end | |
Dir.mkdir(output_dir) | |
gene_id_list_sorted.each do |i| | |
output_list = tmap[i.to_sym].sort{|a,b| a[1] <=> b[1]}.map{|e| e.join("\t")} | |
open("#{output_dir}/#{i}.txt","w") {|f| | |
f.puts "track type=bedGraph name=\"fpkm #{i}\" visibility=full" | |
f.puts output_list | |
} | |
end | |
current_dir = Dir.pwd | |
puts "output : #{current_dir}/#{output_dir}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment