def split_fasta(infile, seqs_per_file, outfile_prefix, working_dir=''):
 if (seqs_per_file <= 0):
  raise ValueError('seqs_per_file must be > 0!')
 seq_counter = 0
 out_files = []
 if (working_dir and (not working_dir.endswith('/'))):
  working_dir += '/'
  create_dir(working_dir)
 for (seq_id, seq) in parse_fasta(infile):
  if (seq_counter == 0):
   current_out_fp = ('%s%s.%d.fasta' % (working_dir, outfile_prefix, len(out_files)))
   current_out_file = open(current_out_fp, 'w')
   out_files.append(current_out_fp)
  current_out_file.write(('>%s\n%s\n' % (seq_id, seq)))
  seq_counter += 1
  if (seq_counter == seqs_per_file):
   current_out_file.close()
   seq_counter = 0
 if (not current_out_file.closed):
  current_out_file.close()
 return out_files

def split_fasta ( infile , seqs_per_file , outfile_prefix , working_dir = '' ) : if ( seqs_per_file <= 0 ) : raise ValueError ( 'seqs_per_file must be > 0!' ) seq_counter = 0 out_files = [ ] if ( working_dir and ( not working_dir . endswith ( '/' ) ) ) : working_dir += '/' create_dir ( working_dir ) for ( seq_id , seq ) in parse_fasta ( infile ) : if ( seq_counter == 0 ) : current_out_fp = ( '%s%s.%d.fasta' % ( working_dir , outfile_prefix , len ( out_files ) ) ) current_out_file = open ( current_out_fp , 'w' ) out_files . append ( current_out_fp ) current_out_file . write ( ( '>%s\n%s\n' % ( seq_id , seq ) ) ) seq_counter += 1 if ( seq_counter == seqs_per_file ) : current_out_file . close ( ) seq_counter = 0 if ( not current_out_file . closed ) : current_out_file . close ( ) return out_files
split infile into files with seqs_per_file sequences in each infile: list of fasta lines or open file object seqs_per_file: the number of sequences to include in each file out_fileprefix: string used to create output filepath - output filepaths are <out_prefix> .