title: parsing a fasta file using a generator ( python )
body: <p>I am trying to parse a large fasta file and I am encountering out of memory errors. Some suggestions to improve the data handling would be appreciated. Currently the program correctly prints out the names however partially through the file I get a MemoryError </p>
  
  <p>Here is the generator</p>
  
  <pre><code>def readFastaEntry( fp ):
      name = ""
      seq = ""
      for line in fp:
          if line.startswith( "&gt;" ):
              tmp = []
              tmp.append( name )
              tmp.append( seq )
              name = line
              seq = ""
              yield tmp
          else:
              seq = seq.join( line )
  </code></pre>
  
  <p>and here is the caller stub more will be added after this part works</p>
  
  <pre><code>fp = open( sys.argv[1], 'r' )
  
  for seq in readFastaEntry( fp ) :
      print seq[0]
  </code></pre>
  
  <p>For those not fimilar with the fasta format here is an example</p>
  
  <pre><code>&gt;1 (PB2)
  AATATATTCAATATGGAGAGAATAAAAGAACTAAGAGATCTAATGTCACAGTCTCGCACTCGCGAGATAC
  TCACCAAAACCACTGTGGACCACATGGCCATAATCAAAAAGTACACATCAGGAAGGCAAGAGAAGAACCC
  TGCACTCAGGATGAAGTGGATGATG
  &gt;2 (PB1)
  AACCATTTGAATGGATGTCAATCCGACTTTACTTTTCTTGAAAGTTCCAGCGCAAAATGCCATAAGCACC
  ACATTTCCCTATACTGGAGACCCTCC
  </code></pre>
  
  <p>each entry starts with a ">" stating the name etc then the next N lines are data. There is no defined ending of the data other than the next line having a ">" at the beginning.</p>