{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Create a repeated treatment design with ISA descriptor\n",
"\n",
"This example creates `ISA study descriptor` for study with sequential treatments organized in an arm. \n",
"This shows how to use objects from the `isatools.create` component in a granular fashion.\n",
"It creates each `Element` of the Study `Arm` at a time.\n",
"\n",
"Finally, the `study design plan` is shown by serializing the `ISA Study Design Model` content as an `ISA_design` JSON document, which can be rendered in various ways (tables, figures)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Let's load the tools"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# If executing the notebooks on `Google Colab`,uncomment the following command \n",
"# and run it to install the required python libraries. Also, make the test datasets available.\n",
"\n",
"# !pip install -r requirements.txt"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"import datetime\n",
"from collections import OrderedDict\n",
"from isatools.model import (\n",
" Investigation,\n",
" Study,\n",
" OntologyAnnotation,\n",
" Sample,\n",
" Characteristic,\n",
" ProtocolParameter,\n",
" ParameterValue,\n",
" StudyFactor,\n",
" FactorValue\n",
")\n",
"\n",
"from bokeh.io import output_file, show\n",
"from bokeh.plotting import figure\n",
"from bokeh.models import ColumnDataSource, Range1d, BoxAnnotation, Label, Legend, LegendItem, LabelSet\n",
"from bokeh.models.tools import HoverTool\n",
"\n",
"import pandas as pd\n",
"import datetime as dt\n",
"\n",
"import holoviews as hv\n",
"from holoviews import opts, dim\n",
"# hv.extension('bokeh')\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Start by creating basic ISA Study metadata"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"investigation = Investigation()\n",
"study = Study(filename=\"s_study_xover.txt\")\n",
"study.identifier = 'S-Xover-1'\n",
"study.title = 'My Simple ISA Study'\n",
"study.description = \"We could alternataly use the class constructor's parameters to set some default \" \\\n",
" \"values at the time of creation, however we want to demonstrate how to use the \" \\\n",
" \"object's instance variables to set values.\"\n",
"study.submission_date = str(datetime.datetime.today())\n",
"study.public_release_date = str(datetime.datetime.today())\n",
"# study.sources = [Source(name=\"source1\")]\n",
"# study.samples = [Sample(name=\"sample1\")]\n",
"# study.protocols = [Protocol(name=\"sample collection\")]\n",
"# study.process_sequence = [Process(executes_protocol=study.protocols[-1], inputs=[study.sources[-1]], outputs=[study.samples[-1]])]\n",
"investigation.studies = [study]\n",
"# investigation"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"# from isatools.isatab import dumps\n",
"# print(dumps(investigation))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"import json\n",
"from isatools.isajson import ISAJSONEncoder\n",
"# print(json.dumps(investigation, cls=ISAJSONEncoder, sort_keys=True, indent=4, separators=(',', ': ')))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Let's load the new ISA create module"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"from isatools.create.model import (\n",
" Treatment,\n",
" NonTreatment,\n",
" StudyCell,\n",
" StudyArm,\n",
" ProductNode,\n",
" ProtocolNode,\n",
" AssayGraph,\n",
" SampleAndAssayPlan,\n",
" StudyDesign\n",
")\n",
"from isatools.create.constants import (\n",
" RUN_IN,\n",
" WASHOUT,\n",
" FOLLOW_UP,\n",
" SAMPLE,\n",
" EXTRACT,\n",
" DATA_FILE\n",
")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1. Creation of the first `ISA Study Design Element` and setting its type"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"nte1 = NonTreatment(element_type='screen', duration_unit=OntologyAnnotation(term=\"days\"))\n",
"print(nte1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 2. Creation of another `ISA Study Design Element`, of type `Treatment`"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"te1 = Treatment()\n",
"te1.type='biological intervention'\n",
"print(te1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 2.1 defining the first treatment as a vector of ISA factor values:\n",
"\n",
"Under \"ISA Study Design Create mode\", a `Study Design Element` of type `Treatment` needs to be defined by a vector of `Factors` and their respective associated `Factor Values`. This is done as follows:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"f1 = StudyFactor(name='virus', factor_type=OntologyAnnotation(term=\"organism\"))\n",
"f1v = FactorValue(factor_name=f1, value=\"hsv1\")\n",
"f2 = StudyFactor(name='dose', factor_type=OntologyAnnotation(term=\"quantity\"))\n",
"f2v = FactorValue(factor_name=f2, value='high dose')\n",
"f3 = StudyFactor(name='time post infection', factor_type=OntologyAnnotation(term=\"time\"))\n",
"f3v = FactorValue(factor_name=f3, value=2, unit=OntologyAnnotation(term='hr'))\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"#assigning the factor values declared above to the ISA treatment element\n",
"te1.factor_values = [f1v,f2v,f3v]\n",
"print(te1)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 3. Creation of a second `ISA Study Design Element`, of type `Treatment`, following the same pattern."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"te2 = Treatment()\n",
"te2.type = 'chemical intervention'\n",
"antivir = StudyFactor(name='antiviral', factor_type=OntologyAnnotation(term=\"chemical entity\"))\n",
"antivirv = FactorValue(factor_name=antivir, value='hsvflumab')\n",
"intensity = StudyFactor(name='dose', factor_type=OntologyAnnotation(term=\"quantity\"))\n",
"intensityv= FactorValue(factor_name=intensity, value = 10, unit=OntologyAnnotation(term='mg/kg/day'))\n",
"duration = StudyFactor(name = 'treatment duration', factor_type=OntologyAnnotation(term=\"time\"))\n",
"durationv = FactorValue(factor_name=duration, value=2, unit=OntologyAnnotation(term='weeks'))\n",
"te2.factor_values = [antivirv,intensityv,durationv]\n",
"print(te2)\n",
" \n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"te3 = Treatment()\n",
"te3.type = 'radiological intervention'\n",
"rays = StudyFactor(name='radiation', factor_type=OntologyAnnotation(term=\"physical entity\"))\n",
"raysv = FactorValue(factor_name=rays, value='neutron beam')\n",
"rays_intensity = StudyFactor(name='dose', factor_type=OntologyAnnotation(term=\"quantity\"))\n",
"rays_intensityv= FactorValue(factor_name=rays_intensity, value = '10', unit=OntologyAnnotation(term='mSev'))\n",
"rays_duration = StudyFactor(name = 'treatment duration', factor_type=OntologyAnnotation(term=\"time\"))\n",
"rays_durationv = FactorValue(factor_name=rays_duration, value='30', unit=OntologyAnnotation(term='minutes'))\n",
"te3.factor_values = [raysv,rays_intensityv,rays_durationv]\n",
"print(te3)\n",
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 4. Creation of 'wash out' period as an `ISA Study Design Element`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"# Creation of another ISA element, which is not a Treatment element, which is of type `screen` by default\n",
"nte2 = NonTreatment(duration_unit=OntologyAnnotation(term=\"days\"))\n",
"print(nte2.type)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"# let's change it by setting its type by relying on the keys defined for the object\n",
"nte2.type=RUN_IN\n",
"print(nte2.type)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"#let's change it again by direct use of the allowed strings (note: the string should match exactly the predefined values)\n",
"nte2.type = WASHOUT\n",
"print(nte2.type)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"# setting the factor values associated with 'default' DURATION Factor associated with such elements\n",
"nte2.duration.value=2\n",
"nte2.duration.unit=OntologyAnnotation(term=\"weeks\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 5. Creation of 'follow-up' period as an `ISA Study Design Element`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"nte3 = NonTreatment(element_type=FOLLOW_UP, duration_value=4, duration_unit=OntologyAnnotation(term=\"month\"))\n",
"# nte3.duration.value = 2\n",
"# nte3.duration.unit = 'months'\n",
"print(nte3)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 6. Creation of the associated container, known as an ISA `Cell` for each ISA `Element`.\n",
"In this example, a single `Element` is hosted by a `Cell`, which must be named. In more complex designs (e.g. study designs with assymetric arms), a `Cell` may contain more than one `Element`, hence the list attribute."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"st_cl1= StudyCell(name=\"st_cl1\", elements=[nte1])\n",
"st_cl2= StudyCell(name=\"st_cl2\", elements=[te1])\n",
"st_cl3= StudyCell(name=\"st_cl3\", elements=[nte2])\n",
"st_cl4= StudyCell(name=\"st_cl4\", elements=[te2])\n",
"st_cl6= StudyCell(name=\"st_cl6\", elements=[nte2])\n",
"st_cl7= StudyCell(name=\"st_cl7\", elements=[te3])\n",
"st_cl5= StudyCell(name=\"st_cl5\", elements=[nte3])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 7. Creation of an ISA `Study Arm` and setting the number of subjects associated to that unique sequence of ISA `Cell`s."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"arm1 = StudyArm(name='Arm 1', group_size=5, )\n",
"print(arm1)\n",
"\n",
"genotype_cat = OntologyAnnotation(term=\"genotype\")\n",
"genotype_value1 = OntologyAnnotation(term=\"control - normal\")\n",
"genotype_value2 = OntologyAnnotation(term=\"mutant\")\n",
"\n",
"arm1 = StudyArm(\n",
" name='Arm 1', \n",
" group_size=2, \n",
" source_type=Characteristic(\n",
" category=genotype_cat,\n",
" value=genotype_value1\n",
" )\n",
")\n",
"print(arm1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 8. Declaring an ISA `Sample Assay Plan`, defining which `Sample` are to be collected and which `Assay`s to be used"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"input_material1=ProductNode(id_=\"MAT1\", name=\"liver\", node_type=SAMPLE,size=1,characteristics=[Characteristic(category=OntologyAnnotation(term='organism part'), value=OntologyAnnotation(term='liver'))])\n",
"input_material2=ProductNode(id_=\"MAT2\", name=\"blood\", node_type=SAMPLE,size=1,characteristics=[Characteristic(category=OntologyAnnotation(term='organism part'), value=OntologyAnnotation(term='blood'))])\n",
"input_material3=ProductNode(id_=\"MAT3\", name=\"urine\", node_type=SAMPLE,size=3,characteristics=[Characteristic(category=OntologyAnnotation(term='organism part'), value=OntologyAnnotation(term='urine'))])\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 9. Loading an isa assay definition in the form of an ordered dictionary. \n",
"\n",
"- It corresponds to an ISA configuration assay table but expressed in JSON.\n",
"\n",
"- In this NMR assay there is a sample extraction step, which produces \"supernatant\" and \"pellet\" extracts (1 of each per input sample).\n",
"\n",
"- IMPORTANT: Note how ISA `OntologyAnnotation` elements are used in this data structure"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"nmr_assay_dict = OrderedDict([\n",
" ('measurement_type', OntologyAnnotation(term='metabolite profiling')),\n",
" ('technology_type', OntologyAnnotation(term='nmr spectroscopy')),\n",
" ('extraction', {}),\n",
" ('extract', [\n",
" {\n",
" 'node_type': EXTRACT,\n",
" 'characteristics_category': OntologyAnnotation(term='extract type'),\n",
" 'characteristics_value': 'supernatant',\n",
" 'size': 1,\n",
" 'technical_replicates': None,\n",
" 'is_input_to_next_protocols': True\n",
" },\n",
" {\n",
" 'node_type': EXTRACT,\n",
" 'characteristics_category': OntologyAnnotation(term='extract type'),\n",
" 'characteristics_value': 'pellet',\n",
" 'size': 1,\n",
" 'technical_replicates': None,\n",
" 'is_input_to_next_protocols': True\n",
" }\n",
" ]),\n",
" ('nmr_spectroscopy', {\n",
" OntologyAnnotation(term='instrument'): ['Bruker AvanceII 1 GHz'],\n",
" OntologyAnnotation(term='acquisition_mode'): ['1D 13C NMR','1D 1H NMR','2D 13C-13C NMR'],\n",
" OntologyAnnotation(term='pulse_sequence'): ['CPMG','TOCSY','HOESY','watergate']\n",
" }),\n",
" ('raw_spectral_data_file', [\n",
" {\n",
" 'node_type': DATA_FILE,\n",
" 'size': 1,\n",
" 'technical_replicates': 2,\n",
" 'is_input_to_next_protocols': False\n",
" }\n",
" ])\n",
" ])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 10. We now show how to create an new AssayGraph structure from scratch, as if we were defining a completely new assay type."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"new_assay_graph1=AssayGraph(\n",
" id_=\"WB\",\n",
" measurement_type=OntologyAnnotation(term=\"protein profiling\"),\n",
" technology_type=OntologyAnnotation(term=\"Western blot\")\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 11. We procede by assembling the Process graph:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"\n",
"protocol_node_protein = ProtocolNode(id_=\"P\",name='Protein extraction')\n",
"protocol_node_data_acq = ProtocolNode(\n",
" id_=\"DA\",\n",
" name='WB imaging',\n",
" parameter_values=[\n",
" ParameterValue(\n",
" category=ProtocolParameter(parameter_name=OntologyAnnotation(term=\"channel\")),\n",
" value=OntologyAnnotation(term=\"360 nm\")\n",
" ),\n",
" ParameterValue(\n",
" category=ProtocolParameter(parameter_name=OntologyAnnotation(term='channel')),\n",
" value=OntologyAnnotation(term=\"550 nm\")\n",
" )\n",
" ]\n",
")\n",
"\n",
"protein_char = Characteristic(category=OntologyAnnotation(term='material type'), value='protein extract')\n",
"protein_sample_node = ProductNode(id_=\"SP\", node_type=EXTRACT, size=1, characteristics=[protein_char])\n",
"wb_data_node = ProductNode(id_=\"WBD\", node_type=DATA_FILE, size=1)\n",
"\n",
"\n",
"nodes = [protein_sample_node, wb_data_node, protocol_node_protein, protocol_node_data_acq]\n",
"links = [\n",
" (protocol_node_protein, protein_sample_node),\n",
" (protein_sample_node, protocol_node_data_acq),\n",
" (protocol_node_data_acq, wb_data_node)\n",
"]\n",
"\n",
"new_assay_graph1.add_nodes(nodes)\n",
"new_assay_graph1.add_links(links)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The following step does 3 things:\n",
"\n",
"- generate an assay plan from the assay declaration data strucure\n",
"- create a `Sample and Assay Plan` object holding a list of samples and the list of assay workflows which have been declared\n",
"- create a `Sample to Assay` object, which details which sample will be input to a specific assay."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"nmr_assay_graph = AssayGraph.generate_assay_plan_from_dict(nmr_assay_dict)\n",
"\n",
"sap1 = SampleAndAssayPlan(\n",
" name='sap1',\n",
" sample_plan=[input_material1,input_material2,input_material3],\n",
" assay_plan=[new_assay_graph1,nmr_assay_graph]\n",
")\n",
"\n",
"sample2assay_plan = {\n",
" input_material3: [new_assay_graph1, nmr_assay_graph],\n",
" input_material2: [nmr_assay_graph],\n",
" input_material1: [nmr_assay_graph]\n",
"}\n",
"\n",
"sap1.sample_to_assay_map = sample2assay_plan"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"# specifying which sample type (sometimes referred to as specimen)\n",
"# sap1.add_sample_type('liver')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"# specifying how many times each specimen is supposed to be collected\n",
"# sap1.add_sample_plan_record('liver',3)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"#### 9. Declaration of an ISA assay and linking specimen type and data acquisition plan for this assay\n",
"# # declare the type of `Assay` which will be performed\n",
"# assay_type1 = Assay(measurement_type='metabolite profiling', technology_type='mass spectrometry')\n",
"# # associate this assay type to the `SampleAssayPlan`\n",
"# sap1.add_assay_type(assay_type1)\n",
"# # specify which `sample type` will be used as input to the declare `Assay`\n",
"# sap1.add_assay_plan_record('liver',assay_type1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 11. Build an ISA `Study Design Arm` by adding the first set of ISA `Cells` and setting the `Sample Assay Plan`"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"arm1.add_item_to_arm_map(st_cl1,sap1)\n",
"print(arm1.name, arm1.source_type)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 12 Now expanding the `Arm` by adding a new `Cell`, which uses the same `Sample Assay Plan` as the one used in Cell #1.\n",
"Of course, the `Sample Assay Plan` for this new `Cell` could be different. It would have to be to built as shown before."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"arm1.add_item_to_arm_map(st_cl2,sap1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"# Adding the last section of the Arm, with a cell which also uses the same sample assay plan.\n",
"arm1.add_item_to_arm_map(st_cl3,sap1)\n",
"arm1.add_item_to_arm_map(st_cl4,sap1)\n",
"arm1.add_item_to_arm_map(st_cl6,sap1)\n",
"arm1.add_item_to_arm_map(st_cl7,sap1)\n",
"arm1.add_item_to_arm_map(st_cl5,sap1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 13. Creation of additional ISA Study Arms and setting the number of subjects associated to that unique sequence of ISA Cells."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"arm2 = StudyArm(\n",
" name='Arm 2',\n",
" source_type=Characteristic(\n",
" category=genotype_cat,\n",
" value=genotype_value1\n",
" )\n",
")\n",
"arm2.group_size=5\n",
"arm2.add_item_to_arm_map(st_cl1,sap1)\n",
"arm2.add_item_to_arm_map(st_cl4,sap1)\n",
"arm2.add_item_to_arm_map(st_cl3,sap1)\n",
"arm2.add_item_to_arm_map(st_cl2,sap1)\n",
"arm2.add_item_to_arm_map(st_cl6,sap1)\n",
"arm2.add_item_to_arm_map(st_cl7,sap1)\n",
"arm2.add_item_to_arm_map(st_cl5,sap1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"arm3 = StudyArm(\n",
" name='Arm 3',\n",
" source_type=Characteristic(\n",
" category=genotype_cat,\n",
" value=genotype_value1\n",
" )\n",
")\n",
"arm3.group_size=5\n",
"arm3.add_item_to_arm_map(st_cl1,sap1)\n",
"arm3.add_item_to_arm_map(st_cl7,sap1)\n",
"arm3.add_item_to_arm_map(st_cl3,sap1)\n",
"arm3.add_item_to_arm_map(st_cl4,sap1)\n",
"arm3.add_item_to_arm_map(st_cl6,sap1)\n",
"arm3.add_item_to_arm_map(st_cl2,sap1)\n",
"arm3.add_item_to_arm_map(st_cl5,sap1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 14. We can now create the ISA `Study Design` object, which will receive the `Arms` defined by the user."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"study_design= StudyDesign(name='trial design #1')\n",
"# print(sd)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"# Adding a study arm to the study design object.\n",
"study_design.add_study_arm(arm1)\n",
"study_design.add_study_arm(arm2)\n",
"study_design.add_study_arm(arm3)\n",
"# print(sd)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"# Let's now serialize the ISA study design to JSON\n",
"import json\n",
"from isatools.isajson import ISAJSONEncoder\n",
"from isatools.create.model import StudyDesignEncoder\n",
"\n",
"f=json.dumps(study_design, cls=StudyDesignEncoder, sort_keys=True, indent=4, separators=(',', ': '))\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"scrolled": true
},
"source": [
"### 15. let's produce a graphical overview of the study design arms and the associated sample assay plans"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"def get_treatment_factors(some_element):\n",
" treat = \"\"\n",
" for j in range(len(some_element['factorValues'])):\n",
" if j < len(some_element['factorValues']) - 1:\n",
" if 'unit' in some_element['factorValues'][j].keys():\n",
" treat = treat + some_element['factorValues'][j]['factor']['name'].lower() + \": \" \\\n",
" + str(some_element['factorValues'][j]['value']) + \" \" \\\n",
" + str(some_element['factorValues'][j]['unit']['term'].lower()) + \", \"\n",
" else:\n",
" treat = treat + some_element['factorValues'][j]['factor']['name'].lower() + \": \" \\\n",
" + str(some_element['factorValues'][j]['value']) + \",\"\n",
" else:\n",
" if 'unit' in some_element['factorValues'][j].keys():\n",
" treat = treat + some_element['factorValues'][j]['factor']['name'].lower() + \": \" \\\n",
" + str(some_element['factorValues'][j]['value']) + \" \" \\\n",
" + str(some_element['factorValues'][j]['unit']['term'].lower())\n",
" else:\n",
" treat = treat + some_element['factorValues'][j]['factor']['name'].lower() + \": \" \\\n",
" + str(some_element['factorValues'][j]['value'])\n",
"\n",
" return treat"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"design = json.loads(json.dumps(study_design, cls=StudyDesignEncoder, sort_keys=True, indent=4, separators=(',', ': ')))\n",
"frames = []\n",
"Items = []\n",
"\n",
"# defining a color pallet for the different element types:\n",
"element_colors = {\"biological intervention\": \"rgb(253,232,37)\",\n",
" \"radiological intervention\": \"rgb(53, 155, 8)\",\n",
" \"dietary intervention\": \"rgb(53, 155, 8)\",\n",
" \"chemical intervention\": \"rgb(69, 13, 83)\",\n",
" \"washout\": \"rgb(45, 62, 120)\",\n",
" \"screen\": \"rgb(33, 144, 140)\",\n",
" \"run in\": \"rgb(43, 144, 180)\",\n",
" \"follow-up\": \"rgb(88, 189, 94)\",\n",
" \"concomitant treatment\": \"rgb(255, 255, 0)\"}\n",
"\n",
"# processing the study design arms and treatment plans:\n",
"for key in design[\"studyArms\"].keys():\n",
" DF = pd.DataFrame(columns=['Arm', 'Cell', 'Type', 'Start_date', 'End_date', 'Treatment', 'Color'])\n",
" arm_name = key\n",
" # print(\"arm: \", arm_name)\n",
" size = design[\"studyArms\"][key][\"groupSize\"]\n",
" size_annotation = \"n=\" + str(size)\n",
"\n",
" cells_per_arm = design[\"studyArms\"][key][\"cells\"]\n",
" cell_counter = 0\n",
" for cell in cells_per_arm:\n",
" cell_name = cell['name']\n",
" elements_per_cell = cell['elements']\n",
"\n",
" for element in elements_per_cell:\n",
" treat = \"\"\n",
" element_counter = 0 \n",
" if 'concomitantTreatments' in element.keys():\n",
" element_counter = element_counter + 1\n",
" treatments = []\n",
" for item in element['concomitantTreatments']:\n",
" treatment = get_treatment_factors(item)\n",
" treatments.append(treatment)\n",
" \n",
" concomitant = ','.join(treatments[0:-1])\n",
" concomitant = concomitant + ' and ' + treatments[-1]\n",
" array = [arm_name, cell_name, arm_name + \": [\" + concomitant + \"]_concomitant_\" + str(cell_counter),\n",
" dt.datetime(cell_counter + 2000, 1, 1), dt.datetime(cell_counter + 2000 + 1, 1, 1),\n",
" str(element['factorValues']),\n",
" concomitant,\n",
" element_colors[\"concomitant treatment\"]]\n",
" Items.append(array)\n",
"\n",
" elif 'type' in element.keys():\n",
" treatment = get_treatment_factors(element)\n",
" element_counter = element_counter + 1\n",
" array = [arm_name, cell_name, arm_name + \": [\" + str(element['type']) + \"]_\" + str(cell_counter),\n",
" dt.datetime((cell_counter + 2000), 1, 1), dt.datetime((cell_counter + 2000 + 1), 1, 1),\n",
" # str(element['factorValues']),\n",
" str(treatment),\n",
" element_colors[element['type']]]\n",
" Items.append(array)\n",
"\n",
" cell_counter = cell_counter + 1\n",
"\n",
"for i, Dat in enumerate(Items):\n",
" DF.loc[i] = Dat\n",
"# print(\"setting:\", DF.loc[i])\n",
"\n",
"# providing the canvas for the figure\n",
"# print(\"THESE ARE THE TYPES_: \", DF.Type.tolist())\n",
"fig = figure(title='Study Design Treatment Plan',\n",
" width=800,\n",
" height=400,\n",
" y_range=DF.Type.tolist(),\n",
" x_range=Range1d(DF.Start_date.min(), DF.End_date.max()),\n",
" tools='save')\n",
"\n",
"# adding a tool tip\n",
"hover = HoverTool(tooltips=\"Task: @Type
\\\n",
"Start: @Start_date
\\\n",
"Cell_Name: @Cell
\\\n",
"Treatment: @Treatment\")\n",
"fig.add_tools(hover)\n",
"\n",
"DF['ID'] = DF.index+0.8\n",
"# print(\"ID: \", DF['ID'])\n",
"DF['ID1'] = DF.index+1.2\n",
"# print(\"ID1: \", DF['ID1'])\n",
"CDS = ColumnDataSource(DF)\n",
"# , legend=str(size_annotation)\n",
"r = fig.quad(left='Start_date', right='End_date', bottom='ID', top='ID1', source=CDS, color=\"Color\")\n",
"fig.xaxis.axis_label = 'Time'\n",
"fig.yaxis.axis_label = 'study arms'\n",
"\n",
"# working at providing a background color change for every arm in the study design\n",
"counts = DF['Arm'].value_counts().tolist()\n",
"# print(\"total number of study arms:\", len(counts), \"| number of phases per arm:\", counts)\n",
"# box = []\n",
"# for i, this_element in enumerate(DF['Arm']):\n",
"# if i==0:\n",
"# box[i] = BoxAnnotation(bottom=0,\n",
"# top=DF['Arm'].value_counts().tolist()[0],\n",
"# fill_color=\"blue\")\n",
"# elif i % 2 == 0:\n",
"# box[i] = BoxAnnotation(bottom=DF['Arm'].value_counts().tolist()[0],\n",
"# top=DF['Arm'].value_counts().tolist()[0],\n",
"# fill_color=\"silver\")\n",
"# else:\n",
"# box[i] = BoxAnnotation(bottom=DF['Arm'].value_counts().tolist()[0],\n",
"# top=DF['Arm'].value_counts().tolist()[0] + DF['Arm'].value_counts().tolist()[1],\n",
"# fill_color=\"grey\",\n",
"# fill_alpha=0.1)\n",
"# # adding the background color for each arm:\n",
"# for element in box:\n",
"# fig.add_layout(element)\n",
"# # fig.add_layout(box2)\n",
"# # fig.add_layout(legend,'right')\n",
"\n",
"caption1 = Legend(items=[(str(size_annotation), [r])])\n",
"fig.add_layout(caption1, 'right')\n",
"\n",
"citation = Label(x=10, y=-80, x_units='screen', y_units='screen',\n",
" text='repeated measure group design layout - isa-api 0.12', render_mode='css',\n",
" border_line_color='gray', border_line_alpha=0.4,\n",
" background_fill_color='white', background_fill_alpha=1.0)\n",
"\n",
"fig.add_layout(citation)\n",
"\n",
"show(fig)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# This statement will take some time to execute. Be patients\n",
"study = study_design.generate_isa_study()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"len(study.assays)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"investigation.studies=[study]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# print(investigation.studies[0].assays[1])\n",
"print(investigation.studies[0].assays[0])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# WRITING ISA-JSON document to string\n",
"isa_json = json.dumps(investigation, cls=ISAJSONEncoder, sort_keys=True, indent=4, separators=(',', ': '))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# from isatools.isatab import dump_tables_to_dataframes as dumpdf\n",
"# dataframes = dumpdf(investigation)\n",
"# dataframes.keys()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"# Alternatevely, if you want to save the ISA-TAB files to a specific directory, you can run:\n",
"from isatools import isatab\n",
"# import os\n",
"# os.makedirs('/notebook-output/isa-repeated-measure-crossover-design', exist_ok = True)\n",
"isatab.dump(investigation, './notebook-output/isa-repeated-measure-crossover-design')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"len(dataframes.keys())"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"len(dataframes.keys())"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"dataframes[list(dataframes.keys())[1]]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"[x for x in study.assays[0].graph.nodes() if isinstance(x, Sample)]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"len([x for x in study.assays[0].graph.nodes() if isinstance(x, Sample)])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"[getattr(x, 'name', None) for x in study.assays[0].graph.nodes()]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## About this notebook\n",
"\n",
"- authors: philippe.rocca-serra@oerc.ox.ac.uk, massimiliano.izzo@oerc.ox.ac.uk\n",
"- license: CC-BY 4.0\n",
"- support: isatools@googlegroups.com\n",
"- issue tracker: https://github.com/ISA-tools/isa-api/issues"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.10"
}
},
"nbformat": 4,
"nbformat_minor": 4
}