Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dataverse data access module #124

Draft
wants to merge 23 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
add dataverse download functionality
  • Loading branch information
abearab committed Apr 13, 2024
commit 53a0944e83f3d4a7db6facd50eab1983026cd83c
60 changes: 60 additions & 0 deletions gget/gget_dataverse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import os
import requests
from tqdm import tqdm
from utils import print_sys


def dataverse_download(url, path, name, types):
"""dataverse download helper with progress bar

Args:
url (str): the url of the dataset
path (str): the path to save the dataset
name (str): the dataset name
types (dict): a dictionary mapping from the dataset name to the file format
"""
save_path = os.path.join(path, f"{name}.{types[name]}")
response = requests.get(url, stream=True)
total_size_in_bytes = int(response.headers.get("content-length", 0))
block_size = 1024
progress_bar = tqdm(total=total_size_in_bytes, unit="iB", unit_scale=True)
with open(save_path, "wb") as file:
for data in response.iter_content(block_size):
progress_bar.update(len(data))
file.write(data)
progress_bar.close()


def download_wrapper(name, path, return_type=None):
"""wrapper for downloading a dataset given the name and path, for csv,pkl,tsv or similar files

Args:
name (str): the rough dataset query name
path (str): the path to save the dataset
return_type (str, optional): the return type. Defaults to None. Can be "url", "name", or ["url", "name"]

Returns:
str: the exact dataset query name
"""
server_path = "https://dataverse.harvard.edu/api/access/datafile/"

url = server_path + str(name2id[name])

if not os.path.exists(path):
os.mkdir(path)

file_name = f"{name}.{name2type[name]}"

if os.path.exists(os.path.join(path, file_name)):
print_sys("Found local copy...")
os.path.join(path, file_name)
else:
print_sys("Downloading...")
dataverse_download(url, path, name, name2type)

if return_type == "url":
return url
elif return_type == "name":
return file_name
elif return_type == ["url", "name"]:
return url, file_name
10 changes: 10 additions & 0 deletions gget/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# from requests.adapters import HTTPAdapter, Retry
# import time
import re
import sys
import os
import uuid
import pandas as pd
Expand All @@ -22,6 +23,15 @@

from .constants import ENSEMBL_FTP_URL, ENSEMBL_FTP_URL_NV, ENS_TO_PDB_API

def print_sys(s):
"""system print

Args:
s (str): the string to print
"""
print(s, flush = True, file = sys.stderr)


def flatten(xss):
"""
Function to flatten a list of lists.
Expand Down