| 1 |
from os import listdir |
| 2 |
from os.path import isfile, join |
| 3 |
|
| 4 |
in_dir = "/home/gk/analysis/intrahost/raw_reads/field_samples/zika" |
| 5 |
out_dir = "/home/gk/analysis/2018.11.22.1542874525.ZI" |
| 6 |
bed = "/home/gk/db/zika/amplicons/zika_primers.bed" |
| 7 |
ref="/home/gk/db/zika/ZIKV_RIO.fasta" |
| 8 |
index="/home/gk/db/zika/ZIKV_RIO" |
| 9 |
pair_information="/home/gk/db/zika/amplicons/pair_information.tsv" |
| 10 |
|
| 11 |
primer_csv = "/home/gk/db/zika/amplicons/zika_primers.csv" |
| 12 |
|
| 13 |
|
| 14 |
#in_dir = "/home/gk/analysis/intrahost/raw_reads/field_samples/wnv" |
| 15 |
|
| 16 |
#bed = "/home/gk/db/wnv/WNV_400.bed" |
| 17 |
#ref="/home/gk/db/wnv/JF703162.fa" |
| 18 |
#index="/home/gk/db/wnv/JF703162" |
| 19 |
#pair_information="/home/gk/db/wnv/pair_information.tsv" |
| 20 |
|
| 21 |
#primer_csv = "/home/gk/db/wnv/wnv_primers.csv" |
| 22 |
|
| 23 |
# File name format of replicates - ZI26_a_R{1-2}.fastq.gz ZI26_b_R{1-2}.fastq.gz |
| 24 |
lst = listdir(in_dir) |
| 25 |
files = [f for f in lst if isfile(join(in_dir, f)) and ".fastq.gz" in f] |
| 26 |
files.sort() |
| 27 |
|
| 28 |
SAMPLES = [] |
| 29 |
for i in files: |
| 30 |
SAMPLES.append(i.split("_")[0]) |
| 31 |
SAMPLES = list(set(SAMPLES)) |
| 32 |
|
| 33 |
SAMPLES=SAMPLES[:2] |
| 34 |
|
| 35 |
REPS=["a","b"] |
| 36 |
|
| 37 |
|
| 38 |
rule all: |
| 39 |
input: |
| 40 |
expand("{out_dir}/masked_variants/{sample}.filtered.tsv", out_dir = out_dir, sample = SAMPLES) |
| 41 |
|
| 42 |
rule filter_variants: |
| 43 |
input: |
| 44 |
expand("{{out_dir}}/masked_variants/{{sample}}_{rep}.tsv", rep=REPS) |
| 45 |
output: |
| 46 |
"{out_dir}/masked_variants/{sample}.filtered.tsv" |
| 47 |
shell: |
| 48 |
""" |
| 49 |
ivar filtervariants -p {output} {input} |
| 50 |
""" |
| 51 |
|
| 52 |
rule call_variants_post_removal: |
| 53 |
input: |
| 54 |
"{out_dir}/masked/{sample}_{rep}.masked.sorted.bam", |
| 55 |
"{out_dir}/consensus/{sample}.fa" |
| 56 |
output: |
| 57 |
"{out_dir}/masked_variants/{sample}_{rep}.tsv" |
| 58 |
shell: |
| 59 |
""" |
| 60 |
samtools mpileup -A -d 0 --reference {input[1]} -Q 0 -F 0 {input[0]} | ivar variants -p {output} -t 0.03 |
| 61 |
""" |
| 62 |
|
| 63 |
rule remove_reads: |
| 64 |
input: |
| 65 |
"{out_dir}/realigned/{sample}_{rep}.realigned.trimmed.sorted.bam", |
| 66 |
"{out_dir}/{sample}_{rep}_masked_primer_names.txt", |
| 67 |
"{out_dir}/bed/{sample}.bed" |
| 68 |
output: |
| 69 |
"{out_dir}/masked/{sample}_{rep}.masked.sorted.bam" |
| 70 |
shell: |
| 71 |
""" |
| 72 |
mkdir -p $(dirname {output})/ |
| 73 |
ivar removereads -i {input[0]} -p {wildcards.out_dir}/masked/{wildcards.sample}_{wildcards.rep}.masked -t {input[1]} -b {input[2]} |
| 74 |
samtools sort -T {wildcards.sample}_{wildcards.rep}_remove_reads -o {output} {wildcards.out_dir}/masked/{wildcards.sample}_{wildcards.rep}.masked.bam |
| 75 |
samtools index {output} |
| 76 |
""" |
| 77 |
|
| 78 |
rule get_masked: |
| 79 |
input: |
| 80 |
"{out_dir}/primer_mismatches/{sample}.tsv", |
| 81 |
"{out_dir}/bed/{sample}.bed", |
| 82 |
"{pair_information}".format(pair_information = pair_information) |
| 83 |
output: |
| 84 |
"{out_dir}/{sample}_{rep}_masked_primer_names.txt" |
| 85 |
shell: |
| 86 |
"ivar getmasked -i {input[0]} -b {input[1]} -f {input[2]} -p {output}" |
| 87 |
|
| 88 |
rule call_variants_in_primer: |
| 89 |
input: |
| 90 |
"{out_dir}/bed/{sample}.bam", |
| 91 |
"{out_dir}/consensus/{sample}.fa" |
| 92 |
output: |
| 93 |
"{out_dir}/primer_mismatches/{sample}.tsv" |
| 94 |
shell: |
| 95 |
"samtools mpileup -A -d 0 --reference {input[1]} -Q 0 -F 0 {input[0]} | ivar variants -p {output} -t 0.03" |
| 96 |
|
| 97 |
rule realigned_trim_primer_quality: |
| 98 |
input: |
| 99 |
"{out_dir}/realigned/{sample}_{rep}.realigned.sorted.bam", |
| 100 |
"{out_dir}/bed/{sample}.bed" |
| 101 |
output: |
| 102 |
"{out_dir}/realigned/{sample}_{rep}.realigned.trimmed.sorted.bam", |
| 103 |
shell: |
| 104 |
""" |
| 105 |
mkdir -p $(dirname {output})/ |
| 106 |
ivar trim -b {input[1]} -p {wildcards.out_dir}/realigned/{wildcards.sample}_{wildcards.rep}.realigned.trimmed -i {input[0]} |
| 107 |
samtools sort -T {wildcards.sample}_{wildcards.rep}_retrim -o {output} {wildcards.out_dir}/realigned/{wildcards.sample}_{wildcards.rep}.realigned.trimmed.bam |
| 108 |
samtools index {output} |
| 109 |
""" |
| 110 |
|
| 111 |
rule create_bed: |
| 112 |
input: |
| 113 |
"{out_dir}/bed/{sample}.bam" |
| 114 |
output: |
| 115 |
"{out_dir}/bed/{sample}.bed" |
| 116 |
shell: |
| 117 |
""" |
| 118 |
bedtools bamtobed -i {input} > {output} |
| 119 |
""" |
| 120 |
|
| 121 |
rule create_primer_bam: |
| 122 |
input: |
| 123 |
"{out_dir}/index/{sample}.bwt", |
| 124 |
"{out_dir}/bed/primers.fa" |
| 125 |
output: |
| 126 |
"{out_dir}/bed/{sample}.bam" |
| 127 |
params: |
| 128 |
index= "{out_dir}/index/{sample}" |
| 129 |
shell: |
| 130 |
""" |
| 131 |
bwa mem -k 5 -T 16 {params.index} {input[1]} | samtools view -bS -F 4 -o {output} |
| 132 |
""" |
| 133 |
|
| 134 |
rule create_primer_fasta: |
| 135 |
input: |
| 136 |
"{csv}".format(csv = primer_csv) |
| 137 |
output: |
| 138 |
"{out_dir}/bed/primers.fa" |
| 139 |
shell: |
| 140 |
"cut -f 1,2 -d ',' {input} | sed 's/^/>/g' | tr ',' '\n' > {output}" |
| 141 |
|
| 142 |
rule align_to_consensus: |
| 143 |
input: |
| 144 |
"{out_dir}/index/{sample}.bwt", |
| 145 |
expand("{in_dir}/{{sample}}_{{rep}}_R{readno}.fastq.gz", readno = [1,2], in_dir = in_dir) |
| 146 |
output: |
| 147 |
"{out_dir}/realigned/{sample}_{rep}.realigned.sorted.bam" |
| 148 |
shell: |
| 149 |
""" |
| 150 |
bwa mem {wildcards.out_dir}/index/{wildcards.sample} {input[1]} {input[2]} | samtools view -F 4 -Sb | samtools sort -T {wildcards.sample}_{wildcards.rep}_align_consensus -o {output} |
| 151 |
samtools index {output} |
| 152 |
""" |
| 153 |
|
| 154 |
rule call_consensus: |
| 155 |
input: |
| 156 |
"{out_dir}/merged_bams/{sample}.merged.bam" |
| 157 |
output: |
| 158 |
"{out_dir}/consensus/{sample}.fa", |
| 159 |
"{out_dir}/index/{sample}.bwt" |
| 160 |
shell: |
| 161 |
""" |
| 162 |
samtools mpileup -A -d 0 -Q 0 -F 0 {input} | ivar consensus -p {output[0]} |
| 163 |
bwa index -p {wildcards.out_dir}/index/{wildcards.sample} {output[0]} |
| 164 |
""" |
| 165 |
|
| 166 |
rule merge_replicates: |
| 167 |
input: |
| 168 |
expand("{{out_dir}}/trimmed/{{sample}}_{rep}.trimmed.sorted.bam", rep=REPS) |
| 169 |
output: |
| 170 |
"{out_dir}/merged_bams/{sample}.merged.bam" |
| 171 |
shell: |
| 172 |
""" |
| 173 |
mkdir -p $(dirname {output}/) |
| 174 |
samtools merge {output} {input} |
| 175 |
""" |
| 176 |
|
| 177 |
rule trim_primer_quality: |
| 178 |
input: |
| 179 |
"{out_dir}/aligned/{sample}_{rep}.sorted.bam", |
| 180 |
"{b}".format(b = bed) |
| 181 |
output: |
| 182 |
"{out_dir}/trimmed/{sample}_{rep}.trimmed.sorted.bam" |
| 183 |
shell: |
| 184 |
""" |
| 185 |
mkdir -p {wildcards.out_dir}/trimmed/ |
| 186 |
ivar trim -b {input[1]} -p {wildcards.out_dir}/trimmed/{wildcards.sample}_{wildcards.rep}.trimmed -i {input[0]} |
| 187 |
samtools sort -T {wildcards.sample}_{wildcards.rep}_trim -o {output} {wildcards.out_dir}/trimmed/{wildcards.sample}_{wildcards.rep}.trimmed.bam |
| 188 |
samtools index {output} |
| 189 |
""" |
| 190 |
|
| 191 |
rule align_reads: |
| 192 |
input: |
| 193 |
expand("{in_dir}/{{sample}}_{{rep}}_R{num}.fastq.gz", in_dir = in_dir, num=[1,2]) |
| 194 |
params: |
| 195 |
index="{i}".format(i = index) |
| 196 |
output: |
| 197 |
"{out_dir}/aligned/{sample}_{rep}.sorted.bam" |
| 198 |
shell: |
| 199 |
""" |
| 200 |
mkdir -p $(dirname {output})/ |
| 201 |
bwa mem {params.index} {input[0]} {input[1]} | samtools view -F 4 -Sb | samtools sort -T {wildcards.sample}_{wildcards.rep}_align -o {output} |
| 202 |
samtools index {output} |
| 203 |
""" |