Skip to content

Instantly share code, notes, and snippets.

@pvanheus
Last active August 16, 2021 08:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pvanheus/efcd16f13e1571cca0924307683864b7 to your computer and use it in GitHub Desktop.
Save pvanheus/efcd16f13e1571cca0924307683864b7 to your computer and use it in GitHub Desktop.
#!/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