rule star_mapping:
    """
    Read mapping for either single end and paired end data using RNA-STAR.

    Required input:
        __star_mapping__input: list with one or two fastq.gz

    Required output:
        __star_mapping__sort: output sorted bam file

    params:

        __star_mapping__output_prefix1: output prefix for first-pass mapping (temporary file)
        __star_mapping__output_prefix2: output prefix for second-pass mapping
        __star_mapping__genome_dir: name of directory for new genome indexation (temporary file)
        __star_mapping__splice_file: name of the file containing splicing information get during firt-pass alignment

    config:

        .. code-block:: yaml

            star_mapping:
                prefix_index: "" #path to the index file of reference genome
                ref: "" #path to the reference genome file in fasta format
                options:  "" #options for bowtie1 you want use
    """
    input:
        fastq = __star_mapping__input,
        index = __star_mapping__index_done
    log:
        __star_mapping__logs
    output:
        __star_mapping__sort
    params:
        prefix1 = temp(__star_mapping__output_prefix1),
        prefix2 = __star_mapping__output_prefix2,
        index = __star_mapping__index,
        ref = __star_mapping__ref,
        #read_groups = __star_mapping__read_groups,
        genome_dir = temp(__star_mapping__genome_dir),
        splice_file = temp(__star_mapping__splice_file),
        kwargs = config['star_mapping']['options']
    threads:
        config['star_mapping']['threads']
    #Resources:
    #    ram = config['star_mapping']['ram']
    run:
        shell(""" echo "Run rna-star 1st pass"
        STAR --genomeDir {params.index} \
             --readFilesIn {input.fastq}  \
             --runThreadN {threads} \
             --genomeLoad NoSharedMemory \
             --outSAMtype BAM SortedByCoordinate \
             --readFilesCommand zcat \
             --seedSearchStartLmax 20 \
             --outFileNamePrefix {params.prefix1} \
            {params.kwargs}  2> {log}""")

        shell(""" echo "run rna-star genome indexing"
        if [ ! -d "{params.genome_dir}" ]; then
            mkdir {params.genome_dir}
        fi
        STAR --runMode genomeGenerate \
             --genomeDir {params.genome_dir} \
             --genomeFastaFiles {params.ref} \
             --sjdbFileChrStartEnd {params.splice_file} \
             --sjdbOverhang 100 \
             --runThreadN {threads}  2>> {log}""")


        shell(""" echo "Run rna-star 2nd pass"
        STAR --genomeDir {params.genome_dir} \
             --readFilesIn {input.fastq}  \
             --runThreadN {threads} \
             --genomeLoad NoSharedMemory \
             --sjdbFileChrStartEnd {params.splice_file} \
             --outSAMtype BAM SortedByCoordinate \
             --readFilesCommand zcat \
             --outFileNamePrefix {params.prefix2} \
             {params.kwargs}  2>> {log}""")


        shell(""" samtools index {params.prefix2}Aligned.sortedByCoord.out.bam  2>> {log} """)