<commit_before>"""Next-gen alignments with TopHat a spliced read mapper for RNA-seq experiments.

http://tophat.cbcb.umd.edu
"""
import sh
import os
import shutil
import subprocess
from contextlib import closing
import glob
import pysam
import numpy
from bcbio.pipeline import config_utils
from bcbio.ngsalign import bowtie, bowtie2
from bcbio.utils import safe_makedir, file_exists, get_in, flatten
from bcbio.distributed.transaction import file_transaction
from bcbio.log import logger


galaxy_location_file = "bowtie_indices.loc"

_out_fnames = ["accepted_hits.sam", "junctions.bed",
               "insertions.bed", "deletions.bed"]


def _determine_quality(options, config):

    qual_format = config["algorithm"].get("quality_format", None)
    if qual_format is None or qual_format.lower() == "illumina":
        options["solexa1.3-quals"] = True
    elif qual_format == "solexa":
        options["solexa-quals"] = True
    return options


def tophat2_align(fastq_file, pair_file, ref_file, out_base, align_dir, config,
                 rg_name=None):
    """
    run alignment using Tophat v2
    """
    options = get_in(config, ("resources", "tophat", "options"), {})
    options = _determine_quality(options, config)

    qual_format = config["algorithm"].get("quality_format", None)

    if qual_format is None or qual_format.lower() == "illumina":
        options["solexa1.3-quals"] = True
    elif qual_format == "solexa":
        options["solexa-quals"] = True

    gtf_file = config.get("gtf", None)
    if gtf_file is not None:
        options["GTF"] = gtf_file

    # select the correct bowtie option to use; tophat2 is ignoring this option
    if _tophat_major_version(config) == 2 and _ref_version(ref_file) == 1:
        options["bowtie1"] = True

    cores = config.get("resources", {}).get("tophat", {}).get("cores", None)
    if cores:
        options["num-threads"] = cores
    out_dir = os.path.join(align_dir, "%s_tophat" % out_base)
    out_file = os.path.join(out_dir, _out_fnames[0])
    files = [ref_file, fastq_file]
    if not file_exists(out_file):
        with file_transaction(out_dir) as tx_out_dir:
            safe_makedir(tx_out_dir)
            if pair_file:
                d, d_stdev = _estimate_paired_innerdist(fastq_file, pair_file,
                                                        ref_file, out_base,
                                                        tx_out_dir, config)
                options["mate-inner-dist"] = d
                options["mate-std-dev"] = d_stdev
                files.append(pair_file)
            options["output-dir"] = tx_out_dir
            options["no-convert-bam"] = True
            tophat_runner = sh.Command(config_utils.get_program("tophat",
                                                                config))
            tophat_runner(options, files)
    out_file_final = os.path.join(out_dir, "%s.sam" % out_base)
    if not os.path.exists(out_file_final):
        os.symlink(out_file, out_file_final)
    return out_file_final


def align(fastq_file, pair_file, ref_file, out_base, align_dir, config,
          rg_name=None):

    out_dir = os.path.join(align_dir, "%s_tophat" % out_base)
    out_file = os.path.join(out_dir, _out_fnames[0])

    if file_exists(out_file):
        return out_file

    if not _bowtie_ref_match(ref_file, config):
        logger.error("Bowtie version %d was detected but the reference "
                     "file %s is built for version %d. Download version "
                     "%d or build it with bowtie-build."
                     % (_bowtie_major_version(config), ref_file,
                        _ref_version(ref_file),
                        _bowtie_major_version(config)))
        exit(1)

    tophat_version = _tophat_major_version(config)
    out_files = tophat2_align(fastq_file, pair_file, ref_file, out_base,
                              align_dir, config, rg_name=None)<commit_msg>Refactor to make it easier to follow.<commit_after>"""Next-gen alignments with TopHat a spliced read mapper for RNA-seq experiments.

http://tophat.cbcb.umd.edu
"""
import sh
import os
import shutil
import subprocess
from contextlib import closing
import glob
import pysam
import numpy
from bcbio.pipeline import config_utils
from bcbio.ngsalign import bowtie, bowtie2
from bcbio.utils import safe_makedir, file_exists, get_in, flatten
from bcbio.distributed.transaction import file_transaction
from bcbio.log import logger


galaxy_location_file = "bowtie_indices.loc"

_out_fnames = ["accepted_hits.sam", "junctions.bed",
               "insertions.bed", "deletions.bed"]


def _set_quality_flag(options, config):
    qual_format = config["algorithm"].get("quality_format", None)
    if qual_format is None or qual_format.lower() == "illumina":
        options["solexa1.3-quals"] = True
    elif qual_format == "solexa":
        options["solexa-quals"] = True
    return options


def _set_gtf(options, config):
    gtf_file = config.get("gtf", None)
    if gtf_file is not None:
        options["GTF"] = gtf_file
    return options


def _set_cores(options, config):
    cores = config.get("resources", {}).get("tophat", {}).get("cores", None)
    if cores and "num-threads" not in options:
        options["num-threads"] = cores
    return options


def tophat_align(fastq_file, pair_file, ref_file, out_base, align_dir, config,
                 rg_name=None):
    """
    run alignment using Tophat v2
    """
    options = get_in(config, ("resources", "tophat", "options"), {})
    options = _set_quality_flag(options, config)
    options = _set_gtf(options, config)
    options = _set_cores(options, config)

    # select the correct bowtie option to use; tophat2 is ignoring this option
    if _tophat_major_version(config) == 2 and _ref_version(ref_file) == 1:
        options["bowtie1"] = True

    out_dir = os.path.join(align_dir, "%s_tophat" % out_base)
    out_file = os.path.join(out_dir, _out_fnames[0])
    if file_exists(out_file):
        return out_file
    files = [ref_file, fastq_file]
    if not file_exists(out_file):
        with file_transaction(out_dir) as tx_out_dir:
            safe_makedir(tx_out_dir)
            if pair_file:
                d, d_stdev = _estimate_paired_innerdist(fastq_file, pair_file,
                                                        ref_file, out_base,
                                                        tx_out_dir, config)
                options["mate-inner-dist"] = d
                options["mate-std-dev"] = d_stdev
                files.append(pair_file)
            options["output-dir"] = tx_out_dir
            options["no-convert-bam"] = True
            tophat_runner = sh.Command(config_utils.get_program("tophat",
                                                                config))
            tophat_runner(options, files)
    out_file_final = os.path.join(out_dir, "%s.sam" % out_base)
    os.symlink(os.path.basename(out_file), out_file_final)
    return out_file_final


def align(fastq_file, pair_file, ref_file, out_base, align_dir, config,
          rg_name=None):

    out_dir = os.path.join(align_dir, "%s_tophat" % out_base)
    out_file = os.path.join(out_dir, _out_fnames[0])

    if file_exists(out_file):
        return out_file

    if not _bowtie_ref_match(ref_file, config):
        logger.error("Bowtie version %d was detected but the reference "
                     "file %s is built for version %d. Download version "
                     "%d or build it with bowtie-build."
                     % (_bowtie_major_version(config), ref_file,
                        _ref_version(ref_file),
                        _bowtie_major_version(config)))
        exit(1)

    out_files = tophat_align(fastq_file, pair_file, ref_file, out_base,
                             align_dir, config, rg_name=None)
{'fraction_non_alphanumeric': 0.10107197549770292, 'fraction_numerical': 0.003318019397651863, 'mean_word_length': 2.527002700270027, 'pattern_counts': {'":': 6, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6853029', 'n_tokens_mistral': 2538, 'n_tokens_neox': 2419, 'n_words': 556}