text: user7168: 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!
  
  user6628: 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)
  
  
  user776: 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
  
  
original_text: 7168: <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>
   6628: <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>
   776: <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>
  