<commit_before>#!/bin/env python
import argparse
import logging
import subprocess
import sys
import os
import re

# Set logging
logging.basicConfig(filename="smrtsv.log", level=logging.DEBUG)

# Set cluster parameters
CLUSTER_SETTINGS = ' -V -cwd -e ./log -o ./log {cluster.params} -w n -S /bin/bash'
CLUSTER_FLAG = ("--drmaa", CLUSTER_SETTINGS, "-w", "60")

# Setup environment for executing commands
PROCESS_ENV = os.environ.copy()

# Prepend to PROCESS_ENV["PATH"]
INSTALL_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

INSTALL_PATH = [  # List of paths relative to INSTALL_DIR to be added to the environment $PATH
    "bin",
    "dist/miniconda/envs/python2/bin",
    "dist/miniconda/envs/python3/bin",
    "dist/miniconda/bin",
    "dist/celera/wgs-8.3rc2/Linux-amd64/bin/",
    "dist/amos-3.1.0/bin",
    "canu/Linux-amd64/bin"
]

PROCESS_ENV_PATH = ":".join([os.path.join(INSTALL_DIR, THIS_PATH) for THIS_PATH in INSTALL_PATH])

if "PATH" in PROCESS_ENV:
    PROCESS_ENV["PATH"] = PROCESS_ENV_PATH + ":" + PROCESS_ENV["PATH"]
else:
    PROCESS_ENV["PATH"] = PROCESS_ENV_PATH

# Prepend to PROCESS_ENV["LD_LIBRARY_PATH"]
INSTALL_LD_PATH = [
    "dist/hdf5/lib"
]

PROCESS_ENV_LD_PATH = ":".join([os.path.join(INSTALL_DIR, THIS_PATH) for THIS_PATH in INSTALL_LD_PATH])

if "LD_LIBRARY_PATH" in PROCESS_ENV:
    PROCESS_ENV["LD_LIBRARY_PATH"] = PROCESS_ENV_LD_PATH + ":" + PROCESS_ENV["LD_LIBRARY_PATH"]
else:
    PROCESS_ENV["LD_LIBRARY_PATH"] = PROCESS_ENV_LD_PATH

os.environ["LD_LIBRARY_PATH"] = PROCESS_ENV["LD_LIBRARY_PATH"]


# Function definitions
def _get_dist_dir():
    dirname, filename = os.path.split(os.path.abspath(__file__))
    return dirname

# def _build_prefix(args):
#     prefix = ["snakemake", "-T", "--rerun-incomplete", "--snakefile", os.path.join(os.path.dirname(_get_dist_dir()), "Snakefile"), "-j", str(args.jobs)]
#     if args.dryrun:
#         prefix.append("-n")
#
#     if args.distribute:
#         prefix.extend(CLUSTER_FLAG)
#
#     return tuple(prefix)

def _run_cmd(args):
    """
    Run a command with the proper environment set.

    :param args: A tuple of arguments starting with the command name.

    :return: Return code or -1 if the process did not complete.
    """
    sys.stdout.flush()

    p = subprocess.Popen(args, env=PROCESS_ENV)

    p.wait()

    ret_code = p.returncode

    return ret_code if ret_code is not None else -1

def _run_snake_target(args, *cmd):
    """
    Run a snakemake target.

    :param args: Arguments processed from the command line.
    :param cmd: The command to run as a tuple starting with the name of the snakemake target.

    :return: Return code from snakemake.
    """
    # Use the user-defined cluster config path if one is given. Otherwise, use
    # an empty config that comes with the SMRT-SV distribution.
    if args.cluster_config is not None:
        cluster_config_path = args.cluster_config
    else:
        cluster_config_path = os.path.join(os.path.dirname(_get_dist_dir()), "cluster.template.json")

    # Setup snakemake command
    prefix = [
        "snakemake",
        "-T",
        "--rerun-incomplete",
        "--cluster-config", cluster_config_path,
        "--snakefile", os.path.join(os.path.dirname(_get_dist_dir()), "Snakefile"),
        "-j", str(args.jobs)
    ]

    if args.dryrun:
        prefix.append("-n")

    if args.distribute:
        prefix.extend(CLUSTER_FLAG)

    # Append command
    prefix.extend(cmd)

    # Append path and ld_path
    prefix.extend([
        "ld_path=%s" % PROCESS_ENV["LD_LIBRARY_PATH"],
        "path=%s" % PROCESS_ENV["PATH"]
    ])

    # Report (verbose)
    if args.verbose:
        print("Running snakemake command: %s" % " ".join(prefix))

    # Run snakemake command
    return _run_cmd(prefix)

def index(args):
    return _run_snake_target(
        args,
        "prepare_reference",
        "--config",
        "reference=%s" % args.reference
    )

def align(args):
    return _run_snake_target(
        args,
        "align_reads",
        "--config",
        "reference=%s" % args.reference,
        "reads=%s" % args.reads,
        "alignments=%s" % args.alignments,
        "alignments_dir=%s" % args.alignments_dir,
        "batches=%s" % args.batches,
        "threads=%s" % args.threads,
        "tmp_dir=%s" % args.tmpdir,
        "alignment_parameters=%s" % args.alignment_parameters
    )

def detect(args):<commit_msg>Quote alignment parameters during align subcommand. This should address issue #52.<commit_after>#!/bin/env python
import argparse
import logging
import subprocess
import sys
import os
import re

# Set logging
logging.basicConfig(filename="smrtsv.log", level=logging.DEBUG)

# Set cluster parameters
CLUSTER_SETTINGS = ' -V -cwd -e ./log -o ./log {cluster.params} -w n -S /bin/bash'
CLUSTER_FLAG = ("--drmaa", CLUSTER_SETTINGS, "-w", "60")

# Setup environment for executing commands
PROCESS_ENV = os.environ.copy()

# Prepend to PROCESS_ENV["PATH"]
INSTALL_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

INSTALL_PATH = [  # List of paths relative to INSTALL_DIR to be added to the environment $PATH
    "bin",
    "dist/miniconda/envs/python2/bin",
    "dist/miniconda/envs/python3/bin",
    "dist/miniconda/bin",
    "dist/celera/wgs-8.3rc2/Linux-amd64/bin/",
    "dist/amos-3.1.0/bin",
    "canu/Linux-amd64/bin"
]

PROCESS_ENV_PATH = ":".join([os.path.join(INSTALL_DIR, THIS_PATH) for THIS_PATH in INSTALL_PATH])

if "PATH" in PROCESS_ENV:
    PROCESS_ENV["PATH"] = PROCESS_ENV_PATH + ":" + PROCESS_ENV["PATH"]
else:
    PROCESS_ENV["PATH"] = PROCESS_ENV_PATH

# Prepend to PROCESS_ENV["LD_LIBRARY_PATH"]
INSTALL_LD_PATH = [
    "dist/hdf5/lib"
]

PROCESS_ENV_LD_PATH = ":".join([os.path.join(INSTALL_DIR, THIS_PATH) for THIS_PATH in INSTALL_LD_PATH])

if "LD_LIBRARY_PATH" in PROCESS_ENV:
    PROCESS_ENV["LD_LIBRARY_PATH"] = PROCESS_ENV_LD_PATH + ":" + PROCESS_ENV["LD_LIBRARY_PATH"]
else:
    PROCESS_ENV["LD_LIBRARY_PATH"] = PROCESS_ENV_LD_PATH

os.environ["LD_LIBRARY_PATH"] = PROCESS_ENV["LD_LIBRARY_PATH"]


# Function definitions
def _get_dist_dir():
    dirname, filename = os.path.split(os.path.abspath(__file__))
    return dirname

# def _build_prefix(args):
#     prefix = ["snakemake", "-T", "--rerun-incomplete", "--snakefile", os.path.join(os.path.dirname(_get_dist_dir()), "Snakefile"), "-j", str(args.jobs)]
#     if args.dryrun:
#         prefix.append("-n")
#
#     if args.distribute:
#         prefix.extend(CLUSTER_FLAG)
#
#     return tuple(prefix)

def _run_cmd(args):
    """
    Run a command with the proper environment set.

    :param args: A tuple of arguments starting with the command name.

    :return: Return code or -1 if the process did not complete.
    """
    sys.stdout.flush()

    p = subprocess.Popen(args, env=PROCESS_ENV)

    p.wait()

    ret_code = p.returncode

    return ret_code if ret_code is not None else -1

def _run_snake_target(args, *cmd):
    """
    Run a snakemake target.

    :param args: Arguments processed from the command line.
    :param cmd: The command to run as a tuple starting with the name of the snakemake target.

    :return: Return code from snakemake.
    """
    # Use the user-defined cluster config path if one is given. Otherwise, use
    # an empty config that comes with the SMRT-SV distribution.
    if args.cluster_config is not None:
        cluster_config_path = args.cluster_config
    else:
        cluster_config_path = os.path.join(os.path.dirname(_get_dist_dir()), "cluster.template.json")

    # Setup snakemake command
    prefix = [
        "snakemake",
        "-T",
        "--rerun-incomplete",
        "--cluster-config", cluster_config_path,
        "--snakefile", os.path.join(os.path.dirname(_get_dist_dir()), "Snakefile"),
        "-j", str(args.jobs)
    ]

    if args.dryrun:
        prefix.append("-n")

    if args.distribute:
        prefix.extend(CLUSTER_FLAG)

    # Append command
    prefix.extend(cmd)

    # Append path and ld_path
    prefix.extend([
        "ld_path=%s" % PROCESS_ENV["LD_LIBRARY_PATH"],
        "path=%s" % PROCESS_ENV["PATH"]
    ])

    # Report (verbose)
    if args.verbose:
        print("Running snakemake command: %s" % " ".join(prefix))

    # Run snakemake command
    return _run_cmd(prefix)

def index(args):
    return _run_snake_target(
        args,
        "prepare_reference",
        "--config",
        "reference=%s" % args.reference
    )

def align(args):
    return _run_snake_target(
        args,
        "align_reads",
        "--config",
        "reference=%s" % args.reference,
        "reads=%s" % args.reads,
        "alignments=%s" % args.alignments,
        "alignments_dir=%s" % args.alignments_dir,
        "batches=%s" % args.batches,
        "threads=%s" % args.threads,
        "tmp_dir=%s" % args.tmpdir,
        "alignment_parameters=\"%s\"" % args.alignment_parameters
    )

def detect(args):
{'fraction_non_alphanumeric': 0.13356510037007963, 'fraction_numerical': 0.0040372322529998875, 'mean_word_length': 3.180965775902485, 'pattern_counts': {'":': 8, '<': 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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1720033', 'n_tokens_mistral': 3164, 'n_tokens_neox': 3079, 'n_words': 683}