Skip to content

Commit

Permalink
Update to executable usage
Browse files Browse the repository at this point in the history
  • Loading branch information
aewebb80 committed Oct 18, 2019
1 parent 0631083 commit 8a01bf5
Show file tree
Hide file tree
Showing 12 changed files with 210 additions and 59 deletions.
10 changes: 9 additions & 1 deletion pgpipe/admixtools.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from pgpipe.plink import *
from pgpipe.model import read_model_file, pops_not_in_model
from pgpipe.logging_module import initLogger, logArgs
from pgpipe.misc import confirm_executable

def check_convertf_for_errors (convertf_stderr):
'''
Expand Down Expand Up @@ -58,8 +59,15 @@ def call_convertf (convertf_call_args):
convertf log output
'''

# Confirm where the specifed executable is located
convertf_path = confirm_executable('convertf')

# Check if executable is installed
if not convertf_path:
raise Exception('convertf not found. Please confirm the executable is installed')

# convertf subprocess call without stdout
convertf_call = subprocess.Popen(['convertf'] + list(map(str, convertf_call_args)), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
convertf_call = subprocess.Popen([convertf_path] + list(map(str, convertf_call_args)), stdout=subprocess.PIPE, stderr=subprocess.PIPE)

# Wait for convertf to finish
convertf_stdout, convertf_stderr = convertf_call.communicate()
Expand Down
20 changes: 11 additions & 9 deletions pgpipe/admixture.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@

from pgpipe.logging_module import initLogger, logArgs
from pgpipe.plink import confirm_ped_prefix, confirm_bed_prefix, confirm_ped_files, confirm_bed_files
from pgpipe.misc import confirm_executable

def admix_parser(passed_arguments):
'''admix Argument Parser - Assigns arguments from command line'''
Expand Down Expand Up @@ -177,14 +178,6 @@ def metavar_list (var_list):

def run(passed_arguments = []):

# Assign location of admixture file
admixture_exec = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))),'bin','admixture')

# Check that admixture exists at specified path
if not os.path.isfile(admixture_exec):
raise IOError('admixture executable not found in Path specified: %s' % admixture_exec)


# Grab admixture arguments from command line
admix_args = admix_parser(passed_arguments)

Expand Down Expand Up @@ -278,8 +271,17 @@ def run(passed_arguments = []):

logging.info('admixture parameters assigned')

# Confirm where the specifed executable is located
admixture_path = confirm_executable('admixture')

# Check if the executable was found
if not admixture_path:
raise IOError('admixture not found. Please confirm the executable is installed')

#admixture_path = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))),'bin','admixture')

# Run 'admixture' executable file with options provided by user
admixture_call = subprocess.Popen([admixture_exec] + list(map(str, admix_call_args)), stdout = subprocess.PIPE, stderr = subprocess.PIPE)
admixture_call = subprocess.Popen([admixture_path] + list(map(str, admix_call_args)), stdout = subprocess.PIPE, stderr = subprocess.PIPE)

# Store command output and/or error to variables
admix_stdout, admix_stderr = admixture_call.communicate()
Expand Down
28 changes: 25 additions & 3 deletions pgpipe/bcftools.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#sys.path.insert(0, os.path.abspath(os.path.join(os.pardir,'pppipe')))

from pgpipe.vcf_reader_func import checkFormat
from pgpipe.misc import confirm_executable

def stdout_bcftools_reference():

Expand Down Expand Up @@ -621,8 +622,15 @@ def pipe_bcftools (bcftools_call_args):
'''

# Confirm where the specifed executable is located
bcftools_path = confirm_executable('bcftools')

# Check if the executable was found
if not bcftools_path:
raise IOError('bcftools not found. Please confirm the executable is installed')

# bcftools subprocess call
bcftools_call = subprocess.Popen(['bcftools'] + list(map(str, bcftools_call_args)), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
bcftools_call = subprocess.Popen([bcftools_path] + list(map(str, bcftools_call_args)), stdout=subprocess.PIPE, stderr=subprocess.PIPE)

return bcftools_call

Expand All @@ -647,8 +655,15 @@ def pipe_bcftools_bcftools (bcftools_first_call_args, bcftools_second_call_args)
# Open bcftools pipe
bcftools_first_call = pipe_bcftools(bcftools_first_call_args)

# Confirm where the specifed executable is located
bcftools_path = confirm_executable('bcftools')

# Check if the executable was found
if not bcftools_path:
raise IOError('bcftools not found. Please confirm the executable is installed')

# bgzip subprocess call
bcftools_second_call = subprocess.Popen(['bcftools'] + list(map(str, bcftools_second_call_args)), stdin = bcftools_first_call.stdout, stdout = subprocess.PIPE, stderr = subprocess.PIPE)
bcftools_second_call = subprocess.Popen([bcftools_path] + list(map(str, bcftools_second_call_args)), stdin = bcftools_first_call.stdout, stdout = subprocess.PIPE, stderr = subprocess.PIPE)

# Wait for vctools to finish
bcftools_first_call.wait()
Expand Down Expand Up @@ -840,8 +855,15 @@ def call_bcftools (bcftools_call_args):
If bcftools stderr returns an error
'''

