Densities#

[1]:
%load_ext autoreload
%autoreload 2
#
from astropathdb import AstroDB
from astropathdb import plotting as pl
from astropathdb import analysis as al
import seaborn as sns
import matplotlib.pyplot as plt
[2]:
db = AstroDB()
[3]:
cell_densities = al.get_cell_densities(astrodb=db)
groups_df = db.query("select sampleid, response as groups from dbo.clinical")
cell_densities_w_groups = cell_densities.merge(groups_df, on = 'sampleid')
cell_densities_w_groups.head()
[3]:
sampleid phenotype count density area groups
0 2 CD163 34344 584.850982 58.722651 Responder
1 2 CD8 29683 505.477863 58.722651 Responder
2 2 FoxP3 13228 225.262311 58.722651 Responder
3 2 FoxP3CD8 1656 28.200362 58.722651 Responder
4 2 Other 93527 1592.690363 58.722651 Responder
[4]:
# fix colors to be base on phenotype colors
g = sns.FacetGrid(
    data = cell_densities_w_groups,
    col="phenotype",
    sharey = False,
    col_wrap = 3,
    height = 2,
    aspect = 1.5
)
generic_args = {
    "y":"density", 'hue':'groups', 'palette':['tab:red', 'tab:blue'],
    'gap':.15, 'density_norm': 'count', 'inner': None
}
g.map_dataframe(
    sns.violinplot, alpha = 0.35, linewidth = 0, **generic_args
)
g.map_dataframe(
    sns.violinplot, fill = False, alpha = 0.8, linewidth = 1, **generic_args
)
g.set_titles(col_template="{col_name}")
#
g.set(ylim=0)
[4]:
<seaborn.axisgrid.FacetGrid at 0x1b8c58714c0>
../../_images/pkg_examples_tutorials_metrics_densities_4_1.png
[5]:
f, axs = plt.subplots(nrows=1, ncols=1, figsize=(6, 3))
#sns.set_theme(style="ticks", palette="pastel")
generic_args = {
    'x': 'phenotype', "y":"density", 'hue':'groups', 'palette':['tab:red', 'tab:blue'],
    'gap':.15, 'density_norm': 'count', 'inner': None, 'data':cell_densities_w_groups,
    'ax':axs,'common_norm':False
}
sns.violinplot(fill=True,linewidth=0, alpha = 0.35, **generic_args)
sns.violinplot(fill=False,linewidth=1, alpha = 0.8, **generic_args)
sns.move_legend(axs, "upper left")
axs.set(ylim=0);

../../_images/pkg_examples_tutorials_metrics_densities_5_0.png
[6]:
db.cellmeta.mixing_matrix = 'default'
db.cellmeta.phenotype
[6]:
ptype phenotype color
0 0 Other #0000FFFF
1 1 CD8 #C9C012FF
2 2 FoxP3 #B91919FF
3 3 Tumor #923D00FF
4 4 CD163 #FF00FFFF
5 5 FoxP3CD8 #F193D2FF
[7]:
p =  {'color':['#cc00cc', 'tab:olive', '#D10000', '#E87980', '#005592', 'tab:orange']}
g = sns.FacetGrid(data = cell_densities_w_groups, col = 'groups',row="phenotype",sharey = 'row',
    sharex=False,aspect = 4,height = 2,hue = 'phenotype',hue_kws=p)
g.map_dataframe(sns.barplot, x= 'sampleid', y= "density", fill = True, linewidth = 0, alpha = .6)
g.map_dataframe(sns.barplot, x= 'sampleid', y= "density", fill = False, linewidth = .5, alpha = .9)
g.set_titles(col_template="{col_name}", row_template="{row_name}")
g.set(ylim=0);
../../_images/pkg_examples_tutorials_metrics_densities_7_0.png

Different regions#

