{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Plotting with `pyft`" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "DataTransformerRegistry.enable('vegafusion')" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pyft\n", "import pandas as pd\n", "import altair as alt\n", "\n", "alt.data_transformers.enable(\"vegafusion\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Read in the results of a ft-footprint calculation and plot the results using `pyft`." ] }, { "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", "
chrommotif_startmotif_endstrandfootprint_codesfire_qualfiber_namehas_spanning_mspfootprintedstartendcentering_positioncentering_strandtype
0chr1152049465204981+3247m64076_211222_124721/148505307/ccsTrueTrue015204946+footprinted
1chr1152049465204981+2-1m64076_211222_124721/51053256/ccsFalseTrue015204946+not-footprinted
\n", "
" ], "text/plain": [ " chrom motif_start motif_end strand footprint_codes fire_qual \\\n", "0 chr11 5204946 5204981 + 3 247 \n", "1 chr11 5204946 5204981 + 2 -1 \n", "\n", " fiber_name has_spanning_msp footprinted start \\\n", "0 m64076_211222_124721/148505307/ccs True True 0 \n", "1 m64076_211222_124721/51053256/ccs False True 0 \n", "\n", " end centering_position centering_strand type \n", "0 1 5204946 + footprinted \n", "1 1 5204946 + not-footprinted " ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dfm = pyft.utils.read_and_center_footprint_table(\n", " \"../../../tests/data/ctcf-footprints.bed.gz\"\n", ")\n", "dfm.head(2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Read in fiber data centered on the footprint locations. " ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "[2024-11-12T22:29:48Z INFO pyft::fiberdata] 181 records fetched in 0.01s\n", "[2024-11-12T22:29:48Z INFO pyft::fiberdata] Fiberdata made for 181 records in 0.11s\n", "[2024-11-12T22:29:48Z INFO pyft::fiberdata] Fiberdata centered for 181 records in 0.02s\n", "[2024-11-12T22:29:48Z INFO pyft::fiberdata] 172 records fetched in 0.11s\n", "[2024-11-12T22:29:48Z INFO pyft::fiberdata] Fiberdata made for 172 records in 0.10s\n", "[2024-11-12T22:29:48Z INFO pyft::fiberdata] Fiberdata centered for 172 records in 0.07s\n" ] } ], "source": [ "rgns = pd.read_csv(\"../../../tests/data/ctcf.bed.gz\", sep=\"\\t\", header=None, nrows=2)\n", "rgns.columns = [\"chrom\", \"start\", \"end\", \"name\", \"score\", \"strand\", \"name2\"]\n", "fiberbam = pyft.Fiberbam(\"../../../tests/data/ctcf.bam\")\n", "centers = []\n", "z = None\n", "for idx, rgn in rgns.iterrows():\n", " region = (rgn[\"chrom\"], rgn[\"start\"], rgn[\"end\"])\n", " z = pyft.utils.region_to_centered_df(\n", " fiberbam, region, strand=rgn[\"strand\"], max_flank=250\n", " )\n", " centers.append(z)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Combine the footprinting results with the fiber data centered around the footprints. " ] }, { "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", "
chromfiber_startfiber_endfiber_namestrandtypestartendqualcentering_positioncentering_strandmotif_startmotif_endfootprint_codesfire_qualhas_spanning_mspfootprinted
0chr115184260.05205600.0m64076_211222_124721/148505307/ccs+msp-225-16005204946+NaNNaNNaNNaNNaNNaN
1chr115184260.05205600.0m64076_211222_124721/148505307/ccs+msp-571352475204946+NaNNaNNaNNaNNaNNaN
\n", "
" ], "text/plain": [ " chrom fiber_start fiber_end fiber_name strand \\\n", "0 chr11 5184260.0 5205600.0 m64076_211222_124721/148505307/ccs + \n", "1 chr11 5184260.0 5205600.0 m64076_211222_124721/148505307/ccs + \n", "\n", " type start end qual centering_position centering_strand motif_start \\\n", "0 msp -225 -160 0 5204946 + NaN \n", "1 msp -57 135 247 5204946 + NaN \n", "\n", " motif_end footprint_codes fire_qual has_spanning_msp footprinted \n", "0 NaN NaN NaN NaN NaN \n", "1 NaN NaN NaN NaN NaN " ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "both_dfs = pd.concat(centers + [dfm], axis=0).reset_index(drop=True)\n", "both_dfs.head(2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Show the chart within the notebook." ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "
\n", "" ], "text/plain": [ "alt.LayerChart(...)" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "chart = pyft.plot.centered_chart(both_dfs, width=400, height=200)\n", "chart" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Save the chart as a html file and open it in your browser.\n" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "chart.save(\"/Users/mrvollger/Desktop/chart.html\")" ] } ], "metadata": { "kernelspec": { "display_name": ".env", "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.13.0" } }, "nbformat": 4, "nbformat_minor": 2 }