{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Interactive Visualisation of Toffee Data\n", "\n", "Through the `ToffeeFragmentsPlotter` we can start to explore the raw data contained within a toffee file. These plots are specifically aimed at extracting the same data that one might use when calculating scores in a DIA-pipeline such as `OpenMSToffee`. There are also highly useful for manually validating results, for example when investigating missing values in a broader downstream analysis." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "%load_ext autoreload\n", "%autoreload 2" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/vnd.plotly.v1+html": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import os\n", "\n", "from IPython.display import SVG\n", "\n", "import pandas as pd\n", "\n", "import plotly\n", "import plotly.offline as po\n", "\n", "import toffee\n", "from toffee.util import calculate_isotope_mz\n", "from toffee.viz import ToffeeFragmentsPlotter\n", "\n", "po.init_notebook_mode(connected=True)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'0.12.18'" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "toffee.__version__" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "base_dir = os.environ.get('DIA_TEST_DATA_REPO', None)\n", "assert base_dir is not None" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For this purpose, we are specifically using a spectral library to define the precursor/product mass over charge ratios" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "columns = [\n", " 'PrecursorMz',\n", " 'ProductMz',\n", " 'LibraryIntensity',\n", " 'ProteinId',\n", " 'PeptideSequence',\n", " 'ModifiedPeptideSequence',\n", " 'TransitionGroupId',\n", " 'TransitionId'\n", "]\n", "srl = pd.read_csv(base_dir + '/ProCan90/srl/hek_srl.OpenSwath.iRT.tsv', sep='\\t')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "so that we can calculate the C12/C13 isotope offsets and use these in our visualisation." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "n_isotopes = 2\n", "precursors, products = calculate_isotope_mz(\n", " precursor_and_product_df=srl,\n", " n_isotopes=n_isotopes,\n", " sort_by_intensity=True,\n", ")\n", "precursors.set_index('Id', inplace=True)\n", "products.set_index('GroupId', inplace=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Constructing the plotters is very simple and requires a path to an existing toffee file." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "plotter = ToffeeFragmentsPlotter(\n", " base_dir + '/ProCan90/tof/ProCan90-M06-07.tof',\n", " use_ms1=True,\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Finally, we can extract out the mass over charge values for specific transition groups and plot these out. In this example, we have picked out the retention time of the peak so that the plots can be zoomed in" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "scrolled": false }, "outputs": [ { "data": { "image/svg+xml": [ "120014001600180020002200010k20kTPVISGGPYYER_2Retention Time (s)XICMass over chargeMS1: 669.84+1 iso+2 isoMS2: 928.42+1 iso+2 isoMS2: 1041.50+1 iso+2 isoMS2: 1140.57+1 iso+2 isoMS2: 841.38+1 iso+2 isoMS2: 784.36+1 iso+2 isoMS2: 727.34+1 iso+2 iso" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "image/svg+xml": [ "28003000320034003600380005k10kLLPSESALLPAPGSPYGR_2Retention Time (s)XICMass over chargeMS1: 913.00+1 iso+2 isoMS2: 901.45+1 iso+2 isoMS2: 1014.54+1 iso+2 isoMS2: 733.36+1 iso+2 isoMS2: 1127.62+1 iso+2 isoMS2: 698.37+1 iso+2 isoMS2: 1285.69+1 iso+2 iso" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "image/svg+xml": [ "01000200030004000500005k10kLLPSESALLPAPGSPYGR_2Retention Time (s)XICMass over chargeMS1: 913.00+1 iso+2 isoMS2: 901.45+1 iso+2 isoMS2: 1014.54+1 iso+2 isoMS2: 733.36+1 iso+2 isoMS2: 1127.62+1 iso+2 isoMS2: 698.37+1 iso+2 isoMS2: 1285.69+1 iso+2 iso" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "image/svg+xml": [ "1200140016001800200022000500100015002000ILAAEESVGTMGNR_2Retention Time (s)XICMass over chargeMS1: 724.36+1 iso+2 isoMS2: 1221.55+1 iso+2 isoMS2: 950.44+1 iso+2 isoMS2: 821.39+1 iso+2 isoMS2: 635.29+1 iso+2 isoMS2: 1150.52+1 iso+2 isoMS2: 1079.48+1 iso+2 iso" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "transition_groups = [\n", " ('TPVISGGPYYER_2', 1779.903),\n", " ('LLPSESALLPAPGSPYGR_2', 3304.652),\n", " ('LLPSESALLPAPGSPYGR_2', None),\n", " ('ILAAEESVGTMGNR_2', 1664.507),\n", "]\n", "\n", "for transition_group, retention_time_line in transition_groups:\n", " precursors_tg = precursors.loc[transition_group].IsotopeMz.tolist()\n", " products_tg = products.loc[transition_group].IsotopeMz.tolist()[:6 * (1 + n_isotopes)]\n", "\n", " zoom = True and retention_time_line is not None\n", "\n", "# output_fname = None\n", " output_fname = f'toffee_fragments.{transition_group}-zoom_{zoom}.svg'\n", " kwargs = dict(\n", " psm_name=transition_group,\n", " number_of_isotopes=n_isotopes,\n", " retention_time_line=retention_time_line,\n", " output_fname=output_fname,\n", " )\n", "\n", " if zoom:\n", " delta_rt = 600\n", " kwargs['retention_time_zoom'] = [retention_time_line - delta_rt, retention_time_line + delta_rt]\n", " kwargs['aspect_scale'] = 0.15\n", "\n", " figure = plotter.create_plotly_figure(\n", " precursors_tg,\n", " products_tg,\n", " **kwargs,\n", " )\n", " if output_fname is not None:\n", " display(SVG(output_fname))\n", " os.remove(output_fname)\n", " elif figure is not None:\n", " po.iplot(figure, show_link=False)" ] } ], "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.6.7" } }, "nbformat": 4, "nbformat_minor": 2 }