-
Notifications
You must be signed in to change notification settings - Fork 0
/
spacer_aquisition_pipeline.py
614 lines (445 loc) · 19.7 KB
/
spacer_aquisition_pipeline.py
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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
#!/usr/bin/env python3
"""Extract real spacers from 50 bp sequencing"""
#Imported modules
from subprocess import call
import os
from Bio import SeqIO
from Bio import Seq
import sys
from reportlab.lib import colors
from reportlab.lib.units import cm
from Bio.Graphics import GenomeDiagram
import math
import random
#Authorship information
__author__ = "Angus Angermeyer"
__copyright__ = "Copyright 2018, Laboratory of Dr. Kimberley Seed, UC Berkeley"
__license__ = "GPL"
__version__ = "0.1.0"
__maintainer__ = "Angus Angermeyer"
__email__ = "[email protected]"
__status__ = "Prototype"
#Input path of working directory
path = ""
#Make sure the reference file is in a folder called reference in the path
#This file is the reference for bowtie mapping. You can include any regions of interest that you would like to map to.
#The included reference file is chrI, chrII, PLE1 (modified to mutate the original lead spacer), and the ICP1 background genome.
index = path+"reference/reference.fasta"
os.chdir(path)
for filename in os.listdir(path):
if filename.endswith("R1.fastq"):
trim_log = path+filename[:filename.rindex(".fastq")]+"_trimlog.txt"
output = path+filename[:filename.rindex(".fastq")]+"_trimmed.fastq"
conditions = "MINLEN:50 AVGQUAL:20"
trim = f"trimmomatic SE -quiet -threads 4 -phred33 -trimlog {trim_log} {filename} {output} {conditions}"
print(f"Trimming:\t{filename}\t{conditions}")
call(trim, shell=True)
for filename in os.listdir(path):
if filename.endswith("_trimmed.fastq"):
log = path+filename[:filename.rindex("_trimmed.fastq")]+"_log.log"
spacers = []
spacers34 = []
pre_filtered = 0
bp31_spacers = 0
bp32_spacers = 0
bp33_spacers = 0
bp34_spacers = 0
motif_rejects = 0
tract_rejects = 0
tail_rejects = 0
N_rejects = 0
print(f"Parsing spacers: {filename}")
for record in SeqIO.parse(filename, "fastq"):
pre_filtered += 1
sequence = str(record.seq)
if sequence[0:16] == "ATAGGCTGCTTAAAGA":
if "CCCCCCCC" in sequence or "GGGGGGGG" in sequence:
tract_rejects += 1
continue
else:
if "N" in sequence:
N_rejects += 1
continue
else:
#rename header to only the non-redundant parts (can still be back-tracked later if needed)
header = record.id.split()
unique_id = header[0]+str(random.randint(1,10000))
record.id = unique_id
record.description = unique_id
#31bp spacers
if sequence[-4:] == "GTTA":
spacers.append(sequence[16:sequence.rindex("GT")])
letter_annotations = record.letter_annotations["phred_quality"][16:sequence.rindex("GT")]
record.letter_annotations = {}
record.seq = Seq.Seq(sequence[16:sequence.rindex("GT")])
record.letter_annotations = {'phred_quality':letter_annotations}
bp31_spacers += 1
with open(filename[:filename.rindex(".fastq")]+"_spacers.fastq", "a") as output:
SeqIO.write(record, output, "fastq")
#32bp spacers
elif sequence[-3:] == "GTT":
spacers.append(sequence[16:sequence.rindex("GT")])
letter_annotations = record.letter_annotations["phred_quality"][16:sequence.rindex("GT")]
record.letter_annotations = {}
record.seq = Seq.Seq(sequence[16:sequence.rindex("GT")])
record.letter_annotations = {'phred_quality':letter_annotations}
bp32_spacers += 1
with open(filename[:filename.rindex(".fastq")]+"_spacers.fastq", "a") as output:
SeqIO.write(record, output, "fastq")
#33bp spacers
elif sequence[-2:] == "GT":
spacers.append(sequence[16:sequence.rindex("GT")])
letter_annotations = record.letter_annotations["phred_quality"][16:sequence.rindex("GT")]
record.letter_annotations = {}
record.seq = Seq.Seq(sequence[16:sequence.rindex("GT")])
record.letter_annotations = {'phred_quality':letter_annotations}
bp33_spacers += 1
with open(filename[:filename.rindex(".fastq")]+"_spacers.fastq", "a") as output:
SeqIO.write(record, output, "fastq")
elif sequence[-1:] == "G":
spacers34.append(sequence[16:-1])
letter_annotations = record.letter_annotations["phred_quality"][16:-1]
record.letter_annotations = {}
record.seq = Seq.Seq(sequence[16:-1])
record.letter_annotations = {'phred_quality':letter_annotations}
bp34_spacers += 1
with open(filename[:filename.rindex(".fastq")]+"_spacers34.fastq", "a") as output:
SeqIO.write(record, output, "fastq")
else:
tail_rejects += 1
else:
motif_rejects += 1
continue
with open(log, "a") as log:
log.write("Filtering results for: "+filename+"\n")
log.write("Pre_filtered reads:\t"+str(pre_filtered)+"\n")
log.write("motif_rejects:\t"+str(motif_rejects)+"\n")
log.write("tract_rejects:\t"+str(tract_rejects)+"\n")
log.write("N_rejects:\t"+str(N_rejects)+"\n")
log.write("tail_rejects:\t"+str(tail_rejects)+"\n\n")
log.write("34bp_spacers:\t"+str(bp34_spacers)+"\n")
log.write("31bp_spacers:\t"+str(bp31_spacers)+"\n")
log.write("32bp_spacers:\t"+str(bp32_spacers)+"\n")
log.write("33bp_spacers:\t"+str(bp33_spacers)+"\n")
log.write("total(31-33)_spacers:\t"+str(len(spacers))+"\n\n\n")
#####Mapping with bowtie
#Before this step you need to create a bowtie index:
#bowtie-build reference.fasta reference_bowtie_index
for filename in os.listdir(path):
if filename.endswith("spacers.fastq"): #Ignoring 34bp spacers currently
output = filename[:filename.rindex(".fastq")]+"_bowtie.txt"
index_file = "reference/CRISPR_combined"
seed_length = 31
max_total_mismatches = 0
max_alignments = 1
log = path+filename[:6]+"_log.log"
#Choose one of the following mapping approaches:
#Bowtie mapping limiting to max_alignments number of hits per spacer
# bowtie_call = f"bowtie -a -l {seed_length} -v {max_total_mismatches} -m {max_alignments} {index_file} -q {filename} {output} >> {log} 2>&1"
#Bowtie mapping allowing all spacer matches
bowtie_call = f"bowtie -a -l {seed_length} -v {max_total_mismatches} {index_file} -q {filename} {output} >> {log} 2>&1"
with open(log, "a") as log:
log.write("Mapping for file:\t"+filename+"\n")
log.write(bowtie_call+"\n")
print(f"bowtie mapping vs reference: {index}")
call(bowtie_call, shell=True)
###Generate weblogos and flanking and spacer fasta files#####
for filename in os.listdir(path):
if filename.endswith("bowtie.txt"):
print("Parsing bowtie mappings:\t"+filename)
mapped_spacer_count=0
chromosome_1 = []
chromosome_2 = []
PLE1_mutated = []
ICP1_2011_A = []
ICP1_2011_A_wo_array = []
with open(filename, "r") as maps:
for line in maps:
mapped_spacer_count+=1
line = line.split()
spacer_ident = line[0]
spacer_strand = line[1]
spacer_location = int(line[3])
spacer_sequence = line[4]
spacer_length = len(spacer_sequence)
spacer_count = 1
spacer_in_list = False
spacer_info = [spacer_sequence, spacer_location, spacer_strand, spacer_length, spacer_count]
if 0 <= spacer_location <= 3070910:
index_pos = 0
for spacer in chromosome_1:
if spacer[0] == spacer_sequence and spacer[1] == spacer_location:
chromosome_1[index_pos][4] +=1
spacer_in_list = True
break
index_pos += 1
if spacer_in_list == False:
chromosome_1.append(spacer_info)
elif 3070920 <= spacer_location <= 4162285:
index_pos = 0
for spacer in chromosome_2:
if spacer[0] == spacer_sequence and spacer[1] == spacer_location:
chromosome_2[index_pos][4] +=1
spacer_in_list = True
break
index_pos += 1
if spacer_in_list == False:
chromosome_2.append(spacer_info)
elif 4162320 <= spacer_location <= 4180365:
index_pos = 0
for spacer in PLE1_mutated:
if spacer[0] == spacer_sequence and spacer[1] == spacer_location:
PLE1_mutated[index_pos][4] +=1
spacer_in_list = True
break
index_pos += 1
if spacer_in_list == False:
PLE1_mutated.append(spacer_info)
elif 4180380 <= spacer_location <= 4306730:
index_pos = 0
for spacer in ICP1_2011_A_wo_array:
if spacer[0] == spacer_sequence and spacer[1] == spacer_location:
ICP1_2011_A_wo_array[index_pos][4] +=1
spacer_in_list = True
break
index_pos += 1
if spacer_in_list == False:
ICP1_2011_A_wo_array.append(spacer_info)
reference_list = [["chromosome_1",chromosome_1, 3070910],["chromosome_2",chromosome_2, 1091365],["PLE1_mutated",PLE1_mutated, 18045], ["ICP1_2011_A_wo_array", ICP1_2011_A_wo_array, 126350]]
record = SeqIO.read(index, "fasta")
for reference in reference_list:
y=1
with open(filename[:filename.rindex("_trimmed_spacers")]+f"_{reference[0]}_flanking.fasta","w") as fasta_flanking:
pam_list=[]
pam_count=[]
pam_cumulative_count=[]
length_list = [0,0,0]
plus_count = 0
minus_count = 0
plus_cumulative = 0
minus_cumulative = 0
for loc in reference[1]:
strand = loc[2]
site = loc[1]
length = loc[3]
count = loc[4]
sequence = loc[0]
length_list[length-31]+=1*count
if strand == "+":
plus_count += 1
plus_cumulative += count
pam = str(record.seq[site-2:site].reverse_complement())
flanking = record.seq[site-10:site] +"NNN"+ record.seq[site+length:site+length+10]
flanking = flanking.reverse_complement()
fasta_flanking.write(f">{reference[0]}_{length}_{strand}_{site}_{count}_{y}\n{flanking}\n")
if pam in pam_list:
pam_count[pam_list.index(pam)]+=1
pam_cumulative_count[pam_list.index(pam)]+=count
else:
pam_list.append(pam)
pam_count.append(1)
pam_cumulative_count.append(count)
elif strand == "-":
minus_count += 1
minus_cumulative += count
pam = str(record.seq[site+length:site+length+2])
flanking = record.seq[site-10:site] +"NNN"+ record.seq[site+length:site+length+10]
fasta_flanking.write(f">{reference[0]}_{length}_{strand}_{site}_{count}_{y}\n{flanking}\n")
if pam in pam_list:
pam_count[pam_list.index(pam)]+=1
pam_cumulative_count[pam_list.index(pam)]+=count
else:
pam_list.append(pam)
pam_count.append(1)
pam_cumulative_count.append(count)
with open(filename[:filename.rindex("_trimmed_spacers")]+f"_{reference[0]}_spacers.fasta","a") as fa_out:
fa_out.write(f">{record.id}_{count}_{length}_{strand}_{site}_{y}\n{sequence}\n")
y+=1
log = path+filename[:6]+"_log.log"
with open(log, "a") as log:
log.write("\n\n"+reference[0]+"\n")
log.write("PAM\tRep\tAll\n")
for i in range(len(pam_list)):
log.write(str(pam_list[i])+"\t"+str(pam_count[i])+"\t"+str(pam_cumulative_count[i])+"\n")
log.write("\n")
log.write("+\t"+str(plus_count)+"\t"+str(plus_cumulative)+"\n")
log.write("-\t"+str(minus_count)+"\t"+str(minus_cumulative)+"\n")
log.write("Tot\t"+str(sum(pam_count))+"\t"+str(sum(pam_cumulative_count))+"\n")
z=31
for count in length_list:
log.write(str(z)+"bp spacer count:\t"+ str(count)+"\n")
z+=1
#Generate flanking weblogos
file = filename[:filename.rindex("_trimmed_spacers")]+f"_{reference[0]}_flanking.fasta"
title = "Sample:"+filename[:filename.rindex(".R1")]+"\ Target:" + reference[0]
file_out = file+".eps"
annotation = "10,,,,,5,,,,1,[,SPACER,],1,,,,,5,,,,10"
weblogo = f"weblogo -A dna -D fasta -E YES -P '' --errorbars NO -U bits -t {title} --title-fontsize 10 --number-fontsize 4 --annotate '{annotation}' -f {file} -o {file_out}"
call(weblogo, shell=True)
#####Output *.graph files for brig#####
index_records = list(SeqIO.parse(index, "fasta"))
for filename in os.listdir(path):
log = path+filename[:6]+"_log.log"
if filename.endswith("spacers.fasta"):
ref = filename[filename.index("R1")+3:filename.rindex("_spacers.fasta")]
for ref_record in reference_list:
if ref_record[0] == ref:
template_len = ref_record[2]
pos_list = []
pos_list2 = []
for i in range(template_len):
pos_list.append(0.0)
pos_list2.append(0.0)
with open(filename, "r") as spacer_file:
count1 = 0
count2 = 0
for spacer_record in SeqIO.parse(spacer_file, "fasta"):
spacer_length = int(spacer_record.id.split("_")[-4])
strand = spacer_record.id.split("_")[-3]
count = int(spacer_record.id.split("_")[-5])
site = int(spacer_record.id.split("_")[-2])
if ref_record[0] == "chromosome_1":
site = site-0
elif ref_record[0] == "chromosome_2":
site = site-3070920
elif ref_record[0] == "PLE1_mutated":
site = site-4162320
elif ref_record[0] == "ICP1_2011_A_wo_array":
site = site-4180380
if strand == "+":
for j in range(spacer_length):
pos_list[site+j]+=1*count
count1+=1
elif strand == "-":
for j in range(spacer_length):
pos_list2[site-j]+=1*count
count2+=1
with open(log, "a") as log:
log.write("\n\n"+ref_record[0]+"\n")
log.write("Max + strand spacers:\t"+str(max(pos_list))+"\n")
log.write("Max - strand spacers:\t"+str(max(pos_list2))+"\n")
with open(filename+"_for.graph", "w") as graph_out:
x=0
for pos in pos_list:
graph_out.write(f"{str(x)}\t{str(x+1)}\t{pos}\n")
x+=1
with open(filename+"_rev.graph", "w") as graph_out:
x=0
for pos2 in pos_list2:
graph_out.write(f"{str(x)}\t{str(x+1)}\t{pos2}\n")
x+=1
######Create brigD3 images#####
for filename in os.listdir(path+"reference/"):
if filename.endswith(".gbk"):
record_name = path+"reference/"+filename
ref_name = filename[:filename.index(".gbk")]
for filename in os.listdir(path):
if filename.endswith(".graph") and ref_name in filename and "_for" in filename:
record = SeqIO.read(record_name, "genbank")
record_ori = SeqIO.read(record_name, "genbank")
scale_height = 0
if "ICP1" in filename:
scale_height = 10
elif "chromosome" in filename:
scale_height = 1000
else:
scale_height = 50000
for_data = []
with open(filename, "r") as for_graph_data:
for line in for_graph_data:
line=line.split()
for_data.append((int(line[1]),float(line[2])))
if float(line[2]) > scale_height:
scale_height=float(line[2])
for_data.append((len(record.seq)+5,scale_height))
rev_data = []
with open(filename.replace("_for.graph", "_rev.graph"), "r") as rev_graph_data:
for line in rev_graph_data:
line=line.split()
rev_data.append((int(line[1]),float(line[2])))
if float(line[2]) > scale_height:
scale_height=float(line[2])
rev_data.append((len(record.seq)+5,scale_height))
print(filename)
print("Max height: "+ str(scale_height))
if "ICP1" in filename:
scale_height = 10
elif "chromosome" in filename:
scale_height = 1000
else:
scale_height = 50000
gd_diagram = GenomeDiagram.Diagram("temp")
gd_track_for_features = gd_diagram.new_track(5, name="Annotated Features", height=1, scale_ticks=0, greytrack=1, greytrack_labels=0, scale=0)
gd_feature_set = gd_track_for_features.new_set(type='feature')
gd_track_for_feature_names = gd_diagram.new_track(2, name="Annotated names", height=1, scale_ticks=1, greytrack=0, greytrack_labels=0, scale=1, hide=1)
gd_feature_names_set = gd_track_for_feature_names.new_set(type='feature')
gd_track_for_feature_scale = gd_diagram.new_track(1, name="Annotated scale", hide=1, height=1, scale_ticks=1, greytrack=0, greytrack_labels=0, scale=1, scale_color=(0,0,0), scale_largetick_interval=20000, scale_largeticks=0.5, scale_smalltick_interval=100000)
gd_feature_scale_set = gd_track_for_feature_scale.new_set(type='feature')
#ORF name lines
for feature in record.features:
if feature.type != "CDS":
continue
feature.strand=-1
if "chromosome" in filename:
gd_feature_set.add_feature(feature, color=(245,245,245), label=False, sigil="BIGARROW", label_position="middle", label_size=10) #Color change from 254 (white) to ring grey
elif "ICP1" in filename:
gd_feature_set.add_feature(feature, color=(245,245,245), label=True, sigil="BIGARROW", label_position="middle", label_size=6)
else:
gd_feature_set.add_feature(feature, color=(245,245,245), label=True, sigil="BIGARROW", label_position="middle", label_size=17) #Color change from 254 (white) to ring grey
#ORF arrows
record = record_ori
for feature in record.features:
if feature.type != "CDS":
continue
if feature.strand == 1:
color = (255,255,255)
else:
color = (255,255,255)
if "chromosome" in filename:
gd_feature_set.add_feature(feature, color=color, label=True, sigil="BIGARROW", arrowshaft_height=1.0, label_position="middle", label_size=10, name="", arrowhead_length=0)
else:
gd_feature_set.add_feature(feature, color=color, label=True, sigil="BIGARROW", arrowshaft_height=1.0, label_position="middle", label_size=10, name="")
feature.strand=1
gd_track_for_graph = gd_diagram.new_track(6, name="graph", height=5, scale_ticks=0, greytrack=0, greytrack_labels=0, scale=0)
gd_graph_set = gd_track_for_graph.new_set(type='graph')
if "chromosome" in filename:
gd_graph_set.new_graph(for_data, name="graph test", style="line", color=colors.red, altcolor=colors.white, linewidth=.2)
else:
gd_graph_set.new_graph(for_data, name="graph test", style="line", color=colors.red, altcolor=colors.white, linewidth=1)
gd_graph_set = gd_track_for_graph.new_set(type='graph')
if "chromosome" in filename:
gd_graph_set.new_graph(rev_data, name="graph test2", style="line", color=colors.blue, altcolor=colors.white, linewidth=.2)
else:
gd_graph_set.new_graph(rev_data, name="graph test2", style="line", color=colors.blue, altcolor=colors.white, linewidth=1)
scale_bar_data = []
for num in range(len(record)):
if num == 0:
scale_bar_data.append((num,scale_height))
elif num == round(len(record)/4):
scale_bar_data.append((num,scale_height))
elif num == round(len(record)/2):
scale_bar_data.append((num,scale_height))
elif num == round(len(record)*3/4):
scale_bar_data.append((num,scale_height))
else:
scale_bar_data.append((num,0))
gd_graph_set = gd_track_for_graph.new_set(type='graph')
gd_graph_set.new_graph(scale_bar_data, name="scale bar", style="line", color=colors.black, altcolor=colors.white)
try:
gd_diagram.draw(format="circular", circular=True, pagesize=(50*cm,50*cm), start=0, end=len(record), circle_core=.3, fragments=1)
gd_diagram.write(filename+"_"+ref_name+"_spacer_map.pdf", "pdf")
except:
print(sys.exc_info())
pass
name = filename+"_"+ref_name+"_spacer_map.pdf"
convert_call = f"convert -density 1200 {name} {name[:name.index('.pdf')]}.jpg"
call(convert_call, shell=True)
weblogo_group = "gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dSAFER -r300 -dDEVICEWIDTHPOINTS=300 -dDEVICEHEIGHTPOINTS=120 -sOutputFile=weblogos_motifs.pdf *.eps"
call(weblogo_group, shell=True)
tar_call = "tar -c -f spacer_and_flanking_sequences.tar *.fasta"
call(tar_call, shell=True)
tar_call = "tar -czvf graph_files_for_brig.tar *.graph"
call(tar_call, shell=True)
for filename in os.listdir(path):
if ".eps" in filename or filename.endswith(".graph") or filename.endswith("flanking.fasta") or filename.endswith("spacers.fasta"):
os.remove(filename)