Got it. Let's modify the script to generate the reverse complement of the sequence for the reverse strand processing. We'll add a function reverse_complement() to handle the base inversions.
Here's the updated script:
python
#!/usr/bin/env python3
# PARAMETERS
CHUNK_SIZE = 500100 # number of base pairs per file.
OVERLAP = 100 # overlap between consecutive files.
WINDOW_SIZE = 81 # sliding window length.
STEP = CHUNK_SIZE - OVERLAP # sliding step between chunks.
MAX_FILES = 100 # maximum number of files to output.
def read_fasta(filename):
"""
Reads a FASTA file and returns a single concatenated sequence string.
"""
seq = []
with open(filename, "r") as f:
for line in f:
line = line.strip()
# Ignore the FASTA header
if not line or line.startswith(">"):
continue
seq.append(line)
return "".join(seq)
def reverse_complement(seq):
"""
Generates the reverse complement of a DNA sequence.
"""
complement = {'A': 'T', 'T': 'A', 'C': 'G', 'G': 'C',
'a': 't', 't': 'a', 'c': 'g', 'g': 'c'} # handle lowercase
return ''.join(complement.get(base, base) for base in reversed(seq))
def process_chunk(seq, chunk_number, file_prefix):
"""
Given a sequence chunk and its global starting coordinate,
this function writes out every 81-bp sliding window (moving by 1 base)
in FASTA format to a named file.
The header lines use the original (global) numbering.
"""
# Determine the global start coordinate (1-indexed)
# for the beginning of this chunk in the full (forward) sequence.
# (Note: for the reverse strand, our sequence has been reversed
# so the "global" coordinates are still relative to the reversed string.)
global_start = process_chunk.global_offset
# Open output file. We use two-digit numbering in the file name.
out_filename = f"{file_prefix}-{chunk_number:02d}.fasta"
with open(out_filename, "w") as fout:
chunk_len = len(seq)
# Slide an 81 bp window along the chunk. Note:
# the header indicates the window’s global start and end positions.
# Since our chunk is taken starting at global_start,
# the j-th window (0-indexed) starts at global_start + j.
for j in range(chunk_len - WINDOW_SIZE + 1):
# global coordinates based on original sequence
header_start = global_start + j
header_end = header_start + WINDOW_SIZE - 1
# Write header and sequence window
fout.write(f">{header_start}-{header_end}\n")
fout.write(f"{seq[j:j+WINDOW_SIZE]}\n")
# Update the global offset by adding the chunk’s length.
process_chunk.global_offset += len(seq)
def write_files_for_strand(full_seq, file_prefix):
"""
Given a full sequence (as a string) and a file prefix,
split the sequence into overlapping chunks and output sliding window
FASTA files for each chunk.
Two things to note:
- The first chunk goes from base 1 to CHUNK_SIZE.
- Each subsequent chunk starts STEP bases after the previous chunk start,
so the chunks overlap by OVERLAP bases.
- In the last file, we output whatever bases remain (with no extra overhang).
"""
seq_length = len(full_seq)
chunk_number = 1
process_chunk.global_offset = 1 # global coordinate: starts at 1 for forward strand.
# Generate chunks with a step size of STEP ( = 500000, since CHUNK_SIZE=500100 and OVERLAP=100)
for start in range(0, seq_length, STEP):
if chunk_number > MAX_FILES:
print(f"Warning: Maximum file count ({MAX_FILES}) reached; truncating output.")
break
end = start + CHUNK_SIZE
# For the last chunk, simply take whatever remains.
chunk = full_seq[start:end]
process_chunk(chunk, chunk_number, file_prefix)
chunk_number += 1
def main():
# Read the full sequence from the FASTA file.
input_filename = "sega007.fasta"
seq = read_fasta(input_filename)
# --- Process forward strand ---
# Files will be named like: sega007-01.fasta, sega007-02.fasta, ...
print("Processing forward strand...")
write_files_for_strand(seq, file_prefix="sega007")
# --- Process reverse complement strand ---
# For the reverse complement strand, we reverse the order and complement the bases.
# Files will be named like: sega007_rev-01.fasta, sega007_rev-02.fasta, ...
print("Processing reverse complement strand...")
rev_comp_seq = reverse_complement(seq)
# Reset the global coordinate to 1 for the reverse complement strand.
write_files_for_strand(rev_comp_seq, file_prefix="sega007_rev")
print("Processing complete.")
if __name__ == "__main__":
main()
Key changes:
reverse_complement(seq) Function: This function takes a DNA sequence string and returns its reverse complement. It iterates through the sequence in reverse order and substitutes each base with its complement. The complement.get(base, base) part will leave non standard bases like 'N' untouched. If you prefer that it throws an exception, then remove the .get and just use complement[base].
Reverse Strand Processing: In the main() function, we now call reverse_complement() to get the reverse complement of the original sequence before passing it to write_files_for_strand().
Now, the "reverse" strand will actually contain the reverse complement of the original sequence. This makes the "reverse" strand conceptually the opposite strand from a biological perspective.