I’m having a problem getting my code to do exactly what I want. I would like to define a function that takes two numbers as the arguments and used the numbers to look at a section of the RNA code which is previously stored as a string.
Then I would like to count all possible base pairs in the section so that ‘a’ and ‘u’ pair and ‘g’ and ‘c’ pair. However there has to be a gap of 3 characters between the pairs and the pairs cannot crossover. e.g. if rna[4] pairs with rna[10], rna[5] cannot pair with rna[12]. however if a pair occurred between 4 and 10 i.e. 5 and 9 that would be ok.
so far I have
def base_pairs(x,y):
return (x=='a' and y=='u' or
x=='u' and y=='a' or
x=='c' and y=='g' or
x=='g' and y=='c' or
x=='g' and y=='u' or
x=='u' and y=='g' )
rna = raw_input('Enter RNA sequence: ')
n = len(rna)
def opt(x,y):
for i in range(x,y-5):
j = i+4
if base_pairs(rna[i],rna[j])==1:
print i,j
a = i
b = j
if b-a > 3:
if base_pairs(a+1,b-1)==1:
print a+1,b-1
a = a+1
b = b-1
else:
j=j+1
for example when I input accguugacgcag I would like to use opt(0,12) and get 0,4 5,11 6,10
currently I only get 0,4
I’ve started on a new version of your function. I’ve incorporated the ideas I mentioned in the comments on your question. In addition, I’ve made the rna string a parameter, but this could be removed fairly easily. This version doesn’t produce the output you asked for, but it might be adaptable to meet your needs. The output on my machine is o,4 and 5,9. I couldn’t figure out why you would prefer 5,11 over 5,9 so I couldn’t change it to give that result.
Let me know if you have any questions on how this works.