Almost right, by the reverse strand I did mean complementary strand, so invert the…
- User
Almost right, by the reverse strand I did mean complementary strand, so invert the bases, please.
Assistant
To get the complementary strand, we need to replace each base with its complement. Here's the updated 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 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 reverse_complement(seq): """ Returns the reverse complement of a DNA sequence. """ complement = {'A': 'T', 'C': 'G', 'G': 'C', 'T': 'A'} return ''.join([complement[base] for base in seq[::-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 strand --- # For the reverse strand, we reverse-complement the sequence. # Files will be named like: sega007_rev-01.fasta, sega007_rev-02.fasta, ... print("Processing reverse strand...") rev_seq = reverse_complement(seq) # Reset the global coordinate to 1 for the reverse strand. process_chunk.global_offset = 1 write_files_for_strand(rev_seq, file_prefix="sega007_rev") print("Processing complete.") if __name__ == "__main__": main()The
reverse_complement()function takes a DNA sequence and returns its reverse complement. This function uses a dictionary to map each base to its complement and then reverses the sequence using slicing ([::-1]). The result is the reverse complement of the input sequence.Note that I also reset the
global_offsetto 1 before processing the reverse strand, so that the coordinates in the output files are correct.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.