{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "670c8d0c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "import lumicks.pylake as lk\n",
    "\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "718e2e36",
   "metadata": {},
   "source": [
    "# Force Clamp - Kinesin Attached to a Bead Walking on Microtubule\n",
    "\n",
    "[Download this page as a Jupyter notebook](https://lumicks-pylake.readthedocs.io/en/v0.13.3/_downloads/0530dd4983b95b6fa4614d8f65a21ab2/kinesin_closed_loop.ipynb)\n",
    "\n",
    "In this assay we had microtubules on the surface. We trapped beads with Kinesin (molecular motor) and had ATP inside the assay. As we lowered the kinesin-coated beads on top of a microtubule, it attached to it and started stepping on the microtubule. Kinesins were pulling the bead out of the center of the trap and thus increasing the force on the bead.\n",
    "\n",
    "At a certain (set) force, we turn the force clamp on and the nanostage moves towards the motion of the bead. Now the force stays constant, and we get an idea of how the motor steps by looking at the motion of the nanostage.\n",
    "\n",
    "With the IRM, you can see unlabeled microtubules and the kinesin-coated bead on top of one of them.\n",
    "\n",
    "Open the file:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ff6b6e0a",
   "metadata": {},
   "outputs": [],
   "source": [
    "file = lk.File(\"20190215-142512 Marker force clamp.h5\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fafca8c5",
   "metadata": {},
   "source": [
    "Load the data:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "349a2799",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Force in the y direction (pN)\n",
    "forcey = file['Force HF']['Force 1y']['6s':'8.5s']\n",
    "# Nanostage position in the y direction (V)\n",
    "nanoy = file['Diagnostics']['Nano Y']['6s':'8.5s']"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c4e0cc63",
   "metadata": {},
   "source": [
    "Downsample the data:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d30a3f60",
   "metadata": {},
   "outputs": [],
   "source": [
    "sample_rate = file['Diagnostics']['Nano Y'].sample_rate\n",
    "downsampled_rate = 100 # Hz\n",
    "\n",
    "# downsample the force, nanostage position and time\n",
    "forcey_downsamp = forcey.downsampled_by(int(sample_rate/downsampled_rate))\n",
    "nanoy_downsamp = nanoy.downsampled_by(int(sample_rate/downsampled_rate))\n",
    "time_downsamp = forcey_downsamp.timestamps/1e9\n",
    "time_downsamp = time_downsamp - time_downsamp[0]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7a1b83e5",
   "metadata": {},
   "source": [
    "Conversion factor for the nanostage:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d953f870",
   "metadata": {},
   "outputs": [],
   "source": [
    "# this is determined for each nanostage and it has 3 different conversion factors for the 3 directions (x,y,z)\n",
    "conv_fact = 50000/(1.849-0.04933) #nm/V"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a0b3094b",
   "metadata": {},
   "source": [
    "## Force versus Time\n",
    "\n",
    "Plot it:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e4d11e68",
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.figure(figsize=(10, 5))\n",
    "forcey.plot()\n",
    "forcey_downsamp.plot(color='r')\n",
    "plt.ylabel('Force 1y (pN)')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "31fd3277",
   "metadata": {},
   "source": [
    "Determine force fluctuations:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9f83770e",
   "metadata": {},
   "outputs": [],
   "source": [
    "print('Mean force is: '+str(np.mean(forcey_downsamp.data)) + ' pN' )\n",
    "print('Variation in the force is: '+str(np.std(forcey_downsamp.data)) + ' pN' )"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dc2bb454",
   "metadata": {},
   "source": [
    "Here we see that the force stay at 1.7 pN and stays relatively constant.\n",
    "\n",
    "## Nanostage Position versus Time\n",
    "\n",
    "Plot it:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5977f68a",
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = plt.figure(figsize=(5,5))\n",
    "\n",
    "# plot position versus time\n",
    "ax = plt.subplot(1,1,1)\n",
    "plt.plot(time_downsamp, nanoy_downsamp.data*conv_fact-2000)\n",
    "\n",
    "plt.xlim([0, 2])\n",
    "plt.ylim([60, 160])\n",
    "\n",
    "# create y-ticks for axis\n",
    "lims2=[]\n",
    "for i in range(14):\n",
    "    lims2.append(i*8+60)\n",
    "ax.set_yticks(lims2)\n",
    "\n",
    "# add grid to the graph\n",
    "ax.yaxis.grid()\n",
    "\n",
    "# label axis\n",
    "ax.set_xlabel('Time (s)')\n",
    "plt.ylabel('Nanostage position (nm)')"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 5
}