Skip to content

Commit

Permalink
Merge pull request #165 from hammerlab/fix-163
Browse files Browse the repository at this point in the history
In load_vcf, when passed a URL download it first to a local file then…
  • Loading branch information
timodonnell committed Aug 8, 2016
2 parents c9a939e + 4939a46 commit e02e8af
Showing 1 changed file with 26 additions and 9 deletions.
35 changes: 26 additions & 9 deletions varcode/vcf.py
Expand Up @@ -14,8 +14,10 @@


from __future__ import absolute_import, print_function, division
import os
import requests
import zlib
import logging
from collections import OrderedDict
from warnings import warn

Expand Down Expand Up @@ -91,15 +93,30 @@ def load_vcf(
# implementation of read_table (with engine="python") helps with some
# issues but introduces a new set of problems (e.g. the dtype parameter
# is not accepted). For these reasons, we're currently not attempting
# to load VCFs over HTTP with pandas, and fall back to the pyvcf
# implementation here.
return load_vcf(
path,
genome=genome,
reference_vcf_key=reference_vcf_key,
only_passing=only_passing,
allow_extended_nucleotides=allow_extended_nucleotides,
max_variants=max_variants)
# 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)
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 e02e8af

Please sign in to comment.