-
Notifications
You must be signed in to change notification settings - Fork 109
/
format10.rb
415 lines (332 loc) · 8.96 KB
/
format10.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
#
# = bio/appl/fasta/format10.rb - FASTA output (-m 10) parser
#
# Copyright:: Copyright (C) 2002 Toshiaki Katayama <k@bioruby.org>
# License:: The Ruby License
#
# $Id:$
#
require 'bio/appl/fasta'
require 'bio/io/flatfile/splitter'
module Bio
class Fasta
# Summarized results of the fasta execution results.
class Report
# Splitter for Bio::FlatFile
class FastaFormat10Splitter < Bio::FlatFile::Splitter::Template
# creates a new splitter object
def initialize(klass, bstream)
super(klass, bstream)
@delimiter = '>>>'
@real_delimiter = /^\s*\d+\>\>\>\z/
end
# do nothing and returns nil
def skip_leader
nil
end
# gets an entry
def get_entry
p0 = stream_pos()
pieces = []
overrun = nil
first = true
while e = stream.gets(@delimiter)
pieces.push e
if @real_delimiter =~ e then
if first then
first = nil
else
overrun = $&
break
end
end
end
ent = (pieces.empty? ? nil : pieces.join(''))
if ent and overrun then
ent[-overrun.length, overrun.length] = ''
stream.ungets(overrun)
end
p1 = stream_pos()
self.entry_start_pos = p0
self.entry = ent
self.entry_ended_pos = p1
return ent
end
end #FastaFormat10Splitter
# Splitter for Bio::FlatFile
FLATFILE_SPLITTER = FastaFormat10Splitter
def initialize(data)
# Split outputs containing multiple query sequences' results
chunks = data.split(/^(\s*\d+\>\>\>.*)/, 3)
if chunks.size >= 3 then
if chunks[0].strip.empty? then
qdef_line = chunks[1]
data = chunks[1..2].join('')
overruns = chunks[3..-1]
elsif /^\>\>\>/ =~ chunks[0] then
qdef_line = nil
data = chunks.shift
overruns = chunks
else
qdef_line = chunks[1]
data = chunks[0..2].join('')
overruns = chunks[3..-1]
end
@entry_overrun = overruns.join('')
if qdef_line and
/^ *\d+\>\>\>([^ ]+) .+ \- +(\d+) +(nt|aa)\s*$/ =~ qdef_line then
@query_def = $1
@query_len = $2.to_i
end
end
# header lines - brief list of the hits
if list_start = data.index("\nThe best scores are") then
data = data[(list_start + 1)..-1]
data.sub!(/(.*)\n\n>>>/m, '')
@list = $1
else
if list_start = data.index(/\n!!\s+/) then
data = data[list_start..-1]
data.sub!(/\n!!\s+/, '')
data.sub!(/.*/) { |x| @list = x; '' }
else
data = data.sub(/.*/) { |x| @list = x; '' }
end
end
# body lines - fasta execution result
program, *hits = data.split(/\n>>/)
# trailing lines - log messages of the execution
@log = hits.pop
@log.sub!(/.*<\n/m, '')
@log.strip!
# parse results
@program = Program.new(program)
@hits = []
hits.each do |x|
@hits.push(Hit.new(x))
end
end
# piece of next entry. Bio::FlatFile uses it.
attr_reader :entry_overrun
# Query definition. For older reports, the value may be nil.
attr_reader :query_def
# Query sequence length. For older reports, the value may be nil.
attr_reader :query_len
# Returns the 'The best scores are' lines as a String.
attr_reader :list
# Returns the trailing lines including library size, execution date,
# fasta function used, and fasta versions as a String.
attr_reader :log
# Returns a Bio::Fasta::Report::Program object.
attr_reader :program
# Returns an Array of Bio::Fasta::Report::Hit objects.
attr_reader :hits
# Iterates on each Bio::Fasta::Report::Hit object.
def each
@hits.each do |x|
yield x
end
end
# Returns an Array of Bio::Fasta::Report::Hit objects having
# better evalue than 'evalue_max'.
def threshold(evalue_max = 0.1)
list = []
@hits.each do |x|
list.push(x) if x.evalue < evalue_max
end
return list
end
# Returns an Array of Bio::Fasta::Report::Hit objects having
# longer overlap length than 'length_min'.
def lap_over(length_min = 0)
list = []
@hits.each do |x|
list.push(x) if x.overlap > length_min
end
return list
end
# Log of the fasta execution environments.
class Program
def initialize(data)
@definition, *program = data.split(/\n/)
@program = {}
pat = /;\s+([^:]+):\s+(.*)/
program.each do |x|
if pat.match(x)
@program[$1] = $2
end
end
end
# Returns a String containing query and library filenames.
attr_reader :definition
# Accessor for a Hash containing 'mp_name', 'mp_ver', 'mp_argv',
# 'pg_name', 'pg_ver, 'pg_matrix', 'pg_gap-pen', 'pg_ktup',
# 'pg_optcut', 'pg_cgap', 'mp_extrap', 'mp_stats', and 'mp_KS' values.
attr_reader :program
end
class Hit
def initialize(data)
score, query, target = data.split(/\n>/)
@definition, *score = score.split(/\n/)
@score = {}
pat = /;\s+([^:]+):\s+(.*)/
score.each do |x|
if pat.match(x)
@score[$1] = $2
end
end
@query = Query.new(query)
@target = Target.new(target)
end
attr_reader :definition, :score, :query, :target
# E-value score
def evalue
if @score['fa_expect']
@score['fa_expect'].to_f
elsif @score['sw_expect']
@score['sw_expect'].to_f
elsif @score['fx_expect']
@score['fx_expect'].to_f
elsif @score['tx_expect']
@score['tx_expect'].to_f
end
end
# Bit score
def bit_score
if @score['fa_bits']
@score['fa_bits'].to_f
elsif @score['sw_bits']
@score['sw_bits'].to_f
elsif @score['fx_bits']
@score['fx_bits'].to_f
elsif @score['tx_bits']
@score['tx_bits'].to_f
end
end
def direction
@score['fa_frame'] || @score['sw_frame'] || @score['fx_frame'] || @score['tx_frame']
end
# Smith-Waterman score
def sw
@score['sw_score'].to_i
end
# percent identity
def identity
@score['sw_ident'].to_f
end
# overlap length
def overlap
@score['sw_overlap'].to_i
end
# Shortcuts for the methods of Bio::Fasta::Report::Hit::Query
def query_id
@query.entry_id
end
def target_id
@target.entry_id
end
def query_def
@query.definition
end
def target_def
@target.definition
end
def query_len
@query.length
end
# Shortcuts for the methods of Bio::Fasta::Report::Hit::Target
def target_len
@target.length
end
def query_seq
@query.sequence
end
def target_seq
@target.sequence
end
def query_type
@query.moltype
end
def target_type
@target.moltype
end
# Information on matching region
def query_start
@query.start
end
def query_end
@query.stop
end
def target_start
@target.start
end
def target_end
@target.stop
end
def lap_at
[ query_start, query_end, target_start, target_end ]
end
class Query
def initialize(data)
@definition, *data = data.split(/\n/)
@data = {}
@sequence = ''
pat = /;\s+([^:]+):\s+(.*)/
data.each do |x|
if pat.match(x)
@data[$1] = $2
else
@sequence += x
end
end
end
# Returns the definition of the entry as a String.
# You can access this value by Report::Hit#query_def method.
attr_reader :definition
# Returns a Hash containing 'sq_len', 'sq_offset', 'sq_type',
# 'al_start', 'al_stop', and 'al_display_start' values.
# You can access most of these values by Report::Hit#query_* methods.
attr_reader :data
# Returns the sequence (with gaps) as a String.
# You can access this value by the Report::Hit#query_seq method.
attr_reader :sequence
# Returns the first word in the definition as a String.
# You can get this value by Report::Hit#query_id method.
def entry_id
@definition[/\S+/]
end
# Returns the sequence length.
# You can access this value by the Report::Hit#query_len method.
def length
@data['sq_len'].to_i
end
# Returns 'p' for protein sequence, 'D' for nucleotide sequence.
def moltype
@data['sq_type']
end
# Returns alignment start position. You can also access this value
# by Report::Hit#query_start method for shortcut.
def start
@data['al_start'].to_i
end
# Returns alignment end position. You can access this value
# by Report::Hit#query_end method for shortcut.
def stop
@data['al_stop'].to_i
end
end
# Same as Bio::Fasta::Report::Hit::Query but for Target.
class Target < Query; end
end
end # Report
end # Fasta
end # Bio
if __FILE__ == $0
begin
require 'pp'
alias p pp
rescue
end
rep = Bio::Fasta::Report.new(ARGF.read)
p rep
end