<p>In general, I usually recommend Biopython for any sort of fasta parsing, but the code below should work as well.</p>
<pre class="lang-py prettyprint-override"><code>with open ('WT.fasta', 'r+') as in_f, open(&quot;WT_out.fasta&quot;, 'w') as out_f:
    for line in in_f:
        if line[0] == &quot;&gt;&quot;:
            out_f.write(&quot;&gt;&quot; + line.split('|')[1] + &quot;\n&quot;)
        else:
            out_f.write(line)
</code></pre>

CC BY-SA 4.0
2021-08-11T04:29:55.253Z
Hi, The code executes, does not show any error messages. But the file looks the same as before!
Noob
CC BY-SA 4.0
2021-08-11T04:30:36.963Z
@Noob which file are you looking at? The code creates a new file WT_out.fasta
Throckmorton
CC BY-SA 4.0
2021-08-11T04:33:07.693Z
Oh sorry! My bad :/... I am an absolute idiot!
Noob
CC BY-SA 4.0
2021-08-10T19:45:01.620Z
2021-08-10T19:45:01.620Z
Throckmorton
<p>You could do this with <code>awk</code> by processing the header:</p>
<pre><code>$ awk -v FS=&quot;|&quot; '{ if ($0 ~ /^&gt;/) { printf(&quot;&gt;%s\n&quot;, $2) } else print $0 }' in.fa &gt; out.fa
</code></pre>
<p>For example:</p>
<pre><code>% echo -e &quot;&gt;sp|Q9UDW1|QCR9_HUMAN Cytochrome b-c1 complex subunit 9 OS=Homo sapiens OX=9606 GN=UQCR10 PE=1 SV=3\nACCGT&quot; | awk -v FS=&quot;|&quot; '{ if ($0 ~ /^&gt;/) { printf(&quot;&gt;%s\n&quot;, $2) } else print $0 }' 
&gt;Q9UDW1
ACCGT
</code></pre>

CC BY-SA 4.0
2021-08-11T04:36:36.807Z
Hi @AlexReynolds, Thanks a lot! I will try it.
Noob
CC BY-SA 4.0
2021-08-10T21:16:55.313Z
2021-08-10T21:16:55.313Z
Alex Reynolds
<p>I have a file containing protein sequence information of 100 odd proteins. Each sequence starts with a header that looks like this:</p>
<pre><code>&gt;sp|Q9UDW1|QCR9_HUMAN Cytochrome b-c1 complex subunit 9 OS=Homo sapiens OX=9606 GN=UQCR10 PE=1 SV=3
</code></pre>
<p>I want to edit these header to look like this:</p>
<pre><code>&gt;Q9UDW1
</code></pre>
<p>I have tried the following code:</p>
<pre><code>import re
with open ('WT.fasta', 'r+') as f:
    contents = f.readlines()
    for i in range(len(contents)-1):
        target = re.compile(r'&gt;..\|(.*)\|.*', contents[i])    
        target.sub(r'\1', contents[i])
</code></pre>
<p>I think I am not being able to call the right methods to do this. I have been trying to read the documentation: <a href="https://docs.python.org/3/library/re.html" rel="nofollow noreferrer">https://docs.python.org/3/library/re.html</a>. I am not been able to understand it and feeling overwhelmed by the details. I would really appreciate if anyone could help or point me in the right direction. Thanks!</p>

CC BY-SA 4.0
2021-08-10T16:16:26.917Z
Do you absolutely need to use python? A simple `sed` would do this much faster.
Ram RS
CC BY-SA 4.0
2021-08-11T04:35:08.367Z
Hi @RamRS, I am actually very new to bioinformatics. I still have not learnt about ```sed``` or ```awk``` commands :-)
Noob
CC BY-SA 4.0
2021-08-11T15:24:55.807Z
You definitely should - these will save you a lot of time for tasks that are too small in get Python/R involved.
Ram RS
CC BY-SA 4.0
2021-08-11T19:03:53.080Z
@RamRS, I would love learn about them. Would you have any suggestions about any course, or tutorials on these topics?
Noob
CC BY-SA 4.0
2021-08-12T14:04:21.280Z
Just google around. The way to learn these would be using them on copies of files for tasks like your current question - start there and you'll find more and more places you can use them.
Ram RS
CC BY-SA 4.0
2021-08-12T14:29:10.107Z
Thanks a lot :-)
Noob
<python>
How to read and edit a FASTA file with python using regular expression?
# How to read and edit a FASTA file with python using regular expression?

(Asked by: username_0 on 2021-08-10)

I have a file containing protein sequence information of 100 odd proteins. Each sequence starts with a header that looks like this:
>sp|Q9UDW1|QCR9_HUMAN Cytochrome b-c1 complex subunit 9 OS=Homo sapiens OX=9606 GN=UQCR10 PE=1 SV=3

I want to edit these header to look like this:
>Q9UDW1

I have tried the following code:
import re
with open ('WT.fasta', 'r+') as f:
    contents = f.readlines()
    for i in range(len(contents)-1):
        target = re.compile(r'>..\|(.*)\|.*', contents[i])    
        target.sub(r'\1', contents[i])

I think I am not being able to call the right methods to do this. I have been trying to read the documentation: https://docs.python.org/3/library/re.html. I am not been able to understand it and feeling overwhelmed by the details. I would really appreciate if anyone could help or point me in the right direction. Thanks!

**Tags:** python
> **Comments:**
> **username_1**: Do you absolutely need to use python? A simple `sed` would do this much faster.  
> **username_0**: Hi @RamRS, I am actually very new to bioinformatics. I still have not learnt about ```sed``` or ```awk``` commands :-)  
> **username_1**: You definitely should - these will save you a lot of time for tasks that are too small in get Python/R involved.  
> **username_0**: @RamRS, I would love learn about them. Would you have any suggestions about any course, or tutorials on these topics?  
> **username_1**: Just google around. The way to learn these would be using them on copies of files for tasks like your current question - start there and you'll find more and more places you can use them.  
> **username_0**: Thanks a lot :-)  

---

## 2 answers

### Answer by username_2 on 2021-08-10. Score: 3 | Accepted answer

In general, I usually recommend Biopython for any sort of fasta parsing, but the code below should work as well.
with open ('WT.fasta', 'r+') as in_f, open("WT_out.fasta", 'w') as out_f:
    for line in in_f:
        if line[0] == ">":
            out_f.write(">" + line.split('|')[1] + "\n")
        else:
            out_f.write(line)

> **Comments:**
> **username_0**: Hi, The code executes, does not show any error messages. But the file looks the same as before!  
> **username_2**: @Noob which file are you looking at? The code creates a new file WT_out.fasta  
> **username_0**: Oh sorry! My bad :/... I am an absolute idiot!  

---

### Answer by username_3 on 2021-08-10. Score: 3

You could do this with awk by processing the header:
$ awk -v FS="|" '{ if ($0 ~ /^>/) { printf(">%s\n", $2) } else print $0 }' in.fa > out.fa

For example:
% echo -e ">sp|Q9UDW1|QCR9_HUMAN Cytochrome b-c1 complex subunit 9 OS=Homo sapiens OX=9606 GN=UQCR10 PE=1 SV=3\nACCGT" | awk -v FS="|" '{ if ($0 ~ /^>/) { printf(">%s\n", $2) } else print $0 }' 
>Q9UDW1
ACCGT

> **Comments:**
> **username_0**: Hi @AlexReynolds, Thanks a lot! I will try it.  

---

