Skip to content

Commit

Permalink
Update functions to work with args. Changed tests to args
Browse files Browse the repository at this point in the history
  • Loading branch information
aewebb80 committed Jul 21, 2020
1 parent f2104b4 commit b47ff9f
Show file tree
Hide file tree
Showing 14 changed files with 166 additions and 626 deletions.
23 changes: 12 additions & 11 deletions pgpipe/bed_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,12 +237,11 @@
import csv
import numpy as np

# Import basic bedtools functions
from pgpipe.bedtools_wrapper import merge_bed_files, standard_bedtools_call, log_bedtools_reference

from pgpipe.logging_module import initLogger, logArgs
from pgpipe.misc import argprase_kwargs

def bed_argument_parser(passed_arguments):
def bed_argument_parser(passed_arguments = []):
'''Phase Argument Parser - Assigns arguments for vcftools from command line.
Depending on the argument in question, a default value may be specified'''

Expand Down Expand Up @@ -314,12 +313,10 @@ def metavar_list (var_list):
bed_parser.add_argument('--out', help = 'Defines the output filename', default = 'out.bed')
bed_parser.add_argument('--overwrite', help = "Defines that previous output files should be overwritten", action = 'store_true')



if passed_arguments:
return bed_parser.parse_args(passed_arguments)
return vars(bed_parser.parse_args(passed_arguments))
else:
return bed_parser.parse_args()
return vars(bed_parser.parse_args())

def random_bed_sampler (bed_filename, out_filename, sample_size, with_replacements = False):
'''
Expand Down Expand Up @@ -364,7 +361,7 @@ def random_bed_sampler (bed_filename, out_filename, sample_size, with_replacemen
# Write the output
out_writer.writerows(random_samples)

def run (passed_arguments = []):
def run (**kwargs):
'''
Utilites for BED-formatted files
Expand Down Expand Up @@ -425,8 +422,12 @@ def run (passed_arguments = []):
'''

# Grab BED arguments from command line
bed_args = bed_argument_parser(passed_arguments)
# Update kwargs with defaults
if __name__ != "__main__":
kwargs = argprase_kwargs(kwargs, bed_argument_parser)

# Assign arguments
bed_args = argparse.Namespace(**kwargs)

# Adds the arguments (i.e. parameters) to the log file
logArgs(bed_args, 'bed_utilities')
Expand Down Expand Up @@ -597,4 +598,4 @@ def run (passed_arguments = []):

if __name__ == "__main__":
initLogger()
run()
run(**bed_argument_parser())
6 changes: 5 additions & 1 deletion pgpipe/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,17 @@ def confirm_executable (executable):
return None


def argprase_kwargs (kwarg_dict, argparse_function):
def argprase_kwargs (kwarg_dict, argparse_function, bad = False):

# Convert the kwargs
kwargs_list = []
for arg, value in kwarg_dict.items():
if isinstance(value, (bool)):
kwargs_list.append('--%s' % arg.replace('_','-'))
elif isinstance(value, (list)) and isinstance(value[0], (list)):
for sub_list in value: kwargs_list.extend(['--%s' % arg.replace('_','-')] + [str(sub_value) for sub_value in sub_list])
elif isinstance(value, (list)):
kwargs_list.extend(['--%s' % arg.replace('_','-')] + [str(sub_value) for sub_value in value])
else:
kwargs_list.extend(['--%s' % arg.replace('_','-'), str(value)])
return argparse_function(kwargs_list)
19 changes: 12 additions & 7 deletions pgpipe/model_creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,9 @@

from pgpipe.model import Model, ModelFile, write_model_file, read_model_file
from pgpipe.logging_module import initLogger, logArgs
from pgpipe.misc import argprase_kwargs

def model_creator_parser (passed_arguments):
def model_creator_parser (passed_arguments = []):
'''VCF Argument Parser - Assigns arguments from command line'''

def parser_confirm_file ():
Expand Down Expand Up @@ -351,9 +352,9 @@ def metavar_list (var_list):
model_parser.add_argument('--overwrite', help = "Specifies if previous output files should be overwritten", action = 'store_true')

if passed_arguments:
return model_parser.parse_args(passed_arguments)
return vars(model_parser.parse_args(passed_arguments))
else:
return model_parser.parse_args()
return vars(model_parser.parse_args())

def incompatible_duplicates_check (term, *arguments_to_test):

Expand Down Expand Up @@ -472,7 +473,7 @@ def file_dict_argument_to_str (term, file_argument):
# Return data as str
return data_str

def run (passed_arguments = []):
def run (**kwargs):
'''
Parameters
Expand Down Expand Up @@ -529,8 +530,12 @@ def run (passed_arguments = []):
No individuals assigned to population
'''

# Grab VCF arguments from command line
creator_args = model_creator_parser(passed_arguments)
# Update kwargs with defaults
if __name__ != "__main__":
kwargs = argprase_kwargs(kwargs, model_creator_parser)

# Assign arguments
creator_args = argparse.Namespace(**kwargs)

# Adds the arguments (i.e. parameters) to the log file
logArgs(creator_args, 'model_creator')
Expand Down Expand Up @@ -751,4 +756,4 @@ def run (passed_arguments = []):

if __name__ == "__main__":
initLogger()
run()
run(**model_creator_parser())
29 changes: 12 additions & 17 deletions pgpipe/stat_sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,22 +95,13 @@
import pandas as pd
from collections import defaultdict

# Insert Jared's directory path, required for calling Jared's functions. Change when directory structure changes.
#sys.path.insert(0, os.path.abspath(os.path.join(os.pardir, 'pppipe')))

# Import log initializer
from pgpipe.logging_module import initLogger, logArgs

# Import basic vcftools functions
from pgpipe.vcftools import *

# Import basic vcf
from pgpipe.bcftools import check_for_index, create_index

# Model file related functions
from pgpipe.model import read_model_file
from pgpipe.misc import argprase_kwargs

def sampler_parser(passed_arguments):
def sampler_parser(passed_arguments = []):
'''
Stat Sampler Phase Argument Parser
Expand Down Expand Up @@ -172,9 +163,9 @@ def metavar_list (var_list):


