Skip to content

Commit

Permalink
cleanup temporary file in load_vcf
Browse files Browse the repository at this point in the history
  • Loading branch information
timodonnell committed Aug 6, 2016
1 parent a328f04 commit 4939a46
Showing 1 changed file with 22 additions and 16 deletions.
38 changes: 22 additions & 16 deletions varcode/vcf.py
Expand Up @@ -17,6 +17,7 @@
import os
import requests
import zlib
import logging
from collections import OrderedDict
from warnings import warn

Expand Down Expand Up @@ -94,23 +95,28 @@ def load_vcf(
# is not accepted). For these reasons, we're currently not attempting
# to load VCFs over HTTP with pandas directly, and instead download it
# to a temporary file and open that.
(filename, headers) = urllib.request.urlretrieve(path)

# The downloaded file has no file extension, which confuses pyvcf for
# gziped files in Python 3. We rename it to have the correct file
# extension.
new_filename = "%s.%s" % (filename, parsed_path.path.split(".")[-1])
os.rename(filename, new_filename)

return load_vcf(
new_filename,
genome=genome,
reference_vcf_key=reference_vcf_key,
only_passing=only_passing,
allow_extended_nucleotides=allow_extended_nucleotides,
include_info=include_info,
chunk_size=chunk_size,
max_variants=max_variants)
(filename, headers) = urllib.request.urlretrieve(path)
try:
# The downloaded file has no file extension, which confuses pyvcf
# for gziped files in Python 3. We rename it to have the correct
# file extension.
new_filename = "%s.%s" % (
filename, parsed_path.path.split(".")[-1])
os.rename(filename, new_filename)
filename = new_filename
return load_vcf(
filename,
genome=genome,
reference_vcf_key=reference_vcf_key,
only_passing=only_passing,
allow_extended_nucleotides=allow_extended_nucleotides,
include_info=include_info,
chunk_size=chunk_size,
max_variants=max_variants)
finally:
logging.info("Removing temporary file: %s" % filename)
os.unlink(filename)

# Loading a local file.
# The file will be opened twice: first to parse the header with pyvcf, then
Expand Down

0 comments on commit 4939a46

Please sign in to comment.