<p><code>re.search</code> method do not return a simply boolean but a useful object contains many information:</p>
<pre class="lang-py prettyprint-override"><code>#! /usr/bin/env python3

import re

peptide1=&quot;MKFSNEVVHKSMNITEDCSALTGALLKYSTDKSNMNFETLYRDAAVESPQHEVSNESGSTLKEHDYFGLSEVSSSNSSSGKQPEKCCREELNLNESATTLQLGPPAAVKPSGHADGADAHDEGAGPENPAKRPAHHMQQESLADGRKAAAEMGSFKIQRKNILEEFRAMKAQAHMTKSPKPVHTMQHNMHASFSGAQMAFGGAKNNGVKRVFSEAVGGNHIAASGVGVGVREGNDDVSRCEEMNGTEQLDLKVHLPKGMGMARMAPVSGGQNGSAWRNLSFDNMQGPLNPFFRKSLVSKMPVPDGGDSSANASNDCANRKGMVASPSVQPPPAQNQTVGWPPVKNFNKMNTPAPPASTPARACPSVQRKGASTSSSGNLVKIYMDGVPFGRKVDLKTNDSYDKLYSMLEDMFQQYISGQYCGGRSSSSGESHWVASSRKLNFLEGSEYVLIYEDHEGDSMLVGDVPWELFVNAVKRLRIMKGSEQVNLAPKNADPTKVQVAVG&quot;

re_search_result = re.search(r'L*L*L', peptide1)
if re_search_result:
    print(&quot;YES&quot;)
    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(&quot;Motif start at position: %d&quot; % motif_start_position)
    print(&quot;Motif end at position  : %d&quot; % motif_end_position)
    print(&quot;SO length is ......... : %d&quot; % motif_length)
    print(&quot;The motif is : &lt;%s&gt;&quot; % peptide1[motif_start_position:motif_end_position])
else:
    print(&quot;NO&quot;)
</code></pre>
<p>Result is:</p>
<pre><code>YES
Motif start at position: 20
Motif end at position  : 488
SO length is ......... : 468
The motif is : &lt;LTGALLKYSTDKSNMNFETLYRDAAVESPQHEVSNESGSTLKEHDYFGLSEVSSSNSSSGKQPEKCCREELNLNESATTLQLGPPAAVKPSGHADGADAHDEGAGPENPAKRPAHHMQQESLADGRKAAAEMGSFKIQRKNILEEFRAMKAQAHMTKSPKPVHTMQHNMHASFSGAQMAFGGAKNNGVKRVFSEAVGGNHIAASGVGVGVREGNDDVSRCEEMNGTEQLDLKVHLPKGMGMARMAPVSGGQNGSAWRNLSFDNMQGPLNPFFRKSLVSKMPVPDGGDSSANASNDCANRKGMVASPSVQPPPAQNQTVGWPPVKNFNKMNTPAPPASTPARACPSVQRKGASTSSSGNLVKIYMDGVPFGRKVDLKTNDSYDKLYSMLEDMFQQYISGQYCGGRSSSSGESHWVASSRKLNFLEGSEYVLIYEDHEGDSMLVGDVPWELFVNAVKRLRIMKGSEQVNL&gt;
</code></pre>
<p>The motif <code>L*L*L</code> is a quite strange: <code>L</code> zero or many times followed by <code>L</code> zero or many times followed by followed by a <code>L</code>. It's an equivalent of <code>L*L</code>, equivalent of <code>L+</code>.</p>
<p>Do you search for <code>L.*L.*L</code>, <code>L[^L]*L[^L]*L</code> or simply <code>LLL</code> ?</p>
<p>Notes:</p>
<ul>
<li>When you post code, use block delimited by two ``` (no by two simple `)</li>
<li>In Python, code indentation is very important, always use spaces (4)</li>
</ul>
<hr />
<p>If you want to loop over all motifs (like strings), use <code>findall</code> method:</p>
<pre class="lang-py prettyprint-override"><code>#! /usr/bin/env python3

import re

