Created
March 26, 2018 22:46
-
-
Save laserson/54d374f00ac7b479ae424cb31e9eeb71 to your computer and use it in GitHub Desktop.
Update Airtable records from Excel metadata
This file contains 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
import pandas as pd | |
from airtable import airtable | |
metadata = pd.read_table(metadata_path, sep=',', header=0, index_col=None) | |
at = airtable.Airtable(base_id, api_key) | |
ibd_records = at.get('sample', filter_by_formula='AND(project = "cho-ibd", phenotype = "")')['records'] | |
for record in ibd_records: | |
if (metadata.local_sample == record['fields']['sample_id']).sum() != 1: | |
print(record) | |
raise ValueError('Could not match records') | |
for record in ibd_records: | |
id_ = record['id'] | |
fields = record['fields'] | |
record_metadata = metadata[metadata.local_sample == fields['sample_id']].iloc[0] | |
consortium_id = record_metadata.consortium_id | |
local_pedigree = record_metadata.local_pedigree | |
sex = record_metadata.sex.lower() | |
if pd.isnull(record_metadata.diag): | |
phenotype = ['unaffected'] | |
elif 'Crohn' in record_metadata.diag: | |
phenotype = ['crohns', 'ibd'] | |
elif 'Ulcer' in record_metadata.diag: | |
phenotype = ['uc', 'ibd'] | |
else: | |
raise ValueError('phenotype') | |
age = record_metadata.age.tolist() | |
notes = fields['Notes'] + f'\nconsortium_id: {consortium_id}\nlocal_pedigree: {local_pedigree}' | |
update_data = { | |
'sex': sex, | |
'age': age, | |
'phenotype': phenotype, | |
'Notes': notes} | |
ret = at.update('sample', id_, update_data) | |
print(ret) | |
# break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment