import numpy as np
import gzip

with gzip.open('sample_001_coverage_unique_no_genes.txt.gz', 'rt') as f:  
    data = np.loadtxt(f)
print(data.size)

import pandas as pd
from Bio import SeqIO
from Bio.Seq import Seq
import numpy as np

# Load the data (don't recreate, just assume it's available)
# data = np.load(...) # <- Already available per your instructions

# Step 1: Read the promoter CSV file
promoter_df = pd.read_csv("sigma70-promoters-filtered.csv", sep=";")

# Step 2: Parse the GenBank file to get the sequence
record = SeqIO.read("sega007.gb", "genbank")
genome_seq = str(record.seq).lower()  # Working in lowercase
genome_length = len(genome_seq)

# Step 3: Prepare output data
output_rows = []

# Step 4: Iterate through each promoter
for idx, row in promoter_df.iterrows():
    name = row["promoter_name"]
    seq = row["promoter_sequence"]

    # Identify the uppercase letter and its position
    upper_idx = next((i for i, c in enumerate(seq) if c.isupper()), None)
    if upper_idx is None:
        print(f"No uppercase found in promoter {name}. Skipping.")
        continue

    # Normalize: sequences are matched in lowercase
    promoter_seq = seq.lower()
    promoter_len = len(promoter_seq)

    found = False  # Flag to track if match is found

    for strand, seq_to_search in [('+', genome_seq), ('-', str(Seq(genome_seq).reverse_complement()))]:
        pos = seq_to_search.find(promoter_seq)
        if pos != -1:
            if strand == '+':
                start = pos
                end = pos + promoter_len - 1
                upper_pos = start + upper_idx
            else:
                # On negative strand, coordinates are reversed
                start = genome_length - pos - promoter_len
                end = genome_length - pos - 1
                # upper index is from reversed promoter string
                upper_pos = end - upper_idx

            # Get coverage at required positions
            try:
                start_cov = data[start]
                end_cov = data[end]
                upper_cov = data[upper_pos]

                # Save the result
                output_rows.append({
                    "promoter_name": name,
                    "promoter_sequence": seq,
                    "start_coverage": start_cov,
                    "end_coverage": end_cov,
                    "up_coverage": upper_cov,
                    "strand": strand
                })

                found = True
                break  # Stop checking once found

            except IndexError as e:
                print(f"Index out of bounds for promoter {name}: {e}")
                continue

    if not found:
        print(f"Promoter {name} not found in genome.")

# Step 5: Write the output to CSV
output_df = pd.DataFrame(output_rows)
output_df.to_csv("sigma70-promoter-diff.csv", sep=";", index=False)
print("Done. Results saved to sigma70-promoter-diff.csv")


See this shitcode? It does not find start and end of the promoter properly on the negative strand, it does it in reverse, upper is also screwed up. In particular the part with the coverage. FIX IT. Start on reverse strand is on the right, end is on the left, keep in mind upper too.