I'll help you find positions where 6 or more consecutive identical nucleotides occur in your FASTA file. Here's a Python script that will do this:
def find_poly_n_positions(fasta_file, output_file, min_length=6):
"""
Find positions where 6 or more consecutive identical nucleotides occur.
"""
import re
# Read the FASTA file
sequence = ""
with open(fasta_file, 'r') as f:
for line in f:
line = line.strip()
if not line.startswith('>'): # Skip header lines
sequence += line.upper()
# Find all positions with 6+ consecutive identical nucleotides
positions = []
# Pattern to match 6 or more consecutive identical nucleotides
pattern = r'([ATCG])\1{5,}'
for match in re.finditer(pattern, sequence):
start_pos = match.start() + 1 # Convert to 1-based indexing
positions.append(start_pos)
# Write positions to output file
with open(output_file, 'w') as f:
for pos in positions:
f.write(f"{pos}\n")
print(f"Found {len(positions)} positions with 6+ consecutive identical nucleotides")
print(f"Results written to {output_file}")
# Run the function
find_poly_n_positions('sega007.fasta', 'polyN-pos.txt')
Save this as a Python script (e.g., find_poly_n.py) and run it in the same directory as your sega007.fasta file.
The script will:
- Read your FASTA file and extract the sequence (ignoring header lines)
- Use a regular expression to find all occurrences of 6 or more consecutive identical nucleotides (A, T, C, or G)
- Record the starting position of each occurrence (using 1-based indexing)
- Write all positions to
polyN-pos.txt, one per line
If you prefer a one-liner approach using command-line tools, you could also use this bash command (though the Python script is more reliable):
grep -v "^>" sega007.fasta | tr -d '\n' | grep -oP -n '([ATCG])\1{5,}' | cut -d: -f1 > polyN-pos.txt
The Python script is recommended as it handles FASTA format more robustly and provides clearer output.