-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprimer_soft_clip.py
More file actions
298 lines (242 loc) · 9.52 KB
/
Copy pathprimer_soft_clip.py
File metadata and controls
298 lines (242 loc) · 9.52 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
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
#!/usr/bin/env python3
# Thanh Le Viet - Theiagen Genomics - 2025
# This script soft clips primer regions in a BAM file based on the direction of the read and the primer.
import pysam
import argparse
from collections import defaultdict
from dataclasses import dataclass
from typing import Dict, List, Tuple, Optional
import logging
import os
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
@dataclass
class PrimerRegion:
"""Dataclass to store primer information."""
chrom: str
start: int
end: int
name: str
strand: str
def read_primer_bed(bed_file: str) -> Dict[str, List[PrimerRegion]]:
"""Read primer regions from BED file."""
primers_by_chrom = defaultdict(list)
with open(bed_file, "r") as f:
for line in f:
fields = line.strip().split("\t")
chrom, start, end, name = fields[0:4]
strand = fields[5] if len(fields) > 5 else "+"
primer = PrimerRegion(chrom, int(start), int(end), name, strand)
primers_by_chrom[chrom].append(primer)
return primers_by_chrom
def get_matching_primers(
read: pysam.AlignedSegment,
primers: List[PrimerRegion],
read_start: int,
read_end: int,
) -> List[PrimerRegion]:
"""Find primers that match read direction and overlap the read."""
read_is_forward = not read.is_reverse
matching = []
for primer in primers:
# Check if read direction matches primer direction
if read_is_forward == (primer.strand == "+"):
if read_is_forward:
# For forward reads: only clip if the start (5' end) overlaps with primer
# Ignore if only the end overlaps
if read_start <= primer.end and read_start >= primer.start:
matching.append(primer)
else:
# For reverse reads: only clip if the end (3' end) overlaps with primer
# Ignore if only the start overlaps
if read_end >= primer.start and read_end <= primer.end:
matching.append(primer)
return matching
def get_soft_clip_info(read: pysam.AlignedSegment) -> Tuple[int, int, int]:
"""Get existing soft clips and aligned length from read."""
left_clip = right_clip = 0
if read.cigartuples:
# Check for soft clips (CIGAR code 4) at start of read
if read.cigartuples[0][0] == 4:
left_clip = read.cigartuples[0][1]
# Check for soft clips at end of read
if read.cigartuples[-1][0] == 4:
right_clip = read.cigartuples[-1][1]
seq_len = len(read.query_sequence) if read.query_sequence else 0
aligned_len = seq_len - (left_clip + right_clip)
return left_clip, right_clip, aligned_len
def create_modified_read(
read: pysam.AlignedSegment, header: Dict
) -> pysam.AlignedSegment:
"""Create a copy of the read with all fields."""
modified = pysam.AlignedSegment(header)
for attr in [
"query_name",
"flag",
"reference_id",
"reference_start",
"mapping_quality",
"cigartuples",
"next_reference_id",
"next_reference_start",
"template_length",
"query_sequence",
"query_qualities",
]:
setattr(modified, attr, getattr(read, attr))
for tag, value in read.get_tags():
try:
modified.set_tag(tag, value)
except ValueError as e:
logger.warning(f"Could not set tag {tag} for read {read.query_name}: {e}")
return modified
def calculate_new_cigar(
read_is_forward: bool,
aligned_len: int,
additional_clip: int,
left_clip: int,
right_clip: int,
) -> List[Tuple[int, int]]:
"""Calculate new CIGAR string based on clipping parameters."""
if read_is_forward:
total_left = left_clip + additional_clip
new_cigar = [(4, total_left)]
remaining = aligned_len - additional_clip
if remaining > 0:
new_cigar.append((0, remaining))
if right_clip > 0:
new_cigar.append((4, right_clip))
else:
new_cigar = []
if left_clip > 0:
new_cigar.append((4, left_clip))
remaining = aligned_len - additional_clip
if remaining > 0:
new_cigar.append((0, remaining))
new_cigar.append((4, right_clip + additional_clip))
return new_cigar
def process_read(
read: pysam.AlignedSegment,
bam_in: pysam.AlignmentFile,
primers_by_chrom: Dict[str, List[PrimerRegion]],
screened_reads: Optional[str] = None,
) -> Tuple[pysam.AlignedSegment, bool]:
"""Process a single read and return modified read if needed."""
if read.is_unmapped:
return read, False
chrom = bam_in.get_reference_name(read.reference_id)
read_start = read.reference_start
read_end = read.reference_end
matching_primers = get_matching_primers(
read, primers_by_chrom.get(chrom, []), read_start, read_end
)
if not matching_primers:
return read, False
modified = create_modified_read(read, bam_in.header)
left_clip, right_clip, aligned_len = get_soft_clip_info(read)
read_is_forward = not read.is_reverse
if read_is_forward:
max_primer_end = max(p.end for p in matching_primers)
additional_clip = min(max_primer_end - read_start, aligned_len)
new_start = read_start + additional_clip
else:
min_primer_start = min(p.start for p in matching_primers)
additional_clip = min(read_end - min_primer_start, aligned_len)
new_start = read_start
new_cigar = calculate_new_cigar(
read_is_forward, aligned_len, additional_clip, left_clip, right_clip
)
# Verify CIGAR length
cigar_length = sum(length for _, length in new_cigar)
if cigar_length != len(read.query_sequence or ""):
logger.warning(f"CIGAR length mismatch for read {read.query_name}")
return read, False
modified.cigartuples = new_cigar
modified.reference_start = new_start
if screened_reads:
for primer in matching_primers:
screened_reads.write(
f"{read.query_name}\t{chrom}\t{read_start}\t{read_end}\t{primer.name}\t"
f"{primer.strand}\t{'+' if read_is_forward else '-'}\tsoft_clipped\n"
)
return modified, True
def process_bam(
input_bam: str,
output_bam: str,
primers_by_chrom: Dict[str, List[PrimerRegion]],
screened_reads_file: str,
):
"""Process BAM file and soft clip primer regions."""
# Initialize counters
total_reads = 0
soft_clipped_reads = 0
with (
pysam.AlignmentFile(input_bam, "rb") as bam_in,
open(screened_reads_file, "w") as screened_reads,
pysam.AlignmentFile(output_bam, "wb", header=bam_in.header) as bam_out,
):
# Write header for screened reads
screened_reads.write(
"read_name\tchromosome\tstart\tend\tprimer_name\tprimer_direction\tread_direction\taction\n"
)
# Process reads in chunks to save memory
chunk_size = 100000
reads_chunk = []
for read in bam_in.fetch(until_eof=True):
total_reads += 1
modified_read, was_modified = process_read(
read, bam_in, primers_by_chrom, screened_reads
)
if was_modified:
soft_clipped_reads += 1
reads_chunk.append((modified_read, read.is_unmapped))
if len(reads_chunk) >= chunk_size:
write_sorted_chunk(reads_chunk, bam_out)
reads_chunk = []
# Write remaining reads
if reads_chunk:
write_sorted_chunk(reads_chunk, bam_out)
# Calculate proportion
proportion = (soft_clipped_reads / total_reads) * 100 if total_reads > 0 else 0
# Print statistics
logger.info(f"Total reads processed: {total_reads:,}")
logger.info(f"Reads soft clipped: {soft_clipped_reads:,}")
logger.info(f"Proportion of reads soft clipped: {proportion:.2f}%")
# Sort and index the final BAM file
try:
pysam.sort("-o", output_bam + ".sorted.bam", output_bam)
os.rename(output_bam + ".sorted.bam", output_bam)
pysam.index(output_bam)
except Exception as e:
logger.error(f"Failed to sort/index BAM file: {e}")
def write_sorted_chunk(
reads: List[Tuple[pysam.AlignedSegment, bool]], bam_out: pysam.AlignmentFile
):
"""Write a sorted chunk of reads to the output BAM file."""
# Separate mapped and unmapped reads
mapped = [(r, pos) for r, pos in reads if not pos]
unmapped = [r for r, pos in reads if pos]
# Sort mapped reads by position
mapped.sort(key=lambda x: (x[0].reference_id, x[0].reference_start))
# Write sorted mapped reads
for read, _ in mapped:
bam_out.write(read)
# Write unmapped reads
for read in unmapped:
bam_out.write(read)
def main():
parser = argparse.ArgumentParser(description="Soft clip primer regions in BAM file")
parser.add_argument("-i", "--input_bam", required=True, help="Input BAM file")
parser.add_argument("-o", "--output_bam", required=True, help="Output BAM file")
parser.add_argument("-p", "--primers", required=True, help="Primers BED file")
parser.add_argument(
"-s", "--screened", required=True, help="Output file for screened reads"
)
args = parser.parse_args()
logger.info("Reading primer regions...")
primers_by_chrom = read_primer_bed(args.primers)
logger.info("Processing BAM file...")
process_bam(args.input_bam, args.output_bam, primers_by_chrom, args.screened)
logger.info("Done!")
if __name__ == "__main__":
main()