{ "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
sampleidphenotypecountptotalcells
0100CD16374290.09936274767
1100CD811810.01579674767
2100FoxP321110.02823474767
3100FoxP3CD8230.00030874767
4100Other349700.46772074767
..................
352566CD81420.00729219474
353566FoxP35600.02875619474
354566FoxP3CD840.00020519474
355566Other130530.67027819474
356566Tumor53770.27611219474
\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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
cellidsampleidhpfidpxpyptypecmembrane520cmembrane540cnucleus570ccytomembrane620cmembrane650cmembrane690exprphenotypetdistrdistpospdl1pd1
0545129201024545545129215361.79289532405.43172534.1993170.1784051.70537520.279751.8300761.8856480-949.8998327680x0101000000d84e917de500ce4011d55fa15ba5df40FalseFalse
1545129201025545545129215453.79289532399.43172533.6414170.2393241.98013726.443392.0797962.0269150-1021.0180327680x0101000000d84e917de52ece4011d55fa1dba3df40FalseFalse
\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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
ptypephenotypecolor
00Other#888888FF
11CD8#C9C012FF
22FoxP3#B91919FF
33Tumor#A55A16FF
44CD163#FF00FFFF
55FoxP3CD8#C16C95FF
\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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
sampleidphenotype
0171Other
1171Other
\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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
sampleidcount
0543103197
115020334
21218387
31971404659
411924615
\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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
sampleidphenotypecount
0100CD1637429
1100CD81181
2100FoxP32111
3100FoxP3CD823
4100Other34970
............
352566CD8142
353566FoxP3560
354566FoxP3CD84
355566Other13053
356566Tumor5377
\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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
sampleidphenotypecountptotalcells
0100CD16374290.09936274767
1100CD811810.01579674767
2100FoxP321110.02823474767
3100FoxP3CD8230.00030874767
4100Other349700.46772074767
..................
352566CD81420.00729219474
353566FoxP35600.02875619474
354566FoxP3CD840.00020519474
355566Other130530.67027819474
356566Tumor53770.27611219474
\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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
astroidsampleidpre- vs. on-treatmenttreatmentrfs (months)recurrenceos (months)deathresidual viable tumor (%)agesexpd-l1 tps
0P40001_D01_C02_S02_B02_L02101On-treatmentAnti-PD-183Yes83No40.067Male
1P40001_D01_C03_S01_B01_L01100Pre-treatmentAnti-PD-183Yes83NoNaN67Male1-49%
\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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
sampleidtreatment
0101Anti-PD-1
1100Anti-PD-1
2105Anti-PD-1
3107Anti-PD-1
4108Anti-PD-1
5109Anti-PD-1
6110Anti-PD-1
7535Anti-PD-1
8112Anti-PD-1
9114Anti-PD-1
10115Anti-PD-1
11116Anti-PD-1
12119Anti-PD-1
13536Anti-PD-1
14117Anti-PD-1
15118Anti-PD-1
16121Anti-PD-1
17537Anti-PD-1
18123Anti-PD-1
19125Anti-PD-1
20128Anti-PD-1+CTLA-4
21129Anti-PD-1+CTLA-4
22131Anti-PD-1
23132Anti-PD-1
24135Anti-PD-1
25134Anti-PD-1
26137Anti-PD-1
27138Anti-PD-1
28139Anti-PD-1
29140Anti-PD-1
30540Anti-PD-1
31144Anti-PD-1
32145Anti-PD-1
33541Anti-PD-1
34150Anti-PD-1+CTLA-4
35149Anti-PD-1+CTLA-4
36167Anti-PD-1+CTLA-4
37165Anti-PD-1+CTLA-4
38170Anti-PD-1+CTLA-4
39169Anti-PD-1+CTLA-4
40172Anti-PD-1+CTLA-4
41171Anti-PD-1+CTLA-4
42185Anti-PD-1
43186Anti-PD-1+CTLA-4
44187Anti-PD-1
45188Anti-PD-1
46194Anti-PD-1
47195Anti-PD-1
48196Anti-PD-1
49197Anti-PD-1+CTLA-4
50543Anti-PD-1+CTLA-4
51521Anti-PD-1+CTLA-4
52523Anti-PD-1
53544Anti-PD-1
54545Anti-PD-1
55546Anti-PD-1
56547Anti-PD-1
57548Anti-PD-1
58561Anti-PD-1
59566Anti-PD-1
\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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
sampleidphenotypetreatmentcountptotalcells
0100CD8Anti-PD-111810.01579674767
1101CD8Anti-PD-12181940.1482821471477
2105CD8Anti-PD-121010.05371339115
3107CD8Anti-PD-13035360.1328782284319
4108CD8Anti-PD-120500.011720174918
5109CD8Anti-PD-1617900.0564421094759
6110CD8Anti-PD-131760.09933731972
7112CD8Anti-PD-1919620.0555901654297
8114CD8Anti-PD-12379600.2055391157735
9115CD8Anti-PD-12700.02286811807
10116CD8Anti-PD-1548480.0483931133377
11117CD8Anti-PD-111960.01678871240
12118CD8Anti-PD-11774260.1224181449342
13119CD8Anti-PD-16980.02835724615
14121CD8Anti-PD-12350.0280208387
15123CD8Anti-PD-14500.02561517568
16125CD8Anti-PD-11248610.0795721569148
17128CD8Anti-PD-1+CTLA-41781310.0842732113728
18129CD8Anti-PD-1+CTLA-4397600.050536786765
19131CD8Anti-PD-118660.05125036410
20132CD8Anti-PD-1210380.069363303302
21134CD8Anti-PD-13550.01606622097
22135CD8Anti-PD-139700.08208648364
23137CD8Anti-PD-1810650.087689924457
24138CD8Anti-PD-1338130.038954868023
25139CD8Anti-PD-117390.02279676285
26140CD8Anti-PD-1445600.047760933002
27144CD8Anti-PD-1746170.0739801008612
28145CD8Anti-PD-13060.0408547490
29149CD8Anti-PD-1+CTLA-4325200.035720910420
30150CD8Anti-PD-1+CTLA-42520.01239320334
31165CD8Anti-PD-1+CTLA-41953730.1004031945897
32167CD8Anti-PD-1+CTLA-42590.01224921144
33169CD8Anti-PD-1+CTLA-42344420.0924732535257
34170CD8Anti-PD-1+CTLA-44730.03048515516
35171CD8Anti-PD-1+CTLA-4643870.094772679392
36172CD8Anti-PD-1+CTLA-4850.00777210937
37185CD8Anti-PD-13428960.2232461535958
38186CD8Anti-PD-1+CTLA-417010.003207530408
39187CD8Anti-PD-17700.01679045861
40188CD8Anti-PD-1502170.070528712017
41194CD8Anti-PD-1738690.102192722846
42195CD8Anti-PD-117420.02116282318
43196CD8Anti-PD-11240190.1080761147519
44197CD8Anti-PD-1+CTLA-4494770.0352231404659
45521CD8Anti-PD-1+CTLA-4620.0067549180
46523CD8Anti-PD-1265720.029627896873
47535CD8Anti-PD-1995460.153670647792
48536CD8Anti-PD-1318190.099700319148
49537CD8Anti-PD-136200.10758433648
50540CD8Anti-PD-113010.03239140166
51541CD8Anti-PD-1334060.053569623603
52543CD8Anti-PD-1+CTLA-4123350.119529103197
53544CD8Anti-PD-190.0015085970
54545CD8Anti-PD-11171180.0727701609429
55546CD8Anti-PD-1982140.0726331352191
56547CD8Anti-PD-14890.0598178175
57548CD8Anti-PD-12147800.1333461610702
58561CD8Anti-PD-11005120.0720241395540
59566CD8Anti-PD-11420.00729219474
\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 }