rule indel_realigner:
    """ Performs local realignment around indels to correct errors made by 
    genome aligners. It identifies regions where alignments may 
    potentially be improved, then realigns the reads in these regions.

    Required input:
        __indel_realigner__input: sorted bam file.
        __indel_realigner__reference: fasta file.
        __indel_realigner__ref_dict: reference dictionnary.

    Required output:
        __indel_realigner__output: bam file.
        __indel_realigner__intervals: intervals file. Must have ".intervals" 
            extension.

    Required parameters:
        config['indel_realigner']['target_creator_options']: any options
            recognised by RealignerTargetCreator.
        config['indel_realigner']['options']: any options recognised by
            IndelRealigner.
    """
    input:
        bam = __indel_realigner__input,
        ref = __indel_realigner__reference,
        ref_dict = __indel_realigner__ref_dict
    output:
        bam = __indel_realigner__output,
        intervals = __indel_realigner__intervals
    log:
        __indel_realigner__log
    params:
        rtc = config['indel_realigner']['target_creator_options'],
        it = config['indel_realigner']['options']
    shell:
        """
        # BAM must be indexed to realign INDEL
        samtools index {input.bam}

        GenomeAnalysisTK -T RealignerTargetCreator {params.rtc} -R \
        {input.ref} -I {input.bam} -o {output.intervals} > {log} 2>&1

        GenomeAnalysisTK -T IndelRealigner {params.it} -R {input.ref} -I \
        {input.bam} -targetIntervals {output.intervals} -o {output.bam} \
        >> {log} 2>&1
        """
