Query Region Densities with Raw SQL#

In this tutorial, we show how to: - Query cell counts - Query tissue areas - Modify area of analysis - Query cell densities - Query clinical data

To demonstrate raw SQL, we will follow a common use-case scenario in which we wish to compare intratumoral and invasive margin CD8+ T cell densities 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 SQLAlchemy for a parallel tutorial using sqlalchemy.

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 in Distinct Regions#

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 CD8 cell counts separately within the tumor and at the invasive margin for each sample in the database and returns our results as a pandas.DataFrame.

[2]:
sql = f"""
SELECT ct.sampleid,
    COUNT(CASE WHEN ct.tdist <= 0 THEN 1 ELSE NULL END) CD8_T,
    COUNT(CASE WHEN (ct.tdist <= 500 and ct.tdist > -500) THEN 1 ELSE NULL END) CD8_IM
FROM dbo.celltag ct, dbo.phenotype p
WHERE ct.ptype = p.ptype
    and p.phenotype = 'CD8'
GROUP BY ct.sampleid
ORDER BY ct.sampleid
"""
db.query(sql)
[2]:
sampleid CD8_T CD8_IM
0 100 876 786
1 101 71312 61355
2 105 473 1477
3 107 16111 44915
4 108 223 1415
5 109 5459 10792
6 110 2223 1978
7 112 0 0
8 114 11923 19995
9 115 147 238
10 116 28888 18318
11 117 107 436
12 118 973 10217
13 119 696 511
14 121 3 47
15 123 387 395
16 125 61344 28695
17 128 44674 137290
18 129 0 0
19 131 1690 1651
20 132 20952 4780
21 134 37 241
22 135 3970 3678
23 137 0 0
24 138 21655 10722
25 139 1736 1560
26 140 39589 4515
27 144 11532 26466
28 145 149 212
29 149 9439 9229
30 150 106 114
31 165 108469 25492
32 167 259 250
33 169 0 0
34 170 471 465
35 171 239 5657
36 172 84 85
37 185 67228 128367
38 186 1697 640
39 187 189 208
40 188 5135 4226
41 194 9591 22573
42 195 1518 1486
43 196 8452 30000
44 197 4567 1566
45 521 62 56
46 523 4559 10623
47 535 0 0
48 536 31819 10859
49 537 3620 2674
50 540 417 475
51 541 160 7465
52 543 3255 4862
53 544 9 9
54 545 27592 40588
55 546 33990 19748
56 547 429 415
57 548 12420 9636
58 561 11058 30751
59 566 142 102

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.

Note:

Passing a docstring (“”” “””) allows us to format line breaks in the SQL string, helping keep longer, complicated queries legible. Using a formatted (f) string allows us to embed python variables in our queries. More on this later.

[3]:
# db.query("SELECT TOP 2 * FROM dbo.celltag")
# Equivalent to below

sql = f"""
SELECT TOP 2 *
FROM dbo.celltag
"""
db.query(sql)
[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 POINT (15361.792894519327 32405.431724508355) 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 POINT (15453.792894519327 32399.431724508355) 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]:
sql = f"""
SELECT *
FROM dbo.phenotype
"""
db.query(sql)
[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.

We also implcitly assign an alias, ct, to the Celltag Table table and p to the Phenotype Table for the sake of legibility. We use these aliases to select the sampleid and the phenotype of the top 2 rows.

[5]:
sql = f"""
SELECT TOP 2 ct.sampleid, p.phenotype
FROM dbo.celltag ct, dbo.phenotype p
WHERE ct.ptype = p.ptype
"""
db.query(sql)
[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 and phenotype. We also dictate the sorting order of rows in the table for clarity.

Since we’re reducing the number of rows to unique combinations of sampleid and phenotype, we can look at the whole query.

[6]:
sql = f"""
SELECT ct.sampleid, p.phenotype, COUNT(*) c
FROM dbo.celltag ct, dbo.phenotype p
WHERE ct.ptype = p.ptype
GROUP BY ct.sampleid, p.phenotype
ORDER BY ct.sampleid, p.phenotype
"""
db.query(sql)
[6]:
sampleid phenotype c
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

We can add an additional where clauses to query only CD8 cells and cells that are inside the tumor annotations in the database (defined as tdist <= 0). Alternatively, try querying cells within 250µm of either side of the tumor edge.

Note:

Many distance features in the databases are measured in pixels; 1µm = 2px. Consult the Constants Table for reference.

[7]:
sql = f"""
SELECT ct.sampleid, COUNT(*) CD8_T
FROM dbo.celltag ct, dbo.phenotype p
WHERE ct.ptype = p.ptype
    and p.phenotype = 'CD8'
    and ct.tdist <= 0
GROUP BY ct.sampleid, p.phenotype
ORDER BY ct.sampleid
"""
db.query(sql)
[7]:
sampleid CD8_T
0 100 876
1 101 71312
2 105 473
3 107 16111
4 108 223
5 109 5459
6 110 2223
7 114 11923
8 115 147
9 116 28888
10 117 107
11 118 973
12 119 696
13 121 3
14 123 387
15 125 61344
16 128 44674
17 131 1690
18 132 20952
19 134 37
20 135 3970
21 138 21655
22 139 1736
23 140 39589
24 144 11532
25 145 149
26 149 9439
27 150 106
28 165 108469
29 167 259
30 170 471
31 171 239
32 172 84
33 185 67228
34 186 1697
35 187 189
36 188 5135
37 194 9591
38 195 1518
39 196 8452
40 197 4567
41 521 62
42 523 4559
43 536 31819
44 537 3620
45 540 417
46 541 160
47 543 3255
48 544 9
49 545 27592
50 546 33990
51 547 429
52 548 12420
53 561 11058
54 566 142

We can step up our SQL syntax by combining the COUNT() function with CASE and WHEN clauses to evaluate different criteria in the same query, et voila: CD8 counts in the tumor and at the invasive margin for each sample. Look familiar?

[8]:
sql = f"""
SELECT ct.sampleid,
    COUNT(CASE WHEN ct.tdist <= 0 THEN 1 ELSE NULL END) CD8_T,
    COUNT(CASE WHEN (ct.tdist <= 500 and ct.tdist > -500) THEN 1 ELSE NULL END) CD8_IM
FROM dbo.celltag ct, dbo.phenotype p
WHERE ct.ptype = p.ptype
    and p.phenotype = 'CD8'
GROUP BY ct.sampleid
ORDER BY ct.sampleid
"""
db.query(sql)
[8]:
sampleid CD8_T CD8_IM
0 100 876 786
1 101 71312 61355
2 105 473 1477
3 107 16111 44915
4 108 223 1415
5 109 5459 10792
6 110 2223 1978
7 112 0 0
8 114 11923 19995
9 115 147 238
10 116 28888 18318
11 117 107 436
12 118 973 10217
13 119 696 511
14 121 3 47
15 123 387 395
16 125 61344 28695
17 128 44674 137290
18 129 0 0
19 131 1690 1651
20 132 20952 4780
21 134 37 241
22 135 3970 3678
23 137 0 0
24 138 21655 10722
25 139 1736 1560
26 140 39589 4515
27 144 11532 26466
28 145 149 212
29 149 9439 9229
30 150 106 114
31 165 108469 25492
32 167 259 250
33 169 0 0
34 170 471 465
35 171 239 5657
36 172 84 85
37 185 67228 128367
38 186 1697 640
39 187 189 208
40 188 5135 4226
41 194 9591 22573
42 195 1518 1486
43 196 8452 30000
44 197 4567 1566
45 521 62 56
46 523 4559 10623
47 535 0 0
48 536 31819 10859
49 537 3620 2674
50 540 417 475
51 541 160 7465
52 543 3255 4862
53 544 9 9
54 545 27592 40588
55 546 33990 19748
56 547 429 415
57 548 12420 9636
58 561 11058 30751
59 566 142 102

Querying Areas#

[9]:
# Normalization Unit Ratio to convert randomcell density to area (mm2)
nur = 8000.0000 / (1.004 * 1.344)

sql = f"""
SELECT sampleid,
    COUNT(CASE WHEN tdist <= 0 THEN 1 ELSE NULL END)/{nur} area_T,
    COUNT(CASE WHEN (tdist <= 500 and tdist > -500) THEN 1 ELSE NULL END)/{nur} area_IM
FROM dbo.randomcell
GROUP BY sampleid
ORDER BY sampleid
"""
db.query(sql)
[9]:
sampleid area_T area_IM
0 100 8.126786 5.672777
1 101 42.775219 34.772239
2 105 2.373890 4.676094
3 107 15.393850 29.413529
4 108 17.282639 14.812269
5 109 69.512430 28.556507
6 110 2.521815 2.346902
7 112 0.000000 0.000000
8 114 16.116441 13.702239
9 115 1.840212 2.338637
10 116 96.025813 41.204714
11 117 5.635669 5.314349
12 118 1.644046 4.592601
13 119 8.604465 5.350107
14 121 0.013494 0.226864
15 123 1.875801 2.075846
16 125 127.244639 36.023954
17 128 95.151080 133.438949
18 129 0.000000 0.000000
19 131 5.477117 5.525695
20 132 84.116895 19.339088
21 134 1.600023 2.405094
22 135 7.874958 7.079164
23 137 0.000000 0.000000
24 138 96.579732 28.390365
25 139 13.738840 11.883280
26 140 136.225579 19.156585
27 144 45.867146 44.170811
28 145 0.468402 0.729506
29 149 37.235019 27.521367
30 150 3.317104 3.537895
31 165 126.298051 23.216351
32 167 2.710559 2.601597
33 169 0.000000 0.000000
34 170 4.758743 4.765827
35 171 2.064545 9.176938
36 172 1.955583 1.962667
37 185 72.270892 65.098284
38 186 81.228387 26.511865
39 187 3.076746 3.562859
40 188 21.148770 15.061398
41 194 3.898685 7.382436
42 195 13.798888 13.005455
43 196 23.025077 28.654674
44 197 64.675929 15.432813
45 521 2.688800 1.812212
46 523 69.309012 39.430791
47 535 0.000000 0.000000
48 536 70.210563 21.907119
49 537 3.524233 2.545766
50 540 2.442033 2.535646
51 541 5.287699 18.219612
52 543 1.692624 4.234342
53 544 0.696447 0.696447
54 545 90.112847 75.244579
55 546 74.893573 25.540652
56 547 0.917744 0.970539
57 548 36.946758 14.132352
58 561 22.175645 41.735356
59 566 2.923423 1.552626

The SQL query above fetches areas separately for tumor and the invasive margin for each sample in the database and returns our results as a Pandas DataFrame. Again, let’s start from scratch to figure out how we got here.

You can compute the areas of regions using the annotation polygons in the database, but it becomes much more efficient at scale to filter the pre-computed random points as needed and use their known density (nur: normalization unit factor) to estimate area (mm2).

Like the celltag table, each row in the randomcell table represents a point. Let’s get acquainted:

[10]:
sql = f"""
SELECT TOP 2 *
FROM dbo.randomcell
"""
db.query(sql)
[10]:
sampleid cellid hpfid px py pos tdist rdist
0 140 140061701133 1400617 12580.569217 25804.692615 POINT (12580.569216767357 25804.692615400912) -6603.201749 32768
1 140 140061701135 1400617 12788.699721 25801.826057 POINT (12788.699720789917 25801.826056849222) -6742.621185 32768

As with cell counts, we can filter random points by tdist and group by sampleid.

[11]:
# Normalization Unit Ratio to convert randomcell density to area (mm2)
nur = 8000.0000 / (1.004 * 1.344)

sql = f"""
SELECT sampleid, COUNT(*)/{nur} area_T
FROM dbo.randomcell
WHERE tdist <= 0
GROUP BY sampleid
ORDER BY sampleid
"""
db.query(sql)
[11]:
sampleid area_T
0 100 8.126786
1 101 42.775219
2 105 2.373890
3 107 15.393850
4 108 17.282639
5 109 69.512430
6 110 2.521815
7 114 16.116441
8 115 1.840212
9 116 96.025813
10 117 5.635669
11 118 1.644046
12 119 8.604465
13 121 0.013494
14 123 1.875801
15 125 127.244639
16 128 95.151080
17 131 5.477117
18 132 84.116895
19 134 1.600023
20 135 7.874958
21 138 96.579732
22 139 13.738840
23 140 136.225579
24 144 45.867146
25 145 0.468402
26 149 37.235019
27 150 3.317104
28 165 126.298051
29 167 2.710559
30 170 4.758743
31 171 2.064545
32 172 1.955583
33 185 72.270892
34 186 81.228387
35 187 3.076746
36 188 21.148770
37 194 3.898685
38 195 13.798888
39 196 23.025077
40 197 64.675929
41 521 2.688800
42 523 69.309012
43 536 70.210563
44 537 3.524233
45 540 2.442033
46 541 5.287699
47 543 1.692624
48 544 0.696447
49 545 90.112847
50 546 74.893573
51 547 0.917744
52 548 36.946758
53 561 22.175645
54 566 2.923423

Again, we can step up our SQL syntax to get the values in tumor and at the invasive margin in the same query- magnifique.

[12]:
# Normalization Unit Ratio to convert randomcell density to area (mm2)
nur = 8000.0000 / (1.004 * 1.344)

sql = f"""
SELECT sampleid,
    COUNT(CASE WHEN tdist <= 0 THEN 1 ELSE NULL END)/{nur} area_T,
    COUNT(CASE WHEN (tdist <= 500 and tdist > -500) THEN 1 ELSE NULL END)/{nur} area_IM
FROM dbo.randomcell
GROUP BY sampleid
ORDER BY sampleid
"""
db.query(sql)
[12]:
sampleid area_T area_IM
0 100 8.126786 5.672777
1 101 42.775219 34.772239
2 105 2.373890 4.676094
3 107 15.393850 29.413529
4 108 17.282639 14.812269
5 109 69.512430 28.556507
6 110 2.521815 2.346902
7 112 0.000000 0.000000
8 114 16.116441 13.702239
9 115 1.840212 2.338637
10 116 96.025813 41.204714
11 117 5.635669 5.314349
12 118 1.644046 4.592601
13 119 8.604465 5.350107
14 121 0.013494 0.226864
15 123 1.875801 2.075846
16 125 127.244639 36.023954
17 128 95.151080 133.438949
18 129 0.000000 0.000000
19 131 5.477117 5.525695
20 132 84.116895 19.339088
21 134 1.600023 2.405094
22 135 7.874958 7.079164
23 137 0.000000 0.000000
24 138 96.579732 28.390365
25 139 13.738840 11.883280
26 140 136.225579 19.156585
27 144 45.867146 44.170811
28 145 0.468402 0.729506
29 149 37.235019 27.521367
30 150 3.317104 3.537895
31 165 126.298051 23.216351
32 167 2.710559 2.601597
33 169 0.000000 0.000000
34 170 4.758743 4.765827
35 171 2.064545 9.176938
36 172 1.955583 1.962667
37 185 72.270892 65.098284
38 186 81.228387 26.511865
39 187 3.076746 3.562859
40 188 21.148770 15.061398
41 194 3.898685 7.382436
42 195 13.798888 13.005455
43 196 23.025077 28.654674
44 197 64.675929 15.432813
45 521 2.688800 1.812212
46 523 69.309012 39.430791
47 535 0.000000 0.000000
48 536 70.210563 21.907119
49 537 3.524233 2.545766
50 540 2.442033 2.535646
51 541 5.287699 18.219612
52 543 1.692624 4.234342
53 544 0.696447 0.696447
54 545 90.112847 75.244579
55 546 74.893573 25.540652
56 547 0.917744 0.970539
57 548 36.946758 14.132352
58 561 22.175645 41.735356
59 566 2.923423 1.552626

Querying Densities#

[13]:
# Normalization Unit Ratio to convert randomcell density to area (mm2)
nur = 8000.0000 / (1.004 * 1.344)

sql = f"""
SELECT ct.sampleid,
    COUNT(CASE WHEN ct.tdist <= 0 THEN 1 ELSE NULL END)/areas.area_T CD8_T_den,
    COUNT(CASE WHEN (ct.tdist <= 500 and ct.tdist > -500) THEN 1 ELSE NULL END)/areas.area_IM CD8_IM_den
FROM dbo.celltag ct, dbo.phenotype p,
    (SELECT sampleid,
        COUNT(CASE WHEN tdist <= 0 THEN 1 ELSE NULL END)/{nur} area_T,
        COUNT(CASE WHEN (tdist <= 500 and tdist > -500) THEN 1 ELSE NULL END)/{nur} area_IM
    FROM dbo.randomcell
    GROUP BY sampleid
    ) areas
WHERE ct.ptype = p.ptype
    and p.phenotype = 'CD8'
    and ct.sampleid = areas.sampleid
    and areas.area_T > 0
    and areas.area_IM > 0
GROUP BY ct.sampleid, areas.area_T, areas.area_IM
ORDER BY ct.sampleid
"""
db.query(sql)
[13]:
sampleid CD8_T_den CD8_IM_den
0 100 107.791695 138.556485
1 101 1667.133479 1764.482302
2 105 199.251041 315.861924
3 107 1046.586781 1527.018381
4 108 12.903122 95.528916
5 109 78.532717 377.917370
6 110 881.507936 842.813132
7 114 739.803537 1459.250608
8 115 79.882121 101.768668
9 116 300.835776 444.560783
10 117 18.986211 82.042038
11 118 591.832594 2224.665178
12 119 80.888239 95.512105
13 121 222.324986 207.172725
14 123 206.311829 190.283837
15 125 482.094968 796.553322
16 128 469.505969 1028.860020
17 131 308.556480 298.785960
18 132 249.081947 247.167806
19 134 23.124673 100.203982
20 135 504.129651 519.552886
21 138 224.218887 377.663337
22 139 126.357098 131.276889
23 140 290.613556 235.689189
24 144 251.421793 599.173961
25 145 318.102728 290.607457
26 149 253.497926 335.339449
27 150 31.955590 32.222549
28 165 858.833519 1098.019216
29 167 95.552244 96.094824
30 170 98.975714 97.569627
31 171 115.763990 616.436583
32 172 42.953939 43.308408
33 185 930.222366 1971.895305
34 186 20.891711 24.140135
35 187 61.428536 58.380087
36 188 242.803718 280.584852
37 194 2460.060498 3057.662770
38 195 110.008867 114.259751
39 196 367.078028 1046.949616
40 197 70.613597 101.472102
41 521 23.058610 30.901462
42 523 65.777882 269.408749
43 536 453.193914 495.683610
44 537 1027.173924 1050.371275
45 540 170.759348 187.328975
46 541 30.258911 409.723322
47 543 1923.050201 1148.230381
48 544 12.922741 12.922741
49 545 306.193854 539.414273
50 546 453.844018 773.198755
51 547 467.450439 427.597586
52 548 336.159398 681.839786
53 561 498.655165 736.809332
54 566 48.573195 65.695161

Above is a Behemoth of a query that combines the cell count and area queries that we built, well… further above. We can compute the density simply by dividing the cell counts by the area per region, per sample. We often normalize cell counts to the analysis area to control for variability in tissue size.

Spot the modifications: we perform a subquery to get the areas and join the result to the celltag table using the sampleid column; we filter empty areas to avoid division error.

Clearly, SQL can get less legible and harder to maintain the more you attempt to complicate a single query.

Note: Alternatively, you can query cell counts and areas separately and merge the DataFrames on the sampleid column in Python (below).

[14]:
import pandas as pd

sql = f"""
SELECT ct.sampleid,
    COUNT(CASE WHEN ct.tdist <= 0 THEN 1 ELSE NULL END) CD8_T,
    COUNT(CASE WHEN (ct.tdist <= 500 and ct.tdist > -500) THEN 1 ELSE NULL END) CD8_IM
FROM dbo.celltag ct, dbo.phenotype p
WHERE ct.ptype = p.ptype
    and p.phenotype = 'CD8'
GROUP BY ct.sampleid
ORDER BY ct.sampleid
"""
cells = db.query(sql)

# Normalization Unit Ratio to convert randomcell density to area (mm2)
nur = 8000.0000 / (1.004 * 1.344)

sql = f"""
SELECT sampleid,
    COUNT(CASE WHEN tdist <= 0 THEN 1 ELSE NULL END)/{nur} area_T,
    COUNT(CASE WHEN (tdist <= 500 and tdist > -500) THEN 1 ELSE NULL END)/{nur} area_IM
FROM dbo.randomcell
GROUP BY sampleid
ORDER BY sampleid
"""
areas = db.query(sql)

den_df = pd.merge(cells, areas, on="sampleid")

den_df = den_df[(den_df["area_T"] > 0) & (den_df["area_IM"] > 0)]

den_df["CD8_T_den"] = den_df["CD8_T"] / den_df["area_T"]
den_df["CD8_IM_den"] = den_df["CD8_IM"] / den_df["area_IM"]

den_df
[14]:
sampleid CD8_T CD8_IM area_T area_IM CD8_T_den CD8_IM_den
0 100 876 786 8.126786 5.672777 107.791695 138.556485
1 101 71312 61355 42.775219 34.772239 1667.133479 1764.482302
2 105 473 1477 2.373890 4.676094 199.251041 315.861924
3 107 16111 44915 15.393850 29.413529 1046.586781 1527.018381
4 108 223 1415 17.282639 14.812269 12.903122 95.528916
5 109 5459 10792 69.512430 28.556507 78.532717 377.917370
6 110 2223 1978 2.521815 2.346902 881.507936 842.813132
8 114 11923 19995 16.116441 13.702239 739.803537 1459.250608
9 115 147 238 1.840212 2.338637 79.882121 101.768668
10 116 28888 18318 96.025813 41.204714 300.835776 444.560783
11 117 107 436 5.635669 5.314349 18.986211 82.042038
12 118 973 10217 1.644046 4.592601 591.832594 2224.665178
13 119 696 511 8.604465 5.350107 80.888239 95.512105
14 121 3 47 0.013494 0.226864 222.324986 207.172725
15 123 387 395 1.875801 2.075846 206.311829 190.283837
16 125 61344 28695 127.244639 36.023954 482.094968 796.553322
17 128 44674 137290 95.151080 133.438949 469.505969 1028.860020
19 131 1690 1651 5.477117 5.525695 308.556480 298.785960
20 132 20952 4780 84.116895 19.339088 249.081947 247.167806
21 134 37 241 1.600023 2.405094 23.124673 100.203982
22 135 3970 3678 7.874958 7.079164 504.129651 519.552886
24 138 21655 10722 96.579732 28.390365 224.218887 377.663337
25 139 1736 1560 13.738840 11.883280 126.357098 131.276889
26 140 39589 4515 136.225579 19.156585 290.613556 235.689189
27 144 11532 26466 45.867146 44.170811 251.421793 599.173961
28 145 149 212 0.468402 0.729506 318.102728 290.607457
29 149 9439 9229 37.235019 27.521367 253.497926 335.339449
30 150 106 114 3.317104 3.537895 31.955590 32.222549
31 165 108469 25492 126.298051 23.216351 858.833519 1098.019216
32 167 259 250 2.710559 2.601597 95.552244 96.094824
34 170 471 465 4.758743 4.765827 98.975714 97.569627
35 171 239 5657 2.064545 9.176938 115.763990 616.436583
36 172 84 85 1.955583 1.962667 42.953939 43.308408
37 185 67228 128367 72.270892 65.098284 930.222366 1971.895305
38 186 1697 640 81.228387 26.511865 20.891711 24.140135
39 187 189 208 3.076746 3.562859 61.428536 58.380087
40 188 5135 4226 21.148770 15.061398 242.803718 280.584852
41 194 9591 22573 3.898685 7.382436 2460.060498 3057.662770
42 195 1518 1486 13.798888 13.005455 110.008867 114.259751
43 196 8452 30000 23.025077 28.654674 367.078028 1046.949616
44 197 4567 1566 64.675929 15.432813 70.613597 101.472102
45 521 62 56 2.688800 1.812212 23.058610 30.901462
46 523 4559 10623 69.309012 39.430791 65.777882 269.408749
48 536 31819 10859 70.210563 21.907119 453.193914 495.683610
49 537 3620 2674 3.524233 2.545766 1027.173924 1050.371275
50 540 417 475 2.442033 2.535646 170.759348 187.328975
51 541 160 7465 5.287699 18.219612 30.258911 409.723322
52 543 3255 4862 1.692624 4.234342 1923.050201 1148.230381
53 544 9 9 0.696447 0.696447 12.922741 12.922741
54 545 27592 40588 90.112847 75.244579 306.193854 539.414273
55 546 33990 19748 74.893573 25.540652 453.844018 773.198755
56 547 429 415 0.917744 0.970539 467.450439 427.597586
57 548 12420 9636 36.946758 14.132352 336.159398 681.839786
58 561 11058 30751 22.175645 41.735356 498.655165 736.809332
59 566 142 102 2.923423 1.552626 48.573195 65.695161

Querying the treatment of each sample will feel like a cake walk after finding the densities. Have a look:

[15]:
sql = f"""
SELECT TOP 2 *
FROM dbo.clinical
"""
db.query(sql)
[15]:
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, so we can join the treatments to our densities on sampleid without going through the samples table.

We specifically select the column containing the treatment based on, you guessed it… the Schema.

[16]:
sql = f"""
SELECT c.sampleid, c.treatment
FROM dbo.clinical c
"""
db.query(sql)
[16]:
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

Building a Unified Query#

If you wish to perform all of the above in a single query, you may do so as such:

[17]:
# Normalization Unit Ratio to convert randomcell density to area (mm2)
nur = 8000.0000 / (1.004 * 1.344)

sql = f"""
SELECT ct.sampleid,
    COUNT(CASE WHEN ct.tdist <= 0 THEN 1 ELSE NULL END)/areas.area_T CD8_T_den,
    COUNT(CASE WHEN (ct.tdist <= 500 and ct.tdist > -500) THEN 1 ELSE NULL END)/areas.area_IM CD8_IM_den,
    c.treatment
FROM dbo.celltag ct, dbo.phenotype p,
    (SELECT sampleid,
        COUNT(CASE WHEN tdist <= 0 THEN 1 ELSE NULL END)/{nur} area_T,
        COUNT(CASE WHEN (tdist <= 500 and tdist > -500) THEN 1 ELSE NULL END)/{nur} area_IM
    FROM dbo.randomcell
    GROUP BY sampleid
    ) areas,
    dbo.clinical c
WHERE ct.ptype = p.ptype
    and p.phenotype = 'CD8'
    and ct.sampleid = areas.sampleid
    and areas.area_T > 0
    and areas.area_IM > 0
    and ct.sampleid = c.sampleid
GROUP BY ct.sampleid, areas.area_T, areas.area_IM, c.treatment
ORDER BY ct.sampleid
"""
db.query(sql)
[17]:
sampleid CD8_T_den CD8_IM_den treatment
0 100 107.791695 138.556485 Anti-PD-1
1 101 1667.133479 1764.482302 Anti-PD-1
2 105 199.251041 315.861924 Anti-PD-1
3 107 1046.586781 1527.018381 Anti-PD-1
4 108 12.903122 95.528916 Anti-PD-1
5 109 78.532717 377.917370 Anti-PD-1
6 110 881.507936 842.813132 Anti-PD-1
7 114 739.803537 1459.250608 Anti-PD-1
8 115 79.882121 101.768668 Anti-PD-1
9 116 300.835776 444.560783 Anti-PD-1
10 117 18.986211 82.042038 Anti-PD-1
11 118 591.832594 2224.665178 Anti-PD-1
12 119 80.888239 95.512105 Anti-PD-1
13 121 222.324986 207.172725 Anti-PD-1
14 123 206.311829 190.283837 Anti-PD-1
15 125 482.094968 796.553322 Anti-PD-1
16 128 469.505969 1028.860020 Anti-PD-1+CTLA-4
17 131 308.556480 298.785960 Anti-PD-1
18 132 249.081947 247.167806 Anti-PD-1
19 134 23.124673 100.203982 Anti-PD-1
20 135 504.129651 519.552886 Anti-PD-1
21 138 224.218887 377.663337 Anti-PD-1
22 139 126.357098 131.276889 Anti-PD-1
23 140 290.613556 235.689189 Anti-PD-1
24 144 251.421793 599.173961 Anti-PD-1
25 145 318.102728 290.607457 Anti-PD-1
26 149 253.497926 335.339449 Anti-PD-1+CTLA-4
27 150 31.955590 32.222549 Anti-PD-1+CTLA-4
28 165 858.833519 1098.019216 Anti-PD-1+CTLA-4
29 167 95.552244 96.094824 Anti-PD-1+CTLA-4
30 170 98.975714 97.569627 Anti-PD-1+CTLA-4
31 171 115.763990 616.436583 Anti-PD-1+CTLA-4
32 172 42.953939 43.308408 Anti-PD-1+CTLA-4
33 185 930.222366 1971.895305 Anti-PD-1
34 186 20.891711 24.140135 Anti-PD-1+CTLA-4
35 187 61.428536 58.380087 Anti-PD-1
36 188 242.803718 280.584852 Anti-PD-1
37 194 2460.060498 3057.662770 Anti-PD-1
38 195 110.008867 114.259751 Anti-PD-1
39 196 367.078028 1046.949616 Anti-PD-1
40 197 70.613597 101.472102 Anti-PD-1+CTLA-4
41 521 23.058610 30.901462 Anti-PD-1+CTLA-4
42 523 65.777882 269.408749 Anti-PD-1
43 536 453.193914 495.683610 Anti-PD-1
44 537 1027.173924 1050.371275 Anti-PD-1
45 540 170.759348 187.328975 Anti-PD-1
46 541 30.258911 409.723322 Anti-PD-1
47 543 1923.050201 1148.230381 Anti-PD-1+CTLA-4
48 544 12.922741 12.922741 Anti-PD-1
49 545 306.193854 539.414273 Anti-PD-1
50 546 453.844018 773.198755 Anti-PD-1
51 547 467.450439 427.597586 Anti-PD-1
52 548 336.159398 681.839786 Anti-PD-1
53 561 498.655165 736.809332 Anti-PD-1
54 566 48.573195 65.695161 Anti-PD-1

Congratulations, Data Scientist! Your next step will be to analyze this queried data.