{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "scanpy==1.6.0 anndata==0.7.4 umap==0.4.6 numpy==1.18.5 scipy==1.4.1 pandas==1.1.3 scikit-learn==0.23.2 statsmodels==0.12.0 python-igraph==0.7.1 leidenalg==0.8.2\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "import dandelion as ddl\n",
    "import pandas as pd\n",
    "import scanpy as sc\n",
    "sc.logging.print_header()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dandelion==0.0.27.post2 pandas==1.1.3 numpy==1.18.5 matplotlib==3.3.2 networkx==2.5 scipy==1.4.1 skbio==0.5.6\n"
     ]
    }
   ],
   "source": [
    "ddl.logging.print_header()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'/lustre/scratch117/cellgen/team297/kt16/newcastle_covid/notebooks'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "os.getcwd()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "os.chdir('/lustre/scratch117/cellgen/team297/kt16/newcastle_covid')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/nfs/users/nfs_k/kt16/miniconda2/envs/dandelion/lib/python3.7/site-packages/anndata/_core/anndata.py:1094: FutureWarning: is_categorical is deprecated and will be removed in a future version.  Use is_categorical_dtype instead\n"
     ]
    }
   ],
   "source": [
    "bdata = sc.read_h5ad('h5ad/covid_jan_2021_bcells.h5ad')\n",
    "bdata = bdata[bdata.obs['Collection_Day'] == 'D0'].copy()\n",
    "bdata = bdata[bdata.obs['bcr_QC_pass'] == 'True'].copy()\n",
    "bvdj = ddl.read_h5('dandelion_output/covid_jan_2021_bcells_vdj.h5')\n",
    "ddl.tl.transfer(bdata, bvdj)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>sample_id</th>\n",
       "      <th>Status_on_day_collection_summary</th>\n",
       "      <th>Sex</th>\n",
       "      <th>v_call_heavy</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>cells</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>AAACGGGCACATTAGC-MH9179824</th>\n",
       "      <td>MH9179824</td>\n",
       "      <td>Moderate</td>\n",
       "      <td>Female</td>\n",
       "      <td>IGHV1-8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>AAAGATGCATAGAAAC-MH9179824</th>\n",
       "      <td>MH9179824</td>\n",
       "      <td>Moderate</td>\n",
       "      <td>Female</td>\n",
       "      <td>IGHV3-11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>AAATGCCAGGCATGGT-MH9179824</th>\n",
       "      <td>MH9179824</td>\n",
       "      <td>Moderate</td>\n",
       "      <td>Female</td>\n",
       "      <td>IGHV3-23</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>AACCATGGTCCAAGTT-MH9179824</th>\n",
       "      <td>MH9179824</td>\n",
       "      <td>Moderate</td>\n",
       "      <td>Female</td>\n",
       "      <td>IGHV3-30</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>AACCATGGTGCAGGTA-MH9179824</th>\n",
       "      <td>MH9179824</td>\n",
       "      <td>Moderate</td>\n",
       "      <td>Female</td>\n",
       "      <td>IGHV2-70</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>BGCV15_TCTTCGGGTTAAGGGC-1</th>\n",
       "      <td>BGCV15_CV0944</td>\n",
       "      <td>Healthy</td>\n",
       "      <td>Male</td>\n",
       "      <td>IGHV3-30</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>BGCV15_TGACTAGTCCCAGGTG-1</th>\n",
       "      <td>BGCV15_CV0944</td>\n",
       "      <td>Healthy</td>\n",
       "      <td>Male</td>\n",
       "      <td>IGHV3-9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>BGCV15_TTAGGACCAGCTTCGG-1</th>\n",
       "      <td>BGCV15_CV0944</td>\n",
       "      <td>Healthy</td>\n",
       "      <td>Male</td>\n",
       "      <td>IGHV4-39</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>BGCV15_TTGCCGTCACTTAAGC-1</th>\n",
       "      <td>BGCV15_CV0944</td>\n",
       "      <td>Healthy</td>\n",
       "      <td>Male</td>\n",
       "      <td>IGHV3-7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>BGCV15_TTTCCTCAGAATGTTG-1</th>\n",
       "      <td>BGCV15_CV0944</td>\n",
       "      <td>Healthy</td>\n",
       "      <td>Male</td>\n",
       "      <td>IGHV1-8</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>43917 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                                sample_id Status_on_day_collection_summary  \\\n",
       "cells                                                                        \n",
       "AAACGGGCACATTAGC-MH9179824      MH9179824                         Moderate   \n",
       "AAAGATGCATAGAAAC-MH9179824      MH9179824                         Moderate   \n",
       "AAATGCCAGGCATGGT-MH9179824      MH9179824                         Moderate   \n",
       "AACCATGGTCCAAGTT-MH9179824      MH9179824                         Moderate   \n",
       "AACCATGGTGCAGGTA-MH9179824      MH9179824                         Moderate   \n",
       "...                                   ...                              ...   \n",
       "BGCV15_TCTTCGGGTTAAGGGC-1   BGCV15_CV0944                          Healthy   \n",
       "BGCV15_TGACTAGTCCCAGGTG-1   BGCV15_CV0944                          Healthy   \n",
       "BGCV15_TTAGGACCAGCTTCGG-1   BGCV15_CV0944                          Healthy   \n",
       "BGCV15_TTGCCGTCACTTAAGC-1   BGCV15_CV0944                          Healthy   \n",
       "BGCV15_TTTCCTCAGAATGTTG-1   BGCV15_CV0944                          Healthy   \n",
       "\n",
       "                               Sex v_call_heavy  \n",
       "cells                                            \n",
       "AAACGGGCACATTAGC-MH9179824  Female      IGHV1-8  \n",
       "AAAGATGCATAGAAAC-MH9179824  Female     IGHV3-11  \n",
       "AAATGCCAGGCATGGT-MH9179824  Female     IGHV3-23  \n",
       "AACCATGGTCCAAGTT-MH9179824  Female     IGHV3-30  \n",
       "AACCATGGTGCAGGTA-MH9179824  Female     IGHV2-70  \n",
       "...                            ...          ...  \n",
       "BGCV15_TCTTCGGGTTAAGGGC-1     Male     IGHV3-30  \n",
       "BGCV15_TGACTAGTCCCAGGTG-1     Male      IGHV3-9  \n",
       "BGCV15_TTAGGACCAGCTTCGG-1     Male     IGHV4-39  \n",
       "BGCV15_TTGCCGTCACTTAAGC-1     Male      IGHV3-7  \n",
       "BGCV15_TTTCCTCAGAATGTTG-1     Male      IGHV1-8  \n",
       "\n",
       "[43917 rows x 4 columns]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test = bdata.obs[['sample_id', 'Status_on_day_collection_summary', 'Sex', 'v_call_heavy']]\n",
    "test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>v_call_heavy</th>\n",
       "      <th>IGHV3-23</th>\n",
       "      <th>IGHV3-30</th>\n",
       "      <th>IGHV4-59</th>\n",
       "      <th>IGHV4-39</th>\n",
       "      <th>IGHV4-34</th>\n",
       "      <th>IGHV1-69</th>\n",
       "      <th>IGHV3-21</th>\n",
       "      <th>IGHV3-48</th>\n",
       "      <th>IGHV1-18</th>\n",
       "      <th>IGHV3-33</th>\n",
       "      <th>IGHV3-7</th>\n",
       "      <th>IGHV3-15</th>\n",
       "      <th>IGHV1-2</th>\n",
       "      <th>IGHV5-51</th>\n",
       "      <th>IGHV3-30-3</th>\n",
       "      <th>IGHV4-4</th>\n",
       "      <th>IGHV3-11</th>\n",
       "      <th>IGHV3-9</th>\n",
       "      <th>IGHV1-46</th>\n",
       "      <th>IGHV4-31</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>sample_id</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>AP1</th>\n",
       "      <td>0.102190</td>\n",
       "      <td>0.043796</td>\n",
       "      <td>0.029197</td>\n",
       "      <td>0.043796</td>\n",
       "      <td>0.021898</td>\n",
       "      <td>0.036496</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.116788</td>\n",
       "      <td>0.021898</td>\n",
       "      <td>0.014599</td>\n",
       "      <td>0.043796</td>\n",
       "      <td>0.087591</td>\n",
       "      <td>0.065693</td>\n",
       "      <td>0.021898</td>\n",
       "      <td>0.029197</td>\n",
       "      <td>0.014599</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.043796</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.051095</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>AP2</th>\n",
       "      <td>0.121739</td>\n",
       "      <td>0.139130</td>\n",
       "      <td>0.104348</td>\n",
       "      <td>0.182609</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.034783</td>\n",
       "      <td>0.139130</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.060870</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.034783</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>AP3</th>\n",
       "      <td>0.220049</td>\n",
       "      <td>0.039120</td>\n",
       "      <td>0.066015</td>\n",
       "      <td>0.056235</td>\n",
       "      <td>0.114914</td>\n",
       "      <td>0.007335</td>\n",
       "      <td>0.031785</td>\n",
       "      <td>0.088020</td>\n",
       "      <td>0.014670</td>\n",
       "      <td>0.044010</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.034230</td>\n",
       "      <td>0.014670</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.007335</td>\n",
       "      <td>0.009780</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.012225</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>AP4</th>\n",
       "      <td>0.109606</td>\n",
       "      <td>0.067734</td>\n",
       "      <td>0.048030</td>\n",
       "      <td>0.061576</td>\n",
       "      <td>0.043103</td>\n",
       "      <td>0.016010</td>\n",
       "      <td>0.049261</td>\n",
       "      <td>0.036946</td>\n",
       "      <td>0.041872</td>\n",
       "      <td>0.017241</td>\n",
       "      <td>0.038177</td>\n",
       "      <td>0.027094</td>\n",
       "      <td>0.030788</td>\n",
       "      <td>0.040640</td>\n",
       "      <td>0.025862</td>\n",
       "      <td>0.023399</td>\n",
       "      <td>0.023399</td>\n",
       "      <td>0.028325</td>\n",
       "      <td>0.017241</td>\n",
       "      <td>0.024631</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>AP5</th>\n",
       "      <td>0.077851</td>\n",
       "      <td>0.015351</td>\n",
       "      <td>0.038377</td>\n",
       "      <td>0.036184</td>\n",
       "      <td>0.040570</td>\n",
       "      <td>0.035088</td>\n",
       "      <td>0.040570</td>\n",
       "      <td>0.031798</td>\n",
       "      <td>0.054825</td>\n",
       "      <td>0.035088</td>\n",
       "      <td>0.040570</td>\n",
       "      <td>0.029605</td>\n",
       "      <td>0.035088</td>\n",
       "      <td>0.038377</td>\n",
       "      <td>0.033991</td>\n",
       "      <td>0.008772</td>\n",
       "      <td>0.029605</td>\n",
       "      <td>0.035088</td>\n",
       "      <td>0.024123</td>\n",
       "      <td>0.013158</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>newcastle21</th>\n",
       "      <td>0.073171</td>\n",
       "      <td>0.091463</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.030488</td>\n",
       "      <td>0.073171</td>\n",
       "      <td>0.036585</td>\n",
       "      <td>0.024390</td>\n",
       "      <td>0.036585</td>\n",
       "      <td>0.067073</td>\n",
       "      <td>0.030488</td>\n",
       "      <td>0.042683</td>\n",
       "      <td>0.042683</td>\n",
       "      <td>0.018293</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.042683</td>\n",
       "      <td>0.024390</td>\n",
       "      <td>0.048780</td>\n",
       "      <td>0.036585</td>\n",
       "      <td>0.073171</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>newcastle49</th>\n",
       "      <td>0.104895</td>\n",
       "      <td>0.034965</td>\n",
       "      <td>0.034965</td>\n",
       "      <td>0.076923</td>\n",
       "      <td>0.097902</td>\n",
       "      <td>0.062937</td>\n",
       "      <td>0.034965</td>\n",
       "      <td>0.027972</td>\n",
       "      <td>0.069930</td>\n",
       "      <td>0.027972</td>\n",
       "      <td>0.020979</td>\n",
       "      <td>0.034965</td>\n",
       "      <td>0.027972</td>\n",
       "      <td>0.041958</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.006993</td>\n",
       "      <td>0.027972</td>\n",
       "      <td>0.013986</td>\n",
       "      <td>0.027972</td>\n",
       "      <td>0.090909</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>newcastle59</th>\n",
       "      <td>0.140496</td>\n",
       "      <td>0.074380</td>\n",
       "      <td>0.082645</td>\n",
       "      <td>0.049587</td>\n",
       "      <td>0.049587</td>\n",
       "      <td>0.008264</td>\n",
       "      <td>0.066116</td>\n",
       "      <td>0.016529</td>\n",
       "      <td>0.008264</td>\n",
       "      <td>0.024793</td>\n",
       "      <td>0.049587</td>\n",
       "      <td>0.024793</td>\n",
       "      <td>0.057851</td>\n",
       "      <td>0.082645</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.049587</td>\n",
       "      <td>0.033058</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.008264</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>newcastle65</th>\n",
       "      <td>0.130000</td>\n",
       "      <td>0.050000</td>\n",
       "      <td>0.080000</td>\n",
       "      <td>0.060000</td>\n",
       "      <td>0.030000</td>\n",
       "      <td>0.010000</td>\n",
       "      <td>0.050000</td>\n",
       "      <td>0.060000</td>\n",
       "      <td>0.050000</td>\n",
       "      <td>0.010000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.040000</td>\n",
       "      <td>0.100000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.030000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.030000</td>\n",
       "      <td>0.070000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.020000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>newcastle74</th>\n",
       "      <td>0.107317</td>\n",
       "      <td>0.102439</td>\n",
       "      <td>0.026829</td>\n",
       "      <td>0.029268</td>\n",
       "      <td>0.031707</td>\n",
       "      <td>0.041463</td>\n",
       "      <td>0.039024</td>\n",
       "      <td>0.046341</td>\n",
       "      <td>0.046341</td>\n",
       "      <td>0.019512</td>\n",
       "      <td>0.048780</td>\n",
       "      <td>0.031707</td>\n",
       "      <td>0.031707</td>\n",
       "      <td>0.039024</td>\n",
       "      <td>0.034146</td>\n",
       "      <td>0.017073</td>\n",
       "      <td>0.021951</td>\n",
       "      <td>0.019512</td>\n",
       "      <td>0.012195</td>\n",
       "      <td>0.009756</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>125 rows × 20 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "v_call_heavy  IGHV3-23  IGHV3-30  IGHV4-59  IGHV4-39  IGHV4-34  IGHV1-69  \\\n",
       "sample_id                                                                  \n",
       "AP1           0.102190  0.043796  0.029197  0.043796  0.021898  0.036496   \n",
       "AP2           0.121739  0.139130  0.104348  0.182609  0.000000  0.034783   \n",
       "AP3           0.220049  0.039120  0.066015  0.056235  0.114914  0.007335   \n",
       "AP4           0.109606  0.067734  0.048030  0.061576  0.043103  0.016010   \n",
       "AP5           0.077851  0.015351  0.038377  0.036184  0.040570  0.035088   \n",
       "...                ...       ...       ...       ...       ...       ...   \n",
       "newcastle21   0.073171  0.091463  0.000000  0.030488  0.073171  0.036585   \n",
       "newcastle49   0.104895  0.034965  0.034965  0.076923  0.097902  0.062937   \n",
       "newcastle59   0.140496  0.074380  0.082645  0.049587  0.049587  0.008264   \n",
       "newcastle65   0.130000  0.050000  0.080000  0.060000  0.030000  0.010000   \n",
       "newcastle74   0.107317  0.102439  0.026829  0.029268  0.031707  0.041463   \n",
       "\n",
       "v_call_heavy  IGHV3-21  IGHV3-48  IGHV1-18  IGHV3-33   IGHV3-7  IGHV3-15  \\\n",
       "sample_id                                                                  \n",
       "AP1           0.000000  0.116788  0.021898  0.014599  0.043796  0.087591   \n",
       "AP2           0.139130  0.000000  0.060870  0.000000  0.000000  0.034783   \n",
       "AP3           0.031785  0.088020  0.014670  0.044010  0.000000  0.034230   \n",
       "AP4           0.049261  0.036946  0.041872  0.017241  0.038177  0.027094   \n",
       "AP5           0.040570  0.031798  0.054825  0.035088  0.040570  0.029605   \n",
       "...                ...       ...       ...       ...       ...       ...   \n",
       "newcastle21   0.024390  0.036585  0.067073  0.030488  0.042683  0.042683   \n",
       "newcastle49   0.034965  0.027972  0.069930  0.027972  0.020979  0.034965   \n",
       "newcastle59   0.066116  0.016529  0.008264  0.024793  0.049587  0.024793   \n",
       "newcastle65   0.050000  0.060000  0.050000  0.010000  0.000000  0.040000   \n",
       "newcastle74   0.039024  0.046341  0.046341  0.019512  0.048780  0.031707   \n",
       "\n",
       "v_call_heavy   IGHV1-2  IGHV5-51  IGHV3-30-3   IGHV4-4  IGHV3-11   IGHV3-9  \\\n",
       "sample_id                                                                    \n",
       "AP1           0.065693  0.021898    0.029197  0.014599  0.000000  0.043796   \n",
       "AP2           0.000000  0.000000    0.000000  0.000000  0.000000  0.000000   \n",
       "AP3           0.014670  0.000000    0.007335  0.009780  0.000000  0.000000   \n",
       "AP4           0.030788  0.040640    0.025862  0.023399  0.023399  0.028325   \n",
       "AP5           0.035088  0.038377    0.033991  0.008772  0.029605  0.035088   \n",
       "...                ...       ...         ...       ...       ...       ...   \n",
       "newcastle21   0.018293  0.000000    0.042683  0.024390  0.048780  0.036585   \n",
       "newcastle49   0.027972  0.041958    0.000000  0.006993  0.027972  0.013986   \n",
       "newcastle59   0.057851  0.082645    0.000000  0.049587  0.033058  0.000000   \n",
       "newcastle65   0.100000  0.000000    0.030000  0.000000  0.030000  0.070000   \n",
       "newcastle74   0.031707  0.039024    0.034146  0.017073  0.021951  0.019512   \n",
       "\n",
       "v_call_heavy  IGHV1-46  IGHV4-31  \n",
       "sample_id                         \n",
       "AP1           0.000000  0.051095  \n",
       "AP2           0.000000  0.000000  \n",
       "AP3           0.012225  0.000000  \n",
       "AP4           0.017241  0.024631  \n",
       "AP5           0.024123  0.013158  \n",
       "...                ...       ...  \n",
       "newcastle21   0.073171  0.000000  \n",
       "newcastle49   0.027972  0.090909  \n",
       "newcastle59   0.008264  0.000000  \n",
       "newcastle65   0.000000  0.020000  \n",
       "newcastle74   0.012195  0.009756  \n",
       "\n",
       "[125 rows x 20 columns]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test2 = pd.crosstab(test['sample_id'], test['v_call_heavy']).apply(lambda r: r/r.sum(), axis=1)\n",
    "s = test2.sum()\n",
    "test2 = test2[s.sort_values(ascending=False).index[:20]]\n",
    "test2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "status_dict = dict(zip(test['sample_id'], test['Status_on_day_collection_summary']))\n",
    "sex_dict = dict(zip(test['sample_id'], test['Sex']))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "test2['status'] = [status_dict[x] for x in test2.index]\n",
    "test2['sex'] = [sex_dict[x] for x in test2.index]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "test2.to_csv('other_analyses/bdata_obs_for_v_gene_usage.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['B_exhausted',\n",
       " 'Plasma_cell_IgA',\n",
       " 'Plasma_cell_IgG',\n",
       " 'B_immature',\n",
       " 'B_switched_memory',\n",
       " 'Plasma_cell_IgM',\n",
       " 'B_naive',\n",
       " 'Plasmablast',\n",
       " 'B_non-switched_memory']"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(set(bdata.obs['celltype_B']))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/nfs/users/nfs_k/kt16/miniconda2/envs/dandelion/lib/python3.7/site-packages/anndata/_core/anndata.py:1094: FutureWarning: is_categorical is deprecated and will be removed in a future version.  Use is_categorical_dtype instead\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "View of AnnData object with n_obs × n_vars = 7170 × 1051\n",
       "    obs: 'Site', 'doublet', 'patient_id', 'sample_id', 'batch', 'Resample', 'Collection_Day', 'Sex', 'Age', 'Swab_result', 'Status', 'Smoker', 'Status_on_day_collection', 'Status_on_day_collection_summary', 'Days_from_onset', 'time_after_LPS', 'Worst_Clinical_Status', 'Outcome', 'leiden', 'consensus', 'initial_clustering', 'study_id', 'initial_clustering_B', 'n_genes_by_counts', 'total_counts', 'total_counts_mt', 'pct_counts_mt', 'leiden_B', 'celltype_B', 'celltype_B_v2', 'Status_on_day_collection_summary_v2', 'filter_rna', 'has_bcr', 'filter_bcr_quality', 'filter_bcr_heavy', 'filter_bcr_light', 'bcr_QC_pass', 'filter_bcr', 'clone_id', 'clone_id_by_size', 'isotype', 'lightchain', 'status', 'vdj_status', 'productive', 'umi_counts_heavy', 'umi_counts_light', 'v_call_heavy', 'v_call_light', 'j_call_heavy', 'j_call_light', 'c_call_heavy', 'c_call_light'\n",
       "    var: 'feature_types', 'mt', 'n_cells_by_counts', 'mean_counts', 'pct_dropout_by_counts', 'total_counts', 'highly_variable', 'means', 'dispersions', 'dispersions_norm', 'mean', 'std'\n",
       "    uns: 'Site_colors', 'Status_on_day_collection_summary_colors', 'Status_on_day_collection_summary_v2_colors', 'celltype_B_colors', 'celltype_B_v2_colors', 'consensus_colors', \"dendrogram_['leiden_B']\", 'filter_bcr_colors', 'hvg', 'initial_clustering_colors', 'leiden', 'leiden_B_colors', 'leiden_colors', 'neighbors', 'pca', 'rank_genes_groups', 'umap'\n",
       "    obsm: 'X_pca', 'X_pca_harmony', 'X_umap'\n",
       "    varm: 'PCs'\n",
       "    obsp: 'connectivities', 'distances'"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## repeat but just for plasmablasts/plasma cells\n",
    "plasma = bdata[bdata.obs['celltype_B'].isin(['Plasmablast', 'Plasma_cell_IgM', 'Plasma_cell_IgA', 'Plasma_cell_IgG'])]\n",
    "plasma"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>sample_id</th>\n",
       "      <th>Status_on_day_collection_summary</th>\n",
       "      <th>Sex</th>\n",
       "      <th>v_call_heavy</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>cells</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>AAACGGGCACATTAGC-MH9179824</th>\n",
       "      <td>MH9179824</td>\n",
       "      <td>Moderate</td>\n",
       "      <td>Female</td>\n",
       "      <td>IGHV1-8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>AACCATGGTCCAAGTT-MH9179824</th>\n",
       "      <td>MH9179824</td>\n",
       "      <td>Moderate</td>\n",
       "      <td>Female</td>\n",
       "      <td>IGHV3-30</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>AACTGGTGTTTACTCT-MH9179824</th>\n",
       "      <td>MH9179824</td>\n",
       "      <td>Moderate</td>\n",
       "      <td>Female</td>\n",
       "      <td>IGHV5-10-1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>AACTGGTTCGTACCGG-MH9179824</th>\n",
       "      <td>MH9179824</td>\n",
       "      <td>Moderate</td>\n",
       "      <td>Female</td>\n",
       "      <td>IGHV3-7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>AATCGGTGTAGTAGTA-MH9179824</th>\n",
       "      <td>MH9179824</td>\n",
       "      <td>Moderate</td>\n",
       "      <td>Female</td>\n",
       "      <td>IGHV4-31</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>BGCV14_CGCTTCATCGTTTATC-1</th>\n",
       "      <td>BGCV14_CV0940</td>\n",
       "      <td>Healthy</td>\n",
       "      <td>Male</td>\n",
       "      <td>IGHV2-70</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>BGCV14_TCGGTAACACTTAACG-1</th>\n",
       "      <td>BGCV14_CV0940</td>\n",
       "      <td>Healthy</td>\n",
       "      <td>Male</td>\n",
       "      <td>IGHV3-30</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>BGCV14_TTTGGTTCAGGAATCG-1</th>\n",
       "      <td>BGCV14_CV0940</td>\n",
       "      <td>Healthy</td>\n",
       "      <td>Male</td>\n",
       "      <td>IGHV3-23</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>BGCV15_CTAACTTCAGATCCAT-1</th>\n",
       "      <td>BGCV15_CV0944</td>\n",
       "      <td>Healthy</td>\n",
       "      <td>Male</td>\n",
       "      <td>IGHV3-9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>BGCV15_GGCAATTGTCTTGATG-1</th>\n",
       "      <td>BGCV15_CV0944</td>\n",
       "      <td>Healthy</td>\n",
       "      <td>Male</td>\n",
       "      <td>IGHV3-9</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>7170 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                                sample_id Status_on_day_collection_summary  \\\n",
       "cells                                                                        \n",
       "AAACGGGCACATTAGC-MH9179824      MH9179824                         Moderate   \n",
       "AACCATGGTCCAAGTT-MH9179824      MH9179824                         Moderate   \n",
       "AACTGGTGTTTACTCT-MH9179824      MH9179824                         Moderate   \n",
       "AACTGGTTCGTACCGG-MH9179824      MH9179824                         Moderate   \n",
       "AATCGGTGTAGTAGTA-MH9179824      MH9179824                         Moderate   \n",
       "...                                   ...                              ...   \n",
       "BGCV14_CGCTTCATCGTTTATC-1   BGCV14_CV0940                          Healthy   \n",
       "BGCV14_TCGGTAACACTTAACG-1   BGCV14_CV0940                          Healthy   \n",
       "BGCV14_TTTGGTTCAGGAATCG-1   BGCV14_CV0940                          Healthy   \n",
       "BGCV15_CTAACTTCAGATCCAT-1   BGCV15_CV0944                          Healthy   \n",
       "BGCV15_GGCAATTGTCTTGATG-1   BGCV15_CV0944                          Healthy   \n",
       "\n",
       "                               Sex v_call_heavy  \n",
       "cells                                            \n",
       "AAACGGGCACATTAGC-MH9179824  Female      IGHV1-8  \n",
       "AACCATGGTCCAAGTT-MH9179824  Female     IGHV3-30  \n",
       "AACTGGTGTTTACTCT-MH9179824  Female   IGHV5-10-1  \n",
       "AACTGGTTCGTACCGG-MH9179824  Female      IGHV3-7  \n",
       "AATCGGTGTAGTAGTA-MH9179824  Female     IGHV4-31  \n",
       "...                            ...          ...  \n",
       "BGCV14_CGCTTCATCGTTTATC-1     Male     IGHV2-70  \n",
       "BGCV14_TCGGTAACACTTAACG-1     Male     IGHV3-30  \n",
       "BGCV14_TTTGGTTCAGGAATCG-1     Male     IGHV3-23  \n",
       "BGCV15_CTAACTTCAGATCCAT-1     Male      IGHV3-9  \n",
       "BGCV15_GGCAATTGTCTTGATG-1     Male      IGHV3-9  \n",
       "\n",
       "[7170 rows x 4 columns]"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test = plasma.obs[['sample_id', 'Status_on_day_collection_summary', 'Sex', 'v_call_heavy']]\n",
    "test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>v_call_heavy</th>\n",
       "      <th>IGHV3-30</th>\n",
       "      <th>IGHV3-23</th>\n",
       "      <th>IGHV4-59</th>\n",
       "      <th>IGHV3-21</th>\n",
       "      <th>IGHV1-2</th>\n",
       "      <th>IGHV1-18</th>\n",
       "      <th>IGHV4-34</th>\n",
       "      <th>IGHV3-7</th>\n",
       "      <th>IGHV4-39</th>\n",
       "      <th>IGHV3-48</th>\n",
       "      <th>IGHV3-11</th>\n",
       "      <th>IGHV3-33</th>\n",
       "      <th>IGHV3-74</th>\n",
       "      <th>IGHV5-51</th>\n",
       "      <th>IGHV3-15</th>\n",
       "      <th>IGHV3-9</th>\n",
       "      <th>IGHV1-46</th>\n",
       "      <th>IGHV3-49</th>\n",
       "      <th>IGHV1-69</th>\n",
       "      <th>IGHV4-31</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>sample_id</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>AP1</th>\n",
       "      <td>0.021739</td>\n",
       "      <td>0.130435</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.021739</td>\n",
       "      <td>0.021739</td>\n",
       "      <td>0.021739</td>\n",
       "      <td>0.065217</td>\n",
       "      <td>0.065217</td>\n",
       "      <td>0.086957</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.021739</td>\n",
       "      <td>0.217391</td>\n",
       "      <td>0.043478</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.021739</td>\n",
       "      <td>0.021739</td>\n",
       "      <td>0.043478</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>AP2</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.222222</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.222222</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.111111</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.111111</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>AP3</th>\n",
       "      <td>0.071429</td>\n",
       "      <td>0.214286</td>\n",
       "      <td>0.035714</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.035714</td>\n",
       "      <td>0.142857</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.107143</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.071429</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>AP4</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.166667</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.166667</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.166667</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.166667</td>\n",
       "      <td>0.166667</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.166667</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>AP5</th>\n",
       "      <td>0.019920</td>\n",
       "      <td>0.107570</td>\n",
       "      <td>0.051793</td>\n",
       "      <td>0.023904</td>\n",
       "      <td>0.043825</td>\n",
       "      <td>0.039841</td>\n",
       "      <td>0.043825</td>\n",
       "      <td>0.031873</td>\n",
       "      <td>0.035857</td>\n",
       "      <td>0.015936</td>\n",
       "      <td>0.023904</td>\n",
       "      <td>0.007968</td>\n",
       "      <td>0.027888</td>\n",
       "      <td>0.043825</td>\n",
       "      <td>0.015936</td>\n",
       "      <td>0.043825</td>\n",
       "      <td>0.023904</td>\n",
       "      <td>0.099602</td>\n",
       "      <td>0.035857</td>\n",
       "      <td>0.011952</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>newcastle21</th>\n",
       "      <td>0.153846</td>\n",
       "      <td>0.030769</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.030769</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.061538</td>\n",
       "      <td>0.046154</td>\n",
       "      <td>0.015385</td>\n",
       "      <td>0.046154</td>\n",
       "      <td>0.030769</td>\n",
       "      <td>0.076923</td>\n",
       "      <td>0.061538</td>\n",
       "      <td>0.030769</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.030769</td>\n",
       "      <td>0.046154</td>\n",
       "      <td>0.092308</td>\n",
       "      <td>0.015385</td>\n",
       "      <td>0.030769</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>newcastle49</th>\n",
       "      <td>0.050000</td>\n",
       "      <td>0.125000</td>\n",
       "      <td>0.050000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.050000</td>\n",
       "      <td>0.225000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.050000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.050000</td>\n",
       "      <td>0.025000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.025000</td>\n",
       "      <td>0.050000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.025000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.050000</td>\n",
       "      <td>0.175000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>newcastle59</th>\n",
       "      <td>0.050000</td>\n",
       "      <td>0.200000</td>\n",
       "      <td>0.200000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.100000</td>\n",
       "      <td>0.050000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.100000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>newcastle65</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>newcastle74</th>\n",
       "      <td>0.333333</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.111111</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.111111</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.222222</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.111111</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.111111</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>118 rows × 20 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "v_call_heavy  IGHV3-30  IGHV3-23  IGHV4-59  IGHV3-21   IGHV1-2  IGHV1-18  \\\n",
       "sample_id                                                                  \n",
       "AP1           0.021739  0.130435  0.000000  0.000000  0.021739  0.021739   \n",
       "AP2           0.000000  0.000000  0.000000  0.222222  0.000000  0.000000   \n",
       "AP3           0.071429  0.214286  0.035714  0.000000  0.000000  0.000000   \n",
       "AP4           0.000000  0.166667  0.000000  0.166667  0.000000  0.000000   \n",
       "AP5           0.019920  0.107570  0.051793  0.023904  0.043825  0.039841   \n",
       "...                ...       ...       ...       ...       ...       ...   \n",
       "newcastle21   0.153846  0.030769  0.000000  0.030769  0.000000  0.061538   \n",
       "newcastle49   0.050000  0.125000  0.050000  0.000000  0.000000  0.050000   \n",
       "newcastle59   0.050000  0.200000  0.200000  0.000000  0.000000  0.000000   \n",
       "newcastle65   0.000000  0.000000  0.000000  0.000000  0.000000  1.000000   \n",
       "newcastle74   0.333333  0.000000  0.000000  0.111111  0.000000  0.111111   \n",
       "\n",
       "v_call_heavy  IGHV4-34   IGHV3-7  IGHV4-39  IGHV3-48  IGHV3-11  IGHV3-33  \\\n",
       "sample_id                                                                  \n",
       "AP1           0.021739  0.065217  0.065217  0.086957  0.000000  0.000000   \n",
       "AP2           0.000000  0.000000  0.222222  0.000000  0.000000  0.000000   \n",
       "AP3           0.000000  0.000000  0.035714  0.142857  0.000000  0.107143   \n",
       "AP4           0.000000  0.000000  0.166667  0.000000  0.000000  0.166667   \n",
       "AP5           0.043825  0.031873  0.035857  0.015936  0.023904  0.007968   \n",
       "...                ...       ...       ...       ...       ...       ...   \n",
       "newcastle21   0.046154  0.015385  0.046154  0.030769  0.076923  0.061538   \n",
       "newcastle49   0.225000  0.000000  0.050000  0.000000  0.050000  0.025000   \n",
       "newcastle59   0.100000  0.050000  0.000000  0.000000  0.000000  0.000000   \n",
       "newcastle65   0.000000  0.000000  0.000000  0.000000  0.000000  0.000000   \n",
       "newcastle74   0.000000  0.000000  0.000000  0.222222  0.000000  0.000000   \n",
       "\n",
       "v_call_heavy  IGHV3-74  IGHV5-51  IGHV3-15   IGHV3-9  IGHV1-46  IGHV3-49  \\\n",
       "sample_id                                                                  \n",
       "AP1           0.000000  0.021739  0.217391  0.043478  0.000000  0.021739   \n",
       "AP2           0.000000  0.000000  0.111111  0.000000  0.000000  0.000000   \n",
       "AP3           0.000000  0.000000  0.071429  0.000000  0.000000  0.000000   \n",
       "AP4           0.166667  0.000000  0.000000  0.166667  0.000000  0.000000   \n",
       "AP5           0.027888  0.043825  0.015936  0.043825  0.023904  0.099602   \n",
       "...                ...       ...       ...       ...       ...       ...   \n",
       "newcastle21   0.030769  0.000000  0.030769  0.046154  0.092308  0.015385   \n",
       "newcastle49   0.000000  0.025000  0.050000  0.000000  0.025000  0.000000   \n",
       "newcastle59   0.000000  0.100000  0.000000  0.000000  0.000000  0.000000   \n",
       "newcastle65   0.000000  0.000000  0.000000  0.000000  0.000000  0.000000   \n",
       "newcastle74   0.000000  0.000000  0.111111  0.000000  0.000000  0.111111   \n",
       "\n",
       "v_call_heavy  IGHV1-69  IGHV4-31  \n",
       "sample_id                         \n",
       "AP1           0.021739  0.043478  \n",
       "AP2           0.111111  0.000000  \n",
       "AP3           0.000000  0.000000  \n",
       "AP4           0.000000  0.000000  \n",
       "AP5           0.035857  0.011952  \n",
       "...                ...       ...  \n",
       "newcastle21   0.030769  0.000000  \n",
       "newcastle49   0.050000  0.175000  \n",
       "newcastle59   0.000000  0.000000  \n",
       "newcastle65   0.000000  0.000000  \n",
       "newcastle74   0.000000  0.000000  \n",
       "\n",
       "[118 rows x 20 columns]"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test2 = pd.crosstab(test['sample_id'], test['v_call_heavy']).apply(lambda r: r/r.sum(), axis=1)\n",
    "s = test2.sum()\n",
    "test2 = test2[s.sort_values(ascending=False).index[:20]]\n",
    "test2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "status_dict = dict(zip(test['sample_id'], test['Status_on_day_collection_summary']))\n",
    "sex_dict = dict(zip(test['sample_id'], test['Sex']))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "test2['status'] = [status_dict[x] for x in test2.index]\n",
    "test2['sex'] = [sex_dict[x] for x in test2.index]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "test2.to_csv('other_analyses/bdata_plasma_obs_for_v_gene_usage.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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": 4
}
