forked from petercombs/HybridSliceSeq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnakefile
1412 lines (1290 loc) · 44.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
from os import path
mel_release = "r5.57_FB2014_03"
sim_release = "r2.01_FB2016_01"
mel_version, mel_date = mel_release.split('_', 1)
sim_version, sim_date = sim_release.split('_', 1)
num_mel_windows = 17
dates = {'mel': mel_date, 'sim': sim_date}
versions = {'mel': mel_version, 'sim': sim_version}
analysis_dir = 'analysis_godot'
hybrid_dir = 'analysis_godot/on_mel'
mel_gtf = 'Reference/mel_good.gtf'
mel_bad_gtf = 'Reference/mel_bad.gtf'
mel_fasta = 'Reference/dmel_prepend.fasta'
variants = path.join(hybrid_dir, 'melsim_variant.bed')
localrules: all, makedir, all_files_per_sample
configfile: "Parameters/config.json"
mxs_r1 = ['melXsim_cyc14C_rep1_sl{:02d}'.format(i) for i in range(4, 30)]
mxs_r2 = ['melXsim_cyc14C_rep2_sl{:02d}'.format(i) for i in range(7, 33)]
mxs_r3 = ['melXsim_cyc14C_rep3_sl{:02d}'.format(i) for i in range(5, 32)]
sxm_r1 = ['simXmel_cyc14C_rep1_sl{:02d}'.format(i) for i in range(37, 62)]
sxm_r2 = ['simXmel_cyc14C_rep2_sl{:02d}'.format(i) for i in range(3, 30)]
mxm_r1 = ['melXmel_cyc14C_sl{:02d}'.format(i) for i in range(7, 34)]
sxs_r1 = ['simXsim_cyc14C_sl{:02d}'.format(i) for i in range(7, 34)]
#samples = mxs_r1 + mxs_r2 + mxs_r3 + sxm_r1 + sxm_r2 + mxm_r1 + sxs_r1
samples = [sample for sample in config['samples'] if 'gdna' not in sample]
module = '''module () {
eval `$LMOD_CMD bash "$@"`
}'''
def getreads(readnum):
if readnum == 0:
formatstr = 'sequence/{}.fastq.gz'
else:
formatstr = 'sequence/{{}}_{}.fastq.gz'.format(readnum)
def retfun(wildcards):
return [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
rule all:
input:
path.join(analysis_dir, "summary.tsv"),
path.join(analysis_dir, "ase_summary_by_read.tsv")
rule all_reads:
input:
[reads for sample in samples for reads in getreads_nowc(1)(sample)]
rule print_config:
run:
print(config)
tfs = "OTF0070.1 hb kni gt D FBgn0001325_4 tll hkb FBgn0000251_3 FBgn0003448_3 twi OTF0532.1 OTF0478.1 OTF0181.1 FBgn0013753 FBgn0001204"
tf_names = "bcd hb kni gt D Kr tll hkb cad sna twi zen Doc2 rho/pnt run/Bgb hkb_FFS"
tf_dict = {
'bcd': 'OTF0070.1',
'hb': 'hb',
'kni': 'kni',
'gt': 'gt',
'D': 'D',
'Kr': 'FBgn0001325_4',
'tll': 'tll',
'hkb': 'hkb',
'cad': 'FBgn0000251_3',
'sna': 'FBgn0003448_3',
'twi': 'twi',
'zen': 'OTF0532.1',
'Doc2': 'OTF0478.1',
'rho/pnt': 'OTF0181.1',
'rho': 'OTF0181.1',
'pnt': 'OTF0181.1',
'run/Bgb': 'FBgn0013753',
'run': 'FBgn0013753',
'hkbFFS': 'hkb_FBgn0001204',
'cic': 'FBgn0028386',
'retn': 'FBgn0004795',
}
rule get_all_memes:
output: "prereqs/all_meme.meme"
shell: """{module}; #module load wget
wget -O prereqs/motif_databases.tgz \
http://meme-suite.org/meme-software/Databases/motifs/motif_databases.12.17.tgz
tar -xvf prereqs/motif_databases.tgz -C prereqs
cat prereqs/motif_databases/FLY/* > {output}
"""
rule condense_memes:
input:
"prereqs/all_meme.meme",
output:
"Reference/all_meme_filtered.meme"
shell: "python CondenseMemes.py"
rule alignment_figure:
input:
aln="analysis/targets/{gene}/{A}_{B}.needleall",
fa="analysis/targets/{gene}/{A}_{B}.fasta",
bed="analysis/targets/{gene}/{A}.bed",
fimoA="analysis/targets/{gene}/{A}/fimo.txt",
fimoB="analysis/targets/{gene}/{B}/fimo.txt",
code="PatserAlignToSVG.py",
output: "analysis/targets/{gene}/{A}_{B}.needleall.svg"
log: "analysis/targets/{gene}/{A}_{B}.needleall.log"
wildcard_constraints:
A="[a-z][a-z][a-z]+",
B="[a-z][a-z][a-z]+",
shell: """python {input.code} \
-v --x-ticks 100 --draw-alignment \
--x-scale 2 --y-sep 60 --y-scale 3.5 \
--bar-width 10 \
--comp1 {wildcards.A} --comp2 {wildcards.B} \
--match-dim 0.7 \
--meme-suite \
--tf {tfs} --tf-names {tf_names} \
--sequence --fasta {input.fa} \
--needleall {input.aln} \
--coordinates-bed {input.bed} \
--bed-track Reference/bdtnp_dnase_2_prepend.bed \
analysis/targets/{wildcards.gene}/
"""
rule alignment_figure_nondefaulttfs:
input:
aln="analysis/targets/{gene}/{A}_{B}.needleall",
fa="analysis/targets/{gene}/{A}_{B}.fasta",
bed="analysis/targets/{gene}/{A}.bed",
fimoA="analysis/targets/{gene}/{A}/fimo.txt",
fimoB="analysis/targets/{gene}/{B}/fimo.txt",
code="PatserAlignToSVG.py",
output: "analysis/targets/{gene}/{A}_{B}.{tfs}.needleall.svg"
log: "analysis/targets/{gene}/{A}_{B}.{tfs}.needleall.log"
wildcard_constraints:
A="[a-z][a-z][a-z]+",
B="[a-z][a-z][a-z]+",
run:
local_tfnames = wildcards.tfs.split('_')
local_tfs = [tf_dict[tf] for tf in local_tfnames]
shell("""python {input.code} \\
-v --x-ticks 100 --draw-alignment \\
--x-scale 2 --y-sep 60 --y-scale 3.5 \\
--bar-width 10 \\
--comp1 {wildcards.A} --comp2 {wildcards.B} \\
--match-dim 0.7 \\
--meme-suite \\
--tf {local_tfs} --tf-names {local_tfnames} \\
--sequence --fasta {input.fa} \\
--needleall {input.aln} \\
--coordinates-bed {input.bed} \\
--bed-track Reference/bdtnp_dnase_2_prepend.bed \\
--outfile {output}\\
analysis/targets/{wildcards.gene}/
""")
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 fimo:
input:
fa="analysis/targets/{gene}/{species}.fasta",
meme="Reference/all_meme_filtered.meme"
output:
"analysis/targets/{gene}/{species}/fimo.txt"
log: "analysis/targets/{gene}/{species}/fimo.txt.log"
shell:"""
mkdir -p `dirname {output}`
fimo -oc `dirname {output}` --thresh 1e-3 {input.meme} {input.fa} 2> {log}
"""
rule non_mel_bed:
input:
fa="analysis/targets/{gene}/mel.fasta",
blastdb="Reference/d{species}_prepend.fasta.nhr"
output: "analysis/targets/{gene}/{species}.bed"
#wildcard_constraints: species="^(?!mel$).*$"
shell: """{module}; module load blast bioawk
blastn -db Reference/d{wildcards.species}_prepend.fasta \
-outfmt "6 sseqid sstart send qseqid evalue sstrand length qlen slen qstart qend" \
-gapextend 0\
-query {input.fa} \
| awk '!_[$4]++' \
| bioawk -t '$10 > 1 && $6 ~ /minus/ {{$2 += $10 + 1}}; \
$10 > 1 && $6 ~ /plus/ {{$2 -= $10 + 1}}; \
$11 < $8 && $6 ~ /minus/ {{$3 -= ($8 - $11) + 1}}; \
$2 > $3 {{gsub("mel", "{wildcards.species}", $4); print $1,$3,$2+1,$4,$7/($8+1),"-", $3, $2 }}; \
$2 < $3 {{gsub("mel", "{wildcards.species}", $4); print $1,$2,$3+1,$4,$7/($8+1),"+", $2, $3 }}; '\
> {output}
"""
rule mel_bed:
input:
gtf="Reference/mel_good.gtf",
melsize="Reference/dmel.chr.sizes",
oreganno="Reference/oreganno.prepend.bed",
dnase="Reference/binding/dnase_peaks_prepend.bed",
output:
"analysis/targets/{gene}/mel.bed"
shell: """{module}; module load bioawk bedtools
mkdir -p `dirname output`
grep '"{wildcards.gene}"' {input.gtf} \
| bedtools sort \
| bedtools merge \
| bedtools window -w 10000 -b - -a {input.dnase} \
| bioawk -t '{{print $1, $2, $3, "mel_{wildcards.gene}_" NR, "0", "+", $2, $3}}' \
| uniq --skip-fields 4 \
> {output}
grep '"{wildcards.gene}"' {input.gtf} \
| bedtools sort \
| bedtools merge \
| bedtools window -w 10000 -b - -a {input.dnase} \
| bioawk -t '{{print $1, $2, $3, "mel_oreganno_{wildcards.gene}_" NR, "0", "+", $2, $3}}' \
| uniq --skip-fields 4 \
>> {output}
"""
rule nonmel_fasta_from_bed:
input:
bed='analysis/targets/{gene}/{species}.bed',
full_fasta='Reference/d{species}_prepend.fasta',
output:
'analysis/targets/{gene}/{species}.fasta'
shell: """ {module}; module load bedtools
bedtools getfasta -fi {input.full_fasta} -bed {input.bed} \
-fo {output} -s -name
"""
rule make_blastdb:
input: "Reference/{file}.fasta"
output: "Reference/{file}.fasta.nhr"
shell: """{module}; module load blast; makeblastdb -dbtype nucl -in {input}"""
rule mel_chr_sizes:
input: "Reference/dmel_prepend.fasta"
output: "Reference/dmel.chr.sizes"
run:
out = open(output[0], 'w')
for line in open(input[0]):
if not line.startswith('>'):
continue
d=line.split()
chrom = d[0][1:]
length = next(el for el in d if el.startswith('length')).replace('length=', '').replace(';', '')
print(chrom, length, sep='\t', file=out)
rule melfasta:
input:
bed="{prefix}/mel.bed",
ref="Reference/dmel_prepend.fasta"
output:
"{prefix}/mel.fasta"
shell: """{module}; module load bedtools
bedtools getfasta -s -name -fi {input.ref} -fo {output} -bed {input.bed}
"""
rule getfasta:
input:
bed="{prefix}/{species}.bed",
ref="Reference/d{species}_prepend.fasta"
output:
"{prefix}/{species}.fasta"
wildcard_constraints: species="^...$"
shell: """{module}; module load bedtools
bedtools getfasta -s -name -fi {input.ref} -fo {output} -bed {input.bed}
"""
rule exons:
input:
bed="analysis/targets/{gene}/{species}.bed",
gtf="Reference/{species}_good.gtf",
output:
"analysis/targets/{gene}/{species}_exons.gtf"
shell: """{module}; module load bedtools
bedtools intersect -b {input.bed} -a {input.gtf} -u \
| grep exon \
> {output}
"""
ruleorder: mel_bed > non_mel_bed
rule make_snpdir:
input:
vcf="analysis_godot/on_{target}/melsim_variants_on{target}.gvcf"
output:
dir="analysis_godot/on_{target}/snpdir",
file="analysis_godot/on_{target}/snpdir/all.txt.gz",
shell:"""
mkdir -p {output.dir}
cat {input.vcf} \
| grep -v "^#" \
| awk 'BEGIN {{OFS="\t"}}; length($4) == 1 && length($5) == 1 {{print $1,$2,$4,$5}};' \
| gzip -c \
> {output.file}
"""
rule wasp_find_snps:
input:
bam="{sample}/{prefix}_dedup.bam",
bai="{sample}/{prefix}_dedup.bam.bai",
snpdir="analysis_godot/on_mel/snpdir",
snpfile="analysis_godot/on_mel/snpdir/all.txt.gz"
output:
temp("{sample}/{prefix}_dedup.remap.fq1.gz"),
temp("{sample}/{prefix}_dedup.remap.fq2.gz"),
temp("{sample}/{prefix}_dedup.keep.bam"),
temp("{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="{sample}/{prefix}.remap.fq1.gz",
R2="{sample}/{prefix}.remap.fq2.gz",
genome="Reference/dmel_prepend/Genome",
genomedir="Reference/dmel_prepend/"
output:
temp("{sample}/{prefix}.remap.bam")
threads: 16
shell: """{module}; module load STAR;
rm -rf {wildcards.sample}/STARtmp
STAR \
--genomeDir {input.genomedir} \
--outFileNamePrefix {wildcards.sample}/remap \
--outSAMattributes MD NH --clip5pNbases 6 \
--outSAMtype BAM Unsorted \
--outTmpDir {wildcards.sample}/STARtmp \
--limitBAMsortRAM 20000000000 \
--runThreadN {threads} \
--readFilesCommand zcat \
--readFilesIn {input.R1} {input.R2}
mv {wildcards.sample}/remapAligned.out.bam {output}
"""
rule wasp_keep:
input:
toremap="{file}.to.remap.bam",
remapped="{file}.remap.bam",
output:
temp("{file}.remap.kept.bam"),
shell: """
export CONDA_PATH_BACKUP=""
export PS1=""
source activate peter
python ~/FWASP/mapping/filter_remapped_reads.py \
-p \
{input.toremap} {input.remapped} \
{output} """
rule wasp_merge:
input:
"{file}.remap.kept.bam",
"{file}.keep.bam",
output:
temp("{file}.keep.merged.bam")
shell:
"{module}; module load samtools; samtools merge {output} {input}"
def samples_to_files(fname):
# Returns a function that is callable by an input rule
def retfun():
return [path.join(analysis_dir, sample, fname) for sample in samples]
return retfun
rule all_files_per_sample:
output: touch("tmp/all_{fname}")
input: lambda wildcards: samples_to_files(wildcards.fname)()
rule sample_expr:
input:
bam="{sample}/assigned_dmelR.bam",
bai="{sample}/assigned_dmelR.bam.bai",
gtf=mel_gtf,
fasta=mel_fasta,
mask_gtf=mel_bad_gtf,
sentinel=path.join(analysis_dir, 'recufflinks')
threads: 4
output:
"{sample}/genes.fpkm_tracking"
log:
"{sample}/genes.log"
shell: """{module}; module load cufflinks
cufflinks \
--num-threads 8 \
--output-dir {wildcards.sample}/ \
--multi-read-correct \
--frag-bias-correct {input.fasta} \
--GTF {input.gtf} \
--mask-file {input.mask_gtf} \
{input.bam}
"""
rule sample_gene_ase:
input:
bam="{sample}/assigned_dmelR_dedup.bam",
bai="{sample}/assigned_dmelR_dedup.bam.bai",
variants=variants,
hets=path.join(analysis_dir, "on_mel", "melsim_true_hets.tsv"),
gtf=mel_gtf,
sentinel=path.join(analysis_dir, 'recalc_ase')
threads: 1
output:
"{sample}/melsim_gene_ase_by_read.tsv"
log:
"{sample}/melsim_gene_ase_by_read.log"
shell: """
source activate peter
export PYTHONPATH=$HOME/ASEr/;
python ~/ASEr/bin/GetGeneASEbyReads.py \
--outfile {output} \
--id-name gene_name \
--ase-function pref_index \
--min-reads-per-allele 0 \
{input.variants} \
{input.gtf} \
{input.bam}
"""
rule wasp_gene_ase:
input:
bam="{sample}/orig_dedup.keep.merged.sorted.bam",
bai="{sample}/orig_dedup.keep.merged.sorted.bam.bai",
variants=variants,
hets=path.join(analysis_dir, "on_mel", "melsim_true_hets.tsv"),
gtf=mel_gtf,
sentinel=path.join(analysis_dir, 'recalc_ase')
threads: 1
output:
"{sample}/wasp_gene_ase_by_read.tsv"
shell: """
source activate peter
export PYTHONPATH=$HOME/ASEr/;
python ~/ASEr/bin/GetGeneASEbyReads.py \
--outfile {output} \
--id-name gene_name \
--assign-all-reads \
--ase-function pref_index \
--min-reads-per-allele 0 \
{input.variants} \
{input.gtf} \
{input.bam}
"""
rule sample_cds_ase:
input:
bam="{sample}/assigned_dmelR_dedup.bam",
bai="{sample}/assigned_dmelR_dedup.bam.bai",
variants=variants,
hets=path.join(analysis_dir, "on_mel", "melsim_true_hets.tsv"),
gtf=mel_gtf,
sentinel=path.join(analysis_dir, 'recalc_ase')
threads: 1
output:
"{sample}/melsim_cds_ase_by_read.tsv"
log:
"{sample}/melsim_cds_ase_by_read.log"
shell: """
source activate peter
export PYTHONPATH=$HOME/ASEr/;
python ~/ASEr/bin/GetGeneASEbyReads.py \
--outfile {output} \
--id-name gene_name \
--ase-function pref_index \
--min-reads-per-allele 0 \
--feature-type CDS \
{input.variants} \
{input.gtf} \
{input.bam}
"""
rule sample_exon_ase:
input:
bam="{sample}/assigned_dmelR_dedup.bam",
bai="{sample}/assigned_dmelR_dedup.bam.bai",
variants=variants,
hets=path.join(analysis_dir, "on_mel", "melsim_true_hets.tsv"),
gtf='Reference/mel_renamed_exons.gtf',
sentinel=path.join(analysis_dir, 'recalc_ase')
threads: 1
output:
"{sample}/melsim_exon_ase_by_read.tsv"
log:
"{sample}/melsim_exon_ase_by_read.log"
shell: """
source activate peter
export PYTHONPATH=$HOME/ASEr/;
python ~/ASEr/bin/GetGeneASEbyReads.py \
--outfile {output} \
--id-name gene_id \
--ase-function pref_index \
--min-reads-per-allele 0 \
{input.variants} \
{input.gtf} \
{input.bam}
"""
rule exons_gtf:
input:
"Reference/{species}_good.gtf"
output:
"Reference/{species}_good_exons.gtf"
shell:"""
python2 $HOME/R/DEXSeq/python_scripts/dexseq_prepare_annotation.py \
--aggregate=yes {input} {output}
"""
rule exons_renamed:
input:
"Reference/{species}_good_exons.gtf"
output:
"Reference/{species}_renamed_exons.gtf"
run:
import Utils as ut
with open(output[0], 'w') as out:
for line in open(input[0]):
data = line.split('\t')
if data[2] != 'exonic_part': continue
annots = ut.parse_annotation(data[-1])
data[-1] = 'gene_id "{}_{}"'.format(annots['gene_id'], annots['exonic_part_number'])
data[2] = 'exon'
print(*data, file=out, sep='\t')
rule sample_psi:
input:
bam="{sample}/assigned_dmelR_dedup.bam",
bai="{sample}/assigned_dmelR_dedup.bam.bai",
gtf="Reference/mel_good_exons.gtf",
sentinel=path.join(analysis_dir, 'recalc_psi')
output:
"{sample}/psi.tsv"
shell:""" {module}; module load fraserconda
python CalculatePSI.py \
--outfile {output} \
{input.bam} {input.gtf}
"""
rule sample_juncs:
input:
bam="{analysis_dir}/{sample}/assigned_dmelR_dedup.bam",
bai="{analysis_dir}/{sample}/assigned_dmelR_dedup.bam.bai",
output:
"{analysis_dir}/velvetant/{sample}.junc"
shell:"""
~/leafcutter/scripts/bam2junc.sh {input.bam} {output}
"""
rule all_sample_juncs:
input: lambda wildcards: [path.join(analysis_dir, 'velvetant', sample +'.junc') for sample in samples]
output: 'analysis/velvetant/juncfiles.txt'
run:
with open(output[0], 'w') as out:
print(*input, sep='\n', file=out)
rule leafcutter_cluster:
input:
juncs='analysis/velvetant/juncfiles.txt',
firstbam=samples_to_files('assigned_dmelR_dedup.bam')()[0],
output:
'analysis/velvetant/clusters_perind.counts.gz'
shell: """
{module}; module load fraserconda
python ~/leafcutter-official/clustering/leafcutter_cluster.py \
-j {input.juncs} \
-o clusters \
--example-bam {input.firstbam} \
--rundir analysis/velvetant/
"""
rule sample_velvetant:
input:
bam="{sample}/assigned_dmelR_dedup.bam",
bai="{sample}/assigned_dmelR_dedup.bam.bai",
juncs="analysis/velvetant/clusters_perind.counts.gz",
snps="analysis_godot/on_mel/melsim_variant.bed",
output:
"{sample}/velvetant.tsv"
shell: """
{module}; module load fraserconda
python VelvetAnt.py -x --snps-bed {input.snps} --splicing-clusters {input.juncs} -o {output} {input.bam}
"""
rule get_all_map_stats:
input: *samples_to_files('assigned_dmelR.mapstats')(),
output:
path.join(analysis_dir, 'map_stats.tsv'),
log:
path.join(analysis_dir, 'map_stats.log'),
shell:"""
{module}; module load fraserconda;
python GetMapStats.py \
--params Parameters/RunConfig.cfg \
--count-unique \
--count-all \
--translate-labels \
{analysis_dir}
"""
rule get_sample_mapstats:
input:
unpack(getreads(1)),
unpack(getreads(2)),
bam="{sample}/{fname}.bam",
bai="{sample}/{fname}.bam.bai",
output: "{sample}/{fname}.mapstats"
log: "{sample}/mapstats.log"
shell: "{module}; module load fraserconda; python GetSingleMapStats.py {input.bam}"
rule snp_counts:
input:
bam="{sample}/assigned_dmelR_dedup.bam",
variants=path.join(analysis_dir, "on_{parent}", "{parent}{other}_variant.bed"),
output:
"{sample}/{parent}{other}_SNP_COUNTS.txt"
wildcard_constraints:
parent='[a-z][a-z][a-z]',
other='[a-z][a-z][a-z]',
shell:"""
{module}; module load samtools
mkdir -p {wildcards.sample}/melsim_countsnpase_tmp
python2 CountSNPASE.py \
--mode single \
--reads {input.bam} \
--snps {input.variants} \
--prefix {wildcards.sample}/melsim_countsnpase_tmp/
mv {wildcards.sample}/melsim_countsnpase_tmp/_SNP_COUNTS.txt {output}
rm -rf {wildcards.sample}/melsim_countsnpase_tmp
"""
rule true_hets:
input:
*samples_to_files('{parent}{other}_SNP_COUNTS.txt')(),
output:
path.join(analysis_dir, "on_{parent}", "{parent}{other}_true_hets.tsv")
shell: """
{module}; module load fraserconda
python GetTrueHets.py \
--min-counts 10 \
--outfile {output} \
{input}
cp {output} `dirname {output}`/true_hets.tsv
"""
rule kallisto_summary:
input:
*samples_to_files('abundance.tsv')(),
map_stats=path.join(analysis_dir, 'map_stats.tsv'),
sentinel=path.join(analysis_dir, 'retabulate'),
output:
path.join(analysis_dir, 'summary_kallisto.tsv')
log:
path.join(analysis_dir, 'mst.log')
shell: """
{module}; module load fraserconda;
python MakeSummaryTable.py \
--strip-low-reads 1000000 \
--strip-on-unique \
--strip-as-nan \
--mapped-bamfile assigned_dmelR.bam \
--strip-low-map-rate 52 \
--map-stats {input.map_stats} \
--filename abundance.tsv \
--key target_id \
--out-basename summary_kallisto \
--column tpm \
{analysis_dir} \
| tee {log}
"""
rule expr_summary:
input:
*samples_to_files('genes.fpkm_tracking')(),
map_stats=path.join(analysis_dir, 'map_stats.tsv'),
sentinel=path.join(analysis_dir, 'retabulate'),
output:
path.join(analysis_dir, 'summary.tsv')
log:
path.join(analysis_dir, 'mst.log')
shell: """
{module}; module load fraserconda;
python MakeSummaryTable.py \
--strip-low-reads 1000000 \
--strip-on-unique \
--strip-as-nan \
--mapped-bamfile assigned_dmelR.bam \
--strip-low-map-rate 52 \
--map-stats {input.map_stats} \
--filename genes.fpkm_tracking \
--key gene_short_name \
--column FPKM \
{analysis_dir} \
| tee {log}
"""
rule ase_summary:
input:
*samples_to_files('melsim_gene_ase_by_read.tsv')(),
map_stats=path.join(analysis_dir, 'map_stats.tsv'),
sentinel=path.join(analysis_dir, 'retabulate'),
output:
path.join(analysis_dir, 'ase_summary_by_read.tsv')
log:
path.join(analysis_dir, 'ase_mst.log')
shell: """
{module}; module load fraserconda;
python MakeSummaryTable.py \
--strip-low-reads 1000000 \
--strip-on-unique \
--strip-as-nan \
--mapped-bamfile assigned_dmelR.bam \
--strip-low-map-rate 52 \
--map-stats {input.map_stats} \
--filename melsim_gene_ase_by_read.tsv \
--key gene \
--column ase_value \
--out-basename ase_summary_by_read \
{analysis_dir} \
| tee {log}
"""
rule ase_summary_refalt:
input:
*samples_to_files('wasp_gene_ase_by_read.tsv')(),
map_stats=path.join(analysis_dir, 'map_stats.tsv'),
sentinel=path.join(analysis_dir, 'retabulate'),
output:
path.join(analysis_dir, 'ase_summary_refalt.tsv')
shell: """
{module}; module load fraserconda;
python MakeSummaryTable.py \
--strip-low-reads 1000000 \
--strip-on-unique \
--strip-as-nan \
--mapped-bamfile assigned_dmelR.bam \
--strip-low-map-rate 52 \
--map-stats {input.map_stats} \
--filename wasp_gene_ase_by_read.tsv \
--key gene \
--refalt \
--float-format %3.0f \
--exclude-column chrom \
--exclude-column-value dmel_X \
--exclude-samples melXsim_cyc14C_rep3 simXmel_cyc14C_rep2 \
--column ref_counts \
--out-basename ase_summary_refalt \
{analysis_dir}
"""
rule ase_summary_wasp:
input:
*samples_to_files('wasp_gene_ase_by_read.tsv')(),
map_stats=path.join(analysis_dir, 'map_stats.tsv'),
sentinel=path.join(analysis_dir, 'retabulate'),
output:
path.join(analysis_dir, 'wasp_summary_by_read.tsv')
log:
path.join(analysis_dir, 'ase_mst.log')
shell: """
{module}; module load fraserconda;
python MakeSummaryTable.py \
--strip-low-reads 1000000 \
--strip-on-unique \
--strip-as-nan \
--mapped-bamfile assigned_dmelR.bam \
--strip-low-map-rate 52 \
--map-stats {input.map_stats} \
--filename wasp_gene_ase_by_read.tsv \
--key gene \
--column ase_value \
--out-basename wasp_summary_by_read \
{analysis_dir} \
| tee {log}
"""
rule cds_ase_summary:
input:
*samples_to_files('melsim_cds_ase_by_read.tsv')(),
map_stats=path.join(analysis_dir, 'map_stats.tsv'),
sentinel=path.join(analysis_dir, 'retabulate'),
output:
path.join(analysis_dir, 'cds_ase_summary_by_read.tsv')
log:
path.join(analysis_dir, 'cds_mst.log')
shell: """
{module}; module load fraserconda;
python MakeSummaryTable.py \
--strip-low-reads 1000000 \
--strip-on-unique \
--strip-as-nan \
--mapped-bamfile assigned_dmelR.bam \
--strip-low-map-rate 52 \
--map-stats {input.map_stats} \
--filename melsim_cds_ase_by_read.tsv \
--key gene \
--column ase_value \
--out-basename cds_ase_summary_by_read \
{analysis_dir} \
| tee {log}
"""
rule exons_ase_summary:
input:
*samples_to_files('melsim_exon_ase_by_read.tsv')(),
map_stats=path.join(analysis_dir, 'map_stats.tsv'),
sentinel=path.join(analysis_dir, 'retabulate'),
output:
path.join(analysis_dir, 'exon_ase_summary_by_read.tsv')
log:
path.join(analysis_dir, 'exon_mst.log')
shell: """
{module}; module load fraserconda;
python MakeSummaryTable.py \
--strip-low-reads 1000000 \
--strip-on-unique \
--strip-as-nan \
--mapped-bamfile assigned_dmelR.bam \
--strip-low-map-rate 52 \
--map-stats {input.map_stats} \
--filename melsim_exon_ase_by_read.tsv \
--key gene \
--column ase_value \
--out-basename exon_ase_summary_by_read \
{analysis_dir} \
| tee {log}
"""
rule psi_summary:
input:
*samples_to_files('psi.tsv')(),
map_stats=path.join(analysis_dir, 'map_stats.tsv'),
sentinel=path.join(analysis_dir, 'retabulate'),
output:
path.join(analysis_dir, 'psi_summary.tsv')
log:
path.join(analysis_dir, 'psi_mst.log')
shell: """
{module}; module load fraserconda;
python MakeSummaryTable.py \
--strip-low-reads 1000000 \
--strip-on-unique \
--strip-as-nan \
--mapped-bamfile assigned_dmelR.bam \
--strip-low-map-rate 52 \
--map-stats {input.map_stats} \
--filename psi.tsv \
--key exon_id \
--column psi \
--out-basename psi_summary \
{analysis_dir} \
| tee {log}
"""
rule velvetant_summary:
input:
*samples_to_files('velvetant.tsv')(),
map_stats=path.join(analysis_dir, 'map_stats.tsv'),
sentinel=path.join(analysis_dir, 'retabulate'),
output:
path.join(analysis_dir, 'velvetant_summary.tsv')
log:
path.join(analysis_dir, 'velvetant_mst.log')
shell: """
{module}; module load fraserconda;
python MakeSummaryTable.py \
--strip-low-reads 1000000 \
--strip-on-unique \
--strip-as-nan \
--mapped-bamfile assigned_dmelR.bam \
--strip-low-map-rate 52 \
--map-stats {input.map_stats} \
--filename velvetant.tsv \
--key 0 \
--column pref_index \
--out-basename velvetant_summary \
{analysis_dir} \
| tee {log}
"""
rule sort_bam:
input: "{sample}.bam"
output: "{sample}.sorted.bam"
log: "{sample}.sorted.log"
threads: 4
shell: """{module}; module load samtools
samtools sort -o {output} --threads {threads} {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 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 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 star_map:
input:
unpack(getreads(1)),
unpack(getreads(2)),
genome=path.join(hybrid_dir, 'masked', 'Genome'),
genomedir=path.join(hybrid_dir, 'masked')
params:
r1s=getreadscomma(1),