Skip to content

Commit 50809ab

Browse files
author
Florian Finkernagel finkernagel@imt.uni-marburg.de
committed
merged
2 parents 8ed86f3 + 218a6e1 commit 50809ab

File tree

1 file changed

+43
-2
lines changed

1 file changed

+43
-2
lines changed

src/mbf/align/post_process.py

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ def process(self, input_bam_name, output_bam_name, result_dir):
261261
def register_qc(self, new_lane):
262262
pass # pragma: no
263263

264+
264265
class R2Only(_PostProcessor):
265266
"""Remove all r2 (0x40 reads)
266267
and turn a paired end alignment into single reads for downstream analyses
@@ -289,7 +290,6 @@ def register_qc(self, new_lane):
289290
pass # pragma: no
290291

291292

292-
293293
class AddChr(_PostProcessor):
294294
def __init__(self):
295295
self.name = "AddChr"
@@ -332,6 +332,48 @@ def process(self, input_bam_name, output_bam_name, result_dir):
332332
out.close()
333333

334334

335+
class RemoveChr(_PostProcessor):
336+
def __init__(self):
337+
self.name = "RemoveChr"
338+
self.result_folder_name = self.name
339+
340+
def register_qc(self, new_lane):
341+
pass # pragma: no
342+
343+
def process(self, input_bam_name, output_bam_name, result_dir):
344+
import pysam
345+
346+
input = pysam.Samfile(input_bam_name)
347+
names_and_lengths = [(k, v) for (k, v) in zip(input.references, input.lengths)]
348+
349+
header = input.header.to_dict()
350+
if "PG" not in header:
351+
header["PG"] = []
352+
header["PG"].append(
353+
{
354+
"ID": "mbf_align_remove_chr",
355+
"PN": "RemoveChr",
356+
"VN": "0.1",
357+
"CL": "",
358+
}
359+
)
360+
del header["SQ"]
361+
header["SQ"] = []
362+
for k, length in names_and_lengths:
363+
new_name = k[3:] if k.startswith("chr") else k
364+
header["SQ"].append({"LN": length, "SN": new_name})
365+
366+
out = pysam.Samfile(
367+
output_bam_name,
368+
"wb",
369+
header=header,
370+
)
371+
for read in input.fetch(until_eof=True):
372+
out.write(read)
373+
input.close()
374+
out.close()
375+
376+
335377
class AddChrAndFilter(_PostProcessor):
336378
def __init__(self, accepted_chrs):
337379
"""accepted_chrs is in terms of the input list, not the chr added list"""
@@ -362,4 +404,3 @@ def get_dependencies(self):
362404
"AddChrAndFilter.mbf_bam.version", mbf_bam.__version__
363405
),
364406
]
365-

0 commit comments

Comments
 (0)