title: Editing FASTA: How to change the headers of FASTA using regex and list, and replace file
body: <p>I have a FASTA that looks like this: </p>
  
  <blockquote>
    <p>'>NZ_UARI01000011.1 Cronobacter sakazakii strain NCTC11467, whole genome shotgun sequence
    GCGCATTTCTTATTACGGAGAAATACAGCAGCGTGTCTGTTTCAATTTTCAGCTTGTTCCGGATTGTTAAAGAGCAAATA
    CTT...</p>
    
    <p>'>NZ_UARI01000001.1 Cronobacter sakazakii strain NCTC11467, whole genome shotgun sequence
    CAATTTTACTTGTTGATATAACAATCACGCTAACTATTCAGCCAATAGCTCCCGCATTAAAACCAGCTACTTCAGCCAAA
    ...</p>
  </blockquote>
  
  <p>And I want to change the headers to this: </p>
  
  <blockquote>
    <p>'>Cronobacter sakazakii strain NCTC11467_1
    GCGCATTTCTTATTACGGAGAAATACAGCAGCGTGTCTGTTTCAATTTTCAGCTTGTTCCGGATTGTTAAAGAGCAAATA
    CTT...
    '>Cronobacter sakazakii strain NCTC11467_2
    CAATTTTACTTGTTGATATAACAATCACGCTAACTATTCAGCCAATAGCTCCCGCATTAAAACCAGCTACTTCAGCCAAA
    ...
    (and so on)
    (Ignore the ' in the beginning of the header)</p>
  </blockquote>
  
  <p>And then I would like to save this file with the name of the header. Ideally I would not like to make a new fasta, but just replace the file with correctionss: 
  Cronobacter_sakazakii_strain NCTC11467.fasta</p>
  
  <p>Now this would be easy to do individually but I have over 600 files. So the idea of doing each individual is not a route I would like to take. I have written a script here where I isolate the section of the header I want using regex and store it in a list called new_new. I then want to match up these values and replace with every line that starts with a '>', and then add _1/2/3/... or a # subsequently (as shown above). 
  Can you help me with this task?
  If the script I provided is not worth continuing, and you have a better solution please let me know.</p>
  
  <pre><code>#usr/bin/python
  import sys
  import os
  import re
  import csv
  #sys.argv[1] =fasta 
  #sys.argv[2] = list of header names (mass) 
  #Gather existing headers to list (new_new)
  with open(sys.argv[1], "r+") as text_file:
      lines = text_file.readlines()[1:]
      mylist = []
      new_new = []
      for i in lines:
          if '.' in i:
              mylist.append(i)
      pattern = r"&gt;*Cronobacter +\w* +\w* +.*[,]" 
      regex = re.compile(pattern, re.IGNORECASE)
      for j in mylist: 
          for match in regex.finditer(j): 
              value = match.group(0)
              new_new.append(value)
              for k in lines: 
                  if '&gt;' in k:
                      k= k.replace('.*',new_new[value])
  text_file.close()
  '''
  
  
  </code></pre>