peptide1=&quot;MKFSNEVVHKSMNITEDCSALTGALLKYSTDKSNMNFETLYRDAAVESPQHEVSNESGSTLKEHDYFGLSEVSSSNSSSGKQPEKCCREELNLNESATTLQLGPPAAVKPSGHADGADAHDEGAGPENPAKRPAHHMQQESLADGRKAAAEMGSFKIQRKNILEEFRAMKAQAHMTKSPKPVHTMQHNMHASFSGAQMAFGGAKNNGVKRVFSEAVGGNHIAASGVGVGVREGNDDVSRCEEMNGTEQLDLKVHLPKGMGMARMAPVSGGQNGSAWRNLSFDNMQGPLNPFFRKSLVSKMPVPDGGDSSANASNDCANRKGMVASPSVQPPPAQNQTVGWPPVKNFNKMNTPAPPASTPARACPSVQRKGASTSSSGNLVKIYMDGVPFGRKVDLKTNDSYDKLYSMLEDMFQQYISGQYCGGRSSSSGESHWVASSRKLNFLEGSEYVLIYEDHEGDSMLVGDVPWELFVNAVKRLRIMKGSEQVNLAPKNADPTKVQVAVG&quot;

re_findall_result = re.findall(r'L[^L]*L[^L]*L', peptide1)
for element in re_findall_result:
    print(&quot;Element: &lt;%s&gt;&quot; % element)
</code></pre>
<p>Result will be:</p>
<pre><code>Element: &lt;LTGALL&gt;
Element: &lt;LYRDAAVESPQHEVSNESGSTLKEHDYFGL&gt;
Element: &lt;LNLNESATTL&gt;
Element: &lt;LGPPAAVKPSGHADGADAHDEGAGPENPAKRPAHHMQQESLADGRKAAAEMGSFKIQRKNIL&gt;
Element: &lt;LDLKVHL&gt;
Element: &lt;LSFDNMQGPLNPFFRKSL&gt;
Element: &lt;LVKIYMDGVPFGRKVDLKTNDSYDKL&gt;
Element: &lt;LEDMFQQYISGQYCGGRSSSSGESHWVASSRKLNFL&gt;
Element: &lt;LIYEDHEGDSMLVGDVPWEL&gt;
</code></pre>
<hr />
<p>If you want all find objects like with <code>search</code> method, use <code>finditer</code> method:</p>
<pre class="lang-py prettyprint-override"><code>#! /usr/bin/env python3

import re

peptide1=&quot;MKFSNEVVHKSMNITEDCSALTGALLKYSTDKSNMNFETLYRDAAVESPQHEVSNESGSTLKEHDYFGLSEVSSSNSSSGKQPEKCCREELNLNESATTLQLGPPAAVKPSGHADGADAHDEGAGPENPAKRPAHHMQQESLADGRKAAAEMGSFKIQRKNILEEFRAMKAQAHMTKSPKPVHTMQHNMHASFSGAQMAFGGAKNNGVKRVFSEAVGGNHIAASGVGVGVREGNDDVSRCEEMNGTEQLDLKVHLPKGMGMARMAPVSGGQNGSAWRNLSFDNMQGPLNPFFRKSLVSKMPVPDGGDSSANASNDCANRKGMVASPSVQPPPAQNQTVGWPPVKNFNKMNTPAPPASTPARACPSVQRKGASTSSSGNLVKIYMDGVPFGRKVDLKTNDSYDKLYSMLEDMFQQYISGQYCGGRSSSSGESHWVASSRKLNFLEGSEYVLIYEDHEGDSMLVGDVPWELFVNAVKRLRIMKGSEQVNLAPKNADPTKVQVAVG&quot;

re_findall_result = re.finditer(r'L[^L]*L[^L]*L', peptide1)
for element in re_findall_result:
    print(&quot;peptide1[%s:%s]=&lt;%s&gt;&quot; % (element.start(), element.end(), peptide1[element.start():element.end()]))
</code></pre>
<p>Result will be:</p>
<pre><code>peptide1[20:26]=&lt;LTGALL&gt;
peptide1[39:69]=&lt;LYRDAAVESPQHEVSNESGSTLKEHDYFGL&gt;
peptide1[90:100]=&lt;LNLNESATTL&gt;
peptide1[101:163]=&lt;LGPPAAVKPSGHADGADAHDEGAGPENPAKRPAHHMQQESLADGRKAAAEMGSFKIQRKNIL&gt;
peptide1[248:255]=&lt;LDLKVHL&gt;
peptide1[278:296]=&lt;LSFDNMQGPLNPFFRKSL&gt;
peptide1[378:404]=&lt;LVKIYMDGVPFGRKVDLKTNDSYDKL&gt;
peptide1[407:443]=&lt;LEDMFQQYISGQYCGGRSSSSGESHWVASSRKLNFL&gt;
peptide1[449:469]=&lt;LIYEDHEGDSMLVGDVPWEL&gt;
</code></pre>

