{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "### Perform volcano plot analysis" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Perform volcano plot analysis using data for named metabolites from the metabolomics workbench or uploaded data files." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
Note: This notebook contains IPython widgets. Consequently, you won't be able to use Kernal/Restart & Restart command to automatically execute all cells in the notebook. You must use Run command individually to execute each cell and advance to the next cell.
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Import Python modules..." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "from __future__ import print_function\n", "\n", "import os\n", "import sys\n", "import time\n", "import re\n", "\n", "import requests\n", "\n", "import pandas as pd\n", "import matplotlib.pyplot as plt\n", "import seaborn as sns\n", "import numpy as np\n", "\n", "import scipy\n", "import statsmodels.stats.multitest\n", "\n", "import ipywidgets as widgets\n", "\n", "from IPython.display import display, HTML\n", "from IPython import __version__ as ipyVersion\n", "\n", "# Import MW modules from the current directory or default Python directory...\n", "import MWUtil\n", "\n", "%matplotlib inline\n", "\n", "print(\"Python: %s.%s.%s\" % sys.version_info[:3])\n", "print(\"IPython: %s\" % ipyVersion)\n", "print(\"ipywidgets: %s\" % widgets.__version__)\n", "\n", "print()\n", "print(time.asctime())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The URL PATH\n", "\n", "The MW REST URL consists of three main parts, separated by forward slashes, after the common prefix specifying the invariant base URL (https://www.metabolomicsworkbench.org/rest/):\n", "\n", "https://www.metabolomicsworkbench.org/rest/context/input_specification/output_specification\n", "\n", "Part 1: The context determines the type of data to be accessed from the Metabolomics Workbench, such as metadata or results related to the submitted studies, data from metabolites, genes/proteins and analytical chemistry databases as well as other services related to mass spectrometry and metabolite identification:\n", "\n", "context = study | compound | refmet | gene | protein | moverz | exactmass\n", "\n", "Part 2: The input specification consists of two required parameters describing the REST request:\n", "\n", "input_specification = input_item/input_value\n", "\n", "Part 3: The output specification consists of two parameters describing the output generated by the REST request:\n", "\n", "output_specification = output_item/(output_format)\n", "\n", "The first parameter is required in most cases. The second parameter is optional. The input and output specifications are context sensitive. The context determines the values allowed for the remaining parameters in the input and output specifications as detailed in the sections below.\n", "\n", "Setup MW REST base URL..." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "MWBaseURL = \"https://www.metabolomicsworkbench.org/rest\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Retrieve or upload data for named metabolites...**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "# Initialize data...\n", "StudiesResultsData = None\n", "RetrievedMWData = None" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "# Set up UIF info text...\n", "TopInfoTextHTML = widgets.HTML(value = \"Retrieve or upload data and process any missing values\", \n", " placeholder='', description='')\n", "\n", "# Setup UIF to process any missing values...\n", "MissingValuesMethods = [\"NoAction\", \"DeleteRows\", \"DeleteColumns\", \"ReplaceByColumnMean\", \"ReplaceColumnMedian\", \"ReplaceByZero\" , \"LinearInterpolation\"]\n", "MissingValuesMethodsDropdown = widgets.Dropdown(options = MissingValuesMethods,\n", " value = \"NoAction\",\n", " description = \" \")\n", "ProcessMissingValueTopTextHTML = widgets.HTML(value = \"Method for processing missing values:\", \n", " placeholder='', description='')\n", "\n", "# Setup UIF to retrieve...\n", "StudyIDText = widgets.Text(value = \"ST000001 ST000002\", description = \"Study ID (s)\",\n", " placeholder = \"Type study ID\", disabled = False,\n", " layout = widgets.Layout(margin='0 10px 0 0'))\n", "RetrieveDataBtn = widgets.Button(description = 'Retrieve Data', disabled = False, button_stype = '',\n", " tooltip = \"Retrieve data for study ID\")\n", "\n", "RetrieveDataOutput = widgets.Output()\n", "def RetrieveDataBtnEventHandler(Object):\n", " global StudiesResultsData, RetrievedMWData\n", " \n", " RetrievedMWData = True\n", " StudiesResultsData = None\n", " \n", " StudyIDs = StudyIDText.value\n", " MissingValuesMethod = MissingValuesMethodsDropdown.value\n", " \n", " RetrieveDataOutput.clear_output()\n", " UploadDataOutput.clear_output()\n", " with RetrieveDataOutput:\n", " if len(StudyIDs):\n", " print(\"\\nProcessing study ID(s): %s\" % StudyIDs)\n", " StudiesResultsData = MWUtil.RetrieveStudiesAnalysisAndResultsData(StudyIDs, MWBaseURL, MissingValuesMethod)\n", " DisplayData = False if len(StudiesResultsData.keys()) > 5 else True\n", " MWUtil.ListStudiesAnalysisAndResultsData(StudiesResultsData, DisplayDataFrame = DisplayData,\n", " IPythonDisplayFuncRef = display, IPythonHTMLFuncRef = HTML)\n", " else:\n", " print(\"\\nNo study ID(s) specified...\")\n", "\n", "RetrieveDataBtn.on_click(RetrieveDataBtnEventHandler)\n", "\n", "# Setup UIF to upload data file(s)...\n", "FileUploadBtn = widgets.FileUpload(description = 'Upload File(s)', accept='.csv,.txt,.tsv', multiple = True,\n", " disabled = False)\n", "FileUploadTextHTML = widgets.HTML(value = \"File format: Col 1: Sample names; \\\n", " Col 2: Class identifiers; Remaining cols: Named metabolites; \\\n", " Exts: .csv, .txt, or .tsv\", placeholder='', description='')\n", "\n", "UploadDataOutput = widgets.Output()\n", "def FileUploadBtnEventHandler(Change):\n", " global StudiesResultsData, RetrievedMWData\n", " \n", " RetrievedMWData = False\n", " StudiesResultsData = None\n", " \n", " MissingValuesMethod = MissingValuesMethodsDropdown.value\n", " UploadedDataInfo = FileUploadBtn.value\n", " \n", " RetrieveDataOutput.clear_output()\n", " UploadDataOutput.clear_output()\n", " with UploadDataOutput:\n", " StudiesResultsData = MWUtil.RetrieveUploadedData(UploadedDataInfo, MissingValuesMethod)\n", " DisplayData = False if len(StudiesResultsData.keys()) > 5 else True\n", " MWUtil.ListStudiesAnalysisAndResultsData(StudiesResultsData, DisplayDataFrame = DisplayData,\n", " IPythonDisplayFuncRef = display, IPythonHTMLFuncRef = HTML)\n", " \n", "FileUploadBtn.observe(FileUploadBtnEventHandler, names = 'value')\n", "\n", "# Setup UIF to retrieve or upload data file...\n", "\n", "DataWarningTextHTML = widgets.HTML(value = \"
Warning: Don't re-run the current cell after specifying study ID(s) or selecting file(s) and retrieving the data. Click on the next cell to advance.
\", placeholder='', description='')\n", "OrTextHTML = widgets.HTML(value = \"Or\", placeholder='', description='')\n", "\n", "UIFDataBoxes = []\n", "UIFDataBoxes.append(widgets.HBox([TopInfoTextHTML]))\n", "UIFDataBoxes.append(widgets.HBox([ProcessMissingValueTopTextHTML, MissingValuesMethodsDropdown]))\n", "UIFDataBoxes.append(widgets.HBox([StudyIDText, RetrieveDataBtn],\n", " layout = widgets.Layout(margin='10px 0 0 0')))\n", "UIFDataBoxes.append(widgets.HBox([OrTextHTML]))\n", "UIFDataBoxes.append(widgets.HBox([FileUploadBtn]))\n", "UIFDataBoxes.append(widgets.HBox([FileUploadTextHTML]))\n", "UIFDataBoxes.append(widgets.HBox([DataWarningTextHTML]))\n", "\n", "for UIFDataBox in UIFDataBoxes:\n", " display(UIFDataBox)\n", "\n", "display(RetrieveDataOutput)\n", "display(UploadDataOutput)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "MWUtil.CheckAndWarnEmptyStudiesData(StudiesResultsData, RetrievedMWData, StudyIDText.value)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Setup UIF for selecting and plotting available data..." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "# Setup UIF data...\n", "StudiesUIFData = MWUtil.SetupUIFDataForStudiesAnalysisAndResults(StudiesResultsData, MinClassCount = 2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "MWUtil.CheckAndWarnEmptyStudiesUIFData(StudiesUIFData, RetrievedMWData, StudyIDText.value)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "# Setup a function to generate data for volcano plots...\n", "def GenerateVolcanoPlotData(DataFrame, FirstClassNum, SecondClassNum, ClassNumColID = \"ClassNum\", ContainsClassCol = True):\n", " \"\"\"Generate data for volcano plots.\"\"\"\n", " \n", " VolcanoPlotDataFrame = None\n", " ErrMsg = None\n", " \n", " with np.errstate(all = 'raise'):\n", " try:\n", " VolcanoPlotDataFrame = _GenerateVolcanoPlotData(DataFrame, FirstClassNum, SecondClassNum, ClassNumColID, ContainsClassCol)\n", " except FloatingPointError as NPErrMsg:\n", " ErrMsg = \"Failed to generate data for volcano plot: %s\" % NPErrMsg\n", " \n", " return (VolcanoPlotDataFrame, ErrMsg)\n", " \n", "def _GenerateVolcanoPlotData(DataFrame, FirstClassNum, SecondClassNum, ClassNumColID = \"ClassNum\", ContainsClassCol = True):\n", " \"\"\"Generate data for volcano plots.\"\"\"\n", " \n", " # Drop Class column...\n", " if ContainsClassCol:\n", " DataFrame = DataFrame.drop('Class', axis = 1)\n", " \n", " # Extract data for two specified classes...\n", " DataA = DataFrame[DataFrame[ClassNumColID] == FirstClassNum]\n", " DataB = DataFrame[DataFrame[ClassNumColID] == SecondClassNum]\n", " \n", " # Drop ClassNum columns from extracted data...\n", " DataA = DataA.drop(ClassNumColID, axis = 1)\n", " DataB = DataB.drop(ClassNumColID, axis = 1)\n", " \n", " # Tranform data...\n", " print(\"Transforming data...\")\n", " DataA = np.log2(DataA)\n", " DataB = np.log2(DataB)\n", " \n", " # Calculate statistics...\n", " print(\"Calculating statistics\")\n", " TStatistics, PValues = scipy.stats.ttest_ind(DataA, DataB, equal_var = False, nan_policy = 'omit')\n", " TStatistics = TStatistics.tolist()\n", " PValues = PValues.tolist()\n", " \n", " # Adjust P-values..\n", " print(\"Adjusting P-values...\")\n", " Rejects, AdjustedPValues = statsmodels.stats.multitest.fdrcorrection(PValues, alpha=0.05, method='indep', is_sorted=False)\n", " AdjustedPValues = AdjustedPValues.tolist()\n", " \n", " # Calculate fold change...\n", " MeanA = DataA.mean(axis = 0)\n", " MeanB = DataB.mean(axis = 0)\n", " Log2FoldChange = MeanB.subtract(MeanA)\n", " Log2FoldChange = Log2FoldChange.tolist()\n", " \n", " # Cast np array to a list...\n", " Log10PValues = -np.log10(PValues)\n", " Log10PValues = Log10PValues.tolist()\n", " \n", " VolcanoPlotDataFrame = pd.DataFrame(\n", " [Log2FoldChange, PValues, Log10PValues, AdjustedPValues, TStatistics],\n", " columns = DataA.columns,\n", " index=['log2(FoldChange)', 'P-value', '-log10(P-value)', 'AdjustedP-value', 't-Statistic']).transpose()\n", " \n", " return VolcanoPlotDataFrame\n", " \n", "def ListSignificantMetabolitesByVolcanoPlotData(StudyID, AnalysisID, VolcanoPlotDataFrame,\n", " LogFoldChangeColID =\"log2(FoldChange)\",\n", " PValueColID = \"P-value\", \n", " LogFoldChangeThreshold = 1.0, \n", " PValueThreshold = 0.05):\n", " \"\"\"List significant metabolites.\"\"\"\n", " \n", " SignificantMetabolitesDataFrame = VolcanoPlotDataFrame[VolcanoPlotDataFrame[PValueColID] <= PValueThreshold]\n", " SignificantMetabolitesDataFrame = SignificantMetabolitesDataFrame[np.abs(SignificantMetabolitesDataFrame[LogFoldChangeColID]) >= LogFoldChangeThreshold]\n", " \n", " # Sort by log2(FoldChange) in ascending order...\n", " SignificantMetabolitesDataFrame = SignificantMetabolitesDataFrame.sort_values(by = [LogFoldChangeColID], ascending = True)\n", " \n", " print(\"\\nMetabolites changes at P-value <= %s and abs(log2(FoldChange)) > %s:\" % (PValueThreshold, LogFoldChangeThreshold))\n", " \n", " if RetrievedMWData:\n", " FileName = \"%s_%s_Significant_Changes_Data.csv\" % (StudyID, AnalysisID)\n", " else:\n", " FileRoot, FileExt = os.path.splitext(StudyID)\n", " FileName = \"%s_Significant_Changes_Data.csv\" % (FileRoot)\n", " \n", " HTMLText = MWUtil.SetupCSVDownloadLink(SignificantMetabolitesDataFrame, Title = \"Download foldchange data\", CSVFilename = FileName)\n", " display(HTML(HTMLText))\n", " \n", " display(HTML(SignificantMetabolitesDataFrame.to_html()))\n", " \n", "# Setup a function to draw volcano plot...\n", "def DrawVolcanoPlot(VolcanoPlotDataFrame, LogFoldChangeColID =\"log2(FoldChange)\", PValueColID = \"P-value\", \n", " LogPValueColID =\"-log10(P-value)\", LogFoldChangeThreshold = 1.0, PValueThreshold = 0.05,\n", " PlotStyle = \"darkgrid\", FontScale = 1.3, TitleFontWeight = \"bold\", LabelsFontWeight = \"bold\",\n", " PlotWidth = 9, PlotHeight = 6):\n", " \n", " # Setup color for data points...\n", " DataFrame = VolcanoPlotDataFrame\n", " \n", " # Significant up foldchange...\n", " ColorColID = 'Color'\n", " DataFrame.loc[(DataFrame[LogFoldChangeColID] >= LogFoldChangeThreshold) \n", " & (DataFrame[PValueColID] <= PValueThreshold), ColorColID] = \"red\"\n", " # Significant down foldchange...\n", " DataFrame.loc[(DataFrame[LogFoldChangeColID] <= -LogFoldChangeThreshold) \n", " & (DataFrame[PValueColID] <= PValueThreshold), ColorColID] = \"blue\"\n", " # Significant change...\n", " DataFrame.loc[(DataFrame[LogFoldChangeColID] >= -LogFoldChangeThreshold)\n", " & (DataFrame[LogFoldChangeColID] <= LogFoldChangeThreshold)\n", " & (DataFrame[PValueColID] <= PValueThreshold), ColorColID] = \"orange\"\n", " # Intermediate change...\n", " DataFrame[ColorColID].fillna('purple', inplace = True)\n", " \n", " ColorsPalette = {\"red\" : \"red\", \"blue\" : \"blue\", \"orange\" : \"orange\", \"purple\" : \"purple\"}\n", " \n", " sns.set(rc = {'figure.figsize':(PlotWidth, PlotHeight)})\n", " sns.set(style = PlotStyle, font_scale = FontScale)\n", " \n", " Axis = sns.scatterplot(x = LogFoldChangeColID, y = LogPValueColID, hue = ColorColID, data = VolcanoPlotDataFrame,\n", " palette = ColorsPalette, legend = False)\n", "\n", " # Draw vertical lines at LogFoldChangeThreshold...\n", " Axis.axvline(-LogFoldChangeThreshold, color = 'violet', linewidth = 1, linestyle = \"dashed\")\n", " Axis.axvline(LogFoldChangeThreshold, color = 'violet', linewidth = 1, linestyle = \"dashed\")\n", " \n", " # Draw a horizontal line at -log10(PValueThreshold)\n", " HLinePos = -np.log10(PValueThreshold)\n", " Axis.axhline(HLinePos, color = 'green', linewidth = 1, linestyle = \"dashed\")\n", " \n", " # Set title and labels...\n", " Axis.set_title(\"Volcano Plot\", fontweight = TitleFontWeight)\n", " Axis.set_xlabel(LogFoldChangeColID, fontweight = LabelsFontWeight)\n", " Axis.set_ylabel(LogPValueColID , fontweight = LabelsFontWeight)\n", " \n", " plt.show()\n", " \n", " # Drop color column...\n", " DataFrame.drop(ColorColID, axis = 1, inplace = True)\n", " " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": false }, "outputs": [], "source": [ "# Setup UIF...\n", "FirstStudyID = StudiesUIFData[\"StudyIDs\"][0]\n", "StudiesDropdown = widgets.Dropdown(options = StudiesUIFData[\"StudyIDs\"], value = FirstStudyID, \n", " description=\"Study:\", disabled = False)\n", "\n", "FirstAnalysisID = StudiesUIFData[\"AnalysisIDs\"][FirstStudyID][0]\n", "AnalysisDropdown = widgets.Dropdown(options = StudiesUIFData[\"AnalysisIDs\"][FirstStudyID], value = FirstAnalysisID,\n", " description = \"Analysis:\", disabled = False)\n", "\n", "FirstClassID = StudiesUIFData[\"ClassIDs\"][FirstStudyID][FirstAnalysisID][0]\n", "FirstClassDropdown = widgets.Dropdown(options = StudiesUIFData[\"ClassIDs\"][FirstStudyID][FirstAnalysisID],\n", " value = FirstClassID, description = \"Class A:\", disabled = False)\n", "\n", "SecondClassID = StudiesUIFData[\"ClassIDs\"][FirstStudyID][FirstAnalysisID][1]\n", "SecondClassDropdown = widgets.Dropdown(options = StudiesUIFData[\"ClassIDs\"][FirstStudyID][FirstAnalysisID],\n", " value = SecondClassID, description = \"Class B:\", disabled = False)\n", "\n", "FirstClassNum = StudiesResultsData[FirstStudyID][FirstAnalysisID][\"class_names_to_nums\"][FirstClassID]\n", "SecondClassNum = StudiesResultsData[FirstStudyID][FirstAnalysisID][\"class_names_to_nums\"][SecondClassID]\n", "\n", "VolcanoPlotDataFrame, ErrMsg = GenerateVolcanoPlotData(StudiesResultsData[FirstStudyID][FirstAnalysisID][\"data_frame\"],\n", " FirstClassNum, SecondClassNum,\n", " ClassNumColID = \"ClassNum\", ContainsClassCol = True)\n", "\n", "def GetMaxPValue(VolcanoPlotDataFrame):\n", " return \"%.2f\" % np.max(VolcanoPlotDataFrame[PValueColID]) if VolcanoPlotDataFrame is not None else DefaultMaxPValue\n", "\n", "def GetMaxLogFoldChange(VolcanoPlotDataFrame):\n", " return \"%.1f\" % np.max(VolcanoPlotDataFrame[LogFoldChangeColID]) if VolcanoPlotDataFrame is not None else DefaultMaxLogFoldChange\n", "\n", "\n", "PValueColID = \"P-value\"\n", "DefaultPValue = \"0.05\"\n", "DefaultMaxPValue = \"5.00\"\n", "MinPValue = \"0.01\"\n", "MaxPValue = GetMaxPValue(VolcanoPlotDataFrame)\n", "PValueSlider = widgets.FloatSlider(value = DefaultPValue, min = MinPValue, max = MaxPValue, step = 0.01,\n", " description = 'P-value:', disabled = False,\n", " continuous_update = False, orientation = 'horizontal',\n", " readout = True, readout_format = '.2f')\n", "\n", "LogFoldChangeColID = \"log2(FoldChange)\"\n", "DefaultLogFoldChangeValue = \"1.0\"\n", "DefaultMaxLogFoldChange = \"5.0\"\n", "MinLogFoldChange = \"0.1\"\n", "MaxLogFoldChange = GetMaxLogFoldChange(VolcanoPlotDataFrame)\n", "LogFoldChangeSlider = widgets.FloatSlider(value = DefaultLogFoldChangeValue, min = MinLogFoldChange, max = MaxLogFoldChange,\n", " step = 0.1, description = 'log2(FoldChange):', disabled = False,\n", " continuous_update = False, orientation = 'horizontal',\n", " readout_format = '.1f')\n", "\n", "PlotStyles = [\"Darkgrid\", \"Whitegrid\", \"Dark\", \"White\", \"Ticks\"]\n", "PlotStylesDropdown = widgets.Dropdown(options = PlotStyles, value = \"Darkgrid\", description = \"Plot style:\")\n", "\n", "DefaultPlotWidth = 9\n", "DefaultPlotHeight = 6\n", "\n", "PlotSizeText = widgets.Text(value = \"9x6\", description = \"Plot size:\", placeholder = \"Type WxH; Hit enter\",\n", " disabled = False, continuous_update=False)\n", "\n", "DataLayout = widgets.Layout(margin='0 0 4px 0')\n", "StudiesDataHBox = widgets.HBox([StudiesDropdown, AnalysisDropdown], layout = DataLayout)\n", "ClassesDataHBox = widgets.HBox([FirstClassDropdown, SecondClassDropdown], layout = DataLayout)\n", "SlidersDataHBox = widgets.HBox([PValueSlider, LogFoldChangeSlider], layout = DataLayout)\n", "PlotsDataHBox = widgets.HBox([PlotStylesDropdown, PlotSizeText], layout = DataLayout)\n", "\n", "Output = widgets.Output()\n", "OutputPlot = widgets.Output()\n", "\n", "UpdatePlot = True\n", "\n", "def DisablePlotUpdate():\n", " global UpdatePlot\n", " UpdatePlot = False\n", "\n", "def EnablePlotUpdate():\n", " global UpdatePlot\n", " UpdatePlot = True\n", "\n", "def GetUpdatePlotStatus():\n", " global UpdatePlot\n", " return True if UpdatePlot else False\n", "\n", "# Setup function to update dropdown options...\n", "def UpdateAnalysisDropdown(StudyID):\n", " AnalysisDropdown.options = StudiesUIFData[\"AnalysisIDs\"][StudyID]\n", " AnalysisDropdown.value = StudiesUIFData[\"AnalysisIDs\"][StudyID][0]\n", " \n", "def UpdateFirstClassDropdown(StudyID, AnalysisID):\n", " FirstClassDropdown.options = StudiesUIFData[\"ClassIDs\"][StudyID][AnalysisID]\n", " FirstClassDropdown.value = StudiesUIFData[\"ClassIDs\"][StudyID][AnalysisID][0]\n", "\n", "def UpdateSecondClassDropdown(StudyID, AnalysisID):\n", " SecondClassDropdown.options = StudiesUIFData[\"ClassIDs\"][StudyID][AnalysisID]\n", " SecondClassDropdown.value = StudiesUIFData[\"ClassIDs\"][StudyID][AnalysisID][1]\n", "\n", "def UpdateSliders(StudyID, AnalysisID):\n", " FirstClassID = FirstClassDropdown.value\n", " SecondClassID = SecondClassDropdown.value\n", " \n", " FirstClassNum = StudiesResultsData[StudyID][AnalysisID][\"class_names_to_nums\"][FirstClassID]\n", " SecondClassNum = StudiesResultsData[StudyID][AnalysisID][\"class_names_to_nums\"][SecondClassID]\n", " \n", " VolcanoPlotDataFrame, ErrMsg = GenerateVolcanoPlotData(StudiesResultsData[StudyID][AnalysisID][\"data_frame\"],\n", " FirstClassNum, SecondClassNum,\n", " ClassNumColID = \"ClassNum\", ContainsClassCol = True)\n", "\n", " MinPValue = \"0.01\"\n", " MaxPValue = GetMaxPValue(VolcanoPlotDataFrame)\n", " PValueSlider.value = DefaultPValue\n", " PValueSlider.min = MinPValue\n", " PValueSlider.max = MaxPValue\n", "\n", " MinLogFoldChange = \"0.1\"\n", " MaxLogFoldChange = GetMaxLogFoldChange(VolcanoPlotDataFrame)\n", " LogFoldChangeSlider.value = DefaultLogFoldChangeValue\n", " LogFoldChangeSlider.min = MinLogFoldChange\n", " LogFoldChangeSlider.max = MaxLogFoldChange \n", " \n", "# Setup dropdown event handlers...\n", "def StudiesDropdownEventHandler(Change):\n", " StudyID = Change[\"new\"]\n", " \n", " DisablePlotUpdate()\n", " UpdateAnalysisDropdown(StudyID)\n", " UpdateFirstClassDropdown(StudyID, StudiesUIFData[\"AnalysisIDs\"][StudyID][0])\n", " UpdateSecondClassDropdown(StudyID, StudiesUIFData[\"AnalysisIDs\"][StudyID][0])\n", " \n", " EnablePlotUpdate()\n", " \n", " PlotData()\n", "\n", "def AnalysisDropdownEventHandler(Change):\n", " UpdatePlotStatus = GetUpdatePlotStatus()\n", " if UpdatePlotStatus:\n", " DisablePlotUpdate()\n", " \n", " UpdateFirstClassDropdown(StudiesDropdown.value, Change[\"new\"])\n", " UpdateSecondClassDropdown(StudiesDropdown.value, Change[\"new\"])\n", " \n", " UpdateSliders(StudiesDropdown.value, Change[\"new\"])\n", " \n", " if UpdatePlotStatus:\n", " EnablePlotUpdate()\n", " \n", " PlotData()\n", "\n", "def FirstClassDropdownEventHandler(Change):\n", " PlotData()\n", "\n", "def SecondClassDropdownEventHandler(Change):\n", " PlotData()\n", "\n", "def PValueSliderEventHandler(Change):\n", " PlotData()\n", "\n", "def LogFoldChangeSliderEventHandler(Change):\n", " PlotData()\n", " \n", "def PlotStylesDropdownEventHandler(Change):\n", " PlotData()\n", " \n", "def PlotSizeTextEventHandler(Change):\n", " PlotData()\n", " \n", "# Bind required event handlers...\n", "StudiesDropdown.observe(StudiesDropdownEventHandler, names = 'value')\n", "AnalysisDropdown.observe(AnalysisDropdownEventHandler, names = 'value')\n", "FirstClassDropdown.observe(FirstClassDropdownEventHandler, names = 'value')\n", "\n", "SecondClassDropdown.observe(SecondClassDropdownEventHandler, names = 'value')\n", "FirstClassDropdown.observe(FirstClassDropdownEventHandler, names = 'value')\n", "\n", "PValueSlider.observe(PValueSliderEventHandler, names='value')\n", "LogFoldChangeSlider.observe(LogFoldChangeSliderEventHandler, names='value')\n", "\n", "PlotStylesDropdown.observe(PlotStylesDropdownEventHandler, names = 'value')\n", "PlotSizeText.observe(PlotSizeTextEventHandler, names = 'value')\n", " \n", "# Set up function to generate volcano plot...\n", "def PlotData():\n", " if not UpdatePlot:\n", " return\n", " \n", " Output.clear_output()\n", " OutputPlot.clear_output()\n", " \n", " StudyID = StudiesDropdown.value\n", " AnalysisID = AnalysisDropdown.value\n", " FirstClassID = FirstClassDropdown.value\n", " SecondClassID = SecondClassDropdown.value\n", " DataFrame = StudiesResultsData[StudyID][AnalysisID][\"data_frame\"]\n", " \n", " PValue = PValueSlider.value\n", " LogFoldChange = LogFoldChangeSlider.value\n", " \n", " # First and second class IDs must be different...\n", " FirstClassNum = StudiesResultsData[StudyID][AnalysisID][\"class_names_to_nums\"][FirstClassID]\n", " SecondClassNum = StudiesResultsData[StudyID][AnalysisID][\"class_names_to_nums\"][SecondClassID]\n", " if FirstClassNum == SecondClassNum:\n", " with Output:\n", " print(\"Selected classes must be different\\nClassNum: %s; Class Name: %s\\n\" % (FirstClassNum, FirstClassID))\n", " ListClassInformation(StudyID, AnalysisID)\n", " return\n", " \n", " Style = PlotStylesDropdown.value\n", " Style = Style.lower()\n", " \n", " PlotSize = PlotSizeText.value.lower()\n", " PlotSize = re.sub(\" \", \"\", PlotSize)\n", " PlotSizeWords = PlotSize.split(\"x\")\n", " \n", " if len(PlotSizeWords) == 2 and len(PlotSizeWords[0]) > 0 and len(PlotSizeWords[1]) > 0:\n", " Width = float(PlotSizeWords[0])\n", " Height = float(PlotSizeWords[1])\n", " else:\n", " Width = DefaultPlotWidth\n", " Height = DefaultHeight\n", " with Output:\n", " print(\"Invalid plot size; Using default plot size: %sx%s\\n\" % (Width, Height))\n", " \n", " with OutputPlot:\n", " # Retrieve data for volcano plot...\n", " VolcanoPlotDataFrame, ErrMsg = GenerateVolcanoPlotData(DataFrame, FirstClassNum, SecondClassNum,\n", " ClassNumColID = \"ClassNum\", ContainsClassCol = True)\n", " if VolcanoPlotDataFrame is None:\n", " if ErrMsg is not None:\n", " print(\"%s\" % ErrMsg)\n", " print(\"\\nTry data for another analysis or study...\")\n", " FileName = \"%s_%s_Data.csv\" % (StudyID, AnalysisID)\n", " HTMLText = MWUtil.SetupCSVDownloadLink(DataFrame, Title = \"Download and review data\", CSVFilename = FileName)\n", " display(HTML(HTMLText))\n", " return\n", " \n", " # Generate volcano plot...\n", " DrawVolcanoPlot(VolcanoPlotDataFrame, PlotStyle = Style, PlotWidth = Width, PlotHeight = Height,\n", " PValueThreshold = PValue, LogFoldChangeThreshold = LogFoldChange)\n", " \n", " with Output:\n", " print(\"Selected classes:\")\n", " print(\"Class A - ClassNum: %s; ClassName: %s\" % (FirstClassNum, FirstClassID))\n", " print(\"Class B - ClassNum: %s; ClassName: %s\" % (SecondClassNum, SecondClassID))\n", " print(\"\\n\")\n", " \n", " MWUtil.ListClassInformation(StudiesResultsData, StudyID, AnalysisID, RetrievedMWData)\n", " \n", " if RetrievedMWData:\n", " FileName = \"%s_%s_Data.csv\" % (StudyID, AnalysisID)\n", " HTMLText = MWUtil.SetupCSVDownloadLink(DataFrame, Title = \"Download data\", CSVFilename = FileName)\n", " display(HTML(HTMLText))\n", " \n", " ListSignificantMetabolitesByVolcanoPlotData(StudyID, AnalysisID, VolcanoPlotDataFrame, PValueThreshold = PValue,\n", " LogFoldChangeThreshold = LogFoldChange)\n", "\n", "\n", "display(StudiesDataHBox)\n", "display(ClassesDataHBox)\n", "display(SlidersDataHBox)\n", "display(PlotsDataHBox)\n", "\n", "display(OutputPlot)\n", "display(Output)\n", "\n", "PlotData()\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.8.5" } }, "nbformat": 4, "nbformat_minor": 2 }