# Confirm where the specifed executable is located
bcftools_path = confirm_executable('bcftools')

# Check if the executable was found
if not bcftools_path:
raise IOError('bcftools not found. Please confirm the executable is installed')

# bcftools subprocess call
bcftools_call = subprocess.Popen(['bcftools'] + list(map(str, bcftools_call_args)), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
bcftools_call = subprocess.Popen([bcftools_path] + list(map(str, bcftools_call_args)), stdout=subprocess.PIPE, stderr=subprocess.PIPE)

# Wait for bcftools to finish
bcftools_stdout, bcftools_stderr = bcftools_call.communicate()
Expand Down
20 changes: 15 additions & 5 deletions pgpipe/beagle.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from pgpipe.logging_module import initLogger, logArgs
from pgpipe.vcftools import bgzip_decompress_vcfgz
from pgpipe.bcftools import convert_vcf, check_for_index, create_index
from pgpipe.misc import confirm_executable

def delete_beagle_log (output_prefix):
'''
Expand Down Expand Up @@ -112,12 +113,21 @@ def standard_beagle_call (beagle_path, beagle_call_args, output_prefix):
Output file prefix
'''

# Assign location of beagle jar file
beagle_jar = os.path.join(beagle_path, 'beagle.jar')
# Assign location of picard jar file
if beagle_path is None:

# Check that beagle.jar exists
if not os.path.isfile(beagle_jar):
raise IOError('beagle.jar not found. Path specified: %s' % beagle_path)
# Create a string with the picard path
beagle_jar = confirm_executable('beagle.jar')

#beagle_jar = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))),'bin','beagle.jar')

else:
# Use path if specified
beagle_jar = os.path.join(picard_path, 'beagle.jar')

# Check if executable is installed
if not beagle_jar:
raise IOError('beagle.jar not found. Please confirm the executable is installed')

logging.info('beagle phasing parameters assigned')

Expand Down
28 changes: 21 additions & 7 deletions pgpipe/bedtools.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import logging
import fileinput

from pgpipe.misc import confirm_executable

def log_bedtools_reference (out_filename, append_mode = False, ref_header = True):

# Check if the file is to be written in append mode
Expand Down Expand Up @@ -71,14 +73,21 @@ def merge_bed_files (bed_files, bed_output_filename, optional_merge_args):
If bedtools stderr returns an error
'''

# Confirm where the specifed executable is located
bedtools_path = confirm_executable('bedtools')

# Check if the executable was found
if not bedtools_path:
raise IOError('bedtools not found. Please confirm the executable is installed')

# Assign the BEDtools input using fileinput
bed_input = fileinput.input(files = bed_files, mode = 'rb')

# Create the output file
bed_output_file = open(bed_output_filename, 'w')

# Call BEDtools to sort the input files
bedtools_sort_call = subprocess.Popen(['bedtools', 'sort', '-i', '-'], stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.PIPE)
bedtools_sort_call = subprocess.Popen([bedtools_path, 'sort', '-i', '-'], stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.PIPE)

# Loop the BEDtools input
for bed_line in bed_input:
Expand All @@ -87,7 +96,7 @@ def merge_bed_files (bed_files, bed_output_filename, optional_merge_args):
bedtools_sort_call.stdin.write(bed_line)

# Call BEDtools to merge the sorted input
bedtools_merge_call = subprocess.Popen(['bedtools', 'merge', '-i', '-'] + list(map(str, optional_merge_args)), stdin = bedtools_sort_call.stdout, stdout = bed_output_file, stderr = subprocess.PIPE)
bedtools_merge_call = subprocess.Popen([bedtools_path, 'merge', '-i', '-'] + list(map(str, optional_merge_args)), stdin = bedtools_sort_call.stdout, stdout = bed_output_file, stderr = subprocess.PIPE)

# Close the sort stdin
bedtools_sort_call.stdin.close()
Expand Down Expand Up @@ -144,15 +153,20 @@ def standard_bedtools_call (bedtools_call_args, bed_output_filename):
If bedtools stderr returns an error
'''

# Create a string with the bedtools path

#bedtools_path = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'bin', 'bedtools')

# Create the output file
bed_output_file = open(bed_output_filename, 'w')

# Confirm where the specifed executable is located
bedtools_path = confirm_executable('bedtools')

# Check if the executable was found
if not bedtools_path:
raise IOError('bedtools not found. Please confirm the executable is installed')

#bedtools_path = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'bin', 'bedtools')

# bedtools subprocess call
bedtools_call = subprocess.Popen(['bedtools'] + list(map(str, bedtools_call_args)), stdout = bed_output_file, stderr = subprocess.PIPE)
bedtools_call = subprocess.Popen([bedtools_path] + list(map(str, bedtools_call_args)), stdout = bed_output_file, stderr = subprocess.PIPE)

# Wait for bedtools to finish
bedtools_out, bedtools_err = bedtools_call.communicate()
Expand Down
Empty file modified pgpipe/convert.py
100644 → 100755
Empty file.
34 changes: 34 additions & 0 deletions pgpipe/misc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import os
import logging

def local_executable (executable):

return os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'bin', executable)

