import numpy as np import pandas as pd --- Load PWM --- pwmfile = 'sigma70-PWM-10.txt'…
- User
import numpy as np import pandas as pd
--- Load PWM ---
pwm_file = 'sigma70-PWM-10.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-rev.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)
scores = np.array(scores)
--- Save array to file ---
np.save('sliding_scores.npy', scores) print(f"Saved {len(scores)} scores to sliding_scores.npy") Modify it so the output file has the same length as fasta file. Fill with zeros
- Assistant
To modify the code so that the output file has the same length as the FASTA file, you can pad the scores array with zeros. Here's the updated code:
import numpy as np import pandas as pd # --- Load PWM --- pwm_file = 'sigma70-PWM-10.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-rev.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.npy', padded_scores) print(f"Saved {len(padded_scores)} scores to sliding_scores.npy")Explanation:
- PWM Loading: The PWM is loaded from the file
sigma70-PWM-10.txt. - FASTA Sequence Loading: The FASTA sequence is read from the file
sega007-rev.fasta. - Sliding Window and Score Calculation: The code slides a window of length
seq_lengthacross the sequence and calculates the PWM score for each window. - Padding with Zeros: The scores array is padded with zeros to match the length of the FASTA sequence. This is done by creating a new array
padded_scoresof the same length as the sequence and filling the firstlen(scores)elements with the calculated scores. - Saving the Scores: The padded scores are saved to the file
sliding_scores.npy.
This ensures that the output file has the same length as the FASTA file, with scores for valid windows and zeros for the rest.
- PWM Loading: The PWM is loaded from the file
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.