Skip to content
Snippets Groups Projects
Commit 2ae7fd8a authored by Hugo Madge Leon's avatar Hugo Madge Leon
Browse files

Add check for positive integers in argparse

parent 1ddf3361
No related branches found
No related tags found
2 merge requests!22Hugo branch merge,!18Refactored + full functionality
This commit is part of merge request !18. Comments created here will be created in the context of that merge request.
...@@ -63,14 +63,21 @@ def extant_file(x): ...@@ -63,14 +63,21 @@ def extant_file(x):
raise argparse.ArgumentTypeError("{0} does not exist".format(x)) raise argparse.ArgumentTypeError("{0} does not exist".format(x))
return x return x
# found on https://stackoverflow.com/questions/14117415/in-python-using-argparse-allow-only-positive-integers
def check_positive(value):
ivalue = int(value)
if ivalue <= 0:
raise argparse.ArgumentTypeError("%s is an invalid positive int value" % value)
return ivalue
# Parse command-line arguments # Parse command-line arguments
def parse_arguments(): def parse_arguments():
parser = argparse.ArgumentParser(description="Takes as input FASTA file of cDNA sequences, a CSV with sequence counts, and mean and std. dev. of fragment lengths. Outputs most terminal fragment (within desired length range) for each sequence.") parser = argparse.ArgumentParser(description="Takes as input FASTA file of cDNA sequences, a CSV with sequence counts, and mean and std. dev. of fragment lengths. Outputs most terminal fragment (within desired length range) for each sequence.")
parser.add_argument('--fasta', required=True, type=extant_file, help="FASTA file with cDNA sequences") parser.add_argument('--fasta', required=True, type=extant_file, help="FASTA file with cDNA sequences")
parser.add_argument('--counts', required=True, type=extant_file, help="CSV file with sequence counts") parser.add_argument('--counts', required=True, type=extant_file, help="CSV file with sequence counts")
parser.add_argument('--mean', required = False, default = 10, type = int, help="Mean fragment length (default: 10)") parser.add_argument('--mean', required = False, default = 10, type = check_positive, help="Mean fragment length (default: 10)")
parser.add_argument('--std', required = False, default = 1, type = int, help="Standard deviation fragment length (defafult: 1)") parser.add_argument('--std', required = False, default = 1, type = check_positive, help="Standard deviation fragment length (defafult: 1)")
args = parser.parse_args() args = parser.parse_args()
return args.fasta, args.counts, args.mean, args.std return args.fasta, args.counts, args.mean, args.std
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment