1
$\begingroup$

I am trying to find some matching motifs on a sequence, as well as the position that the motif is located in and then output that into a fasta file. The code below shows that the motif [L*L*L] is present in the sequence, when I run it returns as "YES" but I do not know where it is positioned.

peptide1="MKFSNEVVHKSMNITEDCSALTGALLKYSTDKSNMNFETLYRDAAVESPQHEVSNESGSTLKEHDYFGLSEVSSSNSSSGKQPEKCCREELNLNESATTLQLGPPAAVKPSGHADGADAHDEGAGPENPAKRPAHHMQQESLADGRKAAAEMGSFKIQRKNILEEFRAMKAQAHMTKSPKPVHTMQHNMHASFSGAQMAFGGAKNNGVKRVFSEAVGGNHIAASGVGVGVREGNDDVSRCEEMNGTEQLDLKVHLPKGMGMARMAPVSGGQNGSAWRNLSFDNMQGPLNPFFRKSLVSKMPVPDGGDSSANASNDCANRKGMVASPSVQPPPAQNQTVGWPPVKNFNKMNTPAPPASTPARACPSVQRKGASTSSSGNLVKIYMDGVPFGRKVDLKTNDSYDKLYSMLEDMFQQYISGQYCGGRSSSSGESHWVASSRKLNFLEGSEYVLIYEDHEGDSMLVGDVPWELFVNAVKRLRIMKGSEQVNLAPKNADPTKVQVAVG"

if re.search(r"L*L*L", peptide1):
    print("YES")
else: 
    print("NO")

The code that I wrote to find the position is below, but when I run it says invalid syntax. Could you please assist as I am still new in Bioinformatics, and I have no clue whether in the right track or not.

for position in range(len(peptide1)):
    if peptide1[position:].startswith(re.search(r"L*L*L")):
        print(position+1)

Lastly once the position of these motifs has been identified, how do I highlight them such that they are visible in the output fasta file.

I am searching for LxLxL where x can be any amino acid. I used the * to indicate to python that it should use any amino acid, but I think I am incorrect. Could you please assist on how I should actually be writing that motif?

$\endgroup$
3
  • $\begingroup$ What is the actual pattern you are looking for? L*L*L* will match anything at all since L* means "0 or more L", so it will match L but it will also match X or anything else. $\endgroup$
    – terdon
    Nov 10, 2022 at 9:00
  • $\begingroup$ @terdon, I am searching for LxLxL where x can be any amino acid. I used the * to indicate to python that it should use any amino acid , but I think I am incorrect. Could you please assist on how I should actually be writing that motif. $\endgroup$
    – thole
    Nov 10, 2022 at 12:48
  • $\begingroup$ @terdon If for example I have a motif LLxxxLxPP, could you please advice on how I am supposed to write it inside re.search or re.finditer? this is where I get confused $\endgroup$
    – thole
    Nov 10, 2022 at 12:59

1 Answer 1

2
$\begingroup$

re.search method do not return a simply boolean but a useful object contains many information:

#! /usr/bin/env python3

import re

peptide1="MKFSNEVVHKSMNITEDCSALTGALLKYSTDKSNMNFETLYRDAAVESPQHEVSNESGSTLKEHDYFGLSEVSSSNSSSGKQPEKCCREELNLNESATTLQLGPPAAVKPSGHADGADAHDEGAGPENPAKRPAHHMQQESLADGRKAAAEMGSFKIQRKNILEEFRAMKAQAHMTKSPKPVHTMQHNMHASFSGAQMAFGGAKNNGVKRVFSEAVGGNHIAASGVGVGVREGNDDVSRCEEMNGTEQLDLKVHLPKGMGMARMAPVSGGQNGSAWRNLSFDNMQGPLNPFFRKSLVSKMPVPDGGDSSANASNDCANRKGMVASPSVQPPPAQNQTVGWPPVKNFNKMNTPAPPASTPARACPSVQRKGASTSSSGNLVKIYMDGVPFGRKVDLKTNDSYDKLYSMLEDMFQQYISGQYCGGRSSSSGESHWVASSRKLNFLEGSEYVLIYEDHEGDSMLVGDVPWELFVNAVKRLRIMKGSEQVNLAPKNADPTKVQVAVG"

