"""
.. module:: star
   :platform: Unix, OSX
   :synopsis: A module of methods for working with the STAR RNA alignment program
   into additional formats.

.. moduleauthor:: Daniel Gaston <daniel.gaston@dal.ca>


"""

from ddb_ngsflow import pipeline


def add_additional_options(command_list, config, flags):
    if 'compressed' in flags:
        command_list.append("--readFilesCommand {}".format(config['compression']))

    if 'encode_options' in flags:
        encode_options = ["--outFilterType BySJout",
                          "--outFilterMultimapNmax 20",
                          "--alignSJDBoverhangMin 1",
                          "--outFilterMismatchNMax 999",
                          "--alignIntronMin 20",
                          "--alignIntronMax 1000000",
                          "--alignMatesGapMax 1000000"]
        command_list.extend(encode_options)

    if 'unstranded' in flags:
        command_list.append("--outSAMstrandField intronMotif")

    if 'removeNonCanonical' in flags:
        command_list.append("--outFilterIntronMotifs RemoveNoncanonical")

    if 'cufflinks' in flags:
        command_list.append("--alignEndsType EndToEnd")

    return command_list


def star_paired(job, config, name, samples, flags):
    """Align RNA-Seq data to a reference using STAR
    :param config: The configuration dictionary.
    :type config: dict.
    :param name: sample name.
    :type name: str.
    :param samples: The samples info and config dictionary.
    :type samples: dict.
    :returns:  str -- The output vcf file name.
    """

    output = "{}.star.".format(name)
    logfile = "{}.star.log".format(name)
    output_file = "{}Aligned.sortedByCoord.out.bam".format(output)

    command = ["{}".format(config['star']['bin']),
               "--genomeDir {}".format(config['star']['index']),
               "--runThreadN {}".format(config['star']['num_cores']),
               "--readFilesIn {} {}".format(samples[name]['fastq1'], samples[name]['fastq2']),
               "--outFileNamePrefix {}".format(output),
               "--outReadsUnmapped Fastx",
               "--outSAMtype BAM SortedByCoordinate"
               ]

    command = add_additional_options(command, config, flags)

    # if "cufflinks" in flags:
    #     compat_sam = "{}.STAR.noS.sam".format(name)
    #     compat_awk = ["awk",
    #                   'BEGIN {OFS="\t"} {split($6,C,/[0-9]*/); split($6,L,/[SMDIN]/); if (C[2]=="S")',
    #                   '{$10=substr($10,L[1]+1); $11=substr($11,L[1]+1)}; if (C[length(C)]=="S")',
    #                   '{L1=length($10)-L[length(L)-1]; $10=substr($10,1,L1); $11=substr($11,1,L1); };',
    #                   'gsub(/[0-9]*S/,"",$6); print}',
    #                   "{}".format(output_sam),
    #                   ">",
    #                   "{}".format(compat_sam)]

    job.fileStore.logToMaster("STAR Command: {}\n".format(command))
    pipeline.run_and_log_command(" ".join(command), logfile)

    return output_file


