/
fasta.rb
48 lines (45 loc) · 1.66 KB
/
fasta.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
require "rubygems"
require "bio"
require "rubydas/model/sequence"
module RubyDAS
module Loader
class FASTA
def store filename
puts "Storing #{filename}"
ff = Bio::FlatFile.open(Bio::FastaFormat, filename)
ff.each do |entry|
s = Sequence.new(
:public_id => entry.entry_id,
:length => entry.length,
:label => entry.entry_id
)
unless s.save
raise s.errors.reduce("Error saving:") {|m, e| m + e.to_s}
end
current_seq = ""
pos = 0
entry.seq.each_char do |c|
pos += 1
current_seq << c
if current_seq.length >= 1000 || pos >= entry.length
sf = SequenceFragment.new(
:fragment => current_seq,
:start => pos - current_seq.length,
:end => pos,
:sequence => s
)
unless sf.save
raise sf.errors.reduce("Error saving:") do |m, e|
m + e.to_s
end
end
current_seq = ""
print "."
STDOUT.flush
end
end
end
end
end
end
end