re_search_result = re.search(r'L*L*L', peptide1)
if re_search_result:
    print("YES")
    motif_start_position=re_search_result.start()
    motif_end_position=re_search_result.end()
    motif_length = re_search_result.end() - re_search_result.start()
    print("Motif start at position: %d" % motif_start_position)
    print("Motif end at position  : %d" % motif_end_position)
    print("SO length is ......... : %d" % motif_length)
    print("The motif is : <%s>" % peptide1[motif_start_position:motif_end_position])
else:
    print("NO")

Result is:

YES
Motif start at position: 20
Motif end at position  : 488
SO length is ......... : 468
The motif is : <LTGALLKYSTDKSNMNFETLYRDAAVESPQHEVSNESGSTLKEHDYFGLSEVSSSNSSSGKQPEKCCREELNLNESATTLQLGPPAAVKPSGHADGADAHDEGAGPENPAKRPAHHMQQESLADGRKAAAEMGSFKIQRKNILEEFRAMKAQAHMTKSPKPVHTMQHNMHASFSGAQMAFGGAKNNGVKRVFSEAVGGNHIAASGVGVGVREGNDDVSRCEEMNGTEQLDLKVHLPKGMGMARMAPVSGGQNGSAWRNLSFDNMQGPLNPFFRKSLVSKMPVPDGGDSSANASNDCANRKGMVASPSVQPPPAQNQTVGWPPVKNFNKMNTPAPPASTPARACPSVQRKGASTSSSGNLVKIYMDGVPFGRKVDLKTNDSYDKLYSMLEDMFQQYISGQYCGGRSSSSGESHWVASSRKLNFLEGSEYVLIYEDHEGDSMLVGDVPWELFVNAVKRLRIMKGSEQVNL>

The motif L*L*L is a quite strange: L zero or many times followed by L zero or many times followed by followed by a L. It's an equivalent of L*L, equivalent of L+.

Do you search for L.*L.*L, L[^L]*L[^L]*L or simply LLL ?

Notes:

  • When you post code, use block delimited by two ``` (no by two simple `)
  • In Python, code indentation is very important, always use spaces (4)

If you want to loop over all motifs (like strings), use findall method:

#! /usr/bin/env python3

import re

peptide1="MKFSNEVVHKSMNITEDCSALTGALLKYSTDKSNMNFETLYRDAAVESPQHEVSNESGSTLKEHDYFGLSEVSSSNSSSGKQPEKCCREELNLNESATTLQLGPPAAVKPSGHADGADAHDEGAGPENPAKRPAHHMQQESLADGRKAAAEMGSFKIQRKNILEEFRAMKAQAHMTKSPKPVHTMQHNMHASFSGAQMAFGGAKNNGVKRVFSEAVGGNHIAASGVGVGVREGNDDVSRCEEMNGTEQLDLKVHLPKGMGMARMAPVSGGQNGSAWRNLSFDNMQGPLNPFFRKSLVSKMPVPDGGDSSANASNDCANRKGMVASPSVQPPPAQNQTVGWPPVKNFNKMNTPAPPASTPARACPSVQRKGASTSSSGNLVKIYMDGVPFGRKVDLKTNDSYDKLYSMLEDMFQQYISGQYCGGRSSSSGESHWVASSRKLNFLEGSEYVLIYEDHEGDSMLVGDVPWELFVNAVKRLRIMKGSEQVNLAPKNADPTKVQVAVG"

re_findall_result = re.findall(r'L[^L]*L[^L]*L', peptide1)
for element in re_findall_result:
    print("Element: <%s>" % element)

Result will be:

