import numpy as np import pandas as pd --- Load PWM --- pwmfile = 'sigma70-PWM.txt'…
- User
import numpy as np import pandas as pd
--- Load PWM ---
pwm_file = 'sigma70-PWM.txt' pwm_df = pd.read_csv(pwm_file, sep='\t')
seq_length = len(pwm_df)
--- Load FASTA sequence ---
def read_fasta(filename): seq = [] with open(filename) as f: for line in f: if line.startswith('>'): continue seq.append(line.strip()) return ''.join(seq).lower()
fasta_file = 'sega007.fasta' # Change to your file path if needed sequence = read_fasta(fasta_file)
--- Slide window and calculate PWM scores ---
scores = [] for i in range(len(sequence) - seq_length + 1): window = sequence[i:i+seq_length] score = 0.0 for j, nt in enumerate(window): # Optionally, you can handle invalid characters as needed (e.g., skip window or assign a penalty). score += pwm_df.loc[j, nt.upper()] scores.append(score)
Pad scores with zeros to match the length of the FASTA sequence
padded_scores = np.zeros(len(sequence)) padded_scores[:len(scores)] = scores
--- Save array to file ---
np.save('sliding_scores-full-pos.npy', padded_scores) print(f"Saved {len(padded_scores)} scores to sliding_scores.npy")
Modify this file to store the scores at the position of +length pwm-20
- Assistant
Here’s one way to do it: instead of “left-padding” the score array, you explicitly place each window score at position
i + (L – 20)where L is your PWM length.
import numpy as np import pandas as pd # --- Load PWM --- pwm_file = 'sigma70-PWM.txt' pwm_df = pd.read_csv(pwm_file, sep='\t') L = len(pwm_df) # PWM length # --- Load FASTA sequence --- def read_fasta(filename): seq = [] with open(filename) as f: for line in f: if line.startswith('>'): continue seq.append(line.strip()) return ''.join(seq).lower() fasta_file = 'sega007.fasta' sequence = read_fasta(fasta_file) N = len(sequence) # --- Prepare output array --- # We'll store each score at position i + (L-20): scores_aligned = np.zeros(N, dtype=float) # --- Slide window and assign at offset --- for i in range(N - L + 1): window = sequence[i:i+L] score = 0.0 for j, nt in enumerate(window): score += pwm_df.loc[j, nt.upper()] # make sure nt in {A,C,G,T} pos = i + (L - 20) # only assign if inside bounds if 0 <= pos < N: scores_aligned[pos] = score # --- Save aligned scores --- np.save('sliding_scores_full_pos.npy', scores_aligned) print(f"Saved {N} scores to sliding_scores_full_pos.npy")Explanation of the key change:
- We create
scores_alignedof length N (the full sequence). - For each window starting at
i, we compute its PWM score. - We then store that score at
pos = i + (L - 20).- If you need 1-based positioning or some other off‐by‐one tweak, adjust the formula appropriately.
- We skip any windows whose
poswould fall outside[0, N-1].
This way you end up with a vector of length = sequence length, where each score is “anchored” at the 20th bp upstream of the window end (L-20).
- We create
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.