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