I have file sega007.fasta with sequence in fasta format. Find all positions where 6 or more of the same nucleotide occur. Output starting position for each of them in a file polyN-pos.txt, each on new line
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
To find all positions where 6 or more of the same nucleotide (A, C, G, or T) occur consecutively in your FASTA file sega007.fasta, you can use a simple Python script. This script will:
Read the FASTA file.
Concatenate all sequence lines, ignoring the header (>...).
Search for runs of 6+ identical nucleotides.
Output the starting positions (1-based) to a file polyN-pos.txt, one per line.
🧬 Sample Python Script (find_polyN.py)
python
import re
# Input and output file names
input_file = 'sega007.fasta'
output_file = 'polyN-pos.txt'
# Read and concatenate sequence from input
sequence = ''
with open(input_file, 'r') as f:
for line in f:
if not line.startswith('>'):
sequence += line.strip().upper()
# Find runs of 6 or more identical nucleotides
pattern = r'(A{6,}|C{6,}|G{6,}|T{6,})'
# List to store starting positions
positions = []
# Use re.finditer to get match positions
for match in re.finditer(pattern, sequence):
start_pos = match.start() + 1 # +1 for 1-based indexing
positions.append(start_pos)
# Write positions to output file
with open(output_file, 'w') as out_f:
for pos in positions:
out_f.write(f"{pos}\n")
print(f"Found {len(positions)} polyN stretches. Output written to {output_file}.")
✅ How to Run the Script
Save the script as find_polyN.py.
Make sure your FASTA file is named sega007.fasta and is in the same directory.
Run it using Python:
bash
python3 find_polyN.py
It will generate polyN-pos.txt with 1-based starting positions of every run of 6+ same nucleotides.
📝 Example Output (polyN-pos.txt)
If your sequence had the following:
code
...CAAAGGGGGGTTTTTTTACGT...
Your output would be:
code
7
13
(assuming G's start at position 7 and T's at 13).
Let me know if you want a shell/awk/grep version or need to specify nucleotide (e.g., just polyA).