{ "cells": [ { "cell_type": "raw", "metadata": { "raw_mimetype": "text/restructuredtext", "vscode": { "languageId": "raw" } }, "source": [ ".. include:: ../../defs/external_refs.txt\n", "\n", ".. _connecting_with_astrodb_example:\n", "\n", "================================================\n", "Connecting to |astropath|\n", "================================================" ] }, { "cell_type": "raw", "metadata": { "raw_mimetype": "text/restructuredtext", "vscode": { "languageId": "raw" } }, "source": [ "In this tutorial, we show how to:\n", " - Connect to the |astropath| database with :class:`.AstroDB`\n", " - Distinguish between raw ``SQL`` and :ref:`sqlalchemy`\n", " - Switch database contexts\n", " - Use built-in properties of :class:`.AstroDB` to quickly access key data structures\n", "\n", "\n", "Getting Started\n", "================\n", "\n", "To get started, the :class:`.AstroDB` class builds a connection to the |astropath| databases\n", "and allows queries to be run against it. This provides a basic access point for the |astropath|\n", "data and most downstream functions accept the :class:`.AstroDB` as an input variable. \n", "\n", "However, much of the functionality has also been (or is in the process of being) generalized\n", "for use with data imported into the :class:`~spatialdata.SpatialData` framework using |sidus|. This allows\n", "much of the functionality for advanced spatial metrics or plotting functions to be applicable to\n", "local datasets. This section details the :class:`.AstroDB` and could be skipped for implementation\n", "with local datasets.\n", "\n", "Below :class:`.AstroDB` is imported from *astropathdb* and an instance of :class:`.AstroDB`\n", "is stored as a variable ``db``." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from astropathdb import AstroDB\n", "db = AstroDB(database = 'apmifwsi001_DR2')" ] }, { "cell_type": "raw", "metadata": { "raw_mimetype": "text/restructuredtext", "vscode": { "languageId": "raw" } }, "source": [ ".. note::\n", " \n", " If using the package locally to access |astropath| datasets, you'll need to input valid |sciserver| credentials when\n", " building :class:`.AstroDB`. See either :class:`.AstroDB` or :ref:`logging_into_sciserver_locally` for more details\n", " on how to input your credentials. When using |sciserver_compute| you don't need to worry about credentials.\n", "\n", ".. note::\n", "\n", " When running this command, the ``database`` input variable is used to specify the\n", " unique database `context` to connect to (above the default ``'apmifwsi001_DR2'`` `context` is specified). \n", " Each public data release is given it's own database `context` with the format `_`, where\n", " `` identifies the dataset and `` the data release (e.g. 'apmifwsi001_DR2', 'apmifwsi002_DR3').\n", " The names are accepted in any letter case. The `wsiXX` databases used within the AstroPath team are also accepted.\n", "\n", "\n", ".. seealso::\n", "\n", " :class:`.AstroDB` or :ref:`logging_into_sciserver_locally`:\n", " - For details on how to input |sciserver| credentials\n", " :ref:`Introduction` and :ref:`database_schema_doc`:\n", " - More information about `contexts`\n", "\n", ":class:`.AstroDB` Overview\n", "====================================\n", "\n", "As noted above, the :class:`.AstroDB` provides the basic connection to the database and is provided\n", "as input for the downstream functionality. Moreover, :class:`.AstroDB` allows queries to be run directly\n", "on the database, either using raw ``SQL`` or using an ORM (Object-Relational Mapping) built using\n", ":ref:`sqlalchemy`. In the subsequent subsections, querying using both\n", "methods is briefly described along with a note about the advantages of both. In the example\n", "sections :ref:`query_with_raw_sql_example` and :ref:`query_with_sqlalchemy_example` more\n", "advanced tutorials are provided using both methods. \n", "\n", "Finally, the :class:`.AstroDB` has a few helper methods and properties for directly accessing metadata\n", "information and tables that are defined in the |astropath| databases. These properties are described\n", "at the end of this tutorial after the discussion about querying the databases directly.\n", "\n", "Writing Your First Query\n", "==========================\n", "\n", "Wait... I still have to write ``SQL``? Yes. Well, sort of. *astropathdb* provides generic wrappers\n", "for many common use cases. This enables initial analysis without having to write any explicit ``SQL``.\n", "As analyses become more complex, writing ``SQL`` will allow users to take full advantage of the\n", "computational power provided by the |sciserver| and ``SQL`` spatial indexing.\n", "\n", "When working with the |astropath| databases for data analysis, there are two primary options: (1) writing\n", "raw ``SQL`` or (2) using an ORM built with :ref:`sqlalchemy`.\n", "\n", "In databases, ORMs provide an abstraction between the code and database, eliminating repetive code,\n", "may improve productivity, and increases maintainability of code. For long term development, using the\n", "ORM has clear benefits, thus most of our internal operations use the ORM. However, an ORM is often more\n", "complex and less flexible than using raw ``SQL`` (especially for early users). Moreover, learning how\n", "to use an ORM takes time with a steep learning curve. Thus an infrequent or new user to the platform\n", "will likely find using raw ``SQL`` much easier. Below a brief introduction of both methods is shown below.\n", "\n", ".. note::\n", "\n", " The |astropath| databases all use Microsoft SQL Server which uses ``t-SQL``.\n", "\n", ".. seealso::\n", "\n", " `CasJobs Guide `_ :\n", " - A great resource for some introductory examples of raw ``SQL`` commands and describes some of the nuances\n", " of working with datasets in the |sciserver|.\n", " :ref:`query_with_raw_sql_example`:\n", " - An advanced example of querying with raw ``SQL``\n", " :ref:`query_with_sqlalchemy_example`:\n", " - An advanced example of using the :ref:`sqlalchemy` ORM\n", "\n", "Writing Queries with Raw ``SQL``\n", "=================================\n", "\n", "Once an :class:`.AstroDB` object is created we can write raw ``SQL`` as a string\n", "and pass that to the :meth:`.AstroDB.query` method. \n", "\n", "An example of this is below where we select the top 2 rows from the :ref:`celltag_schema`.\n", "This table contains all of the cell data for a given database ``context``.\n" ] }, { "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", "
cellidsampleidhpfidpxpyptypeCMembrane520CMembrane540CNucleus570CEntireCell620CMembrane650CEntireCell690exprPhenotypetdistrdistposPDL1PD1
0580036010175858003630313.42298816329.34824930.1205920.0267000.14770116.913810.9983200.2535860-428.670232768POINT (30313.422988093123 16329.348248621156)FalseFalse
1580036010185858003630932.42298816329.34824930.5863280.0100120.16667124.580121.0499850.2671100-104.054732768POINT (30932.422988093123 16329.348248621156)FalseFalse
\n", "
" ], "text/plain": [ " cellid sampleid hpfid px py ptype \\\n", "0 58003601017 58 580036 30313.422988 16329.348249 3 \n", "1 58003601018 58 580036 30932.422988 16329.348249 3 \n", "\n", " CMembrane520 CMembrane540 CNucleus570 CEntireCell620 CMembrane650 \\\n", "0 0.120592 0.026700 0.147701 16.91381 0.998320 \n", "1 0.586328 0.010012 0.166671 24.58012 1.049985 \n", "\n", " CEntireCell690 exprPhenotype tdist rdist \\\n", "0 0.253586 0 -428.6702 32768 \n", "1 0.267110 0 -104.0547 32768 \n", "\n", " pos PDL1 PD1 \n", "0 POINT (30313.422988093123 16329.348248621156) False False \n", "1 POINT (30932.422988093123 16329.348248621156) False False " ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "db.query('Select top 2 * from dbo.celltag')" ] }, { "cell_type": "raw", "metadata": { "raw_mimetype": "text/restructuredtext", "vscode": { "languageId": "raw" } }, "source": [ "Any time we run a query against the database we always store the query into a \n", "property of the :class:`.AstroDB` instance, called :attr:`.AstroDB.sql`. We print\n", "that property below as an example. This can be very helpful for troubleshooting query\n", "results." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Select top 2 * from dbo.celltag'" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "db.sql" ] }, { "cell_type": "raw", "metadata": { "raw_mimetype": "text/restructuredtext", "vscode": { "languageId": "raw" } }, "source": [ "Writing Queries with :ref:`sqlalchemy` Constructs\n", "================================================================================\n", "\n", "On initialization the :class:`.AstroDB` will build a\n", ":ref:`sqlalchemy` ORM for the ``database`` input. The ORM\n", "is stored in an attribute of the :class:`.AstroDB` of the same name as the ``database``\n", "input. We can then write :ref:`sqlalchemy` constructs\n", "from the base class directly, as shown below." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "SELECT TOP 2 celltag.cellid, celltag.sampleid, celltag.hpfid, celltag.px, celltag.py, celltag.ptype, celltag.cmembrane520, celltag.cmembrane540, celltag.cnucleus570, celltag.centirecell620, celltag.cmembrane650, celltag.centirecell690, celltag.exprphenotype, celltag.tdist, celltag.rdist, celltag.pos.STAsBinary() AS pos, celltag.pdl1, celltag.pd1 \n", "FROM celltag\n" ] } ], "source": [ "sql = db.select(db.apmifwsi001_DR2.celltag).limit(2)\n", "db.print_sql(sql) # we can also print out the construct itself db.print_sa_construct(sql)" ] }, { "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
cellidsampleidhpfidpxpyptypecmembrane520cmembrane540cnucleus570centirecell620cmembrane650centirecell690exprphenotypetdistrdistpospdl1pd1
0580036010175858003630313.42298816329.34824930.1205920.0267000.14770116.913810.9983200.2535860-428.6702327680x0101000000a4a63c125b9add405f2b6993ace4cf40FalseFalse
1580036010185858003630932.42298816329.34824930.5863280.0100120.16667124.580121.0499850.2671100-104.0547327680x0101000000a4a63c121b35de405f2b6993ace4cf40FalseFalse
\n", "
" ], "text/plain": [ " cellid sampleid hpfid px py ptype \\\n", "0 58003601017 58 580036 30313.422988 16329.348249 3 \n", "1 58003601018 58 580036 30932.422988 16329.348249 3 \n", "\n", " cmembrane520 cmembrane540 cnucleus570 centirecell620 cmembrane650 \\\n", "0 0.120592 0.026700 0.147701 16.91381 0.998320 \n", "1 0.586328 0.010012 0.166671 24.58012 1.049985 \n", "\n", " centirecell690 exprphenotype tdist rdist \\\n", "0 0.253586 0 -428.6702 32768 \n", "1 0.267110 0 -104.0547 32768 \n", "\n", " pos pdl1 pd1 \n", "0 0x0101000000a4a63c125b9add405f2b6993ace4cf40 False False \n", "1 0x0101000000a4a63c121b35de405f2b6993ace4cf40 False False " ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "db.query(sql)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that this can also be done in one step and that the query output is exactly the same\n", "as if we passed raw `SQL`." ] }, { "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
cellidsampleidhpfidpxpyptypecmembrane520cmembrane540cnucleus570centirecell620cmembrane650centirecell690exprphenotypetdistrdistpospdl1pd1
0580036010175858003630313.42298816329.34824930.1205920.0267000.14770116.913810.9983200.2535860-428.6702327680x0101000000a4a63c125b9add405f2b6993ace4cf40FalseFalse
1580036010185858003630932.42298816329.34824930.5863280.0100120.16667124.580121.0499850.2671100-104.0547327680x0101000000a4a63c121b35de405f2b6993ace4cf40FalseFalse
\n", "
" ], "text/plain": [ " cellid sampleid hpfid px py ptype \\\n", "0 58003601017 58 580036 30313.422988 16329.348249 3 \n", "1 58003601018 58 580036 30932.422988 16329.348249 3 \n", "\n", " cmembrane520 cmembrane540 cnucleus570 centirecell620 cmembrane650 \\\n", "0 0.120592 0.026700 0.147701 16.91381 0.998320 \n", "1 0.586328 0.010012 0.166671 24.58012 1.049985 \n", "\n", " centirecell690 exprphenotype tdist rdist \\\n", "0 0.253586 0 -428.6702 32768 \n", "1 0.267110 0 -104.0547 32768 \n", "\n", " pos pdl1 pd1 \n", "0 0x0101000000a4a63c125b9add405f2b6993ace4cf40 False False \n", "1 0x0101000000a4a63c121b35de405f2b6993ace4cf40 False False " ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "db.query(db.select(db.apmifwsi001_DR2.celltag).limit(2))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "One of the benefits here, is that we remove all the text from our code. This often reduces errors on our queries. As previously noted we've also added some unique filters that we can use for a given query. For example below we can filter our table by ``sampleid`` and ``ptype``." ] }, { "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", "
cellidsampleidhpfidpxpyptypecmembrane520cmembrane540cnucleus570centirecell620cmembrane650centirecell690exprphenotypetdistrdistpospdl1pd1
0190601002571919060132125.1644517048.46235721.2971971.08861051.927649.12693915.772870.0107506414.23974327680x01010000003cab58864a5fdf409ae241971da6d040FalseTrue
1190601002581919060132349.1644517086.46235723.3275101.19878647.109732.02269544.915870.64095364237.84630327680x01010000003cab58864a97df409ae241979dafd040FalseTrue
\n", "
" ], "text/plain": [ " cellid sampleid hpfid px py ptype \\\n", "0 19060100257 19 190601 32125.16445 17048.462357 2 \n", "1 19060100258 19 190601 32349.16445 17086.462357 2 \n", "\n", " cmembrane520 cmembrane540 cnucleus570 centirecell620 cmembrane650 \\\n", "0 1.297197 1.088610 51.92764 9.126939 15.77287 \n", "1 3.327510 1.198786 47.10973 2.022695 44.91587 \n", "\n", " centirecell690 exprphenotype tdist rdist \\\n", "0 0.010750 64 14.23974 32768 \n", "1 0.640953 64 237.84630 32768 \n", "\n", " pos pdl1 pd1 \n", "0 0x01010000003cab58864a5fdf409ae241971da6d040 False True \n", "1 0x01010000003cab58864a97df409ae241979dafd040 False True " ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "db.query(db.select(db.apmifwsi001_DR2.celltag).sampleid_filter(19).ptype_filter(2).limit(2))" ] }, { "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", "
cellidsampleidhpfidpxpyptypecmembrane520cmembrane540cnucleus570centirecell620cmembrane650centirecell690exprphenotypetdistrdistpospdl1pd1
0190601000271919060132877.1644517723.46235751.78530821.7837123.321092.98869432.972020.29561864178.14160327680x01010000009e552c43a50de0409ae24197dd4ed140FalseTrue
1190601000441919060132585.1644517217.46235754.40344318.2128454.848294.76839241.374100.0282186425.65819327680x01010000003cab58864ad2df409ae241975dd0d040FalseTrue
\n", "
" ], "text/plain": [ " cellid sampleid hpfid px py ptype \\\n", "0 19060100027 19 190601 32877.16445 17723.462357 5 \n", "1 19060100044 19 190601 32585.16445 17217.462357 5 \n", "\n", " cmembrane520 cmembrane540 cnucleus570 centirecell620 cmembrane650 \\\n", "0 1.785308 21.78371 23.32109 2.988694 32.97202 \n", "1 4.403443 18.21284 54.84829 4.768392 41.37410 \n", "\n", " centirecell690 exprphenotype tdist rdist \\\n", "0 0.295618 64 178.14160 32768 \n", "1 0.028218 64 25.65819 32768 \n", "\n", " pos pdl1 pd1 \n", "0 0x01010000009e552c43a50de0409ae24197dd4ed140 False True \n", "1 0x01010000003cab58864ad2df409ae241975dd0d040 False True " ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "db.query(db.select(db.apmifwsi001_DR2.celltag).sampleid_filter([19,2]).ptype_filter([0,1,2], include = False).limit(2))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Changing Databases\n", "\n", "As you've probably noticed. We referenced the ``celltag`` table above as a property of ``db.apmifwsi001_DR2``. This is because when we created that ``AstroDB`` object (``db``), a mapping to the database was created dynamically based on the tables that currently exist in that database. In databasing terms this is called the databas ``context``. Below a new ``AstroDB`` object is created and assigned to ``db2`` using ``apmifwsi002_DR3`` as the ``context`` in this case (referred to in *astropathdb* as the ``database``)." ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "db2 = AstroDB(database = 'apmifwsi002_DR3')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "With this object we can now query the ``apmifwsi002_DR3`` database, but we can no longer reference the ``apmifwsi001_DR2`` database with our ``sqlalchemy`` constructs." ] }, { "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", "
cellidsampleidhpfidpxpyptypecmembrane520cmembrane540cnucleus570ccytomembrane620cmembrane650cmembrane690exprphenotypetdistrdistpospdl1pd1
0545129300043545545129317083.93120532500.79295521.2109940.2719659.322213.0370471.0869475.835883048.60199327680x010100000080ffdd98fbaed040cd78c6bf32bddf40FalseFalse
1545129300044545545129317016.93120532558.79295522.1605100.2719620.019332.4309481.8095670.463564013.79003327680x010100000080ffdd983b9ed040cd78c6bfb2cbdf40FalseFalse
\n", "
" ], "text/plain": [ " cellid sampleid hpfid px py ptype \\\n", "0 545129300043 545 5451293 17083.931205 32500.792955 2 \n", "1 545129300044 545 5451293 17016.931205 32558.792955 2 \n", "\n", " cmembrane520 cmembrane540 cnucleus570 ccytomembrane620 cmembrane650 \\\n", "0 1.210994 0.27196 59.32221 3.037047 1.086947 \n", "1 2.160510 0.27196 20.01933 2.430948 1.809567 \n", "\n", " cmembrane690 exprphenotype tdist rdist \\\n", "0 5.835883 0 48.60199 32768 \n", "1 0.463564 0 13.79003 32768 \n", "\n", " pos pdl1 pd1 \n", "0 0x010100000080ffdd98fbaed040cd78c6bf32bddf40 False False \n", "1 0x010100000080ffdd983b9ed040cd78c6bfb2cbdf40 False False " ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "db2.query(db2.select(db2.apmifwsi002_DR3.celltag).ptype_filter(2).limit(2))\n", "# db2.query(db2.select(db2.apmifwsi001_DR2.celltag).sampleid_filter(19).ptype_filter(2).limit(2))\n", "# this one won't work!!!! we can't reference apmifwsi001_DR2 anymore!!!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Referencing between databases is a big next step. We can add a session for another ``database``, either by using the command below or by changing the ``database`` property of the object (the old ``database`` mapping will still exist). " ] }, { "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", "
cellidsampleidhpfidpxpyptypecmembrane520cmembrane540cnucleus570centirecell620cmembrane650centirecell690exprphenotypetdistrdistpospdl1pd1
0190601002571919060132125.1644517048.46235721.2971971.08861051.927649.12693915.772870.0107506414.23974327680x01010000003cab58864a5fdf409ae241971da6d040FalseTrue
1190601002581919060132349.1644517086.46235723.3275101.19878647.109732.02269544.915870.64095364237.84630327680x01010000003cab58864a97df409ae241979dafd040FalseTrue
\n", "
" ], "text/plain": [ " cellid sampleid hpfid px py ptype \\\n", "0 19060100257 19 190601 32125.16445 17048.462357 2 \n", "1 19060100258 19 190601 32349.16445 17086.462357 2 \n", "\n", " cmembrane520 cmembrane540 cnucleus570 centirecell620 cmembrane650 \\\n", "0 1.297197 1.088610 51.92764 9.126939 15.77287 \n", "1 3.327510 1.198786 47.10973 2.022695 44.91587 \n", "\n", " centirecell690 exprphenotype tdist rdist \\\n", "0 0.010750 64 14.23974 32768 \n", "1 0.640953 64 237.84630 32768 \n", "\n", " pos pdl1 pd1 \n", "0 0x01010000003cab58864a5fdf409ae241971da6d040 False True \n", "1 0x01010000003cab58864a97df409ae241979dafd040 False True " ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "db2.load_db_session('apmifwsi001_DR2')\n", "db2.query(\n", " db2.select(db2.apmifwsi001_DR2.celltag).sampleid_filter(19).ptype_filter(2).limit(2),\n", " db='apmifwsi001_DR2'\n", ")" ] }, { "cell_type": "code", "execution_count": 12, "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
0545129300043545545129317083.93120532500.79295521.2109940.2719659.322213.0370471.0869475.835883048.60199327680x010100000080ffdd98fbaed040cd78c6bf32bddf40FalseFalse
1545129300044545545129317016.93120532558.79295522.1605100.2719620.019332.4309481.8095670.463564013.79003327680x010100000080ffdd983b9ed040cd78c6bfb2cbdf40FalseFalse
\n", "
" ], "text/plain": [ " cellid sampleid hpfid px py ptype \\\n", "0 545129300043 545 5451293 17083.931205 32500.792955 2 \n", "1 545129300044 545 5451293 17016.931205 32558.792955 2 \n", "\n", " cmembrane520 cmembrane540 cnucleus570 ccytomembrane620 cmembrane650 \\\n", "0 1.210994 0.27196 59.32221 3.037047 1.086947 \n", "1 2.160510 0.27196 20.01933 2.430948 1.809567 \n", "\n", " cmembrane690 exprphenotype tdist rdist \\\n", "0 5.835883 0 48.60199 32768 \n", "1 0.463564 0 13.79003 32768 \n", "\n", " pos pdl1 pd1 \n", "0 0x010100000080ffdd98fbaed040cd78c6bf32bddf40 False False \n", "1 0x010100000080ffdd983b9ed040cd78c6bfb2cbdf40 False False " ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "db2.query(db2.select(db2.apmifwsi002_DR3.celltag).ptype_filter(2).limit(2))" ] }, { "cell_type": "raw", "metadata": { "raw_mimetype": "text/restructuredtext", "vscode": { "languageId": "raw" } }, "source": [ ":class:`.AstroDB` Unique Properties\n", "====================================\n", "\n", ":class:`.AstroDB` also has a set of properties. The first one to note is the\n", ":attr:`.AstroDB.public_dbs_metadata`. This contains a dictionary of the available\n", "databases and some metadata related to each database. As a demonstration we\n", "have printed the \"apmifwsi001_DR2\" metadata dictionary below. \n" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'Tissue': 'melanoma',\n", " 'cohort': 1,\n", " 'machine': 'JHUVectra3_1',\n", " 'method': 'Manual',\n", " 'panel': 'PD1/PDL1 Axis Panel',\n", " 'stainconfig': 1,\n", " 'xlevel': 1}" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "db.public_dbs_metadata['apmifwsi001_DR2']" ] }, { "cell_type": "raw", "metadata": { "raw_mimetype": "text/restructuredtext", "vscode": { "languageId": "raw" } }, "source": [ "Additionally, it contains properties which can be used to reference\n", "tables in the database related to the database's meta data.\n", "\n", "These properties are grabbed and stored when requested and are cleared when switch to a\n", "new ``database``. The properties are grouped into three elements:\n", ":attr:`.AstroDB.cellmeta`, :attr:`.AstroDB.metadata`, and :attr:`.AstroDB.schema`.\n", "Below we print out some of the referenced tables, for a more detailed summary see the\n", "full :class:`.AstroDB` documentation." ] }, { "cell_type": "code", "execution_count": 14, "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#EE4035FF
22FoxP3#F4E76FFF
33Tumor#F37736FF
44CD163#B361B4FF
55FoxP3CD8#F193D2FF
\n", "
" ], "text/plain": [ " ptype phenotype color\n", "0 0 Other #888888FF\n", "1 1 CD8 #EE4035FF\n", "2 2 FoxP3 #F4E76FFF\n", "3 3 Tumor #F37736FF\n", "4 4 CD163 #B361B4FF\n", "5 5 FoxP3CD8 #F193D2FF" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# the translation of ptype to phenotype\n", "db.cellmeta.phenotype" ] }, { "cell_type": "code", "execution_count": 15, "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", "
exprbitval
0DAPI01
1Opal48012
2Opal52024
3Opal54038
4Opal570416
5Opal620532
6Opal650664
7Opal6707128
8Opal6908256
9Opal7809512
\n", "
" ], "text/plain": [ " expr bit val\n", "0 DAPI 0 1\n", "1 Opal480 1 2\n", "2 Opal520 2 4\n", "3 Opal540 3 8\n", "4 Opal570 4 16\n", "5 Opal620 5 32\n", "6 Opal650 6 64\n", "7 Opal670 7 128\n", "8 Opal690 8 256\n", "9 Opal780 9 512" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# the translation of exprphenotype to their opals\n", "db.cellmeta.exprphenotype" ] }, { "cell_type": "code", "execution_count": 16, "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", "
projectcohortbatchidlayeropaltargetcompartmenttargettypecoexpressionstatussegmentationstatussegmentationhierarchynumberofsegmentationsimageqacolor
01131DAPIDNANucleusNANA000NA#0000FF
11132520PDL1MembraneExpression620,690101NA#7BC043
21133540CD8MembraneLineage570111Immune#EE4035
31134570FoxP3NucleusLineage540111NA#F4E76F
41135620Sox10/S100EntireCellLineageNA231Tumor#F37736
\n", "
" ], "text/plain": [ " project cohort batchid layer opal target compartment targettype \\\n", "0 1 1 3 1 DAPI DNA Nucleus NA \n", "1 1 1 3 2 520 PDL1 Membrane Expression \n", "2 1 1 3 3 540 CD8 Membrane Lineage \n", "3 1 1 3 4 570 FoxP3 Nucleus Lineage \n", "4 1 1 3 5 620 Sox10/S100 EntireCell Lineage \n", "\n", " coexpressionstatus segmentationstatus segmentationhierarchy \\\n", "0 NA 0 0 \n", "1 620,690 1 0 \n", "2 570 1 1 \n", "3 540 1 1 \n", "4 NA 2 3 \n", "\n", " numberofsegmentations imageqa color \n", "0 0 NA #0000FF \n", "1 1 NA #7BC043 \n", "2 1 Immune #EE4035 \n", "3 1 NA #F4E76F \n", "4 1 Tumor #F37736 " ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# metadata about the panel\n", "db.cellmeta.mergeconfig.head()" ] }, { "cell_type": "code", "execution_count": 17, "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", "
TABLE_CATALOGTABLE_SCHEMATABLE_NAMETABLE_TYPE
0apmifwsi001_DR2dboAffineBASE TABLE
1apmifwsi001_DR2dboAlignBASE TABLE
2apmifwsi001_DR2dboAnnotationsBASE TABLE
3apmifwsi001_DR2dboBatchBASE TABLE
4apmifwsi001_DR2dboBoundaryTypeBASE TABLE
\n", "
" ], "text/plain": [ " TABLE_CATALOG TABLE_SCHEMA TABLE_NAME TABLE_TYPE\n", "0 apmifwsi001_DR2 dbo Affine BASE TABLE\n", "1 apmifwsi001_DR2 dbo Align BASE TABLE\n", "2 apmifwsi001_DR2 dbo Annotations BASE TABLE\n", "3 apmifwsi001_DR2 dbo Batch BASE TABLE\n", "4 apmifwsi001_DR2 dbo BoundaryType BASE TABLE" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# a list of tables in the database\n", "db.schema.wsi_tables.head()" ] }, { "cell_type": "code", "execution_count": 18, "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", "
astroidsampleidresponsetreatmentageatcollectionsexanatomicsitemelanomastagelesion_typebest_responseos_daysos statuspfs_dayspfs_statusbraf statusprior immunotherapy
0P10_D01_C01_S01_B01_L0174Non-ResponderIpilimumab + nivolumab81FemaleMucosalStage IVPrimaryPD109Deceased91Non-Responderno mutation detectedNo
1P11_D01_C01_S01_B01_L018ResponderNivolumab80MaleSkin_soft-tissueStage IVMetastasisPR1428Alive1610Responderno mutation detectedNo
2P12_D01_C01_S01_B01_L0176ResponderPembrolizumab51FemaleSkin_soft-tissueStage III unresectableCR670Alive609Non-Responderno mutation detectedNo
3P13_D01_C01_S01_B01_L0117ResponderNivolumab61MaleSkin_soft-tissueStage IVPrimaryPR3435Alive3434ResponderNo
4P14_D01_C01_S01_B01_L0119ResponderIpilimumab + nivolumab63FemaleLymph-nodeStage IVMetastasisPR1219Alive402Non-Responderno mutation detectedNo
\n", "
" ], "text/plain": [ " astroid sampleid response treatment \\\n", "0 P10_D01_C01_S01_B01_L01 74 Non-Responder Ipilimumab + nivolumab \n", "1 P11_D01_C01_S01_B01_L01 8 Responder Nivolumab \n", "2 P12_D01_C01_S01_B01_L01 76 Responder Pembrolizumab \n", "3 P13_D01_C01_S01_B01_L01 17 Responder Nivolumab \n", "4 P14_D01_C01_S01_B01_L01 19 Responder Ipilimumab + nivolumab \n", "\n", " ageatcollection sex anatomicsite melanomastage \\\n", "0 81 Female Mucosal Stage IV \n", "1 80 Male Skin_soft-tissue Stage IV \n", "2 51 Female Skin_soft-tissue Stage III unresectable \n", "3 61 Male Skin_soft-tissue Stage IV \n", "4 63 Female Lymph-node Stage IV \n", "\n", " lesion_type best_response os_days os status pfs_days pfs_status \\\n", "0 Primary PD 109 Deceased 91 Non-Responder \n", "1 Metastasis PR 1428 Alive 1610 Responder \n", "2 CR 670 Alive 609 Non-Responder \n", "3 Primary PR 3435 Alive 3434 Responder \n", "4 Metastasis PR 1219 Alive 402 Non-Responder \n", "\n", " braf status prior immunotherapy \n", "0 no mutation detected No \n", "1 no mutation detected No \n", "2 no mutation detected No \n", "3 No \n", "4 no mutation detected No " ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# clinical data for the database context\n", "db.metadata.clinical.head()" ] }, { "cell_type": "code", "execution_count": 19, "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", "
sampleidslideidprojectcohortscanbatchidisgoodastroid
02M2_311191P26_D01_C01_S01_B01_L01
13M3_111191P25_D01_C01_S01_B01_L01
24M4_211161P28_D01_C01_S01_B01_L01
35M6_111181P38_D01_C01_S01_B01_L01
46M7_211191P39_D01_C01_S01_B01_L01
\n", "
" ], "text/plain": [ " sampleid slideid project cohort scan batchid isgood \\\n", "0 2 M2_3 1 1 1 9 1 \n", "1 3 M3_1 1 1 1 9 1 \n", "2 4 M4_2 1 1 1 6 1 \n", "3 5 M6_1 1 1 1 8 1 \n", "4 6 M7_2 1 1 1 9 1 \n", "\n", " astroid \n", "0 P26_D01_C01_S01_B01_L01 \n", "1 P25_D01_C01_S01_B01_L01 \n", "2 P28_D01_C01_S01_B01_L01 \n", "3 P38_D01_C01_S01_B01_L01 \n", "4 P39_D01_C01_S01_B01_L01 " ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# a list of samples in the database\n", "db.metadata.samples.head()" ] } ], "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 }