Skip to content

Instantly share code, notes, and snippets.

@yagays
Created February 23, 2011 10:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yagays/840281 to your computer and use it in GitHub Desktop.
Save yagays/840281 to your computer and use it in GitHub Desktop.
#/usr/bin/env ruby
def open_indel_file(filename)
h = []
open(filename) { |f|
f.each_line do |line|
a = line.chomp.split("\t")
h << a
end
}
h.reverse!{ |a,b| a[0].to_i <=> b[0].to_i}
h
end
def open_fasta_file(filename)
sequence = ""
seq_name = ""
open(filename){ |f|
f.each_line do |line|
if line =~ />(.+)$/
seq_name = $1
else
sequence += line.chomp
end
end
}
return sequence, seq_name
end
if __FILE__ == $PROGRAM_NAME
indels = open_indel_file(ARGV[0])
sequence, seq_name = open_fasta_file(ARGV[1])
output_file_name = "output.fasta" # 出力ファイル名
output_seq_length = 60 # 出力するFASTAの1行あたりの塩基長
indels.each do |i|
s = i[0].to_i
n = i[1]
flag = i[2]
if flag == "insertion"
sequence.insert(s, n)
elsif flag == "deletion"
sequence[s, n.length] = ""
else
puts "unknown format : #{flag}"
end
end
open(output_file_name, "w"){ |f|
f.puts ">" + seq_name
sequence.each_char.each_slice(output_seq_length){|l| f.puts l.join}
f.puts "\n"
}
end
@yagays
Copy link
Author

yagays commented Feb 23, 2011

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment