-
Notifications
You must be signed in to change notification settings - Fork 1
/
config_parsers.py
387 lines (335 loc) · 13.2 KB
/
config_parsers.py
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#!/usr/bin/env python
import os
import gzip
from typing import List
import pandas as pd
from snakemake.io import expand
def parse_config_tab(tab_file=None, sep = "\t", comment='#', index=None):
"""General purpose parser for configuration table.
Args:
tab_file: configuration file to be parsed
sep: table separator
comment: lines starting with this will be skipped
index: set this field as index
Return:
pd data frame
"""
# TODO:
# - set a better log
# - test!
tab = pd.read_table(tab_file, sep=sep, comment=comment)
if index:
index = list(index)
try:
tab.set_index(index, drop=False, inplace=True, verify_integrity=True)
except ValueError as verr:
msg = verr
raise ValueError(msg)
return tab
def parse_config_tabs(analysis_tab_file=None, reference_tab_file=None, datasets_tab_file=None):
# TODO:
# - test!
analysis_tab = parse_config_tab(tab_file=analysis_tab_file, index=["sample"])
reference_tab = parse_config_tab(tab_file=reference_tab_file, index=["ref_genome_mt", "ref_genome_n"])
datasets_tab = parse_config_tab(tab_file=datasets_tab_file, index=["sample", "library"])
return analysis_tab, reference_tab, datasets_tab
def ref_genome_mt_to_species(ref_genome_mt=None, reference_tab=None):
""" Return a string of species corresponding to a mt reference genome,
given a reference_tab.
Args:
ref_genome_mt: ref_genome_mt as parsed from analysis_tab
reference_tab: table of reference genomes
Returns:
string
"""
# TODO:
# - it should throw an error if multiple instances of species are found in the table, atm it keeps the last one it finds
for row in reference_tab.itertuples():
if getattr(row, "ref_genome_mt") == ref_genome_mt:
species = getattr(row, "species")
return species
def get_analysis_species(ref_genome_mt, reference_tab=None, config_species=None):
""" Return a string of species used for analysis.
Args:
ref_genome_mt: ref_genome_mt as parsed from analysis_tab
config_species: species from config file. This will overseed values parsed from reference_tab
Returns:
string
"""
if config_species:
species = config_species
else:
species = ref_genome_mt_to_species(ref_genome_mt=ref_genome_mt, reference_tab=reference_tab)
return species
# TODO: infolder is not used anywhere
def get_datasets_for_symlinks(df, sample=None, library=None, d=None,
infolder="data/reads", outfolder="data/reads"):
dataset_file = None
for row in df.itertuples():
if (getattr(row, "sample") == sample and
getattr(row, "library") == int(library)):
dataset_file = os.path.join(outfolder, getattr(row, d))
return dataset_file
def is_compr_file(f):
with gzip.open(f, 'r') as fh:
try:
fh.read(1)
return True
except OSError:
return False
# TODO: infolder is not used anywhere
def get_symlinks(df, analysis_tab=None,
infolder="data/reads", outfolder="data/reads"):
outpaths = []
# TODO: convert .iterrows() to .itertuples() for efficiency
for i, l in df.iterrows():
if l["sample"] in list(analysis_tab["sample"]):
if is_compr_file("data/reads/{}".format(l["R1"])):
outpaths.append(
os.path.join(
outfolder,
"{sample}_{library}.R1.fastq.gz".format(
sample=l["sample"], library=l["library"])
)
)
outpaths.append(
os.path.join(
outfolder,
"{sample}_{library}.R2.fastq.gz".format(
sample=l["sample"], library=l["library"])
)
)
else:
outpaths.append(
os.path.join(
outfolder,
"{sample}_{library}.R1.fastq".format(
sample=l["sample"], library=l["library"])
)
)
outpaths.append(
os.path.join(
outfolder,
"{sample}_{library}.R2.fastq".format(
sample=l["sample"], library=l["library"])
)
)
# print(outpaths)
return outpaths
def get_genome_single_vcf_files(df, res_dir="results", ref_genome_mt=None):
outpaths = []
for row in df.itertuples():
if getattr(row, "ref_genome_mt") == ref_genome_mt:
outpaths.append(("{results}/{sample}/{sample}_{ref_genome_mt}_"
"{ref_genome_n}.vcf.gz").format(
results=res_dir,
sample=getattr(row, "sample"),
ref_genome_mt=getattr(row, "ref_genome_mt"),
ref_genome_n=getattr(row, "ref_genome_n")))
return list(set(outpaths))
# TODO: library is not used anywhere
def get_sample_bamfiles(df, res_dir="results", sample=None, library=None,
ref_genome_mt=None, ref_genome_n=None):
outpaths = []
for row in df.itertuples():
if getattr(row, "sample") == sample:
bam_file = ("{sample}_{library}_{ref_genome_mt}_"
"{ref_genome_n}_OUT-sorted.final.bam").format(
sample=sample,
library=getattr(row, "library"),
ref_genome_mt=ref_genome_mt,
ref_genome_n=ref_genome_n)
out_folder = "OUT_{base}".format(
base=bam_file.replace("_OUT-sorted.final.bam", ""))
outpaths.append("{results}/{sample}/map/{out_folder}/{bam_file}".format(
results=res_dir,
bam_file=bam_file,
sample=sample,
out_folder=out_folder))
return outpaths
def get_genome_single_vcf_index_files(df, res_dir="results", ref_genome_mt=None):
outpaths = []
for row in df.itertuples():
if getattr(row, "ref_genome_mt") == ref_genome_mt:
outpaths.append(
("{results}/{sample}/{sample}_{ref_genome_mt}_"
"{ref_genome_n}.vcf.gz.csi").format(
results=res_dir,
sample=getattr(row, "sample"),
ref_genome_mt=getattr(row, "ref_genome_mt"),
ref_genome_n=getattr(row, "ref_genome_n")))
return list(set(outpaths))
def get_genome_vcf_files(df: pd.DataFrame,
annotation = False,
res_dir: str = "results/vcf") -> List[str]:
""" Return a list of output filenames where VCF files will be stored.
Args:
df: input pandas DataFrame
annotation: are these annotated vcfs?
res_dir: output directory name
Returns:
list of paths
"""
outpaths = set()
if annotation:
annotated = ".annotated"
else:
annotated = ""
# TODO: this is inefficient as it goes through every row and
# then removes duplicates, there is a better way for this
for row in df.itertuples():
outpaths.add(
"{results}/{ref_genome_mt}_{ref_genome_n}{annotated}.vcf".format(
results=res_dir,
ref_genome_mt=row.ref_genome_mt,
ref_genome_n=row.ref_genome_n,
annotated=annotated
)
)
return list(outpaths)
def get_bed_files(df: pd.DataFrame,
res_dir: str = "results") -> List[str]:
""" Return a list of output filenames where BED files will be stored.
Args:
df: input pandas DataFrame
res_dir: output directory name
Returns:
list of paths
"""
outpaths = []
for row in df.itertuples():
outpaths.append(
("{results}/{sample}/{sample}_"
"{ref_genome_mt}_{ref_genome_n}.bed").format(
results=res_dir,
sample=getattr(row, "sample"),
ref_genome_mt=row.ref_genome_mt,
ref_genome_n=row.ref_genome_n
)
)
return outpaths
def get_fasta_files(df: pd.DataFrame,
res_dir: str = "results") -> List[str]:
""" Return a list of output filenames where fasta files will be stored.
Args:
df: input pandas DataFrame
res_dir: output directory name
Returns:
list of paths
"""
outpaths = []
for row in df.itertuples():
outpaths.append(
("{results}/{sample}/{sample}_"
"{ref_genome_mt}_{ref_genome_n}.fasta").format(
results=res_dir,
sample=getattr(row, "sample"),
ref_genome_mt=row.ref_genome_mt,
ref_genome_n=row.ref_genome_n
)
)
return outpaths
def get_haplo_prediction_files(df, res_dir="results"):
outpaths = []
for row in df.itertuples():
outpaths.append(
("{results}/{sample}/{sample}_"
"{ref_genome_mt}_{ref_genome_n}.csv").format(
results=res_dir,
sample=getattr(row, "sample"),
ref_genome_mt=getattr(row, "ref_genome_mt"),
ref_genome_n=getattr(row, "ref_genome_n")))
return outpaths
def get_genome_files(df: pd.DataFrame,
ref_genome_mt: str,
field: str) -> List[str]:
""" Return a list of output filenames where fna files will be stored.
Args:
df: input pandas DataFrame
ref_genome_mt: wildcard from snakefile
field: column name
Returns:
list of paths
"""
return expand(df.loc[ref_genome_mt, field])
def get_mt_genomes(df: pd.DataFrame) -> List[str]:
""" Return a list of unique mt genome identifiers from the
given dataframe.
Args:
df: input pandas DataFrame
Returns:
list of str
"""
return df["ref_genome_mt"].unique().tolist()
def get_mt_fasta(df, ref_genome_mt, field):
return df.loc[df['ref_genome_mt'] == ref_genome_mt, field][0]
def fastqc_outputs(datasets_tab: pd.DataFrame,
analysis_tab: pd.DataFrame,
out: str = "raw") -> List[str]:
""" Return a list of output filenames where FastQC results will be stored.
Args:
datasets_tab: input pandas DataFrame with fastq filenames
analysis_tab: input pandas DataFrame with analysis details
out: either 'raw' or 'filtered', determines the output
directory where FastQC results will be stored
Returns:
list of paths
"""
if out == "raw":
outfolder = "results/fastqc_raw"
suffixes = {"R1" : ".R1_fastqc.html", "R2" : ".R2_fastqc.html", "U" : ".U_fastqc.html"}
elif out == "filtered":
outfolder = "results/fastqc_filtered"
suffixes = {"R1" : "_qc_R1_fastqc.html", "R2" : "_qc_R2_fastqc.html", "U" : "_qc_U_fastqc.html"}
else:
raise ValueError(f"{out} is not a valid argument")
fastqc_out = []
samples = analysis_tab["sample"].tolist()
for row in datasets_tab.itertuples():
# TODO: using getattr for the sample column since sample
# is already a method name in pandas dataframes, possibly
# need to change that column name
if getattr(row, "sample") in samples:
fastqc_out.append(
os.path.join(
outfolder,
"{sample}_{library}{suffix}".format(
sample=getattr(row, "sample"),
library=row.library,
suffix=suffixes["R1"]
)
)
)
fastqc_out.append(
os.path.join(
outfolder,
"{sample}_{library}{suffix}".format(
sample=getattr(row, "sample"),
library=row.library,
suffix=suffixes["R2"]
)
)
)
if out == "filtered":
fastqc_out.append(
os.path.join(
outfolder,
"{sample}_{library}{suffix}".format(
sample=getattr(row, "sample"),
library=row.library,
suffix=suffixes["U"]
)
)
)
return fastqc_out
def get_inputs_for_rule_map_nuclear_MT_SE(sample=None, library=None, ref_genome_n=None, ref_genome_mt=None, keep_orphans=True):
outpaths = []
outpaths.append("results/{sample}/map/OUT_{sample}_{library}_{ref_genome_mt}_{ref_genome_n}/{sample}_{library}_{ref_genome_mt}_outmt.fastq.gz")
if keep_orphans:
outpaths.append(
"results/{sample}/map/OUT_{sample}_{library}_{ref_genome_mt}_{ref_genome_n}/{sample}_{library}_{ref_genome_mt}_outmt_U1.fastq.gz"
)
outpaths.append(
"results/{sample}/map/OUT_{sample}_{library}_{ref_genome_mt}_{ref_genome_n}/{sample}_{library}_{ref_genome_mt}_outmt_U2.fastq.gz"
)
return outpaths