-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnakefile
138 lines (119 loc) · 3.84 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
import os
import pandas as pd
# Check that each patient has 2 bams (normal + tumor)
def check_samples(df):
res = df.groupby('patient').size()
passCountCheck = res.loc[res != 2].shape[0] == 0
L = ['normal', 'tumor']
s = set(L)
out = df.groupby('patient')['sample_type'].apply(lambda x: s.issubset(x))
passSampleCheck = out.sum() == out.shape[0]
return passCountCheck and passSampleCheck
CHROMOSOMES = ['chr' + str(i) for i in range(1, 22)] + ['chrX', 'chrY']
configfile: 'config.yaml'
# Sample Info
samples = pd.read_csv(config['samples'])
if not check_samples(samples):
raise ValueError("Your samples CSV is not right! Make sure each patient has a normal and tumor bam")
samples = samples.set_index(['patient', 'sample_type'], drop = False)
## Reference Files
ref_fasta = config['ref_fasta']
## Target Capture Region File
regions = config['capture_regions']
# Logs
slurm_logdir = config['slurm_log_dir']
logpath = Path(slurm_logdir)
logpath.mkdir(parents=True, exist_ok=True)
## CNVKit threads
cnvkit_threads = config['cnvkit_threads']
## Containers
sequenza_env = config['sequenza_env']
cnvkit_env = config['cnvkit_env']
def get_bams(wildcards):
return {'normal' : samples.loc[(wildcards.patient, 'normal'), 'bam'],
'tumor' : samples.loc[(wildcards.patient, 'tumor'), 'bam']}
def get_normal_bams():
return samples.loc[samples['sample_type'] == 'normal', 'bam'].tolist()
def get_tumor_bams():
return samples.loc[samples['sample_type'] == 'tumor', 'bam'].tolist()
rule targets:
input:
"results/sequenza_info.csv",
"cnvkit-results"
rule make_GC_wiggle:
input:
ref_fasta
output:
"hg38.gc50Base.wig.gz"
singularity: sequenza_env
shell:
"""
sequenza-utils gc_wiggle --fasta {input} -w 50 -o {output}
"""
rule process_bam:
input:
unpack(get_bams),
fasta=ref_fasta,
GC="hg38.gc50Base.wig.gz"
output:
seqz="sequenza/{patient}.seqz.gz",
tbi="sequenza/{patient}.seqz.gz.tbi",
small="sequenza/{patient}.small.seqz.gz",
smalltbi="sequenza/{patient}.small.seqz.gz.tbi"
params:
chroms='-C ' + ' '.join(CHROMOSOMES)
singularity: sequenza_env
shell:
"""
sequenza-utils bam2seqz -n {input.normal} -t {input.tumor} --fasta {input.fasta} \
-gc {input.GC} -o {output.seqz} {params.chroms}
sequenza-utils seqz_binning --seqz {output.seqz} -w 50 -o {output.small}
"""
rule sequenza:
input:
seqz="sequenza/{patient}.small.seqz.gz",
tbi="sequenza/{patient}.small.seqz.gz.tbi"
output:
outdir=directory("sequenza/{patient}-results/"),
estimates="sequenza/{patient}-results/{patient}.small.se_confints_CP.txt"
singularity: sequenza_env
script:
"scripts/sequenza.R"
rule gather_info:
input:
expand("sequenza/{patient}-results/{patient}.small.se_confints_CP.txt", patient=samples.patient.unique())
output:
"results/sequenza_info.csv"
singularity: sequenza_env
script:
"scripts/gather_info.R"
rule make_access:
input:
ref_fasta
output:
"access.5kb.hg38.bed"
singularity: cnvkit_env
shell:
"""
cnvkit.py access {input} -o {output}
"""
rule cnvkit:
input:
normals=get_normal_bams(),
tumors=get_tumor_bams(),
targets=regions,
ref=ref_fasta,
access="access.5kb.hg38.bed"
output:
directory('cnvkit-results')
singularity: cnvkit_env
threads: cnvkit_threads
shell:
"""
cnvkit.py batch {input.tumors} --normal {input.normals} \
--targets {input.targets} --fasta {input.ref} \
--access {input.access} \
--output-dir {output} \
--output-reference cnvkit_reference.cnn \
-p {threads}
"""