'Split a FASTA file into chunks based on size on disk.
This does of course preserve complete records - it only splits at the
start of a new FASTQ sequence record.'

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

    log.debug(('Attemping    to    split    FASTA    file    %s    into    chunks    of    %i    bytes' % (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)))
        start_offset = 0
        while True:
            offset = f.tell()
            line = f.readline()
            if (not line):
                break
            if ((line[0] == '>') and ((offset - start_offset) >= 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)))
                start_offset = f.tell()
            part_file.write(line)
    except Exception as e:
        log.error(('Unable    to    size    split    FASTA    file:    %s' % str(e)))
        f.close()
        if (part_file is not None):
            part_file.close()
        raise
    f.close()
