-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathSnakefile
175 lines (155 loc) · 5.1 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
# Split parameters
SPLIT_OUT="split-out"
LINES_PER_FILE=os.environ.get("LINES_PER_FILE", "40000")
# aln parameters
MAP_IM_OUT="map-intermediate-out"
# merge parameters
MERGE_OUT="map"
# picard location
PICARD_DIR="/bubo/home/h16/inod/src/picard-tools-1.89"
# Given there's a pair1.fastq and pair2.fastq, split up all reads
rule all:
message: """(1) Splitting up pair1.fastq and pair2.fastq for parallelization (use --cluster)
(2) map them to ref.fa
(3) sort bam and get coverage info
"""
input: MERGE_OUT + "/pair_ref-s-md-s.bam", MERGE_OUT + "/pair_ref-s-md-s.bam.bai",
MERGE_OUT + "/pair_ref-s-md-s.coverage"
rule merge_all:
input: MERGE_OUT + "/pair_ref.bam"
rule split_all:
input: dynamic(SPLIT_OUT + "/pair1.fastq.split.{splitid}"), dynamic(SPLIT_OUT + "/pair2.fastq.split.{splitid}")
rule split1:
input: "pair1.fastq"
params: lines_per_file=str(LINES_PER_FILE), out_dir=SPLIT_OUT
output: dynamic(SPLIT_OUT + "/pair1.fastq.split.{splitid}")
shell: """
mkdir -p {params.out_dir}
split -d -l {params.lines_per_file} {input[0]} {params.out_dir}/pair1.fastq.split.
"""
rule split2:
input: "pair2.fastq"
params: lines_per_file=str(LINES_PER_FILE), out_dir=SPLIT_OUT
output: dynamic(SPLIT_OUT + "/pair2.fastq.split.{splitid}")
shell: """
mkdir -p {params.out_dir}
split -d -l {params.lines_per_file} {input[0]} {params.out_dir}/pair2.fastq.split.
"""
rule bwt:
input: "ref.fa"
output: "ref.fa.bwt"
log: "ref.fa.bwt.sm.log"
shell: """
bwa index {input}
"""
rule aln:
input: pairn=SPLIT_OUT + "/pair{pair_nr}.fastq.split.{id}", refbwt="ref.fa.bwt", ref="ref.fa"
threads: 8
params: out_dir=MAP_IM_OUT
output: MAP_IM_OUT + "/pair{pair_nr}.fastq.split.{id}.sai"
log: MAP_IM_OUT + "/pair{pair_nr}.fastq.split.{id}.sai.sm.log"
shell: """
mkdir -p {params.out_dir}
bwa aln -t {threads} {input.ref} -{wildcards.pair_nr} {input.pairn} > {output}
"""
rule sampe:
input: sai1=MAP_IM_OUT + "/pair1.fastq.split.{id}.sai", sai2=MAP_IM_OUT + "/pair2.fastq.split.{id}.sai", pair1=SPLIT_OUT + "/pair1.fastq.split.{id}", pair2=SPLIT_OUT + "/pair2.fastq.split.{id}", refbwt="ref.fa.bwt", ref="ref.fa"
params: out_dir=MAP_IM_OUT
output: MAP_IM_OUT + "/pair.fastq.split.{id}.sam"
log: MAP_IM_OUT + "/pair.fastq.split.{id}.sam.sm.log"
shell: """
mkdir -p {params.out_dir}
bwa sampe {input.ref} {input.sai1} {input.sai2} {input.pair1} {input.pair2} > {output}
"""
rule samtobam:
input: "{name}.sam"
output: "{name}.bam"
log: "{name}.bam.sm.log"
shell: """
samtools view -bS {input} > {output}
"""
rule merge:
input: dynamic(MAP_IM_OUT + "/pair.fastq.split.{splitid}.bam")
params: out_dir=MERGE_OUT
output: MERGE_OUT + "/pair_ref.bam"
log: MERGE_OUT + "/pair_ref.bam.sm.log"
shell: """
mkdir -p {params.out_dir}
samtools merge {output} {input}
"""
rule sort:
input: "{name}.bam"
output: "{name}-s.bam"
log: "{name}-s.bam.sm.log"
params: srtsam_jar=PICARD_DIR + "/SortSam.jar"
shell: """
java -jar {params.srtsam_jar} \
INPUT={input} \
OUTPUT={output} \
SORT_ORDER=coordinate \
VALIDATION_STRINGENCY=SILENT
"""
# Removes reads mapping to the exact same location, probably duplicates
rule removeduplicates:
input: "{name}-s.bam"
output: "{name}-s-md.bam", "{name}-s-md.metrics"
log: "{name}-s-md.bam.sm.log"
params: mrkdup_jar=PICARD_DIR + "/MarkDuplicates.jar"
shell: """
java -jar {params.mrkdup_jar} \
INPUT={input} \
OUTPUT={output[0]} \
METRICS_FILE={output[1]} \
MAX_FILE_HANDLES_FOR_READ_ENDS_MAP=1000 \
REMOVE_DUPLICATES=TRUE \
VALIDATION_STRINGENCY=SILENT
"""
# Gets rid of the MAPQ should be 0 warning
rule cleansam:
input: "{name}.bam"
output: "{name}-c.bam"
log: "{name}-c.bam.sm.log"
params: clnsam_jar=PICARD_DIR + "/CleanSam.jar"
shell: """
java -jar {params.clnsam_jar} \
INPUT={input} \
OUTPUT={output} \
QUIET=TRUE \
"""
# Create index file for fast bam access
rule index:
input: "{name}.bam"
output: "{name}.bam.bai"
log: "{name}.bam.bai.sm.log"
shell: """
samtools index {input}
"""
# Use BEDTools to get coverage of bam file
rule coverage:
input: "{name}.bam"
output: "{name}.coverage"
shell: """
genomeCoverageBed -ibam {input} > {output}
"""
# Calculate mean coverage per contig given BEDTools output
# TODO: doesn't work, {} should be escaped somehow
rule mean_coverage_per_contig:
input: "{name}.coverage"
output: "{name}.mean.coverage.per.contig"
shell: """
awk 'BEGIN {pc=""}
{
c=$1;
if (c == pc) {
cov=cov+$2*$5;
} else {
print pc,cov;
cov=$2*$5;
pc=c
}
} END {print pc,cov}' {input} | tail -n +2 > {output}
"""
rule clean:
run:
shell("rm -rf " + " ".join([SPLIT_OUT, MAP_IM_OUT, MERGE_OUT]))
shell("rm -rf " + " ".join(expand("ref.fa.{ext}",ext=["bwt","pac","fai","sa","amb","ann"])))