Absolutely! To make the reverse strand count backwards from the largest coordinate to the smallest, we need to adjust the global coordinate calculation for the reverse complement strand.
What changes?
For the forward strand, coordinates start at 1 and increase.
For the reverse complement strand, coordinates start at the length of the full sequence and decrease.
When sliding the window on the reverse complement strand, the header should reflect the original coordinates on the forward strand , but reversed.
How to implement?
Pass the full length of the original sequence to write_files_for_strand().
For the reverse complement strand, calculate the global coordinates as:
code Copy code
header_start = full_length - (global_offset + j) + 1
header_end = header_start - (WINDOW_SIZE - 1)
This means the window starting at position global_offset + j in the reverse complement corresponds to bases counting backward from the full length.
Updated code snippet for process_chunk and write_files_for_strand:
python Copy code
def process_chunk(seq, chunk_number, file_prefix, full_length=None, reverse=False):
"""
Writes sliding windows for a chunk.
If reverse=True, coordinates count backwards from full_length.
"""
global_start = process_chunk.global_offset
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 reverse:
# Coordinates count backwards from full_length
header_start = full_length - (global_start + j) + 1
header_end = header_start - (WINDOW_SIZE - 1)
# Make sure header_start > header_end for reverse strand
fout.write(f">{header_start}-{header_end}\n")
else:
header_start = global_start + j
header_end = header_start + WINDOW_SIZE - 1
fout.write(f">{header_start}-{header_end}\n")
fout.write(f"{seq[j:j+WINDOW_SIZE]}\n")
process_chunk.global_offset += len(seq)
def write_files_for_strand(full_seq, file_prefix, full_length=None, reverse=False):
seq_length = len(full_seq)
chunk_number = 1
process_chunk.global_offset = 0 # zero-based offset for easier calculation
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
chunk = full_seq[start:end]
process_chunk.global_offset = start
process_chunk(chunk, chunk_number, file_prefix, full_length=full_length, reverse=reverse)
chunk_number += 1
And update the main() accordingly:
python Copy code
def main():
input_filename = "sega007.fasta"
seq = read_fasta(input_filename)
full_length = len(seq)
print("Processing forward strand...")
write_files_for_strand(seq, file_prefix="sega007", full_length=full_length, reverse=False)
print("Processing reverse complement strand...")
rev_comp_seq = reverse_complement(seq)
write_files_for_strand(rev_comp_seq, file_prefix="sega007_rev", full_length=full_length, reverse=True)
print("Processing complete.")
Summary
For forward strand: coordinates increase from 1 upwards.
For reverse complement strand: coordinates count backward from the full sequence length downwards.
The sequence output is the reverse complement.
Headers reflect the original coordinate system but reversed for the reverse strand.
If you want, I can provide the full updated script with these changes integrated. Would you like that?