forked from iwohlers/lied_egypt_genome
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Snakefile
2648 lines (2385 loc) · 124 KB
/
Snakefile
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
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#kate:syntax python;
#######################################
### Analyzing an Egyptian genome
#######################################
from Bio import SeqIO
import os
import gzip
# Rules to be executed for new assemblies: compute_content_and_assembly_numbers,
# repeatmasker_summary_table_egyptrefv2, align_assemblies_with_mummer_all,
################################################################################
############### Writing some general statistics to file ########################
################################################################################
# Chromosome and scaffold names for later use
CHR_GRCh38 = ["chromosome."+str(x) for x in range(1,23)] \
+ ["chromosome."+str(x) for x in ["MT","X","Y"]]
EGYPTREF_SCAFFOLDS = ["fragScaff_scaffold_"+str(x)+"_pilon" for x in range(0,41)] \
+ ["original_scaffold_"+str(x)+"_pilon" for x in range(41,145)]
EGYPTREFV2_SCAFFOLDS = ["fragScaff_scaffold_"+str(x)+"_pilon" for x in range(0,226)] \
+ ["original_scaffold_"+str(x)+"_pilon" for x in range(226,1728)]
# Note: The EGYPTREFV2_SCAFFOLDS for which no repeats have been detected are:
# original_scaffold_{1078,499,447,1298,778,956,1014,583,471,1349,303,1632,1186,1643,399,535,1662,1067,1724,1572,1701,985,719,1711,1101,318,1122,731}_pilon
CEGYPT_CONTIGS = ["Contig"+str(x) for x in range(0,360)]
# Read in contig names from pre-generated file which reads in the fasta headers
CEGYPTV2_CONTIGS = []
with open("data/file.contigsetv2.seqnames.txt","r") as f_in:
for line in f_in:
# Remove ">" at start and "|arrow" at end
CEGYPTV2_CONTIGS.append(line.split("|")[0][1:])
CHR_YORUBA = [x for x in CHR_GRCh38 if not x in ["chromosome.MT","chromosome.Y"]]
YORUBA_SCAFFOLDS = []
if os.path.exists("seq_YORUBA/yoruba_scaffold_to_genbank.txt"):
with open("seq_YORUBA/yoruba_scaffold_to_genbank.txt") as f_in:
for line in f_in:
s = line.split("\t")
if not "HS_" in line:
YORUBA_SCAFFOLDS.append("chromosome."+s[0])
else:
YORUBA_SCAFFOLDS.append(s[0])
AK1_SCAFFOLDS = []
if os.path.exists("seq_AK1/ak1_scaffold_to_genbank.txt"):
with open("seq_AK1/ak1_scaffold_to_genbank.txt") as f_in:
for line in f_in:
AK1_SCAFFOLDS.append(line.split("\t")[0])
# For plotting etc. we sometimes want the longest SCAFFOLDS, since these are not
# named according to size, here are the longest ones
LONGEST_AK1_SCAFFOLDS = [
"Scaffold0147","Scaffold0001","Scaffold00019","Scaffold0008", \
"Scaffold0151","Scaffold0148","Scaffold00033","Scaffold0002", \
"Scaffold00022","Scaffold0152","Scaffold0150","Scaffold00034", \
"Scaffold00063","Scaffold00068","Scaffold0007","Scaffold00025", \
"Scaffold00066","Scaffold00032","Scaffold0010","Scaffold0011", \
"Scaffold00030_pilon","Scaffold00012","Scaffold0009","Scaffold00011", \
"Scaffold00067","Scaffold00027","Scaffold0142","Scaffold0012", \
"Scaffold0056","Scaffold0013"
]
LONGEST_EGYPTREFV2_SCAFFOLDS = ["fragScaff_scaffold_"+str(x)+"_pilon" for x in \
[100,170,6,123,149,184,89,195,205,163,201,76,155,29,68,137,80,61,154,147, \
116,212,196,158,9,26,186,194,98]] + ["original_scaffold_1041_pilon"]
EGYPTREFWTDBG2_SCAFFOLDS = ["ctg"+str(x) for x in range(1,3338)]
# Note: the six EGYPTREFWTDBG2_SCAFFOLFDS for which no repeats have been
# detected are ctg1293, ctg1878, ctg2618, ctg3116, ctg2493, ctg3293
LONGEST_EGYPTREFWTDBG2_SCAFFOLDS = ["ctg"+str(x) for x in range(1,31)]
EGYPTREFMETAV2ADDED_SCAFFOLDS = ["ctg"+str(x) for x in range(1,3338)]
# I made a list of added contigs using this command:
# cat seq_EGYPTREFMETAV2ADDED/Homo_sapiens.EGYPTREFMETAV2ADDED.dna.primary_assembly.fa
# | grep '>' | grep -v ctg | cut -c 2- > seq_EGYPTREFMETAV2ADDED/v2_added_contigs.txt
if os.path.exists("seq_EGYPTREFMETAV2ADDED/v2_added_contigs.txt"):
with open("seq_EGYPTREFMETAV2ADDED/v2_added_contigs.txt") as f_in:
for line in f_in:
EGYPTREFMETAV2ADDED_SCAFFOLDS.append(line.strip("\n"))
################################################################################
######### Preprocessing of the first Novogene assembly called EGYPTREF #########
################################################################################
# Writing the scaffolds of the Egyptian genome to separate fasta files because
# processing the whole assembly often takes too much time
rule write_scaffold_fastas:
input: "data/pilon.fasta"
output: expand("seq_EGYPTREF/Homo_sapiens.EGYPTREF.dna.{scaffold}.fa", \
scaffold=EGYPTREF_SCAFFOLDS)
run:
with open(input[0], "r") as f_in:
i = 0
for record in SeqIO.parse(f_in,"fasta"):
with open(output[i], "w") as f_out:
SeqIO.write(record, f_out, "fasta")
i += 1
# Writing the contigs of the Egyptian genome to separate fasta files because
# processing the whole assembly often takes too much time
rule write_contig_fastas:
input: "data/file.contigset.fasta"
output: expand("seq_CEGYPTREF/Homo_sapiens.CEGYPTREF.dna.{contig}.fa", \
contig=CEGYPT_CONTIGS)
run:
with open(input[0], "r") as f_in:
for record in SeqIO.parse(f_in,"fasta"):
# Remove the trailing "|arrow" since pipe symbols can cause
# problems and the "|arrow" is not needed
record.id = record.id[:-6]
record.name = ''
record.description = ''
record.seq = record.seq.upper()
out_fname = "seq_CEGYPTREF/Homo_sapiens.CEGYPTREF.dna." + \
record.id+".fa"
with open(out_fname, "w") as f_out:
SeqIO.write(record, f_out, "fasta")
# Making a file with all contigs; this is the same as the
# data/file.contigset.fastafile provided by Novogene, but the "|arrow" in the
# sequence names removed
# Note: The original contig file has upper and lower-case letters, don't know
# why! Perhaps repeatmasking was done with them? Anyway, I convert the sequences
# to upper case, because repeatmasking is done later and should be as for the
# other assemblies
rule combine_contigs_to_primary_assembly:
input: expand("seq_CEGYPTREF/Homo_sapiens.CEGYPTREF.dna.{contig}.fa", \
contig=CEGYPT_CONTIGS)
output: "seq_CEGYPTREF/Homo_sapiens.CEGYPTREF.dna.primary_assembly.fa"
shell: "cat {input} > {output}"
# Copy the assembled sequence
rule cp_and_rename_assembly:
input: "data/pilon.fasta"
output: "seq_EGYPTREF/Homo_sapiens.EGYPTREF.dna.primary_assembly.fa"
shell: "cp {input} {output}"
################################################################################
####### Preprocessing of the second Novogene assembly called EGYPTREFV2 ########
################################################################################
# Writing the scaffolds of the Egyptian genome to separate fasta files because
# processing the whole assembly often takes too much time
rule write_scaffold_fastas_v2:
input: "data/pilon_v2.fasta"
output: expand("seq_EGYPTREFV2/Homo_sapiens.EGYPTREFV2.dna.{scaffold}.fa", \
scaffold=EGYPTREFV2_SCAFFOLDS)
run:
with open(input[0], "r") as f_in:
i = 0
for record in SeqIO.parse(f_in,"fasta"):
with open(output[i], "w") as f_out:
SeqIO.write(record, f_out, "fasta")
i += 1
# Writing the contigs of the Egyptian genome to separate fasta files because
# processing the whole assembly often takes too much time
rule write_contig_fastas_v2:
input: "data/file.contigsetv2.fasta"
output: expand("seq_CEGYPTREFV2/Homo_sapiens.CEGYPTREFV2.dna.{contig}.fa", \
contig=CEGYPTV2_CONTIGS)
run:
with open(input[0], "r") as f_in:
for record in SeqIO.parse(f_in,"fasta"):
# Remove the trailing "|arrow" since pipe symbols can cause
# problems and the "|arrow" is not needed
record.id = record.id[:-6]
record.name = ''
record.description = ''
record.seq = record.seq.upper()
out_fname = "seq_CEGYPTREFV2/Homo_sapiens.CEGYPTREFV2.dna." + \
record.id+".fa"
with open(out_fname, "w") as f_out:
SeqIO.write(record, f_out, "fasta")
# Making a file with all contigs; this is the same as the
# data/file.contigsetv2.fastafile provided by Novogene, but the "|arrow" in the
# sequence names removed
# Note: The original contig file has upper and lower-case letters, don't know
# why! Perhaps repeatmasking was done with them? Anyway, I convert the sequences
# to upper case, because repeatmasking is done later and should be as for the
# other assemblies
rule combine_contigs_to_primary_assembly_v2:
input: expand("seq_CEGYPTREFV2/Homo_sapiens.CEGYPTREFV2.dna.{contig}.fa", \
contig=CEGYPTV2_CONTIGS)
output: "seq_CEGYPTREFV2/Homo_sapiens.CEGYPTREFV2.dna.primary_assembly.fa"
shell: "cat {input} > {output}"
# Copy the assembled sequence
rule cp_and_rename_assembly_v2:
input: "data/pilon_v2.fasta"
output: "seq_EGYPTREFV2/Homo_sapiens.EGYPTREFV2.dna.primary_assembly.fa"
shell: "cp {input} {output}"
################################################################################
############### Computing some statistics for all assemblies ###################
################################################################################
# Just getting the header lines of the individual sequences in the fasta
rule scaffold_names:
input: "seq_{assembly}/{fname}.fa"
output: "results/{assembly}/scaffold_names_{fname}.txt"
shell: "cat {input} | grep '>' > {output}"
# Quantifying the sequence content individually for all scaffolds
rule sequence_content:
input: "seq_{assembly}/{fname}.fa"
output: "results/{assembly}/num_bases_{fname}.txt"
script: "scripts/sequence_content.py"
# Quantifying the sequence content over all scaffolds
rule sequence_content_overall:
input: "seq_{assembly}/{fname}.fa"
output: "results/{assembly}/num_all_{fname}.txt"
script: "scripts/sequence_content_overall.py"
# Compute N50 and other related values as statistic for the assembly
rule compute_assembly_stats:
input: "results/{assembly}/num_bases_{fname}.txt"
output: "results/{assembly}/assembly_stats_{fname}.txt"
script: "scripts/compute_assembly_stats.py"
# Computing all info numbers:
# assembly = ["EGYPTREFWTDBG2V4","EGYPTREFWTDBG2V3","EGYPTREFWTDBG2V2","EGYPTREFWTDBG2","GRCh38","EGYPTREF","AK1","YORUBA","CEGYPTREF","EGYPTREFV2","CEGYPTREFV2"], \
rule compute_content_and_assembly_numbers:
input: expand( \
"results/{assembly}/{task}_Homo_sapiens.{assembly}.dna.primary_assembly.txt", \
assembly = ["EGYPTREFWTDBG2V3PILON","EGYPTREFWTDBG2V4","EGYPTREFWTDBG2V3","EGYPTREFWTDBG2V2","EGYPTREFWTDBG2","GRCh38","EGYPTREF","AK1","YORUBA","CEGYPTREF","EGYPTREFV2","CEGYPTREFV2"], \
task = ["scaffold_names","num_bases","num_all","assembly_stats"])
################################################################################
############### Finding mammalian core genes as QC using busco #################
################################################################################
# Downloading the Busco lineage information
rule download_linage:
output: temp("busco_lineage/mammalia_odb9.tar.gz")
shell: "wget -P busco_lineage https://busco.ezlab.org/datasets/mammalia_odb9.tar.gz"
# ... and extracting it; the output files are just two of the many files in this
# archive
rule extract_lineage:
input: "busco_lineage/mammalia_odb9.tar.gz"
output: "busco_lineage/mammalia_odb9/lengths_cutoff",
"busco_lineage/mammalia_odb9/scores_cutoff"
shell: "tar --directory busco_lineage -xvzf {input}"
# Running Busco on a genome file
# --force: Deleting results folder; start new run
# --tmp: Likely /tmp is too small, so make a new tmp folder on scratch (also
# this can be accessed much quicker)
# --blast_single_core: There is a (known!) bug, that blast sometimes fails in
# multi-cpu mode. I also observe this for GRCh38, with exactly the corresponding
# error message; therefore, this is run with a single core.
# Note: According to Busco documentation, 3.1Gbp genome assessment with 12 CPUs
# takes 6 days and 15 hours
# I use a separate environment for busco, because, as of now, its newest version
# cannot be used together with the repeatmasker and installing it together
# would result in downgrading of augustus, blast, boost and busco to older
# versions.
rule run_busco:
input: "busco_lineage/mammalia_odb9/lengths_cutoff",
"seq_{assembly}/Homo_sapiens.{assembly}.dna.{chr_or_type}.fa"
output: "busco_{assembly}/run_busco_{assembly}_{chr_or_type}/short_summary_busco_{assembly}_{chr_or_type}.txt",
"busco_{assembly}/run_busco_{assembly}_{chr_or_type}/full_table_busco_{assembly}_{chr_or_type}.tsv",
threads: 12
conda: "envs/busco.yaml"
shell: "workdir=$PWD; cd /scratch; " + \
"rm -rf /scratch/run_busco_{wildcards.assembly}_{wildcards.chr_or_type}; " + \
"rm -rf /scratch/tmp_busco_{wildcards.assembly}_{wildcards.chr_or_type}; " + \
"mkdir /scratch/tmp_busco_{wildcards.assembly}_{wildcards.chr_or_type}; " + \
"cd /scratch; " + \
"run_busco --in $workdir/{input[1]} " + \
"--out busco_{wildcards.assembly}_{wildcards.chr_or_type} " + \
"--lineage_path $workdir/busco_lineage/mammalia_odb9 " + \
"--mode genome " + \
"--force " + \
"--cpu 12 " + \
# "--blast_single_core " + \
"--tmp /scratch/tmp_busco_{wildcards.assembly}_{wildcards.chr_or_type}; " + \
"rm -rf /scratch/tmp_busco_{wildcards.assembly}_{wildcards.chr_or_type}; " + \
"mkdir -p busco_{wildcards.assembly}; " + \
"cd $workdir; "
"rm -rf busco_{wildcards.assembly}/run_busco_{wildcards.assembly}_{wildcards.chr_or_type}; " + \
"rsync -avz /scratch/run_busco_{wildcards.assembly}_{wildcards.chr_or_type} busco_{wildcards.assembly}/; " + \
"rm -rf /scratch/run_busco_{wildcards.assembly}_{wildcards.chr_or_type}; "
# Running busco on the entire primary assembly...
rule run_busco_primary_assembly:
input: "busco_EGYPTREF/run_busco_EGYPTREF_primary_assembly/short_summary_busco_EGYPTREF_primary_assembly.txt",
"busco_GRCh38/run_busco_GRCh38_primary_assembly/short_summary_busco_GRCh38_primary_assembly.txt"
# ... and running busco chromosome or scaffold-wise
rule run_busco_chromosomewise:
input: expand("busco_EGYPTREF/run_busco_EGYPTREF_{scaffolds}/short_summary_busco_EGYPTREF_{scaffolds}.txt", \
scaffolds=EGYPTREF_SCAFFOLDS),
expand("busco_GRCh38/run_busco_GRCh38_{chrom}/short_summary_busco_GRCh38_{chrom}.txt", \
chrom=CHR_GRCh38)
# Make a comparison table for the busco analysis for EGYPTREF
rule summary_busco_egyptref:
input: expand("busco_EGYPTREF/run_busco_EGYPTREF_{scaffolds}/full_table_busco_EGYPTREF_{scaffolds}.tsv", \
scaffolds=EGYPTREF_SCAFFOLDS)
output: "busco_EGYPTREF/busco_summary.txt"
script: "scripts/busco_summary.py"
# Make a comparison table for the busco analysis for GRCh38
rule summary_busco_grch38:
input: expand("busco_GRCh38/run_busco_GRCh38_{chrom}/full_table_busco_GRCh38_{chrom}.tsv", \
chrom=CHR_GRCh38)
output: "busco_GRCh38/busco_summary.txt"
script: "scripts/busco_summary.py"
################################################################################
###################### Getting reference sequences #############################
################################################################################
# Downloading all GRCh38 sequence data available from Ensembl (release 93,
# but note, that on sequence level, the release shouldn't make a difference)
rule download_GRCh38:
output: "seq_GRCh38/Homo_sapiens.GRCh38.{dna_type}.{chr_or_type}.fa.gz"
run:
# Remove target dir to obtain file name for download
base = output[0].split("/")[1]
shell("wget -P seq_GRCh38 " + \
"ftp://ftp.ensembl.org/pub/release-93/fasta/homo_sapiens/dna/{base}")
# Download README
rule download_GRCh38_readme:
output: "seq_GRCh38/README"
shell: "wget -P seq_GRCh38 " + \
"ftp://ftp.ensembl.org/pub/release-93/fasta/homo_sapiens/dna/README"
# Downloading all GRCh38 sequence files available under the ENSEMBLE release 93
# FTP address
CHR_OR_TYPE = ["chromosome."+str(x) for x in range(1,23)] \
+ ["chromosome."+str(x) for x in ["MT","X","Y"]] \
+ ["nonchromosomal","primary_assembly","toplevel","alt"]
rule download_GRCh38_all:
input: expand("seq_GRCh38/"+ \
"Homo_sapiens.GRCh38.{dna_type}.{chr_or_type}.fa.gz", \
dna_type=["dna","dna_rm","dna_sm"],chr_or_type=CHR_OR_TYPE),
"seq_GRCh38/README"
# Uncompressing fasta files, needed e.g. for Busco analysis
# -d decompress; -k keep archive; -c to stdout
rule uncompress_fasta:
input: "seq_GRCh38/{fname}.fa.gz"
output: "seq_GRCh38/{fname}.fa"
resources: io=1
shell: "gzip -cdk {input} > {output}"
################################################################################
#### Getting the genome assembly (only chromosomes) of a Yoruba individual #####
################################################################################
# Getting the chromosome sequences from Genbank for 1000G individual NA19240,
# which is a Yoruba female
# Getting the assembly report
rule get_yoruba_assembly_report:
output: "seq_YORUBA/GCA_001524155.4_NA19240_prelim_3.0_assembly_report.txt"
shell: "wget -P seq_YORUBA ftp://ftp.ncbi.nlm.nih.gov/genomes/all/GCA/001/524/155/GCA_001524155.4_NA19240_prelim_3.0/GCA_001524155.4_NA19240_prelim_3.0_assembly_report.txt"
# Getting the scaffold names and the corresponding Genbank_ids from the report
rule get_yoruba_scaffold_names:
input: "seq_YORUBA/GCA_001524155.4_NA19240_prelim_3.0_assembly_report.txt"
output: "seq_YORUBA/yoruba_scaffold_to_genbank.txt"
shell: "cat {input} | grep -v '#' | cut -f 1,5 > {output}"
YORUBA_SCAFFOLD_TO_GENBANK = {}
if os.path.exists("seq_YORUBA/yoruba_scaffold_to_genbank.txt"):
with open("seq_YORUBA/yoruba_scaffold_to_genbank.txt") as f_in:
for line in f_in:
s = line.strip("\n").split("\t")
if not "HS_" in line:
YORUBA_SCAFFOLD_TO_GENBANK["chromosome."+s[0]] = s[1]
else:
YORUBA_SCAFFOLD_TO_GENBANK[s[0]] = s[1]
rule get_yoruba:
output: "seq_YORUBA/Homo_sapiens.YORUBA.dna.{id}.fa"
params: genbank_id=lambda wildcards: YORUBA_SCAFFOLD_TO_GENBANK[wildcards.id]
script: "scripts/get_genbank_seqs.py"
rule get_yoruba_all:
input: expand("seq_YORUBA/Homo_sapiens.YORUBA.dna.{scaffold}.fa", \
scaffold=YORUBA_SCAFFOLDS)
rule yoruba_primary_assembly:
input: expand("seq_YORUBA/Homo_sapiens.YORUBA.dna.{scaffolds}.fa", \
scaffolds=YORUBA_SCAFFOLDS)
output: "seq_YORUBA/Homo_sapiens.YORUBA.dna.primary_assembly.fa"
shell: "cat {input} > {output}"
################################################################################
#### Getting the genome assembly (all scaffolds) of a Korean individual ########
################################################################################
# Getting the assembly report
rule get ak1_assembly_report:
output: "seq_AK1/GCA_001750385.2_AK1_v2_assembly_report.txt"
shell: "wget -P seq_AK1 ftp://ftp.ncbi.nlm.nih.gov/genomes/all/GCA/001/750/385/GCA_001750385.2_AK1_v2/GCA_001750385.2_AK1_v2_assembly_report.txt"
# Getting the scaffold names and the corresponding Genbank_ids from the report
rule get_ak1_scaffold_names:
input: "seq_AK1/GCA_001750385.2_AK1_v2_assembly_report.txt"
output: "seq_AK1/ak1_scaffold_to_genbank.txt"
shell: "cat {input} | grep -v '#' | cut -f 1,5 > {output}"
AK1_SCAFFOLD_TO_GENBANK = {}
if os.path.exists("seq_AK1/ak1_scaffold_to_genbank.txt"):
with open("seq_AK1/ak1_scaffold_to_genbank.txt") as f_in:
for line in f_in:
AK1_SCAFFOLD_TO_GENBANK[line.split("\t")[0]]=line.strip().split("\t")[-1]
rule get_ak1:
input: "seq_AK1/ak1_scaffold_to_genbank.txt"
output: "seq_AK1/Homo_sapiens.AK1.dna.{scaffold}.fa"
params: genbank_id=lambda wildcards: AK1_SCAFFOLD_TO_GENBANK[wildcards.scaffold]
script: "scripts/get_genbank_seqs.py"
rule get_ak1_all:
input: expand("seq_AK1/Homo_sapiens.AK1.dna.{scaffold}.fa", \
scaffold=AK1_SCAFFOLDS)
# Construct one file with all AK1 sequences (called "primary_assembly")
rule ak1_primary_assembly:
input: expand("seq_AK1/Homo_sapiens.AK1.dna.{scaffold}.fa", \
scaffold=AK1_SCAFFOLDS)
output: "seq_AK1/Homo_sapiens.AK1.dna.primary_assembly.fa"
shell: "cat {input} > {output}"
################################################################################
######### Getting the genome assembly of EGYPTREF constructed by WTDBG2 ########
################################################################################
rule wtdbg2_primary_assembly:
input: "assembly_wtdbg2/EGYPTREF_wtdbg2.ctg.lay.fa"
output: "seq_EGYPTREFWTDBG2/Homo_sapiens.EGYPTREFWTDBG2.dna.primary_assembly.fa"
shell: "cp {input} {output}"
# Writing the scaffolds of the Egyptian genome to separate fasta files because
# processing the whole assembly often takes too much time
rule write_scaffold_fastas_egyptrefwtdbg2:
input: "seq_EGYPTREFWTDBG2/Homo_sapiens.EGYPTREFWTDBG2.dna.primary_assembly.fa"
output: expand("seq_EGYPTREFWTDBG2/Homo_sapiens.EGYPTREFWTDBG2.dna.{scaffold}.fa", \
scaffold=EGYPTREFWTDBG2_SCAFFOLDS)
run:
with open(input[0], "r") as f_in:
i = 0
for record in SeqIO.parse(f_in,"fasta"):
with open(output[i], "w") as f_out:
SeqIO.write(record, f_out, "fasta")
i += 1
rule wtdbg2_secondary_assembly:
input: "assembly_wtdbg2/EGYPTREF_wtdbg2.ctg.2nd.fa"
output: "seq_EGYPTREFWTDBG2V2/Homo_sapiens.EGYPTREFWTDBG2V2.dna.primary_assembly.fa"
shell: "cp {input} {output}"
# Writing the scaffolds of the Egyptian genome to separate fasta files because
# processing the whole assembly often takes too much time
rule write_scaffold_fastas_egyptrefwtdbg2v2:
input: "seq_EGYPTREFWTDBG2V2/Homo_sapiens.EGYPTREFWTDBG2V2.dna.primary_assembly.fa"
output: expand("seq_EGYPTREFWTDBG2V2/Homo_sapiens.EGYPTREFWTDBG2V2.dna.{scaffold}.fa", \
scaffold=EGYPTREFWTDBG2_SCAFFOLDS)
run:
with open(input[0], "r") as f_in:
i = 0
for record in SeqIO.parse(f_in,"fasta"):
with open(output[i], "w") as f_out:
SeqIO.write(record, f_out, "fasta")
i += 1
rule wtdbg2_shortread_polished_assembly:
input: "assembly_wtdbg2/EGYPTREF_wtdbg2.ctg.3rd.fa"
output: "seq_EGYPTREFWTDBG2V3/Homo_sapiens.EGYPTREFWTDBG2V3.dna.primary_assembly.fa"
shell: "cp {input} {output}"
rule write_scaffold_fastas_egyptrefwtdbg2v3:
input: "seq_EGYPTREFWTDBG2V3/Homo_sapiens.EGYPTREFWTDBG2V3.dna.primary_assembly.fa"
output: expand("seq_EGYPTREFWTDBG2V3/Homo_sapiens.EGYPTREFWTDBG2V3.dna.{scaffold}.fa", \
scaffold=EGYPTREFWTDBG2_SCAFFOLDS)
run:
with open(input[0], "r") as f_in:
i = 0
for record in SeqIO.parse(f_in,"fasta"):
with open(output[i], "w") as f_out:
SeqIO.write(record, f_out, "fasta")
i += 1
# For analyzing the pilon-polished assembly
rule wtdbg2_pilon_polished_assembly:
input: "pilon/pilon.fasta"
output: "seq_EGYPTREFWTDBG2V4/Homo_sapiens.EGYPTREFWTDBG2V4.dna.primary_assembly.fa"
shell: "cp {input} {output}"
rule write_scaffold_fastas_egyptrefwtdbg2v4:
input: "seq_EGYPTREFWTDBG2V4/Homo_sapiens.EGYPTREFWTDBG2V4.dna.primary_assembly.fa"
output: expand("seq_EGYPTREFWTDBG2V4/Homo_sapiens.EGYPTREFWTDBG2V4.dna.{scaffold}.fa", \
scaffold=EGYPTREFWTDBG2_SCAFFOLDS)
run:
with open(input[0], "r") as f_in:
i = 0
for record in SeqIO.parse(f_in,"fasta"):
with open(output[i], "w") as f_out:
SeqIO.write(record, f_out, "fasta")
i += 1
# For analyzing the V3 and subsequently pilon-polished assembly
rule wtdbg2v3pilon_assembly:
input: "polish_pilon_after_wtpoa/pilon.fasta"
output: "seq_EGYPTREFWTDBG2V3PILON/Homo_sapiens.EGYPTREFWTDBG2V3PILON.dna.primary_assembly.fa"
shell: "cp {input} {output}"
rule write_scaffold_fastas_egyptrefwtdbg2v3pilon:
input: "seq_EGYPTREFWTDBG2V3PILON/Homo_sapiens.EGYPTREFWTDBG2V3PILON.dna.primary_assembly.fa"
output: expand("seq_EGYPTREFWTDBG2V3PILON/Homo_sapiens.EGYPTREFWTDBG2V3PILON.dna.{scaffold}.fa", \
scaffold=EGYPTREFWTDBG2_SCAFFOLDS)
run:
with open(input[0], "r") as f_in:
i = 0
for record in SeqIO.parse(f_in,"fasta"):
with open(output[i], "w") as f_out:
SeqIO.write(record, f_out, "fasta")
i += 1
# For analyzing the final meta assembly
rule metav2added_assembly:
input: "meta_assembly/Homo_sapiens.EGYPTREFMETAV2ADDED.dna.primary_assembly.fa"
output: "seq_EGYPTREFMETAV2ADDED/Homo_sapiens.EGYPTREFMETAV2ADDED.dna.primary_assembly.fa"
shell: "cp {input} {output}"
rule write_scaffold_fastas_egyptrefmetav2added:
input: "seq_EGYPTREFMETAV2ADDED/Homo_sapiens.EGYPTREFMETAV2ADDED.dna.primary_assembly.fa"
output: expand("seq_EGYPTREFMETAV2ADDED/Homo_sapiens.EGYPTREFMETAV2ADDED.dna.{scaffold}.fa", \
scaffold=EGYPTREFMETAV2ADDED_SCAFFOLDS)
run:
with open(input[0], "r") as f_in:
i = 0
for record in SeqIO.parse(f_in,"fasta"):
with open(output[i], "w") as f_out:
SeqIO.write(record, f_out, "fasta")
i += 1
################################################################################
######################### Repeat masking with repeatmasker #####################
################################################################################
# Running repeatmasker on the Egyptian genome assembly
# I use a separate environment for repeatmasker, because, as of now, it cannot
# be used together with the newest busco version and installing it together
# would result in downgrading of augustus, blast, boost and busco to older
# versions.
# -s Slow search; 0-5% more sensitive, 2-3 times slower than default
# -q Quick search; 5-10% less sensitive, 2-5 times faster than default
# -qq Rush job; about 10% less sensitive, 4->10 times faster than default
# -html Creates an additional output file in xhtml format
# -gff Creates an additional Gene Feature Finding format output
# Note: Result file
# "repeatmasked_{assembly}/Homo_sapiens.{assembly}.dna.{chr_or_type}.fa.cat.gz"
# is not in the output file list, because depending on the size, either this
# file or the uncompressed file
# "repeatmasked_{assembly}/Homo_sapiens.{assembly}.dna.{chr_or_type}.fa.cat"
# will be generated.
# Temporarily outcommented output files (in case they will also be zipped):
# "repeatmasked_{assembly}/Homo_sapiens.{assembly}.dna.{chr_or_type}.fa.out",
# "repeatmasked_{assembly}/Homo_sapiens.{assembly}.dna.{chr_or_type}.fa.out.gff",
# "repeatmasked_{assembly}/Homo_sapiens.{assembly}.dna.{chr_or_type}.fa.out.html",
rule run_repeatmasker:
input: "seq_{assembly}/Homo_sapiens.{assembly}.dna.{chr_or_type}.fa"
output: "repeatmasked_{assembly}/Homo_sapiens.{assembly}.dna.{chr_or_type}.fa.masked",
"repeatmasked_{assembly}/Homo_sapiens.{assembly}.dna.{chr_or_type}.fa.tbl"
threads: 12
conda: "envs/repeatmasker.yaml"
shell: "RepeatMasker -species human " + \
" -dir repeatmasked_{wildcards.assembly} " + \
" -pa 12 " + \
" -xsmall " + \
" -q " + \
" -html " + \
" -gff {input}; " #+ \
# "workdir=$PWD; cd /scratch; " + \
# "rm -rf /scratch/repeatmasked_{wildcards.assembly}_{wildcards.chr_or_type}; " + \
# "mkdir -p /scratch/repeatmasked_{wildcards.assembly}_{wildcards.chr_or_type}; " + \
# "RepeatMasker -species human " + \
# " -dir /scratch/repeatmasked_{wildcards.assembly}_{wildcards.chr_or_type} " + \
# " -pa 12 " + \
# " -xsmall " + \
# " -q " + \
# " -html " + \
# " -gff $workdir/{input}; " + \
# "cd $workdir; "
# "rsync -avz /scratch/repeatmasked_{wildcards.assembly}_{wildcards.chr_or_type}/ repeatmasked_{wildcards.assembly}/; " + \
# "rm -rf /scratch/repeatmasked_{wildcards.assembly}_{wildcards.chr_or_type}; "
# Running repeatmasker on the primary assembly ...
rule run_repeatmasker_primary_assembly:
input: "repeatmasked_GRCh38/Homo_sapiens.GRCh38.dna.primary_assembly.fa.tbl",
"repeatmasked_EGYPTREF/Homo_sapiens.EGYPTREF.dna.primary_assembly.fa.tbl"
# ... and on the individual scaffolds
# Note: For AK1 and YORUBA, there are the following scaffolds, for which
# repeatmasker does not generate the output files, because "No repetitive
# sequences were detected". For these, I generated the fa.masked file manually
# by copying the input file;
# AK1.Scaffold0873
# AK1.Scaffold1437
# EGYPTREFV2.fragScaff_scaffold_39_pilon
# EGYPTREFV2.original_scaffold_1014_pilon
# EGYPTREFV2.original_scaffold_1067_pilon
# EGYPTREFV2.original_scaffold_1078_pilon
# EGYPTREFV2.original_scaffold_1186_pilon
# EGYPTREFV2.original_scaffold_1349_pilon
# EGYPTREFV2.original_scaffold_1572_pilon
# EGYPTREFV2.original_scaffold_1632_pilon
# EGYPTREFV2.original_scaffold_1643_pilon
# EGYPTREFV2.original_scaffold_1662_pilon
# EGYPTREFV2.original_scaffold_1701_pilon
# EGYPTREFV2.original_scaffold_399_pilon
# EGYPTREFV2.original_scaffold_499_pilon
# EGYPTREFV2.original_scaffold_535_pilon
# EGYPTREFV2.original_scaffold_731_pilon
# EGYPTREFV2.original_scaffold_778_pilon
# EGYPTREFV2.original_scaffold_956_pilon
# EGYPTREFWTDBG2V3PILON.ctg1102
# EGYPTREFWTDBG2V3PILON.ctg1288
# EGYPTREFWTDBG2V3PILON.ctg1391
# EGYPTREFWTDBG2V3PILON.ctg1501
# EGYPTREFWTDBG2V3PILON.ctg1825
# EGYPTREFWTDBG2V3PILON.ctg2771
# EGYPTREFWTDBG2V3PILON.ctg2943
# EGYPTREFWTDBG2V3PILON.ctg3180
# EGYPTREFWTDBG2V3PILON.ctg3319
# YORUBA.HS_LKPB_CHRUN_SCAFFOLD_1014
# YORUBA.HS_LKPB_CHRUN_SCAFFOLD_1147
# YORUBA.HS_LKPB_CHRUN_SCAFFOLD_1638
# YORUBA.HS_LKPB_CHRUN_SCAFFOLD_256
rule run_repeatmasker_chromosomewise:
input: expand("repeatmasked_GRCh38/Homo_sapiens.GRCh38.dna.{x}.fa.tbl", \
x=CHR_GRCh38),
expand("repeatmasked_EGYPTREF/Homo_sapiens.EGYPTREF.dna.{x}.fa.tbl", \
x=EGYPTREF_SCAFFOLDS),
expand("repeatmasked_AK1/Homo_sapiens.AK1.dna.{x}.fa.tbl", \
x=AK1_SCAFFOLDS),
expand("repeatmasked_YORUBA/Homo_sapiens.YORUBA.dna.{x}.fa.tbl", \
x=YORUBA_SCAFFOLDS)
# Summarising the chromosome-wise repeatmasker summary files for Egyptref
rule repeatmasker_summary_table_egyptref:
input: expand("repeatmasked_EGYPTREF/Homo_sapiens.EGYPTREF.dna.{x}.fa.tbl", \
x=EGYPTREF_SCAFFOLDS)
output: "repeatmasked_EGYPTREF/summary.txt"
script: "scripts/repeatmasker_summary.py"
# Summarising the contig-wise repeatmasker summary files for Egyptref
rule repeatmasker_summary_table_cegyptref:
input: expand("repeatmasked_CEGYPTREF/Homo_sapiens.CEGYPTREF.dna.{x}.fa.tbl", \
x=CEGYPT_CONTIGS)
output: "repeatmasked_CEGYPTREF/summary.txt"
script: "scripts/repeatmasker_summary.py"
# Summarising the scaffold-wise repeatmasker summary files for Egyptrefv2
rule repeatmasker_summary_table_egyptrefv2:
input: expand("repeatmasked_EGYPTREFV2/Homo_sapiens.EGYPTREFV2.dna.{x}.fa.tbl", \
x=EGYPTREFV2_SCAFFOLDS)
output: "repeatmasked_EGYPTREFV2/summary.txt"
script: "scripts/repeatmasker_summary.py"
# Summarising the contig-wise repeatmasker summary files for Egyptrefv2
rule repeatmasker_summary_table_cegyptrefv2:
input: expand("repeatmasked_CEGYPTREFV2/Homo_sapiens.CEGYPTREFV2.dna.{x}.fa.tbl", \
x=CEGYPTV2_CONTIGS)
output: "repeatmasked_CEGYPTREFV2/summary.txt"
script: "scripts/repeatmasker_summary.py"
# Summarising the chromosome-wise repeatmasker summary files for GRCh38
rule repeatmasker_summary_table_grch38:
input: expand("repeatmasked_GRCh38/Homo_sapiens.GRCh38.dna.{x}.fa.tbl", \
x=CHR_GRCh38)
output: "repeatmasked_GRCh38/summary.txt"
script: "scripts/repeatmasker_summary.py"
# Summarising the chromosome-wise repeatmasker summary files for AK1
rule repeatmasker_summary_table_ak1:
input: expand("repeatmasked_AK1/Homo_sapiens.AK1.dna.{x}.fa.tbl", \
x=AK1_SCAFFOLDS)
output: "repeatmasked_AK1/summary.txt"
script: "scripts/repeatmasker_summary.py"
# Summarising the chromosome-wise repeatmasker summary files for Yoruba
rule repeatmasker_summary_table_yoruba:
input: expand("repeatmasked_YORUBA/Homo_sapiens.YORUBA.dna.{x}.fa.tbl", \
x=YORUBA_SCAFFOLDS)
output: "repeatmasked_YORUBA/summary.txt"
script: "scripts/repeatmasker_summary.py"
# Summarising the chromosome-wise repeatmasker summary files for Egyptref wtdbg2
rule repeatmasker_summary_table_egyptrefwtdbg2:
input: expand("repeatmasked_EGYPTREFWTDBG2/Homo_sapiens.EGYPTREFWTDBG2.dna.{x}.fa.tbl", \
x=EGYPTREFWTDBG2_SCAFFOLDS)
output: "repeatmasked_EGYPTREFWTDBG2/summary.txt"
script: "scripts/repeatmasker_summary.py"
# Summarising the chromosome-wise repeatmasker summary files for Egyptref wtdbg2
# v3 with additional pilon polishing
rule repeatmasker_summary_table_egyptrefwtdbg2v3pilon:
input: expand("repeatmasked_EGYPTREFWTDBG2V3PILON/Homo_sapiens.EGYPTREFWTDBG2V3PILON.dna.{x}.fa.tbl", \
x=EGYPTREFWTDBG2_SCAFFOLDS)
output: "repeatmasked_EGYPTREFWTDBG2V3PILON/summary.txt"
script: "scripts/repeatmasker_summary.py"
# Summarising the chromosome-wise repeatmasker summary files for Egyptref meta
# assembly with egyptrefv2 sequence added
rule repeatmasker_summary_table_egyptrefmetav2added:
input: expand("repeatmasked_EGYPTREFMETAV2ADDED/Homo_sapiens.EGYPTREFMETAV2ADDED.dna.{x}.fa.tbl", \
x=EGYPTREFMETAV2ADDED_SCAFFOLDS)
output: "repeatmasked_EGYPTREFMETAV2ADDED/summary.txt"
script: "scripts/repeatmasker_summary.py"
# Making a repeatmasker stat table over all chromosomes, one line for EGYPTREF,
# one line for GRCh38
# assembly=["EGYPTREFWTDBG2V2","EGYPTREFWTDBG2","EGYPTREF","EGYPTREFV2","CEGYPTREF","CEGYPTREFV2","AK1","YORUBA","GRCh38"]
rule comparison_repeatmasker:
input: expand("repeatmasked_{assembly}/summary.txt", \
assembly=["EGYPTREFMETAV2ADDED","EGYPTREFWTDBG2V3PILON","EGYPTREFV2","AK1","YORUBA","GRCh38"])
output: "results/repeatmasker_comparison.txt"
script: "scripts/repeatmasker_comparison.py"
################################################################################
########### Reference to assembly genome alignment with lastz ##################
################################################################################
# Computing genome alignments using lastz
# [unmask] Attaching this to the chromosome filename instructs lastz to ignore
# masking information and treat repeats the same as any other part of the
# chromosome -> We do NOT want this, alignments will be crappy with it!!
# Parameters used for quick and dirty, alignment (lastz manual), taking minutes
# --notransition Don't allow any match positions in seeds to be satisified by
# transitions (lowers seeding sensitivity and reduces runtime)
# --nogapped Eliminates the computation of gapped alignments
# --step 20 Lowers seeding senitivity reducing runtime and memory (factor 3.3)
# Parameters from the Korean reference genome AK1 (Seo et al. 2016)
# --gapped Perform gapped extension of HSPs after first reducing them to anchor
# points
# --gap=600,150 Gap open and gap extension penalty
# --hspthresh=4500 Set the score threshold for the x-drop extension method; HSPs
# scoring lower are discarded.
# --seed 12of19 Seeds require a 19bp word with matches in 12 specific positions
# --notransition Don't allow any match positions in seeds to be satisified by
# transitions
# --ydrop=15000 Set the threshold for terminating gapped extension; this
# restricts the endpoints of each local alignment by
# limiting the local region around each anchor in which
# extension is performed
# --chain Perform chaining of HSPs with no penalties
# Parameters from another Korean reference genome, KOREF (Cho et al. 2016)
# --step 19 Offset between the starting positins of successive target words
# considered for potential seeds
# --hspthresh 3000 Set the score threshold for the x-drop extension method; HSPs
# scoring lower are discared.
# --gappedthresh 3000 Set the threshold for gapped extension; alignments scoring
# lower than score are discarded.
# --seed 12of19 Seeds require a 19bp word with matches in 12 specific positions
# --minScore 3000 ? kenttools?
# --linearGap medium ? kenttools?
rule align_with_lastz:
input: "repeatmasked_GRCh38/Homo_sapiens.GRCh38.dna.{chr}.fa.masked",
"repeatmasked_{assembly}/Homo_sapiens.{assembly}.dna.{scaffold}.fa.masked"
output: "align_lastz_GRCh38_vs_{assembly}/{chr}_vs_{scaffold}.maf",
"align_lastz_GRCh38_vs_{assembly}/dotplots/{chr}_vs_{scaffold}.rdotplot"
conda: "envs/lastz.yaml"
shell: "lastz {input[0]} {input[1]} " + \
"--gapped " + \
"--gap=600,150 " + \
"--hspthresh=4500 " + \
"--seed=12of19 " + \
"--notransition " + \
"--ydrop=15000 " + \
"--chain " + \
"--format=maf " + \
"--rdotplot={output[1]} " + \
">{output[0]}"
# Plot the dotplot output of lastz
#rule individual_lastz_dotplot:
# input: "align_lastz_GRCh38_vs_EGYPTREF/dotplots/{chr}_vs_{scaffold}.rdotplot"
# output: "align_lastz_GRCh38_vs_EGYPTREF/dotplots/{chr}_vs_{scaffold}.pdf"
# script: "scripts/dotplot.R"
# Plotting for one scaffold the dotplot versus all chromosomes
rule dotplots_scaffold_vs_chromosomes:
input: "results/GRCh38/num_bases_Homo_sapiens.GRCh38.dna.primary_assembly.txt",
"results/{assembly}/num_bases_Homo_sapiens.{assembly}.dna.primary_assembly.txt",
expand("align_lastz_GRCh38_vs_{{assembly}}/dotplots/{chr}_vs_{{scaffold}}.rdotplot", \
chr=CHR_GRCh38)
output: "align_lastz_GRCh38_vs_{assembly}/dotplots/{scaffold}.pdf"
script: "scripts/scaffold_vs_grch38.R"
# Plotting the dotplots for all scaffolds
rule dotplots_scaffold_vs_chromosomes_all:
input: expand("align_lastz_GRCh38_vs_EGYPTREF/dotplots/{scaffold}.pdf", \
scaffold=EGYPTREF_SCAFFOLDS),
expand("align_lastz_GRCh38_vs_YORUBA/dotplots/{scaffold}.pdf", \
scaffold=YORUBA_SCAFFOLDS[:23]),
expand("align_lastz_GRCh38_vs_CEGYPTREF/dotplots/{contig}.pdf", \
contig=CEGYPT_CONTIGS),
expand("align_lastz_GRCh38_vs_AK1/dotplots/{scaffold}.pdf", \
scaffold=LONGEST_AK1_SCAFFOLDS),
expand("align_lastz_GRCh38_vs_EGYPTREFV2/dotplots/{scaffold}.pdf", \
scaffold=LONGEST_EGYPTREFV2_SCAFFOLDS),
expand("align_lastz_GRCh38_vs_CEGYPTREFV2/dotplots/{contig}.pdf", \
contig=CEGYPTV2_CONTIGS[:50]),
expand("align_lastz_GRCh38_vs_EGYPTREFWTDBG2/dotplots/{scaffold}.pdf", \
scaffold=LONGEST_EGYPTREFWTDBG2_SCAFFOLDS)
# All versus all comparisons of reference and Egyptian genome
rule align_all_vs_all:
input: expand("align_lastz_GRCh38_vs_EGYPTREF/{chr}_vs_{scaffold}.maf", \
chr=CHR_GRCh38, scaffold=EGYPTREF_SCAFFOLDS)
# Computing the GRCh38 recovery rate using the mafTools package
# (as in Cho et al.). Using mafTools program mafPairCoverage, it is necessary
# to first combine all scaffold maf files for a chromosome, and then run
# mafTransitiveClosure
rule combine_maf_files_for_recovery:
input: expand("align_lastz_GRCh38_vs_EGYPTREF/{{chr}}_vs_{scaffold}.maf", \
scaffold=EGYPTREF_SCAFFOLDS)
output: "align_lastz_GRCh38_vs_EGYPTREF/recovery/{chr}_alignments.maf"
run:
shell("cat {input[0]} > {output}")
for filename in input[1:]:
# Append to large file; some file only have comments, no alignments
# therefore we need to add & true because other wise the exit code
# would indicate an error
shell("cat {filename} | grep -v '#' >> {output} & true")
rule transitive_closure:
input: "align_lastz_GRCh38_vs_EGYPTREF/recovery/{chr}_alignments.maf"
output: "align_lastz_GRCh38_vs_EGYPTREF/recovery/{chr}.transclos"
params: chr_number=lambda wildcards: wildcards.chr.split(".")[1]
shell: "./ext_tools/mafTools/bin/mafTransitiveClosure " + \
"--maf {input} > {output}"
rule maftools_coverage:
input: "align_lastz_GRCh38_vs_EGYPTREF/recovery/{chr}.transclos"
output: "align_lastz_GRCh38_vs_EGYPTREF/recovery/{chr}.coverage"
params: chr_number=lambda wildcards: wildcards.chr.split(".")[1]
shell: "./ext_tools/mafTools/bin/mafPairCoverage " + \
"--maf {input} --seq1 {params.chr_number} --seq2 \* > {output}"
rule recovery:
input: expand("align_lastz_GRCh38_vs_EGYPTREF/recovery/{chr}.coverage", \
chr=CHR_GRCh38)
output: "align_lastz_GRCh38_vs_EGYPTREF/recovery/recovery.txt"
run:
pass
################################################################################
########### Reference to assembly genome alignment with mummer #################
################################################################################
# Genome alignments using mummer4
rule align_with_mummer:
input: "repeatmasked_GRCh38/Homo_sapiens.GRCh38.dna.{chr}.fa.masked",
"repeatmasked_EGYPTREF/Homo_sapiens.EGYPTREF.dna.{scaffold}.fa.masked"
output: "align_mummer_GRCh38_vs_EGYPTREF/{chr}_vs_{scaffold}.delta"
conda: "envs/mummer.yaml"
shell: "nucmer " + \
"-p align_mummer_GRCh38_vs_EGYPTREF/{wildcards.chr}_vs_{wildcards.scaffold} " + \
"{input[0]} {input[1]}"
rule plot_mummer:
input: "align_mummer_GRCh38_vs_EGYPTREF/{chr}_vs_{scaffold}.filter"
output: "align_mummer_GRCh38_vs_EGYPTREF/dotplots/{chr}_vs_{scaffold}.gp",
"align_mummer_GRCh38_vs_EGYPTREF/dotplots/{chr}_vs_{scaffold}.rplot",
"align_mummer_GRCh38_vs_EGYPTREF/dotplots/{chr}_vs_{scaffold}.fplot",
"align_mummer_GRCh38_vs_EGYPTREF/dotplots/{chr}_vs_{scaffold}.ps"
conda: "envs/mummer.yaml"
shell: "mummerplot " + \
"--postscript " + \
"-p align_mummer_GRCh38_vs_EGYPTREF/dotplots/{wildcards.chr}_vs_{wildcards.scaffold} " + \
"{input[0]}; " + \
"gnuplot {output[0]}"
# All versus all comparisons of reference and Egyptian genome
rule align_all_vs_all_mummer:
input: expand("align_mummer_GRCh38_vs_EGYPTREF/{chr}_vs_{scaffold}.delta", \
chr=CHR_GRCh38, scaffold=EGYPTREF_SCAFFOLDS)
# Comparing the entire GRCh38 assembly with the entire EGYPTREF assembly
# --mum: Use anchor matches that are unique in both the reference and
# query (false)
# --threads=NUM: Use NUM threads (# of cores)
# Path to conda environment:
# /data/lied_egypt_genome/lied_egypt_genome/.snakemake/conda/5fab0d7a
rule align_assemblies_with_mummer:
input: ref="seq_{a1}/Homo_sapiens.{a1}.dna.primary_assembly.fa",
query="seq_{a2}/Homo_sapiens.{a2}.dna.primary_assembly.fa"
output: "align_mummer_{a1}_vs_{a2}/assemblies/{a1}_vs_{a2}.delta"
conda: "envs/mummer.yaml"
shell: "nucmer " + \
"--mum " + \
"--threads=24 "
"-p align_mummer_{wildcards.a1}_vs_{wildcards.a2}/assemblies/{wildcards.a1}_vs_{wildcards.a2} " + \
"{input[0]} {input[1]}"
rule align_assemblies_with_mummer_all:
input: expand("align_mummer_GRCh38_vs_{a}/assemblies/GRCh38_vs_{a}.delta", \
a=["EGYPTREF","CEGYPTREF","EGYPTREFV2","AK1","YORUBA","GRCh38"])
# All versus all dotplots of reference and Egyptian genome
rule all_vs_all_dotplots_mummer:
input: expand("align_mummer_GRCh38_vs_EGYPTREF/dotplots/{chr}_vs_{scaffold}.gp", \
chr=CHR_GRCh38, scaffold=EGYPTREF_SCAFFOLDS)
# Plotting the dotplots for all scaffolds
rule mummer_dotplots_scaffold_vs_chromosomes_all:
input: expand("align_lastz_GRCh38_vs_EGYPTREF/dotplots/{scaffold}.pdf", \
scaffold=EGYPTREF_SCAFFOLDS)
# Filtering the mummer alignments: Query sequences can be mapped to reference
# sequences with -q, this allows the user to exclude chance and repeat
# alignments, leaving only the best alignments between the two data sets (i.e.
# use the -q option for mapping query contigs to their best reference location)
# -u: float; Set the minimum alignment uniqueness, i.e. percent of the alignment
# matching to unique reference AND query sequence [0, 100], default 0
# -l: int; Set the minimum alignment length, default 0
# -i: float; Set the minimum alignment identity [0, 100], default 0
# -1: 1-to-1 alignment allowing for rearrangements
# -r: Maps each position of each reference to its best hit in the query,
# allowing for query overlaps (intersection of -r and -q alignments)
rule delta_filter_mummer:
input: "align_mummer_GRCh38_vs_EGYPTREF/{chr}_vs_{scaffold}.delta"
output: "align_mummer_GRCh38_vs_EGYPTREF/{chr}_vs_{scaffold}.filter"
conda: "envs/mummer.yaml"
shell: "delta-filter -l 10000 -u 0 -q {input} > {output}"
# Running the tool nucdiff to compare two assemblies based on alignment with
# mummer, which is also performed by the nucdiff tool
rule run_nucdiff:
input: ref="seq_{a1}/Homo_sapiens.{a1}.dna.{chr}.fa", \
query="seq_{a2}/Homo_sapiens.{a2}.dna.primary_assembly.fa"
output: "nucdiff_{a1}_vs_{a2}/results/{a1}_vs_{a2}_{chr}_ref_snps.gff", \
"nucdiff_{a1}_vs_{a2}/results/{a1}_vs_{a2}_{chr}_ref_struct.gff", \
"nucdiff_{a1}_vs_{a2}/results/{a1}_vs_{a2}_{chr}_ref_blocks.gff", \
"nucdiff_{a1}_vs_{a2}/results/{a1}_vs_{a2}_{chr}_ref_snps.vcf", \
"nucdiff_{a1}_vs_{a2}/results/{a1}_vs_{a2}_{chr}_query_snps.gff", \
"nucdiff_{a1}_vs_{a2}/results/{a1}_vs_{a2}_{chr}_query_struct.gff", \
"nucdiff_{a1}_vs_{a2}/results/{a1}_vs_{a2}_{chr}_query_blocks.gff", \
"nucdiff_{a1}_vs_{a2}/results/{a1}_vs_{a2}_{chr}_query_snps.vcf", \
"nucdiff_{a1}_vs_{a2}/results/{a1}_vs_{a2}_{chr}_stat.out"
params: outdir=lambda wildcards: "nucdiff_"+wildcards.a1+"_vs_"+wildcards.a2
conda: "envs/nucdiff.yaml"
shell: "nucdiff {input.ref} {input.query} {params.outdir} " + \
"{wildcards.a1}_vs_{wildcards.a2}_{wildcards.chr} " + \
"--vcf yes " + \
"--filter_opt '-l 1000 -i 99' "
"--proc 24"
rule run_nucdiff_all:
input: expand("nucdiff_GRCh38_vs_EGYPTREF/results/GRCh38_vs_EGYPTREF_{chr}_stat.out", \
chr=CHR_GRCh38)
rule run_nucdiff_for_assembly:
input: ref="seq_{a1}/Homo_sapiens.{a1}.dna.primary_assembly.fa", \
query="seq_{a2}/Homo_sapiens.{a2}.dna.primary_assembly.fa", \
delta="align_mummer_{a1}_vs_{a2}/assemblies/{a1}_vs_{a2}.delta"
output: "nucdiff_{a1}_vs_{a2}/assemblies/results/{a1}_vs_{a2}_ref_snps.gff", \
"nucdiff_{a1}_vs_{a2}/assemblies/results/{a1}_vs_{a2}_ref_struct.gff", \
"nucdiff_{a1}_vs_{a2}/assemblies/results/{a1}_vs_{a2}_ref_blocks.gff", \
"nucdiff_{a1}_vs_{a2}/assemblies/results/{a1}_vs_{a2}_ref_snps.vcf", \
"nucdiff_{a1}_vs_{a2}/assemblies/results/{a1}_vs_{a2}_query_snps.gff", \
"nucdiff_{a1}_vs_{a2}/assemblies/results/{a1}_vs_{a2}_query_struct.gff", \
"nucdiff_{a1}_vs_{a2}/assemblies/results/{a1}_vs_{a2}_query_blocks.gff", \
"nucdiff_{a1}_vs_{a2}/assemblies/results/{a1}_vs_{a2}_query_snps.vcf", \
"nucdiff_{a1}_vs_{a2}/assemblies/results/{a1}_vs_{a2}_stat.out"
params: outdir=lambda wildcards: "nucdiff_"+wildcards.a1+"_vs_"+wildcards.a2
conda: "envs/nucdiff.yaml"