-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplit_fastq_bybarcode.py
More file actions
33 lines (27 loc) · 999 Bytes
/
Copy pathsplit_fastq_bybarcode.py
File metadata and controls
33 lines (27 loc) · 999 Bytes
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
#!/usr/bin/env python
import gzip
from Bio import SeqIO
import os
def get_barcode_from_header(header):
parts = header.split()
for part in parts:
if part.startswith("barcode="):
return part.split('=')[1]
return None
def split_fastq_by_barcode(fastq_file):
barcodes = {}
# opens g zip file
open_func = gzip.open if fastq_file.endswith(".gz") else open
with open_func(fastq_file, "rt") as handle:
for record in SeqIO.parse(handle, "fastq"):
barcode = get_barcode_from_header(record.description)
if barcode:
if barcode not in barcodes:
barcodes[barcode] = []
barcodes[barcode].append(record)
for barcode, records in barcodes.items():
output_filename = f"{barcode}.fastq"
with open(output_filename, "w") as output_handle:
SeqIO.write(records, output_handle, "fastq")
# Usage example
split_fastq_by_barcode("file.fastq.gz")