Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-08-11T04:29:55.253Z
Answers.Text: Hi, The code executes, does not show any error messages. But the file looks the same as before!
Answers.UserDisplayName: Noob
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-08-11T04:30:36.963Z
Answers.Text: @Noob which file are you looking at? The code creates a new file WT_out.fasta
Answers.UserDisplayName: Throckmorton
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-08-11T04:33:07.693Z
Answers.Text: Oh sorry! My bad :/... I am an absolute idiot!
Answers.UserDisplayName: Noob
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-08-10T19:45:01.620Z
Answers.LastActivityDate: 2021-08-10T19:45:01.620Z
Answers.UserDisplayName: Throckmorton
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-08-11T04:36:36.807Z
Answers.Text: Hi @AlexReynolds, Thanks a lot! I will try it.
Answers.UserDisplayName: Noob
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-08-10T21:16:55.313Z
Answers.LastActivityDate: 2021-08-10T21:16:55.313Z
Answers.UserDisplayName: Alex Reynolds
Body: <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>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-08-10T16:16:26.917Z
Comments.Text: Do you absolutely need to use python? A simple `sed` would do this much faster.
Comments.UserDisplayName: Ram RS
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-08-11T04:35:08.367Z
Comments.Text: Hi @RamRS, I am actually very new to bioinformatics. I still have not learnt about ```sed``` or ```awk``` commands :-)
Comments.UserDisplayName: Noob
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-08-11T15:24:55.807Z
Comments.Text: You definitely should - these will save you a lot of time for tasks that are too small in get Python/R involved.
Comments.UserDisplayName: Ram RS
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-08-11T19:03:53.080Z
Comments.Text: @RamRS, I would love learn about them. Would you have any suggestions about any course, or tutorials on these topics?
Comments.UserDisplayName: Noob
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-08-12T14:04:21.280Z
Comments.Text: 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.
Comments.UserDisplayName: Ram RS
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-08-12T14:29:10.107Z
Comments.Text: Thanks a lot :-)
Comments.UserDisplayName: Noob
Tags: <python>
Title: How to read and edit a FASTA file with python using regular expression?
ThreadText: # 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 @username_1, 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**: @username_1, 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.
  
  ```py
  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**: @username_0 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 @username_3, Thanks a lot! I will try it.  
  
  ---
  
  