Here's a Python script that will help you align the promoters, extract coverage data, and create the output CSV file according to your specifications:
import pandas as pd
from Bio import SeqIO
from Bio.Seq import Seq
import numpy as np
def find_uppercase_position(promoter_seq):
"""Find position of uppercase letter in promoter sequence"""
for i, char in enumerate(promoter_seq):
if char.isupper():
return i
return -1
def get_coverage_at_position(pos, coverage_data, strand, seq_length):
"""Get coverage at a specific position, handling strand orientation"""
if strand == '+':
return coverage_data[pos] if 0 <= pos < seq_length else 0
else: # negative strand
return coverage_data[seq_length - pos - 1] if 0 <= pos < seq_length else 0
# Read the promoter CSV file
promoters_df = pd.read_csv('sigma70-promoters-filtered.csv', sep=';')
# Read the GenBank file
gb_record = SeqIO.read('sega007.gb', 'genbank')
genome_seq = str(gb_record.seq)
genome_seq_rc = str(gb_record.seq.reverse_complement())
genome_length = len(genome_seq)
# Prepare results list
results = []
# Process each promoter
for index, row in promoters_df.iterrows():
promoter_name = row['promoter_name']
promoter_seq = row['promoter_sequence']
promoter_len = len(promoter_seq)
# Find uppercase letter position
uppercase_pos = find_uppercase_position(promoter_seq)
# Search on positive strand
pos_start = genome_seq.find(promoter_seq)
if pos_start != -1:
pos_end = pos_start + promoter_len - 1
uppercase_genome_pos = pos_start + uppercase_pos
# Get coverage values
start_cov = get_coverage_at_position(pos_start, data, '+', genome_length)
end_cov = get_coverage_at_position(pos_end, data, '+', genome_length)
up_cov = get_coverage_at_position(uppercase_genome_pos, data, '+', genome_length)
results.append({
'promoter_name': promoter_name,
'promoter_sequence': promoter_seq,
'start_coverage': start_cov,
'end_coverage': end_cov,
'up_coverage': up_cov,
'strand': '+'
})
# Search on negative strand
neg_start = genome_seq_rc.find(promoter_seq)
if neg_start != -1:
# For negative strand, positions need to be converted
pos_end = genome_length - neg_start - 1 # end position in original sequence
pos_start = pos_end - promoter_len + 1 # start position in original sequence
uppercase_genome_pos = pos_start + uppercase_pos
# Get coverage values
start_cov = get_coverage_at_position(pos_start, data, '-', genome_length)
end_cov = get_coverage_at_position(pos_end, data, '-', genome_length)
up_cov = get_coverage_at_position(uppercase_genome_pos, data, '-', genome_length)
results.append({
'promoter_name': promoter_name,
'promoter_sequence': promoter_seq,
'start_coverage': start_cov,
'end_coverage': end_cov,
'up_coverage': up_cov,
'strand': '-'
})
# Create DataFrame from results
results_df = pd.DataFrame(results)
# Save to CSV
results_df.to_csv('sigma70-promoter-diff.csv', sep=';', index=False)
This script:
- Imports necessary libraries (pandas, Biopython, numpy)
- Defines helper functions:
find_uppercase_position: Finds the position of the uppercase letter in the promoter sequence
get_coverage_at_position: Gets coverage data at a specific position, handling both strands
- Reads the promoter CSV file and GenBank file
- For each promoter:
- Searches for matches on both positive and negative strands
- If found, extracts coverage data for start, end, and uppercase letter positions
- Stores the strand information
- Creates a DataFrame with the results
- Saves the results to a CSV file
The output file 'sigma70-promoter-diff.csv' will contain:
- promoter_name
- promoter_sequence
- start_coverage (coverage at the first position of the promoter)
- end_coverage (coverage at the last position of the promoter)
- up_coverage (coverage at the uppercase letter position)
- strand (+ or -)
Note that:
- If a promoter is found on both strands, it will appear twice in the output (once for each strand)
- If a promoter is not found, it will not appear in the output
- The script uses your existing 'data' numpy array for coverage information
- The script preserves the case sensitivity of the sequences during alignment
Make sure you have the required libraries installed (pandas, biopython) and that all input files are in the correct location before running the script.