[45ad7e]: / singlecellmultiomics / bamProcessing / bamSetReadGroup.py

Download this file

125 lines (98 with data), 3.5 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import argparse
import pysam
import sys
from datetime import datetime
import singlecellmultiomics
from singlecellmultiomics.fragment import Fragment
from singlecellmultiomics.bamProcessing.bamFunctions import get_read_group_from_read, sorted_bam_file, write_program_tag
def set_read_group( in_bam_path, out_bam_path, id:str, pl:str, lb:str, sm:str, pu:str, threads=4, sm2reads=False ):
"""
Set read group format of bam file
Args:
in_bam_path(str) : bam file to read
in_bam_path(str) : bam file to write
format(int) : formatting mode, see `singlecellmultiomics.fragment.Fragment.get_read_group`
threads(int) : Amount of decompression threads for reading
"""
"""
read_groups(set/dict) : set or dictionary which contains read groups. The dictionary should have the format { read_group_id (str)
{ 'ID': ID, 'LB':library,
'PL':platform,
'SM':sampleLib,
'PU':readGroup }
"""
read_groups = {id:{ 'ID': id, 'LB':lb,
'PL':pl,
'SM':sm,
'PU':pu }}
with pysam.AlignmentFile(in_bam_path, threads = threads) as input_bam:
input_header = input_bam.header.as_dict()
# Write provenance information to BAM header
write_program_tag(
input_header,
program_name='bamReadGroupFormat',
command_line=" ".join(
sys.argv),
version=singlecellmultiomics.__version__,
description=f'SingleCellMultiOmics read group formatting, executed at {datetime.now().strftime("%d/%m/%Y %H:%M:%S")}')
with sorted_bam_file(out_bam_path, header=input_header, read_groups=read_groups, input_is_sorted=True) as out:
print('Started writing')
for read in input_bam:
rg_id = id
read.set_tag('RG',rg_id)
if sm2reads:
read.set_tag('SM',sm)
out.write(read)
if __name__=='__main__':
argparser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
description='Set read group sample platform unit etc.')
argparser.add_argument('bamin', type=str)
argparser.add_argument(
'-id',
type=str,
required = True,
help="Read group id")
argparser.add_argument(
'-pl',
type=str,
default='ILLUMINA',
help="Read group platform")
argparser.add_argument(
'-pu',
type=str,
default='LANE',
help="Read group platform unit")
argparser.add_argument(
'-sm',
type=str,
required=True,
help="Read group sample name")
argparser.add_argument(
'-lb',
type=str,
default='mix',
help="Library")
argparser.add_argument(
'-t',
type=int,
default=4,
help="Threads")
argparser.add_argument(
'--sm2reads',
action='store_true',
help="Also write the supplied SM tag to all reads")
argparser.add_argument('-o', type=str, help="output bam file", required=True)
args = argparser.parse_args()
set_read_group(
args.bamin,
args.o,
id = args.id,
pl = args.pl,
lb = args.lb,
sm = args.sm,
pu = args.pu,
sm2reads= args.sm2reads,
threads = args.t)