Newer
Older
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
import math
from collections import defaultdict
configfile: "Snake.config.yaml"
import os, sys
# print(os.listdir(os.getcwd()))
# print(os.listdir("bam"))
SAMPLE,BAM = glob_wildcards(config['input_bam_location'] + "{sample}/selected/{bam}.bam")
print(SAMPLE)
print(BAM)
SAMPLES = sorted(set(SAMPLE))
print(SAMPLES)
from pprint import pprint
CELL_PER_SAMPLE= defaultdict(list)
BAM_PER_SAMPLE = defaultdict(list)
for sample,bam in zip(SAMPLE,BAM):
BAM_PER_SAMPLE[sample].append(bam)
CELL_PER_SAMPLE[sample].append(bam.replace('.sort.mdup',''))
pprint(BAM_PER_SAMPLE)
pprint(CELL_PER_SAMPLE)
ALLBAMS_PER_SAMPLE = defaultdict(list)
for sample in SAMPLES:
ALLBAMS_PER_SAMPLE[sample] = glob_wildcards(config['input_bam_location'] + "{}/all/{{bam}}.bam".format(sample)).bam
pprint(ALLBAMS_PER_SAMPLE)
print("Detected {} samples:".format(len(SAMPLES)))
for s in SAMPLES:
print(" {}:\t{} cells\t {} selected cells".format(s, len(ALLBAMS_PER_SAMPLE[s]), len(BAM_PER_SAMPLE[s])))
exit()
import os.path
# Current state of the pipeline:
# ==============================
# * count reads in the BAM files (in fixed and variable-width bins of various sizes)
# * determine strand states of each chromosome in each single cell, including SCEs
# * plot all single cell libraries in different window sizes
# * calculate a segmentation into potential SVs using Mosaicatcher
METHODS = [
"simpleCalls_llr4_poppriorsTRUE_haplotagsTRUE_gtcutoff0_regfactor6_filterFALSE",
"simpleCalls_llr4_poppriorsTRUE_haplotagsFALSE_gtcutoff0.05_regfactor6_filterTRUE",
]
BPDENS = [
"selected_j{}_s{}_scedist{}".format(joint, single, scedist) for joint in [0.1] for single in [0.5] for scedist in [20]
]
# Todo: specify an exact version of the singularity file!
rule all:
input:
expand("counts/{sample}/{window}_fixed.txt.gz", sample=SAMPLES, window=[100000])
################################################################################
# Read counting #
################################################################################
rule generate_exclude_file_1:
output:
temp("log/exclude_file.temp")
input:
bam = expand("bam/{sample}/selected/{bam}.bam", sample = SAMPLES[0], bam = BAM_PER_SAMPLE[SAMPLES[0]][0])
log:
"log/generate_exclude_file_1.log"
params:
samtools = config["samtools"]
shell:
"""
{params.samtools} view -H {input.bam} | awk "/^@SQ/" > {output}
"""
rule generate_exclude_file_2:
output:
"log/exclude_file"
input:
"log/exclude_file.temp"
params:
chroms = config["chromosomes"]
run:
with open(input[0]) as f:
with open(output[0],"w") as out:
for line in f:
contig = line.strip().split()[1]
contig = contig[3:]
# if contig not in params.chroms:
# print(contig, file = out)
rule mosaic_count_fixed:
input:
bam = lambda wc: expand("bam/" + wc.sample + "/selected/{bam}.bam", bam = BAM_PER_SAMPLE[wc.sample]) if wc.sample in BAM_PER_SAMPLE else "FOOBAR",
bai = lambda wc: expand("bam/" + wc.sample + "/selected/{bam}.bam.bai", bam = BAM_PER_SAMPLE[wc.sample]) if wc.sample in BAM_PER_SAMPLE else "FOOBAR",
excl = "log/exclude_file"
output:
counts = "counts/{sample}/{window}_fixed.txt.gz",
info = "counts/{sample}/{window}_fixed.info"
log:
"log/{sample}/mosaic_count_fixed.{window}.log"
params:
mc_command = config["mosaicatcher"]
shell:
"""
echo mosaic_count_fixed &&
{params.mc_command} count \
--verbose \
--do-not-blacklist-hmm \
-o {output.counts} \
-i {output.info} \
-x {input.excl} \
-w {wildcards.window} \
{input.bam}
"""
# rule extract_single_cell_counts:
# input:
# "counts/{sample}/{window}_{file_name}.txt.gz"
# output:
# "counts-per-cell/{sample}/{cell}/{window,[0-9]+}_{file_name}.txt.gz"
# shell:
# """
# # Issue #1022 (https://bitbucket.org/snakemake/snakemake/issues/1022)
# zcat {input} | awk -v name={wildcards.cell} -f utils/command1.awk | gzip > {output}
# """
# ################################################################################
# # Plots #
# ################################################################################
# rule plot_mosaic_counts:
# input:
# counts = "counts/{sample}/{file_name}.txt.gz",
# info = "counts/{sample}/{file_name}.info"
# output:
# "plots/{sample}/{file_name}.pdf"
# log:
# "log/plot_mosaic_counts/{sample}/{file_name}.log"
# params:
# plot_command = "Rscript " + config["plot_script"]
# shell:
# """
# {params.plot_command} {input.counts} {input.info} {outp
# """