<commit_before>from bcbio.ngsalign import tophat
import os
from bcbio.utils import file_exists
from bipy.utils import remove_suffix, is_pair, get_in
from bipy.toolbox import fastqc
from bipy.pipeline.stages import AbstractStage
from bcbio.distributed.transaction import file_transaction
import sh
from bipy.log import logger

FASTQ_FORMAT_TO_BCBIO = {"sanger": None,
                         "illumina_1.3+": "illumina",
                         "illumina_1.5+": "illumina",
                         "illumina_1.8+": None,
                         "solexa": "solexa"}


def run_with_config(fastq_file, pair_file, ref_file,
                    stage_name, config):
    out_file = _bcbio_tophat_wrapper(fastq_file, pair_file,
                                     ref_file, stage_name,
                                     config)
    return out_file


def _bcbio_tophat_wrapper(fastq_file, pair_file, ref_file,
                          stage_name, config):
    bcbio_config = {}
    stage_config = config["stage"][stage_name]
    cores = config["cluster"].get("cores", None)
    # use the listed quality format, if there isn't one, try to figure
    # out what format it is
    quality_format = stage_config.get("quality_format", None)
    if quality_format is None:
        fastq_format = fastqc.detect_fastq_format(fastq_file)
        quality_format = FASTQ_FORMAT_TO_BCBIO[fastq_format]

    max_errors = stage_config.get("max_errors", None)
    tophat_loc = config["program"].get("tophat", "tophat")
    bowtie_loc = config["program"].get("bowtie", "bowtie")
    out_base = remove_suffix(os.path.basename(fastq_file))
    align_dir = os.path.join(config["dir"]["results"], stage_name)

    bcbio_config["resources"] = {"tophat": {"cores": cores}}
    bcbio_config["algorithm"] = {}
    bcbio_config["program"] = {}
    bcbio_config["algorithm"]["quality_format"] = quality_format
    bcbio_config["algorithm"]["max_errors"] = max_errors
    bcbio_config["gtf"] = config.get("gtf", None)
    bcbio_config["program"]["tophat"] = tophat_loc
    bcbio_config["program"]["bowtie"] = bowtie_loc

    out_file = tophat.align(fastq_file, pair_file, ref_file, out_base,
                            align_dir, bcbio_config)
    os.remove(out_file)

    out_dir = os.path.dirname(out_file)
    out_file_fixed = os.path.join(out_dir, out_base + ".sam")
    os.symlink("accepted_hits.sam", out_file_fixed)

    return out_file_fixed


class Tophat(AbstractStage):

    stage = "tophat"

    def __init__(self, config):
        self.config = config
        self.ref = config.get("ref", None)

    def __call__(self, in_file):
        self._start_message(in_file)
        if is_pair(in_file):
            out_file = run_with_config(in_file[0], in_file[1],
                                       self.ref, self.stage, self.config)
        else:
            out_file = run_with_config(in_file[0], None, self.ref,
                                       self.stage, self.config)
        self._end_message(in_file)
        return out_file


class Bowtie(AbstractStage):

    stage = "bowtie"

    def __init__(self, config):
        self.config = config
        self.stage_config = config["stage"][self.stage]
        # defaults = {"q": True, "n": 2, "k": 1,
        #            "X": 2000, "best": True,
        #            "sam": True,
        #            "phred64-quals": True}
        #self.options = dict(defaults.items() +
        #                    self.stage_config.get("options", {}).items())
        self.options = self.stage_config.get("options", {}).items()
        self.bowtie = sh.Command(self.stage_config.get("program", "bowtie"))
        self.out_prefix = os.path.join(get_in(self.config,<commit_msg>Bowtie option changes.<commit_after>from bcbio.ngsalign import tophat
import os
from bcbio.utils import file_exists
from bipy.utils import remove_suffix, is_pair, get_in
from bipy.toolbox import fastqc
from bipy.pipeline.stages import AbstractStage
from bcbio.distributed.transaction import file_transaction
import sh
from bipy.log import logger

FASTQ_FORMAT_TO_BCBIO = {"sanger": None,
                         "illumina_1.3+": "illumina",
                         "illumina_1.5+": "illumina",
                         "illumina_1.8+": None,
                         "solexa": "solexa"}


def run_with_config(fastq_file, pair_file, ref_file,
                    stage_name, config):
    out_file = _bcbio_tophat_wrapper(fastq_file, pair_file,
                                     ref_file, stage_name,
                                     config)
    return out_file


def _bcbio_tophat_wrapper(fastq_file, pair_file, ref_file,
                          stage_name, config):
    bcbio_config = {}
    stage_config = config["stage"][stage_name]
    cores = config["cluster"].get("cores", None)
    # use the listed quality format, if there isn't one, try to figure
    # out what format it is
    quality_format = stage_config.get("quality_format", None)
    if quality_format is None:
        fastq_format = fastqc.detect_fastq_format(fastq_file)
        quality_format = FASTQ_FORMAT_TO_BCBIO[fastq_format]

    max_errors = stage_config.get("max_errors", None)
    tophat_loc = config["program"].get("tophat", "tophat")
    bowtie_loc = config["program"].get("bowtie", "bowtie")
    out_base = remove_suffix(os.path.basename(fastq_file))
    align_dir = os.path.join(config["dir"]["results"], stage_name)

    bcbio_config["resources"] = {"tophat": {"cores": cores}}
    bcbio_config["algorithm"] = {}
    bcbio_config["program"] = {}
    bcbio_config["algorithm"]["quality_format"] = quality_format
    bcbio_config["algorithm"]["max_errors"] = max_errors
    bcbio_config["gtf"] = config.get("gtf", None)
    bcbio_config["program"]["tophat"] = tophat_loc
    bcbio_config["program"]["bowtie"] = bowtie_loc

    out_file = tophat.align(fastq_file, pair_file, ref_file, out_base,
                            align_dir, bcbio_config)
    os.remove(out_file)

    out_dir = os.path.dirname(out_file)
    out_file_fixed = os.path.join(out_dir, out_base + ".sam")
    os.symlink("accepted_hits.sam", out_file_fixed)

    return out_file_fixed


class Tophat(AbstractStage):

    stage = "tophat"

    def __init__(self, config):
        self.config = config
        self.ref = config.get("ref", None)

    def __call__(self, in_file):
        self._start_message(in_file)
        if is_pair(in_file):
            out_file = run_with_config(in_file[0], in_file[1],
                                       self.ref, self.stage, self.config)
        else:
            out_file = run_with_config(in_file[0], None, self.ref,
                                       self.stage, self.config)
        self._end_message(in_file)
        return out_file


class Bowtie(AbstractStage):

    stage = "bowtie"

    def __init__(self, config):
        self.config = config
        self.stage_config = config["stage"][self.stage]
        # defaults = {"q": True, "n": 2, "k": 1,
        #            "X": 2000, "best": True,
        #            "sam": True,
        #            "phred64-quals": True}
        #self.options = dict(defaults.items() +
        #                    self.stage_config.get("options", {}).items())
        self.options = self.stage_config.get("options", {})
        self.bowtie = sh.Command(self.stage_config.get("program", "bowtie"))
        self.out_prefix = os.path.join(get_in(self.config,
{'fraction_non_alphanumeric': 0.12066405722769605, 'fraction_numerical': 0.004589013362127143, 'mean_word_length': 2.540372670807453, 'pattern_counts': {'":': 28, '<': 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': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1268561', 'n_tokens_mistral': 2477, 'n_tokens_neox': 2357, 'n_words': 440}