{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Minimal pipeline example\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import anndata\nimport lineageot\nimport numpy as np\n\nrng = np.random.default_rng()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Creating data\n\nFirst we make a minimal fake AnnData object to run LineageOT on.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "t1 = 5;\nt2 = 10;\n\nn_cells_1 = 5;\nn_cells_2 = 10;\nn_cells = n_cells_1 + n_cells_2;\n\nn_genes = 5;\n\nbarcode_length = 10;\n\nadata = anndata.AnnData(X = np.random.rand(n_cells, n_genes),\n                        obs = {\"time\" : np.concatenate([t1*np.ones(n_cells_1), t2*np.ones(n_cells_2)])},\n                        obsm = {\"barcodes\" : rng.integers(low = -1, high = 10, size = (n_cells, barcode_length))}\n                       )"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Fitting a lineage tree\n\nBefore running LineageOT, we need to build a lineage tree from the observed barcodes.\nThis step is not optimized. We provide an implementation of a heuristic algorithm called neighbor joining.\nFeel free to use your own preferred tree construction algorithm.\nYou can import a tree saved in Newick format with ``lineageot.read_newick``.\n\nThe tree should be formatted as a NetworkX ``DiGraph`` in the same way as the output of ``lineageot.fit_tree()``\nEach node is annotated with ``'time'`` (which indicates either the time of sampling (for observed cells) or the time of division (for unobserved ancestors).\nEdges are directed from parent to child and are annotated with ``'time'`` equal to the child node's ``'time_to_parent'``.\nObserved node indices correspond to their row in ``adata[adata.obs['time'] == t2]``. \n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "lineage_tree_t2 = lineageot.fit_tree(adata[adata.obs['time'] == t2], t2)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Running LineageOT\n\nOnce we have a lineage tree annotated with time, we can compute a LineageOT coupling.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "coupling = lineageot.fit_lineage_coupling(adata, t1, t2, lineage_tree_t2)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Saving \nThe LineageOT package does not include functionality for downstream analysis and plotting.\nWe recommend transitioning to other packages, like `Waddington-OT <https://broadinstitute.github.io/wot/>`_, after computing a coupling.\nThis saves the fitted coupling in a format Waddington-OT can import.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "lineageot.save_coupling_as_tmap(coupling, t1, t2, './tmaps/example')"
      ]
    }
  ],
  "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.7.9"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}