You're right to be meticulous about negative strand calculations; they are notoriously tricky! Let's double-check the logic for the negative strand coordinates.
Assumptions & Definitions:
promoter_seq: The promoter sequence (e.g., acgT) as read from the input file, then lowercased for matching.
upper_idx: The 0-based index of the uppercase letter in the original promoter_sequence (e.g., for acgT, upper_idx is 3).
RC Genome (0-indexed):
G'_{genome_length-1} ... G'_1 G'_0
where G'_i is the complement of G_i.
The match promoter_seq (let its letters be p_0 p_1 ... p_{promoter_len-1}) starts at seq_to_search[pos].
So, seq_to_search[pos + k] == p_k.
The character seq_to_search[j] corresponds to genome_seq[genome_length - 1 - j] (its complement, actually, but we're getting genome coordinates).
The first base of the promoter match on RC genome is seq_to_search[pos].
This corresponds to the genome positive strand position: genome_length - 1 - pos. This will be the end of the match on the positive strand.
The last base of the promoter match on RC genome is seq_to_search[pos + promoter_len - 1].
This corresponds to the genome positive strand position: genome_length - 1 - (pos + promoter_len - 1) = genome_length - pos - promoter_len. This will be the start of the match on the positive strand.
So, on the positive strand of the genome, the region that is the reverse complement of our promoter sequence is:
else: # strand == '-'
# On negative strand, coordinates are reversed
# 'start' and 'end' here are genomic coordinates on the positive strand
# for the region that is the reverse complement of the promoter.
start = genome_length - pos - promoter_len
end = genome_length - pos - 1
This part is correct. The sequence genome_seq[start : end+1] is the reverse complement of promoter_seq.
Position of the Uppercase Letter:
The upper_idx is the 0-based index of the uppercase letter in the original 5'-3' promoter sequence.
Example: p_0 p_1 p_UPPER ... p_{promoter_len-1}. upper_idx points to p_UPPER.
When this promoter is on the negative strand, the corresponding sequence on the positive strand is p'_{promoter_len-1} ... p'_{UPPER} ... p'_1 p'_0.
(where p'_i is complement of p_i).
p'_0 (complement of the 1st base of promoter) is at end_on_genome.
p'_1 (complement of the 2nd base of promoter) is at end_on_genome - 1.
...
p'_{UPPER} (complement of the uppercase base) is at end_on_genome - upper_idx.
So, the genomic coordinate for the base corresponding to the promoter's uppercase letter is:
upper_pos_on_genome = end_on_genome - upper_idx
This matches the code:
python
# upper index is from the original promoter string (5' to 3')
# If promoter is P1 P2 P_UP P4
# On positive strand, its RC is P4' P_UP' P2' P1'
# P1' is at 'end_on_genome'
# P_UP' is at 'end_on_genome - upper_idx'
upper_pos = end - upper_idx
rc_genome_seq = Seq("agcttttcgat").reverse_complement() which is atcgaaaagct
pos = rc_genome_seq.find("tcgaa")
atcgaaaagct.find(tcgaa) -> pos = 1
genome_length = 11, promoter_len = 5
start = 11 - 1 - 5 = 5
end = 11 - 1 - 1 = 9
So, on the positive genome, the match region is genome_seq[5:10], which is genome_seq[5], genome_seq[6], genome_seq[7], genome_seq[8], genome_seq[9].
genome_seq: a g c t t T T C G A t (indices 0-10)
genome_seq[5:10] is ttcga.
Is ttcga the reverse complement of tcgaa?
Seq("tcgaa").reverse_complement() is ttcga. Yes, it is.
upper_pos = end - upper_idx = 9 - 2 = 7.
This means genome_seq[7].
genome_seq[7] is 'c'.
The original uppercase letter was 'G'. Its complement is 'C'. This matches.
The logic for calculating start, end, and upper_pos for the negative strand seems sound. The coordinates derived are for the positive strand of the genome, representing the location where the reverse complement of the promoter sequence lies. The upper_pos correctly identifies the base on the positive strand that corresponds (by complementation and reversal) to the original uppercase letter in the promoter.
The code seems correct for the negative strand as well.