<p>This is how it managed to give the desired output.</p>
<pre><code>import Bio
import re

from Bio import SeqIO
input_file = 'sequences.fasta'
fasta_sequences = SeqIO.parse(open(input_file),'fasta')
for fasta in fasta_sequences:
     name, sequence = fasta.id, str(fasta.seq)
     matches=re.finditer(r&quot;[YFWLIMVA]..[LMALVN].[AGSTCD]..[LAIVNFYMW]&quot;,sequence)
     for x in matches:
     print(name, x.start(), x.end()+6,sequence[x.start():x.end()+6])

#Output
P1 33 47 VTLLPAADLLMAIID
</code></pre>

CC BY-SA 4.0
2022-11-19T10:31:38.820Z
This looks like a great answer, which has worked for you. Can you please flag this as your accepted answer (it's okay to do this for your own answer), so that the StackExchange automated algorithms do the right thing?
gringer
CC BY-SA 4.0
2022-11-17T10:47:50.243Z
2022-11-17T10:47:50.243Z
thole
<p>Just add 6 extra aminoacids to your motif:</p>
<pre><code>matches=re.finditer(r&quot;[YFWLIMVA]..[LMALVN].[AGSTCD]..[LAIVNFYMW]......&quot;,sequence)
</code></pre>
<p>That's all you need, now the existing code will give you the answer you want.</p>

CC BY-SA 4.0
2022-11-17T12:02:04.597Z
2022-11-17T12:02:04.597Z
terdon
<p>I am able to find matching motif in my sequence, and I would like to now find overlapping motifs. Basically, after matching my motif, I want to find the 6 amino acids after it. This is the code below that I used to find the motif:</p>
<pre><code>import Bio
import regex

from Bio import SeqIO
input_file = 'sequences.fasta'
fasta_sequences = SeqIO.parse(open(input_file),'fasta')
for fasta in fasta_sequences:
     name, sequence = fasta.id, str(fasta.seq)
     result=regex.finditer(r&quot;[YFWLIMVA]..[LMALVN]..[AGSTCD].[LAIVNFYMW]&quot;,sequence)
     for x in result:
     print(name, x.start(), x.end(), x.group())
</code></pre>
<p>The above code works perfectly becasue it give me the sequence id, positions and the motif. The output is below</p>
<pre><code>P1  33 41 VTLLPAADL
</code></pre>
<p>Right now, what I want to do is to also get the 6 amino acids after matching this motif, such that I get an output like the one below where the length will now be 15,.</p>
<pre><code>P1 33 47 VTLLPAADLLMAIID
</code></pre>
<p>The code that I have tried to get the 6 amino acids after my match is below.</p>
<pre><code>import Bio
import regex

from Bio import SeqIO
input_file = 'sequences.fasta'
fasta_sequences = SeqIO.parse(open(input_file),'fasta')
for fasta in fasta_sequences:
     name, sequence = fasta.id, str(fasta.seq)
     result=regex.finditer(r&quot;[YFWLIMVA]..[LMALVN]..[AGSTCD].[LAIVNFYMW]&quot;,sequence)
     for x in result:
     print(name, x.start(), x.end() + 6, x.group())
</code></pre>
<p>This the output it gives me:</p>
<pre><code>#It does not extend my motif by 6 amino acids, after getting the match, it only extends the 
#end position by 6. From 41 to 47.     
P1  33 47 VTLLPAADL 

#My desired output is this which include the overlapping LMAIID, the LMAIID is after the 
#match.
P1   33 47 VTLLPAADLLMAIID
</code></pre>
<p>I also tried the code below, but it returns an error.</p>
<pre><code>import Bio
import regex

from Bio import SeqIO
input_file = 'sequences.fasta'
fasta_sequences = SeqIO.parse(open(input_file),'fasta')
for fasta in fasta_sequences:
     name, sequence = fasta.id, str(fasta.seq)
     result=regex.finditer(r&quot;[YFWLIMVA]..[LMALVN]..[AGSTCD].[LAIVNFYMW]&quot;,sequence)
     for x in result:
     print(name, x.start(), x.end() + 6, x.group() +6)
</code></pre>

CC BY-SA 4.0
2022-11-20T10:17:21.977Z
Cross-post https://www.biostars.org/p/9545486/#9545494
M__
<proteins><python><motifs><multi-fasta>
finding overlapping motifs to increase length of motif
# finding overlapping motifs to increase length of motif

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

I am able to find matching motif in my sequence, and I would like to now find overlapping motifs. Basically, after matching my motif, I want to find the 6 amino acids after it. This is the code below that I used to find the motif:
import Bio
import regex

from Bio import SeqIO
input_file = 'sequences.fasta'
fasta_sequences = SeqIO.parse(open(input_file),'fasta')
for fasta in fasta_sequences:
     name, sequence = fasta.id, str(fasta.seq)
     result=regex.finditer(r"[YFWLIMVA]..[LMALVN]..[AGSTCD].[LAIVNFYMW]",sequence)
     for x in result:
     print(name, x.start(), x.end(), x.group())

The above code works perfectly becasue it give me the sequence id, positions and the motif. The output is below
P1  33 41 VTLLPAADL

Right now, what I want to do is to also get the 6 amino acids after matching this motif, such that I get an output like the one below where the length will now be 15,.
P1 33 47 VTLLPAADLLMAIID

The code that I have tried to get the 6 amino acids after my match is below.
import Bio
import regex

from Bio import SeqIO
input_file = 'sequences.fasta'
fasta_sequences = SeqIO.parse(open(input_file),'fasta')
for fasta in fasta_sequences:
     name, sequence = fasta.id, str(fasta.seq)
     result=regex.finditer(r"[YFWLIMVA]..[LMALVN]..[AGSTCD].[LAIVNFYMW]",sequence)
     for x in result:
     print(name, x.start(), x.end() + 6, x.group())

This the output it gives me:
#It does not extend my motif by 6 amino acids, after getting the match, it only extends the 
#end position by 6. From 41 to 47.     
P1  33 47 VTLLPAADL 

#My desired output is this which include the overlapping LMAIID, the LMAIID is after the 
#match.
P1   33 47 VTLLPAADLLMAIID

I also tried the code below, but it returns an error.
import Bio
import regex

from Bio import SeqIO
input_file = 'sequences.fasta'
fasta_sequences = SeqIO.parse(open(input_file),'fasta')
for fasta in fasta_sequences:
     name, sequence = fasta.id, str(fasta.seq)
     result=regex.finditer(r"[YFWLIMVA]..[LMALVN]..[AGSTCD].[LAIVNFYMW]",sequence)
     for x in result:
     print(name, x.start(), x.end() + 6, x.group() +6)

**Tags:** proteins, python, motifs, multi-fasta
> **Comments:**
> **username_1**: Cross-post https://www.biostars.org/p/9545486/#9545494  

---

## 2 answers

### Answer by username_0 on 2022-11-17. Score: 1 | Accepted answer

This is how it managed to give the desired output.
import Bio
import re

from Bio import SeqIO
input_file = 'sequences.fasta'
fasta_sequences = SeqIO.parse(open(input_file),'fasta')
for fasta in fasta_sequences:
     name, sequence = fasta.id, str(fasta.seq)
     matches=re.finditer(r"[YFWLIMVA]..[LMALVN].[AGSTCD]..[LAIVNFYMW]",sequence)
     for x in matches:
     print(name, x.start(), x.end()+6,sequence[x.start():x.end()+6])

#Output
P1 33 47 VTLLPAADLLMAIID

> **Comments:**
> **username_2**: This looks like a great answer, which has worked for you. Can you please flag this as your accepted answer (it's okay to do this for your own answer), so that the StackExchange automated algorithms do the right thing?  

---

### Answer by username_3 on 2022-11-17. Score: 0

Just add 6 extra aminoacids to your motif:
matches=re.finditer(r"[YFWLIMVA]..[LMALVN].[AGSTCD]..[LAIVNFYMW]......",sequence)

That's all you need, now the existing code will give you the answer you want.


---

