Class: GappedAligner

Inherits:
Object
  • Object
show all
Defined in:
lib/protk/gapped_aligner.rb

Overview

Uses a dynamic programming algorithm (Smith-Waterman like) to align a peptide sequence to a nucleotide. This aligner assumes you are doing protogenomics and just want to assume that

(a) The entire peptide sequence matches (with gaps) to the DNA sequence

Instance Method Summary collapse

Constructor Details

#initializeGappedAligner

Returns a new instance of GappedAligner.



103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/protk/gapped_aligner.rb', line 103

def initialize
	@big_penalty = -1000000000
	@gap_open_penalty = -10000
	@gap_extend_penalty = -1
	@end_gap_penalty = 0
	@match_bonus = 400

	@match_move=0
	@aadel_move=-1
	@nadel_move=1
	@triplet_offsets = [[0,-2,-1],[-1,0,-2],[-2,-1,0]]
end

Instance Method Details

#aa_deletionObject



116
117
118
# File 'lib/protk/gapped_aligner.rb', line 116

def aa_deletion()
	return @big_penalty
end

#align(pep_seq, gene_seq) ⇒ Object



254
255
256
257
258
259
260
261
262
# File 'lib/protk/gapped_aligner.rb', line 254

def align pep_seq, gene_seq

	trace = calculate_dp(pep_seq,gene_seq)
	alignment = PeptideToGeneAlignment.new(gene_seq,pep_seq,trace)
	# puts alignment
	# require 'debugger';debugger

	return alignment
end

#calculate_dp(pep_seq, gene_seq) ⇒ Object



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
# File 'lib/protk/gapped_aligner.rb', line 186

def calculate_dp(pep_seq,gene_seq)
	gene_seq = Bio::Sequence::NA.new(gene_seq)
	nrow = pep_seq.length*3+1
	ncol = gene_seq.length+1

	throw "Peptide sequence is longer than gene" if nrow > ncol

	pep_triples=""
	pep_seq.each_char { |c| pep_triples<<c;pep_triples<<c;pep_triples<<c }

	dpmoves=Matrix.build(nrow,ncol) {|r,c| 0 }.to_a
	dpmatrix=Matrix.build(nrow,ncol) { |r,c| 0 }.to_a
	dpframes=Matrix.build(nrow,ncol) { |r,c| 0 }.to_a
	# before_gap_positions = Matrix.build(nrow,ncol) { |r,c| 0 }.to_a

	# Boundary conditions
	(0..(nrow-1)).each { |i| 
		dpmatrix[i][0] = aa_deletion*i 
		dpmoves[i][0] = @aadel_move
	}
	(0..(ncol-1)).each { |j| 
		dpmatrix[0][j] = @end_gap_penalty*j 
		dpmoves[0][j] = @nadel_move
		dpframes[0][j] = j % 3
	}
	dpmoves[0][0]=0
	dpframes[0][0]=0

	(1..(nrow-1)).each do |i|
		(1..(ncol-1)).each do |j|
			aa = pep_triples[i-1]

			translated_na = translate_na_at(j-1,dpframes[i-1][j-1],gene_seq)

			match = score_match(aa,translated_na) + dpmatrix[i-1][j-1]

			nadel = score_na_deletion(dpmoves[i][j-1]) + dpmatrix[i][j-1]

			# if (translated_na=="R") && (pep_seq=="FR") && (aa == "R")
				# require 'debugger';debugger					
			# end

			if match >= nadel
				dpmatrix[i][j] = match					
				dpmoves[i][j] = @match_move
				dpframes[i][j] = dpframes[i-1][j-1]
			else
				dpmatrix[i][j] = nadel
				dpmoves[i][j] = @nadel_move
				dpframes[i][j] = next_frame(dpframes[i][j-1])
			end

		end
	end

	# Find best end-point
	end_score = dpmatrix[nrow-1].max
	end_j = dpmatrix[nrow-1].index(end_score)

	save_matrix(dpmatrix,pep_triples,gene_seq,"dpmatrix")
	save_matrix(dpmoves,pep_triples,gene_seq,"moves")
	save_matrix(dpframes,pep_triples,gene_seq,"frames")
#		require 'debugger';debugger

	traceback(nrow-1,end_j,dpmoves)
end

#next_frame(previous_frame) ⇒ Object



153
154
155
# File 'lib/protk/gapped_aligner.rb', line 153

def next_frame(previous_frame)
	(previous_frame+1) % 3
end

#save_matrix(dpmatrix, pep_triples, gene_seq, name) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/protk/gapped_aligner.rb', line 167

def save_matrix(dpmatrix,pep_triples,gene_seq,name)
	matfile=File.open("#{name}.csv", "w+")
	matfile.write(",,")
	gene_seq.each_char { |na| matfile.write("#{na},")  }
	matfile.write("\n")
	dpmatrix.each_with_index { |row,ri|  
		if ri>0
			matfile.write("#{pep_triples[ri-1]},")
		else
			matfile.write(",")
		end
		row.each { |col|  
			matfile.write("#{col},")
		}
		matfile.write("\n")
	}
	matfile.close()
end

#score_match(aa, na) ⇒ Object



127
128
129
130
131
132
# File 'lib/protk/gapped_aligner.rb', line 127

def score_match(aa,na)
	if aa==na
		return @match_bonus		
	end
	return @big_penalty
end

#score_na_deletion(move_type) ⇒ Object



120
121
122
123
124
125
# File 'lib/protk/gapped_aligner.rb', line 120

def score_na_deletion(move_type)
	if move_type==@nadel_move
		return @gap_extend_penalty
	end
	return @gap_open_penalty
end

#traceback(from_row, from_col, dpmoves) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/protk/gapped_aligner.rb', line 134

def traceback(from_row,from_col,dpmoves)
	last_move = dpmoves[from_row][from_col]
	last_row = from_row-1
	last_col = from_col-1
	if last_move==@aadel_move
		last_col+=1
	elsif last_move==@nadel_move			
		last_row+=1
	end

	if last_col==0 && last_row==0
		return [last_move]
	else
		throw "Beyond end of array" if last_col<0 || last_row <0

		return traceback(last_row,last_col,dpmoves).push(last_move)
	end
end

#translate_na_at(j, frame, gene_seq) ⇒ Object



157
158
159
160
161
162
163
164
165
# File 'lib/protk/gapped_aligner.rb', line 157

def translate_na_at(j,frame,gene_seq)
	rm = j % 3 
	start_pos=j+@triplet_offsets[rm][frame]
	if start_pos < 0
		return '-'
	else
		return gene_seq[start_pos,3].translate
	end
end