def filter_bam_file(bamfile, chromosome, outfile):
    """

    filter_bam_file uses samtools to read a <bamfile> and read only
    the reads that are mapped to <chromosome>.
    It saves the filtered reads into <outfile>.

    """
    inputs = [bamfile]
    outputs = [outfile]
    options = {
        'cores': 1,
        'memory': '4g',
        'account': 'NChain',
        'walltime': '01:00:00'
    }

    directory = "/".join(outfile.split("/")[:-1])

    spec = '''
    source /com/extra/samtools/1.6.0/load.sh

    mkdir -p {dirc}

    samtools view -b {infile} {chrom} > {out}
    '''.format(infile=bamfile, chrom=chromosome, out=outfile, dirc=directory)

    return inputs, outputs, options, spec