-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfind_alignments_R.R
More file actions
292 lines (258 loc) · 9.51 KB
/
find_alignments_R.R
File metadata and controls
292 lines (258 loc) · 9.51 KB
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
find_alignments_R <- function(seq1, seq2) {
# INPUTS:
# seq1: the original Wuhan sequence as a character string
# seq2: user input sequence as a character string
# OUTPUTS:
# a visual graphic showing identical sequence regions and mutations between covid sequences
# PARAMETERS: (hardcoded as these are ideal for Covid sequences):
min_seq_len = 10;
match_len = 5;
indel_len = 0;
# convert seq1 and seq2 to uppercase in case they're not already:
seq1 <- toupper(seq1);
seq2 <- toupper(seq2);
# get lengths of each sequence:
len1 <- nchar(seq1);
len2 <- nchar(seq2);
# find alignment for sequences in both orders:
reps1 <- make_align(seq1, seq2, min_seq_len, match_len, indel_len);
reps2 <- make_align(seq2, seq1, min_seq_len, match_len, indel_len);
# combine reps1 and reps2 into one variable, switching the columns in reps2:
temp <- reps2[,1];
reps2[,1] <- reps2[,3];
reps2[,3] <- temp;
temp <- reps2[,2];
reps2[,2] <- reps2[,4];
reps2[,4] <- temp;
reps <- rbind(reps1,reps2);
# merge the acceptable overlaps in reps:
num_reps = nrow(reps);
r1 <- 1;
while(r1 <= num_reps-1) {
r2 <- r1 + 1;
while(r2 <= num_reps) {
if(reps[r1,1]==reps[r2,1] && reps[r1,2]==reps[r2,2] && reps[r1,3]==reps[r2,3] && reps[r1,4]== reps[r2,4]) {
reps <- reps[-c(r2:r2),];
num_reps <- num_reps - 1;
} else if(reps[r2,1] >= reps[r1,1] && reps[r2,1] <= reps[r1,2] || reps[r2,2] >= reps[r1,1] && reps[r2,2] <= reps[r1,2]) {
if(reps[r1,1]-reps[r2,1] == reps[r1,3]-reps[r2,3] && reps[r1,2]-reps[r2,2] == reps[r1,4]-reps[r2,4]) {
reps[r1,1] <- min(reps[r1,1],reps[r2,1]);
reps[r1,2] <- max(reps[r1,2],reps[r2,2]);
reps[r1,3] <- min(reps[r1,3],reps[r2,3]);
reps[r1,4] <- min(reps[r1,4],reps[r2,4]);
reps <- reps[-c(r2:r2),];
num_reps <- num_reps - 1;
}
} else {
r2 <- r2 + 1;
}
}
r1 <- r1 + 1;
}
# recreate reps:
reps <- matrix(reps,ncol = 4);
# generate coordinates of identical sequence region boxes to be plotted:
spacer <- 500;
spaced_reps <- matrix(reps,ncol = 4);
num_rows <- nrow(spaced_reps);
for(r in 1:num_rows) {
spaced_reps[r,] <- spaced_reps[r,] + spacer*r;
}
# make a visual display of the sequence:
plot(0,0,
main = 'Comparison of input CoVid sequence and original human Wuhan strain',
xlim = c(0,max(spaced_reps[num_rows,2],spaced_reps[num_rows,4])+spacer),
ylim = c(0,3),
xlab = 'base pairs',
ylab = 'strains',
xaxt = 'n',
yaxt = 'n'
)
par(new = TRUE);
# add labels for the y-axis:
axis(side = 2, at = c(0.5,2.5), labels = c('original Wuhan strain','input sequence'));
# plot the other elements:
for(r in 1:num_rows) {
# plot rectangles of identical sequence regions:
rect(spaced_reps[r,1], 0, spaced_reps[r,2], 1, col = rgb(0,0.9,0.3,0.5))
rect(spaced_reps[r,3], 2, spaced_reps[r,4], 3, col = rgb(0,0.9,0.3,0.5))
# plot a dotted line between identical regions:
midpt1 <- spaced_reps[r,1] + (spaced_reps[r,2]-spaced_reps[r,1])/2;
midpt2 <- spaced_reps[r,3] + (spaced_reps[r,4]-spaced_reps[r,3])/2;
lines(c(midpt1,midpt2),c(1,2),col = rgb(0,0.4,0),lty = 'dashed')
# label identical regions with base pair length:
text(midpt1,0.5,paste(spaced_reps[r,2]-spaced_reps[r,1]+1,'bp'),srt = 90)
text(midpt1,2.5,paste(spaced_reps[r,4]-spaced_reps[r,3]+1,'bp'),srt = 90)
# plot mutations:
# mutations that occur before identical regions:
if(r==1) {
if(reps[1,1] > 1) {
misalign1 <- substr(seq1,1,reps[1,1]-1);
if(reps[1,1]-1 == 1) {
bp <- '(1)';
} else {
bp <- paste('(1:',reps[1,1]-1,')',sep = '');
}
text(spacer/2,0.5,paste(misalign1,bp),srt = 90)
}
if(reps[1,3] > 1) {
misalign2 <- substr(seq2,1,reps[1,3]-1);
if(reps[1,3]-1 == 1) {
bp <- '(1)';
} else {
bp <- paste('(1:',reps[1,3]-1,')',sep = '');
}
text(spacer/2,2.5,paste(misalign2,bp),srt = 90)
}
}
# mutations that occur between identical regions:
if(r < nrow(reps)) {
misalign1 <- substr(seq1,reps[r,2]+1,reps[r+1,1]-1)
if(reps[r+1,1]-reps[r,2] == 2) {
bp <- paste('(',reps[r,2]+1,')',sep = '');
} else {
bp <- paste('(',reps[r,2]+1,':',reps[r+1,1]-1,')',sep = '');
}
text(spaced_reps[r,2]+1+(spaced_reps[r+1,1]-1-(spaced_reps[r,2]+1))/2,0.5,paste(misalign1,bp),srt = 90)
misalign2 <- substr(seq2,reps[r,4]+1,reps[r+1,3]-1)
if(reps[r+1,3]-reps[r,4] == 2) {
bp <- paste('(',reps[r,4]+1,')',sep = '');
} else {
bp <- paste('(',reps[r,4]+1,':',reps[r+1,3]-1,')',sep = '');
}
text(spaced_reps[r,4]+1+(spaced_reps[r+1,3]-1-(spaced_reps[r,4]+1))/2,2.5,paste(misalign2,bp),srt = 90)
} else if(r == nrow(reps)) {
if(reps[nrow(reps),2] < len1) {
misalign1 <- substr(seq1,reps[nrow(reps),2]+1,len1);
if(reps[r,2] == len1-1) {
bp <- paste('(',len1,')',sep = '');
} else {
bp <- paste('(',reps[r,2]+1,':',len1,')',sep = '');
}
text(spaced_reps[nrow(reps),2]+spacer/2,0.5,paste(misalign1,bp),srt = 90)
}
if(reps[nrow(reps),4] < len2) {
misalign2 <- substr(seq2,reps[nrow(reps),4]+1,len2);
if(reps[r,4] == len2-1) {
bp <- paste('(',len2,')',sep = '');
} else {
bp <- paste('(',reps[r,4]+1,':',len2,')',sep = '');
}
text(spaced_reps[nrow(reps),4]+spacer/2,2.5,paste(misalign2,bp),srt = 90)
}
}
# keep current figure:
par(new = TRUE);
}
}
#######################################################################################################################
make_align <- function(seq1, seq2, min_seq_len, match_len, indel_len) {
# determine the length of each input sequence:
len1 <- nchar(seq1);
len2 <- nchar(seq2);
# initialize the variables that will hold alignments for each sequence:
align1 <- matrix(nrow = 0,ncol = 2);
align2 <- matrix(nrow = 0,ncol = 2);
# find the ranges where sequences match exactly:
OS1 <- 0;
while(OS1 <= len1-match_len) {
OS2 <- 0;
while(OS2 <= len2-match_len && OS1 <= len1-match_len) {
S1 <- substr(seq1,1+OS1,OS1+match_len);
S2 <- substr(seq2,1+OS2,OS2+match_len);
if(S1==S2) {
expanding = TRUE;
expand = 0;
while(expanding) {
if(OS1+match_len+expand+1 <= len1 && OS2+match_len+expand+1 <= len2 && substr(seq1,OS1+match_len+expand+1,OS1+match_len+expand+1)==substr(seq2,OS2+match_len+expand+1,OS2+match_len+expand+1)) {
expand <- expand+1;
} else {
expanding <- FALSE;
}
}
align1 <- rbind(align1,c(1+OS1,OS1+match_len+expand));
align2 <- rbind(align2,c(1+OS2,OS2+match_len+expand));
OS1 <- OS1+match_len+expand+1;
OS2 <- OS2+match_len+expand+1;
} else {
OS2 <- OS2 + 1;
}
}
OS1 <- OS1 + 1;
}
# make a matrix of possible transitions between matching ranges:
aligns = nrow(align1);
mat = matrix(nrow = aligns, ncol = aligns);
for(r in seq(1,aligns,1)) {
for(c in seq(1,aligns,1)) {
diff1 <- align1[c,1]-align1[r,2]-1;
diff2 <- align2[c,1]-align2[r,2]-1;
if(abs(diff1) <= indel_len && abs(diff2) <= indel_len && abs(diff1-diff2) <= indel_len) {
mat[r,c] = 1;
} else {
mat[r,c] = 0;
}
}
}
# find all possible starting ranges for longer sequences, including lone ranges:
starts <- matrix(nrow = 1,ncol = 0);
for(a in 1:aligns) {
row_sum <- sum(mat[a,]);
col_sum <- sum(mat[,a]);
if(row_sum > 0 && col_sum == 0 || row_sum == 0 && col_sum == 0) {
starts <- c(starts,a);
}
}
# make a list of all possible sequences of ranges:
listo <- list();
if(length(starts) > 0) {
for(s in 1:length(starts)) {
listo[[s]] <- starts[s];
}
k <- 1;
while(k <= length(listo)) {
len_seq <- length(listo[[k]]);
last_el = listo[[k]][len_seq];
tails = c();
for(c in 1:ncol(mat)) {
if(mat[last_el,c] != 0) {
tails <- c(tails,c);
}
}
tail <- 1;
while(tail <= length(tails)) {
if(tails[tail] < last_el) {
tails[tail] <- c();
} else {
tail <- tail + 1;
}
}
if(length(tails) > 0) {
listo[[k]] <- c(listo[[k]],tails[1]);
if(length(tails) > 1) {
for(t in 2:length(tails)) {
#listo(size(listo,2)+1).seq = [listo(k).seq(1:len_seq), tails(t)];
listo[[length(listo)+1]] <- c(listo[[k]][1:len_seq], tails[t]);
}
}
} else {
k <- k + 1;
}
}
}
reps <- matrix(nrow = 0, ncol = 4);
i <- 0;
if(length(listo) > 0) {
for(k in 1:length(listo)) {
first_el <- listo[[k]][1];
last_el <- listo[[k]][length(listo[[k]])];
range1 <- c(align1[first_el,1],align1[last_el,2]);
range2 <- c(align2[first_el,1],align2[last_el,2]);
if(range1[2]-range1[1]+1 >= min_seq_len && range2[2]-range2[1]+1 >= min_seq_len) {
i <- i + 1;
reps <- rbind(reps,c(range1[1],range1[2],range2[1],range2[2]));
}
}
}
return(reps)
}