{
"cells": [
{
"cell_type": "raw",
"metadata": {
"raw_mimetype": "text/restructuredtext",
"vscode": {
"languageId": "raw"
}
},
"source": [
".. include:: ../../defs/external_refs.txt\n",
"\n",
".. _query_with_sqlalchemy_example:\n",
"\n",
"=============================================\n",
"Query the database with ``SQLAlchemy``\n",
"============================================="
]
},
{
"cell_type": "raw",
"metadata": {
"raw_mimetype": "text/restructuredtext",
"vscode": {
"languageId": "raw"
}
},
"source": [
"In this tutorial we demonstrate how to:\n",
" - Query cell counts as proportions using ``SQLAlchemy``\n",
" - Query clinical data using ``SQLAlchemy``\n",
" - Merge the data together to compare by treatment\n",
"\n",
"To demonstrate ``SQLAlchemy``, we will follow a common use-case scenario in which we wish\n",
"to compare cell phenotype proportions between patients treated with anti-PD-1\n",
"(``Anti-PD-1``) and patients treated with anti-PD-1 plus anti-CTLA-4 (``Anti-PD-1+CTLA-4``).\n",
"\n",
".. seealso::\n",
" \n",
" :ref:`query_with_raw_sql_example` for a parallel tutorial\n",
" using raw ``SQL``.\n",
"\n",
".. note::\n",
"\n",
" The |astropath| databases all use Microsoft SQL Server which uses ``t-SQL``.\n",
"\n",
"We begin by initializing our connection to |astropath|. In this scenario, we will be\n",
"connecting to ``apmifwsi002_DR3``.\n",
"\n",
".. seealso::\n",
"\n",
" Consult the :ref:`database_schema_doc` when navigating the different database contexts\n",
" and their respective data tables."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from astropathdb import AstroDB\n",
"db = AstroDB(database='apmifwsi002_DR3')"
]
},
{
"cell_type": "raw",
"metadata": {
"raw_mimetype": "text/restructuredtext",
"vscode": {
"languageId": "raw"
}
},
"source": [
"Querying Cell Counts using ``SQLAlchemy``\n",
"===========================================\n",
"\n",
"We'll start with a more complicated query then, in the subsequent cells, we'll break\n",
"the query down into it's parts and see how we got there.\n",
"\n",
"This query fetches cell counts and proportions grouped by phenotype and sample in the database\n",
"and returns our results as a :class:`pandas.DataFrame`. We also merge on the :ref:`phenotype_schema`\n",
"so that we can get the string representations of the phenotypes. The ``suffix_with`` at the\n",
"end of the query adds ``OPTION (MAXDOP 4)`` to the ``SQL``, which limits ``SQL Server`` to four\n",
"parallel threads. Without it, this query takes longer than the 60 seconds that ``CasJobs`` allows a query."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" sampleid | \n",
" phenotype | \n",
" count | \n",
" p | \n",
" totalcells | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" 100 | \n",
" CD163 | \n",
" 7429 | \n",
" 0.099362 | \n",
" 74767 | \n",
"
\n",
" \n",
" | 1 | \n",
" 100 | \n",
" CD8 | \n",
" 1181 | \n",
" 0.015796 | \n",
" 74767 | \n",
"
\n",
" \n",
" | 2 | \n",
" 100 | \n",
" FoxP3 | \n",
" 2111 | \n",
" 0.028234 | \n",
" 74767 | \n",
"
\n",
" \n",
" | 3 | \n",
" 100 | \n",
" FoxP3CD8 | \n",
" 23 | \n",
" 0.000308 | \n",
" 74767 | \n",
"
\n",
" \n",
" | 4 | \n",
" 100 | \n",
" Other | \n",
" 34970 | \n",
" 0.467720 | \n",
" 74767 | \n",
"
\n",
" \n",
" | ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
"
\n",
" \n",
" | 352 | \n",
" 566 | \n",
" CD8 | \n",
" 142 | \n",
" 0.007292 | \n",
" 19474 | \n",
"
\n",
" \n",
" | 353 | \n",
" 566 | \n",
" FoxP3 | \n",
" 560 | \n",
" 0.028756 | \n",
" 19474 | \n",
"
\n",
" \n",
" | 354 | \n",
" 566 | \n",
" FoxP3CD8 | \n",
" 4 | \n",
" 0.000205 | \n",
" 19474 | \n",
"
\n",
" \n",
" | 355 | \n",
" 566 | \n",
" Other | \n",
" 13053 | \n",
" 0.670278 | \n",
" 19474 | \n",
"
\n",
" \n",
" | 356 | \n",
" 566 | \n",
" Tumor | \n",
" 5377 | \n",
" 0.276112 | \n",
" 19474 | \n",
"
\n",
" \n",
"
\n",
"
357 rows × 5 columns
\n",
"
"
],
"text/plain": [
" sampleid phenotype count p totalcells\n",
"0 100 CD163 7429 0.099362 74767\n",
"1 100 CD8 1181 0.015796 74767\n",
"2 100 FoxP3 2111 0.028234 74767\n",
"3 100 FoxP3CD8 23 0.000308 74767\n",
"4 100 Other 34970 0.467720 74767\n",
".. ... ... ... ... ...\n",
"352 566 CD8 142 0.007292 19474\n",
"353 566 FoxP3 560 0.028756 19474\n",
"354 566 FoxP3CD8 4 0.000205 19474\n",
"355 566 Other 13053 0.670278 19474\n",
"356 566 Tumor 5377 0.276112 19474\n",
"\n",
"[357 rows x 5 columns]"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from sqlalchemy import text\n",
"from sqlalchemy.orm import aliased\n",
"from sqlalchemy import func\n",
"\n",
"total_cells = (\n",
" db.select(\n",
" db.apmifwsi002_DR3.celltag.sampleid, func.count().label(\"count\")\n",
" ).group_by(db.apmifwsi002_DR3.celltag.sampleid).subquery()\n",
")\n",
"db.query(\n",
" db.select(\n",
" db.apmifwsi002_DR3.celltag.sampleid,\n",
" db.apmifwsi002_DR3.phenotype.phenotype,\n",
" func.count().label('count'),\n",
" \"cast(count(*) as float) / count as p\",\n",
" \"count as totalcells\",\n",
" )\n",
" .select_from(db.apmifwsi002_DR3.celltag)\n",
" .where(db.apmifwsi002_DR3.phenotype.ptype == db.apmifwsi002_DR3.celltag.ptype)\n",
" .where(db.apmifwsi002_DR3.celltag.sampleid == total_cells.c.sampleid)\n",
" .group_by(db.apmifwsi002_DR3.celltag.sampleid, db.apmifwsi002_DR3.phenotype.phenotype, total_cells.c.count)\n",
" .order_by(text(\"1,2\"))\n",
" .suffix_with(\"OPTION (MAXDOP 4)\")\n",
")"
]
},
{
"cell_type": "raw",
"metadata": {
"raw_mimetype": "text/restructuredtext",
"vscode": {
"languageId": "raw",
"raw_mimetype": "text/restructuredtext"
}
},
"source": [
"Okay, now lets start with the use case from :ref:`connecting_with_astrodb_example` and\n",
"select the top few rows (``TOP 2``) across each column (``*``) from the\n",
":ref:`celltag_schema`.\n",
"\n",
"This allows us to preview our data and saves us having to load thousands of rows of data."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" cellid | \n",
" sampleid | \n",
" hpfid | \n",
" px | \n",
" py | \n",
" ptype | \n",
" cmembrane520 | \n",
" cmembrane540 | \n",
" cnucleus570 | \n",
" ccytomembrane620 | \n",
" cmembrane650 | \n",
" cmembrane690 | \n",
" exprphenotype | \n",
" tdist | \n",
" rdist | \n",
" pos | \n",
" pdl1 | \n",
" pd1 | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" 545129201024 | \n",
" 545 | \n",
" 5451292 | \n",
" 15361.792895 | \n",
" 32405.431725 | \n",
" 3 | \n",
" 4.199317 | \n",
" 0.178405 | \n",
" 1.705375 | \n",
" 20.27975 | \n",
" 1.830076 | \n",
" 1.885648 | \n",
" 0 | \n",
" -949.8998 | \n",
" 32768 | \n",
" 0x0101000000d84e917de500ce4011d55fa15ba5df40 | \n",
" False | \n",
" False | \n",
"
\n",
" \n",
" | 1 | \n",
" 545129201025 | \n",
" 545 | \n",
" 5451292 | \n",
" 15453.792895 | \n",
" 32399.431725 | \n",
" 3 | \n",
" 3.641417 | \n",
" 0.239324 | \n",
" 1.980137 | \n",
" 26.44339 | \n",
" 2.079796 | \n",
" 2.026915 | \n",
" 0 | \n",
" -1021.0180 | \n",
" 32768 | \n",
" 0x0101000000d84e917de52ece4011d55fa1dba3df40 | \n",
" False | \n",
" False | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" cellid sampleid hpfid px py ptype \\\n",
"0 545129201024 545 5451292 15361.792895 32405.431725 3 \n",
"1 545129201025 545 5451292 15453.792895 32399.431725 3 \n",
"\n",
" cmembrane520 cmembrane540 cnucleus570 ccytomembrane620 cmembrane650 \\\n",
"0 4.199317 0.178405 1.705375 20.27975 1.830076 \n",
"1 3.641417 0.239324 1.980137 26.44339 2.079796 \n",
"\n",
" cmembrane690 exprphenotype tdist rdist \\\n",
"0 1.885648 0 -949.8998 32768 \n",
"1 2.026915 0 -1021.0180 32768 \n",
"\n",
" pos pdl1 pd1 \n",
"0 0x0101000000d84e917de500ce4011d55fa15ba5df40 False False \n",
"1 0x0101000000d84e917de52ece4011d55fa1dba3df40 False False "
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"db.query(db.select(db.apmifwsi002_DR3.celltag).limit(2))"
]
},
{
"cell_type": "raw",
"metadata": {
"raw_mimetype": "text/restructuredtext",
"vscode": {
"languageId": "raw"
}
},
"source": [
"Here, each row is a cell, and each column a feature. Notice the cell phenotype is\n",
"coded as ``ptype`` - consulting the :ref:`database_schema_doc`, we can use the\n",
":ref:`phenotype_schema` to translate between ``ptype`` and ``phenotype``. Below we print\n",
"out the :ref:`phenotype_schema` to see what it looks like."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" ptype | \n",
" phenotype | \n",
" color | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" 0 | \n",
" Other | \n",
" #888888FF | \n",
"
\n",
" \n",
" | 1 | \n",
" 1 | \n",
" CD8 | \n",
" #C9C012FF | \n",
"
\n",
" \n",
" | 2 | \n",
" 2 | \n",
" FoxP3 | \n",
" #B91919FF | \n",
"
\n",
" \n",
" | 3 | \n",
" 3 | \n",
" Tumor | \n",
" #A55A16FF | \n",
"
\n",
" \n",
" | 4 | \n",
" 4 | \n",
" CD163 | \n",
" #FF00FFFF | \n",
"
\n",
" \n",
" | 5 | \n",
" 5 | \n",
" FoxP3CD8 | \n",
" #C16C95FF | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" ptype phenotype color\n",
"0 0 Other #888888FF\n",
"1 1 CD8 #C9C012FF\n",
"2 2 FoxP3 #B91919FF\n",
"3 3 Tumor #A55A16FF\n",
"4 4 CD163 #FF00FFFF\n",
"5 5 FoxP3CD8 #C16C95FF"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"db.query(db.select(db.apmifwsi002_DR3.phenotype))"
]
},
{
"cell_type": "raw",
"metadata": {
"raw_mimetype": "text/restructuredtext",
"vscode": {
"languageId": "raw"
}
},
"source": [
"In the following query, we implicitly join the :ref:`phenotype_schema` to the\n",
":ref:`celltag_schema` using ``ptype`` as a common key. "
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" sampleid | \n",
" phenotype | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" 171 | \n",
" Other | \n",
"
\n",
" \n",
" | 1 | \n",
" 171 | \n",
" Other | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" sampleid phenotype\n",
"0 171 Other\n",
"1 171 Other"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"db.query(\n",
" db.select(db.apmifwsi002_DR3.celltag.sampleid, db.apmifwsi002_DR3.phenotype.phenotype)\n",
" .where(db.apmifwsi002_DR3.phenotype.ptype == db.apmifwsi002_DR3.celltag.ptype)\n",
" .limit(2)\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can use a the aggregate function ``COUNT()`` to count the number of rows (i.e. cells), assigned ``c``, per sampleid. \n",
"\n",
"Since we're reducing the number of rows to unique sampleids, we can look at the whole query."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" sampleid | \n",
" count | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" 543 | \n",
" 103197 | \n",
"
\n",
" \n",
" | 1 | \n",
" 150 | \n",
" 20334 | \n",
"
\n",
" \n",
" | 2 | \n",
" 121 | \n",
" 8387 | \n",
"
\n",
" \n",
" | 3 | \n",
" 197 | \n",
" 1404659 | \n",
"
\n",
" \n",
" | 4 | \n",
" 119 | \n",
" 24615 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" sampleid count\n",
"0 543 103197\n",
"1 150 20334\n",
"2 121 8387\n",
"3 197 1404659\n",
"4 119 24615"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\n",
"db.query(\n",
" db.select(db.apmifwsi002_DR3.celltag.sampleid, func.count().label('count'))\n",
" .group_by(db.apmifwsi002_DR3.celltag.sampleid)\n",
" .limit(5)\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Next, we can do this same thing, but grouping by both phenotype and sampleid. We also dictate the sorting order of rows in the table for clarity."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" sampleid | \n",
" phenotype | \n",
" count | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" 100 | \n",
" CD163 | \n",
" 7429 | \n",
"
\n",
" \n",
" | 1 | \n",
" 100 | \n",
" CD8 | \n",
" 1181 | \n",
"
\n",
" \n",
" | 2 | \n",
" 100 | \n",
" FoxP3 | \n",
" 2111 | \n",
"
\n",
" \n",
" | 3 | \n",
" 100 | \n",
" FoxP3CD8 | \n",
" 23 | \n",
"
\n",
" \n",
" | 4 | \n",
" 100 | \n",
" Other | \n",
" 34970 | \n",
"
\n",
" \n",
" | ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
"
\n",
" \n",
" | 352 | \n",
" 566 | \n",
" CD8 | \n",
" 142 | \n",
"
\n",
" \n",
" | 353 | \n",
" 566 | \n",
" FoxP3 | \n",
" 560 | \n",
"
\n",
" \n",
" | 354 | \n",
" 566 | \n",
" FoxP3CD8 | \n",
" 4 | \n",
"
\n",
" \n",
" | 355 | \n",
" 566 | \n",
" Other | \n",
" 13053 | \n",
"
\n",
" \n",
" | 356 | \n",
" 566 | \n",
" Tumor | \n",
" 5377 | \n",
"
\n",
" \n",
"
\n",
"
357 rows × 3 columns
\n",
"
"
],
"text/plain": [
" sampleid phenotype count\n",
"0 100 CD163 7429\n",
"1 100 CD8 1181\n",
"2 100 FoxP3 2111\n",
"3 100 FoxP3CD8 23\n",
"4 100 Other 34970\n",
".. ... ... ...\n",
"352 566 CD8 142\n",
"353 566 FoxP3 560\n",
"354 566 FoxP3CD8 4\n",
"355 566 Other 13053\n",
"356 566 Tumor 5377\n",
"\n",
"[357 rows x 3 columns]"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"db.query(\n",
" db.select(db.apmifwsi002_DR3.celltag.sampleid, db.apmifwsi002_DR3.phenotype.phenotype, func.count().label('count'))\n",
" .where(db.apmifwsi002_DR3.phenotype.ptype == db.apmifwsi002_DR3.celltag.ptype)\n",
" .group_by(db.apmifwsi002_DR3.celltag.sampleid, db.apmifwsi002_DR3.phenotype.phenotype)\n",
" .order_by(text(\"1,2\"))\n",
")"
]
},
{
"cell_type": "raw",
"metadata": {
"raw_mimetype": "text/restructuredtext",
"vscode": {
"languageId": "raw"
}
},
"source": [
"Typically we look at cell proportions or densities by treatment to normalize the data from different samples.\n",
"Because it is slightly simpler, we first compute the cell proportions below. A subquery is used here to add a nested\n",
"query in the ``FROM`` clause to get the total cells for each sample. "
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" sampleid | \n",
" phenotype | \n",
" count | \n",
" p | \n",
" totalcells | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" 100 | \n",
" CD163 | \n",
" 7429 | \n",
" 0.099362 | \n",
" 74767 | \n",
"
\n",
" \n",
" | 1 | \n",
" 100 | \n",
" CD8 | \n",
" 1181 | \n",
" 0.015796 | \n",
" 74767 | \n",
"
\n",
" \n",
" | 2 | \n",
" 100 | \n",
" FoxP3 | \n",
" 2111 | \n",
" 0.028234 | \n",
" 74767 | \n",
"
\n",
" \n",
" | 3 | \n",
" 100 | \n",
" FoxP3CD8 | \n",
" 23 | \n",
" 0.000308 | \n",
" 74767 | \n",
"
\n",
" \n",
" | 4 | \n",
" 100 | \n",
" Other | \n",
" 34970 | \n",
" 0.467720 | \n",
" 74767 | \n",
"
\n",
" \n",
" | ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
"
\n",
" \n",
" | 352 | \n",
" 566 | \n",
" CD8 | \n",
" 142 | \n",
" 0.007292 | \n",
" 19474 | \n",
"
\n",
" \n",
" | 353 | \n",
" 566 | \n",
" FoxP3 | \n",
" 560 | \n",
" 0.028756 | \n",
" 19474 | \n",
"
\n",
" \n",
" | 354 | \n",
" 566 | \n",
" FoxP3CD8 | \n",
" 4 | \n",
" 0.000205 | \n",
" 19474 | \n",
"
\n",
" \n",
" | 355 | \n",
" 566 | \n",
" Other | \n",
" 13053 | \n",
" 0.670278 | \n",
" 19474 | \n",
"
\n",
" \n",
" | 356 | \n",
" 566 | \n",
" Tumor | \n",
" 5377 | \n",
" 0.276112 | \n",
" 19474 | \n",
"
\n",
" \n",
"
\n",
"
357 rows × 5 columns
\n",
"
"
],
"text/plain": [
" sampleid phenotype count p totalcells\n",
"0 100 CD163 7429 0.099362 74767\n",
"1 100 CD8 1181 0.015796 74767\n",
"2 100 FoxP3 2111 0.028234 74767\n",
"3 100 FoxP3CD8 23 0.000308 74767\n",
"4 100 Other 34970 0.467720 74767\n",
".. ... ... ... ... ...\n",
"352 566 CD8 142 0.007292 19474\n",
"353 566 FoxP3 560 0.028756 19474\n",
"354 566 FoxP3CD8 4 0.000205 19474\n",
"355 566 Other 13053 0.670278 19474\n",
"356 566 Tumor 5377 0.276112 19474\n",
"\n",
"[357 rows x 5 columns]"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"total_cells = (\n",
" db.select(\n",
" db.apmifwsi002_DR3.celltag.sampleid, func.count().label(\"count\")\n",
" ).group_by(db.apmifwsi002_DR3.celltag.sampleid).subquery()\n",
")\n",
"db.query(\n",
" db.select(\n",
" db.apmifwsi002_DR3.celltag.sampleid,\n",
" db.apmifwsi002_DR3.phenotype.phenotype,\n",
" func.count().label('count'),\n",
" \"cast(count(*) as float) / count as p\",\n",
" \"count as totalcells\",\n",
" )\n",
" .select_from(db.apmifwsi002_DR3.celltag)\n",
" .where(db.apmifwsi002_DR3.phenotype.ptype == db.apmifwsi002_DR3.celltag.ptype)\n",
" .where(db.apmifwsi002_DR3.celltag.sampleid == total_cells.c.sampleid)\n",
" .group_by(db.apmifwsi002_DR3.celltag.sampleid, db.apmifwsi002_DR3.phenotype.phenotype, total_cells.c.count)\n",
" .order_by(text(\"1,2\"))\n",
" .suffix_with(\"OPTION (MAXDOP 4)\")\n",
")"
]
},
{
"cell_type": "raw",
"metadata": {
"raw_mimetype": "text/restructuredtext",
"vscode": {
"languageId": "raw"
}
},
"source": [
"Querying the Clinical Data using ``SQLAlchemy``\n",
"================================================\n",
"\n",
"Next, we can extend this query to include the treatment of each sample. First, we take a look at the\n",
":ref:`clinical_schema`."
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" astroid | \n",
" sampleid | \n",
" pre- vs. on-treatment | \n",
" treatment | \n",
" rfs (months) | \n",
" recurrence | \n",
" os (months) | \n",
" death | \n",
" residual viable tumor (%) | \n",
" age | \n",
" sex | \n",
" pd-l1 tps | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" P40001_D01_C02_S02_B02_L02 | \n",
" 101 | \n",
" On-treatment | \n",
" Anti-PD-1 | \n",
" 83 | \n",
" Yes | \n",
" 83 | \n",
" No | \n",
" 40.0 | \n",
" 67 | \n",
" Male | \n",
" | \n",
"
\n",
" \n",
" | 1 | \n",
" P40001_D01_C03_S01_B01_L01 | \n",
" 100 | \n",
" Pre-treatment | \n",
" Anti-PD-1 | \n",
" 83 | \n",
" Yes | \n",
" 83 | \n",
" No | \n",
" NaN | \n",
" 67 | \n",
" Male | \n",
" 1-49% | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" astroid sampleid pre- vs. on-treatment treatment \\\n",
"0 P40001_D01_C02_S02_B02_L02 101 On-treatment Anti-PD-1 \n",
"1 P40001_D01_C03_S01_B01_L01 100 Pre-treatment Anti-PD-1 \n",
"\n",
" rfs (months) recurrence os (months) death residual viable tumor (%) age \\\n",
"0 83 Yes 83 No 40.0 67 \n",
"1 83 Yes 83 No NaN 67 \n",
"\n",
" sex pd-l1 tps \n",
"0 Male \n",
"1 Male 1-49% "
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"db.query(db.select(db.apmifwsi002_DR3.clinical).limit(2))"
]
},