-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnakefile
140 lines (124 loc) · 4 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
import pandas as pd
import os
for directory in ['fastqc', 'fastqc_post_trim', 'trim', 'logs', 'logs/slurm_reports', 'logs/trim_reports', 'alignment', 'alignment/frag_len', 'logs/alignment_reports', 'peaks', 'logs/MACS2']:
if not os.path.isdir(directory):
os.mkdir(directory)
configfile: "config.yaml"
sample_file = config["sample_file"]
genome = config["genome"]
table = pd.read_table(sample_file)
sample = table['Sample']
replicate = table['Replicate']
condition = table['Condition']
Antibody = table['Antibody']
File_R1 = table['File_Name_R1']
File_R2 = table['File_Name_R2']
File_names = File_R1.append(File_R2)
sample_ids = []
for i in range(len(sample)):
sample_ids.append('%s_%s_%s' % (sample[i], condition[i], replicate[i]))
sample_ids = pd.unique(sample_ids).tolist()
sample_ids_file = []
for i in range(len(sample)):
sample_ids_file.append('%s_%s_%s_%s' % (sample[i], condition[i], replicate[i], Antibody[i]))
read = ['_R1', '_R2']
rule all:
input:
expand('fastqc/{sample_file}{read}_fastqc.html', sample_file = sample_ids_file, read = read),
expand('fastqc_post_trim/{sample_file}_trimmed{read}_fastqc.html', sample_file = sample_ids_file, read = read),
expand('peaks/{sample}_peaks.narrowPeak', sample = sample_ids),
'FRP.txt',
expand('alignment/{sample}_sorted.bam', sample = sample_ids_file),
expand('alignment/{sample}_sorted.bam.bai', sample = sample_ids_file),
expand('alignment/frag_len/{sample}.txt', sample = sample_ids_file)
rule fastqc:
input:
fastq = "fastq/{sample}{read}.fastq.gz"
output:
"fastqc/{sample}{read}_fastqc.html",
params:
'fastqc/'
shell:
'fastqc {input.fastq} -o {params}'
rule fastqc_post_trim:
input:
fastq = "trim/{sample}{read}.fastq.gz"
output:
"fastqc_post_trim/{sample}{read}_fastqc.html",
params:
'fastqc_post_trim/'
shell:
'fastqc {input.fastq} -o {params}'
rule trim:
input:
R1='fastq/{sample}_R1.fastq.gz',
R2='fastq/{sample}_R2.fastq.gz'
output:
R1='trim/{sample}_trimmed_R1.fastq.gz',
R2='trim/{sample}_trimmed_R2.fastq.gz',
html='logs/trim_reports/{sample}.html',
json='logs/trim_reports/{sample}.json'
threads: 4
log:
'logs/trim_reports/{sample}.log'
params:
'--detect_adapter_for_pe'
shell:
'fastp -w {threads} {params} -i {input.R1} -I {input.R2} -o {output.R1} -O {output.R2} --html {output.html} --json {output.json} 2> {log}'
rule align:
input:
R1='trim/{sample}_trimmed_R1.fastq.gz',
R2='trim/{sample}_trimmed_R2.fastq.gz'
output:
'alignment/{sample}.bam'
threads: 20
log:
'logs/alignment_reports/{sample}.log'
params:
'--end-to-end --very-sensitive --no-mixed --no-unal --no-discordant --phred33'
shell:
'bowtie2 {params} -x %s --threads {threads} -1 {input.R1} -2 {input.R2} 2> {log} | samtools view -bh -q 3 > alignment/{wildcards.sample}.bam' % (genome)
rule sort:
input:
'alignment/{sample}.bam'
output:
'alignment/{sample}_sorted.bam'
threads: 40
shell:
'samtools sort -@ {threads} {input} > {output}'
rule index:
input:
'alignment/{sample}_sorted.bam'
output:
'alignment/{sample}_sorted.bam.bai'
threads: 20
shell:
'samtools index -@ {threads} {input} > {output}'
rule MACS2:
input:
exp='alignment/{sample}_Antibody.bam',
con='alignment/{sample}_Control.bam'
output:
'peaks/{sample}_peaks.narrowPeak'
log:
'logs/MACS2/{sample}.log'
params:
'-B --outdir peaks/ -g 2.7e9 -q 0.05 --keep-dup auto'
shell:
'macs2 callpeak -t {input.exp} -c {input.con} {params} -n {wildcards.sample} 2> {log}'
rule fragment_size:
input:
'alignment/{sample}.bam'
output:
'alignment/frag_len/{sample}.txt'
shell:
"""
samtools view {input} | awk -F'\t' 'function abs(x){{return ((x < 0.0) ? -x : x)}} {{print abs($9)}}' | sort | uniq -c | awk -v OFS="\t" '{{print $2, $1/2}}' > {output}
"""
rule FRP:
input:
expand('peaks/{sample}_peaks.narrowPeak', sample = sample_ids)
output:
'FRP.txt'
script:
'FRP.py'