<commit_before>"""
.. See the NOTICE file distributed with this work for additional information
   regarding copyright ownership.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
"""
from __future__ import print_function

import os
import shlex
import subprocess
import sys

from utils import logger

try:
    if hasattr(sys, '_run_from_cmdl') is True:
        raise ImportError
    from pycompss.api.parameter import FILE_IN, FILE_OUT, IN
    from pycompss.api.task import task
    from pycompss.api.api import compss_wait_on
except ImportError:
    logger.warn("[Warning] Cannot import \"pycompss\" API packages.")
    logger.warn("          Using mock decorators.")

    from utils.dummy_pycompss import FILE_IN, FILE_OUT, IN  # pylint: disable=ungrouped-imports
    from utils.dummy_pycompss import task
    from utils.dummy_pycompss import compss_wait_on

from basic_modules.tool import Tool
from basic_modules.metadata import Metadata

from tool.common import cd

# ------------------------------------------------------------------------------

class bsgenomeTool(Tool):
    """
    Tool for peak calling for iDamID-seq data
    """

    def __init__(self, configuration=None):
        """
        Init function
        """
        print("Forge BSgenome")
        Tool.__init__(self)

        if configuration is None:
            configuration = {}

        self.configuration.update(configuration)

    @task(
        returns=int,
        genome=FILE_IN, circ_chrom=IN, seed_file_param=IN,
        genome_2bit=FILE_OUT, chrom_size=FILE_OUT, seed_file=FILE_OUT, bsgenome=FILE_OUT,
        isModifier=False)
    def bsgenome_creater(
            self, genome, circ_chrom, seed_file_param,
            genome_2bit, chrom_size, seed_file, bsgenome):
        """
        Make iDamID-seq peak calls. These are saved as bed files That can then
        get displayed on genome browsers. Uses an R script that wraps teh iDEAR
        protocol.

        Parameters
        ----------
        sample_name : str
        bg_name : str
        sample_bam_file_1 : str
            Location of the aligned sequences in bam format
        sample_bam_file_2 : str
            Location of the aligned sequences in bam format
        bg_bam_file_1 : str
            Location of the aligned background sequences in bam format
        bg_bam_file_2 : str
            Location of the aligned background sequences in bam format
        species : str
            Species name for the alignments
        assembly : str
            Assembly used for teh aligned sequences
        peak_bed : str
            Location of the peak bed file

        Returns
        -------
        peak_bed : str
            Location of the collated bed file
        """

        command_line_2bit = "faToTwoBit " + genome + " " + genome_2bit
        command_line_chrom_size = "twoBitInfo " + genome_2bit + " stdout | sort -k2rn"

        args = shlex.split(command_line_2bit)
        process = subprocess.Popen(args)
        process.wait()

        args = shlex.split(command_line_chrom_size)

        with open(chrom_size, "w") as f_out:
            sub_proc_1 = subprocess.Popen(command_line_chrom_size, shell=True, stdout=f_out)
            sub_proc_1.wait()

        chrom_seq_list = []
        chrom_circ_list = []
        with open(chrom_size, "r") as f_in:
            for line in f_in:
                line = line.split("\t")
                if any(cc in line[0] for cc in circ_chrom):
                    chrom_circ_list.append(line[0])
                else:
                    chrom_seq_list.append(line[0])

        genome_split = genome_2bit.split("/")
        seed_file_param["seqnames"] = 'c("' + '","'.join(chrom_seq_list) + '")'
        if len(chrom_circ_list) > 0:
            seed_file_param["circ_seqs"] = 'c("' + '","'.join(chrom_circ_list) + '")'
        seed_file_param["circ_seqs"] = ""
        seed_file_param["seqs_srcdir"] = "/".join(genome_split[0:-1])
        seed_file_param["seqfile_name"] = genome_split[-1]

        # Create the seed file
        seed_order = [
            "Package", "Title", "Description", "Version", "organism", "common_name",
            "provider", "provider_version", "release_date", "release_name", "organism_biocview",
            "BSgenomeObjname", "seqnames", "circ_seqs", "seqs_srcdir", "seqfile_name"
        ]
        with open(seed_file, "wb") as f_out:
            for seed_key in seed_order:
                f_out.write(seed_key + ": " + seed_file_param[seed_key] + "\n")

        # Forge the BSgenomedirectory
        rscript = os.path.join(os.path.dirname(__file__), "../scripts/forge_bsgenome.R")
        command_line = "Rscript " + rscript + " --file " + seed_file
        logger.info("BSGENOME CMD: " + command_line)

        with cd(seed_file_param["seqs_srcdir"]):
            args = shlex.split(command_line)
            process = subprocess.Popen(args)
            process.wait()

            package_build = seed_file_param["seqs_srcdir"] + "/" + seed_file_param["Package"]
            command_line_build = "R CMD build " + package_build
            command_line_check = "R CMD check " + package_build
<commit_msg>Minor change to the info text wrt calling the Rscript<commit_after>"""
.. See the NOTICE file distributed with this work for additional information
   regarding copyright ownership.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
"""
from __future__ import print_function