if passed_arguments:
return sampler_parser.parse_args(passed_arguments)
return vars(sampler_parser.parse_args(passed_arguments))
else:
return sampler_parser.parse_args()
return vars(sampler_parser.parse_args())

def random_vcftools_sampler (stat_file_data, sample_size, with_replacements = False):
'''
Expand Down Expand Up @@ -331,7 +322,7 @@ def assign_statistic_column (sample_headers, statistic):
# Return the converted statistic
return converted_statistic

def run (passed_arguments = []):
def run (**kwargs):
'''
Statistics sampler.
Expand Down Expand Up @@ -379,8 +370,12 @@ def run (passed_arguments = []):
Window size argument not defined (if necessary)
'''

# Get arguments from command line
sampler_args = sampler_parser(passed_arguments)
# Update kwargs with defaults
if __name__ != "__main__":
kwargs = argprase_kwargs(kwargs, sampler_parser)

# Assign arguments
sampler_args = argparse.Namespace(**kwargs)

# Adds the arguments (i.e. parameters) to the log file
logArgs(sampler_args, func_name = 'stat_sampler')
Expand Down Expand Up @@ -468,4 +463,4 @@ def run (passed_arguments = []):

if __name__ == "__main__":
initLogger()
run()
run(**sampler_parser())
25 changes: 12 additions & 13 deletions pgpipe/vcf_calc.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,16 +154,12 @@
import shutil
import logging

# Import basic vcftools functions
from pgpipe.vcftools import *

# Model file related functions
from pgpipe.model import read_model_file

# Import logging functions
from pgpipe.logging_module import initLogger, logArgs
from pgpipe.misc import argprase_kwargs

def vcf_calc_parser(passed_arguments):
def vcf_calc_parser(passed_arguments = []):
'''
VCF Calc Argument Parser
Expand Down Expand Up @@ -270,12 +266,11 @@ def metavar_list (var_list):
vcf_parser.add_argument('--filter-exclude-indv-file', help = 'Defines a file of individuals to exclude', action = parser_confirm_file())

if passed_arguments:
return vcf_parser.parse_args(passed_arguments)
return vars(vcf_parser.parse_args(passed_arguments))
else:
return vcf_parser.parse_args()
return vars(vcf_parser.parse_args())


def run (passed_arguments = []):
def run (**kwargs):
'''
Statistic calculation using VCFTools.
Expand Down Expand Up @@ -343,8 +338,12 @@ def calc_exception (selected_model, exc_type, exc_value, exc_traceback):
# Report the original error
sys.__excepthook__(exc_type, exc_value, exc_traceback)

# Grab VCF arguments from command line
vcf_args = vcf_calc_parser(passed_arguments)
# Update kwargs with defaults
if __name__ != "__main__":
kwargs = argprase_kwargs(kwargs, vcf_calc_parser)

# Assign arguments
vcf_args = argparse.Namespace(**kwargs)

# Adds the arguments (i.e. parameters) to the log file
logArgs(vcf_args, func_name = 'vcf_calc')
Expand Down Expand Up @@ -712,4 +711,4 @@ def return_filename (filepath):

if __name__ == "__main__":
initLogger()
run()
run(**vcf_calc_parser())
10 changes: 0 additions & 10 deletions pgpipe/vcf_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,16 +303,6 @@ def metavar_list (var_list):
snps_filters.add_argument('--filter-include-snps', help = 'Include variants if they contain a SNP', action = 'store_true')
snps_filters.add_argument('--filter-exclude-snps', help = 'Exclude variants if they contain a SNP', action = 'store_true')

def str_to_bool(value):
if value.lower() in {'false', 'f', '0', 'no', 'n'}:
return False
elif value.lower() in {'true', 't', '1', 'yes', 'y'}:
return True
raise ValueError(f'{value} is not a valid boolean value')

vcf_parser.add_argument('--foo', type=str_to_bool, nargs='?', const=True, default=False)


# Position filters
vcf_parser.add_argument('--filter-include-pos', help = 'Defines comma seperated positions (i.e. CHROM:START-END) to include. START and END are optional. May be used multiple times', nargs = '+', type = str, action = parser_add_to_list())
vcf_parser.add_argument('--filter-exclude-pos', help = 'Defines comma seperated positions (i.e. CHROM:START-END) to exclude. START and END are optional. May be used multiple times', nargs = '+', type = str, action = parser_add_to_list())
Expand Down
22 changes: 12 additions & 10 deletions pgpipe/vcf_format_conversions.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,15 @@
import glob
import logging

#sys.path.insert(0, os.path.abspath(os.path.join(os.pardir, 'pppipe')))

# Import PPP modules and scripts
from pgpipe.eigenstrat_wrapper import *
from pgpipe.bcftools import convert_vcf, log_bcftools_reference
from pgpipe.plink import convert_ped, convert_bed, convert_vcf_to_plink, log_plink_reference
from pgpipe.vcf_reader_func import checkFormat
from pgpipe.logging_module import initLogger, logArgs
from pgpipe.misc import argprase_kwargs


def convert_argument_parser(passed_arguments):
def convert_argument_parser(passed_arguments = []):
'''
Convert Argument Parser
Expand Down Expand Up @@ -148,9 +146,9 @@ def metavar_list (var_list):
convert_parser.add_argument('--threads', help = "Set the number of threads. Only supported with ped", type = int, default = 1)

