{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using Theano backend.\n",
      "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/matplotlib/__init__.py:872: UserWarning: axes.color_cycle is deprecated and replaced with axes.prop_cycle; please use the latter.\n",
      "  warnings.warn(self.msg_depr % (key, alt_key))\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Couldn't import dot_parser, loading of dot files will not be possible.\n"
     ]
    }
   ],
   "source": [
    "import mhcflurry, seaborn, numpy, pandas, pickle, sklearn, collections, scipy, time\n",
    "from mhcflurry.dataset import Dataset\n",
    "import fancyimpute, locale\n",
    "\n",
    "import sklearn.metrics\n",
    "import sklearn.cross_validation\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "min_peptides_to_consider_allele = 10\n",
    "max_ic50 = 50000"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Unfiltered training data: Dataset(n=137654, alleles=['ELA-A1', 'Gogo-B0101', 'H-2-DB', 'H-2-DD', 'H-2-KB', 'H-2-KD', 'H-2-KK', 'H-2-LD', 'HLA-A0101', 'HLA-A0201', 'HLA-A0202', 'HLA-A0203', 'HLA-A0205', 'HLA-A0206', 'HLA-A0207', 'HLA-A0210', 'HLA-A0211', 'HLA-A0212', 'HLA-A0216', 'HLA-A0219', 'HLA-A0250', 'HLA-A0301', 'HLA-A0302', 'HLA-A11', 'HLA-A1101', 'HLA-A2', 'HLA-A2301', 'HLA-A2402', 'HLA-A2403', 'HLA-A2501', 'HLA-A26', 'HLA-A2601', 'HLA-A2602', 'HLA-A2603', 'HLA-A2902', 'HLA-A3001', 'HLA-A3002', 'HLA-A3101', 'HLA-A3201', 'HLA-A3301', 'HLA-A6601', 'HLA-A6801', 'HLA-A6802', 'HLA-A6901', 'HLA-A8001', 'HLA-B0702', 'HLA-B0801', 'HLA-B0802', 'HLA-B0803', 'HLA-B1402', 'HLA-B1501', 'HLA-B1502', 'HLA-B1503', 'HLA-B1509', 'HLA-B1517', 'HLA-B1801', 'HLA-B2701', 'HLA-B2702', 'HLA-B2703', 'HLA-B2705', 'HLA-B3501', 'HLA-B3503', 'HLA-B3508', 'HLA-B3801', 'HLA-B3901', 'HLA-B4001', 'HLA-B4002', 'HLA-B4201', 'HLA-B44', 'HLA-B4402', 'HLA-B4403', 'HLA-B4501', 'HLA-B4601', 'HLA-B4801', 'HLA-B5101', 'HLA-B5301', 'HLA-B5401', 'HLA-B5701', 'HLA-B5801', 'HLA-B5802', 'HLA-B7', 'HLA-B7301', 'HLA-E0101', 'Mamu-A01', 'Mamu-A02', 'Mamu-A07', 'Mamu-A11', 'Mamu-A2201', 'Mamu-A2601', 'Mamu-B01', 'Mamu-B03', 'Mamu-B04', 'Mamu-B08', 'Mamu-B17', 'Mamu-B52', 'Patr-A0101', 'Patr-A0301', 'Patr-A0401', 'Patr-A0602', 'Patr-A0701', 'Patr-A0901', 'Patr-B0101', 'Patr-B0901', 'Patr-B1301', 'Patr-B1701', 'Patr-B2401'])\n",
      "Training data after filtering: Dataset(n=137610, alleles=['ELA-A1', 'Gogo-B0101', 'H-2-DB', 'H-2-DD', 'H-2-KB', 'H-2-KD', 'H-2-KK', 'H-2-LD', 'HLA-A0101', 'HLA-A0201', 'HLA-A0202', 'HLA-A0203', 'HLA-A0205', 'HLA-A0206', 'HLA-A0207', 'HLA-A0210', 'HLA-A0211', 'HLA-A0212', 'HLA-A0216', 'HLA-A0219', 'HLA-A0250', 'HLA-A0301', 'HLA-A0302', 'HLA-A11', 'HLA-A1101', 'HLA-A2', 'HLA-A2301', 'HLA-A2402', 'HLA-A2403', 'HLA-A2501', 'HLA-A2601', 'HLA-A2602', 'HLA-A2603', 'HLA-A2902', 'HLA-A3001', 'HLA-A3002', 'HLA-A3101', 'HLA-A3201', 'HLA-A3301', 'HLA-A6801', 'HLA-A6802', 'HLA-A6901', 'HLA-A8001', 'HLA-B0702', 'HLA-B0801', 'HLA-B0802', 'HLA-B0803', 'HLA-B1501', 'HLA-B1502', 'HLA-B1503', 'HLA-B1509', 'HLA-B1517', 'HLA-B1801', 'HLA-B2703', 'HLA-B2705', 'HLA-B3501', 'HLA-B3801', 'HLA-B3901', 'HLA-B4001', 'HLA-B4002', 'HLA-B4402', 'HLA-B4403', 'HLA-B4501', 'HLA-B4601', 'HLA-B4801', 'HLA-B5101', 'HLA-B5301', 'HLA-B5401', 'HLA-B5701', 'HLA-B5801', 'HLA-B5802', 'HLA-B7', 'HLA-B7301', 'Mamu-A01', 'Mamu-A02', 'Mamu-A07', 'Mamu-A11', 'Mamu-A2201', 'Mamu-A2601', 'Mamu-B01', 'Mamu-B03', 'Mamu-B08', 'Mamu-B17', 'Patr-A0101', 'Patr-A0301', 'Patr-A0401', 'Patr-A0701', 'Patr-A0901', 'Patr-B0101', 'Patr-B1301', 'Patr-B2401'])\n",
      "Validation data: Dataset(n=27680, alleles=['H-2-DB', 'H-2-KB', 'H-2-KD', 'HLA-A0101', 'HLA-A0201', 'HLA-A0202', 'HLA-A0203', 'HLA-A0206', 'HLA-A0301', 'HLA-A1101', 'HLA-A2301', 'HLA-A2402', 'HLA-A2501', 'HLA-A2601', 'HLA-A2602', 'HLA-A2603', 'HLA-A2902', 'HLA-A3001', 'HLA-A3002', 'HLA-A3101', 'HLA-A3201', 'HLA-A3301', 'HLA-A6801', 'HLA-A6802', 'HLA-A6901', 'HLA-A8001', 'HLA-B0702', 'HLA-B0801', 'HLA-B0802', 'HLA-B0803', 'HLA-B1501', 'HLA-B1503', 'HLA-B1509', 'HLA-B1517', 'HLA-B1801', 'HLA-B2703', 'HLA-B2705', 'HLA-B3501', 'HLA-B3801', 'HLA-B3901', 'HLA-B4001', 'HLA-B4002', 'HLA-B4402', 'HLA-B4403', 'HLA-B4501', 'HLA-B4601', 'HLA-B5101', 'HLA-B5301', 'HLA-B5401', 'HLA-B5701', 'HLA-B5801', 'Mamu-A01', 'Mamu-A02'])\n",
      "Validation data after filtering: Dataset(n=27680, alleles=['H-2-DB', 'H-2-KB', 'H-2-KD', 'HLA-A0101', 'HLA-A0201', 'HLA-A0202', 'HLA-A0203', 'HLA-A0206', 'HLA-A0301', 'HLA-A1101', 'HLA-A2301', 'HLA-A2402', 'HLA-A2501', 'HLA-A2601', 'HLA-A2602', 'HLA-A2603', 'HLA-A2902', 'HLA-A3001', 'HLA-A3002', 'HLA-A3101', 'HLA-A3201', 'HLA-A3301', 'HLA-A6801', 'HLA-A6802', 'HLA-A6901', 'HLA-A8001', 'HLA-B0702', 'HLA-B0801', 'HLA-B0802', 'HLA-B0803', 'HLA-B1501', 'HLA-B1503', 'HLA-B1509', 'HLA-B1517', 'HLA-B1801', 'HLA-B2703', 'HLA-B2705', 'HLA-B3501', 'HLA-B3801', 'HLA-B3901', 'HLA-B4001', 'HLA-B4002', 'HLA-B4402', 'HLA-B4403', 'HLA-B4501', 'HLA-B4601', 'HLA-B5101', 'HLA-B5301', 'HLA-B5401', 'HLA-B5701', 'HLA-B5801', 'Mamu-A01', 'Mamu-A02'])\n"
     ]
    }
   ],
   "source": [
    "# load training data\n",
    "all_train_data_with_small_alleles = Dataset.from_csv(\"../data/bdata.2009.mhci.public.1.txt\")\n",
    "print(\"Unfiltered training data: %s\" % (all_train_data_with_small_alleles,))\n",
    "\n",
    "# filter out smaller alleles\n",
    "all_train_data = all_train_data_with_small_alleles.filter_alleles_by_count(min_peptides_to_consider_allele)\n",
    "\n",
    "print(\"Training data after filtering: %s\" % (all_train_data,))\n",
    "\n",
    "alleles = all_train_data.unique_alleles()\n",
    "\n",
    "all_validation_data_with_small_alleles = Dataset.from_csv(\"../data/bdata.2013.mhci.public.blind.1.txt\")\n",
    "\n",
    "print(\"Validation data: %s\" % (all_validation_data_with_small_alleles,))\n",
    "\n",
    "all_validation_data = all_validation_data_with_small_alleles.filter_alleles_by_count(min_peptides_to_consider_allele)\n",
    "\n",
    "print(\"Validation data after filtering: %s\" % (\n",
    "    all_validation_data,))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Dataset(n=2126, alleles=['HLA-A0201'])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "all_validation_data.get_allele(\"HLA-A0201\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\\begin{tabular}{lll}\n",
      "\\toprule\n",
      "{} & Alleles & Total IC50 Measurements \\\\\n",
      "\\midrule\n",
      "BD2009 &     106 &                 137,654 \\\\\n",
      "BLIND  &      53 &                  27,680 \\\\\n",
      "\\bottomrule\n",
      "\\end{tabular}\n",
      "\n"
     ]
    }
   ],
   "source": [
    "\n",
    "def thousands(num):\n",
    "    locale.setlocale(locale.LC_ALL, 'en_US')\n",
    "    return locale.format(\"%d\", num, grouping=True)\n",
    "\n",
    "def format_pair(lst1, lst2):\n",
    "    return [\"%s (%s)\" % (thousands(a), thousands(b)) for (a,b) in zip(lst1, lst2)]\n",
    "\n",
    "description_df = pandas.DataFrame(index= [\"BD2009\", \"BLIND\"])\n",
    "#description_df[\"Alleles\"] = [\n",
    "#    thousands(len(all_train_data_with_small_alleles.unique_alleles())), \n",
    "#    thousands(len(all_validation_data_with_small_alleles.unique_alleles()))\n",
    "#]\n",
    "description_df[\"Alleles\"] = [\n",
    "    thousands(len(all_train_data_with_small_alleles.unique_alleles())), \n",
    "    thousands(len(all_validation_data_with_small_alleles.unique_alleles()))\n",
    "]\n",
    "description_df[\"Total IC50 Measurements\"] = [\n",
    "    thousands(len(all_train_data_with_small_alleles.unique_allele_peptide_pairs())), \n",
    "    thousands(len(all_validation_data_with_small_alleles.unique_allele_peptide_pairs()))\n",
    "]\n",
    "\n",
    "print(description_df.to_latex(index_names=False))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "description_df.to_latex?"
   ]
  }
 ],
 "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.4.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
