Skip to content

Commit

Permalink
new notebook no. 16
Browse files Browse the repository at this point in the history
  • Loading branch information
RahiNav committed Jul 10, 2024
1 parent 2f53e9c commit 5200345
Showing 1 changed file with 132 additions and 0 deletions.
132 changes: 132 additions & 0 deletions notebooks/useful_notebooks/16_add_lab_award.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "48aa1039-e6f5-4afb-9333-53ce2bab75cd",
"metadata": {},
"source": [
"## Adds lab and award details (rows and columns) to all sheets of a curated spreadsheet\n",
"### The input file should be a curated excel file."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "2b15efee-e106-4d4b-960a-881d28a0f4e5",
"metadata": {},
"outputs": [],
"source": [
"#setup\n",
"from dcicutils import ff_utils\n",
"from functions.notebook_functions import *\n",
"from functions.wfr import *\n",
"from openpyxl import Workbook, load_workbook\n",
"\n",
"my_auth = get_key('default') #Add key name"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "7ad91fe5-db5a-4f2a-9a12-a91544430a18",
"metadata": {},
"outputs": [],
"source": [
"# List file locations. Infile is a curated workbook, not blank\n",
"infile = ''\n",
"outfile = ''"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d682f105-a450-4c6d-b351-a08af85eafc1",
"metadata": {},
"outputs": [],
"source": [
"# Add lab and award\n",
"lab = ''\n",
"award = ''"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "dd6024e6-fe67-4175-8327-a73ec4bf4c12",
"metadata": {},
"outputs": [],
"source": [
"wb = load_workbook(filename = infile)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bbd0fd41-75fe-4d37-8b89-11159a2deca4",
"metadata": {},
"outputs": [],
"source": [
"for sheet in wb:\n",
" first_row = []\n",
" for row in sheet.iter_rows(min_row=1, max_col=sheet.max_column, max_row=1):\n",
" for cell in row:\n",
" if cell.value != None:\n",
" first_row.append(cell.value)\n",
" last_col = len(first_row) #gets the index of the last column after ignoring None value cells\n",
"\n",
" no_of_aliases = []\n",
" for row in sheet:\n",
" if row[1].value != None:\n",
" no_of_aliases.append(row[1].value)\n",
" t_rows = len(no_of_aliases) #gets the index of total aliases in each sheet after ignoring the None value cells\n",
" \n",
" #adding headers\n",
" l = sheet.cell(row=1, column=last_col + 1, value='lab')\n",
" a = sheet.cell(row=1, column=last_col + 2, value='award')\n",
" l2 = sheet.cell(row=2, column=last_col + 1, value='Item:Lab')\n",
" a2 = sheet.cell(row=2, column=last_col + 2, value='Item:Award')\n",
" \n",
" #adding lab and award details based on how many aliases the sheet has\n",
" for row_no_lab in range(5, t_rows + 1):\n",
" c = sheet.cell(row=row_no_lab, column=last_col + 1, value=lab)\n",
" for row_no_award in range(5, t_rows + 1):\n",
" f = sheet.cell(row=row_no_award, column=last_col + 2, value=award)\n",
" \n",
" print(sheet.title, last_col, t_rows)\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6b3aef60-a25d-4944-b295-d1025a59e8ce",
"metadata": {},
"outputs": [],
"source": [
"#saving in separate file\n",
"wb.save(outfile)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

0 comments on commit 5200345

Please sign in to comment.