-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathCRISPRFinder.java
383 lines (313 loc) · 13.1 KB
/
CRISPRFinder.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
import java.io.*;
import java.util.Vector;
public class CRISPRFinder
{
private String inputFileName;
private String outputFileName;
private String outputGffFileName;
private int screenDisplay;
private int minNumRepeats;
private int minRepeatLength;
private int maxRepeatLength;
private int minSpacerLength;
private int maxSpacerLength;
private int searchWindowLength;
private int outputformat;
private boolean printSpacers;
private PrintStream spacers;
private boolean printGffHeader;
DNASequence sequence = null;
int sequenceLength = 0;
private int totalCrisprCount = 0;
public CRISPRFinder(String _inputFileName,
String _outputFileName,
String _outputGffFileName,
int _screenDisplay,
int _minNumRepeats,
int _minRepeatLength,
int _maxRepeatLength,
int _minSpacerLength,
int _maxSpacerLength,
int _searchWindowLength,
int _outputformat,
boolean _spacers)
{
inputFileName = _inputFileName;
outputFileName = _outputFileName;
outputGffFileName = _outputGffFileName;
screenDisplay = _screenDisplay;
minNumRepeats = _minNumRepeats;
minRepeatLength = _minRepeatLength;
maxRepeatLength = _maxRepeatLength;
minSpacerLength = _minSpacerLength;
maxSpacerLength = _maxSpacerLength;
searchWindowLength = _searchWindowLength;
outputformat = _outputformat;
printSpacers = _spacers;
printGffHeader = true;
if(_spacers) {
File outputFile;
FileOutputStream outputFileStream;
if(_outputFileName == "") {
String input_file_prefix = removeExtention(inputFileName);
outputFile = new File(input_file_prefix + "_spacers.fa");
} else {
String input_file_prefix = removeExtention(outputFileName);
outputFile = new File(input_file_prefix + "_spacers.fa");
}
try {
outputFileStream = new FileOutputStream(outputFile, false);
spacers = new PrintStream(outputFileStream);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private static String removeExtention(String filePath) {
// These first few lines the same as Justin's
File f = new File(filePath);
// if it's a directory, don't remove the extention
if (f.isDirectory()) return filePath;
String name = f.getName();
// Now we know it's a file - don't need to do any special hidden
// checking or contains() checking because of:
final int lastPeriodPos = name.lastIndexOf('.');
if (lastPeriodPos <= 0)
{
// No period after first character - return name as it was passed in
return filePath;
}
else
{
// Remove the last period and everything after it
File renamed = new File(f.getParent(), name.substring(0, lastPeriodPos));
return renamed.getPath();
}
}
public boolean goCRISPRFinder()
{
File inputFile = new File(inputFileName);
if (! inputFile.exists())
{
System.err.println("File name does not exist: " + inputFile.getPath());
return false;
}
//System.out.println("\n\nReading file '" + inputFile.getPath() + "'");
FASTAReader fastaReader = new FASTAReader(inputFile.getPath());
if (! fastaReader.isFASTA())
{
System.err.println("File '" + inputFile.getPath() + "' does not seem to be FASTA-formatted.");
return false;
}
while ( fastaReader.read() )
{
sequence = new DNASequence(fastaReader.getSequence(), fastaReader.getName(), fastaReader.getDesc());
//System.out.println("Sequence '" + sequence.getName() + "' (" + sequence.length() + " bp)");
/*
// Check that sequence is a valid DNA sequence
if ( ! sequence.isDNASequence() )
{
System.out.println(sequence.getErrorLog() + "Skipping this sequence!");
System.out.println("");
continue;
}
*/
sequence.mask(100);
try
{
findRepeats(sequence, fastaReader.getNum());
}
catch (Exception e)
{
System.err.println ("Error processing input file '" + inputFile.getPath() + "'. Please, check contents.\n");
e.printStackTrace(System.err);
}
}
//System.out.println("Done!");
return true;
}
private boolean findRepeats( DNASequence sequence, int readNum )
{
Vector<CRISPR> CRISPRVector = new Vector<CRISPR>();
sequenceLength = sequence.length();
int actualRepeatLength;
boolean repeatsFound = false;
CRISPR candidateCRISPR;
String pattern;
if ((searchWindowLength < 6) || (searchWindowLength > 9))
{
// Change window length
int oldSearchWindowlength = searchWindowLength;
searchWindowLength = 8;
System.err.println("Changing window length to " + searchWindowLength + " instead of " + oldSearchWindowlength);
}
double spacerToSpacerMaxSimilarity = 0.62;
int spacerToSpacerLengthDiff = 12;
int spacerToRepeatLengthDiff = 30;
//the mumber of bases that can be skipped while we still guarantee that the entire search
//window will at some point in its iteration thru the sequence will not miss a any repeat
int skips = minRepeatLength - (2 * searchWindowLength - 1);
if (skips < 1)
skips = 1;
//System.out.println("Searching for repeats...");
long repeatSearchStart = System.currentTimeMillis();
SearchUtil searchUtil = new SearchUtil();
int searchEnd = sequenceLength - maxRepeatLength - maxSpacerLength - searchWindowLength;
for (int j = 0; j <= searchEnd; j = j + skips)
{
candidateCRISPR = new CRISPR();
int beginSearch = j + minRepeatLength + minSpacerLength;
int endSearch = j + maxRepeatLength + maxSpacerLength + searchWindowLength;
if (endSearch > sequenceLength)
endSearch = sequenceLength;
if (endSearch < beginSearch) //should never occur
endSearch = beginSearch;
int mask_end = sequence.masked(beginSearch, endSearch);
if(mask_end != -1)
{
j = mask_end;
continue;
}
String text = sequence.substring(beginSearch, endSearch);
pattern = sequence.substring(j, j + searchWindowLength);
//if pattern is found, add it to candidate list and scan right for additional similarly spaced repeats
int patternInTextIndex = searchUtil.boyer_mooreSearch(text, pattern);
if (patternInTextIndex >= 0)
{
candidateCRISPR.addRepeat(j);
candidateCRISPR.addRepeat(beginSearch + patternInTextIndex);
candidateCRISPR = CRISPRUtil.scanRight(candidateCRISPR, pattern, minSpacerLength, 24, searchUtil);
}
if ( (candidateCRISPR.numRepeats() >= minNumRepeats) ) //make sure minNumRepeats is always at least 2
{
candidateCRISPR = CRISPRUtil.getActualRepeatLength(candidateCRISPR, searchWindowLength, minSpacerLength, minNumRepeats);
actualRepeatLength = candidateCRISPR.repeatLength();
if ( (actualRepeatLength >= minRepeatLength) && (actualRepeatLength <= maxRepeatLength) )
{ if (CRISPRUtil.hasNonRepeatingSpacers(candidateCRISPR, spacerToSpacerMaxSimilarity))
{
if (CRISPRUtil.hasSimilarlySizedSpacers(candidateCRISPR, spacerToSpacerLengthDiff, spacerToRepeatLengthDiff))
{
candidateCRISPR = CRISPRUtil.checkFlank("left", candidateCRISPR, minSpacerLength, 30, spacerToSpacerMaxSimilarity, .70);
candidateCRISPR = CRISPRUtil.checkFlank("right", candidateCRISPR, minSpacerLength, 30, spacerToSpacerMaxSimilarity, .70);
candidateCRISPR = CRISPRUtil.trim(candidateCRISPR, minRepeatLength);
CRISPRVector.addElement(candidateCRISPR);
repeatsFound = true;
//we may skip current CRISPR (assuming CRISPRs are not interleaved)
j = candidateCRISPR.end() + 1;
}
}
}
}
}
long repeatSearchEnd = System.currentTimeMillis();
//System.out.println("Time to search for repeats: " + (repeatSearchEnd - repeatSearchStart) + " ms");
//System.out.println(CRISPRVector.size() + " possible CRISPR(s) found" + "\n");
// ********************** Display CRISPR elements ********************** //
// ********************************************************************* //
try
{
FileOutputStream outputFileStream;
PrintStream out;
FileOutputStream outputGffFileStream;
PrintStream gffOut = null;
if (screenDisplay == 1)
out = System.out;
else
{
if ( outputFileName == "" )
outputFileName = "a.out";
File outputFile = new File(outputFileName);
if ( readNum == 1 && outputFile.exists() )
{
boolean success = outputFile.delete();
if (!success)
throw new IllegalArgumentException("Error: Could not delete file '" + outputFile + "'");
}
outputFileStream = new FileOutputStream(outputFile, true);
out = new PrintStream(outputFileStream);
if (! outputGffFileName.equals(""))
{
File outputGffFile = new File(outputGffFileName);
if ( readNum == 1 && outputGffFile.exists() )
{
boolean success = outputFile.delete();
if (!success)
throw new IllegalArgumentException("Error: Could not delete file '" + outputFile + "'");
}
outputGffFileStream = new FileOutputStream(outputGffFile, true);
gffOut = new PrintStream(outputGffFileStream);
gffOut.println("##gff-version 3");
printGffHeader = false;
}
}
if (repeatsFound)
{
if(outputformat == 0) {
out.print("Sequence '" + sequence.getName() + "' (" + sequence.length() + " bp)\n");
out.print("\n");
} else {
if(printGffHeader) {
out.println("##gff-version 3");
printGffHeader = false;
}
}
//int repeatLength, numRepeats, numSpacers;
CRISPR currCRISPR;
//String repeat, spacer, prevSpacer;
//repeat = spacer = prevSpacer = "";
//add 1 to each position, to offset programming languagues that begin at 0 rather than 1
for (int k = 0; k < CRISPRVector.size(); k++)
{
currCRISPR = (CRISPR)CRISPRVector.elementAt(k);
totalCrisprCount++;
if(outputformat > 0 && gffOut == null ) {
printGff(out, sequence, currCRISPR);
} else {
printTable(out, currCRISPR);
}
if (gffOut != null) {
printGff(gffOut, sequence, currCRISPR);
}
if(printSpacers) {
for (int i = 0; i < currCRISPR.numSpacers(); ++i) {
spacers.print(">"+ sequence.getName() +"_CRISPR_"+ (totalCrisprCount) +"_spacer_"+ (i+1)+"\n" + currCRISPR.spacerStringAt(i) + "\n");
}
}
}
if(outputformat == 0) {
out.print("Time to find repeats: " + (repeatSearchEnd - repeatSearchStart) + " ms\n\n");
out.print("\n");
}
}
if (screenDisplay == 0)
out.close();
if (gffOut != null) {
gffOut.close();
}
}
catch (Exception e) {
System.err.println ("--Error writing to file-- \n");
e.printStackTrace(System.err);
}
return true;
}
private boolean printGff(PrintStream out, DNASequence sequence, CRISPR currCRISPR) {
String crispr_id = "CRISPR" + totalCrisprCount;
out.print(sequence.getName() + "\tminced:" + minced.VERSION + "\trepeat_region\t");
out.print((currCRISPR.start() + 1) + "\t" + (currCRISPR.end() + 1) + "\t");
out.print(currCRISPR.numRepeats() + "\t.\t.\tID="+ crispr_id + ";rpt_type=direct;rpt_family=CRISPR;rpt_unit_seq="+ currCRISPR.repeatStringAt(1));
out.print("\n");
if(outputformat == 2) {
out.print(currCRISPR.toGff(sequence.getName(), crispr_id));
}
return true;
}
private boolean printTable(PrintStream out, CRISPR currCRISPR) {
out.print("CRISPR " + totalCrisprCount + " Range: " + (currCRISPR.start() + 1) + " - " + (currCRISPR.end() + 1) + "\n");
out.print(currCRISPR.toString());
out.print("Repeats: " + currCRISPR.numRepeats() + "\t" + "Average Length: " + currCRISPR.averageRepeatLength() + "\t\t");
out.print("Average Length: " + currCRISPR.averageSpacerLength() + "\n\n");
return true;
}
}