"""
.. 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 star_paired_uncompressed_basic(job, config, name, samples):
    """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_dir = "{}.star.output".format(name)
    logfile = "{}.star.log".format(name)

    command = ("{}".format(config['star']['bin']),
               "--genomeDir {}".format(config['star']['reference']),
               "--runThreadN {}".format(config['star']['num_cores']),
               "--readFilesIn {} {}".format(samples[name]['fastq1'], samples[name]['fastq2']),
               "--outFileNamePrefix {}".format(output_dir)
               )

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

    return output_dir


def star_unpaired_uncompressed_basic(job, config, name, samples):
    """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_dir = "{}.star.output".format(name)
    logfile = "{}.star.log".format(name)

    command = ("{}".format(config['star']['bin']),
               "--genomeDir {}".format(config['star']['reference']),
               "--runThreadN {}".format(config['star']['num_cores']),
               "--readFilesIn {}".format(samples[name]['fastq1']),
               "--outFileNamePrefix {}".format(output_dir)
               )

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

    return output_dir


def star_paired_compressed_basic(job, config, name, samples):
    """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_dir = "{}.star.output".format(name)
    logfile = "{}.star.log".format(name)

    command = ("{}".format(config['star']['bin']),
               "--genomeDir {}".format(config['star']['reference']),
               "--runThreadN {}".format(config['star']['num_cores']),
               "--readFilesIn {} {}".format(samples[name]['fastq1'], samples[name]['fastq2']),
               "--readFilesCommand {}".format(config['compression']),
               "--outFileNamePrefix {}".format(output_dir)
               )

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

    return output_dir


def star_paired_uncompressed_encode(job, config, name, samples):
    """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_dir = "{}.star.output".format(name)
    logfile = "{}.star.log".format(name)

    command = ("{}".format(config['star']['bin']),
               "--genomeDir {}".format(config['star']['reference']),
               "--runThreadN {}".format(config['star']['num_cores']),
               "--readFilesIn {} {}".format(samples[name]['fastq1'], samples[name]['fastq2']),
               "--outFilterType BySJout",
               "--outFilterMultimapNmax 20",
               "--alignSJDBoverhangMin 1",
               "--outFilterMismatchNMax 999",
               "--alignIntronMin 20",
               "--alignIntronMax 1000000",
               "--alignMatesGapMax 1000000",
               "--outFileNamePrefix {}".format(output_dir)
               )

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

    return output_dir


def star_paired_compressed_encode(job, config, name, samples):
    """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_dir = "{}.star.output".format(name)
    logfile = "{}.star.log".format(name)

    command = ("{}".format(config['star']['bin']),
               "--genomeDir {}".format(config['star']['reference']),
               "--runThreadN {}".format(config['star']['num_cores']),
               "--readFilesIn {} {}".format(samples[name]['fastq1'], samples[name]['fastq2']),
               "--outFilterType BySJout",
               "--outFilterMultimapNmax 20",
               "--alignSJDBoverhangMin 1",
               "--outFilterMismatchNMax 999",
               "--alignIntronMin 20",
               "--alignIntronMax 1000000",
               "--alignMatesGapMax 1000000",
               "--outFileNamePrefix {}".format(output_dir),
               "--readFilesCommand {}".format(config['compression'])
               )

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

    return output_dir

@@ -819,33 +819,29 @@
 ig%5B'star'%5D%5B'
-reference
+index
 '%5D),%0A       
@@ -1804,33 +1804,29 @@
 ig%5B'star'%5D%5B'
-reference
+index
 '%5D),%0A       
@@ -2757,33 +2757,29 @@
 ig%5B'star'%5D%5B'
-reference
+index
 '%5D),%0A       
@@ -3811,33 +3811,29 @@
 ig%5B'star'%5D%5B'
-reference
+index
 '%5D),%0A       
@@ -5099,25 +5099,21 @@
 star'%5D%5B'
-reference
+index
 '%5D),%0A   

Fix STAR methods