Skip to content

Commit

Permalink
Improve arg parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
mjsteinbaugh committed Sep 18, 2019
1 parent 060424b commit ea088e4
Showing 1 changed file with 32 additions and 19 deletions.
51 changes: 32 additions & 19 deletions bin/ensembl-fasta
@@ -1,10 +1,11 @@
#!/usr/bin/env python3
"""Download Ensembl genome annotations in FASTA format.
"""
Download Ensembl genome annotations in FASTA format.
# See also:
# - https://docs.python.org/2/howto/argparse.html
# - https://stackoverflow.com/questions/24180527
See also:
- https://docs.python.org/2/howto/argparse.html
- https://stackoverflow.com/questions/24180527
"""

import argparse
import os
Expand All @@ -15,41 +16,52 @@ parser = argparse.ArgumentParser()
optional = parser._action_groups.pop()
required = parser.add_argument_group('required arguments')
required.add_argument( \
"--organism", required=True, \
"--organism", \
required=True, \
type=str, \
help="latin name (e.g. \"Homo sapiens\")" \
)
required.add_argument( \
"--build", required=True, \
help="genome build (e.g. \"GRCh38\")" \
"--type", \
required=True, \
choices=['dna', 'cdna'], \
help="\"dna\" (genome) or \"cdna\" (transcriptome)" \
)
required.add_argument( \
"--type", required=True, \
help="\"dna\" (genome) or \"cdna\" (transcriptome)" \
"--build", \
required=True, \
type=str, \
help="genome build (e.g. \"GRCh38\")" \
)
optional.add_argument( \
"--release", \
type=int, \
help="release version (e.g. \"96\")" \
)
optional.add_argument( \
"--decompress", \
action='store_true', \
# default=False, \
help="Decompress (but keep) the original file." \
)
parser._action_groups.append(optional)
args = parser.parse_args()

organism = args.organism.replace(" ", "_")
build = args.build
type = args.type

release = args.release
decompress = args.decompress

# Set the release automatically, if necessary.
if release is None:
release = subprocess.check_output( \
"_koopa_variable ensembl_release_version", \
"koopa variable ensembl_release_version", \
shell=True, universal_newlines=True \
)
release = release.rstrip()

# Inform and exit on legacy "transcriptome" usage.
if type == "transcriptome":
print("Use `cdna` instead of `transcriptome`.")
sys.exit(1)

# Set the base URL of FTP server.
if build == "GRCh37":
release = "87"
base_url = "ftp://ftp.ensembl.org/pub/grch37"
Expand Down Expand Up @@ -80,6 +92,7 @@ except subprocess.CalledProcessError as e:
sys.exit(1)

# Decompress, but also keep the original compressed file.
print("Decompressing " + file + ".")
unzip_file = os.path.splitext(file)[0]
os.system("gunzip -c " + file + " > " + unzip_file)
if decompress is True:
print("Decompressing " + file + ".")
unzip_file = os.path.splitext(file)[0]
os.system("gunzip -c " + file + " > " + unzip_file)

0 comments on commit ea088e4

Please sign in to comment.