<commit_before>#(c) 2016 by Authors
#This file is a part of ABruijn program.
#Released under the BSD license (see LICENSE file)

"""
Runs polishing binary in parallel and concatentes output
"""

import logging
import random
import subprocess
import os
from collections import defaultdict
from threading import Thread

import flye.bubbles as bbl
import flye.fasta_parser as fp
from flye.utils import which
import flye.config as config


POLISH_BIN = "flye-polish"

logger = logging.getLogger()


class PolishException(Exception):
    pass


def check_binaries():
    if not which(POLISH_BIN):
        raise PolishException("polishing binary was not found. "
                              "Did you run 'make'?")
    try:
        devnull = open(os.devnull, "w")
        subprocess.check_call([POLISH_BIN, "-h"], stderr=devnull)
    except subprocess.CalledProcessError as e:
        if e.returncode == -9:
            logger.error("Looks like the system ran out of memory")
        raise PolishException(str(e))
    except OSError as e:
        raise PolishException(str(e))


def polish(bubbles_file, num_proc, err_mode, work_dir, iter_id, out_polished,
           output_progress):
    _ROOT = os.path.dirname(__file__)

    subs_matrix = os.path.join(_ROOT, "resource",
                               config.vals["err_modes"][err_mode]["subs_matrix"])
    hopo_matrix = os.path.join(_ROOT, "resource",
                               config.vals["err_modes"][err_mode]["hopo_matrix"])

    consensus_out = os.path.join(work_dir, "consensus_{0}.fasta"
                                                .format(iter_id))
    _run_polish_bin(bubbles_file, subs_matrix, hopo_matrix,
                    consensus_out, num_proc, output_progress)
    polished_fasta, polished_lengths = _compose_sequence([consensus_out])
    fp.write_fasta_dict(polished_fasta, out_polished)

    return polished_lengths


def _run_polish_bin(bubbles_in, subs_matrix, hopo_matrix,
                    consensus_out, num_threads, output_progress):
    """
    Invokes polishing binary
    """
    cmdline = [POLISH_BIN, "-t", str(num_threads), bubbles_in, subs_matrix,
               hopo_matrix, consensus_out]
    if not output_progress:
        cmdline.append("-q")

    try:
        subprocess.check_call(cmdline)<commit_msg>polishing argumnets order copatible with OSX optarg<commit_after>#(c) 2016 by Authors
#This file is a part of ABruijn program.
#Released under the BSD license (see LICENSE file)

"""
Runs polishing binary in parallel and concatentes output
"""

import logging
import random
import subprocess
import os
from collections import defaultdict
from threading import Thread

import flye.bubbles as bbl
import flye.fasta_parser as fp
from flye.utils import which
import flye.config as config


POLISH_BIN = "flye-polish"

logger = logging.getLogger()


class PolishException(Exception):
    pass


def check_binaries():
    if not which(POLISH_BIN):
        raise PolishException("polishing binary was not found. "
                              "Did you run 'make'?")
    try:
        devnull = open(os.devnull, "w")
        subprocess.check_call([POLISH_BIN, "-h"], stderr=devnull)
    except subprocess.CalledProcessError as e:
        if e.returncode == -9:
            logger.error("Looks like the system ran out of memory")
        raise PolishException(str(e))
    except OSError as e:
        raise PolishException(str(e))


def polish(bubbles_file, num_proc, err_mode, work_dir, iter_id, out_polished,
           output_progress):
    _ROOT = os.path.dirname(__file__)

    subs_matrix = os.path.join(_ROOT, "resource",
                               config.vals["err_modes"][err_mode]["subs_matrix"])
    hopo_matrix = os.path.join(_ROOT, "resource",
                               config.vals["err_modes"][err_mode]["hopo_matrix"])

    consensus_out = os.path.join(work_dir, "consensus_{0}.fasta"
                                                .format(iter_id))
    _run_polish_bin(bubbles_file, subs_matrix, hopo_matrix,
                    consensus_out, num_proc, output_progress)
    polished_fasta, polished_lengths = _compose_sequence([consensus_out])
    fp.write_fasta_dict(polished_fasta, out_polished)

    return polished_lengths


def _run_polish_bin(bubbles_in, subs_matrix, hopo_matrix,
                    consensus_out, num_threads, output_progress):
    """
    Invokes polishing binary
    """
    cmdline = [POLISH_BIN, "-t", str(num_threads)]
    if not output_progress:
        cmdline.append("-q")
    cmdline.extend([bubbles_in, subs_matrix,
                    hopo_matrix, consensus_out])

    try:
        subprocess.check_call(cmdline)
{'fraction_non_alphanumeric': 0.09168808911739502, 'fraction_numerical': 0.002570694087403599, 'mean_word_length': 2.881130507065669, 'pattern_counts': {'":': 0, '<': 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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '294482', 'n_tokens_mistral': 1453, 'n_tokens_neox': 1410, 'n_words': 347}