Last active
August 16, 2021 08:46
-
-
Save pvanheus/efcd16f13e1571cca0924307683864b7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import argparse | |
import json | |
from typing import TextIO | |
import urllib3 | |
def submit_project(project_name: str, csv_file: TextIO, newick_file: TextIO, | |
access_token: str = None): | |
http = urllib3.PoolManager() | |
csv_data = csv_file.read() | |
tree_data = newick_file.read() | |
data = { | |
'name': project_name, | |
'data': csv_data, | |
'tree': tree_data, | |
} | |
encoded_data = json.dumps(data).encode('utf-8') | |
headers = { | |
'Content-Type': 'application/json', | |
} | |
if access_token is not None: | |
headers['access-token'] = access_token | |
response = http.request('POST', 'https://microreact.org/api/project/', | |
body=encoded_data, | |
headers=headers) | |
if response.status == 400 or response.status == 200: | |
response_data = json.loads(response.data.decode('utf-8')) | |
if response.status == 200: | |
print(response_data['shortId']) | |
print(response_data['url']) | |
elif response.status == 400: | |
print('ERROR:', response_data['error']) | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser( | |
description='Upload data and create Microreact project') | |
parser.add_argument('--access_token', | |
help='Microreact access token, get it from the API access tab of https://microreact.org/myaccount') | |
parser.add_argument('project_name') | |
parser.add_argument('csv_file', type=argparse.FileType(), | |
help='Sample metadata as described here: https://microreact.org/instructions') | |
parser.add_argument('newick_file', type=argparse.FileType(), | |
help='Newick format tree file (tip labels must match IDs in CSV file)') | |
args = parser.parse_args() | |
submit_project(args.project_name, args.csv_file, args.newick_file, args.access_token) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment