<commit_before>'''
Command utilities for biolib

This module provides utilities to run external commands into biolib
'''

# Copyright 2009 <NAME>, <NAME>, COMAV-Univ. Politecnica Valencia
# This file is part of biolib.
# biolib is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.

# biolib is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR  PURPOSE.  See the
# GNU Affero General Public License for more details.

# You should have received a copy of the GNU Affero General Public License
# along with biolib. If not, see <http://www.gnu.org/licenses/>.


from biolib.biolib_utils import temp_fasta_file, NamedTemporaryDir, parse_fasta
import subprocess, signal, tempfile, os
import StringIO, logging

def call(cmd, env=None, stdin=None):
    'It calls a command and it returns stdout, stderr and retcode'
    def subprocess_setup():
        ''' Python installs a SIGPIPE handler by default. This is usually not
        what non-Python subprocesses expect.  Taken from this url:
        http://www.chiark.greenend.org.uk/ucgi/~cjwatson/blosxom/2009/07/02#
        2009-07-02-python-sigpipe'''
        signal.signal(signal.SIGPIPE, signal.SIG_DFL)

    if stdin is None:
        pstdin = None
    else:
        pstdin = subprocess.PIPE

    process = subprocess.Popen(cmd, stdout=subprocess.PIPE,
                               stderr=subprocess.PIPE, env=env, stdin=pstdin,
                               preexec_fn=subprocess_setup)
    if stdin is None:
        stdout, stderr = process.communicate()
    else:
#        a = stdin.read()
#        print a
#        stdout, stderr = subprocess.Popen.stdin = stdin
#        print stdin.read()
        stdout, stderr = process.communicate(stdin)
    retcode = process.returncode
    return stdout, stderr, retcode


# Runner definitions, Define here the parameters of the prgrams you want to
# use with this class
STDOUT   = 'stdout'
ARGUMENT = 'argument'
STDIN    = 'stdin'
RUNNER_DEFINITIONS = {
    'blast': {'parameters': {'database' :{'required':True,  'option': '-d'},
                             'program'  :{'required':True,  'option':'-p'},
                             'expect'   :{'default': 0.0001,'option': '-e'},
                             'nhitsv'   :{'default': 20,    'option':'-v'},
                             'nhitsb'   :{'default': 20,    'option':'-b'},
                             'megablast':{'default':'T',  'option':'-n'},
                             'alig_format': {'default':7, 'option':'-m'}<commit_msg>remove magablast T from blast_def<commit_after>'''
Command utilities for biolib

This module provides utilities to run external commands into biolib
'''

# Copyright 2009 <NAME>, <NAME>, COMAV-Univ. Politecnica Valencia
# This file is part of biolib.
# biolib is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.

# biolib is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR  PURPOSE.  See the
# GNU Affero General Public License for more details.

# You should have received a copy of the GNU Affero General Public License
# along with biolib. If not, see <http://www.gnu.org/licenses/>.


from biolib.biolib_utils import temp_fasta_file, NamedTemporaryDir, parse_fasta
import subprocess, signal, tempfile, os
import StringIO, logging

def call(cmd, env=None, stdin=None):
    'It calls a command and it returns stdout, stderr and retcode'
    def subprocess_setup():
        ''' Python installs a SIGPIPE handler by default. This is usually not
        what non-Python subprocesses expect.  Taken from this url:
        http://www.chiark.greenend.org.uk/ucgi/~cjwatson/blosxom/2009/07/02#
        2009-07-02-python-sigpipe'''
        signal.signal(signal.SIGPIPE, signal.SIG_DFL)

    if stdin is None:
        pstdin = None
    else:
        pstdin = subprocess.PIPE

    process = subprocess.Popen(cmd, stdout=subprocess.PIPE,
                               stderr=subprocess.PIPE, env=env, stdin=pstdin,
                               preexec_fn=subprocess_setup)
    if stdin is None:
        stdout, stderr = process.communicate()
    else:
#        a = stdin.read()
#        print a
#        stdout, stderr = subprocess.Popen.stdin = stdin
#        print stdin.read()
        stdout, stderr = process.communicate(stdin)
    retcode = process.returncode
    return stdout, stderr, retcode


# Runner definitions, Define here the parameters of the prgrams you want to
# use with this class
STDOUT   = 'stdout'
ARGUMENT = 'argument'
STDIN    = 'stdin'
RUNNER_DEFINITIONS = {
    'blast': {'parameters': {'database' :{'required':True,  'option': '-d'},
                             'program'  :{'required':True,  'option':'-p'},
                             'expect'   :{'default': 0.0001,'option': '-e'},
                             'nhitsv'   :{'default': 20,    'option':'-v'},
                             'nhitsb'   :{'default': 20,    'option':'-b'},
                             #'megablast':{'default':'T',  'option':'-n'},
                             'alig_format': {'default':7, 'option':'-m'}
{'fraction_non_alphanumeric': 0.10642103380432838, 'fraction_numerical': 0.01108925058129136, 'mean_word_length': 2.6007726980038637, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '1196802', 'n_tokens_mistral': 1730, 'n_tokens_neox': 1624, 'n_words': 549}