def confirm_executable (executable):

'''
Confirm if an executable exists.
Parameters
----------
executable : str
Executable to confirm
'''

# Loop potental locations of executables
for path in os.environ['PATH'].split(os.pathsep):

# Join current path and executable
executable_file = os.path.join(path, executable)

# Check if the executable path exists, and if so, is an executable
if os.path.isfile(executable_file) and os.access(executable_file, os.X_OK):

logging.info('Calling executable: %s' % executable_file)

# Return the path if the executable was found
return executable_file

# Return None if the executable was not found
return None
16 changes: 12 additions & 4 deletions pgpipe/picard.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from pgpipe.logging_module import initLogger, logArgs
from pgpipe.fasta import check_format
from pgpipe.misc import confirm_executable

def check_ref_file_association (ref_filename, dict_filename, dictonary_ext = 'dict', gunzip_ext = 'gz'):

Expand Down Expand Up @@ -213,13 +214,20 @@ def standard_picard_call (picard_path, picard_call_args, output_filename):

# Assign location of picard jar file
if picard_path is None:
picard_jar = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))),'bin','picard.jar')

# Create a string with the picard path
picard_jar = confirm_executable('picard.jar')

#picard_jar = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))),'bin','picard.jar')

else:

# Use path if specified
picard_jar = os.path.join(picard_path, 'picard.jar')

# Check that picard.jar exists
if not os.path.isfile(picard_jar):
raise IOError('picard.jar not found. Path specified: %s' % picard_path)
# Check if executable is installed
if not picard_jar:
raise IOError('picard.jar not found. Please confirm the executable is installed')

logging.info('picard parameters assigned')

Expand Down
19 changes: 16 additions & 3 deletions pgpipe/plink.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from pgpipe.vcf_reader_func import checkFormat
from pgpipe.bcftools import convert_vcf
from pgpipe.logging_module import initLogger, logArgs
from pgpipe.misc import confirm_executable

def log_plink_reference (out_filename, append_mode = False, ref_header = True):

Expand Down Expand Up @@ -502,9 +503,16 @@ def standard_plink2_call (plink2_call_args):
If plink2 stderr returns an error
'''

# Confirm where the specifed executable is located
plink2_path = confirm_executable('plink2')

# Check if the executable was found
if not plink2_path:
raise IOError('plink2 not found. Please confirm the executable is installed')

#plink2_path = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))),'bin','plink2')

# plink subprocess call
plink2_path = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))),'bin','plink2')
plink2_call = subprocess.Popen([plink2_path] + list(map(str, plink2_call_args)), stdout=subprocess.PIPE, stderr=subprocess.PIPE)

# Wait for plink to finish
Expand Down Expand Up @@ -539,9 +547,14 @@ def standard_plink_call (plink_call_args):
If plink stderr returns an error
'''

# Create a string with the plink path
# Confirm where the specifed executable is located
plink_path = confirm_executable('plink')

# Check if the executable was found
if not plink_path:
raise IOError('plink not found. Please confirm the executable is installed')

plink_path = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'bin', 'plink')
#plink_path = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'bin', 'plink')

# plink subprocess call
plink_call = subprocess.Popen([plink_path] + list(map(str, plink_call_args)), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
Expand Down
15 changes: 10 additions & 5 deletions pgpipe/shapeit.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from pgpipe.vcf_reader_func import checkFormat
from pgpipe.logging_module import initLogger, logArgs
from pgpipe.plink import convert_haps_to_vcf
from pgpipe.misc import confirm_executable

def check_shapeit_for_errors (shapeit_stdout, output_prefix):
'''
Expand Down Expand Up @@ -153,11 +154,15 @@ def standard_shapeit_call (shapeit_call_args, output_prefix):
'''

logging.info('shapeit phasing parameters assigned')
#shapeit_path = '../bin/shapeit'
#shapeit_path = pkg_resources.resource_filename('shapeit','../bin/shapeit')
# Phasing subprocess call
#shapeit_path='shapeit'
shapeit_path = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))),'bin','shapeit')

# Confirm where the specifed executable is located
shapeit_path = confirm_executable('shapeit')

# Check if the executable was found
if not shapeit_path:
raise IOError('shapeit not found. Please confirm the executable is installed')

#shapeit_path = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))),'bin','shapeit')

#sys.stderr.write(str(shapeit_path)+'\n')
phase_call = subprocess.Popen([shapeit_path] + shapeit_call_args, stdout = subprocess.PIPE, stderr = subprocess.PIPE)
Expand Down

0 comments on commit 8a01bf5

Please sign in to comment.