Home > Enterprise >  Function that returns in a list the positions where the motif was found in a sequence
Function that returns in a list the positions where the motif was found in a sequence

Time:12-19

I have these two strings:

 t = 'GATATCATGCATATACTT'
 s = 'ATAT'

I am trying to create a function that returns in a list the positions where the reason for the string s was found in the sequence of the string t. That is, in this case, the positions would be [1, 10]. But also, if the reason is not found, the list will return [-1].

 def search_motif (t, s):
     search_motif = list()
     for i, x in enumerate(t):
         if s in x:
             search_motif.append(i)
     i   = 1

     return [-1] # this must go to the end

 print (search_motif (t, s))

Am I going the right way? Can someone please guide me?

CodePudding user response:

Using regular expressions seems to be a better way.

import re

def search_motif(t, s):
    p = re.compile(s)
    motif = []
    for mo in p.finditer(t):
        motif.append(mo.span()[0])
    
    if motif:
        return motif
    else:
        return -1

CodePudding user response:

You can use str.index for this

>>> def findall(text,subtext):
        i=0
        ind=[]
        while True:
            try:
                i = text.index(subtext,i) #the second argument tell where to start looking for the sub string
                ind.append(i)
                i  =1 #for the next iteration we start looking in the immediate next index
            except ValueError: #we break when no more occurrences are found 
                break 
        if i==0: #we check if we didn't found a match before
            ind.append(-1)
        return ind

        
>>> t = 'GATATCATGCATATACTT'
>>> s = 'ATAT'
>>> findall(t,s)
[1, 10]
>>> findall(t,"X")
[-1]
>>> 

Now about your code, there are 2 reason it doesn't work: the first is that you don't return the list you make and the second is because you are looking at the individual characters

>>> for i,x in enumerate("HELLO"):
    print(x)

    
H
E
L
L
O
>>>     

and if you aren't searching for a character you would never get a match

CodePudding user response:

Use the find method on https://pythonbasics.org/string-find/

Apologies for the poor formatting. I’m on mobile phone.

  • Related