rule feature_counts:
    """
    Feature counts (subread) is a highly efficient general-purpose read summarization program that counts mapped reads for genomic features such as genes, exons, promoter, gene bodies, genomic bins and chromosomal locations.
    :reference: http://bioinf.wehi.edu.au/featureCounts/

    Required input:
         __feature_counts__input: sorted bam file

    Required output:
        __feature_counts__output_count: output tabulated-delimited file
        __feature_counts__output_gene_count: output formatted tab-delimited file

    Config:

        .. code-block:: yaml

            feature_counts:
                gff: " "       #path to the GFF/GTF annotation file
                options:  " "  #options for featureCounts you want use
    """


    input:
        bam = __feature_counts__input
    output:
        count = __feature_counts__output_count,
        gene_count = __feature_counts__output_gene_count
    params:
        gff = __feature_counts__gff,
        mapp = config['feature_counts']["options"]  # -t exon
    log:
        __feature_counts__log
    threads:
        config['feature_counts']['threads']
    run:
        shell("""featureCounts -T {threads} {params.mapp} \
                 -a {params.gff} -o {output.count} {input.bam} 2> {log}""")

        shell("""cut -f 1,7- {output.count} | sed '2d' > {output.gene_count}""")
      
