Yes! We'll modify the script to count backwards for the reverse complement strand. For example, if the sequence is 1000000 bases long, the reverse complement headers will start with ">1000000-999920", ">999999-999919", etc.
Here's the modified script:
#!/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'}
return ''.join(complement.get(base, base) for base in reversed(seq))
def process_chunk(seq, chunk_number, file_prefix, reverse=False, seq_length=None):
"""
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.
For reverse strand, seq_length is required to calculate correct positions.
"""
# 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)
for j in range(chunk_len - WINDOW_SIZE + 1):
if not reverse:
# Forward strand: count up from global_offset
header_start = process_chunk.global_offset + j
header_end = header_start + WINDOW_SIZE - 1
else:
# Reverse strand: count down from sequence length
header_start = seq_length - (process_chunk.global_offset + j) + 1
header_end = header_start - WINDOW_SIZE + 1
# Swap start/end if needed to maintain larger number first
header_start, header_end = max(header_start, header_end), min(header_start, header_end)
# 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, reverse=False):
"""
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.
"""
seq_length = len(full_seq)
chunk_number = 1
process_chunk.global_offset = 1
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, reverse, seq_length)
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 ---
print("Processing forward strand...")
write_files_for_strand(seq, file_prefix="sega007", reverse=False)
# --- Process reverse complement strand ---
print("Processing reverse complement strand...")
rev_comp_seq = reverse_complement(seq)
write_files_for_strand(rev_comp_seq, file_prefix="sega007_rev", reverse=True)
print("Processing complete.")
if __name__ == "__main__":
main()
Key changes:
-
Added a reverse parameter to process_chunk() and write_files_for_strand() to indicate whether we're processing the reverse complement strand.
-
Modified the header coordinate calculation in process_chunk():
- For forward strand (unchanged): counts up from 1
- For reverse strand: counts down from the sequence length
- For reverse strand headers, ensures the larger number is always first
Now the output will look like:
Forward strand files:
>1-81
ACTG...
>2-82
CTGA...
Reverse complement strand files (assuming sequence length 1000000):
>1000000-999920
TGAC...
>999999-999919
GACT...
The coordinates in the reverse complement strand now count down from the total sequence length, while still maintaining the proper base order of the reverse complement sequence.