1

I am trying to write a program which will search through a series of strings and return true if any part of a substring is found.

For example, say the substring I am interested in is:

GATCGATC

The program should return True for:

GGTGGATCGATC

And should also return true for (because it ends with GATC):

GGTGTTTTGATC

So far I have:

def matchpat(str1, str2):
    '''Find a pattern in a string'''
    if str1 in str2:
        return True
    else:
        return False

This function works, but only if the whole pattern is present, it will return False for partial matches.

8
  • 2
    Partial matches with any threshold in particular? Because a matching letter is also a partial match Commented Mar 28, 2019 at 10:49
  • Please clarify your task: "any part of a substring is found" - how big the part should be? Does one letter counts as part of the string? Or do we look at the fact that "GATCGATC" is repeating of "GATC" substring? Commented Mar 28, 2019 at 10:55
  • Please post a sample for both True and False cases. Commented Mar 28, 2019 at 10:57
  • 1
    So the sentence This is not working should also return TRUE because it ends with a substring of GATCGATC? (The sentence contains the letter G, which is a substring of GATCGATC) Commented Mar 28, 2019 at 11:01
  • Yes, if the string ends with a partial match, even a single G, then it should return True. Commented Mar 28, 2019 at 11:10

3 Answers 3

2

Hi i made this code which works. You can change it to be more dynamical with the variables

text = 'GGTGGATCGATC'
lookingFor = 'GATCGATC'

def method():
  print('in check')
  if lookingFor in text:
    return true
  else:
    return false

def main():
  method()
  if __name__ == "__main__":

If you want to make the method take in input you can pass it in the method definition:

def method(text, lookingFor)
Sign up to request clarification or add additional context in comments.

1 Comment

A lot of typos in the code, but it works :-). def method():, True, False, indents below def
0

I have used a library called Fuzzywuzzy for a similar problem, which worked well for my requirements and may help.

It uses Levenshtein distance metric to compare the strings.

1 Comment

Thank you for your suggestion, but I wanted to try and write the code without importing any modules.
0

You can use re module to do that

import re

patterntomatch = "GATCGATC"
patterntomatch = "[{0}]".format(patterntomatch)

TextTomatch = "This is something"

matchObj = re.match(patterntomatch,TextTomatch,re.I)

if matchObj:
    print ("match found")
else:
    print("no match found")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.