
{
"cell_type": "raw",
"metadata": {
"raw_mimetype": "text/restructuredtext",
"vscode": {
"languageId": "raw"
}
},
"source": [
"Conveniently, the :ref:`clinical_schema` has a ``sampleid`` column for this database\n",
"``context``, so we can join the treatments to our cell proportions on ``sampleid`` without\n",
"going through the :ref:`samples_schema`. We also subselect the treatment."
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" sampleid | \n",
" treatment | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" 101 | \n",
" Anti-PD-1 | \n",
"
\n",
" \n",
" | 1 | \n",
" 100 | \n",
" Anti-PD-1 | \n",
"
\n",
" \n",
" | 2 | \n",
" 105 | \n",
" Anti-PD-1 | \n",
"
\n",
" \n",
" | 3 | \n",
" 107 | \n",
" Anti-PD-1 | \n",
"
\n",
" \n",
" | 4 | \n",
" 108 | \n",
" Anti-PD-1 | \n",
"
\n",
" \n",
" | 5 | \n",
" 109 | \n",
" Anti-PD-1 | \n",
"
\n",
" \n",
" | 6 | \n",
" 110 | \n",
" Anti-PD-1 | \n",
"
\n",
" \n",
" | 7 | \n",
" 535 | \n",
" Anti-PD-1 | \n",
"
\n",
" \n",
" | 8 | \n",
" 112 | \n",
" Anti-PD-1 | \n",
"
\n",
" \n",
" | 9 | \n",
" 114 | \n",
" Anti-PD-1 | \n",
"
\n",
" \n",
" | 10 | \n",
" 115 | \n",
" Anti-PD-1 | \n",
"
\n",
" \n",
" | 11 | \n",
" 116 | \n",
" Anti-PD-1 | \n",
"
\n",
" \n",
" | 12 | \n",
" 119 | \n",
" Anti-PD-1 | \n",
"
\n",
" \n",
" | 13 | \n",
" 536 | \n",
" Anti-PD-1 | \n",
"
\n",
" \n",
" | 14 | \n",
" 117 | \n",
" Anti-PD-1 | \n",
"
\n",
" \n",
" | 15 | \n",
" 118 | \n",
" Anti-PD-1 | \n",
"
\n",
" \n",
" | 16 | \n",
" 121 | \n",
" Anti-PD-1 | \n",
"
\n",
" \n",
" | 17 | \n",
" 537 | \n",
" Anti-PD-1 | \n",
"
\n",
" \n",
" | 18 | \n",
" 123 | \n",
" Anti-PD-1 | \n",
"
\n",
" \n",
" | 19 | \n",
" 125 | \n",
" Anti-PD-1 | \n",
"
\n",
" \n",
" | 20 | \n",
" 128 | \n",
" Anti-PD-1+CTLA-4 | \n",
"
\n",
" \n",
" | 21 | \n",
" 129 | \n",
" Anti-PD-1+CTLA-4 | \n",
"
\n",
" \n",
" | 22 | \n",
" 131 | \n",
" Anti-PD-1 | \n",
"
\n",
" \n",
" | 23 | \n",
" 132 | \n",
" Anti-PD-1 | \n",
"
\n",
" \n",
" | 24 | \n",
" 135 | \n",
" Anti-PD-1 | \n",
"
\n",
" \n",
" | 25 | \n",
" 134 | \n",
" Anti-PD-1 | \n",
"
\n",
" \n",
" | 26 | \n",
" 137 | \n",
" Anti-PD-1 | \n",
"
\n",
" \n",
" | 27 | \n",
" 138 | \n",
" Anti-PD-1 | \n",
"
\n",
" \n",
" | 28 | \n",
" 139 | \n",
" Anti-PD-1 | \n",
"
\n",
" \n",
" | 29 | \n",
" 140 | \n",
" Anti-PD-1 | \n",
"
\n",
" \n",
" | 30 | \n",
" 540 | \n",
" Anti-PD-1 | \n",
"
\n",
" \n",
" | 31 | \n",
" 144 | \n",
" Anti-PD-1 | \n",
"
\n",
" \n",
" | 32 | \n",
" 145 | \n",
" Anti-PD-1 | \n",
"
\n",
" \n",
" | 33 | \n",
" 541 | \n",
" Anti-PD-1 | \n",
"
\n",
" \n",
" | 34 | \n",
" 150 | \n",
" Anti-PD-1+CTLA-4 | \n",
"
\n",
" \n",
" | 35 | \n",
" 149 | \n",
" Anti-PD-1+CTLA-4 | \n",
"
\n",
" \n",
" | 36 | \n",
" 167 | \n",
" Anti-PD-1+CTLA-4 | \n",
"
\n",
" \n",
" | 37 | \n",
" 165 | \n",
" Anti-PD-1+CTLA-4 | \n",
"
\n",
" \n",
" | 38 | \n",
" 170 | \n",
" Anti-PD-1+CTLA-4 | \n",
"
\n",
" \n",
" | 39 | \n",
" 169 | \n",
" Anti-PD-1+CTLA-4 | \n",
"
\n",
" \n",
" | 40 | \n",
" 172 | \n",
" Anti-PD-1+CTLA-4 | \n",
"
\n",
" \n",
" | 41 | \n",
" 171 | \n",
" Anti-PD-1+CTLA-4 | \n",
"
\n",
" \n",
" | 42 | \n",
" 185 | \n",
" Anti-PD-1 | \n",
"
\n",
" \n",
" | 43 | \n",
" 186 | \n",
" Anti-PD-1+CTLA-4 | \n",
"
\n",
" \n",
" | 44 | \n",
" 187 | \n",
" Anti-PD-1 | \n",
"
\n",
" \n",
" | 45 | \n",
" 188 | \n",
" Anti-PD-1 | \n",
"
\n",
" \n",
" | 46 | \n",
" 194 | \n",
" Anti-PD-1 | \n",
"
\n",
" \n",
" | 47 | \n",
" 195 | \n",
" Anti-PD-1 | \n",
"
\n",
" \n",
" | 48 | \n",
" 196 | \n",
" Anti-PD-1 | \n",
"
\n",
" \n",
" | 49 | \n",
" 197 | \n",
" Anti-PD-1+CTLA-4 | \n",
"
\n",
" \n",
" | 50 | \n",
" 543 | \n",
" Anti-PD-1+CTLA-4 | \n",
"
\n",
" \n",
" | 51 | \n",
" 521 | \n",
" Anti-PD-1+CTLA-4 | \n",
"
\n",
" \n",
" | 52 | \n",
" 523 | \n",
" Anti-PD-1 | \n",
"
\n",
" \n",
" | 53 | \n",
" 544 | \n",
" Anti-PD-1 | \n",
"
\n",
" \n",
" | 54 | \n",
" 545 | \n",
" Anti-PD-1 | \n",
"
\n",
" \n",
" | 55 | \n",
" 546 | \n",
" Anti-PD-1 | \n",
"
\n",
" \n",
" | 56 | \n",
" 547 | \n",
" Anti-PD-1 | \n",
"
\n",
" \n",
" | 57 | \n",
" 548 | \n",
" Anti-PD-1 | \n",
"
\n",
" \n",
" | 58 | \n",
" 561 | \n",
" Anti-PD-1 | \n",
"
\n",
" \n",
" | 59 | \n",
" 566 | \n",
" Anti-PD-1 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" sampleid treatment\n",
"0 101 Anti-PD-1\n",
"1 100 Anti-PD-1\n",
"2 105 Anti-PD-1\n",
"3 107 Anti-PD-1\n",
"4 108 Anti-PD-1\n",
"5 109 Anti-PD-1\n",
"6 110 Anti-PD-1\n",
"7 535 Anti-PD-1\n",
"8 112 Anti-PD-1\n",
"9 114 Anti-PD-1\n",
"10 115 Anti-PD-1\n",
"11 116 Anti-PD-1\n",
"12 119 Anti-PD-1\n",
"13 536 Anti-PD-1\n",
"14 117 Anti-PD-1\n",
"15 118 Anti-PD-1\n",
"16 121 Anti-PD-1\n",
"17 537 Anti-PD-1\n",
"18 123 Anti-PD-1\n",
"19 125 Anti-PD-1\n",
"20 128 Anti-PD-1+CTLA-4\n",
"21 129 Anti-PD-1+CTLA-4\n",
"22 131 Anti-PD-1\n",
"23 132 Anti-PD-1\n",
"24 135 Anti-PD-1\n",
"25 134 Anti-PD-1\n",
"26 137 Anti-PD-1\n",
"27 138 Anti-PD-1\n",
"28 139 Anti-PD-1\n",
"29 140 Anti-PD-1\n",
"30 540 Anti-PD-1\n",
"31 144 Anti-PD-1\n",
"32 145 Anti-PD-1\n",
"33 541 Anti-PD-1\n",
"34 150 Anti-PD-1+CTLA-4\n",
"35 149 Anti-PD-1+CTLA-4\n",
"36 167 Anti-PD-1+CTLA-4\n",
"37 165 Anti-PD-1+CTLA-4\n",
"38 170 Anti-PD-1+CTLA-4\n",
"39 169 Anti-PD-1+CTLA-4\n",
"40 172 Anti-PD-1+CTLA-4\n",
"41 171 Anti-PD-1+CTLA-4\n",
"42 185 Anti-PD-1\n",
"43 186 Anti-PD-1+CTLA-4\n",
"44 187 Anti-PD-1\n",
"45 188 Anti-PD-1\n",
"46 194 Anti-PD-1\n",
"47 195 Anti-PD-1\n",
"48 196 Anti-PD-1\n",
"49 197 Anti-PD-1+CTLA-4\n",
"50 543 Anti-PD-1+CTLA-4\n",
"51 521 Anti-PD-1+CTLA-4\n",
"52 523 Anti-PD-1\n",
"53 544 Anti-PD-1\n",
"54 545 Anti-PD-1\n",
"55 546 Anti-PD-1\n",
"56 547 Anti-PD-1\n",
"57 548 Anti-PD-1\n",
"58 561 Anti-PD-1\n",
"59 566 Anti-PD-1"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"db.query(\n",
" db.select(db.apmifwsi002_DR3.clinical.sampleid, db.apmifwsi002_DR3.clinical.treatment)\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Putting this altogether, and passing to variable `sample_proportions`, we get the following:"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" sampleid | \n",
" phenotype | \n",
" treatment | \n",
" count | \n",
" p | \n",
" totalcells | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" 100 | \n",
" CD8 | \n",
" Anti-PD-1 | \n",
" 1181 | \n",
" 0.015796 | \n",
" 74767 | \n",
"
\n",
" \n",
" | 1 | \n",
" 101 | \n",
" CD8 | \n",
" Anti-PD-1 | \n",
" 218194 | \n",
" 0.148282 | \n",
" 1471477 | \n",
"
\n",
" \n",
" | 2 | \n",
" 105 | \n",
" CD8 | \n",
" Anti-PD-1 | \n",
" 2101 | \n",
" 0.053713 | \n",
" 39115 | \n",
"
\n",
" \n",
" | 3 | \n",
" 107 | \n",
" CD8 | \n",
" Anti-PD-1 | \n",
" 303536 | \n",
" 0.132878 | \n",
" 2284319 | \n",
"
\n",
" \n",
" | 4 | \n",
" 108 | \n",
" CD8 | \n",
" Anti-PD-1 | \n",
" 2050 | \n",
" 0.011720 | \n",
" 174918 | \n",
"
\n",
" \n",
" | 5 | \n",
" 109 | \n",
" CD8 | \n",
" Anti-PD-1 | \n",
" 61790 | \n",
" 0.056442 | \n",
" 1094759 | \n",
"
\n",
" \n",
" | 6 | \n",
" 110 | \n",
" CD8 | \n",
" Anti-PD-1 | \n",
" 3176 | \n",
" 0.099337 | \n",
" 31972 | \n",
"
\n",
" \n",
" | 7 | \n",
" 112 | \n",
" CD8 | \n",
" Anti-PD-1 | \n",
" 91962 | \n",
" 0.055590 | \n",
" 1654297 | \n",
"
\n",
" \n",
" | 8 | \n",
" 114 | \n",
" CD8 | \n",
" Anti-PD-1 | \n",
" 237960 | \n",
" 0.205539 | \n",
" 1157735 | \n",
"
\n",
" \n",
" | 9 | \n",
" 115 | \n",
" CD8 | \n",
" Anti-PD-1 | \n",
" 270 | \n",
" 0.022868 | \n",
" 11807 | \n",
"
\n",
" \n",
" | 10 | \n",
" 116 | \n",
" CD8 | \n",
" Anti-PD-1 | \n",
" 54848 | \n",
" 0.048393 | \n",
" 1133377 | \n",
"
\n",
" \n",
" | 11 | \n",
" 117 | \n",
" CD8 | \n",
" Anti-PD-1 | \n",
" 1196 | \n",
" 0.016788 | \n",
" 71240 | \n",
"
\n",
" \n",
" | 12 | \n",
" 118 | \n",
" CD8 | \n",
" Anti-PD-1 | \n",
" 177426 | \n",
" 0.122418 | \n",
" 1449342 | \n",
"
\n",
" \n",
" | 13 | \n",
" 119 | \n",
" CD8 | \n",
" Anti-PD-1 | \n",
" 698 | \n",
" 0.028357 | \n",
" 24615 | \n",
"
\n",
" \n",
" | 14 | \n",
" 121 | \n",
" CD8 | \n",
" Anti-PD-1 | \n",
" 235 | \n",
" 0.028020 | \n",
" 8387 | \n",
"
\n",
" \n",
" | 15 | \n",
" 123 | \n",
" CD8 | \n",
" Anti-PD-1 | \n",
" 450 | \n",
" 0.025615 | \n",
" 17568 | \n",
"
\n",
" \n",
" | 16 | \n",
" 125 | \n",
" CD8 | \n",
" Anti-PD-1 | \n",
" 124861 | \n",
" 0.079572 | \n",
" 1569148 | \n",
"
\n",
" \n",
" | 17 | \n",
" 128 | \n",
" CD8 | \n",
" Anti-PD-1+CTLA-4 | \n",
" 178131 | \n",
" 0.084273 | \n",
" 2113728 | \n",
"
\n",
" \n",
" | 18 | \n",
" 129 | \n",
" CD8 | \n",
" Anti-PD-1+CTLA-4 | \n",
" 39760 | \n",
" 0.050536 | \n",
" 786765 | \n",
"
\n",
" \n",
" | 19 | \n",
" 131 | \n",
" CD8 | \n",
" Anti-PD-1 | \n",
" 1866 | \n",
" 0.051250 | \n",
" 36410 | \n",
"
\n",
" \n",
" | 20 | \n",
" 132 | \n",
" CD8 | \n",
" Anti-PD-1 | \n",
" 21038 | \n",
" 0.069363 | \n",
" 303302 | \n",
"
\n",
" \n",
" | 21 | \n",
" 134 | \n",
" CD8 | \n",
" Anti-PD-1 | \n",
" 355 | \n",
" 0.016066 | \n",
" 22097 | \n",
"
\n",
" \n",
" | 22 | \n",
" 135 | \n",
" CD8 | \n",
" Anti-PD-1 | \n",
" 3970 | \n",
" 0.082086 | \n",
" 48364 | \n",
"
\n",
" \n",
" | 23 | \n",
" 137 | \n",
" CD8 | \n",
" Anti-PD-1 | \n",
" 81065 | \n",
" 0.087689 | \n",
" 924457 | \n",
"
\n",
" \n",
" | 24 | \n",
" 138 | \n",
" CD8 | \n",
" Anti-PD-1 | \n",
" 33813 | \n",
" 0.038954 | \n",
" 868023 | \n",
"
\n",
" \n",
" | 25 | \n",
" 139 | \n",
" CD8 | \n",
" Anti-PD-1 | \n",
" 1739 | \n",
" 0.022796 | \n",
" 76285 | \n",
"
\n",
" \n",
" | 26 | \n",
" 140 | \n",
" CD8 | \n",
" Anti-PD-1 | \n",
" 44560 | \n",
" 0.047760 | \n",
" 933002 | \n",
"
\n",
" \n",
" | 27 | \n",
" 144 | \n",
" CD8 | \n",
" Anti-PD-1 | \n",
" 74617 | \n",
" 0.073980 | \n",
" 1008612 | \n",
"
\n",
" \n",
" | 28 | \n",
" 145 | \n",
" CD8 | \n",
" Anti-PD-1 | \n",
" 306 | \n",
" 0.040854 | \n",
" 7490 | \n",
"
\n",
" \n",
" | 29 | \n",
" 149 | \n",
" CD8 | \n",
" Anti-PD-1+CTLA-4 | \n",
" 32520 | \n",
" 0.035720 | \n",
" 910420 | \n",
"
\n",
" \n",
" | 30 | \n",
" 150 | \n",
" CD8 | \n",
" Anti-PD-1+CTLA-4 | \n",
" 252 | \n",
" 0.012393 | \n",
" 20334 | \n",
"
\n",
" \n",
" | 31 | \n",
" 165 | \n",
" CD8 | \n",
" Anti-PD-1+CTLA-4 | \n",
" 195373 | \n",
" 0.100403 | \n",
" 1945897 | \n",
"
\n",
" \n",
" | 32 | \n",
" 167 | \n",
" CD8 | \n",
" Anti-PD-1+CTLA-4 | \n",
" 259 | \n",
" 0.012249 | \n",
" 21144 | \n",
"
\n",
" \n",
" | 33 | \n",
" 169 | \n",
" CD8 | \n",
" Anti-PD-1+CTLA-4 | \n",
" 234442 | \n",
" 0.092473 | \n",
" 2535257 | \n",
"
\n",
" \n",
" | 34 | \n",
" 170 | \n",
" CD8 | \n",
" Anti-PD-1+CTLA-4 | \n",
" 473 | \n",
" 0.030485 | \n",
" 15516 | \n",
"
\n",
" \n",
" | 35 | \n",
" 171 | \n",
" CD8 | \n",
" Anti-PD-1+CTLA-4 | \n",
" 64387 | \n",
" 0.094772 | \n",
" 679392 | \n",
"
\n",
" \n",
" | 36 | \n",
" 172 | \n",
" CD8 | \n",
" Anti-PD-1+CTLA-4 | \n",
" 85 | \n",
" 0.007772 | \n",
" 10937 | \n",
"
\n",
" \n",
" | 37 | \n",
" 185 | \n",
" CD8 | \n",
" Anti-PD-1 | \n",
" 342896 | \n",
" 0.223246 | \n",
" 1535958 | \n",
"
\n",
" \n",
" | 38 | \n",
" 186 | \n",
" CD8 | \n",
" Anti-PD-1+CTLA-4 | \n",
" 1701 | \n",
" 0.003207 | \n",
" 530408 | \n",
"
\n",
" \n",
" | 39 | \n",
" 187 | \n",
" CD8 | \n",
" Anti-PD-1 | \n",
" 770 | \n",
" 0.016790 | \n",
" 45861 | \n",
"
\n",
" \n",
" | 40 | \n",
" 188 | \n",
" CD8 | \n",
" Anti-PD-1 | \n",
" 50217 | \n",
" 0.070528 | \n",
" 712017 | \n",
"
\n",
" \n",
" | 41 | \n",
" 194 | \n",
" CD8 | \n",
" Anti-PD-1 | \n",
" 73869 | \n",
" 0.102192 | \n",
" 722846 | \n",
"
\n",
" \n",
" | 42 | \n",
" 195 | \n",
" CD8 | \n",
" Anti-PD-1 | \n",
" 1742 | \n",
" 0.021162 | \n",
" 82318 | \n",
"
\n",
" \n",
" | 43 | \n",
" 196 | \n",
" CD8 | \n",
" Anti-PD-1 | \n",
" 124019 | \n",
" 0.108076 | \n",
" 1147519 | \n",
"
\n",
" \n",
" | 44 | \n",
" 197 | \n",
" CD8 | \n",
" Anti-PD-1+CTLA-4 | \n",
" 49477 | \n",
" 0.035223 | \n",
" 1404659 | \n",
"
\n",
" \n",
" | 45 | \n",
" 521 | \n",
" CD8 | \n",
" Anti-PD-1+CTLA-4 | \n",
" 62 | \n",
" 0.006754 | \n",
" 9180 | \n",
"
\n",
" \n",
" | 46 | \n",
" 523 | \n",
" CD8 | \n",
" Anti-PD-1 | \n",
" 26572 | \n",
" 0.029627 | \n",
" 896873 | \n",
"
\n",
" \n",
" | 47 | \n",
" 535 | \n",
" CD8 | \n",
" Anti-PD-1 | \n",
" 99546 | \n",
" 0.153670 | \n",
" 647792 | \n",
"
\n",
" \n",
" | 48 | \n",
" 536 | \n",
" CD8 | \n",
" Anti-PD-1 | \n",
" 31819 | \n",
" 0.099700 | \n",
" 319148 | \n",
"
\n",
" \n",
" | 49 | \n",
" 537 | \n",
" CD8 | \n",
" Anti-PD-1 | \n",
" 3620 | \n",
" 0.107584 | \n",
" 33648 | \n",
"
\n",
" \n",
" | 50 | \n",
" 540 | \n",
" CD8 | \n",
" Anti-PD-1 | \n",
" 1301 | \n",
" 0.032391 | \n",
" 40166 | \n",
"
\n",
" \n",
" | 51 | \n",
" 541 | \n",
" CD8 | \n",
" Anti-PD-1 | \n",
" 33406 | \n",
" 0.053569 | \n",
" 623603 | \n",
"
\n",
" \n",
" | 52 | \n",
" 543 | \n",
" CD8 | \n",
" Anti-PD-1+CTLA-4 | \n",
" 12335 | \n",
" 0.119529 | \n",
" 103197 | \n",
"
\n",
" \n",
" | 53 | \n",
" 544 | \n",
" CD8 | \n",
" Anti-PD-1 | \n",
" 9 | \n",
" 0.001508 | \n",
" 5970 | \n",
"
\n",
" \n",
" | 54 | \n",
" 545 | \n",
" CD8 | \n",
" Anti-PD-1 | \n",
" 117118 | \n",
" 0.072770 | \n",
" 1609429 | \n",
"
\n",
" \n",
" | 55 | \n",
" 546 | \n",
" CD8 | \n",
" Anti-PD-1 | \n",
" 98214 | \n",
" 0.072633 | \n",
" 1352191 | \n",
"
\n",
" \n",
" | 56 | \n",
" 547 | \n",
" CD8 | \n",
" Anti-PD-1 | \n",
" 489 | \n",
" 0.059817 | \n",
" 8175 | \n",
"
\n",
" \n",
" | 57 | \n",
" 548 | \n",
" CD8 | \n",
" Anti-PD-1 | \n",
" 214780 | \n",
" 0.133346 | \n",
" 1610702 | \n",
"
\n",
" \n",
" | 58 | \n",
" 561 | \n",
" CD8 | \n",
" Anti-PD-1 | \n",
" 100512 | \n",
" 0.072024 | \n",
" 1395540 | \n",
"
\n",
" \n",
" | 59 | \n",
" 566 | \n",
" CD8 | \n",
" Anti-PD-1 | \n",
" 142 | \n",
" 0.007292 | \n",
" 19474 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" sampleid phenotype treatment count p totalcells\n",
"0 100 CD8 Anti-PD-1 1181 0.015796 74767\n",
"1 101 CD8 Anti-PD-1 218194 0.148282 1471477\n",
"2 105 CD8 Anti-PD-1 2101 0.053713 39115\n",
"3 107 CD8 Anti-PD-1 303536 0.132878 2284319\n",
"4 108 CD8 Anti-PD-1 2050 0.011720 174918\n",
"5 109 CD8 Anti-PD-1 61790 0.056442 1094759\n",
"6 110 CD8 Anti-PD-1 3176 0.099337 31972\n",
"7 112 CD8 Anti-PD-1 91962 0.055590 1654297\n",
"8 114 CD8 Anti-PD-1 237960 0.205539 1157735\n",
"9 115 CD8 Anti-PD-1 270 0.022868 11807\n",
"10 116 CD8 Anti-PD-1 54848 0.048393 1133377\n",
"11 117 CD8 Anti-PD-1 1196 0.016788 71240\n",
"12 118 CD8 Anti-PD-1 177426 0.122418 1449342\n",
"13 119 CD8 Anti-PD-1 698 0.028357 24615\n",
"14 121 CD8 Anti-PD-1 235 0.028020 8387\n",
"15 123 CD8 Anti-PD-1 450 0.025615 17568\n",
"16 125 CD8 Anti-PD-1 124861 0.079572 1569148\n",
"17 128 CD8 Anti-PD-1+CTLA-4 178131 0.084273 2113728\n",
"18 129 CD8 Anti-PD-1+CTLA-4 39760 0.050536 786765\n",
"19 131 CD8 Anti-PD-1 1866 0.051250 36410\n",
"20 132 CD8 Anti-PD-1 21038 0.069363 303302\n",
"21 134 CD8 Anti-PD-1 355 0.016066 22097\n",
"22 135 CD8 Anti-PD-1 3970 0.082086 48364\n",
"23 137 CD8 Anti-PD-1 81065 0.087689 924457\n",
"24 138 CD8 Anti-PD-1 33813 0.038954 868023\n",
"25 139 CD8 Anti-PD-1 1739 0.022796 76285\n",
"26 140 CD8 Anti-PD-1 44560 0.047760 933002\n",
"27 144 CD8 Anti-PD-1 74617 0.073980 1008612\n",
"28 145 CD8 Anti-PD-1 306 0.040854 7490\n",
"29 149 CD8 Anti-PD-1+CTLA-4 32520 0.035720 910420\n",
"30 150 CD8 Anti-PD-1+CTLA-4 252 0.012393 20334\n",
"31 165 CD8 Anti-PD-1+CTLA-4 195373 0.100403 1945897\n",
"32 167 CD8 Anti-PD-1+CTLA-4 259 0.012249 21144\n",
"33 169 CD8 Anti-PD-1+CTLA-4 234442 0.092473 2535257\n",
"34 170 CD8 Anti-PD-1+CTLA-4 473 0.030485 15516\n",
"35 171 CD8 Anti-PD-1+CTLA-4 64387 0.094772 679392\n",
"36 172 CD8 Anti-PD-1+CTLA-4 85 0.007772 10937\n",
"37 185 CD8 Anti-PD-1 342896 0.223246 1535958\n",
"38 186 CD8 Anti-PD-1+CTLA-4 1701 0.003207 530408\n",
"39 187 CD8 Anti-PD-1 770 0.016790 45861\n",
"40 188 CD8 Anti-PD-1 50217 0.070528 712017\n",
"41 194 CD8 Anti-PD-1 73869 0.102192 722846\n",
"42 195 CD8 Anti-PD-1 1742 0.021162 82318\n",
"43 196 CD8 Anti-PD-1 124019 0.108076 1147519\n",
"44 197 CD8 Anti-PD-1+CTLA-4 49477 0.035223 1404659\n",
"45 521 CD8 Anti-PD-1+CTLA-4 62 0.006754 9180\n",
"46 523 CD8 Anti-PD-1 26572 0.029627 896873\n",
"47 535 CD8 Anti-PD-1 99546 0.153670 647792\n",
"48 536 CD8 Anti-PD-1 31819 0.099700 319148\n",
"49 537 CD8 Anti-PD-1 3620 0.107584 33648\n",
"50 540 CD8 Anti-PD-1 1301 0.032391 40166\n",
"51 541 CD8 Anti-PD-1 33406 0.053569 623603\n",
"52 543 CD8 Anti-PD-1+CTLA-4 12335 0.119529 103197\n",
"53 544 CD8 Anti-PD-1 9 0.001508 5970\n",
"54 545 CD8 Anti-PD-1 117118 0.072770 1609429\n",
"55 546 CD8 Anti-PD-1 98214 0.072633 1352191\n",
"56 547 CD8 Anti-PD-1 489 0.059817 8175\n",
"57 548 CD8 Anti-PD-1 214780 0.133346 1610702\n",
"58 561 CD8 Anti-PD-1 100512 0.072024 1395540\n",
"59 566 CD8 Anti-PD-1 142 0.007292 19474"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"total_cells = (\n",
" db.select(\n",
" db.apmifwsi002_DR3.celltag.sampleid, func.count().label(\"totalcells\")\n",
" ).group_by(db.apmifwsi002_DR3.celltag.sampleid).subquery()\n",
")\n",
"sample_proportions = db.query(\n",
" db.select(\n",
" db.apmifwsi002_DR3.celltag.sampleid,\n",
" db.apmifwsi002_DR3.phenotype.phenotype,\n",
" db.apmifwsi002_DR3.clinical.treatment,\n",
" func.count().label('count'),\n",
" \"cast(count(*) as float) / totalcells as p\",\n",
" \"totalcells\",\n",
" )\n",
" .select_from(db.apmifwsi002_DR3.celltag)\n",
" .where(db.apmifwsi002_DR3.phenotype.ptype == db.apmifwsi002_DR3.celltag.ptype)\n",
" .where(db.apmifwsi002_DR3.celltag.sampleid == total_cells.c.sampleid)\n",
" .where(db.apmifwsi002_DR3.celltag.sampleid == db.apmifwsi002_DR3.clinical.sampleid)\n",
" .group_by(\n",
" db.apmifwsi002_DR3.celltag.sampleid,\n",
" db.apmifwsi002_DR3.phenotype.phenotype,\n",
" total_cells.c.totalcells,\n",
" db.apmifwsi002_DR3.clinical.treatment\n",
" )\n",
" .order_by(text(\"1,2\"))\n",
" .ptype_filter(1, table_name=\"celltag\")\n",
")\n",
"sample_proportions"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Congratulations, Data Scientist! Your next step will be to analyze this queried data."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "astropath_test",
"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.10.11"
}
},
"nbformat": 4,
"nbformat_minor": 2
}