|
| 1 | +import synapseclient |
| 2 | +import pandas as pd |
| 3 | + |
| 4 | + |
| 5 | +def get_bladder_pdo_experiments(synObject, samples, drugs): |
| 6 | + # get list of syn id info |
| 7 | + files = list(syn.getChildren(parent='syn64765430', includeTypes=['file'])) |
| 8 | + # load sample sheet and format _ to . |
| 9 | + # load conversion table and remove trailing _T |
| 10 | + conversion_table = syn.get(files[50]['id']) |
| 11 | + conversion_table_df = pd.read_excel(conversion_table.path, header=None) |
| 12 | + conversion_table_df[2] = conversion_table_df[1].str.rsplit("_", expand=True)[0]#.replace(".", "_") |
| 13 | + conversion_table_df[3] = conversion_table_df[2].str.replace(".", "_") |
| 14 | + #print(conversion_table_df.head) |
| 15 | + |
| 16 | + # initiate empty pd.dat.frame |
| 17 | + drug_df = pd.DataFrame() |
| 18 | + # for each drug, |
| 19 | + for i in range(len(files)-4): |
| 20 | + drug_table_syn =syn.get(files[i]['id']) |
| 21 | + drug_table = pd.read_csv(drug_table_syn.path, sep="\t") |
| 22 | + # melt |
| 23 | + # link to conversion table |
| 24 | + # link to sample sheet |
| 25 | + # Rename, add columns |
| 26 | + melted_single_drug = drug_table.melt(id_vars = 'Unnamed: 0', value_vars = drug_table.columns[1:], var_name="sample") |
| 27 | + melted_single_drug['linkID'] = melted_single_drug['sample'].str.split(".", expand=True)[0] |
| 28 | + drugdata = melted_single_drug.merge(conversion_table_df, left_on = 'linkID', right_on = 0, how='left')[['Unnamed: 0', 'value', 3]] |
| 29 | + #print(drugdata.head) |
| 30 | + drugdata_with_improvesample = drugdata.merge(samples, left_on = 3, right_on='common_name') |
| 31 | + |
| 32 | + # print(drugdata_with_improvesample.head) |
| 33 | + drugdata_with_improvesample = drugdata_with_improvesample[['Unnamed: 0', 'value', 'improve_sample_id']] |
| 34 | + #print(drugdata_with_improvesample.columns) |
| 35 | + drugdata_with_improvesample = drugdata_with_improvesample.rename({"Unnamed: 0" : "DOSE", 'value' : 'GROWTH'}, axis=1) |
| 36 | + #print(drugdata_with_improvesample.columns) |
| 37 | + |
| 38 | + selected_drugdata = drugdata_with_improvesample |
| 39 | + selected_drugdata['chem_name'] = files[i]['name'].split(")")[1].split("(")[0].split(".")[0].strip().lower() |
| 40 | + #print(selected_drugdata.head) |
| 41 | + drugdata_with_both_improveIds = selected_drugdata.merge(drugs[['improve_drug_id', 'chem_name']], how='left') |
| 42 | + final_drugdata = drugdata_with_both_improveIds[['DOSE', 'GROWTH', 'improve_sample_id', 'improve_drug_id']] |
| 43 | + final_drugdata = final_drugdata.rename({'improve_drug_id' : "Drug"}, axis=1) |
| 44 | + final_drugdata['study'] = "Lee etal 2018 Bladder PDOs" |
| 45 | + final_drugdata['source'] = "Synapse" |
| 46 | + final_drugdata['time'] = 6 |
| 47 | + final_drugdata['time_unit'] = 'days' |
| 48 | + #print(final_drugdata.head) |
| 49 | + # append to dataframe |
| 50 | + dose_resp_df = pd.concat([drug_df, final_drugdata]) |
| 51 | + |
| 52 | + return dose_resp_df |
| 53 | + |
| 54 | + |
| 55 | +if __name__ == "__main__": |
| 56 | + parser = argparse.ArgumentParser() |
| 57 | + parser.add_argument('-t' '--token', help='Synapse authentication token') |
| 58 | + parser.add_argument('-s', '--curSampleFile', help='Sample mapping file for bladder pdo samples') |
| 59 | + parser.add_argument('-d', '--drugfile', help='Drug mapping file for bladder pdo samples') |
| 60 | + parser.add_argument('-o', '--output', default = '/tmp/bladderpdo_doserep.tsv',help='Output file to be read into curve fitting code') |
| 61 | + |
| 62 | + args = parser.parse_args() |
| 63 | + print("Logging into Synapse") |
| 64 | + PAT = args.token |
| 65 | + synObject = synapseclient.login(authToken=PAT) |
| 66 | + drug_df = pd.read_csv(args.drugfile, sep='\t') |
| 67 | + samples_df = pd.read_csv(args.curSampleFile) |
| 68 | + |
| 69 | + doseresponse_data = get_bladder_pdo_experiments(synObject, samples_df, drug_df) |
| 70 | + doseresponse_data.to_csv(args.output, sep='\t') |
| 71 | + |
0 commit comments