-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimport-csv-experiments.py
90 lines (78 loc) · 3.6 KB
/
import-csv-experiments.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env python
###############
# DESCRIPTION #
##############
#
# This script will read a csv file that contains a list of antibodies, and add
# them to the experiments database with a fine control on which columns are
# processed and how.
#
import elabapi_python
import csv
import json
import urllib3
# disable warnings (they are scary)
urllib3.disable_warnings(category=urllib3.exceptions.InsecureRequestWarning)
#########################
# CONFIG #
#########################
# replace with your instance address
API_HOST_URL = 'https://elab.local:3148/api/v2'
# replace with your api key
API_KEY = 'apiKey4Test'
# path to the csv file, change this too
CSV_PATH = './data/antibodies.csv'
#########################
# END CONFIG #
#########################
# Configure the api client
configuration = elabapi_python.Configuration()
configuration.api_key['api_key'] = API_KEY
configuration.api_key_prefix['api_key'] = 'Authorization'
configuration.host = API_HOST_URL
configuration.debug = False
configuration.verify_ssl = False
api_client = elabapi_python.ApiClient(configuration)
api_client.set_default_header(header_name='Authorization', header_value=API_KEY)
experimentApi = elabapi_python.ExperimentsApi(api_client)
# function to build the metadata json for a row
def getMetadataFromRow(row):
metadata = {'extra_fields': {}}
for keyval in row.items():
field_type = 'text'
if keyval[0] in ['Name', 'Comment', 'ID']:
continue
if keyval[0].lower() == 'url':
field_type = 'url'
if keyval[0].lower() == 'price':
field_type = 'number'
if keyval[0].lower() == 'concentration' and keyval[1]:
split_conc = keyval[1].split()
metadata['extra_fields'].update({keyval[0]: {'value': split_conc[0], 'type': 'number', 'unit': split_conc[1], 'units': ['mg/mL', 'μg/mL']}})
elif keyval[0].lower() == 'primary vs secondary':
metadata['extra_fields'].update({keyval[0]: {'value': 'Primary', 'type': 'select', 'options': ['Primary', 'Secondary']}})
elif keyval[0].lower() == 'raised in':
metadata['extra_fields'].update({keyval[0]: {'value': keyval[1], 'type': 'select', 'options': ['Rabbit', 'Mouse']}})
elif keyval[0].lower() == 'recognizes':
metadata['extra_fields'].update({keyval[0]: {
'value': keyval[1].split(', '), 'type': 'select', 'allow_multi_values': True, 'options': ['Ape', 'Chicken', 'Dog', 'Goat', 'Guinea Pig', 'Hamster', 'Human', 'Mink', 'Monkey', 'Mouse', 'Rabbit', 'Rat', 'Sheep', 'Zebrafish']}})
else:
metadata['extra_fields'].update({keyval[0]: {'value': keyval[1], 'type': field_type}})
return json.dumps(metadata)
def getBodyFromRow(row) -> str:
return f'<p>{row["Comment"]}</p>' if "Comment" in row else ''
print("Starting import script...")
with open(CSV_PATH, newline='') as csvfile:
csvreader = csv.DictReader(csvfile, delimiter=',', quotechar='"')
for row in csvreader:
if row.get("elabftw_id"):
itemId = row["elabftw_id"]
else:
print(f'Creating experiment...')
response = experimentApi.post_experiment_with_http_info(body={})
locationHeaderInResponse = response[2].get("Location")
itemId = int(locationHeaderInResponse.split("/").pop())
row["elabftw_id"] = itemId
print(f'[-] Patching item {itemId}')
experimentApi.patch_experiment(itemId, body={'title': row['Name'], 'body': getBodyFromRow(row), 'custom_id': row['ID'], 'metadata': getMetadataFromRow(row)})
print("Everything imported successfully! :)")