Query the database with raw SQL#

In this tutorial we demonstrate how to:
  • Query cell counts as proportions using raw SQL

  • Query clinical data using raw SQL

  • Merge the data together to compare by treatment

To demonstrate raw SQL, 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 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 using Raw SQL#

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 last line of the query, OPTION (MAXDOP 4), limits SQL Server to four parallel threads. Without it, this query takes longer than the 60 seconds that CasJobs allows a query.

[2]:
sql = f"""
SELECT c.sampleid, count(*) c, cast(count(*) as float) / totalcells p, Phenotype
FROM dbo.celltag c, dbo.phenotype p, (
    SELECT sampleid, count(*) totalcells
    FROM dbo.celltag
    GROUP BY sampleid
) t
WHERE c.ptype = p.ptype
and t.sampleid = c.sampleid
GROUP BY c.sampleid, Phenotype, totalcells
ORDER BY sampleid, phenotype
OPTION (MAXDOP 4)
"""
db.query(sql)
[2]:
sampleid c p Phenotype
0 100 7429 0.099362 CD163
1 100 1181 0.015796 CD8
2 100 2111 0.028234 FoxP3
3 100 23 0.000308 FoxP3CD8
4 100 34970 0.467720 Other
... ... ... ... ...
352 566 142 0.007292 CD8
353 566 560 0.028756 FoxP3
354 566 4 0.000205 FoxP3CD8
355 566 13053 0.670278 Other
356 566 5377 0.276112 Tumor

357 rows × 4 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.

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 10 * 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.

Since we’re reducing the number of rows to unique sampleids, we can look at the whole query.

[6]:
sql = f"""
    SELECT sampleid, COUNT(*) c
    FROM dbo.celltag
    GROUP BY sampleid
"""
db.query(sql)
[6]:
sampleid c
0 121 8387
1 150 20334
2 172 10937
3 169 2535257
4 132 303302
5 100 74767
6 101 1471477
7 135 48364
8 171 679392
9 112 1654297
10 197 1404659
11 117 71240
12 185 1535958
13 123 17568
14 536 319148
15 187 45861
16 535 647792
17 544 5970
18 129 786765
19 149 910420
20 105 39115
21 131 36410
22 167 21144
23 145 7490
24 115 11807
25 566 19474
26 116 1133377
27 165 1945897
28 546 1352191
29 138 868023
30 119 24615
31 543 103197
32 523 896873
33 545 1609429
34 118 1449342
35 537 33648
36 186 530408
37 107 2284319
38 541 623603
39 128 2113728
40 170 15516
41 195 82318
42 110 31972
43 140 933002
44 194 722846
45 521 9180
46 561 1395540
47 547 8175
48 144 1008612
49 196 1147519
50 188 712017
51 548 1610702
52 125 1569148
53 139 76285
54 540 40166
55 108 174918
56 114 1157735
57 137 924457
58 109 1094759
59 134 22097

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]:
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)
[7]:
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

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. Parentheses are used here to add a nested query in the FROM clause to get the total cells for each sample.

[8]:
sql = f"""
SELECT c.sampleid, count(*) c, cast(count(*) as float) / totalcells p, Phenotype
FROM dbo.celltag c, dbo.phenotype p, (
    SELECT sampleid, count(*) totalcells
    FROM dbo.celltag
    GROUP BY sampleid
) t
WHERE c.ptype = p.ptype
and t.sampleid = c.sampleid
GROUP BY c.sampleid, Phenotype, totalcells
ORDER BY sampleid, phenotype
OPTION (MAXDOP 4)
"""
db.query(sql)
[8]:
sampleid c p Phenotype
0 100 7429 0.099362 CD163
1 100 1181 0.015796 CD8
2 100 2111 0.028234 FoxP3
3 100 23 0.000308 FoxP3CD8
4 100 34970 0.467720 Other
... ... ... ... ...
352 566 142 0.007292 CD8
353 566 560 0.028756 FoxP3
354 566 4 0.000205 FoxP3CD8
355 566 13053 0.670278 Other
356 566 5377 0.276112 Tumor

357 rows × 4 columns

Querying the Clinical Data using Raw SQL#

Next, we can extend this query to include the treatment of each sample. First, we take a look at the Clinical Table.

[9]:
sql = f"""
SELECT TOP 2 *
FROM dbo.clinical
"""
db.query(sql)
[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]:
sql = f"""
SELECT c.sampleid, c.treatment
FROM dbo.clinical c
"""
db.query(sql)
[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]:
sql = """
    SELECT celltag.sampleid, phenotype.phenotype, clinical.treatment,
        count(*) AS count, cast(count(*) as float) / totalcells as p, totalcells
    FROM celltag, phenotype, clinical, (
        SELECT celltag.sampleid AS sampleid, count(*) AS totalcells
        FROM celltag GROUP BY celltag.sampleid
    ) AS t
    WHERE phenotype.ptype = celltag.ptype
    AND celltag.sampleid = t.sampleid
    AND celltag.sampleid = clinical.sampleid
    AND celltag.ptype = 1
    GROUP BY celltag.sampleid, phenotype.phenotype,
        t.totalcells, clinical.treatment ORDER BY 1,2
"""
sample_proportions = db.query(sql)
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.