-
Notifications
You must be signed in to change notification settings - Fork 0
/
Snakefile
1179 lines (1062 loc) · 37.5 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
configfile: "config.json"
mel_release = "r5.57_FB2014_03"
sim_release = "r2.01_FB2016_01"
sec_release = "r1.3_FB2015_01"
mel_version, mel_date = mel_release.split('_', 1)
sim_version, sim_date = sim_release.split('_', 1)
sec_version, sec_date = sec_release.split('_', 1)
num_mel_windows = 17
dates = {'mel': mel_date, 'sim': sim_date, 'sec': sec_date}
versions = {'mel': mel_version, 'sim': sim_version, 'sec': sec_version}
module = '''module () { eval `$LMOD_CMD bash "$@"` }'''
preconda = '''
export CONDA_PATH_BACKUP=""
export PS1=""
'''
from os import path
def getreads(readnum):
if readnum == 0:
formatstr = 'sequence/{}.fastq.gz'
else:
formatstr = 'sequence/{{}}_{}.fastq.gz'.format(readnum)
def retfun(wildcards):
return [ancient(formatstr.format(srr))
for srr in config['samples'][path.basename(wildcards.sample)]]
return retfun
def getreads_nowc(readnum):
if readnum == 0:
formatstr = 'sequence/{}.fastq.gz'
else:
formatstr = 'sequence/{{}}_{}.fastq.gz'.format(readnum)
def retfun(sample):
return [formatstr.format(srr)
for srr in config['samples'][path.basename(sample)]]
return retfun
def getreadscomma(readnum):
if readnum == 0:
formatstr = 'sequence/{}.fastq.gz'
else:
formatstr = 'sequence/{{}}_{}.fastq.gz'.format(readnum)
def retfun(wildcards):
return ",".join([formatstr.format(srr)
for srr in config['samples'][path.basename(wildcards.sample)]])
return retfun
def interleave_reads(wildcards):
retval = " ".join(" ".join([a, b])
for a, b
in zip(
getreads_nowc(1)(path.basename(wildcards.sample)),
getreads_nowc(2)(path.basename(wildcards.sample))
)
)
return retval
rule all:
input:
expand('analysis/{target}/figure-specificity/oefemale.svg',
target=['mel', 'sim', 'sec'])
rule prepare_emase:
input:
ancient("Reference/{species}/"),
fasta="prereqs/d{species}.fasta",
gtf="Reference/{species}_good_exonids.gtf"
output:
"Reference/{species}/emase.transcripts.fa",
"Reference/{species}/emase.transcripts.info",
"Reference/{species}/emase.gene2transcripts.tsv",
shell: """
source activate emase
prepare-emase -G {input.fasta} -g {input.gtf} -o Reference/{wildcards.species}/ -m --no-bowtie-index
"""
rule bam_to_emase:
input:
bam="analysis/{genome}/{sample}/mapped.bam",
transcriptome_info="Reference/{genome}/emase.transcripts.info",
output:
"analysis/{genome}/{sample}/aligned.transcriptome.h5"
shell:"""
source activate emase
bam-to-emase -a {input.bam} \
-i {input.transcriptome_info} \
-s sim,sec \
-o {output}
"""
rule run_emase:
input:
h5 = "analysis/{target}/{sample}/aligned.transcriptome.h5",
transcripts = "Reference/{target}/emase.gene2transcripts.tsv",
transcript_info = "Reference/{target}/emase.pooled.transcripts.info",
output:
"analysis/{target}/{sample}/emase.isoforms.effective_read_counts",
"analysis/{target}/{sample}/emase.isoforms.tpm",
"analysis/{target}/{sample}/emase.isoforms.alignment_counts",
"analysis/{target}/{sample}/emase.genes.effective_read_counts",
"analysis/{target}/{sample}/emase.genes.tpm",
"analysis/{target}/{sample}/emase.genes.alignment_counts",
shell: """ source activate emase
run-emase -i {input.h5} \
-g {input.transcripts} \
-L {input.transcript_info} \
-M 4 \
-o analysis/{wildcards.target}/{wildcards.sample}/emase \
-r 101 \
-c
"""
rule block_gzip:
input:
vcf="{file}.gvcf"
output:
gz="{file}.gvcf.gz",
gzi="{file}.gvcf.gz.gzi",
tbi="{file}.gvcf.gz.tbi",
shell:""" source activate emase
bgzip --index --stdout {input.vcf} > {output.gz}
tabix -p vcf -f {output.gz}
"""
rule add_exonids:
input: gtf="{file}.gtf", code="AddExonIDs.py"
output: "{file}_exonids.gtf"
shell: "python AddExonIDs.py {input.gtf} {output}"
rule vcf_to_genome:
input:
vcf_indels="Reference/{genome}/simsec_variants_on_{genome}_indels.gvcf.gz",
vcf_snps="Reference/{genome}/simsec_variants_on_{genome}_snps.gvcf.gz",
reffasta="Reference/d{genome}.fa",
gtf="Reference/{genome}_good_exonids.gtf",
output:
chain="Reference/{genome}/ref-to-{target}.chain",
patched="Reference/{genome}/{target}.patched.fa",
transformed="Reference/{genome}/{target}.fa",
gtf="Reference/{genome}/{target}.gtf",
gtfdb="Reference/{genome}/{target}.gtf.db",
shell:"""
source activate emase
g2gtools vcf2chain --pass -f {input.reffasta} -i {input.vcf_indels} -s {wildcards.target}_gdna -o {output.chain}
g2gtools patch -i {input.reffasta} -s {wildcards.target}_gdna -v {input.vcf_snps} -o {output.patched}
g2gtools transform -i {output.patched} -c {output.chain} -o {output.transformed}
g2gtools convert -c {output.chain} -i {input.gtf} -f gtf -o {output.gtf}
g2gtools gtf2db -i {output.gtf} -o {output.gtfdb}
"""
rule build_transcriptome:
input:
sim="Reference/{target}/sim.fa",
sec="Reference/{target}/sec.fa",
output:
"Reference/{target}/emase.pooled.transcripts.fa",
"Reference/{target}/emase.pooled.transcripts.info",
shell:""" source activate emase
prepare-emase \
-G {input.sim},{input.sec} \
-s sim,sec -o Reference/{wildcards.target} \
--no-bowtie-index
"""
ruleorder: fbtr_transcriptome > tophat_transcriptome > build_transcriptome > prepare_emase > vcf_to_genome
rule orig_seq_star_ref:
input:
fasta="Reference/{target}/simsec_corrected.fasta",
gtf="Reference/{target}_good.gtf",
output:
outdir="Reference/{target}/orig",
outfile="Reference/{target}/orig/Genome"
priority: 50
shell:""" {module}; module load STAR
rm -rf {output.outdir}
mkdir -p {output.outdir}
STAR --runMode genomeGenerate --genomeDir {output.outdir} \
--runThreadN 12 \
--limitGenomeGenerateRAM 48705389440 \
--outTmpDir {output.outdir}/_tmp/ \
--sjdbGTFfile {input.gtf} \
--genomeFastaFiles {input.fasta}
"""
# I don't actually need to mask for the WASP pipeline, but it's safer to use
# the program that I know already works to generate the bed file.
rule mask_and_make_bed:
input:
var_tab='Reference/{target}/simsec_variants.tsv',
ref_fasta="prereqs/d{target}.fasta",
output:
fasta="Reference/{target}/simsec_masked.fasta",
corr_fasta="Reference/{target}/simsec_corrected.fasta",
bed="Reference/{target}/simsec_variant.bed",
shell: """
{preconda}
source activate peter
python MaskReferenceFromGATKTable.py \
--target-species sim \
--emit-bed {output.bed} \
--emit-corrected {output.corr_fasta} \
--outfasta {output.fasta} \
{input.ref_fasta} \
{input.var_tab}
"""
rule diploid_star_ref:
input:
fasta="Reference/{target}/emase.pooled.transcripts.fa",
gtf="Reference/{target}_good.gtf",
output:
outdir="Reference/{target}/emase.pooled",
outfile="Reference/{target}/emase.pooled/Genome"
priority: 50
shell:""" {module}; module load STAR
rm -rf {output.outdir}
mkdir -p {output.outdir}
STAR --runMode genomeGenerate --genomeDir {output.outdir} \
--runThreadN 12 \
--limitGenomeGenerateRAM 48705389440 \
--outTmpDir {output.outdir}/_tmp/ \
--genomeFastaFiles {input.fasta}
"""
rule star_map_orig:
input:
unpack(getreads(1)),
unpack(getreads(2)),
genome="Reference/{target}/orig/Genome",
genomedir="Reference/{target}/orig/",
params:
r1s=getreadscomma(1),
r2s=getreadscomma(2),
output: "analysis/{target}/{sample}/orig_mapped.bam"
threads: 16
shell: """{module}; module load STAR
STAR \
--runThreadN 16 \
--runMode alignReads \
--readFilesCommand zcat \
--genomeDir {input.genomedir} \
--outFileNamePrefix analysis/{wildcards.target}/{wildcards.sample}/orig_ \
--outSAMattributes All \
--outSAMstrandField intronMotif \
--outSAMtype BAM SortedByCoordinate \
--readFilesIn {params.r1s} {params.r2s}
if [ -s analysis/{wildcards.target}/{wildcards.sample}/orig_Aligned.sortedByCoord.out.bam ]; then
mv analysis/{wildcards.target}/{wildcards.sample}/orig_Aligned.sortedByCoord.out.bam {output};
fi
"""
rule make_snpdir:
input:
vcf="Reference/{target}/simsec_variants_on_{target}.gvcf.gz"
output:
dir="Reference/{target}/snpdir",
file="Reference/{target}/snpdir/all.txt.gz",
shell:"""
mkdir -p {output.dir}
gzip -dc {input.vcf} \
| grep -v "^#" \
| awk 'BEGIN {{OFS="\t"}}; length($4) == 1 && length($5) == 1 {{print $1,$2,$4,$5}};' \
| gzip -c \
> {output.file}
"""
rule dedup:
input: "{sample}.bam"
output: "{sample}.dedup.bam"
log: "{sample}_dedup.log"
shell: """{module}; module load picard
picard MarkDuplicates \
SORTING_COLLECTION_SIZE_RATIO=.05 \
MAX_FILE_HANDLES_FOR_READ_ENDS_MAP=1000 \
MAX_RECORDS_IN_RAM=2500000 \
READ_NAME_REGEX=null \
REMOVE_DUPLICATES=true \
DUPLICATE_SCORING_STRATEGY=RANDOM \
INPUT={input} OUTPUT={output} METRICS_FILE={log}
"""
rule wasp_find_snps:
input:
bam="analysis/{genome}/{sample}/{prefix}.dedup.bam",
bai="analysis/{genome}/{sample}/{prefix}.dedup.bam.bai",
snpdir="Reference/{genome}/snpdir",
snpfile="Reference/{genome}/snpdir/all.txt.gz"
output:
temp("analysis/{genome}/{sample}/{prefix}.dedup.remap.fq1.gz"),
temp("analysis/{genome}/{sample}/{prefix}.dedup.remap.fq2.gz"),
temp("analysis/{genome}/{sample}/{prefix}.dedup.keep.bam"),
temp("analysis/{genome}/{sample}/{prefix}.dedup.to.remap.bam"),
shell:
"""python ~/FWASP/mapping/find_intersecting_snps.py \
--progressbar \
--phased --paired_end \
{input.bam} {input.snpdir}
"""
rule wasp_remap:
input:
R1="analysis/{genome}/{sample}/{prefix}.remap.fq1.gz",
R2="analysis/{genome}/{sample}/{prefix}.remap.fq2.gz",
genome="Reference/{genome}/orig/Genome",
genomedir="Reference/{genome}/orig/"
output:
temp("analysis/{genome}/{sample}/{prefix}.remap.bam")
threads: 16
shell: """{module}; module load STAR;
rm -rf analysis/{wildcards.genome}/{wildcards.sample}/STARtmp
STAR \
--genomeDir {input.genomedir} \
--outFileNamePrefix analysis/{wildcards.genome}/{wildcards.sample}/remap \
--outSAMattributes MD NH --clip5pNbases 6 \
--outSAMtype BAM Unsorted \
--outTmpDir analysis/{wildcards.genome}/{wildcards.sample}/STARtmp \
--limitBAMsortRAM 20000000000 \
--runThreadN {threads} \
--readFilesCommand zcat \
--readFilesIn {input.R1} {input.R2}
mv analysis/{wildcards.genome}/{wildcards.sample}/remapAligned.out.bam {output}
"""
rule wasp_keep:
input:
toremap="analysis/{file}.to.remap.bam",
remapped="analysis/{file}.remap.bam",
output:
temp("analysis/{file}.remap.kept.bam"),
shell: """
{preconda}
source activate peter
python ~/FWASP/mapping/filter_remapped_reads.py \
-p \
{input.toremap} {input.remapped} \
{output} """
rule wasp_merge:
input:
"analysis/{file}.remap.kept.bam",
"analysis/{file}.keep.bam",
output:
temp("analysis/{file}.keep.merged.bam")
shell:
"{module}; module load samtools; samtools merge {output} {input}"
rule star_map_emase:
input:
unpack(getreads(1)),
unpack(getreads(2)),
genome="Reference/{target}/emase.pooled/Genome",
genomedir="Reference/{target}/emase.pooled/",
params:
r1s=getreadscomma(1),
r2s=getreadscomma(2),
output: "analysis/{target}/{sample}/mapped.bam"
threads: 6
log: "{sample}/assigned_dmelR.log"
shell: """{module}; module load STAR
STAR \
--runThreadN 16 \
--runMode alignReads \
--readFilesCommand zcat \
--genomeDir {input.genomedir} \
--outFileNamePrefix analysis/{wildcards.target}/{wildcards.sample}/ \
--outSAMattributes All \
--outSAMstrandField intronMotif \
--outSAMtype BAM SortedByCoordinate \
--readFilesIn {params.r1s} {params.r2s}
if [ -s analysis/{wildcards.target}/{wildcards.sample}/Aligned.sortedByCoord.out.bam ]; then
mv analysis/{wildcards.target}/{wildcards.sample}/Aligned.sortedByCoord.out.bam {output};
fi
"""
rule makedir:
output: "{prefix}.log", "{prefix}/"
shell: "touch {wildcards.prefix}.log; mkdir -p {wildcards.prefix}"
rule kallisto_index_unsplit:
input:
fasta="Reference/{target}/simsec_corrected_fbtr.fa"
output:
'Reference/{target}/kallisto_unsplit'
shell:"""~/Downloads/kallisto/kallisto index\
--index {output} \
{input.fasta}
"""
rule bowtie_build_corrected:
input:
fasta="Reference/{target}/simsec_corrected.fasta"
output:
expand("Reference/{{target}}/simsec_corrected.{n}.bt2", n=[1,2,3,4])
shell:"""
{module}
module load bowtie2
bowtie2-build --threads 16 {input.fasta} Reference/{wildcards.target}/simsec_corrected
"""
rule tophat_transcriptome:
input:
"Reference/{target}/simsec_corrected.1.bt2",
gtf="Reference/{target}_good.gtf"
output:
"Reference/{target}/simsec_corrected_transcripts.fa",
"Reference/{target}/simsec_corrected_transcripts.fa.tlst",
"Reference/{target}/simsec_corrected_transcripts.gff",
shell:""" {module}
module load tophat bowtie2
tophat2 \
--GTF {input.gtf} \
--num-threads 16 \
--transcriptome-index Reference/{wildcards.target}/simsec_corrected_transcripts \
Reference/{wildcards.target}/simsec_corrected
"""
# Tophat gives the transcripts in the least useful format for my purposes:
# >29823 FBtr0113891 YHet+ 311-424,540-799,857-1196,1254-1519, ...
# ^ ^
# ??? |
# Useful
rule fbtr_transcriptome:
input:
"Reference/{target}/simsec_corrected_transcripts.fa"
output:
"Reference/{target}/simsec_corrected_fbtr.fa"
shell:"perl -pe 's/>[^F]*FBtr/>FBtr/' < {input} > {output}"
rule kallisto_index:
input:
fasta="Reference/{target}/emase.pooled.transcripts.fa"
output:
'Reference/{target}/kallisto'
shell:"""~/Downloads/kallisto/kallisto index\
--index {output} \
{input.fasta}
"""
rule kallisto_quant_unsplit:
input:
unpack(getreads(1)),
unpack(getreads(2)),
index='Reference/{target}/kallisto_unsplit',
dir=ancient('analysis/{target}/{sample}/unsplit/')
priority: 50
params:
reads=interleave_reads
output: "analysis/{target}/{sample}/unsplit/abundance.h5", "analysis/{target}/{sample}/unsplit/abundance.tsv"
shell:"""
mkdir -p {wildcards.sample}
~/Downloads/kallisto/kallisto quant \
-i {input.index} \
-b 50 -t 5 \
-o analysis/{wildcards.target}/{wildcards.sample}/unsplit \
{params.reads}
"""
rule kallisto_quant:
input:
unpack(getreads(1)),
unpack(getreads(2)),
index='Reference/{target}/kallisto',
dir=ancient('analysis/{target}/{sample}/')
priority: 50
params:
reads=interleave_reads
output: "analysis/{target}/{sample}/abundance.h5", "analysis/{target}/{sample}/abundance.tsv"
shell:"""
mkdir -p {wildcards.sample}
~/Downloads/kallisto/kallisto quant \
-i {input.index} \
-b 50 -t 5 \
-o analysis/{wildcards.target}/{wildcards.sample}/ \
{params.reads}
"""
rule get_combined_variants:
input:
ref_fasta="prereqs/d{parent}.fasta",
sim_gvcf="Reference/{parent}/sim_gdna_raw_variants_uncalibrated.p.g.vcf",
sec_gvcf="Reference/{parent}/sec_gdna_raw_variants_uncalibrated.p.g.vcf",
output:
tsv="Reference/{parent}/simsec_variants.tsv",
vcf="Reference/{parent}/simsec_variants_on_{parent}.gvcf"
params:
dir="Reference/{parent}/"
shell: """
{module}; module load java
gatk -T GenotypeGVCFs \
-R {input.ref_fasta} \
-V {input.sim_gvcf} \
-V {input.sec_gvcf} \
-o {output.vcf}
gatk -T VariantsToTable \
-R {input.ref_fasta} \
-V {output.vcf} \
-F CHROM -F POS -F REF -F ALT -F QUAL \
-F HET -F HOM-REF -F HOM-VAR -F NCALLED \
-GF GT \
-o {output.tsv}
"""
rule split_variants:
input:
"{variants}.gvcf"
output:
indels="{variants}_indels.gvcf",
snps="{variants}_snps.gvcf"
shell:"""
{module}; module load bcftools
bcftools view --types snps -o {output.snps} {input}
bcftools view --types indels -o {output.indels} {input}
"""
rule map_gdna:
input:
unpack(getreads(1)),
unpack(getreads(2)),
ancient("Reference/{reference}/"),
bt2_index="prereqs/d{reference}.1.bt2"
output:
"Reference/{reference}/{sample}_bowtie2.bam"
params:
index="prereqs/d{reference}",
r1s=getreadscomma(1),
r2s=getreadscomma(2),
outdir= lambda wildcards, output: path.dirname(output[0])
threads: 12
shell: """{module}; module load samtools/1.3 bowtie2
bowtie2 \
--very-sensitive-local \
-p 11 \
--rg-id {wildcards.sample} \
--rg "SM:{wildcards.sample}" \
--rg "PL:illumina" \
--rg "LB:lib1"\
--rg "PU:unit1" \
--no-unal \
-x {params.index} \
-1 {params.r1s} \
-2 {params.r2s} \
| samtools view -b \
| samtools sort -o {output} -T {params.outdir}/{wildcards.sample}_bowtie2_sorting
"""
rule sort_bam:
input: "{sample}.bam"
output: "{sample}.sort.bam"
shell: "{module}; module load samtools; samtools sort -o {output} {input}"
rule index_bam:
input: "{sample}.bam"
output: "{sample}.bam.bai"
log: "{sample}.bam.bai_log"
shell: "{module}; module load samtools; samtools index {input}"
rule bowtie2_build:
input: "{base}.fasta"
output: "{base}.1.bt2"
log: "{base}.bt2.log"
shell: "{module}; module load bowtie2; bowtie2-build --offrate 3 {input} {wildcards.base}"
ruleorder: bowtie_build_corrected > bowtie2_build
rule call_variants:
input:
ref_fasta="prereqs/d{reference}.fasta",
ref_fai="prereqs/d{reference}.fasta.fai",
ref_dict="prereqs/d{reference}.dict",
bam="Reference/{reference}/{species}_gdna_bowtie2.dedup.bam",
bai="Reference/{reference}/{species}_gdna_bowtie2.dedup.bam.bai",
output:
"Reference/{reference}/{species}_gdna_raw_variants_uncalibrated.p.g.vcf"
threads: 4
shell: """
{module}; module load java
gatk -T HaplotypeCaller \
-R {input.ref_fasta} \
-I {input.bam} \
-nct 16 \
--genotyping_mode DISCOVERY \
--output_mode EMIT_ALL_SITES \
--emitRefConfidence GVCF \
-GQB 10 -GQB 20 -GQB 30 -GQB 50 \
-stand_emit_conf 10 \
-stand_call_conf 30 \
-o {output}
"""
rule get_sra:
output:
"sequence/{srr}_1.fastq.gz",
"sequence/{srr}_2.fastq.gz"
#log: "sequence/{srr}.log"
resources: max_downloads=1
shell: """{module}; module load sra-tools
fastq-dump --gzip --split-3 --outdir sequence {wildcards.srr}
"""
rule extra_fasta: # Some things DEMAND a .fa extension. Fuckers.
output: "Reference/d{species}.fa"
input: "prereqs/d{species}.fasta"
shell: "cp {input} {output} """
rule ref_genome:
output: "prereqs/d{species}.fasta"
log: "prereqs/d{species}.log"
params:
date=lambda wildcards: dates[wildcards.species],
version=lambda wildcards: versions[wildcards.species]
shell: """{module}; #module load wget
mkdir -p prereqs
wget -O {output}.gz ftp://ftp.flybase.org/releases/{params.date}/d{wildcards.species}_{params.version}/fasta/d{wildcards.species}-all-chromosome-{params.version}.fasta.gz
gunzip --force {output}.gz
"""
rule fadict:
input: "{file}.fasta"
output: "{file}.dict"
shell: "{module}; module load picard; picard CreateSequenceDictionary R={input} O={output}"
rule index_fasta:
input: "{file}.fasta"
output: "{file}.fasta.fai"
shell: "samtools faidx {input}"
rule ref_gff:
output: "prereqs/d{species}.gff"
log: "prereqs/d{species}.log"
params:
date=lambda wildcards: dates[wildcards.species],
version=lambda wildcards: versions[wildcards.species]
shell: """{module}; #module load wget
mkdir -p prereqs
wget -O {output}.gz ftp://ftp.flybase.org/releases/{params.date}/d{wildcards.species}_{params.version}/gff/d{wildcards.species}-all-{params.version}.gff.gz
gunzip --force {output}.gz
"""
rule all_gtf:
input: "prereqs/d{species}.gff"
output: "Reference/{species}_all.gtf"
log: "Reference/{species}_all.log"
shell: """{module}; module load cufflinks
gffread {input} -C -E -T -o- \
> {output}
"""
rule good_gtf:
input: "Reference/{species}_all.gtf"
output: "Reference/{species}_good.gtf"
log: "Reference/{species}_good.log"
shell: """
cat {input} \
| grep -vP '(snoRNA|CR[0-9]{{4}}|Rp[ILS]|mir-|tRNA|unsRNA|snRNA|snmRNA|scaRNA|rRNA|RNA:|mt:|His.*:)' \
| grep 'gene_id' \
> {output}
"""
rule bad_gtf:
input: "Reference/{species}_all.gtf"
output: "Reference/{species}_bad.gtf"
log: "Reference/{species}_bad.log"
shell: """
echo {threads}
cat {input} \
| grep -vP '(snoRNA|CR[0-9]{{4}}|Rp[ILS]|mir-|tRNA|unsRNA|snRNA|snmRNA|scaRNA|rRNA|RNA:|mt:|His.*:)' \
> {output}
"""
rule sample_gene_ase_pval:
input:
bam="analysis/{target}/{sample}/orig_mapped.dedup.keep.merged.sort.bam",
bai="analysis/{target}/{sample}/orig_mapped.dedup.keep.merged.sort.bam.bai",
variants="Reference/{target}/simsec_variant.bed",
gtf="Reference/{target}_good.gtf",
sentinel=path.join("analysis", 'recalc_ase')
threads: 1
output:
"analysis/{target}/{sample}/gene_ase_pval.tsv"
shell: """ export PYTHONPATH=$HOME/ASEr/
python ~/ASEr/bin/GetGeneASEbyReads.py \
--outfile {output} \
--id-name gene_name \
--ase-function log10pval \
--min-reads-per-allele 0 \
{input.variants} \
{input.gtf} \
{input.bam}
"""
rule sample_gene_ase:
input:
bam="analysis/{target}/{sample}/orig_mapped.dedup.keep.merged.sort.bam",
bai="analysis/{target}/{sample}/orig_mapped.dedup.keep.merged.sort.bam.bai",
variants="Reference/{target}/simsec_variant.bed",
gtf="Reference/{target}_good.gtf",
sentinel=path.join("analysis", 'recalc_ase')
threads: 1
output:
"analysis/{target}/{sample}/gene_ase_by_read.tsv"
shell: """ export PYTHONPATH=$HOME/ASEr/
python ~/ASEr/bin/GetGeneASEbyReads.py \
--outfile {output} \
--assign-all-reads \
--id-name gene_name \
--ase-function log2offset \
--min-reads-per-allele 0 \
{input.variants} \
{input.gtf} \
{input.bam}
"""
rule nowasp_gene_ase:
input:
bam="analysis/{target}/{sample}/orig_mapped.dedup.sort.bam",
bai="analysis/{target}/{sample}/orig_mapped.dedup.sort.bam.bai",
variants="Reference/{target}/simsec_variant.bed",
gtf="Reference/{target}_good.gtf",
sentinel=path.join("analysis", 'recalc_ase')
threads: 1
output:
"analysis/{target}/{sample}/gene_ase_nowasp.tsv"
shell: """ export PYTHONPATH=$HOME/ASEr/
python ~/ASEr/bin/GetGeneASEbyReads.py \
--outfile {output} \
--assign-all-reads \
--id-name gene_name \
--ase-function pref_index \
--min-reads-per-allele 0 \
{input.variants} \
{input.gtf} \
{input.bam}
"""
rule ase_summary:
input:
lambda wc: expand('analysis/{target}/{sample}/gene_ase_by_read.tsv',
target=[wc.target],
sample=[c for c in config['samples'] if 'gdna' not in c])
output:
'analysis/{target}/summary_ase.tsv'
log:
'analysis/{target}/mst.log'
shell: """
{preconda}
source activate peter
python MakeSummaryTable.py \
--filename gene_ase_by_read.tsv \
--key gene \
--out-basename summary_ase \
--column ase_value \
analysis/{wildcards.target}/ \
| tee {log}
"""
rule ase_refalt_summary:
input:
lambda wc: expand('analysis/{target}/{sample}/gene_ase_by_read.tsv',
target=[wc.target],
sample=[c for c in config['samples'] if 'gdna' not in c])
output:
'analysis/{target}/summary_ase_refalt.tsv'
log:
'analysis/{target}/mst_refalt.log'
shell: """
{preconda}
source activate peter
python MakeSummaryTable.py \
--filename gene_ase_by_read.tsv \
--key gene \
--refalt \
--out-basename summary_ase_refalt \
--float-format %5.0f \
--column ref_counts \
analysis/{wildcards.target}/ \
| tee {log}
"""
rule nowasp_ase_summary:
input:
lambda wc: expand('analysis/{target}/{sample}/gene_ase_nowasp.tsv', target=[wc.target], sample=[c for c in config['samples'] if 'gdna' not in c])
output:
'analysis/{target}/summary_ase_nowasp.tsv'
log:
'analysis/{target}/mst.log'
shell: """
{preconda}
source activate peter
python MakeSummaryTable.py \
--filename gene_ase_nowasp.tsv \
--key gene \
--out-basename summary_ase_nowasp \
--column ase_value \
analysis/{wildcards.target}/ \
| tee {log}
"""
rule pval_summary:
input:
lambda wc: expand('analysis/{target}/{sample}/gene_ase_pval.tsv', target=[wc.target], sample=[c for c in config['samples'] if 'gdna' not in c])
output:
'analysis/{target}/summary_ase_pvals.tsv'
log:
'analysis/{target}/mst.log'
shell: """
{preconda}
source activate peter
python MakeSummaryTable.py \
--filename gene_ase_pval.tsv \
--key gene \
--out-basename summary_ase_pvals \
--column ase_value \
analysis/{wildcards.target}/ \
| tee {log}
"""
rule tissue_pvals:
input:
lambda wc: expand('analysis/{target}/{sample}/gene_ase_by_read.tsv', target=[wc.target], sample=[c for c in config['samples'] if 'gdna' not in c])
output:
'analysis/{target}/summary_ase_tissue_pvals.tsv'
shell:"""
{preconda}
source activate peter
python CombinedASEbySample.py -o {output} {input}
"""
rule kallisto_summary:
input:
lambda wc: expand('analysis/{target}/{sample}/unsplit/abundance.tsv', target=[wc.target], sample=[c for c in config['samples'] if 'gdna' not in c])
output:
'analysis/{target}/summary_kallisto_in_unsplit.tsv'
log:
'analysis/{target}/mst.log'
shell: """
{preconda}
source activate peter
python MakeSummaryTable.py \
--filename abundance.tsv \
--in-subdirectory unsplit \
--key target_id \
--out-basename summary_kallisto \
--column tpm \
analysis/{wildcards.target}/ \
| tee {log}
"""
rule kallisto_summary_renamed:
input:
expr = 'analysis/{target}/summary_kallisto_in_unsplit.tsv',
info = 'Reference/{target}/simsec_corrected_transcripts.gff',
tlst = 'Reference/{target}/simsec_corrected_transcripts.fa.tlst',
output: 'analysis/{target}/summary_kallisto_genes.tsv'
shell: """
{preconda}
source activate peter
python CombineExprByGenes.py \
--cufflinks-tlst {input.tlst} \
--use-gtf --use-gene-name \
--outfile {output} \
{input.info} {input.expr}
"""
rule draw_specificity:
input:
code="DrawSpecificity.py",
outdir=ancient('analysis/{target}/figure-specificity/'),
expr='analysis/{target}/summary_kallisto_genes.tsv',
ase='analysis/{target}/summary_ase.tsv',
pvals='analysis/{target}/deseq_pvals.tsv',
specificities="analysis/{target}/combined/oefemale_spec_genes.txt",
output:
expand('analysis/{{target}}/figure-specificity/{tissue}{sex}.svg',
tissue=['oe', 'fb'], sex=['male', 'female'],)
shell: """
{preconda}
source activate peter
python DrawSpecificity.py \
--pvals {input.pvals} \
--try-orthologs prereqs \
--specificities analysis/{wildcards.target}/combined/ \
{input.expr} {input.ase} {input.outdir}
"""
rule translate_fbtrs:
input:
in_file="analysis/{species}/{prefix}.{suffix}",
gtf="Reference/{species}_good.gtf",
output:
"analysis/{species,[^/]+}/{prefix}.translated.{suffix}"
shell: "python TranslateToMel.py {input.gtf} {input.in_file} {output}"
rule alignment:
input:
A="analysis/targets/{gene}/{A}.fasta",
B="analysis/targets/{gene}/{B}.fasta",
wildcard_constraints:
A="[a-z][a-z][a-z]+",
B="[a-z][a-z][a-z]+",
output:
"analysis/targets/{gene}/{A}_{B}.needleall"
log:
"analysis/targets/{gene}/{A}_{B}.needleall.log"
shell: """{module}; module load EMBOSS
needleall -aseq {input.A} -bseq {input.B} \
-aformat3 srspair -gapopen 10.0 -gapextend 0.5 \
-outfile {output}"""
rule combined_fasta:
input:
A="analysis/targets/{gene}/{A}.fasta",
B="analysis/targets/{gene}/{B}.fasta",
wildcard_constraints:
A="[a-z][a-z][a-z]+",
B="[a-z][a-z][a-z]+",
output:
"analysis/targets/{gene}/{A}_{B}.fasta"
log:
"analysis/targets/{gene}/{A}_{B}.fasta.log"
shell:
"cat {input.A} {input.B} > {output}"
rule melsimsec_fastas:
input:
dir=ancient("analysis/targets/{region}/mss/"),
mel="analysis/targets/{region}/mel.fasta",
sim="analysis/targets/{region}/sim.fasta",
sec="analysis/targets/{region}/sec.fasta",
output:
#dynamic("analysis/targets/{region}/mss/region_{rnum}.fasta")
shell:"""
{preconda}
source activate peter
python PrepareForClustal.py \
--outdir {input.dir} \
{input.mel} {input.sim} {input.sec}
"""
rule clustalo_align:
input:
"analysis/targets/{region}/mss/region_{rnum}.fasta"
output:
"analysis/targets/{region}/mss/region_{rnum}.clu"
shell: """
{preconda}
source activate peter
clustalo \
--in {input} --out {output} --outfmt=clustal
"""
rule mel_intergenic_bed:
input:
gtf="Reference/mel_all.gtf",
dir=ancient("analysis/targets/{upstream}_{downstream}_intergenic/"),
output:
outbed="analysis/targets/{upstream}_{downstream}_intergenic/mel.bed"
run:
starts = []
stops = []
active = False
with open(output.outbed, 'w') as outf:
for line in open(input.gtf):
data = line.split('\t')
if len(data) < 8: