-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSnakefile
147 lines (112 loc) · 3.6 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
import pandas as pd
from collections import defaultdict
import vcf
SAMPLES = list(config['samples'].keys())
os.makedirs("logs",exist_ok=True)
rule all:
input:
expand("bam/{sample}.bam",sample=SAMPLES),
expand("snps/{sample}.snps",sample=SAMPLES),
expand("pileup/{sample}.pileup",sample=SAMPLES),
expand("calls/{sample}.calls",sample=SAMPLES),
"final.tsv"
rule bowtie:
input:
config['fastqPath']+"/{sample}.fastq.gz"
output:
"bam/{sample}.bam"
params:
threads=4,
index=config['bowtie_index'],
log="logs/bowtie_{sample}.out",
mem="4G"
shell:
"bowtie2 -p {params.threads} -x {params.index} -U {input} \
| samtools sort -m {params.mem} -@ {params.threads} | samtools markdup - {output} "
rule grep_snps_from_vcf:
input:
config['GenoVCF'],
output:
"snps/{sample}.vcf",
params:
log="logs/grep_snps_{sample}.out",
rec_id=lambda wildcards: config['samples'][wildcards.sample]['rec_id'],
donor_id=lambda wildcards: config['samples'][wildcards.sample]['donor_id'],
shell:
"bcftools query -f '%CHROM,%POS,%ID,%REF,%ALT[,%GT]\n' -s {params.rec_id},{params.donor_id} {input} > {output} "
rule make_snps:
input:
"snps/{sample}.vcf",
output:
"snps/{sample}.snps",
params:
log="logs/make_snps_{sample}.out",
rec_id=lambda wildcards: config['samples'][wildcards.sample]['rec_id'],
donor_id=lambda wildcards: config['samples'][wildcards.sample]['donor_id'],
run:
df=pd.read_csv(input[0],header=None)
chr_list=['chr%s' %x for x in list(range(1,23))+['X']]
df.columns=['chr','pos','rsid','ref','alt','GT_rec','GT_donor']
df=df.loc[ (df['GT_rec']=="0/0") | (df['GT_rec']=="1/1") ,: ]
df=df.loc[ (df['GT_donor']=="0/0") | (df['GT_donor']=="1/1") ,: ]
df.loc[df['GT_rec']=="0/0",'rec_base']=df.loc[df['GT_rec']=="0/0",'ref']
df.loc[df['GT_rec']=="1/1",'rec_base']=df.loc[df['GT_rec']=="1/1",'alt']
df.loc[df['GT_donor']=="0/0",'donor_base']=df.loc[df['GT_donor']=="0/0",'ref']
df.loc[df['GT_donor']=="1/1",'donor_base']=df.loc[df['GT_donor']=="1/1",'alt']
df=df.loc[df['chr'].isin(chr_list),:]
df.to_csv(output[0],sep='\t',index=False)
shell("rm {input} ")
rule mileup:
input:
bam="bam/{sample}.bam",
snps="snps/{sample}.snps"
output:
"pileup/{sample}.pileup",
params:
log="logs/mpileup_{sample}.out",
ref=config['human_fasta'],
samtools018="/home/tunci/tools/samtools-0.1.18/samtools"
shell:
"awk 'NR >1 {{print $1,$2}}' {input.snps} > {output}.pos.tmp ;"
"samtools mpileup -O -s -f {params.ref} -l {output}.pos.tmp {input.bam} > {output} ;"
"rm {output}.pos.tmp "
rule call_cfdna:
input:
pileup="pileup/{sample}.pileup",
snps="snps/{sample}.snps",
output:
"calls/{sample}.calls",
params:
log="logs/make_snps_{sample}.out",
rec_id=lambda wildcards: config['samples'][wildcards.sample]['rec_id'],
donor_id=lambda wildcards: config['samples'][wildcards.sample]['donor_id'],
bq_filter=config['bq_filter'],
mq_filter=config['mq_filter'],
ref_snps=config['ref_snp'],
filter_snps=config['filter_snps']
shell:
"python {config[binPath]}/call_cfdna.py "
"--snps {input.snps} "
"--rec-id {params.rec_id} "
"--sampleid {wildcards.sample} "
"--donor-id {params.donor_id} "
"--pileup {input.pileup} "
"--bq {params.bq_filter} "
"--mq {params.mq_filter} "
"--ref_snps {params.ref_snps} "
"--filter_snps {params.filter_snps} "
"--out {output} "
rule final_report:
input:
expand("calls/{sample}.calls",sample=SAMPLES)
output:
"final.tsv"
run:
dfm=None
for f in input:
df=pd.read_csv(f,sep='\t')
if dfm is None:
dfm=df
continue
dfm=dfm.append(df)
dfm.to_csv(output[0],sep='\t')