I have file "sega007.fasta" with DNA sequence in FASTA format. I want you to write a…
- User
I have file "sega007.fasta" with DNA sequence in FASTA format. I want you to write a script that reads it and outputs basepairs in new file. Output basepais in the following way:
1-81 AAA...CCC 2-82 AAT...CCG 3-83 ATT...CGG
So with a sliding window. Output in multiple files, each 500100bp long, with 100bp overhang over the next file, so if the first ends at 500100, the second starts at 500000. For the last file, no overhang. Then do the same for reverse strand, but there, start from the other end, so the last base becomes the first base, count backwards, hope you understand what I mean. Call files sega007-XX.fasta, with XX being a number, for example 01 or 25. No more than 100!
- Assistant
Below is one example in Python. In this script we:
- Read the FASTA file “sega007.fasta”, ignoring the FASTA header line.
- Divide the full sequence into chunks of 500,100 bases. Each consecutive chunk “overlaps” the previous one by 100 bases (that is, if chunk #1 covers bases 1–500100 then chunk #2 starts at base 500000—the overlap is 500100 – 500000 + 1 = 101 bases, but this is exactly what the sample instruction indicates).
- For each chunk we “slide” an 81‑bp window one base at a time. For every window we output a header line showing the global base positions (for example “>1-81”, “>2-82”, …) and on the next line the 81‑bp string.
- We do the above processing twice. First we work on the original (forward) strand and write files named “sega007-XX.fasta” (where XX is a two–digit file number, numbered sequentially starting at 01). Then we “reverse” the sequence (so that the very last base now becomes the first) and do the same. For clarity the reverse–strand files are named “sega007_rev-XX.fasta”.
- The script enforces that no more than 100 files are written (if the genome is extraordinarily long, you might wish to tweak this).
You can adjust paths, file names and parameters if needed.
Below is the complete 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 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 the order so that the final base becomes the first. # Files will be named like: sega007_rev-01.fasta, sega007_rev-02.fasta, ... print("Processing reverse strand...") rev_seq = seq[::-1] # Reset the global coordinate to 1 for the reverse strand. write_files_for_strand(rev_seq, file_prefix="sega007_rev") print("Processing complete.") if __name__ == "__main__": main()
Explanation
-
Reading the FASTA
- The
read_fasta()function reads the file and returns a single string (ignoring lines that start with “>”).
- The
-
Chunking with Overlap
- We use a constant
CHUNK_SIZE = 500100and calculate the step between the start positions of consecutive chunks asSTEP = CHUNK_SIZE - OVERLAP(here, 500000). - For a chunk starting at index 0 (base 1), the first chunk goes from base 1 to base 500100. The next chunk begins at index 500000 (i.e. base 500001 if counting from 1) so that the last 100 bases of the previous chunk are included.
- We use a constant
-
Sliding Window
- Within each chunk, we loop from position 0 up to
len(chunk) - WINDOW_SIZE, and for each window we write a header (showing the global coordinates such as “>1-81”, “>2-82”, etc.) followed by the 81‑bp sequence.
- Within each chunk, we loop from position 0 up to
-
Reverse Strand
- To process the reverse strand, we simply take the reverse of the full sequence (
rev_seq = seq[::-1]) and then process it in exactly the same way. - The header “positions” will now refer to the ordering of the reversed sequence; if you require the true original coordinates (or a reverse–complement), additional work is needed.
- To process the reverse strand, we simply take the reverse of the full sequence (
-
File Naming
- The forward strand files are named
sega007-XX.fasta(for example,sega007-01.fasta,sega007-02.fasta, …) and the reverse strand files are namedsega007_rev-XX.fasta.
- The forward strand files are named
-
Limit to 100 Files
- The script stops after generating 100 files (per strand).
You can run this script from the command line (assuming you have Python 3) by saving it as, for example,
process_sega007.pyand running:python3 process_sega007.pyMake sure that
"sega007.fasta"is in the same directory (or update the path accordingly).Feel free to modify the script if you need to adjust parameters or if you want to output the headers in a different style.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.