-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0-ETL_1001_methylomes.R
827 lines (744 loc) · 54.5 KB
/
0-ETL_1001_methylomes.R
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
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
#!/usr/bin/env Rscript
# 0-ETL_1001_methylomes.R
#
# Jay Moore, John Innes Centre, Norwich
#
# 28/10/2019
#
# Version 1 - Staging script for 1001 methylomes data
#
# Change log
#
# Summary of functions
#
# Done:
#
# Generate batch scripts to grab 1001 methylomes data and prepare it for analysis
#
# Batch up the samples into small groups (10 samples per batch)
# Divide the process up into steps.
# Step 0 gets run on the software7 node so is isolated from other steps.
# For the remaining steps, sbatch step 1 on the cluster, and when a step completes for its batch of 10 samples it calls the next step in the chain
#
# 0_wget_batch: retrieve the fastq.gz files by ftp from EBI SRA
# 1_dtool_create: make a local dtool dataset for each sample
# 2_dtool_readme: prepare a readme for the dtool dataset
# 3_dtool_freeze: freeze the dtool dataset by making hash
# 4_dtool_copy: transfer the dataset to iRODS
# 5_dtool_tidy: delete the local dtool dataset
# 6_align: bring the dataset back from iRODS to the cluster node SSD and align each run to the reference. Transfer the resulting .cov file to storage
# 7_call_methylation: Call R script to load the .cov file for each run, consolidate runs for each sample, apply models to call methylation status, and save the reulting all_samples_meth_status for the sample
#
#
# Underway:
#
#
# To do:
#
#
#
# Results:
#
#
# R script to assemble raw data from manifest files from SRA and ENA, and generate a script to download and align these data
path = "X:/Daniel-Zilberman/Projects/Jay-1001_methylomes/0-raw_data/"
setwd(path)
linux_path = "/jic/scratch/groups/Daniel-Zilberman/Projects/Jay-1001_methylomes/0-raw_data/"
# Global Arabidopsis accessions from Ecker study (927 of)
sample_list1 = read.table(paste0(path,"GSE43857.txt"), sep="\t", header=TRUE) # from https://www.ncbi.nlm.nih.gov/geo/browse/?view=samples&series=43857
fastq_list1 = read.table(paste0(path,"ENA_SRA065807.txt"), sep="\t", header=TRUE) # from https://www.ebi.ac.uk/ena/data/view/SRA065807
# Swedish Arabidopsis accessions from separate study (284 of)
sample_list2 = read.table(paste0(path,"GSE54292.txt"), sep="\t", header=TRUE) # from https://www.ncbi.nlm.nih.gov/geo/browse/?view=samples&series=54292
fastq_list2 = read.table(paste0(path,"ENA_SRP035593.txt"), sep="\t", header=TRUE) # from https://www.ebi.ac.uk/ena/data/view/PRJNA236110
# consolidate the two sets of data
sample_list = rbind.data.frame(sample_list1, sample_list2)
fastq_list = rbind.data.frame(fastq_list1, fastq_list2)
# clean up
rm(sample_list1, fastq_list1, sample_list2, fastq_list2)
# associate fastq files with runs, and runs with samples
sample_files = data.frame()
for (this_accession in sample_list$SRA.Accession) {
these_files = fastq_list[fastq_list$experiment_accession == this_accession,"fastq_ftp"]
for (this_file in these_files) {
some_files=strsplit(this_file,";")
sample_files = rbind.data.frame(sample_files, c("Accession" = as.character(sample_list[sample_list$SRA.Accession==this_accession,"Accession"]), "Title" = as.character(sample_list[sample_list$SRA.Accession==this_accession,"Title"]), "SRA_Accession" = this_accession, "FASTQ_file" = some_files[[1]][[1]]), stringsAsFactors=FALSE)
if (length(some_files[[1]]) > 1) {
sample_files = rbind.data.frame(sample_files, c("Accession" = as.character(sample_list[sample_list$SRA.Accession==this_accession,"Accession"]), "Title" = as.character(sample_list[sample_list$SRA.Accession==this_accession,"Title"]), "SRA_Accession" = this_accession, "FASTQ_file" = some_files[[1]][[2]]), stringsAsFactors=FALSE)
# it looks like a lot of the paired-end files are represented as 'SINGLE' but should be 'PAIRED' - replace where necessary
if (fastq_list[fastq_list$fastq_ftp == this_file,"library_layout"] == "SINGLE") {
fastq_list[fastq_list$fastq_ftp == this_file,"library_layout"] = "PAIRED"
}
} else {
#cat(fastq_list[fastq_list$experiment_accession == this_accession,"library_layout"])
}
}
}
colnames(sample_files) = c("Accession", "Title", "SRA_Accession", "FASTQ_file")
# break data set up into batches of X samples, and write a separate script to download each batch
batch_size = 10
# Capture the end of each batch run in an overall staging log
log_file = paste0(linux_path, "staging_log.txt")
# Ouput all the steps to a launch script for completeness
launch_script = paste0("launch_script.sh")
launch_script_contents = list()
# Generate the script files for the first step (0)
current_step = "0-wget_batch_"
batch_counter = 0
batch_number = 0
# start a new batch
script_contents = list()
for (this_accession in 1:nrow(sample_list)) {
batch_counter = batch_counter + 1
for (this_file in sample_files[sample_files$SRA_Accession == sample_list[this_accession,"SRA.Accession"],"FASTQ_file"]) {
script_contents = c(script_contents, paste0("wget ",this_file, "\n"))
}
if ((batch_counter==batch_size) | (this_accession==nrow(sample_list))) {
# finish batch
# at the start do stuff (not needed here)
script_header = ""
script_header = paste0("if [ ! -f ", linux_path, current_step, batch_number, ".txt ]; then\n")
# at the end a flag file indicating the step is complete
#script_footer = paste0("echo complete>", linux_path, current_step, batch_number, ".txt")
script_footer = paste0("echo complete>", linux_path, current_step, batch_number, ".txt\necho ", current_step, batch_number, " complete>>", log_file, "\nelse\necho ", current_step, batch_number, " already_complete>>", log_file, "\nfi\n")
script_contents = c(script_header, script_contents, script_footer)
write.table(script_contents, file=paste0(path,current_step,batch_number,".sh"), sep="", col.names=FALSE, row.names=FALSE, quote=FALSE)
launch_script_contents = c(launch_script_contents, "sbatch ", paste0(path,current_step,batch_number,".sh\n"))
if (this_accession<nrow(sample_list)) {
# start new batch
script_contents = list()
batch_number = batch_number + 1
batch_counter = 0
}
}
}
# Generate the script files for the next step (1)
previous_step = "0-wget_batch_"
current_step = "1-dtool_create_"
next_step = "2-dtool_readme_"
batch_counter = 0
batch_number = 0
# start a new batch
script_contents = list()
for (this_accession in 1:nrow(sample_list)) {
batch_counter = batch_counter + 1
# make a new dtool_dataset for the accession
script_contents = c(script_contents, paste0("dtool create ",sample_list[this_accession,"SRA.Accession"], "\n"))
for (this_file in sample_files[sample_files$SRA_Accession == sample_list[this_accession,"SRA.Accession"],"FASTQ_file"]) {
# add each of the files for the accession to the dtool_dataset
this_filename = strsplit(this_file,"/")[[1]][[length(strsplit(this_file,"/")[[1]])]]
script_contents = c(script_contents, paste0("mv ", linux_path, this_filename, " ", linux_path, "dtool_datasets/", sample_list[this_accession, "SRA.Accession"], "/data \n"))
}
if ((batch_counter==batch_size) | (this_accession==nrow(sample_list))) {
# finish batch
slurm_header = paste0("#!/bin/bash -e\n#SBATCH -p jic-short # partition (queue)\n#SBATCH --mail-type=END,FAIL # notifications for job done & fail\n#SBATCH [email protected] # send-to address\n#SBATCH --array=\n#SBATCH --mem=16000\n#SBATCH -o ", current_step, batch_number, ".out\n#SBATCH -e ", current_step, batch_number, ".err\n")
# at the start check to ensure flag file exists for previous step, and flag file doesn't exist for current step
script_header = paste0(slurm_header, "until [ -f ", linux_path, previous_step, batch_number, ".txt ]; do\n sleep 60\ndone\nif [ ! -f ", linux_path, current_step, batch_number, ".txt ]; then\ncd ",linux_path,"dtool_datasets\n#iinit\nsource /jic/scratch/groups/Daniel-Zilberman/load_dtool.sh\n")
# at the end make a flag file indicating the step is complete
script_footer = paste0("echo complete>", linux_path, current_step, batch_number, ".txt\necho ", current_step, batch_number, " complete>>", log_file, "\ncd ..\nsbatch ", next_step, batch_number, ".sh\nelse\necho ", current_step, batch_number, " already_complete>>", log_file, "\nfi\n")
script_contents = c(script_header, script_contents, script_footer)
write.table(script_contents, file=paste0(path,current_step,batch_number,".sh"), sep="", col.names=FALSE, row.names=FALSE, quote=FALSE)
launch_script_contents = c(launch_script_contents, "sbatch ", paste0(path,current_step,batch_number,".sh\n"))
if (this_accession<nrow(sample_list)) {
# start new batch
script_contents = list()
batch_number = batch_number + 1
batch_counter = 0
}
}
}
# Generate the script files for the next step (2)
previous_step = "1-dtool_create_"
current_step = "2-dtool_readme_"
next_step = "3-dtool_freeze_"
batch_counter = 0
batch_number = 0
# start a new batch
script_contents = list()
for (this_accession in 1:nrow(sample_list)) {
batch_counter = batch_counter + 1
# make a new dtool readme for the accession based on the pre-prepared README.yml
script_contents = c(script_contents, paste0("echo --- > ", batch_counter, "_readme.yml\n"))
script_contents = c(script_contents, paste0("echo description: Bisulphite sequencing, SRA accession ", sample_list[this_accession,"SRA.Accession"], " >> ", batch_counter, "_readme.yml\n"))
script_contents = c(script_contents, paste0("cat README.yml >> ", batch_counter, "_readme.yml\n"))
script_contents = c(script_contents, paste0("dtool readme write ",sample_list[this_accession,"SRA.Accession"], " ", batch_counter, "_readme.yml\n"))
script_contents = c(script_contents, paste0("rm ", batch_counter, "_readme.yml\n"))
if ((batch_counter==batch_size) | (this_accession==nrow(sample_list))) {
# finish batch
slurm_header = paste0("#!/bin/bash -e\n#SBATCH -p jic-short # partition (queue)\n#SBATCH --mail-type=END,FAIL # notifications for job done & fail\n#SBATCH [email protected] # send-to address\n#SBATCH --array=\n#SBATCH --mem=16000\n#SBATCH -o ", current_step, batch_number, ".out\n#SBATCH -e ", current_step, batch_number, ".err\n")
# at the start check to ensure flag file exists for previous step, and flag file doesn't exist for current step
script_header = paste0(slurm_header, "until [ -f ", linux_path, previous_step, batch_number, ".txt ]; do\n sleep 60\ndone\nif [ ! -f ", linux_path, current_step, batch_number, ".txt ]; then\ncd ",linux_path,"dtool_datasets\n#iinit\nsource /jic/scratch/groups/Daniel-Zilberman/load_dtool.sh\n")
# at the end make a flag file indicating the step is complete
script_footer = paste0("echo complete>", linux_path, current_step, batch_number, ".txt\necho ", current_step, batch_number, " complete>>", log_file, "\ncd ..\nsbatch ", next_step, batch_number, ".sh\nelse\necho ", current_step, batch_number, " already_complete>>", log_file, "\nfi\n")
script_contents = c(script_header, script_contents, script_footer)
write.table(script_contents, file=paste0(path,current_step,batch_number,".sh"), sep="", col.names=FALSE, row.names=FALSE, quote=FALSE)
launch_script_contents = c(launch_script_contents, "sbatch ", paste0(path,current_step,batch_number,".sh\n"))
if (this_accession<nrow(sample_list)) {
# start new batch
script_contents = list()
batch_number = batch_number + 1
batch_counter = 0
}
}
}
# Generate the script files for the next step (3)
previous_step = "2-dtool_readme_"
current_step = "3-dtool_freeze_"
next_step = "4-dtool_copy_"
batch_counter = 0
batch_number = 0
# start a new batch
script_contents = list()
for (this_accession in 1:nrow(sample_list)) {
batch_counter = batch_counter + 1
# freeze the dtool dataset
script_contents = c(script_contents, paste0("/jic/scratch/groups/Daniel-Zilberman/dtool-slurm-scripts/dtool_freeze.slurm.sh ",sample_list[this_accession,"SRA.Accession"], "\n"))
if ((batch_counter==batch_size) | (this_accession==nrow(sample_list))) {
# finish batch
slurm_header = paste0("#!/bin/bash -e\n#SBATCH -p jic-short # partition (queue)\n#SBATCH --mail-type=END,FAIL # notifications for job done & fail\n#SBATCH [email protected] # send-to address\n#SBATCH --array=\n#SBATCH --mem=16000\n#SBATCH -o ", current_step, batch_number, ".out\n#SBATCH -e ", current_step, batch_number, ".err\n")
# at the start check to ensure flag file exists for previous step, and flag file doesn't exist for current step
script_header = paste0(slurm_header, "until [ -f ", linux_path, previous_step, batch_number, ".txt ]; do\n sleep 60\ndone\nif [ ! -f ", linux_path, current_step, batch_number, ".txt ]; then\ncd ",linux_path,"dtool_datasets\n#iinit\nsource /jic/scratch/groups/Daniel-Zilberman/load_dtool.sh\n")
# at the end make a flag file indicating the step is complete
script_footer = paste0("echo complete>", linux_path, current_step, batch_number, ".txt\necho ", current_step, batch_number, " complete>>", log_file, "\ncd ..\nsbatch ", next_step, batch_number, ".sh\nelse\necho ", current_step, batch_number, " already_complete>>", log_file, "\nfi\n")
script_contents = c(script_header, script_contents, script_footer)
write.table(script_contents, file=paste0(path,current_step,batch_number,".sh"), sep="", col.names=FALSE, row.names=FALSE, quote=FALSE)
launch_script_contents = c(launch_script_contents, "sbatch ", paste0(path,current_step,batch_number,".sh\n"))
if (this_accession<nrow(sample_list)) {
# start new batch
script_contents = list()
batch_number = batch_number + 1
batch_counter = 0
}
}
}
irods_uri = "irods:/jic_overflow/rg-daniel-zilberman/"
# Generate the script files for the next step (4)
previous_step = "3-dtool_freeze_"
current_step = "4-dtool_copy_"
next_step = "5-dtool_tidy_"
batch_counter = 0
batch_number = 0
# start a new batch
script_contents = list()
for (this_accession in 1:nrow(sample_list)) {
batch_counter = batch_counter + 1
# commit the dtool dataset to iRODS
#script_contents = c(script_contents, paste0("/jic/scratch/groups/Daniel-Zilberman/dtool-slurm-scripts/dtool_copy.slurm.sh ",sample_list[this_accession,"SRA.Accession"], " ", irods_uri, "\n"))
# Commit the dataset to iRODS and capture the URI
script_contents = c(script_contents, paste0("OUTPUT_URI=$(/jic/scratch/groups/Daniel-Zilberman/dtool-slurm-scripts/dtool_copy.slurm.sh ",sample_list[this_accession,"SRA.Accession"], " ", irods_uri, ")\n"))
# Add the current accession and the iRODS URI to a URIs log file for this batch
script_contents = c(script_contents, paste0("echo ", sample_list[this_accession,"SRA.Accession"], " $OUTPUT_URI >>", current_step, batch_number, "_uris.txt\n"))
if ((batch_counter==batch_size) | (this_accession==nrow(sample_list))) {
# finish batch
slurm_header = paste0("#!/bin/bash -e\n#SBATCH -p jic-long # partition (queue)\n#SBATCH --mail-type=END,FAIL # notifications for job done & fail\n#SBATCH [email protected] # send-to address\n#SBATCH --array=\n#SBATCH --mem=16000\n#SBATCH -o ", current_step, batch_number, ".out\n#SBATCH -e ", current_step, batch_number, ".err\n")
# at the start check to ensure flag file exists for previous step, and flag file doesn't exist for current step
script_header = paste0(slurm_header, "until [ -f ", linux_path, previous_step, batch_number, ".txt ]; do\n sleep 60\ndone\nif [ ! -f ", linux_path, current_step, batch_number, ".txt ]; then\ncd ",linux_path,"dtool_datasets\n#iinit\nsource /jic/scratch/groups/Daniel-Zilberman/load_dtool.sh\n")
# at the end make a flag file indicating the step is complete
script_footer = paste0("echo complete>", linux_path, current_step, batch_number, ".txt\necho ", current_step, batch_number, " complete>>", log_file, "\ncd ..\nsbatch ", next_step, batch_number, ".sh\nelse\necho ", current_step, batch_number, " already_complete>>", log_file, "\nfi\n")
script_contents = c(script_header, script_contents, script_footer)
write.table(script_contents, file=paste0(path,current_step,batch_number,".sh"), sep="", col.names=FALSE, row.names=FALSE, quote=FALSE)
launch_script_contents = c(launch_script_contents, "sbatch ", paste0(path,current_step,batch_number,".sh\n"))
if (this_accession<nrow(sample_list)) {
# start new batch
script_contents = list()
batch_number = batch_number + 1
batch_counter = 0
}
}
}
# Generate the script files for the next step (5)
previous_step = "4-dtool_copy_"
current_step = "5-dtool_tidy_"
next_step = "6-align_"
batch_counter = 0
batch_number = 0
# start a new batch
script_contents = list()
for (this_accession in 1:nrow(sample_list)) {
batch_counter = batch_counter + 1
# delete the local dtool dataset
script_contents = c(script_contents, paste0("rm -r ", linux_path, "dtool_datasets/", sample_list[this_accession,"SRA.Accession"], "\n"))
if ((batch_counter==batch_size) | (this_accession==nrow(sample_list))) {
# finish batch
slurm_header = paste0("#!/bin/bash -e\n#SBATCH -p jic-short # partition (queue)\n#SBATCH --mail-type=END,FAIL # notifications for job done & fail\n#SBATCH [email protected] # send-to address\n#SBATCH --array=\n#SBATCH --mem=16000\n#SBATCH -o ", current_step, batch_number, ".out\n#SBATCH -e ", current_step, batch_number, ".err\n")
# at the start check to ensure flag file exists for previous step, and flag file doesn't exist for current step
script_header = paste0(slurm_header, "until [ -f ", linux_path, previous_step, batch_number, ".txt ]; do\n sleep 60\ndone\nif [ ! -f ", linux_path, current_step, batch_number, ".txt ]; then\ncd ",linux_path,"dtool_datasets\n")
# at the end make a flag file indicating the step is complete
script_footer = paste0("echo complete>", linux_path, current_step, batch_number, ".txt\necho ", current_step, batch_number, " complete>>", log_file, "\ncd ..\nsbatch ", next_step, batch_number, ".sh\nelse\necho ", current_step, batch_number, " already_complete>>", log_file, "\nfi\n")
script_contents = c(script_header, script_contents, script_footer)
write.table(script_contents, file=paste0(path,current_step,batch_number,".sh"), sep="", col.names=FALSE, row.names=FALSE, quote=FALSE)
launch_script_contents = c(launch_script_contents, "sbatch ", paste0(path,current_step,batch_number,".sh\n"))
if (this_accession<nrow(sample_list)) {
# start new batch
script_contents = list()
batch_number = batch_number + 1
batch_counter = 0
}
}
}
# This is the Col-0 reference
reference_file = "/jic/scratch/groups/Daniel-Zilberman/Reference_data/Genomes/Arabidopsis/TAIR10/TAIR10_Chr.all.fasta"
reference_id = "TAIR10"
# This is the base location of the 1001 pseudogenomes references
#reference_file = ""
#reference_id=""
alignment_location = paste0(linux_path, "../3-alignments/")
# Generate the script files for the next step (6)
previous_step = "5-dtool_tidy_"
current_step = "6-align_"
next_step = "7-call_methylation_"
batch_counter = 0
batch_number = 0
# start a new batch
script_contents = list()
task_array = list()
for (this_accession in 1:nrow(sample_list)) {
# add the accession to the task array
#task_array = c(task_array, as.character(sample_list[this_accession,"SRA.Accession"]))
# abandoned task arrays as they would involve having the whole batch of alignment files on the SSD at the same time (ran out of space for some batches). Instead, decided to do them serially
batch_counter = batch_counter + 1
# go through the individual sets of reads (runs) for the accession, and check each one for SINGLE or PAIRED status
for (this_run in fastq_list[as.character(fastq_list$experiment_accession) == as.character(sample_list[this_accession,"SRA.Accession"]),"run_accession"]) {
this_library_layout = as.character(fastq_list[fastq_list$run_accession==this_run,"library_layout"])
if (this_library_layout == "SINGLE") {
# We are aligning SRRxxxxxxx.fastq.gz to the reference
script_contents = c(script_contents, "bsmap -a $BASE_DIR/", this_run, ".fastq.gz -d ", reference_file, " -o $BASE_DIR/", this_run, "_", reference_id, ".bsp\n")
# bsmap -a $R1fq -b $R2fq -d "${rawroot}"/Mp_TGAC_V1.1_scaffolds.fa -o "${alignroot}"/${ARRAY[$SLURM_ARRAY_TASK_ID]}_TGAC1.1.bsp
} else if (this_library_layout == "PAIRED") {
# we are aligning SRRxxxxxxx_1.fastq.gz and SRRxxxxxxx_2.fastq.gz to the reference
script_contents = c(script_contents, "bsmap -a $BASE_DIR/", this_run, "_1.fastq.gz -b $BASE_DIR/", this_run, "_2.fastq.gz -d ", reference_file, " -o $BASE_DIR/", this_run, "_", reference_id, ".bsp\n")
} else {
# there is no else because this can't happen
}
# The run is now aligned to the reference
# once alignment is done, make the CG sites concatenated file of c/t counts (.methratio)
script_contents = c(script_contents, "python ../Scripts/methratio.py -z -g -o $BASE_DIR/", this_run, "_", reference_id, ".methratio2 -d ", reference_file, " $BASE_DIR/", this_run, "_", reference_id, ".bsp\n")
# make the per-cytosine file
#python Scripts/methratio.py -z -o "${alignroot}"/${ARRAY[$SLURM_ARRAY_TASK_ID]}_TGAC1.1.methratio -d "${rawroot}"/Mp_TGAC_V1.1_scaffolds.fa "${alignroot}"/${ARRAY[$SLURM_ARRAY_TASK_ID]}_TGAC1.1.bsp
# filter the .methratio file for just CG sites (takes file size down from 5G to 0.6G so easier to load in R)
script_contents = c(script_contents, "head -n 1 $BASE_DIR/", this_run, "_", reference_id, ".methratio2 > $BASE_DIR/", this_run, "_", reference_id, ".cg.methratio2\n")
script_contents = c(script_contents, "awk '{if (substr($4,3,2) == \"CG\") { print } }' $BASE_DIR/", this_run, "_", reference_id, ".methratio2 >> $BASE_DIR/", this_run, "_", reference_id, ".cg.methratio2\n")
# move the finished gffs (.methratio and .methratio2) to storage
script_contents = c(script_contents, "mv $BASE_DIR/", this_run, "_", reference_id, ".methratio2 ", alignment_location, "\n")
script_contents = c(script_contents, "mv $BASE_DIR/", this_run, "_", reference_id, ".cg.methratio2 ", alignment_location, "\n")
# clean up the temporary space to make space for the next alignment
script_contents = c(script_contents, "rm $BASE_DIR/", this_run, "_", reference_id, ".*\n")
}
if ((batch_counter==batch_size) | (this_accession==nrow(sample_list))) {
# finish batch
# make a slurm header with the right number of array entries for the job array
# output the contents of task_array as the ARRAY variable in the script
#slurm_header = paste0("#!/bin/bash -e\n#SBATCH -p jic-long # partition (queue)\n#SBATCH --mail-type=END,FAIL # notifications for job done & fail\n#SBATCH [email protected] # send-to address\n#SBATCH --array=0-",batch_counter - 1,"\n#SBATCH --mem=64000\nARRAY=(", paste(task_array, collapse=" "), ")\n")
# abandoned task arrays as they would involve having the whole batch of alignment files on the SSD at the same time (ran out of space for some batches). Instead, decided to do them serially
# version with no task array
slurm_header = paste0("#!/bin/bash -e\n#SBATCH -p jic-long # partition (queue)\n#SBATCH --mail-type=END,FAIL # notifications for job done & fail\n#SBATCH [email protected] # send-to address\n#SBATCH --array=\n#SBATCH --mem=64000\n#SBATCH -o ", current_step, batch_number, ".out\n#SBATCH -e ", current_step, batch_number, ".err\n#SBATCH --localscratch=ssd:100 # request 100GB SSD\n")
# at the start check to ensure flag file exists for previous step, and flag file doesn't exist for current step
script_header = paste0(slurm_header, "until [ -f ", linux_path, previous_step, batch_number, ".txt ]; do\n sleep 60\ndone\nif [ ! -f ", linux_path, current_step, batch_number, ".txt ]; then\n")
script_header = paste0(script_header, "BASE_DIR=$SLURM_LOCAL_SCRATCH\n")
script_header = paste0(script_header, "export DTOOL_CACHE_DIRECTORY=$BASE_DIR\n")
script_header = paste0(script_header, "#iinit\n")
script_header = paste0(script_header, "source /jic/scratch/groups/Daniel-Zilberman/load_dtool.sh\n")
script_header = paste0(script_header, "source bsmap-2.90.0\n")
script_header = paste0(script_header, "source samtools-1.9\n")
# add lines at start of script to go through the list of iRODS data sets associated with this batch and retrieve the data files
script_header = paste0(script_header, "input=dtool_datasets/4-dtool_copy_",batch_number,"_uris.txt\n")
script_header = paste0(script_header, "while IFS='\\n' read -r line\n")
script_header = paste0(script_header, "do\n")
script_header = paste0(script_header, "this_dataset=($line)\n")
script_header = paste0(script_header, "#echo ${this_dataset[1]}\n")
script_header = paste0(script_header, "dtool ls ${this_dataset[1]} | while read item; do\n")
script_header = paste0(script_header, "echo $item\n")
script_header = paste0(script_header, "this_item=($item)\n")
script_header = paste0(script_header, "#echo ${this_item[0]}\n")
script_header = paste0(script_header, "#echo ${this_item[1]}\n")
script_header = paste0(script_header, "READ1_ABSPATH=$(dtool item fetch ${this_dataset[1]} ${this_item[0]})\n")
script_header = paste0(script_header, "#echo $READ1_ABSPATH\n")
script_header = paste0(script_header, "mv $READ1_ABSPATH $BASE_DIR/${this_item[1]}\n")
script_header = paste0(script_header, "done\n")
script_header = paste0(script_header, "done<\"$input\"\n")
# we now have all the data files for the batch in SSD storage with their original filenames
# at the end make a flag file indicating the step is complete
script_footer = paste0("echo complete>", linux_path, current_step, batch_number, ".txt\necho ", current_step, batch_number, " complete>>", log_file, "\nsbatch ", next_step, batch_number, ".sh\nelse\necho ", current_step, batch_number, " already_complete>>", log_file, "\nfi\n")
script_contents = c(script_header, script_contents, script_footer)
write.table(script_contents, file=paste0(path,current_step,batch_number,".sh"), sep="", col.names=FALSE, row.names=FALSE, quote=FALSE)
launch_script_contents = c(launch_script_contents, "sbatch ", paste0(path,current_step,batch_number,".sh\n"))
if (this_accession<nrow(sample_list)) {
# start new batch
script_contents = list()
batch_number = batch_number + 1
batch_counter = 0
# start a new task_array
task_array = list()
}
}
}
# Generate the script files for the next step (7)
previous_step = "6-align_"
current_step = "7-call_methylation_"
next_step = "8-non_CG_methylation_"
batch_counter = 0
batch_number = 0
# start a new batch
script_contents = list()
for (this_accession in 1:nrow(sample_list)) {
batch_counter = batch_counter + 1
# Call R and pass the sample ID to the call_methylation script
script_contents = c(script_contents, paste0("R_zg <../Scripts/7-call_methylation.R --no-save --args --sample=", sample_list[this_accession,"SRA.Accession"], "\n"))
if ((batch_counter==batch_size) | (this_accession==nrow(sample_list))) {
# finish batch
slurm_header = paste0("#!/bin/bash -e\n#SBATCH -p jic-medium # partition (queue)\n#SBATCH --mail-type=END,FAIL # notifications for job done & fail\n#SBATCH [email protected] # send-to address\n#SBATCH --array=\n#SBATCH --mem=64000\n#SBATCH -o ", current_step, batch_number, ".out\n#SBATCH -e ", current_step, batch_number, ".err\n")
# at the start check to ensure flag file exists for previous step, and flag file doesn't exist for current step
script_header = paste0(slurm_header, "until [ -f ", linux_path, previous_step, batch_number, ".txt ]; do\n sleep 60\ndone\nif [ ! -f ", linux_path, current_step, batch_number, ".txt ]; then\n#cd ",linux_path,"../5-analysis\nsource R_zg-1.0.2\n")
# at the end make a flag file indicating the step is complete
script_footer = paste0("echo complete>", linux_path, current_step, batch_number, ".txt\necho ", current_step, batch_number, " complete>>", log_file, "\nsbatch ", next_step, batch_number, ".sh\nelse\necho ", current_step, batch_number, " already_complete>>", log_file, "\nfi\n")
script_contents = c(script_header, script_contents, script_footer)
write.table(script_contents, file=paste0(path,current_step,batch_number,".sh"), sep="", col.names=FALSE, row.names=FALSE, quote=FALSE)
# launch the next step in the process
#launch_script_contents = c(launch_script_contents, "sbatch ", paste0(path,current_step,batch_number,".sh\n"))
if (this_accession<nrow(sample_list)) {
# start new batch
script_contents = list()
batch_number = batch_number + 1
batch_counter = 0
}
}
}
# Generate the script files for the next step (8)
previous_step = "7-call_methylation_"
current_step = "8-non_CG_methylation_"
next_step = "9-dtool_create_mc_"
batch_counter = 0
batch_number = 0
# start a new batch
script_contents = list()
for (this_accession in 1:nrow(sample_list)) {
batch_counter = batch_counter + 1
for (this_run in fastq_list[as.character(fastq_list$experiment_accession) == as.character(sample_list[this_accession,"SRA.Accession"]),"run_accession"]) {
# make a file of the non-cg methylation
#script_contents = c(script_contents, "head -n 1 $BASE_DIR/", this_run, "_", reference_id, ".methratio2 > $BASE_DIR/", this_run, "_", reference_id, ".non-cg.methratio2\n")
# don't need to add header, as header does not contain "CG" in relevant position so gets output anyway. Also changed >> to > in line below
script_contents = c(script_contents, "awk '{if (substr($4,3,2) != \"CG\") { print } }' $BASE_DIR/", this_run, "_", reference_id, ".methratio2 > $BASE_DIR/", this_run, "_", reference_id, ".non-cg.methratio2\n")
}
# Call R and pass the sample ID to the call_methylation script
script_contents = c(script_contents, paste0("R_zg <../Scripts/8-non_CG_methylation.R --no-save --args --sample=", sample_list[this_accession,"SRA.Accession"], "\n"))
# delete the non-cg methylation files as redundant
for (this_run in fastq_list[as.character(fastq_list$experiment_accession) == as.character(sample_list[this_accession,"SRA.Accession"]),"run_accession"]) {
script_contents = c(script_contents, "rm $BASE_DIR/", this_run, "_", reference_id, ".non-cg.methratio2\n")
}
if ((batch_counter==batch_size) | (this_accession==nrow(sample_list))) {
# finish batch
slurm_header = paste0("#!/bin/bash -e\n#SBATCH -p jic-short # partition (queue)\n#SBATCH --mail-type=END,FAIL # notifications for job done & fail\n#SBATCH [email protected] # send-to address\n#SBATCH --array=\n#SBATCH --mem=64000\n#SBATCH -o ", current_step, batch_number, ".out\n#SBATCH -e ", current_step, batch_number, ".err\n")
# at the start check to ensure flag file exists for previous step, and flag file doesn't exist for current step
script_header = paste0(slurm_header, "until [ -f ", linux_path, previous_step, batch_number, ".txt ]; do\n sleep 60\ndone\nif [ ! -f ", linux_path, current_step, batch_number, ".txt ]; then\n#cd ",linux_path,"../5-analysis\nsource R_zg-1.0.2\n")
script_header = paste0(script_header, "BASE_DIR=../3-alignments\n")
# at the end make a flag file indicating the step is complete
script_footer = paste0("echo complete>", linux_path, current_step, batch_number, ".txt\necho ", current_step, batch_number, " complete>>", log_file, "\nsbatch ", next_step, batch_number, ".sh\nelse\necho ", current_step, batch_number, " already_complete>>", log_file, "\nfi\n")
script_contents = c(script_header, script_contents, script_footer)
write.table(script_contents, file=paste0(path,current_step,batch_number,".sh"), sep="", col.names=FALSE, row.names=FALSE, quote=FALSE)
# launch the next step in the process
#launch_script_contents = c(launch_script_contents, "sbatch ", paste0(path,current_step,batch_number,".sh\n"))
if (this_accession<nrow(sample_list)) {
# start new batch
script_contents = list()
batch_number = batch_number + 1
batch_counter = 0
}
}
}
# Generate the script files for the next step (9)
previous_step = "8-non_CG_methylation_"
current_step = "9-dtool_create_mc_"
next_step = "10-dtool_readme_mc_"
batch_counter = 0
batch_number = 0
# start a new batch
script_contents = list()
for (this_accession in 1:nrow(sample_list)) {
batch_counter = batch_counter + 1
# make a new dtool_dataset for the accession's methylation data
script_contents = c(script_contents, paste0("dtool create ",sample_list[this_accession,"SRA.Accession"], "_meth\n"))
for (this_run in fastq_list[as.character(fastq_list$experiment_accession) == as.character(sample_list[this_accession,"SRA.Accession"]),"run_accession"]) {
# add each of the files for the accession to the dtool_dataset
#this_filename = strsplit(this_file,"/")[[1]][[length(strsplit(this_file,"/")[[1]])]]
script_contents = c(script_contents, paste0("mv ", alignment_location, this_run, "_TAIR10.cg.methratio2 ", linux_path, "dtool_datasets/", sample_list[this_accession, "SRA.Accession"], "_meth/data \n"))
script_contents = c(script_contents, paste0("mv ", alignment_location, this_run, "_TAIR10.methratio2 ", linux_path, "dtool_datasets/", sample_list[this_accession, "SRA.Accession"], "_meth/data \n"))
}
if ((batch_counter==batch_size) | (this_accession==nrow(sample_list))) {
# finish batch
slurm_header = paste0("#!/bin/bash -e\n#SBATCH -p jic-short # partition (queue)\n#SBATCH --mail-type=END,FAIL # notifications for job done & fail\n#SBATCH [email protected] # send-to address\n#SBATCH --array=\n#SBATCH --mem=16000\n#SBATCH -o ", current_step, batch_number, ".out\n#SBATCH -e ", current_step, batch_number, ".err\n")
# at the start check to ensure flag file exists for previous step, and flag file doesn't exist for current step
script_header = paste0(slurm_header, "until [ -f ", linux_path, previous_step, batch_number, ".txt ]; do\n sleep 60\ndone\nif [ ! -f ", linux_path, current_step, batch_number, ".txt ]; then\ncd ",linux_path,"dtool_datasets\n#iinit\nsource /jic/scratch/groups/Daniel-Zilberman/load_dtool.sh\n")
# at the end make a flag file indicating the step is complete
script_footer = paste0("echo complete>", linux_path, current_step, batch_number, ".txt\necho ", current_step, batch_number, " complete>>", log_file, "\ncd ..\nsbatch ", next_step, batch_number, ".sh\nelse\necho ", current_step, batch_number, " already_complete>>", log_file, "\nfi\n")
script_contents = c(script_header, script_contents, script_footer)
write.table(script_contents, file=paste0(path,current_step,batch_number,".sh"), sep="", col.names=FALSE, row.names=FALSE, quote=FALSE)
launch_script_contents = c(launch_script_contents, "sbatch ", paste0(path,current_step,batch_number,".sh\n"))
if (this_accession<nrow(sample_list)) {
# start new batch
script_contents = list()
batch_number = batch_number + 1
batch_counter = 0
}
}
}
# Generate the script files for the next step (10)
previous_step = "9-dtool_create_mc_"
current_step = "10-dtool_readme_mc_"
next_step = "11-dtool_freeze_mc_"
batch_counter = 0
batch_number = 0
# start a new batch
script_contents = list()
for (this_accession in 1:nrow(sample_list)) {
batch_counter = batch_counter + 1
# make a new dtool readme for the accession based on the pre-prepared README.yml
script_contents = c(script_contents, paste0("echo --- > ", batch_counter, "_readme.yml\n"))
script_contents = c(script_contents, paste0("echo description: Bisulphite sequencing methylation coverage data, SRA accession ", sample_list[this_accession,"SRA.Accession"], " >> ", batch_counter, "_readme.yml\n"))
script_contents = c(script_contents, paste0("cat README.yml >> ", batch_counter, "_readme.yml\n"))
script_contents = c(script_contents, paste0("dtool readme write ",sample_list[this_accession,"SRA.Accession"], "_meth ", batch_counter, "_readme.yml\n"))
script_contents = c(script_contents, paste0("rm ", batch_counter, "_readme.yml\n"))
if ((batch_counter==batch_size) | (this_accession==nrow(sample_list))) {
# finish batch
slurm_header = paste0("#!/bin/bash -e\n#SBATCH -p jic-short # partition (queue)\n#SBATCH --mail-type=END,FAIL # notifications for job done & fail\n#SBATCH [email protected] # send-to address\n#SBATCH --array=\n#SBATCH --mem=16000\n#SBATCH -o ", current_step, batch_number, ".out\n#SBATCH -e ", current_step, batch_number, ".err\n")
# at the start check to ensure flag file exists for previous step, and flag file doesn't exist for current step
script_header = paste0(slurm_header, "until [ -f ", linux_path, previous_step, batch_number, ".txt ]; do\n sleep 60\ndone\nif [ ! -f ", linux_path, current_step, batch_number, ".txt ]; then\ncd ",linux_path,"dtool_datasets\n#iinit\nsource /jic/scratch/groups/Daniel-Zilberman/load_dtool.sh\n")
# at the end make a flag file indicating the step is complete
script_footer = paste0("echo complete>", linux_path, current_step, batch_number, ".txt\necho ", current_step, batch_number, " complete>>", log_file, "\ncd ..\nsbatch ", next_step, batch_number, ".sh\nelse\necho ", current_step, batch_number, " already_complete>>", log_file, "\nfi\n")
script_contents = c(script_header, script_contents, script_footer)
write.table(script_contents, file=paste0(path,current_step,batch_number,".sh"), sep="", col.names=FALSE, row.names=FALSE, quote=FALSE)
launch_script_contents = c(launch_script_contents, "sbatch ", paste0(path,current_step,batch_number,".sh\n"))
if (this_accession<nrow(sample_list)) {
# start new batch
script_contents = list()
batch_number = batch_number + 1
batch_counter = 0
}
}
}
# Generate the script files for the next step (11)
previous_step = "10-dtool_readme_mc_"
current_step = "11-dtool_freeze_mc_"
next_step = "12-dtool_copy_mc_"
batch_counter = 0
batch_number = 0
# start a new batch
script_contents = list()
for (this_accession in 1:nrow(sample_list)) {
batch_counter = batch_counter + 1
# freeze the dtool dataset
script_contents = c(script_contents, paste0("/jic/scratch/groups/Daniel-Zilberman/dtool-slurm-scripts/dtool_freeze.slurm.sh ",sample_list[this_accession,"SRA.Accession"], "_meth\n"))
if ((batch_counter==batch_size) | (this_accession==nrow(sample_list))) {
# finish batch
slurm_header = paste0("#!/bin/bash -e\n#SBATCH -p jic-short # partition (queue)\n#SBATCH --mail-type=END,FAIL # notifications for job done & fail\n#SBATCH [email protected] # send-to address\n#SBATCH --array=\n#SBATCH --mem=16000\n#SBATCH -o ", current_step, batch_number, ".out\n#SBATCH -e ", current_step, batch_number, ".err\n")
# at the start check to ensure flag file exists for previous step, and flag file doesn't exist for current step
script_header = paste0(slurm_header, "until [ -f ", linux_path, previous_step, batch_number, ".txt ]; do\n sleep 60\ndone\nif [ ! -f ", linux_path, current_step, batch_number, ".txt ]; then\ncd ",linux_path,"dtool_datasets\n#iinit\nsource /jic/scratch/groups/Daniel-Zilberman/load_dtool.sh\n")
# at the end make a flag file indicating the step is complete
script_footer = paste0("echo complete>", linux_path, current_step, batch_number, ".txt\necho ", current_step, batch_number, " complete>>", log_file, "\ncd ..\nsbatch ", next_step, batch_number, ".sh\nelse\necho ", current_step, batch_number, " already_complete>>", log_file, "\nfi\n")
script_contents = c(script_header, script_contents, script_footer)
write.table(script_contents, file=paste0(path,current_step,batch_number,".sh"), sep="", col.names=FALSE, row.names=FALSE, quote=FALSE)
launch_script_contents = c(launch_script_contents, "sbatch ", paste0(path,current_step,batch_number,".sh\n"))
if (this_accession<nrow(sample_list)) {
# start new batch
script_contents = list()
batch_number = batch_number + 1
batch_counter = 0
}
}
}
irods_uri = "irods:/jic_overflow/rg-daniel-zilberman/"
# Generate the script files for the next step (12)
previous_step = "11-dtool_freeze_mc_"
current_step = "12-dtool_copy_mc_"
next_step = "13-dtool_tidy_mc_"
batch_counter = 0
batch_number = 0
# start a new batch
script_contents = list()
for (this_accession in 1:nrow(sample_list)) {
batch_counter = batch_counter + 1
# commit the dtool dataset to iRODS
#script_contents = c(script_contents, paste0("/jic/scratch/groups/Daniel-Zilberman/dtool-slurm-scripts/dtool_copy.slurm.sh ",sample_list[this_accession,"SRA.Accession"], " ", irods_uri, "\n"))
# Commit the dataset to iRODS and capture the URI
script_contents = c(script_contents, paste0("OUTPUT_URI=$(/jic/scratch/groups/Daniel-Zilberman/dtool-slurm-scripts/dtool_copy.slurm.sh ",sample_list[this_accession,"SRA.Accession"], "_meth ", irods_uri, ")\n"))
# Add the current accession and the iRODS URI to a URIs log file for this batch
script_contents = c(script_contents, paste0("echo ", sample_list[this_accession,"SRA.Accession"], "_meth $OUTPUT_URI >>", current_step, batch_number, "_uris.txt\n"))
if ((batch_counter==batch_size) | (this_accession==nrow(sample_list))) {
# finish batch
slurm_header = paste0("#!/bin/bash -e\n#SBATCH -p jic-long # partition (queue)\n#SBATCH --mail-type=END,FAIL # notifications for job done & fail\n#SBATCH [email protected] # send-to address\n#SBATCH --array=\n#SBATCH --mem=16000\n#SBATCH -o ", current_step, batch_number, ".out\n#SBATCH -e ", current_step, batch_number, ".err\n")
# at the start check to ensure flag file exists for previous step, and flag file doesn't exist for current step
script_header = paste0(slurm_header, "until [ -f ", linux_path, previous_step, batch_number, ".txt ]; do\n sleep 60\ndone\nif [ ! -f ", linux_path, current_step, batch_number, ".txt ]; then\ncd ",linux_path,"dtool_datasets\n#iinit\nsource /jic/scratch/groups/Daniel-Zilberman/load_dtool.sh\n")
# at the end make a flag file indicating the step is complete
script_footer = paste0("echo complete>", linux_path, current_step, batch_number, ".txt\necho ", current_step, batch_number, " complete>>", log_file, "\ncd ..\nsbatch ", next_step, batch_number, ".sh\nelse\necho ", current_step, batch_number, " already_complete>>", log_file, "\nfi\n")
script_contents = c(script_header, script_contents, script_footer)
write.table(script_contents, file=paste0(path,current_step,batch_number,".sh"), sep="", col.names=FALSE, row.names=FALSE, quote=FALSE)
launch_script_contents = c(launch_script_contents, "sbatch ", paste0(path,current_step,batch_number,".sh\n"))
if (this_accession<nrow(sample_list)) {
# start new batch
script_contents = list()
batch_number = batch_number + 1
batch_counter = 0
}
}
}
# Generate the script files for the next step (13)
previous_step = "12-dtool_copy_mc_"
current_step = "13-dtool_tidy_mc_"
next_step = ""
batch_counter = 0
batch_number = 0
# start a new batch
script_contents = list()
for (this_accession in 1:nrow(sample_list)) {
batch_counter = batch_counter + 1
# delete the local dtool dataset
script_contents = c(script_contents, paste0("rm -r ", linux_path, "dtool_datasets/", sample_list[this_accession,"SRA.Accession"], "_meth\n"))
if ((batch_counter==batch_size) | (this_accession==nrow(sample_list))) {
# finish batch
slurm_header = paste0("#!/bin/bash -e\n#SBATCH -p jic-short # partition (queue)\n#SBATCH --mail-type=END,FAIL # notifications for job done & fail\n#SBATCH [email protected] # send-to address\n#SBATCH --array=\n#SBATCH --mem=16000\n#SBATCH -o ", current_step, batch_number, ".out\n#SBATCH -e ", current_step, batch_number, ".err\n")
# at the start check to ensure flag file exists for previous step, and flag file doesn't exist for current step
script_header = paste0(slurm_header, "until [ -f ", linux_path, previous_step, batch_number, ".txt ]; do\n sleep 60\ndone\nif [ ! -f ", linux_path, current_step, batch_number, ".txt ]; then\ncd ",linux_path,"dtool_datasets\n")
# at the end make a flag file indicating the step is complete
script_footer = paste0("echo complete>", linux_path, current_step, batch_number, ".txt\necho ", current_step, batch_number, " complete>>", log_file, "\ncd ..\nsbatch ", next_step, batch_number, ".sh\nelse\necho ", current_step, batch_number, " already_complete>>", log_file, "\nfi\n")
script_contents = c(script_header, script_contents, script_footer)
write.table(script_contents, file=paste0(path,current_step,batch_number,".sh"), sep="", col.names=FALSE, row.names=FALSE, quote=FALSE)
launch_script_contents = c(launch_script_contents, "sbatch ", paste0(path,current_step,batch_number,".sh\n"))
if (this_accession<nrow(sample_list)) {
# start new batch
script_contents = list()
batch_number = batch_number + 1
batch_counter = 0
}
}
}
# Step 6a is for retrieving alignments that have already been booked into iRODS
# Generate the script files for the next step (6a)
previous_step = "6-align_"
current_step = "6a-retrieve_alignments_"
next_step = "8a-segmentation_"
batch_counter = 0
batch_number = 0
# start a new batch
script_contents = list()
task_array = list()
for (this_accession in 1:nrow(sample_list)) {
# add the accession to the task array
#task_array = c(task_array, as.character(sample_list[this_accession,"SRA.Accession"]))
# abandoned task arrays as they would involve having the whole batch of alignment files on the SSD at the same time (ran out of space for some batches). Instead, decided to do them serially
batch_counter = batch_counter + 1
# go through the individual sets of reads (runs) for the accession, and check each one for SINGLE or PAIRED status
if ((batch_counter==batch_size) | (this_accession==nrow(sample_list))) {
# finish batch
# make a slurm header with the right number of array entries for the job array
# output the contents of task_array as the ARRAY variable in the script
#slurm_header = paste0("#!/bin/bash -e\n#SBATCH -p jic-long # partition (queue)\n#SBATCH --mail-type=END,FAIL # notifications for job done & fail\n#SBATCH [email protected] # send-to address\n#SBATCH --array=0-",batch_counter - 1,"\n#SBATCH --mem=64000\nARRAY=(", paste(task_array, collapse=" "), ")\n")
# abandoned task arrays as they would involve having the whole batch of alignment files on the SSD at the same time (ran out of space for some batches). Instead, decided to do them serially
# version with no task array
slurm_header = paste0("#!/bin/bash -e\n#SBATCH -p jic-medium # partition (queue)\n#SBATCH --mail-type=END,FAIL # notifications for job done & fail\n#SBATCH [email protected] # send-to address\n#SBATCH --array=\n#SBATCH --mem=64000\n#SBATCH -o ", current_step, batch_number, ".out\n#SBATCH -e ", current_step, batch_number, ".err\n#SBATCH --localscratch=ssd:100 # request 100GB SSD\n")
# at the start check to ensure flag file exists for previous step, and flag file doesn't exist for current step
script_header = paste0(slurm_header, "until [ -f ", linux_path, previous_step, batch_number, ".txt ]; do\n sleep 60\ndone\nif [ ! -f ", linux_path, current_step, batch_number, ".txt ]; then\n")
script_header = paste0(script_header, "BASE_DIR=$SLURM_LOCAL_SCRATCH\n")
#script_header = paste0(script_header, "BASE_DIR=../3-alignments\n")
script_header = paste0(script_header, "export DTOOL_CACHE_DIRECTORY=$BASE_DIR\n")
script_header = paste0(script_header, "#iinit\n")
script_header = paste0(script_header, "source /jic/scratch/groups/Daniel-Zilberman/load_dtool.sh\n")
# add lines at start of script to go through the list of iRODS data sets associated with this batch and retrieve the data files
script_header = paste0(script_header, "input=dtool_datasets/12-dtool_copy_mc_",batch_number,"_uris.txt\n")
script_header = paste0(script_header, "while IFS='\\n' read -r line\n")
script_header = paste0(script_header, "do\n")
script_header = paste0(script_header, "this_dataset=($line)\n")
script_header = paste0(script_header, "#echo ${this_dataset[1]}\n")
script_header = paste0(script_header, "dtool ls ${this_dataset[1]} | while read item; do\n")
script_header = paste0(script_header, "echo $item\n")
script_header = paste0(script_header, "this_item=($item)\n")
script_header = paste0(script_header, "#echo ${this_item[0]}\n")
script_header = paste0(script_header, "#echo ${this_item[1]}\n")
script_header = paste0(script_header, "READ1_ABSPATH=$(dtool item fetch ${this_dataset[1]} ${this_item[0]})\n")
script_header = paste0(script_header, "#echo $READ1_ABSPATH\n")
script_header = paste0(script_header, "mv $READ1_ABSPATH ../3-alignments/${this_item[1]}\n")
script_header = paste0(script_header, "done\n")
script_header = paste0(script_header, "done<\"$input\"\n")
# we now have all the data files for the batch in SSD storage with their original filenames
# at the end make a flag file indicating the step is complete
#script_footer = paste0("echo complete>", linux_path, current_step, batch_number, ".txt\necho ", current_step, batch_number, " complete>>", log_file, "\nsbatch ", next_step, batch_number, ".sh\nelse\necho ", current_step, batch_number, " already_complete>>", log_file, "\nfi\n")
# version which also calls script to retrieve next set in series:
script_footer = paste0("echo complete>", linux_path, current_step, batch_number, ".txt\necho ", current_step, batch_number, " complete>>", log_file, "\nsbatch ", next_step, batch_number, ".sh\nsbatch ", current_step, batch_number+1, ".sh\nelse\necho ", current_step, batch_number, " already_complete>>", log_file, "\nfi\n")
script_contents = c(script_header, script_contents, script_footer)
write.table(script_contents, file=paste0(path,current_step,batch_number,".sh"), sep="", col.names=FALSE, row.names=FALSE, quote=FALSE)
launch_script_contents = c(launch_script_contents, "sbatch ", paste0(path,current_step,batch_number,".sh\n"))
if (this_accession<nrow(sample_list)) {
# start new batch
script_contents = list()
batch_number = batch_number + 1
batch_counter = 0
# start a new task_array
task_array = list()
}
}
}
# 8a is the step for segmenting the methylome
# Generate the script files for the next step (8a)
previous_step = "6a-retrieve_alignments_"
current_step = "8a-segmentation_"
next_step = ""
batch_counter = 0
batch_number = 0
# start a new batch
script_contents = list()
for (this_accession in 1:nrow(sample_list)) {
batch_counter = batch_counter + 1
for (this_run in fastq_list[as.character(fastq_list$experiment_accession) == as.character(sample_list[this_accession,"SRA.Accession"]),"run_accession"]) {
# make a file of the non-cg methylation
#script_contents = c(script_contents, "head -n 1 $BASE_DIR/", this_run, "_", reference_id, ".methratio2 > $BASE_DIR/", this_run, "_", reference_id, ".non-cg.methratio2\n")
# don't need to add header, as header does not contain "CG" in relevant position so gets output anyway. Also changed >> to > in line below
script_contents = c(script_contents, "awk '{if (substr($4,3,2) != \"CG\") { print } }' $BASE_DIR/", this_run, "_", reference_id, ".methratio2 > $BASE_DIR/", this_run, "_", reference_id, ".non-cg.methratio2\n")
}
# Call R and pass the sample ID to the call_methylation script
script_contents = c(script_contents, paste0("mkdir ../5-analysis/", sample_list[this_accession,"SRA.Accession"], "\n"))
script_contents = c(script_contents, paste0("R_zg <../Scripts/8a-non_CG_methylation.R --no-save --args --sample=", sample_list[this_accession,"SRA.Accession"], "\n"))
# delete the non-cg methylation files as redundant
for (this_run in fastq_list[as.character(fastq_list$experiment_accession) == as.character(sample_list[this_accession,"SRA.Accession"]),"run_accession"]) {
script_contents = c(script_contents, "rm $BASE_DIR/", this_run, "_", reference_id, ".non-cg.methratio2\n")
}
if ((batch_counter==batch_size) | (this_accession==nrow(sample_list))) {
# finish batch
slurm_header = paste0("#!/bin/bash -e\n#SBATCH -p jic-medium # partition (queue)\n#SBATCH --mail-type=END,FAIL # notifications for job done & fail\n#SBATCH [email protected] # send-to address\n#SBATCH --array=\n#SBATCH --mem=64000\n#SBATCH -o ", current_step, batch_number, ".out\n#SBATCH -e ", current_step, batch_number, ".err\n")
# at the start check to ensure flag file exists for previous step, and flag file doesn't exist for current step
script_header = paste0(slurm_header, "until [ -f ", linux_path, previous_step, batch_number, ".txt ]; do\n sleep 60\ndone\nif [ ! -f ", linux_path, current_step, batch_number, ".txt ]; then\n#cd ",linux_path,"../5-analysis\nsource R_zg-1.0.2\n")
script_header = paste0(script_header, "BASE_DIR=../3-alignments\n")
# at the end make a flag file indicating the step is complete
#script_footer = paste0("echo complete>", linux_path, current_step, batch_number, ".txt\necho ", current_step, batch_number, " complete>>", log_file, "\nsbatch ", next_step, batch_number, ".sh\nelse\necho ", current_step, batch_number, " already_complete>>", log_file, "\nfi\n")
# version which does not call next step:
script_footer = paste0("echo complete>", linux_path, current_step, batch_number, ".txt\necho ", current_step, batch_number, " complete>>", log_file, "\nelse\necho ", current_step, batch_number, " already_complete>>", log_file, "\nfi\n")
script_contents = c(script_header, script_contents, script_footer)
write.table(script_contents, file=paste0(path,current_step,batch_number,".sh"), sep="", col.names=FALSE, row.names=FALSE, quote=FALSE)
# launch the next step in the process
#launch_script_contents = c(launch_script_contents, "sbatch ", paste0(path,current_step,batch_number,".sh\n"))
if (this_accession<nrow(sample_list)) {
# start new batch
script_contents = list()
batch_number = batch_number + 1
batch_counter = 0
}
}
}
write.table(launch_script_contents, file=paste0(path,launch_script), sep="", col.names=FALSE, row.names=FALSE, quote=FALSE)
# After scripts have been generated in windows using this script, they need to be converted for use on the cluster. Run these commands at an interactive prompt:
perl -pi -e 's/\r\n/\n/g' 0-wget_batch_*.sh
perl -pi -e 's/\r\n/\n/g' 1-dtool_create_*.sh
perl -pi -e 's/\r\n/\n/g' 2-dtool_readme_*.sh
perl -pi -e 's/\r\n/\n/g' 3-dtool_freeze_*.sh
perl -pi -e 's/\r\n/\n/g' 4-dtool_copy_*.sh
perl -pi -e 's/\r\n/\n/g' 5-dtool_tidy_*.sh
perl -pi -e 's/\r\n/\n/g' 6-align_*.sh
perl -pi -e 's/\r\n/\n/g' 7-call_methylation_*.sh
perl -pi -e 's/\r\n/\n/g' 8-non_CG_methylation_*.sh
perl -pi -e 's/\r\n/\n/g' 9-dtool_create_mc_*.sh
perl -pi -e 's/\r\n/\n/g' 10-dtool_readme_mc_*.sh
perl -pi -e 's/\r\n/\n/g' 11-dtool_freeze_mc_*.sh
perl -pi -e 's/\r\n/\n/g' 12-dtool_copy_mc_*.sh
perl -pi -e 's/\r\n/\n/g' 13-dtool_tidy_mc_*.sh
perl -pi -e 's/\r\n/\n/g' launch_script.sh
perl -pi -e 's/\r\n/\n/g' 6a-retrieve_alignments_*.sh
perl -pi -e 's/\r\n/\n/g' 8a-segmentation_*.sh
# Count SRX datasets in iRODS
dtool ls irods:/jic_overflow/rg-daniel-zilberman|grep SRX|wc -l