Here's a Python script that will process your CSV file, convert sequences to lowercase, calculate the position probability matrix (PPM) for nucleotides 'c', 't', and 'g', and save the results to sigma70-PPM.txt:
import pandas as pd
import numpy as np
# Read the CSV file
df = pd.read_csv('sigma70-promoters-filtered.csv', sep=';')
# Convert sequences to lowercase
sequences = df['promoter_sequence'].str.lower().tolist()
# Get sequence length (assuming all sequences are same length)
seq_length = len(sequences[0])
# Initialize count matrix for c, t, g
count_matrix = np.zeros((3, seq_length))
# Nucleotides we're interested in
nucleotides = ['c', 't', 'g']
# Count occurrences at each position
for seq in sequences:
for pos in range(seq_length):
if seq[pos] in nucleotides:
nuc_index = nucleotides.index(seq[pos])
count_matrix[nuc_index, pos] += 1
# Convert counts to probabilities
# Add pseudocount of 1 to avoid zero probabilities
total_sequences = len(sequences)
prob_matrix = (count_matrix + 1) / (total_sequences + 3) # +3 because we have 3 nucleotides
# Prepare output
output_lines = []
output_lines.append("Position Probability Matrix (PPM) for c, t, g")
output_lines.append("Position\tc\tt\tg")
# Add probabilities for each position
for pos in range(seq_length):
probs = prob_matrix[:, pos]
line = f"{pos+1}\t{probs[0]:.4f}\t{probs[1]:.4f}\t{probs[2]:.4f}"
output_lines.append(line)
# Write to file
with open('sigma70-PPM.txt', 'w') as f:
f.write('\n'.join(output_lines))
print("PPM has been saved to sigma70-PPM.txt")
This script will:
- Read your CSV file
- Convert all sequences to lowercase
- Create a matrix counting occurrences of 'c', 't', and 'g' at each position
- Convert counts to probabilities (with pseudocounts to avoid zeros)
- Save the results to sigma70-PPM.txt in a tab-separated format
The output file will look something like this:
Position Probability Matrix (PPM) for c, t, g
Position c t g
1 0.3333 0.3333 0.3333
2 0.2500 0.5000 0.2500
...
Each row represents a position in the sequence, and the columns show the probability of finding 'c', 't', or 'g' at that position. The probabilities at each position sum to 1 (considering only these three nucleotides, with 'a' implicitly making up the remainder).
Note that this script:
- Uses pseudocounts to avoid zero probabilities
- Assumes all sequences are the same length
- Ignores 'a' in the probability calculations (its probability can be inferred as 1 minus the sum of the other three)
- Doesn't normalize based on total nucleotides (only c, t, g are considered)