-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbam_split.py
56 lines (39 loc) · 1.33 KB
/
bam_split.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
# -*- coding: utf-8 -*-
# Author: LeiTY
import random
import pysam
from collections import Counter
import click
@click.command(context_settings=dict(help_option_names=['-h', '--help'], show_default=True))
@click.argument('bam_path', type=click.STRING)
@click.option('-p', '--prefix', default='split_tmp', type=click.STRING,
help='output prefix')
def bam_split(bam_path, prefix = 'split_tmp'):
"""
Random split a bam file into two bam files based on read names.
The two output files have the same number of reads.
All the alignment of a single read will be put into the same file.
"""
file = pysam.AlignmentFile(bam_path, 'rb')
out1 = f'{prefix}.1.bam'
out2 = f'{prefix}.2.bam'
outfile1 = pysam.AlignmentFile(out1, 'wb', template = file)
outfile2 = pysam.AlignmentFile(out2, 'wb', template = file)
name = Counter()
for line in file:
name[line.query_name] = 1
select_name = random.sample(list(name.keys()), len(name)//2)
for i in select_name:
name[i] = 2
file.reset()
for line in file:
if name[line.query_name] == 1:
outfile1.write(line)
else:
outfile2.write(line)
file.close()
outfile1.close()
outfile2.close()
return
if __name__ == '__main__':
bam_split()