-
Notifications
You must be signed in to change notification settings - Fork 0
/
fasta_to_csv.rb
53 lines (44 loc) · 995 Bytes
/
fasta_to_csv.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
require_relative 'fasta.rb'
require 'optparse'
usage = <<-USAGE
ruby fasta_to_csv.rb [option] fasta_file
-F: --FS field seperator, tab is default
-s: --sanitize type, to remove invalid character in the sequence, type should be nu or aa
USAGE
if ARGV.size ==0
puts usage
abort
end
delim="\t"
sanitize=""
OptionParser.new do |opts|
opts.banner = "Usage: ruby csv_to_fasta.rb csf_file"
opts.on("-F seperator","--FS","Field seperator") do |v|
if v !=""
delim=v
end
end
opts.on("-s type","--sanitize","sanitize the sequence") do |type|
sanitize=type
end
end.parse!
file_name = ARGV.shift
if file_name.empty?
puts usage
abort
end
if !File.exists?(file_name)
puts "file not found"
puts usage;
abort
end
fp=Fasta_parser.new(file_name)
while ft = fp.next_fasta do
if sanitize=="nu"
ft.sanitize
elsif sanitize=="aa"
ft.sanitize("aa")
end
puts ft.header[1..-1]+delim+ft.seq
end
fp.close