'Split a FASTA file into chunks based on counting records.'

def _count_split(cls, input_file, chunk_size, subdir_generator_function):

    log.debug(('Attemping    to    split    FASTA    file    %s    into    chunks    of    %i    sequences' % (input_file, chunk_size)))
    f = open(input_file, 'rU')
    part_file = None
    try:
        part_dir = subdir_generator_function()
        part_path = os.path.join(part_dir, os.path.basename(input_file))
        part_file = open(part_path, 'w')
        log.debug(('Writing    %s    part    to    %s' % (input_file, part_path)))
        rec_count = 0
        while True:
            line = f.readline()
            if (not line):
                break
            if (line[0] == '>'):
                rec_count += 1
                if (rec_count > chunk_size):
                    part_file.close()
                    part_dir = subdir_generator_function()
                    part_path = os.path.join(part_dir, os.path.basename(input_file))
                    part_file = open(part_path, 'w')
                    log.debug(('Writing    %s    part    to    %s' % (input_file, part_path)))
                    rec_count = 1
            part_file.write(line)
        part_file.close()
    except Exception as e:
        log.error(('Unable    to    count    split    FASTA    file:    %s' % str(e)))
        f.close()
        if (part_file is not None):
            part_file.close()
        raise
    f.close()
