def kmc(forward_in, database_name, min_occurrences=1, reverse_in='NA', k=31, cleanup=True,
        returncmd=False, tmpdir='tmp', **kwargs):
    """
    Runs kmc to count kmers.
    :param forward_in: Forward input reads. Assumed to be fastq.
    :param database_name: Name for output kmc database.
    :param min_occurrences: Minimum number of times kmer must be seen to be included in database.
    :param reverse_in: Reverse input reads. Automatically found.
    :param k: Kmer size. Default 31.
    :param cleanup: If true, deletes tmpdir that kmc needs.
    :param tmpdir: Temporary directory to store intermediary kmc files. Default tmp.
    :param returncmd: If true, will return the command used to call KMC as well as out and err.
    :param kwargs: Other kmc arguments in parameter='argument' format.
    :return: Stdout and stderr from kmc.
    """
    # Create the tmpdir kmc needs if it isn't already present.
    if not os.path.isdir(tmpdir):
        os.makedirs(tmpdir)
    options = kwargs_to_string(kwargs)
    if os.path.isfile(forward_in.replace('_R1', '_R2')) and reverse_in == 'NA' and '_R1' in forward_in:
        reverse_in = forward_in.replace('_R1', '_R2')
        filelist = os.path.join(tmpdir, 'filelist.txt')
        with open(filelist, 'w') as f:
            f.write(forward_in + '\n')
            f.write(reverse_in + '\n')
        cmd = 'kmc -k{} -ci{} {} @{} {} {}'.format(k, min_occurrences, options, filelist, database_name, tmpdir)
    elif reverse_in == 'NA':
        cmd = 'kmc -k{} -ci{} {} {} {} {}'.format(k, min_occurrences, options, forward_in, database_name, tmpdir)
    else:
        filelist = os.path.join(tmpdir, 'filelist.txt')
        with open(filelist, 'w') as f:
            f.write(forward_in + '\n')
            f.write(reverse_in + '\n')
        cmd = 'kmc -k{} -ci{} {} @{} {} {}'.format(k, min_occurrences, options, filelist, database_name, tmpdir)
    out, err = accessoryfunctions.run_subprocess(cmd)
    if cleanup:
        shutil.rmtree(tmpdir)
    if returncmd:
        return out, err, cmd
    else:
        return out, err