def star_unpaired(job, config, name, samples, flags):
    """Align RNA-Seq data to a reference using STAR
    :param config: The configuration dictionary.
    :type config: dict.
    :param name: sample name.
    :type name: str.
    :param samples: The samples info and config dictionary.
    :type samples: dict.
    :returns:  str -- The output vcf file name.
    """

    output = "{}.star.output".format(name)
    logfile = "{}.star.log".format(name)

    command = ["{}".format(config['star']['bin']),
               "--genomeDir {}".format(config['star']['index']),
               "--runThreadN {}".format(config['star']['num_cores']),
               "--readFilesIn {}".format(samples[name]['fastq1']),
               "--outFileNamePrefix {}".format(output),
               "--outReadsUnmapped Fastx",
               "--outSAMtype BAM SortedByCoordinate"
               ]

    command = add_additional_options(command, config, flags)

    # if "cufflinks" in flags:
    #     compat_sam = "{}.STAR.noS.sam".format(name)
    #     compat_awk = ["awk",
    #                   'BEGIN {OFS="\t"} {split($6,C,/[0-9]*/); split($6,L,/[SMDIN]/); if (C[2]=="S")',
    #                   '{$10=substr($10,L[1]+1); $11=substr($11,L[1]+1)}; if (C[length(C)]=="S")',
    #                   '{L1=length($10)-L[length(L)-1]; $10=substr($10,1,L1); $11=substr($11,1,L1); };',
    #                   'gsub(/[0-9]*S/,"",$6); print}',
    #                   "{}".format(output_sam),
    #                   ">",
    #                   "{}".format(compat_sam)]

    job.fileStore.logToMaster("STAR Command: {}\n".format(command))
    pipeline.run_and_log_command(" ".join(command), logfile)

    aligned_sam = "{}Aligned.out.sam".format(output)
    samples[name]['bam'] = aligned_sam

    return output

@@ -2227,620 +2227,8 @@
 s)%0A%0A
-    # if %22cufflinks%22 in flags:%0A    #     compat_sam = %22%7B%7D.STAR.noS.sam%22.format(name)%0A    #     compat_awk = %5B%22awk%22,%0A    #                   'BEGIN %7BOFS=%22%5Ct%22%7D %7Bsplit($6,C,/%5B0-9%5D*/); split($6,L,/%5BSMDIN%5D/); if (C%5B2%5D==%22S%22)',%0A    #                   '%7B$10=substr($10,L%5B1%5D+1); $11=substr($11,L%5B1%5D+1)%7D; if (C%5Blength(C)%5D==%22S%22)',%0A    #                   '%7BL1=length($10)-L%5Blength(L)-1%5D; $10=substr($10,1,L1); $11=substr($11,1,L1); %7D;',%0A    #                   'gsub(/%5B0-9%5D*S/,%22%22,$6); print%7D',%0A    #                   %22%7B%7D%22.format(output_sam),%0A    #                   %22%3E%22,%0A    #                   %22%7B%7D%22.format(compat_sam)%5D%0A%0A
     
@@ -2772,22 +2772,16 @@
 %7B%7D.star.
-output
 %22.format
@@ -2827,16 +2827,83 @@
 at(name)
+%0A    output_file = %22%7B%7DAligned.sortedByCoord.out.bam%22.format(output)
 %0A%0A    co
@@ -3385,620 +3385,8 @@
 s)%0A%0A
-    # if %22cufflinks%22 in flags:%0A    #     compat_sam = %22%7B%7D.STAR.noS.sam%22.format(name)%0A    #     compat_awk = %5B%22awk%22,%0A    #                   'BEGIN %7BOFS=%22%5Ct%22%7D %7Bsplit($6,C,/%5B0-9%5D*/); split($6,L,/%5BSMDIN%5D/); if (C%5B2%5D==%22S%22)',%0A    #                   '%7B$10=substr($10,L%5B1%5D+1); $11=substr($11,L%5B1%5D+1)%7D; if (C%5Blength(C)%5D==%22S%22)',%0A    #                   '%7BL1=length($10)-L%5Blength(L)-1%5D; $10=substr($10,1,L1); $11=substr($11,1,L1); %7D;',%0A    #                   'gsub(/%5B0-9%5D*S/,%22%22,$6); print%7D',%0A    #                   %22%7B%7D%22.format(output_sam),%0A    #                   %22%3E%22,%0A    #                   %22%7B%7D%22.format(compat_sam)%5D%0A%0A
     
@@ -3515,101 +3515,8 @@
 e)%0A%0A
-    aligned_sam = %22%7B%7DAligned.out.sam%22.format(output)%0A    samples%5Bname%5D%5B'bam'%5D = aligned_sam%0A%0A
     
@@ -3528,9 +3528,14 @@
 n output
+_file
 %0A

Fix star method calling