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