-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcommon.smk
More file actions
291 lines (237 loc) · 9.25 KB
/
common.smk
File metadata and controls
291 lines (237 loc) · 9.25 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import re
import logging
import sys
import pysam
FIRST_REPORT = True
def get_ref_orig():
if "ref" not in config:
raise ValueError("FIRE: ref parameter is missing in config.yaml")
ref = config["ref"]
if not os.path.exists(ref):
raise ValueError(f"FIRE: reference file {ref} does not exist")
return os.path.abspath(ref)
def get_ref_old(wc):
if "ref" in config:
ref = config["ref"]
if "ref" not in config:
if "ref" in MANIFEST.columns:
ref = get_input_ref()
else:
raise ValueError("FIRE: ref parameter is missing in config.yaml and no ref column in manifest")
#ref = config["ref"]
if not os.path.exists(ref):
raise ValueError(f"FIRE: reference file {ref} does not exist")
return os.path.abspath(ref)
def get_ref(wc):
ref = MANIFEST.loc[wc.sm, "ref"]
if not os.path.exists(ref):
raise ValueError(f"FIRE: reference file {ref} does not exist")
return os.path.abspath(ref)
def get_ref_name_old(wc):
if "ref_name" in config:
ref_name=config["ref_name"]
else:
if "ref_name" in MANIFEST.columns:
ref_name= get_input_ref_name()
else:
raise ValueError("FIRE: ref_name parameter is missing in config.yaml and no ref_name column in manifest")
#ref=get_ref()
#temp_split=ref.strip().split('/')[-1]
#fa_split=temp_split.split('.fa')
#ref_name = '.fa'.join(fa_split[:-1])
return(ref_name)
def get_ref_name(wc):
return MANIFEST.loc[wc.sm, "ref_name"]
def get_fai_orig(wc):
fai = f"{get_ref(wc)}.fai"
if not os.path.exists(fai):
raise ValueError(f"FIRE: reference index file {fai} does not exist")
return fai
def get_fai(wc):
ref = MANIFEST.loc[wc.sm, "ref"]
#fai= ref + ".fai"
fai = str(ref) + ".fai"
if not os.path.exists(fai):
raise ValueError(f"FIRE: reference index file {fai} does not exist")
return fai
def get_excludes(wc):
excludes = config.get("excludes", [])
ref_name = get_ref_name(wc)
if ref_name == "hg38" or ref_name == "GRCh38":
files = [
"../annotations/hg38.gap.bed.gz",
"../annotations/hg38.blacklist.ENCFF356LFX.bed.gz",
"../annotations/SDs.merged.hg38.bed.gz",
]
excludes += [workflow.source_path(file) for file in files]
return excludes
def get_fai_df(wc):
fai = get_fai(wc)
return pd.read_csv(fai, sep="\t", names=["chr", "length", "x", "y", "z"])
def get_chroms_orig(wc):
global FIRST_REPORT
min_contig_length = config.get("min_contig_length", 0)
fai_df=get_fai_df(wc)
skipped_contigs = fai_df["chr"][fai_df["length"] < min_contig_length]
if len(skipped_contigs) > 0 and FIRST_REPORT:
print(
f"WARNING: Skipping contigs with length < {min_contig_length:,}: {skipped_contigs}",
file=sys.stderr,
)
chroms = fai_df["chr"][fai_df["length"] >= min_contig_length]
chroms = sorted([chrom for chrom in chroms if "chrUn_" not in chrom])
chroms = [chrom for chrom in chroms if "_random" not in chrom]
chroms = [chrom for chrom in chroms if re.fullmatch(KEEP_CHRS, chrom)]
if FIRST_REPORT:
FIRST_REPORT = False
print(f"INFO: Using N chromosomes: {len(chroms)}", file=sys.stderr)
if len(chroms) == 0:
raise ValueError(
f"No chromosomes left after filtering. Check your keep_chromosomes parameter in config.yaml. "
f"Your fai file contains the following chromosomes: {fai_df['chr']}"
)
return chroms
def get_chroms(wc):
global FIRST_REPORT
min_contig_length = config.get("min_contig_length", 0)
fai_df=get_fai_df(wc)
skipped_contigs = fai_df["chr"][fai_df["length"] < min_contig_length]
if len(skipped_contigs) > 0 and FIRST_REPORT:
print(
f"WARNING: Skipping contigs with length < {min_contig_length:,}: {skipped_contigs}",
file=sys.stderr,
)
bam_chr_list=[]
input_bam_path=get_input_bam(wc)
input_bam = pysam.AlignmentFile(input_bam_path, "rc", threads=MAX_THREADS)
bam_header_dict = input_bam.header.to_dict()
for line in bam_header_dict['SQ']:
chr_name=line['SN']
bam_chr_list.append(chr_name)
input_bam.close()
chroms = fai_df["chr"][fai_df["length"] >= min_contig_length]
chroms = sorted([chrom for chrom in chroms if "chrUn_" not in chrom])
chroms = [chrom for chrom in chroms if "_random" not in chrom]
chroms = [chrom for chrom in chroms if re.fullmatch(KEEP_CHRS, chrom)]
chroms = [chrom for chrom in chroms if chrom in bam_chr_list]
if FIRST_REPORT:
FIRST_REPORT = False
print(f"INFO: Using N chromosomes: {len(chroms)}", file=sys.stderr)
if len(chroms) == 0:
raise ValueError(
f"No chromosomes left after filtering. Check your keep_chromosomes parameter in config.yaml. "
f"Your fai file contains the following chromosomes: {fai_df['chr']}"
)
return chroms
def get_chroms_first_element(wc):
global FIRST_REPORT
min_contig_length = config.get("min_contig_length", 0)
fai_df=get_fai_df(wc)
skipped_contigs = fai_df["chr"][fai_df["length"] < min_contig_length]
if len(skipped_contigs) > 0 and FIRST_REPORT:
print(
f"WARNING: Skipping contigs with length < {min_contig_length:,}: {skipped_contigs}",
file=sys.stderr,
)
bam_chr_list=[]
input_bam_path=get_input_bam(wc)
input_bam = pysam.AlignmentFile(input_bam_path, "rc", threads=MAX_THREADS)
bam_header_dict = input_bam.header.to_dict()
for line in bam_header_dict['SQ']:
chr_name=line['SN']
bam_chr_list.append(chr_name)
input_bam.close()
chroms = fai_df["chr"][fai_df["length"] >= min_contig_length]
chroms = sorted([chrom for chrom in chroms if "chrUn_" not in chrom])
chroms = [chrom for chrom in chroms if "_random" not in chrom]
chroms = [chrom for chrom in chroms if re.fullmatch(KEEP_CHRS, chrom)]
chroms = [chrom for chrom in chroms if chrom in bam_chr_list]
if FIRST_REPORT:
FIRST_REPORT = False
print(f"INFO: Using N chromosomes: {len(chroms)}", file=sys.stderr)
if len(chroms) == 0:
raise ValueError(
f"No chromosomes left after filtering. Check your keep_chromosomes parameter in config.yaml. "
f"Your fai file contains the following chromosomes: {fai_df['chr']}"
)
return chroms[0]
def get_chroms_first_element_orig(wc):
global FIRST_REPORT
min_contig_length = config.get("min_contig_length", 0)
fai_df=get_fai_df(wc)
skipped_contigs = fai_df["chr"][fai_df["length"] < min_contig_length]
if len(skipped_contigs) > 0 and FIRST_REPORT:
print(
f"WARNING: Skipping contigs with length < {min_contig_length:,}: {skipped_contigs}",
file=sys.stderr,
)
chroms = fai_df["chr"][fai_df["length"] >= min_contig_length]
chroms = sorted([chrom for chrom in chroms if "chrUn_" not in chrom])
chroms = [chrom for chrom in chroms if "_random" not in chrom]
chroms = [chrom for chrom in chroms if re.fullmatch(KEEP_CHRS, chrom)]
if FIRST_REPORT:
FIRST_REPORT = False
print(f"INFO: Using N chromosomes: {len(chroms)}", file=sys.stderr)
if len(chroms) == 0:
raise ValueError(
f"No chromosomes left after filtering. Check your keep_chromosomes parameter in config.yaml. "
f"Your fai file contains the following chromosomes: {fai_df['chr']}"
)
return chroms[0]
def get_manifest():
manifest = config.get("manifest")
if manifest is None:
raise ValueError("manifest parameter is missing in config.yaml")
if not os.path.exists(manifest):
raise ValueError(f"Manifest file {manifest} does not exist")
manifest = pd.read_csv(config["manifest"], sep=r"\s+", comment="#").set_index(
"sample"
)
return manifest
def get_input_bam(wc):
return MANIFEST.loc[wc.sm, "bam"]
def get_input_ref(wc):
return MANIFEST.loc[wc.sm, "ref"]
def get_input_ref_name(wc):
return MANIFEST.loc[wc.sm, "ref_name"]
def get_mem_mb(wildcards, attempt):
if attempt < 3:
return attempt * 1024 * 32
return attempt * 1024 * 48
def get_large_mem_mb(wildcards, attempt):
return attempt * 1024 * 64
def get_mem_mb_xl(wildcards, attempt):
return attempt * 1024 * 92
def get_mem_mb_small(wildcards, attempt):
return attempt * 1024 * 4
def get_load(wc):
if "all" in wc.sm:
return 100
return 50
def get_hap_col_suffix(wc):
if wc.hp == "all":
return ""
elif wc.hp == "hap1":
return "_H1"
elif wc.hp == "hap2":
return "_H2"
else:
raise ValueError(f"Unknown haplotype {wc.hp}")
def pileup_cut_cmd(wc):
if wc.hp == "all":
tail = ""
elif wc.hp == "hap1":
tail = "_H1"
elif wc.hp == "hap2":
tail = "_H2"
else:
raise ValueError(f"Unknown haplotype {wc.hp}")
if wc.el_type == "nucleosome":
col = f"$nuc_coverage{tail}"
elif wc.el_type == "linker":
col = f"$msp_coverage{tail}-$fire_coverage{tail}"
elif wc.el_type == "fire":
col = f"$fire_coverage{tail}"
else:
raise ValueError(f"Unknown element type {wc.el_type}")
return f"bioawk -tc hdr '{{print $1,$2,$3,{col}}}'"