rule extract_fasta_from_bed:
    """
    Extracts features (ex: rRNA) from gff and than extracts sequences from a FASTA file for each of the intervals defined using bedtools getfasta.

    Required input:
        __extract_fasta_from_bed__input: input fasta file
        __extract_fasta_from_bed__gff: coordinates to extract in a BED interval file

    params:
        __extract_fasta_from_bed__feature: type of feature you want to extract from 3rd column of gff file

    Required output:
        __extract_fasta_from_bed__output: output fasta file containing extract sequence
        __extract_fasta_from_bed__output_features: output gff file containing extract sequence


    """
    input:
        fasta = __extract_fasta_from_bed__input,
        gff = __extract_fasta_from_bed__gff
    params:
        feature = __extract_fasta_from_bed__feature
    output:
        fasta = __extract_fasta_from_bed__output,
        gff = __extract_fasta_from_bed__output_features
    log:
        __extract_fasta_from_bed__log
    shell:
        """
        gawk '{{ if ($3=="{params.feature}") print }}' {input.gff} > {output.gff}
        if [ -s {output.gff} ]
        then
            bedtools getfasta -fi {input.fasta} -bed {output.gff}  -fo {output.fasta}
        else :
            echo -e ">empty\\nAAAAAAAAAAAAAA" > {output.fasta}
        fi
        samtools faidx {output.fasta}
        """
