Thank you for reporting the error!
The error TypeError: 'float' object is not iterable usually means that somewhere in the code, a float is being used where an iterable (like a list or dict) is expected.
In this context, the most likely cause is that the results list is being constructed incorrectly, possibly due to a missing or malformed dictionary.
Let's review and fix the code.
I'll also make the code more robust and add some extra checks.
Corrected and Improved Script
import numpy as np
import pandas as pd
from Bio import SeqIO
# Read the genomic sequence from FASTA file
with open('sega007.fasta', 'r') as f:
record = SeqIO.read(f, 'fasta')
genome_sequence = str(record.seq).upper() # Convert to uppercase for case-insensitive search
# Load coverage data
coverage = np.load('positive_strand_coverage1.npy')
# Read promoter sequences
promoters_df = pd.read_csv('sigma70-promoters-pwm.tsv', sep='\t')
# Prepare delta columns
delta_columns = ['delta0']
delta_columns += [f'delta{offset}' for offset in range(-500, 0, 10)]
delta_columns += [f'delta{offset}' for offset in range(10, 510, 10)]
# Store results as a list of dicts
results = []
for idx, row in promoters_df.iterrows():
promoter_seq = row['PromoterSequence']
# Find the position of the capital letter
capital_pos = next((i for i, c in enumerate(promoter_seq) if c.isupper()), None)
if capital_pos is None:
continue # Skip if no capital letter
promoter_upper = promoter_seq.upper()
start_pos = 0
found = False
while True:
pos = genome_sequence.find(promoter_upper, start_pos)
if pos == -1:
break
found = True
tss_position = pos + capital_pos
# Prepare result dict
result = {col: np.nan for col in delta_columns}
result['promoter_index'] = idx
result['delta0'] = 0 # By definition
for offset in range(-500, 510, 10):
if offset == 0:
continue
ref_pos = tss_position
comp_pos = tss_position + offset
if 0 <= ref_pos < len(coverage) and 0 <= comp_pos < len(coverage):
result[f'delta{offset}'] = float(coverage[comp_pos]) - float(coverage[ref_pos])
else:
result[f'delta{offset}'] = np.nan
results.append(result)
start_pos = pos + 1
if not found:
# Optionally, you can add a row with NaNs if promoter not found
pass
# Create DataFrame
if results:
df = pd.DataFrame(results)
# Ensure column order
df = df[['promoter_index'] + delta_columns]
df.to_csv('deltacoverage-promoters-positive.tsv', sep='\t', index=False)
print("Results saved to deltacoverage-promoters-positive.tsv")
else:
print("No promoter sequences found in the genome.")
Key Fixes and Notes
- Each
result is a dictionary, not a float.
- The
results list is a list of dicts, which is what pd.DataFrame() expects.
- The code now ensures that if a promoter is not found, it is simply skipped (you can change this if you want to record missing promoters).
- The column order is enforced for the output file.
- The code is robust to out-of-bounds coverage indices.
If you still get an error, please copy the full traceback and the line it points to, so I can help further!