/
orf_emitter.rb
208 lines (183 loc) · 6.4 KB
/
orf_emitter.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
require 'set'
module Bio
module Big
module FrameCodonHelpers
STOP_CODONS = Set.new(%w{TAG TAA TGA UAG UAA UGA})
START_CODONS = Set.new(%w{ATG AUG})
# Track sequence position in parent sequence (in nucleotides)
module TrackSequenceTrait
attr_accessor :track_ntseq_pos
def TrackSequenceTrait.update_sequence_pos orfs, ntseq_pos
orfs.each { | orf | orf.track_ntseq_pos = ntseq_pos + orf.pos*3 }
orfs
end
end
# Functions that move a frame forward, or backward,
# creating new short frames.
module CreateShortFrame
def CreateShortFrame.create_right fr,orfs,rseq
seq = fr.seq
ntseq_pos = fr.ntseq_pos
remove = if orfs.size > 0
orfs.last.rpos*3
else
0
end
ntseq_pos += remove
nseq = seq[remove..-1] + rseq
ShortFrameState.new nseq,ntseq_pos,fr.min_size_codons*3
end
def CreateShortFrame.create_left fr,orfs,lseq
seq = fr.seq # original sequence
ntseq_pos = fr.ntseq_pos # right side of seq
bridge = seq.size % 3 # chomp left side
remove = if orfs.size > 0
# remove the tail of the sequence
seq.size - bridge - (orfs.first.pos)*3 +1
else
0
end
ntseq_pos += remove
nseq = lseq + seq[0..(seq.size-remove)]
ShortFrameState.new nseq,ntseq_pos,fr.min_size_codons*3
end
end
class FrameCodonSequence
include Enumerable
include TrackSequenceTrait
attr_reader :pos # codon position in short parent sequence
attr_reader :codons
def initialize seq, pos=0
if seq.kind_of?(String)
@codons = seq.upcase.scan(/(\w\w\w)/).flatten
else
@codons = seq
end
@pos = pos
end
def size
@codons.size
end
def rpos
pos + size
end
def [] index
@codons[index]
end
def shift
list = @codons
list.shift
FrameCodonSequence.new(list,@pos+1)
end
def to_seq
@codons.join
end
def each
@codons.each { | c| yield c }
end
end
end # FrameCodonHelpers
# The short frame uses the simplest concept to find ORFs. The sequence is
# immutable, always forward and in frame 0. That makes it easy to reason.
# It also return all ORF's in one go, with the left/right locations.
class ShortFrameState
include FrameCodonHelpers
attr_reader :seq, :ntseq_pos, :min_size_codons, :codons
def initialize seq, ntseq_pos, ntmin_size
# @seq = seq.upcase
@seq = seq
@min_size_codons = if ntmin_size > 3
(ntmin_size/3).to_i
else
2 # otherwise we get single STOP codons
end
@codons = FrameCodonSequence.new(seq,ntseq_pos)
@ntseq_pos = ntseq_pos # nucleotides
# @codons.track_sequence_pos = seq_pos
end
# Return a list of ORFs delimited by STOP codons.
def get_stopstop_orfs
get_codon_orfs1(Proc.new { | codon | STOP_CODONS.include?(codon) },false,true)
end
# Return a list of ORFs delimited by START-STOP codons
def get_startstop_orfs
get_codon_orfs2(
Proc.new { | codon | STOP_CODONS.include?(codon) },
Proc.new { | codon | START_CODONS.include?(codon) })
end
# Splitter for one delimiter function. +include_leftmost+ decides
# the first sequence is returned when incomplete. +strip_leading+
# is used to remove the shared codon with the last sequence.
#
def get_codon_orfs1 splitter_func,do_include_leftmost_orf,do_strip_leading_codon
orfs = split(@codons,splitter_func)
return [] if orfs.size == 0
# Drop the first sequence, if there is no match on the first position
orfs.shift if !do_include_leftmost_orf and !splitter_func.call(orfs.first[0])
orfs = orfs.map { |codons|
codons = codons.shift if do_strip_leading_codon and splitter_func.call(codons[0])
codons
}
TrackSequenceTrait.update_sequence_pos(orfs,@ntseq_pos) # nail against parent
end
# Splitter for two delimeter functions
def get_codon_orfs2 splitter_func, start_func
orfs = get_codon_orfs1(splitter_func,true,true)
orfs.find_all { | orf | start_func.call(orf[0]) }
end
# Return list of codon sequences, split on the +is_splitter+
# function.
#
def split codons, is_splitter_func
list = []
node = []
codons.each_with_index do | c, pos |
# p [c,pos]
if is_splitter_func.call(c)
node.push c
size = node.size
# p node
list.push FrameCodonSequence.new(node,pos+1-size) if size > @min_size_codons
node = []
end
node.push c # always push boundary codon
end
list
end
end
# This is the reversed version, which is rather the same as the forward,
# though the tracked ntseq_pos should be seen from the end of the sequence,
# as we are emmiting sequences from the end(!) Also we need to make sure
# the sequence is always in frame (from the left).
class ShortReversedFrameState < ShortFrameState
def initialize seq, ntseq_pos, ntmin_size
chop = seq.size % 3 # align on codons
super seq[chop..-1],ntseq_pos,ntmin_size
@seq = seq # but record full seq
end
end
class OrfEmitter
# 6-frame ORF emitter for (growing) sequences from the +emit+
# object. Type can be a symbol or a function. Symbols are
#
# :stopstop All sequences from STOP to STOP codon
#
# size control is in nucleotides.
#
def initialize emit, type, min_size=30, max_size=nil
@em = emit
@type = type
@min_size = min_size
@max_size = max_size
end
# Concats sequences from the emitter and yields the
# contained ORFs for every resulting frame (-3..-1, 1..3 )
def emit_seq
@em.emit_seq do | part, index, tag, seq |
# p [part, seq]
break
end
end
end
end
end