Untitled conversation
- User
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
import numpy as np
import pandas as pd
import math
def load_pwm(pwm_file):
pwm_df = pd.read_csv(pwm_file, sep='\t')
seq_length = len(pwm_df)
valid_nts = set(pwm_df.columns.str.upper())
pwm_rows = [row.to_dict() for _, row in pwm_df.iterrows()]
return pwm_rows, seq_length, valid_nts
def best_pwm_hit(raw_seq, pwm_rows, seq_length, valid_nts):
if pd.isna(raw_seq):
return ('', np.nan)
seq = str(raw_seq).strip().lower()
if len(seq) < seq_length:
return ('', np.nan)
best_score = -math.inf
best_sub = ''
for i in range(len(seq) - seq_length + 1):
window = seq[i:i+seq_length]
score = 0.0
for j, nt in enumerate(window):
nt_u = nt.upper()
if nt_u in valid_nts:
score += pwm_rows[j][nt_u]
else:
score = -math.inf
break
if score > best_score:
best_score = score
best_sub = window
if best_score == -math.inf:
return ('', np.nan)
return (best_sub.upper(), best_score)
def find_promoter_sequences(promoters_df, pwm_35, pwm_10):
pwm_35_rows, seq_35_length, valid_nts_35 = pwm_35
pwm_10_rows, seq_10_length, valid_nts_10 = pwm_10
results = []
for _, row in promoters_df.iterrows():
seq = row['PromoterSequence']
if pd.isna(seq):
results.append(('FILTERED', np.nan, np.nan, 'FILTERED', np.nan, np.nan))
continue
seq = str(seq).strip().lower()
# Find best -35 and -10 hits
best_35_sub, best_35_score = best_pwm_hit(seq, pwm_35_rows, seq_35_length, valid_nts_35)
best_10_sub, best_10_score = best_pwm_hit(seq, pwm_10_rows, seq_10_length, valid_nts_10)
# Check if either score is below threshold
if best_35_score < 1.55 or best_10_score < 3.24:
results.append((best_35_sub, best_35_score, best_10_sub, best_10_score, 'FILTERED', np.nan))
continue
# Find all -35 hits above threshold
hits_35 = []
for i in range(len(seq) - seq_35_length + 1):
window = seq[i:i+seq_35_length]
score = 0.0
for j, nt in enumerate(window):
nt_u = nt.upper()
if nt_u in valid_nts_35:
score += pwm_35_rows[j][nt_u]
else:
score = -math.inf
break
if score >= 1.55:
hits_35.append((i, window.upper(), score))
# For each -35 hit, check for -10 hits in the downstream window
found = False
best_result = None
best_distance = math.inf
for start_35, sub_35, score_35 in hits_35:
end_35 = start_35 + seq_35_length
window_start = end_35 + 14
window_end = end_35 + 32
if window_end > len(seq):
continue
window_seq = seq[window_start:window_end]
# Find best -10 hit in this window
best_window_10_sub = ''
best_window_10_score = -math.inf
best_window_10_pos = -1
for i in range(len(window_seq) - seq_10_length + 1):
window = window_seq[i:i+seq_10_length]
score = 0.0
for j, nt in enumerate(window):
nt_u = nt.upper()
if nt_u in valid_nts_10:
score += pwm_10_rows[j][nt_u]
else:
score = -math.inf
break
if score > best_window_10_score:
best_window_10_score = score
best_window_10_sub = window.upper()
best_window_10_pos = window_start + i
if best_window_10_score >= 3.24:
distance = best_window_10_pos - start_35
if distance < best_distance:
best_distance = distance
best_result = (sub_35, score_35, best_window_10_sub, best_window_10_score, 'FOUND', distance)
found = True
if found:
results.append(best_result)
else:
results.append((best_35_sub, best_35_score, best_10_sub, best_10_score, 'NOTFOUND', np.nan))
return results
pwm_35 = load_pwm('sigma70-PWM-35.txt')
pwm_10 = load_pwm('sigma70-PWM-10.txt')
promoters_df = pd.read_csv('promoters_with_sequences-new.tsv', sep='\t')
results = find_promoter_sequences(promoters_df, pwm_35, pwm_10)
promoters_df['Best-35'] = [x[0] for x in results]
promoters_df['Score-35'] = [x[1] for x in results]
promoters_df['Best-10'] = [x[2] for x in results]
promoters_df['Score-10'] = [x[3] for x in results]
promoters_df['Status'] = [x[4] for x in results]
promoters_df['Distance'] = [x[5] for x in results]
promoters_df.to_csv('promoters_with_sequences-processed.tsv', sep='\t', index=False)
print('Finished. Results written to promoters_with_sequences-processed.tsv')
I don't want the best hits reported, I want the earliest hits
It looks like you're modifying your script to prioritize the earliest hits for the -35 and -10 boxes instead of the best (highest-scoring) hits. I'll walk you through the necessary changes to your code based on your request.
Understanding "Earliest Hits":
Where to Make Changes:
best_pwm_hit is currently used for initial filtering (to check if there are any hits above the thresholds). We'll leave this as is because it correctly determines if any hits exist above the threshold by finding the best score. If we change it to "earliest," it might not accurately filter sequences.find_promoter_sequences, specifically in the loop where you process the -35 hits and search for -10 hits in their downstream windows.Modified Code:
Here's the updated version of your script with the changes applied. I've only modified the relevant parts in find_promoter_sequences.
import numpy as np
import pandas as pd
import math
def load_pwm(pwm_file):
pwm_df = pd.read_csv(pwm_file, sep='\t')
seq_length = len(pwm_df)
valid_nts = set(pwm_df.columns.str.upper())
pwm_rows = [row.to_dict() for _, row in pwm_df.iterrows()]
return pwm_rows, seq_length, valid_nts
def best_pwm_hit(raw_seq, pwm_rows, seq_length, valid_nts):
if pd.isna(raw_seq):
return ('', np.nan)
seq = str(raw_seq).strip().lower()
if len(seq) < seq_length:
return ('', np.nan)
best_score = -math.inf
best_sub = ''
for i in range(len(seq) - seq_length + 1):
window = seq[i:i+seq_length]
score = 0.0
for j, nt in enumerate(window):
nt_u = nt.upper()
if nt_u in valid_nts:
score += pwm_rows[j][nt_u]
else:
score = -math.inf
break
if score > best_score:
best_score = score
best_sub = window
if best_score == -math.inf:
return ('', np.nan)
return (best_sub.upper(), best_score)
def find_promoter_sequences(promoters_df, pwm_35, pwm_10):
pwm_35_rows, seq_35_length, valid_nts_35 = pwm_35
pwm_10_rows, seq_10_length, valid_nts_10 = pwm_10
results = []
for _, row in promoters_df.iterrows():
seq = row['PromoterSequence']
if pd.isna(seq):
results.append(('FILTERED', np.nan, np.nan, 'FILTERED', np.nan, np.nan))
continue
seq = str(seq).strip().lower()
# Find best -35 and -10 hits for initial filtering
best_35_sub, best_35_score = best_pwm_hit(seq, pwm_35_rows, seq_35_length, valid_nts_35)
best_10_sub, best_10_score = best_pwm_hit(seq, pwm_10_rows, seq_10_length, valid_nts_10)
# Check if either score is below threshold
if best_35_score < 1.55 or best_10_score < 3.24:
results.append((best_35_sub, best_35_score, best_10_sub, best_10_score, 'FILTERED', np.nan))
continue
# Find all -35 hits above threshold
hits_35 = []
for i in range(len(seq) - seq_35_length + 1):
window = seq[i:i+seq_35_length]
score = 0.0
for j, nt in enumerate(window):
nt_u = nt.upper()
if nt_u in valid_nts_35:
score += pwm_35_rows[j][nt_u]
else:
score = -math.inf
break
if score >= 1.55:
hits_35.append((i, window.upper(), score))
# For each -35 hit (in order), check for the earliest -10 hit in the downstream window
found = False
best_result = None
for start_35, sub_35, score_35 in hits_35: # Process in order
end_35 = start_35 + seq_35_length
window_start = end_35 + 14
window_end = end_35 + 32
if window_end > len(seq):
continue
window_seq = seq[window_start:window_end]
# Find the earliest -10 hit in this window
for i in range(len(window_seq) - seq_10_length + 1): # Scan in order
sub_10 = window_seq[i:i+seq_10_length]
score_10 = 0.0
for j, nt in enumerate(sub_10):
nt_u = nt.upper()
if nt_u in valid_nts_10:
score_10 += pwm_10_rows[j][nt_u]
else:
score_10 = -math.inf
break
if score_10 >= 3.24: # First (earliest) -10 hit that meets threshold
best_window_10_sub = sub_10.upper()
best_window_10_score = score_10
best_window_10_pos = window_start + i
distance = best_window_10_pos - start_35
best_result = (sub_35, score_35, best_window_10_sub, best_window_10_score, 'FOUND', distance)
found = True
break # Break inner loop (stop after finding earliest -10 for this -35)
if found:
break # Break outer loop (stop after finding earliest valid pair)
if found:
results.append(best_result)
else:
results.append((best_35_sub, best_35_score, best_10_sub, best_10_score, 'NOTFOUND', np.nan))
return results
pwm_35 = load_pwm('sigma70-PWM-35.txt')
pwm_10 = load_pwm('sigma70-PWM-10.txt')
promoters_df = pd.read_csv('promoters_with_sequences-new.tsv', sep='\t')
results = find_promoter_sequences(promoters_df, pwm_35, pwm_10)
promoters_df['Best-35'] = [x[0] for x in results]
promoters_df['Score-35'] = [x[1] for x in results]
promoters_df['Best-10'] = [x[2] for x in results]
promoters_df['Score-10'] = [x[3] for x in results]
promoters_df['Status'] = [x[4] for x in results]
promoters_df['Distance'] = [x[5] for x in results]
promoters_df.to_csv('promoters_with_sequences-processed.tsv', sep='\t', index=False)
print('Finished. Results written to promoters_with_sequences-processed.tsv')
In find_promoter_sequences:
-35 hits (for start_35, sub_35, score_35 in hits_35) remains in order, so we're naturally processing the earliest -35 hits first.window_seq) and check each possible -10 subsequence in order.found = True and break out of the outer loop, ensuring we don't process later -35 hits.Why We Kept best_pwm_hit for Filtering:
if best_35_score < 1.55 or best_10_score < 3.24) uses the best scores to determine if there are any hits above the threshold. This is still valid because if the best score is below the threshold, no hits qualify. We don't want to change this to "earliest" here, as it could lead to incorrect filtering.Output:
promoters_with_sequences-processed.tsv) to verify that the reported -35 and -10 subsequences are from the earliest possible positions.If you need further adjustments or have additional requirements, let me know!