import os
import shlex
import subprocess
import sys

from utils import logger

try:
    if hasattr(sys, '_run_from_cmdl') is True:
        raise ImportError
    from pycompss.api.parameter import FILE_IN, FILE_OUT, IN
    from pycompss.api.task import task
    from pycompss.api.api import compss_wait_on
except ImportError:
    logger.warn("[Warning] Cannot import \"pycompss\" API packages.")
    logger.warn("          Using mock decorators.")

    from utils.dummy_pycompss import FILE_IN, FILE_OUT, IN  # pylint: disable=ungrouped-imports
    from utils.dummy_pycompss import task
    from utils.dummy_pycompss import compss_wait_on

from basic_modules.tool import Tool
from basic_modules.metadata import Metadata

from tool.common import cd

# ------------------------------------------------------------------------------

class bsgenomeTool(Tool):
    """
    Tool for peak calling for iDamID-seq data
    """

    def __init__(self, configuration=None):
        """
        Init function
        """
        print("Forge BSgenome")
        Tool.__init__(self)

        if configuration is None:
            configuration = {}

        self.configuration.update(configuration)

    @task(
        returns=int,
        genome=FILE_IN, circ_chrom=IN, seed_file_param=IN,
        genome_2bit=FILE_OUT, chrom_size=FILE_OUT, seed_file=FILE_OUT, bsgenome=FILE_OUT,
        isModifier=False)
    def bsgenome_creater(
            self, genome, circ_chrom, seed_file_param,
            genome_2bit, chrom_size, seed_file, bsgenome):
        """
        Make iDamID-seq peak calls. These are saved as bed files That can then
        get displayed on genome browsers. Uses an R script that wraps teh iDEAR
        protocol.

        Parameters
        ----------
        sample_name : str
        bg_name : str
        sample_bam_file_1 : str
            Location of the aligned sequences in bam format
        sample_bam_file_2 : str
            Location of the aligned sequences in bam format
        bg_bam_file_1 : str
            Location of the aligned background sequences in bam format
        bg_bam_file_2 : str
            Location of the aligned background sequences in bam format
        species : str
            Species name for the alignments
        assembly : str
            Assembly used for teh aligned sequences
        peak_bed : str
            Location of the peak bed file

        Returns
        -------
        peak_bed : str
            Location of the collated bed file
        """

        command_line_2bit = "faToTwoBit " + genome + " " + genome_2bit
        command_line_chrom_size = "twoBitInfo " + genome_2bit + " stdout | sort -k2rn"

        args = shlex.split(command_line_2bit)
        process = subprocess.Popen(args)
        process.wait()

        args = shlex.split(command_line_chrom_size)

        with open(chrom_size, "w") as f_out:
            sub_proc_1 = subprocess.Popen(command_line_chrom_size, shell=True, stdout=f_out)
            sub_proc_1.wait()

        chrom_seq_list = []
        chrom_circ_list = []
        with open(chrom_size, "r") as f_in:
            for line in f_in:
                line = line.split("\t")
                if any(cc in line[0] for cc in circ_chrom):
                    chrom_circ_list.append(line[0])
                else:
                    chrom_seq_list.append(line[0])

        genome_split = genome_2bit.split("/")
        seed_file_param["seqnames"] = 'c("' + '","'.join(chrom_seq_list) + '")'
        if len(chrom_circ_list) > 0:
            seed_file_param["circ_seqs"] = 'c("' + '","'.join(chrom_circ_list) + '")'
        seed_file_param["circ_seqs"] = ""
        seed_file_param["seqs_srcdir"] = "/".join(genome_split[0:-1])
        seed_file_param["seqfile_name"] = genome_split[-1]

        # Create the seed file
        seed_order = [
            "Package", "Title", "Description", "Version", "organism", "common_name",
            "provider", "provider_version", "release_date", "release_name", "organism_biocview",
            "BSgenomeObjname", "seqnames", "circ_seqs", "seqs_srcdir", "seqfile_name"
        ]
        with open(seed_file, "wb") as f_out:
            for seed_key in seed_order:
                f_out.write(seed_key + ": " + seed_file_param[seed_key] + "\n")

        # Forge the BSgenomedirectory
        rscript = os.path.join(os.path.dirname(__file__), "../scripts/forge_bsgenome.R")
        command_line = "Rscript " + rscript + " --file " + seed_file
        logger.info("BSGENOME CMD: Rscript scripts/forge_bsgenome.R --file " + seed_file)

        with cd(seed_file_param["seqs_srcdir"]):
            args = shlex.split(command_line)
            process = subprocess.Popen(args)
            process.wait()

            package_build = seed_file_param["seqs_srcdir"] + "/" + seed_file_param["Package"]
            command_line_build = "R CMD build " + package_build
            command_line_check = "R CMD check " + package_build

{'fraction_non_alphanumeric': 0.10273070506629203, 'fraction_numerical': 0.004390201071209061, 'mean_word_length': 2.3255474452554745, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3478196', 'n_tokens_mistral': 3387, 'n_tokens_neox': 3265, 'n_words': 922}