[8]:
cell_densities = al.get_cell_densities(astrodb=db, tdist = {'tdist': 0, 'operator':'>='})
[9]:
db.print_sql()
SELECT celltag.sampleid, phenotype as phenotype, count(*) as count, cast(count(*) as float) / area as density, area as area
FROM celltag JOIN (SELECT phenotype.ptype AS ptype, phenotype.phenotype AS phenotype
FROM phenotype) AS p ON p.ptype = celltag.ptype JOIN (SELECT randomcell.sampleid AS sampleid, count (*)/5928.666287232024 as area
FROM randomcell
WHERE tdist >= 0 GROUP BY sampleid) AS a ON celltag.sampleid = a.sampleid
WHERE tdist >= 0 GROUP BY celltag.sampleid, phenotype, area ORDER BY 1,2
[10]:
groups_df = db.query("select sampleid, response as groups from dbo.clinical")
cell_densities_w_groups = cell_densities.merge(groups_df, on = 'sampleid')
cell_densities_w_groups.head()
[10]:
sampleid phenotype count density area groups
0 2 CD163 4849 254.012360 19.089622 Responder
1 2 CD8 6315 330.808012 19.089622 Responder
2 2 FoxP3 5417 283.766746 19.089622 Responder
3 2 FoxP3CD8 593 31.063999 19.089622 Responder
4 2 Other 58837 3082.145847 19.089622 Responder

Different Expression Phenotypes#

[11]:
cell_densities = al.get_cell_densities(astrodb=db, sampleid = {'sampleid': (19, 20), 'table_name':'celltag'})
groups_df = db.query("select sampleid, response as groups from dbo.clinical")
cell_densities_w_groups = cell_densities.merge(groups_df, on = 'sampleid')
cell_densities_w_groups.head()
[11]:
sampleid phenotype count density area groups
0 19 CD163 24208 324.980535 74.490615 Responder
1 19 CD8 64201 861.866957 74.490615 Responder
2 19 FoxP3 58324 782.971113 74.490615 Responder
3 19 FoxP3CD8 3466 46.529351 74.490615 Responder
4 19 Other 725627 9741.186813 74.490615 Responder
[12]:
cell_densities = al.get_cell_densities(astrodb=db, exprphenotype = {'phenotype': 'PD1', 'bitwise':True})
cell_densities.head(10)
[12]:
sampleid phenotype count density area
0 2 CD163 375 6.385952 58.722651
1 2 CD8 19698 335.441261 58.722651
2 2 FoxP3 7251 123.478758 58.722651
3 2 FoxP3CD8 1380 23.500302 58.722651
4 2 Other 15948 271.581745 58.722651
5 2 Tumor 1785 30.397129 58.722651
6 3 CD163 1780 6.381614 278.926308
7 3 CD8 22411 80.347387 278.926308
8 3 FoxP3 34588 124.004079 278.926308
9 3 FoxP3CD8 1437 5.151898 278.926308
[13]:
cell_densities = al.get_cell_densities(astrodb=db, exprphenotype = {'phenotype': 'PD1'})
cell_densities.head(10)
[13]:
sampleid phenotype count density area
0 2 CD163 178 3.031198 58.722651
1 2 CD8 14849 252.866650 58.722651
2 2 FoxP3 5291 90.101518 58.722651
3 2 FoxP3CD8 980 16.688620 58.722651
4 2 Other 13485 229.638816 58.722651
5 2 Tumor 1153 19.634672 58.722651
6 3 CD163 1380 4.947543 278.926308
7 3 CD8 19639 70.409278 278.926308
8 3 FoxP3 30915 110.835726 278.926308
9 3 FoxP3CD8 1280 4.589026 278.926308
[14]:
cell_densities = al.get_cell_densities(astrodb=db, exprphenotype = {'phenotype': 'PD1', 'include': False, 'bitwise': True})
cell_densities.head(10)
[14]:
sampleid phenotype count density area
0 2 CD163 33969 578.465031 58.722651
1 2 CD8 9985 170.036602 58.722651
2 2 FoxP3 5977 101.783552 58.722651
3 2 FoxP3CD8 276 4.700060 58.722651
4 2 Other 77579 1321.108618 58.722651
5 2 Tumor 269311 4586.151960 58.722651
6 3 CD163 185849 666.301437 278.926308
7 3 CD8 55195 197.883808 278.926308
8 3 FoxP3 26479 94.931884 278.926308
9 3 FoxP3CD8 1074 3.850479 278.926308