Element: <LTGALL>
Element: <LYRDAAVESPQHEVSNESGSTLKEHDYFGL>
Element: <LNLNESATTL>
Element: <LGPPAAVKPSGHADGADAHDEGAGPENPAKRPAHHMQQESLADGRKAAAEMGSFKIQRKNIL>
Element: <LDLKVHL>
Element: <LSFDNMQGPLNPFFRKSL>
Element: <LVKIYMDGVPFGRKVDLKTNDSYDKL>
Element: <LEDMFQQYISGQYCGGRSSSSGESHWVASSRKLNFL>
Element: <LIYEDHEGDSMLVGDVPWEL>

If you want all find objects like with search method, use finditer method:

#! /usr/bin/env python3

import re

peptide1="MKFSNEVVHKSMNITEDCSALTGALLKYSTDKSNMNFETLYRDAAVESPQHEVSNESGSTLKEHDYFGLSEVSSSNSSSGKQPEKCCREELNLNESATTLQLGPPAAVKPSGHADGADAHDEGAGPENPAKRPAHHMQQESLADGRKAAAEMGSFKIQRKNILEEFRAMKAQAHMTKSPKPVHTMQHNMHASFSGAQMAFGGAKNNGVKRVFSEAVGGNHIAASGVGVGVREGNDDVSRCEEMNGTEQLDLKVHLPKGMGMARMAPVSGGQNGSAWRNLSFDNMQGPLNPFFRKSLVSKMPVPDGGDSSANASNDCANRKGMVASPSVQPPPAQNQTVGWPPVKNFNKMNTPAPPASTPARACPSVQRKGASTSSSGNLVKIYMDGVPFGRKVDLKTNDSYDKLYSMLEDMFQQYISGQYCGGRSSSSGESHWVASSRKLNFLEGSEYVLIYEDHEGDSMLVGDVPWELFVNAVKRLRIMKGSEQVNLAPKNADPTKVQVAVG"

re_findall_result = re.finditer(r'L[^L]*L[^L]*L', peptide1)
for element in re_findall_result:
    print("peptide1[%s:%s]=<%s>" % (element.start(), element.end(), peptide1[element.start():element.end()]))

Result will be:

peptide1[20:26]=<LTGALL>
peptide1[39:69]=<LYRDAAVESPQHEVSNESGSTLKEHDYFGL>
peptide1[90:100]=<LNLNESATTL>
peptide1[101:163]=<LGPPAAVKPSGHADGADAHDEGAGPENPAKRPAHHMQQESLADGRKAAAEMGSFKIQRKNIL>
peptide1[248:255]=<LDLKVHL>
peptide1[278:296]=<LSFDNMQGPLNPFFRKSL>
peptide1[378:404]=<LVKIYMDGVPFGRKVDLKTNDSYDKL>
peptide1[407:443]=<LEDMFQQYISGQYCGGRSSSSGESHWVASSRKLNFL>
peptide1[449:469]=<LIYEDHEGDSMLVGDVPWEL>
$\endgroup$
6
  • $\begingroup$ thank you so much. I have another question, if for example the motif is written as LLxxLxPP, where x can be any amino acid, how must I write it? I think that is one of the things that confuses me $\endgroup$
    – thole
    Nov 10, 2022 at 12:40
  • $\begingroup$ So if I have LxxLxPP where x can be any amino acid, how must I write it inside the re.finditer ? This is where I get confused $\endgroup$
    – thole
    Nov 10, 2022 at 12:57
  • $\begingroup$ @thole in regular expressions, * means "0 or more of the previous character". To match any character, you want .. Also, [ABC] means "find either A or B or C". So the actual pattern you want is r'L.L.L' but of course that will also match LLLLL. $\endgroup$
    – terdon
    Nov 10, 2022 at 13:29
  • 1
    $\begingroup$ @terdon thank you, that makes a lot of sense. So if I had a motif LxxLxP then it would be r'L..L.P' $\endgroup$
    – thole
    Nov 10, 2022 at 13:43
  • $\begingroup$ Yes, exactly right @thole. $\endgroup$
    – terdon
    Nov 10, 2022 at 13:53

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.