Skip to content
Snippets Groups Projects
Forked from an inaccessible project.
rename_reads_for_bam.py 818 B
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# Author: Xiaofei Zeng
# Email: xiaofei_zeng@whu.edu.cn
# Created Time: 2023-12-18 15:27


import argparse
import os

def rename_reads(bam):

    n = 0
    with os.popen('samtools view -h {}'.format(bam)) as f:
        for line in f:
            if not line.strip():
                continue
            if line.startswith('@'):
                print(line, end='')
            elif line.split()[1] != '0':
                print('read{}\t{}'.format(n//2, line.split('\t', 1)[-1]), end='')
                n += 1


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('bam', help='paired-end bam output by wf-pore-c')
    args = parser.parse_args()

    rename_reads(args.bam)


if __name__ == '__main__':
    main()