-
Notifications
You must be signed in to change notification settings - Fork 17
/
CRISPRUtil.java
525 lines (429 loc) · 18.7 KB
/
CRISPRUtil.java
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
import java.util.*;
public class CRISPRUtil
{
//identified repeats may represent only a subset of a larger repeat. this method extends these
//repeats as long as they continue to match within some range. assumes there are at least two repeats
public static CRISPR getActualRepeatLength(CRISPR candidateCRISPR, int searchWindowLength, int minSpacerLength, int minNumRepeats)
{
int firstRepeatStartIndex = candidateCRISPR.repeatAt(0);
int lastRepeatStartIndex = candidateCRISPR.repeatAt(candidateCRISPR.numRepeats()-1);
int shortestRepeatSpacing = candidateCRISPR.repeatAt(1) - candidateCRISPR.repeatAt(0);
for (int i = 0; i < candidateCRISPR.numRepeats() - 1; i++)
{
int currRepeatIndex = candidateCRISPR.repeatAt(i);
int nextRepeatIndex = candidateCRISPR.repeatAt(i + 1);
int currRepeatSpacing = nextRepeatIndex - currRepeatIndex;
if (currRepeatSpacing < shortestRepeatSpacing)
shortestRepeatSpacing = currRepeatSpacing;
}
int sequenceLength = DNASequence.seq.length();
int rightExtensionLength = searchWindowLength;
int maxRightExtensionLength = shortestRepeatSpacing - minSpacerLength;
int currRepeatStartIndex;
String currRepeat;
int charCountA, charCountC, charCountT, charCountG;
charCountA = charCountC = charCountT = charCountG = 0;
double threshold;
boolean done = false;
threshold = .75;
//(from the right side) extend the length of the repeat to the right as long as the last base of all repeats are at least threshold
while (!done && (rightExtensionLength <= maxRightExtensionLength))
{
if (lastRepeatStartIndex + rightExtensionLength >= sequenceLength)
{
if (candidateCRISPR.numRepeats() - 1 > minNumRepeats)
{
candidateCRISPR.removeRepeat(candidateCRISPR.lastRepeat());
lastRepeatStartIndex = candidateCRISPR.repeatAt(candidateCRISPR.numRepeats()-1);
}
else
{
done = true;
break;
}
}
for (int k = 0; k < candidateCRISPR.numRepeats(); k++ )
{
currRepeatStartIndex = candidateCRISPR.repeatAt(k);
//System.out.println("currRepeatStartIndex: " + currRepeatStartIndex + " currRepeatStartIndex + rightExtensionLength " + (currRepeatStartIndex + rightExtensionLength) + " maxRightExtensionLength "+maxRightExtensionLength+" sequence length "+sequenceLength);
currRepeat = DNASequence.seq.substring(currRepeatStartIndex, currRepeatStartIndex + rightExtensionLength);
char lastChar = currRepeat.charAt(currRepeat.length() - 1);
if (lastChar == 'A') charCountA++;
if (lastChar == 'C') charCountC++;
if (lastChar == 'T') charCountT++;
if (lastChar == 'G') charCountG++;
}
double percentA = (double)charCountA/candidateCRISPR.numRepeats();
double percentC = (double)charCountC/candidateCRISPR.numRepeats();
double percentT = (double)charCountT/candidateCRISPR.numRepeats();
double percentG = (double)charCountG/candidateCRISPR.numRepeats();
if ( (percentA >= threshold) || (percentC >= threshold) || (percentT >= threshold) || (percentG >= threshold) )
{
rightExtensionLength++;
charCountA = charCountC = charCountT = charCountG = 0;
}
else
{
done = true;
}
}
rightExtensionLength--;
int leftExtensionLength = 0;
charCountA = charCountC = charCountT = charCountG = 0;
done = false;
int maxLeftExtensionLength = shortestRepeatSpacing - minSpacerLength - rightExtensionLength;
//(from the left side) extends the length of the repeat to the left as long as the first base of all repeats is at least threshold
while (!done && (leftExtensionLength <= maxLeftExtensionLength) )
{
if (firstRepeatStartIndex - leftExtensionLength < 0)
{
if (candidateCRISPR.numRepeats() - 1 > minNumRepeats)
{
candidateCRISPR.removeRepeat(candidateCRISPR.firstRepeat());
firstRepeatStartIndex = candidateCRISPR.repeatAt(0);
}
else
{
done = true;
break;
}
}
for (int k = 0; k < candidateCRISPR.numRepeats(); k++ )
{
currRepeatStartIndex = candidateCRISPR.repeatAt(k);
char firstChar = DNASequence.seq.charAt(currRepeatStartIndex - leftExtensionLength);
if (firstChar == 'A') charCountA++;
if (firstChar == 'C') charCountC++;
if (firstChar == 'T') charCountT++;
if (firstChar == 'G') charCountG++;
}
double percentA = (double)charCountA/candidateCRISPR.numRepeats();
double percentC = (double)charCountC/candidateCRISPR.numRepeats();
double percentT = (double)charCountT/candidateCRISPR.numRepeats();
double percentG = (double)charCountG/candidateCRISPR.numRepeats();
if ( (percentA >= threshold) || (percentC >= threshold) || (percentT >= threshold) || (percentG >= threshold) )
{
leftExtensionLength++;
charCountA = charCountC = charCountT = charCountG = 0;
}
else
{
done = true;
}
}
leftExtensionLength--;
// Ok to suppress warnings since we know that we are casting integers
@SuppressWarnings("unchecked")
Vector<Integer> newPositions = (Vector<Integer>)(candidateCRISPR.repeats()).clone();
for (int m = 0; m < newPositions.size(); m++)
{
int newValue = candidateCRISPR.repeatAt(m) - leftExtensionLength;
newPositions.setElementAt(new Integer(newValue), m);
}
int actualPatternLength = rightExtensionLength + leftExtensionLength;
return new CRISPR(newPositions, actualPatternLength);
}
public static CRISPR trim(CRISPR candidateCRISPR, int minRepeatLength)
{
int numRepeats = candidateCRISPR.numRepeats();
int left = candidateCRISPR.start();
int right = candidateCRISPR.end();
String currRepeat;
int charCountA, charCountC, charCountT, charCountG;
charCountA = charCountC = charCountT = charCountG = 0;
boolean done = false;
//trim from right
while (!done && (candidateCRISPR.repeatLength() > minRepeatLength) )
{
for (int k = 0; k < candidateCRISPR.numRepeats(); k++ )
{
currRepeat = candidateCRISPR.repeatStringAt(k);
char lastChar = currRepeat.charAt(currRepeat.length() - 1);
if (lastChar == 'A') charCountA++;
if (lastChar == 'C') charCountC++;
if (lastChar == 'T') charCountT++;
if (lastChar == 'G') charCountG++;
}
double percentA = (double)charCountA/candidateCRISPR.numRepeats();
double percentC = (double)charCountC/candidateCRISPR.numRepeats();
double percentT = (double)charCountT/candidateCRISPR.numRepeats();
double percentG = (double)charCountG/candidateCRISPR.numRepeats();
if ( (percentA < .75) && (percentC < .75) && (percentT < .75) && (percentG < .75) )
{
candidateCRISPR.setRepeatLength(candidateCRISPR.repeatLength() - 1);
charCountA = charCountC = charCountT = charCountG = 0;
}
else
{
done = true;
}
}
charCountA = charCountC = charCountT = charCountG = 0;
done = false;
//trim from left
while (!done && (candidateCRISPR.repeatLength() > minRepeatLength) )
{
for (int k = 0; k < candidateCRISPR.numRepeats(); k++ )
{
currRepeat = candidateCRISPR.repeatStringAt(k);
char firstChar = currRepeat.charAt(0);
if (firstChar == 'A') charCountA++;
if (firstChar == 'C') charCountC++;
if (firstChar == 'T') charCountT++;
if (firstChar == 'G') charCountG++;
}
double percentA = (double)charCountA/candidateCRISPR.numRepeats();
double percentC = (double)charCountC/candidateCRISPR.numRepeats();
double percentT = (double)charCountT/candidateCRISPR.numRepeats();
double percentG = (double)charCountG/candidateCRISPR.numRepeats();
if ( (percentA < .75) && (percentC < .75) && (percentT < .75) && (percentG < .75) )
{
for (int m = 0; m < candidateCRISPR.numRepeats(); m++ )
{
int newValue = candidateCRISPR.repeatAt(m) + 1;
candidateCRISPR.setRepeatAt(newValue, m);
}
candidateCRISPR.setRepeatLength(candidateCRISPR.repeatLength() - 1);
charCountA = charCountC = charCountT = charCountG = 0;
}
else
{
done = true;
}
}
return candidateCRISPR;
}
public static boolean hasSimilarlySizedSpacers(CRISPR candidateCRISPR, int spacerToSpacerLengthOffset, int spacerToRepeatLengthOffset)
{
int initialSpacerLength = candidateCRISPR.spacerStringAt(0).length();
int repeatLength = candidateCRISPR.repeatLength();
for (int i = 0 ; i < candidateCRISPR.numSpacers(); i++)
{
int currSpacerLength = candidateCRISPR.spacerStringAt(i).length();
//checks that each spacer is of similar size to other spacers
if ( Math.abs(currSpacerLength - initialSpacerLength) > spacerToSpacerLengthOffset )
{
return false;
}
//checks that each spacer is of similar size to the repeats
if ( Math.abs(currSpacerLength - repeatLength) > spacerToRepeatLengthOffset)
{
return false;
}
}
return true;
}
//checks first five spacers
public static boolean hasNonRepeatingSpacers(CRISPR candidateCRISPR, double maxSimilarity)
{
//assumes at least two elements
String firstRepeat = candidateCRISPR.repeatStringAt(0);
String firstSpacer = candidateCRISPR.spacerStringAt(0);
if (candidateCRISPR.numRepeats() >= 3)
{
int i = 0;
while ( (i < candidateCRISPR.numSpacers() - 1) )
{
if (i == 4) //only check first 5 spacers
return true;
String currSpacer = candidateCRISPR.spacerStringAt(i);
String nextSpacer = candidateCRISPR.spacerStringAt(i + 1);
String currRepeat = candidateCRISPR.repeatStringAt(i);
//spacers should be different
if ( DNASequence.getSimilarity(currSpacer, nextSpacer) > maxSimilarity )
return false;
//repeats should also be different from spacers, otherwise may be tandem repeat
if ( DNASequence.getSimilarity(currRepeat, currSpacer) > maxSimilarity )
return false;
i++;
}
//checks last repeat/spacer
if ( DNASequence.getSimilarity(candidateCRISPR.repeatStringAt(i), candidateCRISPR.spacerStringAt(i)) > maxSimilarity )
return false;
return true;
}
//we check that the spacer is different from the repeat
else if (candidateCRISPR.numRepeats() == 2)
{
if (firstSpacer.equals(""))
return false;
else
return ( DNASequence.getSimilarity(firstSpacer, firstRepeat) < maxSimilarity );
}
else
{
return false;
}
}
public static CRISPR checkFlank(String side, CRISPR candidateCRISPR, int minSpacerLength, int scanRange, double spacerToSpacerMaxSimilarity, double confidence)
{
boolean moreToSearch = true;
while (moreToSearch)
{
int result = scan(side, candidateCRISPR, minSpacerLength, scanRange, confidence);
if (result > 0) //if another repeat found on flank
{
if (side.equals("left"))
candidateCRISPR.insertRepeatAt(result, 0);
else if (side.equals("right"))
candidateCRISPR.addRepeat(result);
}
else
moreToSearch = false;
}
return candidateCRISPR;
}
/*
scan to the right and left of the first and last repeat to see if there is a region
that is similar to the repeats. necessary in case we missed a repeat because of
inexact matches or a result of one of the filters
*/
private static int scan(String side, CRISPR candidateCRISPR, int minSpacerLength, int scanRange, double confidence)
{
int repeatSpacing1, repeatSpacing2, avgRepeatSpacing;
int firstRepeatIndex, lastRepeatIndex, candidateRepeatIndex;
String repeatString, candidateRepeatString, newCandidateRepeatString;
int repeatLength = candidateCRISPR.repeatLength();
int numRepeats = candidateCRISPR.numRepeats();
int sequenceLength = DNASequence.seq.length();
firstRepeatIndex = candidateCRISPR.repeatAt(0);
lastRepeatIndex = candidateCRISPR.repeatAt(numRepeats-1);
if (side.equals("left"))
{
repeatString = candidateCRISPR.repeatStringAt(0);
repeatSpacing1 = candidateCRISPR.repeatSpacing(0, 1);
if (numRepeats >= 3)
{
repeatSpacing2 = candidateCRISPR.repeatSpacing(1, 2);
avgRepeatSpacing = (repeatSpacing1 + repeatSpacing2)/2;
}
else
avgRepeatSpacing = repeatSpacing1;
candidateRepeatIndex = firstRepeatIndex - avgRepeatSpacing;
}
else //if (side.equals("right"))
{
repeatString = candidateCRISPR.repeatStringAt(numRepeats-1);
repeatSpacing1 = candidateCRISPR.repeatSpacing(numRepeats-2, numRepeats-1);
if (numRepeats >= 3)
{
repeatSpacing2 = candidateCRISPR.repeatSpacing(numRepeats-3, numRepeats-2);
avgRepeatSpacing = (repeatSpacing1 + repeatSpacing2)/2;
}
else
avgRepeatSpacing = repeatSpacing1;
candidateRepeatIndex = lastRepeatIndex + avgRepeatSpacing;
}
int begin = candidateRepeatIndex - scanRange;
int end = candidateRepeatIndex + scanRange;
/******************** range checks ********************/
//check that we do not search too far within an existing repeat when scanning right and left
int scanLeftMaxEnd = firstRepeatIndex - repeatLength - minSpacerLength;
int scanRightMinBegin = lastRepeatIndex + repeatLength + minSpacerLength;
if (side.equals("left"))
{
if (end > scanLeftMaxEnd)
end = scanLeftMaxEnd;
}
if (side.equals("right"))
{
if (begin < scanRightMinBegin)
begin = scanRightMinBegin;
}
//out of bounds check for scanning left
if ( (begin) < 0)
return 0;
//out of bounds check for scanning right
if ( (begin + repeatLength) > sequenceLength)
return 0;
if ( (end + repeatLength) > sequenceLength)
end = sequenceLength - repeatLength;
if ( begin >= end)
return 0;
/******************** end range checks ********************/
int[] array = new int[end - begin + 1];
int index = 0;
for (int i = begin; i <= end; i++)
{
candidateRepeatString = DNASequence.seq.substring(i, i + repeatLength);
array[index] = DNASequence.getHammingDistance(repeatString, candidateRepeatString);
index++;
}
//min(array) returns the index of the smallest value in array in this case, it refers to
//the candidate string theat is closest to the repeatString. uses hamming distance as levenshteinDistance is not useful for this particular task
int newCandidateRepeatIndex = begin + min(array);
newCandidateRepeatString = DNASequence.seq.substring(newCandidateRepeatIndex, newCandidateRepeatIndex + repeatLength);
boolean match = patternMatches(repeatString, newCandidateRepeatString, confidence);
if (match)
return newCandidateRepeatIndex;
else
return 0;
}
public static CRISPR scanRight(CRISPR candidateCRISPR, String pattern, int minSpacerLength, int scanRange, SearchUtil searchUtil)
{
int numRepeats = candidateCRISPR.numRepeats();
int patternLength = pattern.length();
int sequenceLength = DNASequence.seq.length();
int lastRepeatIndex = candidateCRISPR.repeatAt(numRepeats-1);
int secondToLastRepeatIndex = candidateCRISPR.repeatAt(numRepeats-2);
int repeatSpacing = lastRepeatIndex - secondToLastRepeatIndex;
int candidateRepeatIndex, beginSearch, endSearch, position;
String text = "";
boolean moreToSearch = true;
while (moreToSearch)
{
candidateRepeatIndex = lastRepeatIndex + repeatSpacing;
beginSearch = candidateRepeatIndex - scanRange;
endSearch = candidateRepeatIndex + patternLength + scanRange;
/******************** range checks ********************/
//check that we do not search too far within an existing repeat when scanning right
int scanRightMinBegin = lastRepeatIndex + patternLength + minSpacerLength;
if (beginSearch < scanRightMinBegin)
beginSearch = scanRightMinBegin;
//System.out.print("beginSearch " + beginSearch + " " + "endSearch" + endSearch);
if (beginSearch > sequenceLength - 1)
return candidateCRISPR;
if (endSearch > sequenceLength)
endSearch = sequenceLength;
if ( beginSearch >= endSearch)
return candidateCRISPR;
/******************** end range checks ********************/
text = DNASequence.seq.substring(beginSearch, endSearch);
position = searchUtil.boyer_mooreSearch(text, pattern);
if (position >= 0)
{
candidateCRISPR.addRepeat(beginSearch + position);
secondToLastRepeatIndex = lastRepeatIndex;
lastRepeatIndex = beginSearch + position;
repeatSpacing = lastRepeatIndex - secondToLastRepeatIndex;
if (repeatSpacing < (minSpacerLength + patternLength))
moreToSearch = false;
}
else
moreToSearch = false;
}
return candidateCRISPR;
}
private static boolean patternMatches(String pattern1, String pattern2, double confidence)
{
double patternSimilarity = DNASequence.getSimilarity(pattern1, pattern2);
if (patternSimilarity >= confidence)
return true;
else
return false;
}
private static int min (int[] array)
{
int min = array[0];
int minIndex = 0;
for (int i = 0; i < array.length; i++)
{
if (array[i] < min)
{
min = array[i];
minIndex = i;
}
}
return minIndex;
}
}