text: user8692: I have a file contain multiple sequence, and I want to separate them by "gene:" into different file.
  example: 
  
  example.fa
  >KQK21959
  ATGGTGCTCGACCCCGTCGTCGGGGGCTCGGCCTCCCGCAAGGTCGAGCCCCTAACCCTA
  
  >KQK14546
  ATGCCTCCGCCGGACGCGGCCACCTACGCTCCCACGCTGCCAATGGCGACGTACCTCCCA
  
  
  how to get results like this? one sequence per file, and after ">" is gene id, not cds id
  
  
  KQK21959.fa
      >KQK21959
      ATGGTGCTCGACCCCGTCGTCGGGGGCTCGGCCTCCCGCAAGGTCGAGCCCCTAACCCTA
  
  KQK14546.fa
      >KQK14546
      ATGCCTCCGCCGGACGCGGCCACCTACGCTCCCACGCTGCCAATGGCGACGTACCTCCCA
  
  
  user8795: That really depends on the tools you are using. If anything will do you can use the following python snippet.
  If you know that each sequence is on a single line and your file isnt gigantic use this
  EDIT
  Updated so the file isn't read to memory at once to address @terdon point. Although, the above solutions are probably much better.
  with open( "seqs.fasta" ) as f:
      out = ""
      
      for line in f:
          if not line.strip(): continue 
          if line.startswith( ">" ):
              if not type( out ) == str: out.close()
              gene = line[1:].strip().split()[0]
              out = open( f"{gene}.fa", "w" )
              out.write( line )
              continue 
          out.write( line.strip() )
      
  ```
  
  
  user104: A simple Biopython solution:
  from Bio import SeqIO
  
  for record in SeqIO.parse("example.fa", "fasta"):
      SeqIO.write(record, record.id + ".fa", "fasta")
  
  Note, if you have multiple sequences with the same id, only the last sequence will be kept in the final output files
  
  user439: This can also be done using awk. This is harder to read, but probably faster than python:
  awk 'sub(/^>/,""){
          FILE=$0".fa"; 
      print ">"$0 >FILE; 
          next
      }{print > FILE}' file.fa. 
  
  
  This will define a new output file FILE at each header line, whose filename is the header minus the > character, suffixed with .fa. And all lines in the file are appended to the current output file.
  EDIT: Changed to a faster version found on: https://crashcourse.housegordon.org/split-fasta-files.html#using-awk-instead-of-csplitsed
  
  user298: There's an app for that! The exonerate package (see here) includes a nice fastaexplode command that does exactly what you're looking for and will be much, much faster than anything we give you here since it is written in C and not a scripting language. Just install exonerate (you can do that with sudo apt install exonerate on Debian-based systems) and then:
  fastaexplode file.fa
  
  This will create these two files (file.fa is the original input file from your question):
  $ ls
  file.fa  KQK14546.fa  KQK21959.fa
  
  
original_text: 8692: <p>I have a file contain multiple sequence, and I want to separate them by "gene:" into different file.
  example: </p>
  
  <ul>
  <li><p>example.fa</p>
  
  <pre><code>&gt;KQK21959
  ATGGTGCTCGACCCCGTCGTCGGGGGCTCGGCCTCCCGCAAGGTCGAGCCCCTAACCCTA
  
  &gt;KQK14546
  ATGCCTCCGCCGGACGCGGCCACCTACGCTCCCACGCTGCCAATGGCGACGTACCTCCCA
  </code></pre>
  
  <ul>
  <li>how to get results like this? one sequence per file, and after ">" is gene id, not cds id</li>
  </ul></li>
  </ul>
  
  <p>KQK21959.fa</p>
  
  <pre><code>    &gt;KQK21959
      ATGGTGCTCGACCCCGTCGTCGGGGGCTCGGCCTCCCGCAAGGTCGAGCCCCTAACCCTA
  </code></pre>
  
  <p>KQK14546.fa</p>
  
  <pre><code>    &gt;KQK14546
      ATGCCTCCGCCGGACGCGGCCACCTACGCTCCCACGCTGCCAATGGCGACGTACCTCCCA
  </code></pre>
   8795: <p>That really depends on the tools you are using. If anything will do you can use the following python snippet.</p>
  <p>If you know that each sequence is on a single line and your file isnt gigantic use this</p>
  <h1>EDIT</h1>
  <p>Updated so the file isn't read to memory at once to address @terdon point. Although, the above solutions are probably much better.</p>
  <pre><code>with open( &quot;seqs.fasta&quot; ) as f:
      out = &quot;&quot;
      
      for line in f:
          if not line.strip(): continue 
          if line.startswith( &quot;&gt;&quot; ):
              if not type( out ) == str: out.close()
              gene = line[1:].strip().split()[0]
              out = open( f&quot;{gene}.fa&quot;, &quot;w&quot; )
              out.write( line )
              continue 
          out.write( line.strip() )
      
  <span class="math-container">```</span>
  </code></pre>
   104: <p>A simple Biopython solution:</p>
  
  <pre><code>from Bio import SeqIO
  
  for record in SeqIO.parse("example.fa", "fasta"):
      SeqIO.write(record, record.id + ".fa", "fasta")
  </code></pre>
  
  <p>Note, if you have multiple sequences with the same id, only the last sequence will be kept in the final output files</p>
   439: <p>This can also be done using awk. This is harder to read, but probably faster than python:</p>
  
  <pre><code>awk 'sub(/^&gt;/,""){
          FILE=<span class="math-container">$0".fa"; 
      print "&gt;"$</span>0 &gt;FILE; 
          next
      }{print &gt; FILE}' file.fa. 
  
  </code></pre>
  
  <p>This will define a new output file <code>FILE</code> at each header line, whose filename is the header minus the <code>&gt;</code> character, suffixed with <code>.fa</code>. And all lines in the file are appended to the current output file.</p>
  
  <p>EDIT: Changed to a faster version found on: <a href="https://crashcourse.housegordon.org/split-fasta-files.html#using-awk-instead-of-csplitsed" rel="nofollow noreferrer">https://crashcourse.housegordon.org/split-fasta-files.html#using-awk-instead-of-csplitsed</a></p>
   298: <p>There's an app for that! The <code>exonerate</code> package (see <a href="https://www.ebi.ac.uk/about/vertebrate-genomics/software/exonerate" rel="nofollow noreferrer">here</a>) includes a nice <code>fastaexplode</code> command that does exactly what you're looking for and will be much, much faster than anything we give you here since it is written in C and not a scripting language. Just install <code>exonerate</code> (you can do that with <code>sudo apt install exonerate</code> on Debian-based systems) and then:</p>
  
  <pre><code>fastaexplode file.fa
  </code></pre>
  
  <p>This will create these two files (<code>file.fa</code> is the original input file from your question):</p>
  
  <pre><code>$ ls
  file.fa  KQK14546.fa  KQK21959.fa
  </code></pre>
  