if passed_arguments:
return convert_parser.parse_args(passed_arguments)
return vars(convert_parser.parse_args(passed_arguments))
else:
return convert_parser.parse_args()
return vars(convert_parser.parse_args())

def check_if_conversion (input_format, out_format):
'''
Expand Down Expand Up @@ -196,7 +194,7 @@ def check_conversion_support (out_format, supported_formats):
if out_format not in supported_formats:
raise Exception('Input cannot be converted into the format specified')

def run (passed_arguments = []):
def run (**kwargs):
'''
File conversion suite
Expand Down Expand Up @@ -236,8 +234,12 @@ def run (passed_arguments = []):
Conversion to same format given
'''

# Grab plink arguments from command line
convert_args = convert_argument_parser(passed_arguments)
# Update kwargs with defaults
if __name__ != "__main__":
kwargs = argprase_kwargs(kwargs, convert_argument_parser)

# Assign arguments
convert_args = argparse.Namespace(**kwargs)

# Adds the arguments (i.e. parameters) to the log file
logArgs(convert_args, func_name = 'convert')
Expand Down Expand Up @@ -384,4 +386,4 @@ def run (passed_arguments = []):

if __name__ == "__main__":
initLogger()
run()
run(**convert_argument_parser())
23 changes: 14 additions & 9 deletions pgpipe/vcf_phase.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,9 @@
from pgpipe.shapeit import call_shapeit, remove_shapeit_intermediate_files, check_for_shapeit_intermediate_files
from pgpipe.bcftools import get_unique_chrs, get_samples, chr_subset_file, concatenate, check_for_index, create_index
from pgpipe.plink import convert_haps_to_vcf
from pgpipe.misc import argprase_kwargs

def phase_argument_parser(passed_arguments):
def phase_argument_parser(passed_arguments = []):
'''
VCF Phase Argument Parser
Expand Down Expand Up @@ -287,9 +288,9 @@ def metavar_list (var_list):
phase_parser.add_argument('--beagle-nsteps', help = 'Number of consecutive --beagle-steps used for identifying long IBS segments (beagle)', type = int)

if passed_arguments:
return phase_parser.parse_args(passed_arguments)
return vars(phase_parser.parse_args(passed_arguments))
else:
return phase_parser.parse_args()
return vars(phase_parser.parse_args())

def log_to_stdout (log_filename):
'''
Expand Down Expand Up @@ -437,7 +438,7 @@ def assign_filename_prefix (output_filename, output_format):
# Return the updated prefix
return updated_prefix

def run (passed_arguments = []):
def run (**kwargs):
'''
Phaser for VCF files.
Expand Down Expand Up @@ -516,8 +517,12 @@ def run (passed_arguments = []):
Incompatible arguments
'''

# Grab VCF arguments from command line
phase_args = phase_argument_parser(passed_arguments)
# Update kwargs with defaults
if __name__ != "__main__":
kwargs = argprase_kwargs(kwargs, phase_argument_parser)

# Assign arguments
phase_args = argparse.Namespace(**kwargs)

# Adds the arguments (i.e. parameters) to the log file
logArgs(phase_args, func_name = 'vcf_phase')
Expand Down Expand Up @@ -555,8 +560,8 @@ def run (passed_arguments = []):
filter_indv_filename = ''

# Check if the has specified the output filename, without a prefix
if phase_args.out and '--out-prefix' not in passed_arguments and '--out-prefix' not in sys.argv:

if phase_args.out:
# Assign a prefix based on the output filename
phase_args.out_prefix = assign_filename_prefix(phase_args.out, phase_args.out_format)

Expand Down Expand Up @@ -997,4 +1002,4 @@ def run (passed_arguments = []):

if __name__ == "__main__":
initLogger()
run()
run(**phase_argument_parser())

0 comments on commit b47ff9f

Please sign in to comment.