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
To modify the code to report the earliest hits instead of the best hits, you can make the following changes:
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_start = 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:
if start_35 < best_start:
best_start = start_35
best_result = (sub_35, score_35, best_window_10_sub, best_window_10_score, 'FOUND', best_window_10_pos - start_35)
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')
The changes made were:
find_promoter_sequences function, the best_distance variable was replaced with best_start.best_result was changed from distance < best_distance to start_35 < best_start.best_result tuple was updated to include the distance between the -35 and -10 hits, calculated as best_window_10_pos - start_35.