0

Given 2 strings, each containing a DNA sequence, the function returns a bool to show if a contiguous sub-fragment of length 5 or above exists in string1 that can pair w/ a fragment of str2.

Here is what I tried using the functions "complement" and "reverese_complement" that I created but it doesn't give the right result:

def complement5(sequence1,sequence2):
    for i in range(len(sequence1) - 4):
        substr1 = sequence1[i:i+5]
        if complement(substr1) in sequence2 or reverese_complement(substr1) in sequence2:
            print(f'{substr1, complement(substr1), reverese_complement(substr1)} in {sequence2}')
            return True
        else:
            return False

Then when I try: complement5('CAATTCC','CTTAAGG') it gives False instead of True

Erksterx
  • 5
  • 4

1 Answers1

-1

I personally would try to identify the whole complement first, then try to use the python function find(). See the example below

s = "This be a string"
if s.find("is") == -1:
    print("No 'is' here!")
else:
    print("Found 'is' in the string.")

So... for your genetics problem here:
Given str1 = "AGAGAG..."
Given str2 = "TCTCTC..."

Iterate through str1 sub-chunks (of length 5?) with a for loop and identify possible complements.

str1-complement-chunk = "TCTCTC" #use a method here. Obviously i'm simplifiying.
if (str2.find(str1-complement-chunk ) == -1):
   print("dang couldn't find it") 
else:
    print('There exists a complement at <index>')

Additionally, you may reverse the chunk if you need to do so. I believe it can be accessed with str1[:-1], or something like that idk tho.

Erksterx
  • 5
  • 4
  • One more thing, I hope your data set is small as you're using a slow language, trying to resolve a permutation intensive problem. I would recommend you use a more robust programming language before you tackle this problem on. – Erksterx Sep 13 '22 at 18:54