CC BY-SA 4.0
2022-11-10T12:40:10.210Z
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
thole
CC BY-SA 4.0
2022-11-10T12:57:48.823Z
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
thole
CC BY-SA 4.0
2022-11-10T13:29:05.917Z
@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`.
terdon
CC BY-SA 4.0
2022-11-10T13:43:27.523Z
@terdon thank you, that makes a lot of sense. So if I had a motif LxxLxP then it would be r'L..L.P'
thole
CC BY-SA 4.0
2022-11-10T13:53:16.907Z
Yes, exactly right @thole.
terdon
CC BY-SA 4.0
2022-11-10T14:01:53.723Z
if you are interested by Regular Expression (RegEx) in Python, see https://www.w3schools.com/python/python_regex.asp
Arnaud Valmary
CC BY-SA 4.0
2022-11-10T11:36:05.570Z
2022-11-10T11:36:05.570Z
Arnaud Valmary
<p>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 <code>[L*L*L]</code> is present in the sequence, when I run it returns as &quot;YES&quot; but I do not know where it is positioned.</p>
<pre class="lang-py prettyprint-override"><code>peptide1=&quot;MKFSNEVVHKSMNITEDCSALTGALLKYSTDKSNMNFETLYRDAAVESPQHEVSNESGSTLKEHDYFGLSEVSSSNSSSGKQPEKCCREELNLNESATTLQLGPPAAVKPSGHADGADAHDEGAGPENPAKRPAHHMQQESLADGRKAAAEMGSFKIQRKNILEEFRAMKAQAHMTKSPKPVHTMQHNMHASFSGAQMAFGGAKNNGVKRVFSEAVGGNHIAASGVGVGVREGNDDVSRCEEMNGTEQLDLKVHLPKGMGMARMAPVSGGQNGSAWRNLSFDNMQGPLNPFFRKSLVSKMPVPDGGDSSANASNDCANRKGMVASPSVQPPPAQNQTVGWPPVKNFNKMNTPAPPASTPARACPSVQRKGASTSSSGNLVKIYMDGVPFGRKVDLKTNDSYDKLYSMLEDMFQQYISGQYCGGRSSSSGESHWVASSRKLNFLEGSEYVLIYEDHEGDSMLVGDVPWELFVNAVKRLRIMKGSEQVNLAPKNADPTKVQVAVG&quot;

if re.search(r&quot;L*L*L&quot;, peptide1):
    print(&quot;YES&quot;)
else: 
    print(&quot;NO&quot;)
</code></pre>
<p>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.</p>
<pre class="lang-py prettyprint-override"><code>for position in range(len(peptide1)):
    if peptide1[position:].startswith(re.search(r&quot;L*L*L&quot;)):
        print(position+1)
</code></pre>
<p>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.</p>
<p>I am searching for <code>LxLxL</code> where <code>x</code> can be any amino acid. I used the <code>*</code> 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?</p>

CC BY-SA 4.0
2022-11-10T09:00:00.633Z
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.
terdon
CC BY-SA 4.0
2022-11-10T12:48:01.780Z
@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.
thole
CC BY-SA 4.0
2022-11-10T12:59:54.063Z
@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
thole
<fasta><python><motifs><sequence>
finding matching motifs in a sequence and their position
# finding matching motifs in a sequence and their position

(Asked by: username_0 on 2022-11-09)

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?

**Tags:** fasta, python, motifs, sequence
> **Comments:**
> **username_1**: 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.  
> **username_0**: @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.  
> **username_0**: @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  

---

## 1 answers

### Answer by username_2 on 2022-11-10. Score: 2

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 : 

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: 
Element: 
Element: 
Element: 
Element: 
Element: 
Element: 
Element: 
Element: 


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]=
peptide1[39:69]=
peptide1[90:100]=
peptide1[101:163]=
peptide1[248:255]=
peptide1[278:296]=
peptide1[378:404]=
peptide1[407:443]=
peptide1[449:469]=

> **Comments:**
> **username_0**: 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  
> **username_0**: 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  
> **username_1**: @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`.  
> **username_0**: @terdon thank you, that makes a lot of sense. So if I had a motif LxxLxP then it would be r'L..L.P'  
> **username_1**: Yes, exactly right @thole.  
> **username_2**: if you are interested by Regular Expression (RegEx) in Python, see https://www.w3schools.com/python/python_regex.asp  

---

