Certainly! Here’s a step-by-step Python script to accomplish your task. This script will:
- Read the promoter CSV and parse the sequences, noting the position of the uppercase letter.
- Read the GenBank file to get the full sequence.
- Align each promoter (both strands) to the GenBank sequence, locating the match and the position of the uppercase letter.
- Extract coverage values from your existing
data numpy array for the start, end, and uppercase letter positions.
- Write the results to a new CSV file in the specified format.
- Delete unused variables to limit memory usage.
Assumptions:
- Your
data numpy array is already loaded and matches the length of the sequence in sega007.gb.
- The GenBank file contains a single sequence.
- The CSV uses
; as a delimiter.
Here’s the code:
import numpy as np
import pandas as pd
from Bio import SeqIO
from Bio.Seq import Seq
# 1. Read the promoter CSV
promoters = pd.read_csv('sigma70-promoters-filtered.csv', sep=';')
# 2. Read the GenBank file to get the sequence
record = next(SeqIO.parse('sega007.gb', 'genbank'))
genome_seq = str(record.seq).lower() # Lowercase for matching
# 3. Prepare output
output_rows = []
for idx, row in promoters.iterrows():
name = row['promoter_name']
seq = row['promoter_sequence']
# Find uppercase letter position
for i, c in enumerate(seq):
if c.isupper():
up_idx = i
break
else:
# No uppercase letter found, skip
continue
seq_lower = seq.lower()
seq_rc = str(Seq(seq_lower).reverse_complement())
found = False
# Search positive strand
pos = genome_seq.find(seq_lower)
if pos != -1:
strand = '+'
found = True
start = pos
end = pos + len(seq) - 1
up_pos = pos + up_idx
start_cov = data[start]
end_cov = data[end]
up_cov = data[up_pos]
output_rows.append([name, seq, start_cov, end_cov, up_cov, strand])
# Search negative strand
pos_rc = genome_seq.find(seq_rc)
if pos_rc != -1:
strand = '-'
found = True
start = pos_rc
end = pos_rc + len(seq) - 1
# For reverse complement, uppercase position is reversed
up_pos = pos_rc + (len(seq) - 1 - up_idx)
start_cov = data[start]
end_cov = data[end]
up_cov = data[up_pos]
output_rows.append([name, seq, start_cov, end_cov, up_cov, strand])
# Clean up memory
del seq_lower, seq_rc
# Clean up large variables
del genome_seq, promoters, record
# 4. Write to output CSV
import csv
with open('sigma70-promoter-diff.csv', 'w', newline='') as f:
writer = csv.writer(f, delimiter=';')
writer.writerow(['promoter_name', 'promoter_sequence', 'start_coverage', 'end_coverage', 'up_coverage', 'strand'])
writer.writerows(output_rows)
Notes:
- This script will output one row per match (so if a promoter matches both strands, it will appear twice).
- If a promoter is not found, it is skipped.
- The uppercase letter position is handled for both strands.
- Unused variables are deleted as soon as possible to save memory.
If you have any questions or need further customization, let me know!