<?xml version="1.0" encoding="UTF-8"?>
<rss  xmlns:atom="http://www.w3.org/2005/Atom" 
      xmlns:media="http://search.yahoo.com/mrss/" 
      xmlns:content="http://purl.org/rss/1.0/modules/content/" 
      xmlns:dc="http://purl.org/dc/elements/1.1/" 
      version="2.0">
<channel>
<title>All Things Bioinformatics</title>
<link>https://jaychung10010.quarto.pub/all-things-bioinformatics/</link>
<atom:link href="https://jaychung10010.quarto.pub/all-things-bioinformatics/index.xml" rel="self" type="application/rss+xml"/>
<description>A blog about bioinformatics, machine learning, and drug discovery.</description>
<generator>quarto-1.8.26</generator>
<lastBuildDate>Wed, 25 Feb 2026 08:00:00 GMT</lastBuildDate>
<item>
  <title>Evaluating Data Leakage in Protein Binding Affinity Prediction</title>
  <dc:creator>Jay Chung</dc:creator>
  <link>https://jaychung10010.quarto.pub/all-things-bioinformatics/posts/P7_data_leakage_peptide_mhc/</link>
  <description><![CDATA[ 





<section id="research-impact" class="level2">
<h2 class="anchored" data-anchor-id="research-impact">Research Impact</h2>
<p>Data leakage is a critical issue in machine learning, especially in the context of protein-protein interaction (PPI) prediction. Data leakage occurs when information from the training data is inadvertently used in the testing phase, allowing the model to “remember” specific examples rather than learning generalizable patterns. To address this issue, this post compares two data splitting strategies, regular C3 split and strict C3 split, to evaluate the extent of data leakage when using pre-trained protein large language models (pLLMs) to predict peptide-MHC2 binding. The findings showed that while the neural network model generalized well on predicting binding peptides of seen MHC2 alleles, it struggled to generalize to unseen MHC2 alleles. This highlights the importance of using strict data splitting strategies to ensure that models are evaluated on truly unseen data, which is crucial for developing robust and generalizable models in protein binding affinity prediction.</p>
</section>
<section id="introduction" class="level2">
<h2 class="anchored" data-anchor-id="introduction">Introduction</h2>
<p>I’ve recently come across <a href="https://www.biorxiv.org/content/10.1101/2025.04.21.649858v2">this paper</a> about data leakage in PPI prediction models using pLLMs, and it got me thinking about how data leakage might affect other types of protein interaction predictions, such as peptide-MHC2 binding. In this project, I decided to explore this issue by comparing two different data splitting strategies: a regular C3 split and a strict C3 split. The regular C3 split allows for some overlap between the training and testing sets, while the strict C3 split ensures that there is no overlap at all. By evaluating the performance of a neural network model trained on embeddings extracted from a pre-trained pLLM (ESM2) under these two splitting strategies, I aimed to understand the extent of data leakage and its impact on model generalization in the context of peptide-MHC2 binding prediction.</p>
</section>
<section id="key-steps" class="level2">
<h2 class="anchored" data-anchor-id="key-steps">Key Steps</h2>
<ol type="1">
<li><p><strong>Splitting Strategies</strong>: Peptide or MHC sequences were first clustered independently using two different approaches. In both cases, the goal was to ensure that similar protein sequences were not present in both the training and testing sets. Peptides were clustered based on a 3-gram sequence similarity approach, while MHC pseudo-sequences were clustered based on their BLOSUM62 amino acid similarity. After that, the cluster classes were split into training and testing sets using two different strategies largely based on the <a href="https://doi.org/10.1038/nmeth.2259">C3 approach</a>:</p>
<ul>
<li><p><strong>Regular C3 split</strong>: This split ensures that the test set contains pairs <img src="https://latex.codecogs.com/png.latex?(Peptide_%7Bnew%7D,%20MHC_%7Bany%7D)"> and <img src="https://latex.codecogs.com/png.latex?(Peptide_%7Bany%7D,%20MHC_%7Bnew%7D)">. This is a less strict C3 split, as it allows for some overlap in the individual components (peptides and MHCs) between the training and testing sets, but not in the interactions.</p></li>
<li><p><strong>Strict C3 split</strong>: This split ensures that for every pair <img src="https://latex.codecogs.com/png.latex?(Peptide,%20MHC)"> in the test set, both <img src="https://latex.codecogs.com/png.latex?Peptide"> and <img src="https://latex.codecogs.com/png.latex?MHC"> classes are entirely absent from the training set. This is the “Double-Cold” strategy, which is more stringent and ensures that the model is evaluated on completely unseen peptides and MHCs, thus providing a more accurate assessment of the model’s generalization capabilities. A down side of this split is that it results in a smaller training and testing set. To ensure a fair comparison between the two splits, the sample number from the “regular C3 split” was downsampled to match the sample number from the “strict C3 split”.</p></li>
</ul></li>
<li><p><strong>Embeddings Extraction and Model Training</strong>: Similar to <a href="https://jaychung10010.quarto.pub/all-things-bioinformatics/posts/P6_cnn_dnn_peptide_mhc/#references">my previous post</a>, we will extract ESM2 embeddings for both peptides and MHC2 pseudo-sequences. The embeddings will be extracted in a way that retains the 2D structure, which is crucial for applying convolutional layers in the model. After extracting the embeddings, we will concatenate the peptide and MHC2 embeddings along the sequence length dimension, retaining the sequence information and the 2D structure. We will then define a model architecture that includes two 1D convolutional layers followed by a self-attention layer and five dense layers. The model will be trained separately on the datasets generated from the regular C3 split and the strict C3 split, allowing us to compare the performance of the model under both splitting strategies.</p></li>
<li><p><strong>Comparing Regular vs.&nbsp;Strict C3 split</strong>: After training the model on both datasets, we will evaluate its performance using appropriate metrics such as <img src="https://latex.codecogs.com/png.latex?R%5E2"> score, root mean squared error (RMSE), and loss.</p></li>
</ol>
</section>
<section id="data-splitting-strategies" class="level2">
<h2 class="anchored" data-anchor-id="data-splitting-strategies">1. Data Splitting Strategies</h2>
<p>Loading required libraries:</p>
<div id="f295f940" class="cell" data-execution_count="1">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> pandas <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> pd</span>
<span id="cb1-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> numpy <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> np</span>
<span id="cb1-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> sklearn.cluster <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> AgglomerativeClustering</span>
<span id="cb1-4"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> sklearn.model_selection <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> GroupShuffleSplit</span>
<span id="cb1-5"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> Bio.Align <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> substitution_matrices</span>
<span id="cb1-6"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> sklearn.feature_extraction.text <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> CountVectorizer</span>
<span id="cb1-7"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> sklearn.metrics.pairwise <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> cosine_similarity</span>
<span id="cb1-8"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> sklearn.model_selection <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> train_test_split</span></code></pre></div></div>
</details>
</div>
<p>See <a href="https://jaychung10010.quarto.pub/all-things-bioinformatics/posts/2026-01-18_peptide_affinity_llm_nn/">my previous post</a> for source of data. First, let’s see what the input data looks like:</p>
<div id="c016792c" class="cell" data-execution_count="2">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1">df.head()</span></code></pre></div></div>
</details>
</div>
<p><strong>Output:</strong></p>
<pre><code>   Peptide_ID           Peptide     MHC_ID  \
0      104653   VAPIEHIASMRRNYF  DRB1_1302   
1       37106   HDDKETSFIRNCARK  DRB1_0101   
2      118433   LIWVGINTRNMTMSM  DRB1_0101   
3       80770  GVTVIKNNMINNDLGP  DRB1_1501   
4       19888   PAPMLAAAAGWQTLS  DRB1_1101   

                                  MHC         Y  
0  QEFFIASGAAVDAIMESSFDYFDIDEATYHVGFT  0.501084  
1  QEFFIASGAAVDAIMWLFLECYDLQRATYHVGFT  0.441298  
2  QEFFIASGAAVDAIMWLFLECYDLQRATYHVGFT  0.217673  
3  QEFFIASGAAVDAIMWPRFDYFDIQAATYHVVFT  0.807811  
4  QEFFIASGAAVDAIMESSFDYFDFDRATYHVGFT  0.583271  </code></pre>
<p>Here we have the peptide sequences, MHC pseudo-sequences, and their corresponding binding affinity values (Y).</p>
<p>Let’s perfrom peptide clustering using a 3-gram approach to find overlapping peptides without source information. We will use the <code>CountVectorizer</code> from <code>sklearn</code> to create a matrix of 3-gram counts for each unique peptide, and then compute the cosine similarity between the peptides based on this matrix. Finally, we will use hierarchical clustering to group similar peptides together.</p>
<div id="af5a4842" class="cell" data-execution_count="3">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1">vect <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> CountVectorizer(analyzer<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'char'</span>, ngram_range<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># sequence of 3 a.a.</span></span>
<span id="cb4-2">pep_matrix <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> vect.fit_transform(df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Peptide'</span>].unique()) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># row: unique peptides, col: unique 3 a.a.</span></span>
<span id="cb4-3">pep_sim <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> cosine_similarity(pep_matrix) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># peptide X peptide similarity matrix</span></span>
<span id="cb4-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># use cosine dist when the orientation or pattern of the data is more important than the absolute scale</span></span>
<span id="cb4-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># often used in text, genetic sequence</span></span>
<span id="cb4-6"></span>
<span id="cb4-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Cluster peptides that share many 3-mers (likely from the same protein)</span></span>
<span id="cb4-8">pep_clusters <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> AgglomerativeClustering(</span>
<span id="cb4-9">    n_clusters<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>,</span>
<span id="cb4-10">    distance_threshold<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Adjust: lower = more groups, stricter split</span></span>
<span id="cb4-11">    metric<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'precomputed'</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># input is pre-computed dist</span></span>
<span id="cb4-12">    linkage<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'complete'</span></span>
<span id="cb4-13">).fit_predict(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> pep_sim)</span>
<span id="cb4-14"></span>
<span id="cb4-15">pep_map <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.DataFrame({</span>
<span id="cb4-16">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Peptide'</span>: df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Peptide'</span>].unique(),</span>
<span id="cb4-17">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'pep_group'</span>: pep_clusters</span>
<span id="cb4-18">})</span>
<span id="cb4-19"></span>
<span id="cb4-20">df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> df.merge(pep_map, on<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Peptide'</span>)</span></code></pre></div></div>
</details>
</div>
<div id="ad8a8776" class="cell" data-execution_count="4">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb5-1"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(pep_map.sort_values(by<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'pep_group'</span>).head(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>))</span></code></pre></div></div>
</details>
</div>
<p><strong>Output:</strong></p>
<pre><code>                    Peptide  pep_group
1576         QYIKANAKFIGITE          0
702       YATFFIKANSKFIGITE          0
6842       MQYIKANSKFIGITEL          0
15556        QYQKANSKFIGITE          0
4185         QYIKANSKFIGITE          0
7940   SAMILAAYHPQQFIYAGSLS          1
16944  AAIGLSMAGSSAMILAAYHP          1
9611       RQIRMAKLLGRDPEQS          2
860        HGRQIRMAKLFGRDPE          2
6826       EGELHGRQIRMAKLLG          2
14166      HGRQIKMAKLLGRDPE          2
687         HGRQIRMAKLLGRDP          2
4640       HGRQIRMAKLLGRDPE          2
6532       HGRQIRMAKLLTRDPE          2
4614        QIRMAKLLGRDPEQS          2</code></pre>
<p>We can see that peptides that are likely from the same protein (e.g., “QYIKANAKFIGITE”, “YATFFIKANSKFIGITE”, “MQYIKANSKFIGITEL”) are clustered together in the same group (group 0). This indicates that our clustering approach is effectively grouping similar peptides together based on their 3-gram composition.</p>
<p>Next, we will cluster MHC pseudo-sequences based on their BLOSUM62 amino acid similarity. We will define a function to calculate the BLOSUM62 similarity between two sequences, and then create a similarity matrix for the MHC pseudo-sequences. Finally, we will use hierarchical clustering to group similar MHCs together.</p>
<div id="ca096edc" class="cell" data-execution_count="5">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb7-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Function to calculate BLOSUM62 similarity</span></span>
<span id="cb7-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> blosum62_similarity(seq1, seq2):</span>
<span id="cb7-3">    matrix <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> substitution_matrices.load(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'BLOSUM62'</span>)</span>
<span id="cb7-4">    score <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0</span></span>
<span id="cb7-5"></span>
<span id="cb7-6">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Align sequences by padding the shorter one (simple padding for score calculation)</span></span>
<span id="cb7-7">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Note: For rigorous alignment, a proper sequence alignment algorithm (e.g., Needleman-Wunsch) is needed.</span></span>
<span id="cb7-8">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># This simplified approach assumes aligned positions are comparable.</span></span>
<span id="cb7-9">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">min</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(seq1), <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(seq2))):</span>
<span id="cb7-10">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">try</span>:</span>
<span id="cb7-11">            score <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> matrix[seq1[i], seq2[i]]</span>
<span id="cb7-12">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">except</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">KeyError</span>: <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Handle cases where amino acid might not be in BLOSUM (e.g., 'X')</span></span>
<span id="cb7-13">            score <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span> </span>
<span id="cb7-14"></span>
<span id="cb7-15">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Normalize score (for simplicity, divide by max possible score)</span></span>
<span id="cb7-16">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># A more robust normalization might involve the self-similarity score.</span></span>
<span id="cb7-17">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Here a max possible similarity score for each aa is calculated and summed</span></span>
<span id="cb7-18">    max_score1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>(matrix[aa, aa] <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> aa <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> seq1 <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (aa, aa) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> matrix.keys())</span>
<span id="cb7-19">    max_score2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>(matrix[aa, aa] <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> aa <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> seq2 <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (aa, aa) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> matrix.keys())</span>
<span id="cb7-20">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> max_score1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">or</span> max_score2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>: <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Avoid division by zero</span></span>
<span id="cb7-21">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0</span></span>
<span id="cb7-22">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> score <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">max</span>(max_score1, max_score2)</span>
<span id="cb7-23"></span>
<span id="cb7-24">mhc_sequences <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'MHC'</span>].unique()</span>
<span id="cb7-25"></span>
<span id="cb7-26"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create a similarity matrix for MHCs</span></span>
<span id="cb7-27">n_mhc <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(mhc_sequences)</span>
<span id="cb7-28">mhc_sim_matrix <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.zeros((n_mhc, n_mhc))</span>
<span id="cb7-29"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(n_mhc):</span>
<span id="cb7-30">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> j <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(i, n_mhc):</span>
<span id="cb7-31">        sim <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> blosum62_similarity(mhc_sequences[i], mhc_sequences[j])</span>
<span id="cb7-32">        mhc_sim_matrix[i, j] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> sim</span>
<span id="cb7-33">        mhc_sim_matrix[j, i] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> sim</span>
<span id="cb7-34"></span>
<span id="cb7-35"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Cluster MHCs</span></span>
<span id="cb7-36">mhc_clusters <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> AgglomerativeClustering(</span>
<span id="cb7-37">    n_clusters<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>,</span>
<span id="cb7-38">    distance_threshold<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Adjust: lower = more groups, stricter split for MHCs</span></span>
<span id="cb7-39">    metric<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'precomputed'</span>,</span>
<span id="cb7-40">    linkage<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'complete'</span></span>
<span id="cb7-41">).fit_predict(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> mhc_sim_matrix)</span>
<span id="cb7-42"></span>
<span id="cb7-43">mhc_map <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.DataFrame({</span>
<span id="cb7-44">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'MHC'</span>: mhc_sequences,</span>
<span id="cb7-45">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'mhc_group'</span>: mhc_clusters</span>
<span id="cb7-46">})</span>
<span id="cb7-47"></span>
<span id="cb7-48">df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> df.merge(mhc_map, on<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'MHC'</span>)</span></code></pre></div></div>
</details>
</div>
<div id="52f1bdc4" class="cell" data-execution_count="6">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb8-1"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(mhc_map.sort_values(by<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'mhc_group'</span>))</span></code></pre></div></div>
</details>
</div>
<p><strong>Output:</strong></p>
<pre><code>                                   MHC  mhc_group
5   QEFFIASGAAVDAIMELSFEYYVLQKQNYHVVFT          0
15  QEFFIASGAAVDAIMERSYDYYVLQKRNYHVGFT          0
12  QEFFIASGAAVDAIMELSFEHYDLQKQNYHVGFT          0
8   QEFFIASGAAVDAIMESSYDYFDLQKRNYHVVFT          0
9   CNYHQGGGARVAHIMFFGLTYYDVGTETVHVAGI          1
..                                 ...        ...
64  YTYFLRRGGQTGHILHFPLIYYDYRTETVHKTPT         26
66  XXYHWTSGGQTGHGWALGSNYYDIRTETVHGVHT         27
70  QEFFIASGAAVDAIMESSFEYYDLQRATYHVGFT         28
62  QEFFIASGAAVDAIMESSFEYYDLQKRNYHVGFT         28
29  QEFFIASGAAVDAIMESGLEHFVIDRATYHAVFT         29

[75 rows x 2 columns]</code></pre>
<p>We can see that MHC pseudo-sequences that are similar based on their BLOSUM62 scores are clustered together in the same group (e.g., “QEFFIASGAAVDAIMELSFEYYVLQKQNYHVVFT”, “QEFFIASGAAVDAIMERSYDYYVLQKRNYHVGFT”, “QEFFIASGAAVDAIMELSFEHYDLQKQNYHVGFT” are all in group 0). This indicates that our clustering approach is effectively grouping similar MHC pseudo-sequences together based on their amino acid composition and similarity.</p>
<p>Next, we will perform the regular C3 split. To ensure total isolation between the training and testing sets, we will create a ‘SuperGroup’ that combines both the peptide and MHC groups. This way, we can ensure that no similar peptide-MHC pairs are present in both the training and testing sets, thus minimizing data leakage.</p>
<div id="30b059bc" class="cell" data-execution_count="7">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb10-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Perform the "Regular C3 split" first</span></span>
<span id="cb10-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># We create a 'SuperGroup' that combines both to ensure total isolation</span></span>
<span id="cb10-3">df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'super_group'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'pep_group'</span>].astype(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"_"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'mhc_group'</span>].astype(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>)</span>
<span id="cb10-4"></span>
<span id="cb10-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Split train vs temp_test</span></span>
<span id="cb10-6">gss <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> GroupShuffleSplit(n_splits<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, train_size<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.7</span>, random_state<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">42</span>)</span>
<span id="cb10-7">train_indices, temp_test_indices <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">next</span>(gss.split(df, groups<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'super_group'</span>]))</span>
<span id="cb10-8"></span>
<span id="cb10-9">train_df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> df.iloc[train_indices][[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Peptide_ID'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Peptide'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'MHC_ID'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'MHC'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Y'</span>]]</span>
<span id="cb10-10">temp_test_df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> df.iloc[temp_test_indices]</span>
<span id="cb10-11"></span>
<span id="cb10-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Split temp_text into test and valid</span></span>
<span id="cb10-13">gss1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> GroupShuffleSplit(n_splits<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, train_size<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.66</span>, random_state<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">42</span>)</span>
<span id="cb10-14">test_indices, valid_indices <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">next</span>(gss1.split(temp_test_df, groups<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>temp_test_df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'super_group'</span>]))</span>
<span id="cb10-15"></span>
<span id="cb10-16">test_df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> df.iloc[test_indices][[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Peptide_ID'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Peptide'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'MHC_ID'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'MHC'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Y'</span>]]</span>
<span id="cb10-17">valid_df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> df.iloc[valid_indices][[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Peptide_ID'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Peptide'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'MHC_ID'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'MHC'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Y'</span>]]</span></code></pre></div></div>
</details>
</div>
<div id="cbc2c3a1" class="cell" data-execution_count="8">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb11-1"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(df)</span></code></pre></div></div>
</details>
</div>
<p><strong>Output:</strong></p>
<pre><code>        Peptide_ID               Peptide                 MHC_ID  \
0            29993       MSGPMQQLTQPLQQV  HLA-DPA10201-DPB11401   
1            50950  CGKYLFNWAVRTKLKLTPIA              DRB1_1302   
2            58992       YKRQLMNILGAVYRY  HLA-DPA10201-DPB10101   
3             8140       FLGCLVKEIPPRLLY  HLA-DQA10501-DQB10201   
4           111514       KTQIDQVESTAGSLQ  HLA-DPA10201-DPB10501   
...            ...                   ...                    ...   
134276       15592       RFFLPIFSEFVLLAT              DRB1_0405   
134277       21667      EVFFQRLGIASGRARY              DRB1_1302   
134278       62271       YLFAKDKSGPLQPGV  HLA-DQA10102-DQB10602   
134279       79102       GELQIVDKIDADFKI              DRB1_1302   
134280      100096       SAAPLRTITADTFRK              DRB1_0701   

                                       MHC         Y  pep_group  mhc_group  \
0       YAFFQFSGGAILNTLHLQFEYFDLEKVRVHLDVT  0.216969       6334          6   
1       QEFFIASGAAVDAIMESSFDYFDIDEATYHVGFT  0.559077        205          3   
2       YAFFQFSGGAILNTLYGQFEYFAIEKVRVHLDVT  0.423504        457          6   
3       YNYHQRXFATVLHSLYFGLSSFAIRKARVHLETT  0.328660       1876         21   
4       YAFFQFSGGAILNTLFGQFEYFEIEKVRMHLDVT  0.000000       1157          6   
...                                    ...       ...        ...        ...   
134276  QEFFIASGAAVDAIMEVHFDYYSLQRATYHVGFT  0.721843       6951          4   
134277  QEFFIASGAAVDAIMESSFDYFDIDEATYHVGFT  0.257618       3632          3   
134278  CNYHQGGGARVAHIMFFGLTYYDVGTETVHVAGI  0.141380       2158          1   
134279  QEFFIASGAAVDAIMESSFDYFDIDEATYHVGFT  0.409599       3568          3   
134280  QEFFIASGAAVDAIMWGYFELYVIDRQTVHVGFT  0.242001       1440         17   

       super_group  
0           6334_6  
1            205_3  
2            457_6  
3          1876_21  
4           1157_6  
...            ...  
134276      6951_4  
134277      3632_3  
134278      2158_1  
134279      3568_3  
134280     1440_17  

[134281 rows x 8 columns]</code></pre>
<p>Now, let’s perform the strict C3 split. In this split, we will ensure that for every pair <img src="https://latex.codecogs.com/png.latex?(Peptide,%20MHC)"> in the test set, both the peptide and MHC classes are entirely absent from the training set. This means that we will first split the MHC groups and then split the peptide groups independently, ensuring that there is no overlap in either component between the training and testing sets.</p>
<div id="69e0f86c" class="cell" data-execution_count="9">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb13-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> strict_double_split(df, train_size<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.7</span>):</span>
<span id="cb13-2">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 1. First, split the MHC groups</span></span>
<span id="cb13-3">    gss_mhc <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> GroupShuffleSplit(n_splits<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, train_size<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>train_size, random_state<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">42</span>)</span>
<span id="cb13-4">    mhc_train_idx, mhc_test_idx <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">next</span>(gss_mhc.split(df, groups<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'mhc_group'</span>]))</span>
<span id="cb13-5">    </span>
<span id="cb13-6">    mhc_train_alleles <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> df.iloc[mhc_train_idx][<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'mhc_group'</span>].unique()</span>
<span id="cb13-7">    mhc_test_alleles <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> df.iloc[mhc_test_idx][<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'mhc_group'</span>].unique()</span>
<span id="cb13-8">    </span>
<span id="cb13-9">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 2. Second, split the Peptides groups</span></span>
<span id="cb13-10">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># This prevents the model from seeing different fragments of the same protein</span></span>
<span id="cb13-11">    gss_pep <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> GroupShuffleSplit(n_splits<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, train_size<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>train_size, random_state<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">42</span>)</span>
<span id="cb13-12">    pep_train_idx, pep_test_idx <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">next</span>(gss_pep.split(df, groups<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'pep_group'</span>]))</span>
<span id="cb13-13">    </span>
<span id="cb13-14">    pep_train_prots <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> df.iloc[pep_train_idx][<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'pep_group'</span>].unique()</span>
<span id="cb13-15">    pep_test_prots <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> df.iloc[pep_test_idx][<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'pep_group'</span>].unique()</span>
<span id="cb13-16">    </span>
<span id="cb13-17">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 3. Create the "Strict" Test Set: </span></span>
<span id="cb13-18">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Only interactions where BOTH the MHC is new AND the Protein is new.</span></span>
<span id="cb13-19">    train_df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> df[df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'mhc_group'</span>].isin(mhc_train_alleles) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'pep_group'</span>].isin(pep_train_prots)].copy()</span>
<span id="cb13-20">    test_df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> df[df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'mhc_group'</span>].isin(mhc_test_alleles) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'pep_group'</span>].isin(pep_test_prots)].copy()</span>
<span id="cb13-21">    </span>
<span id="cb13-22">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> train_df, test_df</span>
<span id="cb13-23"></span>
<span id="cb13-24">train_temp, test_strict <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> strict_double_split(df)</span>
<span id="cb13-25"></span>
<span id="cb13-26"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Random split of data into train or valid data</span></span>
<span id="cb13-27">train_strict, valid_strict <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> train_test_split(train_temp, test_size<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.125</span>, random_state<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">42</span>)</span>
<span id="cb13-28"></span>
<span id="cb13-29"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Save data to disc</span></span>
<span id="cb13-30"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> os</span>
<span id="cb13-31">save_path <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'/data'</span></span>
<span id="cb13-32">train_df.to_feather(os.path.join(save_path, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'train_dat_cold.feather'</span>))</span>
<span id="cb13-33">valid_df.to_feather(os.path.join(save_path, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'valid_dat_cold.feather'</span>))</span>
<span id="cb13-34">test_df.to_feather(os.path.join(save_path, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'test_dat_cold.feather'</span>))</span>
<span id="cb13-35">train_strict.to_feather(os.path.join(save_path, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'train_dat_strict.feather'</span>))</span>
<span id="cb13-36">valid_strict.to_feather(os.path.join(save_path, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'valid_dat_strict.feather'</span>))</span>
<span id="cb13-37">test_strict.to_feather(os.path.join(save_path, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'test_dat_strict.feather'</span>))</span></code></pre></div></div>
</details>
</div>
<div id="c55b4930" class="cell" data-execution_count="10">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb14-1">train_df.shape, test_df.shape, valid_df.shape</span>
<span id="cb14-2">train_strict.shape, test_strict.shape, valid_strict.shape</span></code></pre></div></div>
</details>
</div>
<p><strong>Output:</strong></p>
<pre><code>Regular split shape:
((93392, 5), (26531, 5), (14358, 5))

Strict split shape:
((59757, 8), (9588, 8), (8537, 8))</code></pre>
<p>We can see that the regular C3 split results in a larger training and testing set compared to the strict C3 split. When we prepare the data for model training, we will need to downsample the regular C3 split to match the sample size of the strict C3 split to ensure a fair comparison between the two splitting strategies.</p>
</section>
<section id="embeddings-extraction-and-model-training" class="level2">
<h2 class="anchored" data-anchor-id="embeddings-extraction-and-model-training">2. Embeddings Extraction and Model Training</h2>
<p>Embedding extraction and model training procedures are similar to <a href="https://jaychung10010.quarto.pub/all-things-bioinformatics/posts/P6_cnn_dnn_peptide_mhc/">my previous post</a>, so I will not go through the code in detail here. Let’s load the data and downsample the regular C3 split to match the sample size of the strict C3 split:</p>
<div id="acb104e9" class="cell" data-execution_count="11">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb16-1">all_dat <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {</span>
<span id="cb16-2">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'train'</span>: pd.read_feather(os.path.join(load_path, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'train_dat_cold.feather'</span>)).sample(n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">59757</span>, random_state<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">42</span>).reset_index(drop<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>), </span>
<span id="cb16-3">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'valid'</span>: pd.read_feather(os.path.join(load_path, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'valid_dat_cold.feather'</span>)).sample(n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8537</span>, random_state<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">42</span>).reset_index(drop<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>),</span>
<span id="cb16-4">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'test'</span>: pd.read_feather(os.path.join(load_path, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'test_dat_cold.feather'</span>)).sample(n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9588</span>, random_state<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">42</span>).reset_index(drop<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)</span>
<span id="cb16-5">}</span>
<span id="cb16-6"></span>
<span id="cb16-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Shuffle samples</span></span>
<span id="cb16-8"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> keys, df <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> all_dat.items():</span>
<span id="cb16-9">  all_dat[keys] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> df.sample(frac<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, random_state<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">42</span>).reset_index(drop<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)</span></code></pre></div></div>
</details>
</div>
<p>For both data splits, we will extract ESM2 embeddings (<code>facebook/esm2_t30_150M_UR50D</code>) for the peptides and MHC pseudo-sequences, concatenate them, and then train a neural network model with convolutional layers. The model architecture and training procedure will be the same for both splits.</p>
<p>Here is the model architecture we will use:</p>
<div id="4d7867d2" class="cell" data-execution_count="12">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb17-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Define 1D CNN + attention + MLP with multiple inputs using functional API</span></span>
<span id="cb17-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> tensorflow <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> tf</span>
<span id="cb17-3">tf.keras.backend.clear_session()</span>
<span id="cb17-4"></span>
<span id="cb17-5">tf.random.set_seed(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">42</span>)</span>
<span id="cb17-6"></span>
<span id="cb17-7"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> make_dense_block(n_neurons, dropout_rate<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>):</span>
<span id="cb17-8">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> tf.keras.Sequential([</span>
<span id="cb17-9">        tf.keras.layers.Dense(n_neurons, activation<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'relu'</span>, kernel_initializer<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'he_normal'</span>),</span>
<span id="cb17-10">        tf.keras.layers.BatchNormalization(),</span>
<span id="cb17-11">        tf.keras.layers.Dropout(dropout_rate)</span>
<span id="cb17-12">    ])</span>
<span id="cb17-13"></span>
<span id="cb17-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Define the input layers</span></span>
<span id="cb17-15">embedding_input <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> tf.keras.layers.Input(shape<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>embedding_dict[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'train'</span>].shape[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>:], name<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'embedding_input'</span>)</span>
<span id="cb17-16">peptide_length_input <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> tf.keras.layers.Input(shape<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,), name<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'peptide_length_input'</span>)</span>
<span id="cb17-17">mhc_length_input <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> tf.keras.layers.Input(shape<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,), name<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'mhc_length_input'</span>)</span>
<span id="cb17-18"></span>
<span id="cb17-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># CNN branch for embeddings</span></span>
<span id="cb17-20">x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> tf.keras.layers.Conv1D(filters<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">128</span>, kernel_size<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, padding<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'same'</span>, activation<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'relu'</span>)(embedding_input)</span>
<span id="cb17-21">x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> tf.keras.layers.BatchNormalization()(x)</span>
<span id="cb17-22">x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> tf.keras.layers.Dropout(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>)(x)</span>
<span id="cb17-23">x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> tf.keras.layers.Conv1D(filters<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">64</span>, kernel_size<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, padding<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'same'</span>, activation<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'relu'</span>)(x)</span>
<span id="cb17-24">x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> tf.keras.layers.BatchNormalization()(x)</span>
<span id="cb17-25">x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> tf.keras.layers.Dropout(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>)(x)</span>
<span id="cb17-26">x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> tf.keras.layers.AveragePooling1D(pool_size<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)(x)</span>
<span id="cb17-27"></span>
<span id="cb17-28"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Self-attention layer</span></span>
<span id="cb17-29">attention_output <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> tf.keras.layers.Attention()([x, x])</span>
<span id="cb17-30">flat_attention <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> tf.keras.layers.Flatten()(attention_output)</span>
<span id="cb17-31"></span>
<span id="cb17-32"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Concatenate attention output with length inputs</span></span>
<span id="cb17-33">combined_features <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> tf.keras.layers.Concatenate()([flat_attention, peptide_length_input, mhc_length_input])</span>
<span id="cb17-34"></span>
<span id="cb17-35"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># MLP branch</span></span>
<span id="cb17-36">y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> make_dense_block(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">128</span>)(combined_features)</span>
<span id="cb17-37">y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> make_dense_block(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">128</span>)(y)</span>
<span id="cb17-38">y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> make_dense_block(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">128</span>)(y)</span>
<span id="cb17-39">y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> make_dense_block(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">128</span>)(y)</span>
<span id="cb17-40">y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> make_dense_block(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">128</span>)(y)</span>
<span id="cb17-41">output <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> tf.keras.layers.Dense(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)(y)</span>
<span id="cb17-42"></span>
<span id="cb17-43"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create the functional model</span></span>
<span id="cb17-44">cnn_model <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> tf.keras.Model(inputs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[embedding_input, peptide_length_input, mhc_length_input], outputs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[output])</span>
<span id="cb17-45"></span>
<span id="cb17-46">cnn_model.summary()</span></code></pre></div></div>
</details>
</div>
<pre><code> Model: "functional_5"

┏━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━┓
┃ Layer (type)        ┃ Output Shape      ┃    Param # ┃ Connected to      ┃
┡━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━┩
│ embedding_input     │ (None, 71, 640)   │          0 │ -                 │
│ (InputLayer)        │                   │            │                   │
├─────────────────────┼───────────────────┼────────────┼───────────────────┤
│ conv1d (Conv1D)     │ (None, 71, 128)   │    409,728 │ embedding_input[… │
├─────────────────────┼───────────────────┼────────────┼───────────────────┤
│ batch_normalization │ (None, 71, 128)   │        512 │ conv1d[0][0]      │
│ (BatchNormalizatio… │                   │            │                   │
├─────────────────────┼───────────────────┼────────────┼───────────────────┤
│ dropout (Dropout)   │ (None, 71, 128)   │          0 │ batch_normalizat… │
├─────────────────────┼───────────────────┼────────────┼───────────────────┤
│ conv1d_1 (Conv1D)   │ (None, 71, 64)    │     24,640 │ dropout[0][0]     │
├─────────────────────┼───────────────────┼────────────┼───────────────────┤
│ batch_normalizatio… │ (None, 71, 64)    │        256 │ conv1d_1[0][0]    │
│ (BatchNormalizatio… │                   │            │                   │
├─────────────────────┼───────────────────┼────────────┼───────────────────┤
│ dropout_1 (Dropout) │ (None, 71, 64)    │          0 │ batch_normalizat… │
├─────────────────────┼───────────────────┼────────────┼───────────────────┤
│ average_pooling1d   │ (None, 35, 64)    │          0 │ dropout_1[0][0]   │
│ (AveragePooling1D)  │                   │            │                   │
├─────────────────────┼───────────────────┼────────────┼───────────────────┤
│ attention           │ (None, 35, 64)    │          0 │ average_pooling1… │
│ (Attention)         │                   │            │ average_pooling1… │
├─────────────────────┼───────────────────┼────────────┼───────────────────┤
│ flatten (Flatten)   │ (None, 2240)      │          0 │ attention[0][0]   │
├─────────────────────┼───────────────────┼────────────┼───────────────────┤
│ peptide_length_inp… │ (None, 1)         │          0 │ -                 │
│ (InputLayer)        │                   │            │                   │
├─────────────────────┼───────────────────┼────────────┼───────────────────┤
│ mhc_length_input    │ (None, 1)         │          0 │ -                 │
│ (InputLayer)        │                   │            │                   │
├─────────────────────┼───────────────────┼────────────┼───────────────────┤
│ concatenate         │ (None, 2242)      │          0 │ flatten[0][0],    │
│ (Concatenate)       │                   │            │ peptide_length_i… │
│                     │                   │            │ mhc_length_input… │
├─────────────────────┼───────────────────┼────────────┼───────────────────┤
│ sequential          │ (None, 128)       │    287,616 │ concatenate[0][0] │
│ (Sequential)        │                   │            │                   │
├─────────────────────┼───────────────────┼────────────┼───────────────────┤
│ sequential_1        │ (None, 128)       │     17,024 │ sequential[0][0]  │
│ (Sequential)        │                   │            │                   │
├─────────────────────┼───────────────────┼────────────┼───────────────────┤
│ sequential_2        │ (None, 128)       │     17,024 │ sequential_1[0][… │
│ (Sequential)        │                   │            │                   │
├─────────────────────┼───────────────────┼────────────┼───────────────────┤
│ sequential_3        │ (None, 128)       │     17,024 │ sequential_2[0][… │
│ (Sequential)        │                   │            │                   │
├─────────────────────┼───────────────────┼────────────┼───────────────────┤
│ sequential_4        │ (None, 128)       │     17,024 │ sequential_3[0][… │
│ (Sequential)        │                   │            │                   │
├─────────────────────┼───────────────────┼────────────┼───────────────────┤
│ dense_5 (Dense)     │ (None, 1)         │        129 │ sequential_4[0][… │
└─────────────────────┴───────────────────┴────────────┴───────────────────┘

 Total params: 790,977 (3.02 MB)

 Trainable params: 789,313 (3.01 MB)

 Non-trainable params: 1,664 (6.50 KB)</code></pre>
<p>Training with max 100 epochs, with early stopping and performance scheduler callbacks:</p>
<div id="852571e2" class="cell" data-execution_count="13">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb19-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Performance scheduling of learning rate</span></span>
<span id="cb19-2">lr_scheduler <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> tf.keras.callbacks.ReduceLROnPlateau(factor<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, patience<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>)</span>
<span id="cb19-3"></span>
<span id="cb19-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Early stopping</span></span>
<span id="cb19-5">early_stopping <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> tf.keras.callbacks.EarlyStopping(patience<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, restore_best_weights<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)</span>
<span id="cb19-6"></span>
<span id="cb19-7">optimizer <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> tf.keras.optimizers.Adam(learning_rate<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e-4</span>)</span>
<span id="cb19-8">cnn_model.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">compile</span>(loss<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'mse'</span>, optimizer<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>optimizer, metrics<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'RootMeanSquaredError'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'R2Score'</span>])</span>
<span id="cb19-9">fit_history <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> cnn_model.fit(train_tfds, epochs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, validation_data<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>valid_tfds,</span>
<span id="cb19-10">                            callbacks<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[lr_scheduler, early_stopping])</span></code></pre></div></div>
</details>
</div>
<p>Regular C3 split training metrics: <img src="https://jaychung10010.quarto.pub/all-things-bioinformatics/posts/P7_data_leakage_peptide_mhc/images/r_train.png" class="img-fluid"></p>
<p>Strict C3 split training metrics: <img src="https://jaychung10010.quarto.pub/all-things-bioinformatics/posts/P7_data_leakage_peptide_mhc/images/s_train.png" class="img-fluid"></p>
</section>
<section id="comparing-regular-vs.-strict-c3-split" class="level2">
<h2 class="anchored" data-anchor-id="comparing-regular-vs.-strict-c3-split">3. Comparing Regular vs.&nbsp;Strict C3 split</h2>
<p>We will now compare the performance of the model trained on the regular C3 split and the strict C3 split using an independent test set. We will evaluate the models using metrics such as <img src="https://latex.codecogs.com/png.latex?R%5E2"> score, root mean squared error (RMSE), and loss.</p>
<div id="738c5f58" class="cell" data-execution_count="14">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb20-1">cnn_model.evaluate(X_test_inputs, y_test)</span></code></pre></div></div>
</details>
</div>
<p><strong>Regular C3 split test metrics:</strong></p>
<pre><code>300/300 ━━━━━━━━━━━━━━━━━━━━ 4s 8ms/step - R2Score: 0.6597 - RootMeanSquaredError: 0.1524 - loss: 0.0232
[0.023598162457346916, 0.15361693501472473, 0.6540257930755615]</code></pre>
<p><strong>Strict C3 split test metrics:</strong></p>
<pre><code>300/300 ━━━━━━━━━━━━━━━━━━━━ 5s 13ms/step - R2Score: 0.1770 - RootMeanSquaredError: 0.2391 - loss: 0.0572
[0.05780040845274925, 0.24041715264320374, 0.18053573369979858]</code></pre>
<p>As we can see, the model trained on the regular C3 split performs significantly better on the test set compared to the model trained on the strict C3 split. The <img src="https://latex.codecogs.com/png.latex?R%5E2"> score is much higher and the RMSE is much lower for the regular C3 split. This suggests that although the model generalizes well on predicting binding peptides of seen MHC2 alleles, it struggles to generalize to unseen MHC2 alleles when using the strict C3 split.</p>
<p>Let’s take a look at the prediction plots:</p>
<p>Regular C3 split prediction plot for all MHC2: <img src="https://jaychung10010.quarto.pub/all-things-bioinformatics/posts/P7_data_leakage_peptide_mhc/images/r_pred.png" class="img-fluid"></p>
<p>Strict C3 split prediction plot for all MHC2: <img src="https://jaychung10010.quarto.pub/all-things-bioinformatics/posts/P7_data_leakage_peptide_mhc/images/s_pred.png" class="img-fluid"></p>
<p>Regular C3 split prediction plot separated by MHC2: <img src="https://jaychung10010.quarto.pub/all-things-bioinformatics/posts/P7_data_leakage_peptide_mhc/images/r_pred_sep.png" class="img-fluid"></p>
<p>Strict C3 split prediction plot separated by MHC2: <img src="https://jaychung10010.quarto.pub/all-things-bioinformatics/posts/P7_data_leakage_peptide_mhc/images/s_pred_sep.png" class="img-fluid"></p>
<p>As we can see, while the regular C3 split shows good performance across multiple MHC2 alleles, with some achieving an <img src="https://latex.codecogs.com/png.latex?R%5E2"> score &gt; 0.8, the strict C3 split shows max <img src="https://latex.codecogs.com/png.latex?R%5E2"> score of around 0.2 for the best performing MHC2 allele.</p>
<p>Notably, although the total test sample numbers are the same between the two split, due to the strict nature of the strict C3 split, the variety of MHC2 alleles in the test set is much smaller compared to the regular C3 split. Thus, we are perhaps being a little “strict” in our evaluation, since the strict C3 split has not been evaluated on many MHC2 alleles.</p>
<p>This result suggests important limitations for this model: it predicts well on MHC2 alleles that are present in the training data, but it does not perform well on unseen or novel MHC2 alleles. Perhaps the model is very good at memorizing specific interactions between peptides and MHC2 alleles, but did not learn well the general rules of protein-protein interaction, which is also a significant challenge for many PPI prediction models utilizing pLLMs. Some potential solutions to this issue could include:</p>
<ul>
<li>Increasing the diversity of the training data to include a wider range of MHC2 alleles, which may help the model learn more generalizable patterns.</li>
<li>Incorporating additional features or using more complex model architectures that can capture the underlying biology of peptide binding to MHC2 molecules, rather than relying solely on the embeddings from the pre-trained pLLM.</li>
<li>Re-train a pLLM on strict data with held out samples, so that the embeddings themselves are less prone to data leakage and more generalizable to unseen data.</li>
</ul>
</section>
<section id="conclusion" class="level2">
<h2 class="anchored" data-anchor-id="conclusion">Conclusion</h2>
<p>In this project, I explored the issue of data leakage in peptide-MHC2 binding prediction models using pre-trained protein language models (pLLMs). I compared two data splitting strategies, a regular C3 split and a strict C3 split, to evaluate the extent of data leakage and its impact on model performance. My findings showed that while the model trained on the regular C3 split performed well on the test set, it struggled to generalize to unseen MHC2 alleles when evaluated using the strict C3 split. This highlights the importance of using stringent data splitting strategies to ensure that models are evaluated on truly unseen data, which is crucial for developing robust and generalizable models in protein binding affinity prediction.</p>
</section>
<section id="references" class="level2">
<h2 class="anchored" data-anchor-id="references">References</h2>
<ul>
<li><a href="https://doi.org/10.1038/nmeth.2259">Park, Y., Marcotte, E. Flaws in evaluation schemes for pair-input computational predictions. Nat Methods 9, 1134–1136 (2012).</a></li>
<li><a href="https://www.biorxiv.org/content/10.1101/2025.04.21.649858v2">Szymborski, Joseph, and Amin Emad. “Data for”A Flaw in Using Pre-trained pLMs in Protein-protein Interaction Inference Models””. bioRxiv, December 5, 2025.</a></li>
<li><a href="https://www.science.org/doi/10.1126/science.ade2574">Lin Z, Akin H, Rao R, Hie B, Zhu Z, Lu W, Smetanin N, Verkuil R, Kabeli O, Shmueli Y, Dos Santos Costa A, Fazel-Zarandi M, Sercu T, Candido S, Rives A. Evolutionary-scale prediction of atomic-level protein structure with a language model. Science. 2023 Mar 17;379(6637):1123-1130. doi: 10.1126/science.ade2574. Epub 2023 Mar 16. PMID: 36927031.</a></li>
</ul>


</section>

 ]]></description>
  <category>Python</category>
  <category>Deep Learning</category>
  <category>Convolutional Neural Networks</category>
  <category>Self-Attention</category>
  <category>Keras</category>
  <category>ESM2</category>
  <category>Protein Language Models</category>
  <category>Data Leakage</category>
  <guid>https://jaychung10010.quarto.pub/all-things-bioinformatics/posts/P7_data_leakage_peptide_mhc/</guid>
  <pubDate>Wed, 25 Feb 2026 08:00:00 GMT</pubDate>
</item>
<item>
  <title>Peptide-MHCII Binding Affinity Prediction with Convolutional Neural Networks and Protein Large Language Model</title>
  <dc:creator>Jay Chung</dc:creator>
  <link>https://jaychung10010.quarto.pub/all-things-bioinformatics/posts/P6_cnn_dnn_peptide_mhc/</link>
  <description><![CDATA[ 





<section id="research-impact" class="level2">
<h2 class="anchored" data-anchor-id="research-impact">Research Impact</h2>
<p>To accurately predict peptide-MHCII binding affinity, I developed a 1D Convolutional Neural Network (CNN) combined with Multi-Layer Perceptron (MLP) model using protein embeddings from the ESM2 large language model.</p>
<p>Comparing with a simpler MLP-only model, the CNN + MLP model demonstrated over 12% improvement in peptide-MHCII binding prediction accuracy, achieving an <img src="https://latex.codecogs.com/png.latex?R%5E2"> score of 0.62 for all MHC alleles, and close to 0.8 for specific alleles. Saliency Map and In Silico Mutagenesis analyses revealed key amino acid positions that significantly influence binding affinity predictions, providing insights into the underlying biological interactions.</p>
<p>This exercise showcases the potential of combining Large Language Models with machine learning to tackle complex biological challenges. These could range from predicting protein-protein interactions and drug-target binding to forecasting drug responses from gene expression data.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jaychung10010.quarto.pub/all-things-bioinformatics/posts/P6_cnn_dnn_peptide_mhc/images/summary_figure.jpeg" class="img-fluid figure-img"></p>
<figcaption>Schematic overview of the CNN + MLP pipeline for predicting peptide-MHC binding affinity (figure generated with the assistance of Gemini).</figcaption>
</figure>
</div>
</section>
<section id="introduction" class="level2">
<h2 class="anchored" data-anchor-id="introduction">Introduction</h2>
<p>In <a href="https://jaychung10010.quarto.pub/all-things-bioinformatics/posts/2026-01-18_peptide_affinity_llm_nn/">my last post</a>, I explored the use of protein large language model (LLM) embeddings from ESM2 for predicting peptide-MHCII binding affinity using a Multi-Layer Perceptron (MLP). While the MLP performed reasonably well, I believe that incorporating Convolutional Neural Networks (CNNs) could further enhance the model’s ability to capture local sequence patterns in the protein embeddings.</p>
<p>The protein sequence embeddings generated by ESM2 are typically 2-dimensional arrays, where one dimension represents the amino acid sequence and the other represents the embedding features. In my previous approach, I flattened these embeddings into 1-dimensional vectors by taking the mean before feeding them into the MLP. However, this flattening process may lead to the loss of important spatial relationships between amino acids in the sequence.</p>
<p>In this post, I will implement a 1D CNN to process the full embeddings from ESM2 without pre-flattening. I will apply 2 layers of 1D convolution followed by average pooling to capture local patterns in the sequence embeddings. After the CNN layers, I will add fully connected MLP layers to perform the final prediction task. I will compare the performance of this CNN + MLP model with a similar MLP-only model to evaluate the impact of incorporating convolutional layers.</p>
<p>Finally, to understand which amino acid positions contribute most to the binding affinity predictions, I will perform Saliency Map analysis on both the peptide and MHCII sequences, using gradients calculated from the model outputs with respect to the input embeddings. I will also perform an In Silico Mutagenesis (ISM) analysis to see how disruptive single amino acid changes affect the predicted binding affinity.</p>
</section>
<section id="key-steps" class="level2">
<h2 class="anchored" data-anchor-id="key-steps">Key Steps</h2>
<p>I will not go through all the steps in detail, as they are similar to my previous post. Instead, I will highlight the key steps involved in this approach:</p>
<ol type="1">
<li><p><strong>Embeddings Extraction</strong>: We will have to modify the embeddings extraction code to retain the 2D structure of the embeddings. We need to input the max sequence length to ensure consistent input sizes for the model. After that, we will concatenate the peptide and MHC2 embeddings along the sequence length dimension, retaining the sequence information and the 2D structure. This allows for the saliency map analysis later on.</p></li>
<li><p><strong>Model Architecture</strong>: We will define a new model architecture that includes 1D convolutional layers followed by dense layers. Using the functional API in TensorFlow/Keras, we will create a model that takes the 2D embeddings and peptide/MHC sequence lengths as inputs, applies convolutional layers, and then concatenates them to pass it through dense layers to make the final prediction.</p></li>
<li><p><strong>Training and Evaluation</strong>: We will train the new model and evaluate its performance against a similar MLP-only model, to see if the CNN layers improve binding affinity prediction.</p></li>
<li><p><strong>Saliency Map and In Silico Mutagenesis Analysis</strong>: We will compute saliency maps to identify important amino acid positions in the peptide and MHC2 sequences that influence binding affinity predictions. We will also perform ISM to assess the impact of single amino acid mutations on the predicted affinity.</p></li>
</ol>
</section>
<section id="embeddings-extraction" class="level2">
<h2 class="anchored" data-anchor-id="embeddings-extraction">1. Embeddings Extraction</h2>
<p>First, let’s see what the input data looks like:</p>
<div id="d3e8eebb" class="cell" data-execution_count="1">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1">all_dat[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'train'</span>].head()</span></code></pre></div></div>
</details>
</div>
<p><strong>Output:</strong></p>
<pre><code>   Peptide_ID           Peptide     MHC_ID  \
0      104653   VAPIEHIASMRRNYF  DRB1_1302   
1       37106   HDDKETSFIRNCARK  DRB1_0101   
2      118433   LIWVGINTRNMTMSM  DRB1_0101   
3       80770  GVTVIKNNMINNDLGP  DRB1_1501   
4       19888   PAPMLAAAAGWQTLS  DRB1_1101   

                                  MHC         Y  
0  QEFFIASGAAVDAIMESSFDYFDIDEATYHVGFT  0.501084  
1  QEFFIASGAAVDAIMWLFLECYDLQRATYHVGFT  0.441298  
2  QEFFIASGAAVDAIMWLFLECYDLQRATYHVGFT  0.217673  
3  QEFFIASGAAVDAIMWPRFDYFDIQAATYHVVFT  0.807811  
4  QEFFIASGAAVDAIMESSFDYFDFDRATYHVGFT  0.583271  </code></pre>
<p><code>all_dat</code> is a dictionary containing the training, validation, and test datasets as dataframes, each with peptide and MHC2 sequences, and binding affinity Y.</p>
<p>The embeddings extraction code to retain the 2D structure of the embeddings:</p>
<div id="7f3d8d49" class="cell" data-execution_count="2">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb3-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> transformers <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> AutoTokenizer, EsmModel</span>
<span id="cb3-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> numpy <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> np</span>
<span id="cb3-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> torch</span>
<span id="cb3-4"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> math</span>
<span id="cb3-5"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> tqdm <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> tqdm</span>
<span id="cb3-6"></span>
<span id="cb3-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Load pre-trained ESM2 model and tokenizer</span></span>
<span id="cb3-8">model_checkpoint <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"facebook/esm2_t30_150M_UR50D"</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 640 dim</span></span>
<span id="cb3-9">tokenizer <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> AutoTokenizer.from_pretrained(model_checkpoint)</span>
<span id="cb3-10">model <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> EsmModel.from_pretrained(model_checkpoint)</span>
<span id="cb3-11"></span>
<span id="cb3-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Function to get embeddings</span></span>
<span id="cb3-13"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> extract_full_embedding(</span>
<span id="cb3-14">    sequence: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>],</span>
<span id="cb3-15">    tokenizer: AutoTokenizer,</span>
<span id="cb3-16">    model: EsmModel,</span>
<span id="cb3-17">    device: torch.device <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>,</span>
<span id="cb3-18">    batch_size: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">64</span>,</span>
<span id="cb3-19">    max_len: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span></span>
<span id="cb3-20">    ) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> np.ndarray:</span>
<span id="cb3-21"></span>
<span id="cb3-22">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""Extract full embeddings for peptide sequences from an LLM model.</span></span>
<span id="cb3-23"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">       Batch iteration is required as this is memory intensive."""</span></span>
<span id="cb3-24"></span>
<span id="cb3-25">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Use GPU when available</span></span>
<span id="cb3-26">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">not</span> device:</span>
<span id="cb3-27">        device <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> torch.device(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cuda"</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> torch.cuda.is_available() <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cpu"</span>)</span>
<span id="cb3-28"></span>
<span id="cb3-29">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Separate sequence list into batches</span></span>
<span id="cb3-30">    n_batches <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> math.ceil(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(sequence) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> batch_size)</span>
<span id="cb3-31">    all_batch_embeddings <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb3-32"></span>
<span id="cb3-33">    model <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> model.to(device) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Move model to the target device</span></span>
<span id="cb3-34">    model.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">eval</span>() <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Set model to evaluation mode</span></span>
<span id="cb3-35"></span>
<span id="cb3-36">    steps <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> tqdm(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(n_batches))</span>
<span id="cb3-37"></span>
<span id="cb3-38">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> steps:</span>
<span id="cb3-39">        steps.set_description(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Processing batch </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">/</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>n_batches<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb3-40">        start <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> i <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> batch_size</span>
<span id="cb3-41">        end <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (i <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> batch_size</span>
<span id="cb3-42">        batch <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> sequence[start:end]</span>
<span id="cb3-43"></span>
<span id="cb3-44">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Tokenize peptide sequence and pad to max length</span></span>
<span id="cb3-45">        inputs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> tokenizer(batch, return_tensors<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pt"</span>, padding<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'max_length'</span>, truncation<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>, max_length<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>max_len)</span>
<span id="cb3-46"></span>
<span id="cb3-47">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Move input to the target device</span></span>
<span id="cb3-48">        inputs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {name: tensor.to(device) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> name, tensor <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> inputs.items()}</span>
<span id="cb3-49"></span>
<span id="cb3-50">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Forward pass through the model without gradient tracking to get embeddings</span></span>
<span id="cb3-51">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">with</span> torch.no_grad():</span>
<span id="cb3-52">          batch_mean_embeddings <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> model(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>inputs).last_hidden_state.detach().cpu().numpy()</span>
<span id="cb3-53">        all_batch_embeddings.append(batch_mean_embeddings)</span>
<span id="cb3-54"></span>
<span id="cb3-55">    embeddings <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.concatenate(all_batch_embeddings, axis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb3-56">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># This will return a (N_samples, max_len, n_dim) array</span></span>
<span id="cb3-57">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> embeddings</span></code></pre></div></div>
</details>
</div>
<p>Since each peptide and MHC2 sequence can have different lengths, we need to pad them to a consistent length during the embedding extraction so that they can be concatenated later. Let’s get the max sequence lengths for peptides or MHC2 sequences so we can pad the embeddings accordingly.</p>
<div id="1f22821b" class="cell" data-execution_count="3">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1">length_pt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb4-2"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> df <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> all_dat.values():</span>
<span id="cb4-3">  length_pt.append(df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Peptide'</span>].<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">apply</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>).<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">max</span>())</span>
<span id="cb4-4">max_len_pt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">max</span>(length_pt)</span>
<span id="cb4-5"></span>
<span id="cb4-6">length_mhc <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb4-7"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> df <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> all_dat.values():</span>
<span id="cb4-8">  length_mhc.append(df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'MHC'</span>].<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">apply</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>).<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">max</span>())</span>
<span id="cb4-9">max_len_mhc <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">max</span>(length_mhc)</span>
<span id="cb4-10"></span>
<span id="cb4-11"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(max_len_pt, max_len_mhc)</span></code></pre></div></div>
</details>
</div>
<p><strong>Output:</strong></p>
<pre><code>37 34</code></pre>
<p>The maximum peptide length is 37, and the maximum MHC2 length is 34.</p>
<p>Now we can extract the full embeddings for both peptides and MHC2 sequences, and pad them each to their respective max lengths.</p>
<div id="d420e238" class="cell" data-execution_count="4">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb6-1">embedding_dict_pt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {}</span>
<span id="cb6-2"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> data, sequence <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> sequence_dict_pt.items():</span>
<span id="cb6-3">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Extracting embeddings for </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">..."</span>)</span>
<span id="cb6-4">    embedding_dict_pt[data] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> extract_full_embedding(sequence, tokenizer, model, batch_size<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1280</span>, max_len<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>max_len_pt)</span>
<span id="cb6-5">    </span>
<span id="cb6-6">embedding_dict_mhc <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {}</span>
<span id="cb6-7"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> data, sequence <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> sequence_dict_mhc.items():</span>
<span id="cb6-8">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Extracting embeddings for </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">..."</span>)</span>
<span id="cb6-9">    embedding_dict_mhc[data] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> extract_full_embedding(sequence, tokenizer, model, batch_size<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1280</span>, max_len<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>max_len_mhc)</span></code></pre></div></div>
</details>
</div>
<p>Concatenate the peptide and MHC2 embeddings along the sequence dimension (axis=1) to create the final input embeddings for our model.</p>
<div id="3cda9488" class="cell" data-execution_count="5">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb7-1">embedding_dict <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {}</span>
<span id="cb7-2"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> data_type <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'train'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'valid'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'test'</span>]:</span>
<span id="cb7-3">  pt_embeddings <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> embedding_dict_pt[<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f'</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>data_type<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">_pt'</span>]</span>
<span id="cb7-4">  mhc_embeddings <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> embedding_dict_mhc[<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f'</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>data_type<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">_mhc'</span>]</span>
<span id="cb7-5">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Concatenate along the sequence length axis</span></span>
<span id="cb7-6">  embedding_dict[data_type] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.concatenate([pt_embeddings, mhc_embeddings], axis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb7-7"></span>
<span id="cb7-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Print shapes to verify</span></span>
<span id="cb7-9"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> key, value <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> embedding_dict.items():</span>
<span id="cb7-10">  <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Shape of </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>key<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> combined embeddings: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>value<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>shape<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div></div>
</details>
</div>
<p><strong>Output:</strong></p>
<pre><code>Shape of train combined embeddings: (82388, 71, 640)
Shape of valid combined embeddings: (11702, 71, 640)
Shape of test combined embeddings: (23410, 71, 640)</code></pre>
<p>The output here is: (sample size, sequence_length, embedding_dimension).</p>
<p>Finally, we need to determine the actual lengths of each peptide and MHC2 sequence (before padding) to provide as additional inputs to the model:</p>
<div id="cb3cdb7e" class="cell" data-execution_count="6">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb9-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Determine each peptide or MHC sequence length to add into the embeddings for training input</span></span>
<span id="cb9-2">seq_length_pt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {}</span>
<span id="cb9-3"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> key, sequence <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> sequence_dict_pt.items():</span>
<span id="cb9-4">    seq_length_pt[key] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(seq) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> seq <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> sequence]</span>
<span id="cb9-5"></span>
<span id="cb9-6">seq_length_mhc <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {}</span>
<span id="cb9-7"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> key, sequence <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> sequence_dict_mhc.items():</span>
<span id="cb9-8">    seq_length_mhc[key] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(seq) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> seq <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> sequence]</span></code></pre></div></div>
</details>
</div>
</section>
<section id="model-architecture" class="level2">
<h2 class="anchored" data-anchor-id="model-architecture">2. Model Architecture</h2>
<p>Make the TensorFlow datasets from these 2D embeddings.</p>
<div id="17a28dfd" class="cell" data-execution_count="7">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb10-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Convert data to Tensorflow dataset</span></span>
<span id="cb10-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> tensorflow <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> tf</span>
<span id="cb10-3"></span>
<span id="cb10-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Embeddings (already concatenated along sequence length axis)</span></span>
<span id="cb10-5">X_train_emb <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> embedding_dict[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'train'</span>]</span>
<span id="cb10-6">X_valid_emb <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> embedding_dict[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'valid'</span>]</span>
<span id="cb10-7">X_test_emb <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> embedding_dict[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'test'</span>]</span>
<span id="cb10-8"></span>
<span id="cb10-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Target variable</span></span>
<span id="cb10-10">y_train <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> all_dat[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'train'</span>][<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Y'</span>].values</span>
<span id="cb10-11">y_valid <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> all_dat[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'valid'</span>][<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Y'</span>].values</span>
<span id="cb10-12">y_test <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> all_dat[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'test'</span>][<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Y'</span>].values</span>
<span id="cb10-13"></span>
<span id="cb10-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Sequence lengths (converting lists to numpy arrays and then to tf tensors)</span></span>
<span id="cb10-15">X_train_pt_len <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> tf.convert_to_tensor(np.array(seq_length_pt[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'train_pt'</span>]), dtype<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>tf.float32)</span>
<span id="cb10-16">X_train_mhc_len <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> tf.convert_to_tensor(np.array(seq_length_mhc[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'train_mhc'</span>]), dtype<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>tf.float32)</span>
<span id="cb10-17">X_valid_pt_len <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> tf.convert_to_tensor(np.array(seq_length_pt[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'valid_pt'</span>]), dtype<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>tf.float32)</span>
<span id="cb10-18">X_valid_mhc_len <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> tf.convert_to_tensor(np.array(seq_length_mhc[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'valid_mhc'</span>]), dtype<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>tf.float32)</span>
<span id="cb10-19">X_test_pt_len <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> tf.convert_to_tensor(np.array(seq_length_pt[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'test_pt'</span>]), dtype<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>tf.float32)</span>
<span id="cb10-20">X_test_mhc_len <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> tf.convert_to_tensor(np.array(seq_length_mhc[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'test_mhc'</span>]), dtype<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>tf.float32)</span>
<span id="cb10-21"></span>
<span id="cb10-22"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create tf.data.Dataset with multiple inputs</span></span>
<span id="cb10-23">train_tfds <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> tf.data.Dataset.from_tensor_slices(((X_train_emb, X_train_pt_len, X_train_mhc_len), y_train))</span>
<span id="cb10-24"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Shuffle, batch and prefetch the train tfds</span></span>
<span id="cb10-25">train_tfds <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> train_tfds.shuffle(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1024</span>, seed<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">42</span>).batch(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">32</span>).prefetch(tf.data.AUTOTUNE)</span>
<span id="cb10-26"></span>
<span id="cb10-27">valid_tfds <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> tf.data.Dataset.from_tensor_slices(((X_valid_emb, X_valid_pt_len, X_valid_mhc_len), y_valid))</span>
<span id="cb10-28">valid_tfds <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> valid_tfds.batch(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">32</span>).prefetch(tf.data.AUTOTUNE)</span>
<span id="cb10-29"></span>
<span id="cb10-30"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># For evaluation on X_test, we will need to ensure it's a tuple of tensors</span></span>
<span id="cb10-31">X_test_inputs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (X_test_emb, X_test_pt_len, X_test_mhc_len)</span>
<span id="cb10-32"></span>
<span id="cb10-33"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># For X_train_inputs, we will need to ensure it's a tuple of tensors for R2 evaluation</span></span>
<span id="cb10-34">X_train_inputs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (X_train_emb, X_train_pt_len, X_train_mhc_len)</span></code></pre></div></div>
</details>
</div>
<p>Define the CNN + MLP model architecture. I’m using average pooling rather than max pooling as I find it tends to work better for this task, likely because it captures the overall presence of features rather than just the strongest activation.</p>
<div id="ee0af507" class="cell" data-execution_count="8">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb11-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Define 1D CNN + MLP with multiple inputs using functional API</span></span>
<span id="cb11-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> tensorflow <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> tf</span>
<span id="cb11-3">tf.keras.backend.clear_session()</span>
<span id="cb11-4"></span>
<span id="cb11-5">tf.random.set_seed(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">42</span>)</span>
<span id="cb11-6"></span>
<span id="cb11-7"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> make_dense_block(n_neurons, dropout_rate<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>):</span>
<span id="cb11-8">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> tf.keras.Sequential([</span>
<span id="cb11-9">        tf.keras.layers.Dense(n_neurons, activation<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'relu'</span>, kernel_initializer<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'he_normal'</span>),</span>
<span id="cb11-10">        tf.keras.layers.BatchNormalization(),</span>
<span id="cb11-11">        tf.keras.layers.Dropout(dropout_rate)</span>
<span id="cb11-12">    ])</span>
<span id="cb11-13"></span>
<span id="cb11-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Define the input layers</span></span>
<span id="cb11-15">embedding_input <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> tf.keras.layers.Input(shape<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>embedding_dict[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'train'</span>].shape[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>:], name<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'embedding_input'</span>)</span>
<span id="cb11-16">peptide_length_input <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> tf.keras.layers.Input(shape<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,), name<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'peptide_length_input'</span>)</span>
<span id="cb11-17">mhc_length_input <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> tf.keras.layers.Input(shape<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,), name<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'mhc_length_input'</span>)</span>
<span id="cb11-18"></span>
<span id="cb11-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># CNN branch for embeddings</span></span>
<span id="cb11-20">x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> tf.keras.layers.Conv1D(filters<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">128</span>, kernel_size<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, padding<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'same'</span>, activation<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'relu'</span>)(embedding_input)</span>
<span id="cb11-21">x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> tf.keras.layers.BatchNormalization()(x)</span>
<span id="cb11-22">x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> tf.keras.layers.Dropout(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>)(x)</span>
<span id="cb11-23">x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> tf.keras.layers.Conv1D(filters<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">64</span>, kernel_size<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, padding<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'same'</span>, activation<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'relu'</span>)(x)</span>
<span id="cb11-24">x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> tf.keras.layers.BatchNormalization()(x)</span>
<span id="cb11-25">x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> tf.keras.layers.Dropout(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>)(x)</span>
<span id="cb11-26">x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> tf.keras.layers.AveragePooling1D(pool_size<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)(x)</span>
<span id="cb11-27">x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> tf.keras.layers.Flatten()(x)</span>
<span id="cb11-28"></span>
<span id="cb11-29"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Concatenate CNN output with length inputs</span></span>
<span id="cb11-30">combined_features <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> tf.keras.layers.Concatenate()([x, peptide_length_input, mhc_length_input])</span>
<span id="cb11-31"></span>
<span id="cb11-32"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># MLP branch</span></span>
<span id="cb11-33">y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> make_dense_block(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">128</span>)(combined_features)</span>
<span id="cb11-34">y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> make_dense_block(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">128</span>)(y)</span>
<span id="cb11-35">y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> make_dense_block(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">128</span>)(y)</span>
<span id="cb11-36">y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> make_dense_block(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">128</span>)(y)</span>
<span id="cb11-37">y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> make_dense_block(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">128</span>)(y)</span>
<span id="cb11-38">output <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> tf.keras.layers.Dense(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)(y)</span>
<span id="cb11-39"></span>
<span id="cb11-40"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create the functional model</span></span>
<span id="cb11-41">cnn_model <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> tf.keras.Model(inputs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[embedding_input, peptide_length_input, mhc_length_input], outputs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[output])</span>
<span id="cb11-42"></span>
<span id="cb11-43">cnn_model.summary()</span></code></pre></div></div>
</details>
</div>
<p>As we can see below, under the CNN architecture, the filter effectively compresses the embedding dimension from 640 down to 128, and then to 64. We set the padding to ‘same’ to retain the original sequence length of 71 after convolution. But if set to ‘valid’, the sequence length would reduce after each convolution layer, depending on the kernel size. The average pooling layer then reduces the sequence length dimension according to the pool size of 2 (basically halves it), and finally, we flatten the CNN output before concatenating it with the peptide and MHC2 lengths. This results in a feature vector of size 64 * 35 = 2240 from the CNN branch, which is then concatenated with the two length inputs, giving a total of 2242 features fed into the dense layers. Ultimately, the pooling layers help to prevent overfitting and save computational resources by reducing the dimensionality of the feature maps.</p>
<p><img src="https://jaychung10010.quarto.pub/all-things-bioinformatics/posts/P6_cnn_dnn_peptide_mhc/images/cnn_model_summary.png" class="img-fluid"></p>
<p>Compare this with the MLP-only model architecture that we will compare against:</p>
<div id="4e561b06" class="cell" data-execution_count="9">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb12-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Define MLP layers</span></span>
<span id="cb12-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Define a helper function for the repetitive dense -&gt; batch norm -&gt; dropout block</span></span>
<span id="cb12-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> make_dense_block(n_neurons, dropout_rate<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>):</span>
<span id="cb12-4">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> tf.keras.Sequential([</span>
<span id="cb12-5">        tf.keras.layers.Dense(n_neurons, activation<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'relu'</span>, kernel_initializer<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'he_normal'</span>),</span>
<span id="cb12-6">        tf.keras.layers.BatchNormalization(),</span>
<span id="cb12-7">        tf.keras.layers.Dropout(dropout_rate)</span>
<span id="cb12-8">    ])</span>
<span id="cb12-9"></span>
<span id="cb12-10">tf.random.set_seed(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">42</span>)</span>
<span id="cb12-11"></span>
<span id="cb12-12">dnn_model <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> tf.keras.Sequential([</span>
<span id="cb12-13">    tf.keras.layers.Input(shape<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>X_train.shape[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>:]),</span>
<span id="cb12-14">    make_dense_block(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">512</span>),</span>
<span id="cb12-15">    make_dense_block(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">256</span>),</span>
<span id="cb12-16">    make_dense_block(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">128</span>),</span>
<span id="cb12-17">    make_dense_block(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">128</span>),</span>
<span id="cb12-18">    make_dense_block(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">128</span>),</span>
<span id="cb12-19">    tf.keras.layers.Dense(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb12-20">])</span></code></pre></div></div>
</details>
</div>
</section>
<section id="training-and-evaluation" class="level2">
<h2 class="anchored" data-anchor-id="training-and-evaluation">3. Training and Evaluation</h2>
<p>Set performance scheduling, early stopping, optimizer, compile model, and train the CNN + MLP model. The MLP-only model is trained with the same settings for comparison.</p>
<div id="172d7145" class="cell" data-execution_count="10">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb13-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Performance scheduling of learning rate</span></span>
<span id="cb13-2">lr_scheduler <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> tf.keras.callbacks.ReduceLROnPlateau(factor<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, patience<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>)</span>
<span id="cb13-3"></span>
<span id="cb13-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Early stopping</span></span>
<span id="cb13-5">early_stopping <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> tf.keras.callbacks.EarlyStopping(patience<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, restore_best_weights<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)</span>
<span id="cb13-6"></span>
<span id="cb13-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Define optimizer</span></span>
<span id="cb13-8">optimizer <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> tf.keras.optimizers.Adam(learning_rate<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e-4</span>)</span>
<span id="cb13-9"></span>
<span id="cb13-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Compile and train the model</span></span>
<span id="cb13-11">cnn_model.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">compile</span>(loss<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'mse'</span>, optimizer<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>optimizer, metrics<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'RootMeanSquaredError'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'R2Score'</span>])</span>
<span id="cb13-12">fit_history <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> cnn_model.fit(train_tfds, epochs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, validation_data<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>valid_tfds,</span>
<span id="cb13-13">                            callbacks<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[lr_scheduler, early_stopping])</span></code></pre></div></div>
</details>
</div>
<p>Training CNN models is quite memory intensive. This specific data and model architecture required around 30-40 GB of GPU memory and 80-90 GB of system memory. With sufficient resources, the training completed in about 20 minutes per run on an NVIDIA A100 GPU.</p>
<p>Plot training metrics:</p>
<div id="68303194" class="cell" data-execution_count="11">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb14-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Plot train and validation loss and RMSE across epochs</span></span>
<span id="cb14-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> matplotlib.pyplot <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> plt</span>
<span id="cb14-3"></span>
<span id="cb14-4">fig, ax <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> plt.subplots(nrows<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, ncols<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>))</span>
<span id="cb14-5"></span>
<span id="cb14-6">pd.DataFrame(fit_history.history)[[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'loss'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'val_loss'</span>]].plot(</span>
<span id="cb14-7">    grid<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>, xlabel<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Epoch"</span>, ax<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>ax[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>],</span>
<span id="cb14-8">    style<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"r--"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"b-"</span>])</span>
<span id="cb14-9"></span>
<span id="cb14-10">pd.DataFrame(fit_history.history)[[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'RootMeanSquaredError'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'val_RootMeanSquaredError'</span>]].plot(</span>
<span id="cb14-11">    grid<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>, xlabel<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Epoch"</span>, ax<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>ax[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>],</span>
<span id="cb14-12">    style<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"r--"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"b-"</span>])</span>
<span id="cb14-13"></span>
<span id="cb14-14">pd.DataFrame(fit_history.history)[[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'R2Score'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'val_R2Score'</span>]].plot(</span>
<span id="cb14-15">    grid<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>, xlabel<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Epoch"</span>, ax<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>ax[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>],</span>
<span id="cb14-16">    style<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"r--"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"b-"</span>])</span>
<span id="cb14-17"></span>
<span id="cb14-18">pd.DataFrame(fit_history.history)[[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'learning_rate'</span>]].plot(</span>
<span id="cb14-19">    grid<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>, xlabel<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Epoch"</span>, ax<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>ax[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>],</span>
<span id="cb14-20">    style<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"g-"</span>])</span>
<span id="cb14-21"></span>
<span id="cb14-22">ax[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>].set_ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Loss'</span>)</span>
<span id="cb14-23">ax[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>].set_title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Loss Over Epochs'</span>)</span>
<span id="cb14-24">ax[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>].legend([<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Training Loss'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Validation Loss'</span>])</span>
<span id="cb14-25"></span>
<span id="cb14-26">ax[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>].set_ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'RMSE'</span>)</span>
<span id="cb14-27">ax[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>].set_title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'RMSE Over Epochs'</span>)</span>
<span id="cb14-28">ax[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>].legend([<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Training RMSE'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Validation RMSE'</span>])</span>
<span id="cb14-29"></span>
<span id="cb14-30">ax[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>].set_ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'R2'</span>)</span>
<span id="cb14-31">ax[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>].set_title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'R2 Over Epochs'</span>)</span>
<span id="cb14-32">ax[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>].legend([<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Training R2'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Validation R2'</span>])</span>
<span id="cb14-33"></span>
<span id="cb14-34">ax[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>].set_ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Learning Rate'</span>)</span>
<span id="cb14-35">ax[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>].set_title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Learning Rate Over Epochs'</span>)</span>
<span id="cb14-36"></span>
<span id="cb14-37">plt.tight_layout()</span>
<span id="cb14-38">plt.show()</span></code></pre></div></div>
</details>
</div>
<p><img src="https://jaychung10010.quarto.pub/all-things-bioinformatics/posts/P6_cnn_dnn_peptide_mhc/images/download (14).png" class="img-fluid"></p>
<p>The CNN + MLP model trained for the full 100 epochs, while the MLP-only model stopped early at epoch 80. Both models showed good convergence without overfitting.</p>
<p>Evaluate the model on the test set:</p>
<div id="7f3efdd6" class="cell" data-execution_count="12">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb15-1">cnn_model.evaluate(X_test_inputs, y_test)</span></code></pre></div></div>
</details>
</div>
<p><strong>Test Results:</strong><br>
- R2Score: 0.6219<br>
- RootMeanSquaredError: 0.1606<br>
- loss: 0.0258<br>
</p>
<p>For comparison, the MLP-only model achieved the following test results:<br>
- R2Score: 0.5452<br>
- RootMeanSquaredError: 0.1775<br>
- loss: 0.0315<br>
</p>
<p>This indicates that, in this specific setting, incorporating CNN layers improved the model’s performance in predicting protein binding affinity.</p>
<p>Let’s plot a comparison of MLP-only vs CNN + MLP prediction accuracy on the test set, stratified by MHC2 alleles. Higher R2Score indicates better prediction performance.</p>
<p><img src="https://jaychung10010.quarto.pub/all-things-bioinformatics/posts/P6_cnn_dnn_peptide_mhc/images/download1.png" class="img-fluid"></p>
<p>From the comparison plot, we can see that the CNN + MLP model generally provides better predictions across various MHC2 alleles compared to the MLP-only model.</p>
<p>Let’s look at one allele example on how well the CNN + MLP model predicts the affinity of <strong>HLA-DPA10201-DPB10101</strong>:</p>
<p><img src="https://jaychung10010.quarto.pub/all-things-bioinformatics/posts/P6_cnn_dnn_peptide_mhc/images/download2.png" class="img-fluid"></p>
<p>As we can see, the <img src="https://latex.codecogs.com/png.latex?R%5E2"> = 0.8, which is an improvement over the MLP-only model of 0.72.</p>
<p>It is interesting to note that the MHC allele sample number does not correlate with the prediction performance, indicating that the model has learned meaningful patterns rather than just memorizing frequent alleles.</p>
<p><img src="https://jaychung10010.quarto.pub/all-things-bioinformatics/posts/P6_cnn_dnn_peptide_mhc/images/download3.png" class="img-fluid"></p>
</section>
<section id="saliency-map-and-in-silico-mutagenesis-analysis" class="level2">
<h2 class="anchored" data-anchor-id="saliency-map-and-in-silico-mutagenesis-analysis">4. Saliency Map and In Silico Mutagenesis Analysis</h2>
<p>To compute the saliency maps, we will calculate the gradients of the model output with respect to the input embeddings. This will help us identify which amino acid positions in the peptide and MHC2 sequences are most influential for the binding affinity predictions.</p>
<p>We will first randomly select 5 high-affinity samples from the test set for saliency analysis:</p>
<div id="f2363482" class="cell" data-execution_count="13">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb16-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Select 5 samples from the test data where Y &gt; 0.9</span></span>
<span id="cb16-2">high_affinity_data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> all_dat[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'test'</span>][all_dat[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'test'</span>][<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Y'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.9</span>].sample(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, random_state<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">42</span>)</span>
<span id="cb16-3"></span>
<span id="cb16-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Get the indices of these samples</span></span>
<span id="cb16-5">sample_indices <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> high_affinity_data.index.values</span>
<span id="cb16-6"></span>
<span id="cb16-7"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Selected </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(high_affinity_data)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> samples with Y &gt; 0.9 for saliency analysis."</span>)</span>
<span id="cb16-8"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(high_affinity_data[[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Peptide'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'MHC_ID'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'MHC'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Y'</span>]])</span></code></pre></div></div>
</details>
</div>
<p><strong>Output:</strong></p>
<pre><code>Selected 5 samples with Y &gt; 0.9 for saliency analysis.
               Peptide                 MHC_ID  \
21199  EKKYYAATQFEPLAA  HLA-DPA10301-DPB10402   
5997   AFLIGANYLGKPKEQ              DRB1_0101   
22257  DKRLAAYLMLMRSPS              DRB1_1501   
8396   SQVNPITLTAALLLL              DRB1_0701   
17575  NDKFTVFEGAFNKAI              DRB5_0101   

                                      MHC         Y  
21199  YMFFMFSGGAISNTLFGQFEYFDIEKVRMHLGMT  0.915458  
5997   QEFFIASGAAVDAIMWLFLECYDLQRATYHVGFT  1.000000  
22257  QEFFIASGAAVDAIMWPRFDYFDIQAATYHVVFT  1.000000  
8396   QEFFIASGAAVDAIMWGYFELYVIDRQTVHVGFT  0.956843  
17575  QEFFIASGAAVDAIMQDYFHDYDFDRATYHVGFT  1.000000  </code></pre>
<p>Now, we will extract the embeddings for these high-affinity samples:</p>
<div id="145a8e4a" class="cell" data-execution_count="14">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb18-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Get embeddings for these high affinity data</span></span>
<span id="cb18-2">high_affinity_peptide <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> high_affinity_data[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Peptide'</span>].tolist()</span>
<span id="cb18-3">high_affinity_mhc <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> high_affinity_data[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'MHC'</span>].tolist()</span>
<span id="cb18-4"></span>
<span id="cb18-5">max_len_pt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">37</span></span>
<span id="cb18-6">max_len_mhc <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">34</span></span>
<span id="cb18-7"></span>
<span id="cb18-8">peptide_embeddings <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> extract_full_embedding(high_affinity_peptide, tokenizer, model, batch_size<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1280</span>, max_len<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>max_len_pt)</span>
<span id="cb18-9">mhc_embeddings <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> extract_full_embedding(high_affinity_mhc, tokenizer, model, batch_size<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1280</span>, max_len<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>max_len_mhc)</span>
<span id="cb18-10"></span>
<span id="cb18-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Concatenate the embeddings</span></span>
<span id="cb18-12">X_high_affinity_embeddings <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.concatenate([peptide_embeddings, mhc_embeddings], axis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb18-13"></span>
<span id="cb18-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Extract corresponding sequence lengths</span></span>
<span id="cb18-15">X_high_affinity_pt_len <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> high_affinity_data[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Peptide'</span>].<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">apply</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>).values</span>
<span id="cb18-16">X_high_affinity_mhc_len <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> high_affinity_data[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'MHC'</span>].<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">apply</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>).values</span></code></pre></div></div>
</details>
</div>
<p>Define a helper function to compute saliency maps:</p>
<div id="c8f73e55" class="cell" data-execution_count="15">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb19-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> tensorflow <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> tf</span>
<span id="cb19-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> numpy <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> np</span>
<span id="cb19-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> matplotlib.pyplot <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> plt</span>
<span id="cb19-4"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> seaborn <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> sns</span>
<span id="cb19-5"></span>
<span id="cb19-6"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> calculate_saliency_map(model: tf.keras.Model,</span>
<span id="cb19-7">                           embedding_data: np.ndarray,</span>
<span id="cb19-8">                           peptide_len_data: np.ndarray,</span>
<span id="cb19-9">                           mhc_len_data: np.ndarray,</span>
<span id="cb19-10">                           target_idx: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> np.ndarray:</span>
<span id="cb19-11"></span>
<span id="cb19-12">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""Calculates saliency map for a given input using the trained model."""</span></span>
<span id="cb19-13"></span>
<span id="cb19-14">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Convert numpy arrays to TensorFlow tensors and ensure float32</span></span>
<span id="cb19-15">    input_embeddings_tensor <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> tf.convert_to_tensor(embedding_data, dtype<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>tf.float32)</span>
<span id="cb19-16">    peptide_len_tensor <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> tf.convert_to_tensor(peptide_len_data, dtype<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>tf.float32)</span>
<span id="cb19-17">    mhc_len_tensor <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> tf.convert_to_tensor(mhc_len_data, dtype<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>tf.float32)</span>
<span id="cb19-18"></span>
<span id="cb19-19">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">with</span> tf.GradientTape() <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> tape:</span>
<span id="cb19-20">        tape.watch(input_embeddings_tensor) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Watch only the embeddings for saliency</span></span>
<span id="cb19-21">        </span>
<span id="cb19-22">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Pass all inputs to the model for prediction of Y</span></span>
<span id="cb19-23">        predictions <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> model((input_embeddings_tensor, peptide_len_tensor, mhc_len_tensor))</span>
<span id="cb19-24">        </span>
<span id="cb19-25">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Select the target output if there are multiple outputs or a specific class</span></span>
<span id="cb19-26">        target_output <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> predictions[:, target_idx]</span>
<span id="cb19-27"></span>
<span id="cb19-28">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calculate gradients of the target output with respect to the input embeddings</span></span>
<span id="cb19-29">    gradients <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> tape.gradient(target_output, input_embeddings_tensor)</span>
<span id="cb19-30"></span>
<span id="cb19-31">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Take the mean across the embedding dimension to get a single saliency score per amino acid position</span></span>
<span id="cb19-32">    saliency <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> tf.reduce_mean(gradients, axis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Take mean along last axis: embedding_dim</span></span>
<span id="cb19-33"></span>
<span id="cb19-34">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> saliency.numpy().flatten()</span></code></pre></div></div>
</details>
</div>
<div id="56b37520" class="cell" data-execution_count="16">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb20-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Package inputs for the model's predict method</span></span>
<span id="cb20-2">X_high_affinity_inputs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (X_high_affinity_embeddings, X_high_affinity_pt_len, X_high_affinity_mhc_len)</span>
<span id="cb20-3"></span>
<span id="cb20-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Predictions for selected samples</span></span>
<span id="cb20-5">y_pred_high_affinity <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> cnn_model.predict(X_high_affinity_inputs)</span></code></pre></div></div>
</details>
</div>
<p>Compute and plot the saliency maps for each selected high-affinity sample:</p>
<div id="b1224140" class="cell" data-execution_count="17">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb21" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb21-1">fig, axes <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> plt.subplots(nrows<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(high_affinity_data), ncols<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">18</span>, <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(high_affinity_data) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>))</span>
<span id="cb21-2">fig.suptitle(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Saliency Maps for High Affinity Peptide-MHC Pairs (Y &gt; 0.9)'</span>, fontsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span>)</span>
<span id="cb21-3"></span>
<span id="cb21-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Ensure axes is always 2D even for a single row</span></span>
<span id="cb21-5"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(high_affinity_data) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>:</span>
<span id="cb21-6">    axes <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.array([axes])</span>
<span id="cb21-7"></span>
<span id="cb21-8"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(high_affinity_data)):</span>
<span id="cb21-9">    sample_info <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> high_affinity_data.iloc[i]</span>
<span id="cb21-10">    peptide_sequence <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> sample_info[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Peptide'</span>]</span>
<span id="cb21-11">    mhc_sequence <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> sample_info[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'MHC'</span>]</span>
<span id="cb21-12">    mhc_id <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> sample_info[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'MHC_ID'</span>]</span>
<span id="cb21-13">    y_true_sample <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> sample_info[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Y'</span>]</span>
<span id="cb21-14">    y_pred_sample <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> y_pred_high_affinity[i][<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>] <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Access pre-calculated prediction</span></span>
<span id="cb21-15"></span>
<span id="cb21-16">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Get one sample's embeddings and lengths at a time</span></span>
<span id="cb21-17">    current_embedding_data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> X_high_affinity_embeddings[i:i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span>
<span id="cb21-18">    current_peptide_len_data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> X_high_affinity_pt_len[i:i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span>
<span id="cb21-19">    current_mhc_len_data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> X_high_affinity_mhc_len[i:i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span>
<span id="cb21-20"></span>
<span id="cb21-21">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calculate saliency map using the updated function signature</span></span>
<span id="cb21-22">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Normalize with a constant to enhance visualization</span></span>
<span id="cb21-23">    beta <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e5</span></span>
<span id="cb21-24">    saliency_scores <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> calculate_saliency_map(cnn_model, </span>
<span id="cb21-25">                                             current_embedding_data, </span>
<span id="cb21-26">                                             current_peptide_len_data, </span>
<span id="cb21-27">                                             current_mhc_len_data) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> beta</span>
<span id="cb21-28"></span>
<span id="cb21-29">    peptide_len <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(peptide_sequence)</span>
<span id="cb21-30">    mhc_len <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(mhc_sequence)</span>
<span id="cb21-31"></span>
<span id="cb21-32">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Correctly slice saliency scores based on actual sequence lengths</span></span>
<span id="cb21-33">    peptide_saliency <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> saliency_scores[:peptide_len]</span>
<span id="cb21-34">    mhc_saliency <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> saliency_scores[max_len_pt : max_len_pt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> mhc_len]</span>
<span id="cb21-35"></span>
<span id="cb21-36">    annot_settings <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rotation"</span>: <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">90</span>}</span>
<span id="cb21-37"></span>
<span id="cb21-38">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Plot for Peptide Saliency</span></span>
<span id="cb21-39">    sns.heatmap(</span>
<span id="cb21-40">        [peptide_saliency],</span>
<span id="cb21-41">        cmap<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'coolwarm'</span>, </span>
<span id="cb21-42">        annot<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>,</span>
<span id="cb21-43">        annot_kws<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>annot_settings,</span>
<span id="cb21-44">        fmt<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">".2f"</span>,</span>
<span id="cb21-45">        xticklabels<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>(peptide_sequence),</span>
<span id="cb21-46">        yticklabels<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Peptide'</span>],</span>
<span id="cb21-47">        cbar_kws<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>{<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'label'</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Saliency Score'</span>},</span>
<span id="cb21-48">        ax<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>axes[i, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]</span>
<span id="cb21-49">    )</span>
<span id="cb21-50">    axes[i, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>].set_title(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f'Sample </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> - Peptide: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>peptide_sequence<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\n</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">True Y: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>y_true_sample<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.2f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">, Pred Y: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>y_pred_sample<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.2f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">'</span>)</span>
<span id="cb21-51">    axes[i, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>].tick_params(axis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'x'</span>, rotation<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb21-52"></span>
<span id="cb21-53">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Plot for MHC Saliency</span></span>
<span id="cb21-54">    sns.heatmap(</span>
<span id="cb21-55">        [mhc_saliency],</span>
<span id="cb21-56">        cmap<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'coolwarm'</span>, </span>
<span id="cb21-57">        annot<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>,</span>
<span id="cb21-58">        annot_kws<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>annot_settings,</span>
<span id="cb21-59">        fmt<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">".2f"</span>,</span>
<span id="cb21-60">        xticklabels<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>(mhc_sequence),</span>
<span id="cb21-61">        yticklabels<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'MHC'</span>],</span>
<span id="cb21-62">        cbar_kws<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>{<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'label'</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Saliency Score'</span>},</span>
<span id="cb21-63">        ax<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>axes[i, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span>
<span id="cb21-64">    )</span>
<span id="cb21-65">    axes[i, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>].set_title(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f'Sample </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> - MHC: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>mhc_sequence<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\n</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">MHC ID: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>mhc_id<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">'</span>)</span>
<span id="cb21-66">    axes[i, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>].tick_params(axis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'x'</span>, rotation<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb21-67"></span>
<span id="cb21-68">plt.tight_layout(rect<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.03</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.95</span>]) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Adjust layout to make space for suptitle</span></span>
<span id="cb21-69">plt.show()</span></code></pre></div></div>
</details>
</div>
<p><img src="https://jaychung10010.quarto.pub/all-things-bioinformatics/posts/P6_cnn_dnn_peptide_mhc/images/s_map.png" class="img-fluid"></p>
<p>Positive saliency scores (red) indicate that the amino acids are positively correlated with a higher binding affinity, while negative scores (blue) suggest that those amino acids contribute to lower binding affinity.</p>
<p>Next, let’s perform In Silico Mutagenesis (ISM) analysis to see how single amino acid mutations affect the predicted binding affinity. Instead of mutating every position, which will be computationally expensive, we will focus on mutating only the most salient positions (highest saliency score) in the peptide sequences for each sample.</p>
<p>First, generate a dictionary of the most disruptive mutations for each amino acid:</p>
<div id="db854816" class="cell" data-execution_count="18">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb22" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb22-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># A dictionary that suggests the most disruptive mutagenesis for each a.a.</span></span>
<span id="cb22-2">disruptive_mutations <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {</span>
<span id="cb22-3">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'A'</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'W'</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Small nonpolar -&gt; Bulkiest nonpolar/aromatic</span></span>
<span id="cb22-4">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'R'</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'D'</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Positively charged -&gt; Negatively charged</span></span>
<span id="cb22-5">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'N'</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'F'</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Polar uncharged -&gt; Hydrophobic aromatic</span></span>
<span id="cb22-6">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'D'</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'R'</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Negatively charged -&gt; Positively charged</span></span>
<span id="cb22-7">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'C'</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'S'</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Disulfide bond former -&gt; Similar size but non-disulfide/polar</span></span>
<span id="cb22-8">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'E'</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'K'</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Negatively charged -&gt; Positively charged</span></span>
<span id="cb22-9">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Q'</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'W'</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Polar uncharged -&gt; Bulkiest nonpolar/aromatic</span></span>
<span id="cb22-10">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'G'</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'P'</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Flexible -&gt; Conformational constraint</span></span>
<span id="cb22-11">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'H'</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'D'</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Positively charged -&gt; Negatively charged (pH depending)</span></span>
<span id="cb22-12">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'I'</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'E'</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Hydrophobic -&gt; Negatively charged hydrophilic</span></span>
<span id="cb22-13">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'L'</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'K'</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Hydrophobic -&gt; Positively charged hydrophilic</span></span>
<span id="cb22-14">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'K'</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'D'</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Positively charged -&gt; Negatively charged</span></span>
<span id="cb22-15">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'M'</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'E'</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Nonpolar/sulfur -&gt; Negatively charged hydrophilic</span></span>
<span id="cb22-16">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'F'</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'E'</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Hydrophobic aromatic -&gt; Negatively charged hydrophilic</span></span>
<span id="cb22-17">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'P'</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'G'</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Conformational constraint -&gt; Flexible</span></span>
<span id="cb22-18">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'S'</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'L'</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Polar uncharged -&gt; Hydrophobic</span></span>
<span id="cb22-19">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'T'</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'I'</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Polar uncharged -&gt; Hydrophobic</span></span>
<span id="cb22-20">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'W'</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'A'</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Bulkiest nonpolar -&gt; Smallest nonpolar</span></span>
<span id="cb22-21">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Y'</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'D'</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Aromatic/polar -&gt; Negatively charged</span></span>
<span id="cb22-22">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'V'</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'E'</span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Hydrophobic -&gt; Negatively charged hydrophilic</span></span>
<span id="cb22-23">}</span></code></pre></div></div>
</details>
</div>
<p>Perform ISM on the top salient positions in the peptide sequences:</p>
<div id="32e1509f" class="cell" data-execution_count="19">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb23" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb23-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Find aa with the highest saliency score across all samples</span></span>
<span id="cb23-2">max_scores_aa_pos_pt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {}</span>
<span id="cb23-3"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(high_affinity_data)):</span>
<span id="cb23-4">  sample_info <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> high_affinity_data.iloc[i]</span>
<span id="cb23-5">  peptide_sequence <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> sample_info[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Peptide'</span>]</span>
<span id="cb23-6">  current_embedding_data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> X_high_affinity_embeddings[i:i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span>
<span id="cb23-7">  current_peptide_len_data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> X_high_affinity_pt_len[i:i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span>
<span id="cb23-8">  current_mhc_len_data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> X_high_affinity_mhc_len[i:i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span>
<span id="cb23-9">  saliency <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> calculate_saliency_map(cnn_model, current_embedding_data, current_peptide_len_data, current_mhc_len_data)</span>
<span id="cb23-10">  max_score_pos <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> saliency[:current_peptide_len_data[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]].argmax().tolist()</span>
<span id="cb23-11">  max_score_aa <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> peptide_sequence[max_score_pos]</span>
<span id="cb23-12">  max_scores_aa_pos_pt[sample_info[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Peptide_ID'</span>].tolist()] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {max_score_aa: max_score_pos}</span>
<span id="cb23-13"></span>
<span id="cb23-14"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Identified highest saliency amino acids and their positions:"</span>)</span>
<span id="cb23-15">display(max_scores_aa_pos_pt)</span></code></pre></div></div>
</details>
</div>
<p><strong>Output:</strong></p>
<pre><code>Identified highest saliency amino acids and their positions:
{60550: {'L': 12},
 117646: {'L': 2},
 51178: {'P': 13},
 73939: {'A': 10},
 26350: {'F': 3}}</code></pre>
<div id="75f88de0" class="cell" data-execution_count="20">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb25" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb25-1">mutated_peptide_predictions <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb25-2"></span>
<span id="cb25-3"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i, (peptide_id, saliency_info) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">enumerate</span>(max_scores_aa_pos_pt.items()):</span>
<span id="cb25-4">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Get original sample info</span></span>
<span id="cb25-5">    sample_row <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> high_affinity_data[high_affinity_data[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Peptide_ID'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> peptide_id].iloc[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]</span>
<span id="cb25-6">    original_peptide <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> sample_row[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Peptide'</span>]</span>
<span id="cb25-7">    original_mhc <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> sample_row[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'MHC'</span>]</span>
<span id="cb25-8">    original_y_pred <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> y_pred_high_affinity[high_affinity_data[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Peptide_ID'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> peptide_id].flatten()[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]</span>
<span id="cb25-9"></span>
<span id="cb25-10">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Get the AA with highest saliency and its position</span></span>
<span id="cb25-11">    saliency_aa <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>(saliency_info.keys())[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]</span>
<span id="cb25-12">    saliency_pos <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>(saliency_info.values())[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]</span>
<span id="cb25-13"></span>
<span id="cb25-14">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Determine the disruptive mutation</span></span>
<span id="cb25-15">    disruptive_aa <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> disruptive_mutations.get(saliency_aa, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'A'</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Default to Alanine if not in dict</span></span>
<span id="cb25-16"></span>
<span id="cb25-17">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create mutated peptide sequence</span></span>
<span id="cb25-18">    mutated_peptide_sequence_list <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>(original_peptide)</span>
<span id="cb25-19">    mutated_peptide_sequence_list[saliency_pos] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> disruptive_aa</span>
<span id="cb25-20">    mutated_peptide <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">''</span>.join(mutated_peptide_sequence_list)</span>
<span id="cb25-21"></span>
<span id="cb25-22">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># --- Extract embeddings for the mutated peptide ---</span></span>
<span id="cb25-23">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Ensure original MHC embeddings are reused to isolate peptide effect</span></span>
<span id="cb25-24">    original_mhc_embeddings <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> extract_full_embedding([original_mhc], tokenizer, model, batch_size<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, max_len<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>max_len_mhc)</span>
<span id="cb25-25"></span>
<span id="cb25-26">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Extract embeddings for the mutated peptide</span></span>
<span id="cb25-27">    mutated_peptide_embeddings <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> extract_full_embedding([mutated_peptide], tokenizer, model, batch_size<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, max_len<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>max_len_pt)</span>
<span id="cb25-28"></span>
<span id="cb25-29">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Concatenate mutated peptide embeddings with original MHC embeddings</span></span>
<span id="cb25-30">    X_mutated_combined_embeddings <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.concatenate([mutated_peptide_embeddings, original_mhc_embeddings], axis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb25-31"></span>
<span id="cb25-32">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Get sequence lengths for mutated peptide and original MHC</span></span>
<span id="cb25-33">    mutated_peptide_len <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.array([<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(mutated_peptide)])</span>
<span id="cb25-34">    original_mhc_len <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.array([<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(original_mhc)])</span>
<span id="cb25-35"></span>
<span id="cb25-36">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Package inputs for prediction</span></span>
<span id="cb25-37">    X_mutated_inputs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (</span>
<span id="cb25-38">        X_mutated_combined_embeddings,</span>
<span id="cb25-39">        tf.convert_to_tensor(mutated_peptide_len, dtype<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>tf.float32),</span>
<span id="cb25-40">        tf.convert_to_tensor(original_mhc_len, dtype<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>tf.float32)</span>
<span id="cb25-41">    )</span>
<span id="cb25-42"></span>
<span id="cb25-43">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Predict binding affinity for the mutated peptide</span></span>
<span id="cb25-44">    mutated_y_pred <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> cnn_model.predict(X_mutated_inputs).flatten()[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]</span>
<span id="cb25-45"></span>
<span id="cb25-46">    mutated_peptide_predictions.append({</span>
<span id="cb25-47">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Peptide_ID'</span>: peptide_id,</span>
<span id="cb25-48">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Original_Peptide'</span>: original_peptide,</span>
<span id="cb25-49">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Mutated_Peptide'</span>: mutated_peptide,</span>
<span id="cb25-50">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Saliency_AA'</span>: saliency_aa,</span>
<span id="cb25-51">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Saliency_Pos'</span>: saliency_pos <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,</span>
<span id="cb25-52">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Disruptive_AA'</span>: disruptive_aa,</span>
<span id="cb25-53">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Original_Pred_Y'</span>: original_y_pred,</span>
<span id="cb25-54">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Mutated_Pred_Y'</span>: mutated_y_pred,</span>
<span id="cb25-55">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Change_in_Y'</span>: mutated_y_pred <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> original_y_pred</span>
<span id="cb25-56">    })</span>
<span id="cb25-57"></span>
<span id="cb25-58">mutated_predictions_df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.DataFrame(mutated_peptide_predictions)</span>
<span id="cb25-59"></span>
<span id="cb25-60"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(mutated_predictions_df)</span></code></pre></div></div>
</details>
</div>
<p><strong>Output:</strong></p>
<pre><code>   Peptide_ID Original_Peptide  Mutated_Peptide Saliency_AA  Saliency_Pos  \
0       60550  EKKYYAATQFEPLAA  EKKYYAATQFEPKAA           L            13   
1      117646  AFLIGANYLGKPKEQ  AFKIGANYLGKPKEQ           L             3   
2       51178  DKRLAAYLMLMRSPS  DKRLAAYLMLMRSGS           P            14   
3       73939  SQVNPITLTAALLLL  SQVNPITLTAWLLLL           A            11   
4       26350  NDKFTVFEGAFNKAI  NDKETVFEGAFNKAI           F             4   

  Disruptive_AA  Original_Pred_Y  Mutated_Pred_Y  Change_in_Y  
0             K         0.879057        0.721061    -0.157996  
1             K         0.782684        0.729118    -0.053567  
2             G         0.652823        0.643233    -0.009590  
3             W         0.665965        0.581599    -0.084366  
4             E         0.678189        0.355576    -0.322613  </code></pre>
<p>Plot the changes in predicted binding affinity due to the mutations:</p>
<div id="c8ef1f07" class="cell" data-execution_count="21">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb27" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb27-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> matplotlib.pyplot <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> plt</span>
<span id="cb27-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> seaborn <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> sns</span>
<span id="cb27-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> adjustText <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> adjust_text</span>
<span id="cb27-4"></span>
<span id="cb27-5">plt.figure(figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>))</span>
<span id="cb27-6">sns.scatterplot(</span>
<span id="cb27-7">    data<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>mutated_predictions_df,</span>
<span id="cb27-8">    x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Original_Pred_Y'</span>,</span>
<span id="cb27-9">    y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Mutated_Pred_Y'</span>,</span>
<span id="cb27-10">    hue<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Change_in_Y'</span>, </span>
<span id="cb27-11">    s<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span>,</span>
<span id="cb27-12">    palette<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'coolwarm'</span>, </span>
<span id="cb27-13">    legend<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'full'</span></span>
<span id="cb27-14">)</span>
<span id="cb27-15"></span>
<span id="cb27-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Add a diagonal line for reference (where Y_original == Y_mutated)</span></span>
<span id="cb27-17">plt.plot([<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>], [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>], <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'k--'</span>, lw<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, alpha<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'No Change'</span>)</span>
<span id="cb27-18"></span>
<span id="cb27-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Annotate points with Peptide_ID or a combination of info</span></span>
<span id="cb27-20">texts <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb27-21"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> idx, row <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> mutated_predictions_df.iterrows():</span>
<span id="cb27-22">    label <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>row[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Original_Peptide'</span>]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\n</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>row[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Saliency_AA'</span>]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}{</span>row[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Saliency_Pos'</span>]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">-&gt;</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>row[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Disruptive_AA'</span>]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span></span>
<span id="cb27-23">    texts.append(plt.text(row[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Original_Pred_Y'</span>], row[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Mutated_Pred_Y'</span>], label, fontsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>))</span>
<span id="cb27-24"></span>
<span id="cb27-25">adjust_text(texts, arrowprops<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>(arrowstyle<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"-"</span>, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'gray'</span>, lw<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>))</span>
<span id="cb27-26"></span>
<span id="cb27-27">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Predicted Binding Affinity: Original vs. Mutated Peptides'</span>)</span>
<span id="cb27-28">plt.xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Original Predicted Y'</span>)</span>
<span id="cb27-29">plt.ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Mutated Predicted Y'</span>)</span>
<span id="cb27-30">plt.grid(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>, linestyle<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'--'</span>, alpha<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.6</span>)</span>
<span id="cb27-31">plt.xlim(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb27-32">plt.ylim(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb27-33">plt.axhline(y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'gray'</span>, linestyle<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">':'</span>, lw<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, alpha<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.7</span>)</span>
<span id="cb27-34">plt.axvline(x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'gray'</span>, linestyle<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">':'</span>, lw<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, alpha<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.7</span>)</span>
<span id="cb27-35">plt.legend(title<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Change in Y'</span>)</span>
<span id="cb27-36">plt.show()</span></code></pre></div></div>
</details>
</div>
<p><img src="https://jaychung10010.quarto.pub/all-things-bioinformatics/posts/P6_cnn_dnn_peptide_mhc/images/ism_scatter.png" class="img-fluid"></p>
<p>As we can see from the ISM analysis, mutating the most salient amino acid positions in the peptide sequences generally leads to a decrease in predicted binding affinity, as indicated by the points falling below the diagonal line. Some mutations result in significant drops in predicted affinity, while others are less impactful. However, we are only mutating a single position here, and multiple mutations may have a more pronounced effect. This analysis provides a hypothesis for experimental validation of key residues involved in peptide-MHC binding.</p>
</section>
<section id="conclusion" class="level2">
<h2 class="anchored" data-anchor-id="conclusion">Conclusion</h2>
<p>Incorporating convolutional neural networks (CNNs) into the protein binding prediction model improved performance compared to a MLP-only approach. By retaining the 2D structure of the ESM2 embeddings and applying 1D convolutional layers, the model was able to capture local sequence patterns that are important for binding affinity prediction. The CNN + MLP model achieved higher R2 scores and lower RMSE on the test set, demonstrating its effectiveness.</p>
<p>It is important to note that further hyperparameter tuning for either model could yield different results. Additionally, exploring other architectures, such as attention mechanisms, could provide additional insights into modeling protein binding affinity.</p>
</section>
<section id="references" class="level2">
<h2 class="anchored" data-anchor-id="references">References</h2>
<ul>
<li><a href="https://github.com/facebookresearch/esm">ESM2</a></li>
<li><a href="https://www.oreilly.com/library/view/hands-on-machine-learning/9781492032632/">Hands-on Machine Learning with Scikit-Learn, Keras, and TensorFlow</a></li>
<li><a href="https://github.com/deep-learning-for-biology">Deep Learning for Biology</a></li>
</ul>


</section>

 ]]></description>
  <category>Python</category>
  <category>Deep Learning</category>
  <category>Convolutional Neural Networks</category>
  <category>Keras</category>
  <category>ESM2</category>
  <category>Protein Language Models</category>
  <guid>https://jaychung10010.quarto.pub/all-things-bioinformatics/posts/P6_cnn_dnn_peptide_mhc/</guid>
  <pubDate>Tue, 03 Feb 2026 08:00:00 GMT</pubDate>
</item>
<item>
  <title>Predicting Peptide-MHC Class II Binding with ESM2 and Neural Networks</title>
  <dc:creator>Jay Chung</dc:creator>
  <link>https://jaychung10010.quarto.pub/all-things-bioinformatics/posts/2026-01-18_peptide_affinity_llm_nn/</link>
  <description><![CDATA[ 





<p>In this post, I will build a neural network to predict the binding affinity between peptides and MHC Class II alleles, utilizing embeddings extracted from the Evolutionary Scale Modeling 2 (ESM2) protein large language model (LLM).</p>
<section id="background" class="level2">
<h2 class="anchored" data-anchor-id="background">Background</h2>
<p><strong>Why is this important?</strong></p>
<ul>
<li><strong>Understanding Immune Response</strong>: MHC Class II molecules present peptide fragments to CD4+ T helper cells, which are crucial for initiating and regulating adaptive immune responses. Understanding which peptides bind strongly to MHC II allows researchers to identify potential T cell epitopes.</li>
<li><strong>Vaccine Design</strong>: Accurate prediction of MHC II-binding peptides can guide the design of subunit vaccines. By selecting peptides likely to bind to a broad range of MHC II alleles (covering genetic diversity in a population), vaccines can be designed to elicit robust T cell responses.</li>
<li><strong>Autoimmune Diseases</strong>: Inappropriate presentation of self-peptides by MHC II molecules can contribute to autoimmune diseases. Predicting these interactions helps in understanding disease mechanisms and developing therapeutic interventions.</li>
<li><strong>Allergy Research</strong>: Similarly, understanding how allergens bind to MHC II can shed light on allergic reactions and aid in treatment development.</li>
<li><strong>Personalized Medicine</strong>: With advancements in sequencing, predicting an individual’s MHC II genotype and the peptides they present could lead to personalized immunotherapies.</li>
</ul>
</section>
<section id="analysis-plan" class="level2">
<h2 class="anchored" data-anchor-id="analysis-plan">Analysis Plan</h2>
<ol type="1">
<li>Download and split the TDC MHC2_IEDB-Jensen dataset.</li>
<li>Perform exploratory data analysis (EDA).</li>
<li>Extract and merge LLM embeddings for peptides and MHC sequences.</li>
<li>Setup TensorFlow datasets, build a Multilayer Perceptron (MLP) model, with defined loss function and optimizer.</li>
<li>Train the model and evaluate metrics.</li>
<li>Assess prediction accuracy on test data.</li>
</ol>
</section>
<section id="summary-figure" class="level2">
<h2 class="anchored" data-anchor-id="summary-figure">Summary Figure</h2>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jaychung10010.quarto.pub/all-things-bioinformatics/posts/2026-01-18_peptide_affinity_llm_nn/images/summary_figure.png" class="img-fluid figure-img"></p>
<figcaption>Schematic overview of the Deep Neural Network pipeline for predicting peptide-MHC binding affinity (Figure generated with the assistance of Gemini).</figcaption>
</figure>
</div>
</section>
<section id="data-selection-and-splitting" class="level2">
<h2 class="anchored" data-anchor-id="data-selection-and-splitting">1. Data Selection and Splitting</h2>
<p>We will use datasets from <strong>Therapeutics Data Commons (TDC)</strong>, a large-scale data repository for machine learning projects.</p>
<div id="4f38659f" class="cell" data-execution_count="1">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Access the MHC2_IEDB_Jensen data from TDC API and obtain splitted data</span></span>
<span id="cb1-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> tdc.multi_pred <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> PeptideMHC</span>
<span id="cb1-3"></span>
<span id="cb1-4">data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> PeptideMHC(name <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"MHC2_IEDB_Jensen"</span>)</span>
<span id="cb1-5">split <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> data.get_split(method <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"random"</span>, seed <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">524</span>, frac <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.7</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>])</span>
<span id="cb1-6">train_df, valid_df, test_df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> split[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'train'</span>], split[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'valid'</span>], split[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'test'</span>]</span></code></pre></div></div>
</details>
</div>
<div id="a35b133b" class="cell" data-execution_count="2">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Save the files to drive</span></span>
<span id="cb2-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> os</span>
<span id="cb2-3">save_path <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'./data'</span></span>
<span id="cb2-4">os.makedirs(save_path, exist_ok<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)</span>
<span id="cb2-5"></span>
<span id="cb2-6">train_df.to_feather(os.path.join(save_path, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'train_dat.feather'</span>))</span>
<span id="cb2-7">valid_df.to_feather(os.path.join(save_path, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'valid_dat.feather'</span>))</span>
<span id="cb2-8">test_df.to_feather(os.path.join(save_path, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'test_dat.feather'</span>))</span></code></pre></div></div>
</details>
</div>
</section>
<section id="exploratory-data-analysis" class="level2">
<h2 class="anchored" data-anchor-id="exploratory-data-analysis">2. Exploratory Data Analysis</h2>
<p>Let’s examine the structure and distribution of our data.</p>
<div id="8fc79174" class="cell" data-execution_count="3">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># how many data points in each data set</span></span>
<span id="cb3-2"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Number of data points in the training set: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(train_df)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb3-3"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Number of data points in the validation set: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(valid_df)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb3-4"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Number of data points in the test set: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(test_df)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div></div>
</details>
</div>
<p><strong>Output:</strong></p>
<pre><code>Number of data points in the training set: 93997
Number of data points in the validation set: 13428
Number of data points in the test set: 26856</code></pre>
<p>A quick look at the training data reveals the structure: <code>Peptide</code> sequence, <code>MHC</code> sequence (pseudo-sequence), and the binding affinity <code>Y</code>.</p>
<div id="7a2fc3c0" class="cell" data-execution_count="4">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb5-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># quick look at the train data structure</span></span>
<span id="cb5-2">train_df.head()</span></code></pre></div></div>
</details>
</div>
<div class="scrolling-table">
<table class="caption-top table">
<thead>
<tr class="header">
<th style="text-align: right;"></th>
<th style="text-align: left;">Peptide</th>
<th style="text-align: left;">MHC</th>
<th style="text-align: right;">Y</th>
<th style="text-align: left;">MHC_ID</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: right;">0</td>
<td style="text-align: left;">PKYVKQNTLKLAT</td>
<td style="text-align: left;">YAFFMF…</td>
<td style="text-align: right;">0.000000</td>
<td style="text-align: left;">HLA-DPA10103-DPB10201</td>
</tr>
<tr class="even">
<td style="text-align: right;">1</td>
<td style="text-align: left;">AAAAGWQTLSAALDA</td>
<td style="text-align: left;">YAFFMF…</td>
<td style="text-align: right;">0.238910</td>
<td style="text-align: left;">HLA-DPA10103-DPB10201</td>
</tr>
<tr class="odd">
<td style="text-align: right;">2</td>
<td style="text-align: left;">AALDAQAVELTARLN</td>
<td style="text-align: left;">YAFFMF…</td>
<td style="text-align: right;">0.357937</td>
<td style="text-align: left;">HLA-DPA10103-DPB10201</td>
</tr>
<tr class="even">
<td style="text-align: right;">3</td>
<td style="text-align: left;">ADLGYGPATPAAPAA</td>
<td style="text-align: left;">YAFFMF…</td>
<td style="text-align: right;">0.285795</td>
<td style="text-align: left;">HLA-DPA10103-DPB10201</td>
</tr>
<tr class="odd">
<td style="text-align: right;">4</td>
<td style="text-align: left;">AGSYAADLGYGPATP</td>
<td style="text-align: left;">YAFFMF…</td>
<td style="text-align: right;">0.108843</td>
<td style="text-align: left;">HLA-DPA10103-DPB10201</td>
</tr>
</tbody>
</table>
</div>
<section id="distribution-of-binding-affinity" class="level3">
<h3 class="anchored" data-anchor-id="distribution-of-binding-affinity">Distribution of Binding Affinity</h3>
<p>We can visualize the distribution of the target variable <code>Y</code> (binding affinity) in the training set.</p>
<div id="8068a933" class="cell" data-execution_count="5">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb6-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># look at the distribution of Y in training data</span></span>
<span id="cb6-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> matplotlib.pyplot <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> plt</span>
<span id="cb6-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> seaborn <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> sns</span>
<span id="cb6-4"></span>
<span id="cb6-5">plt.figure(figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>))</span>
<span id="cb6-6">sns.histplot(train_df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Y'</span>], bins<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>, kde<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)</span>
<span id="cb6-7">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Distribution of Y (Binding Affinity) in Training Data'</span>)</span>
<span id="cb6-8">plt.xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Binding Affinity (Y)'</span>)</span>
<span id="cb6-9">plt.ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Frequency'</span>)</span>
<span id="cb6-10">plt.grid(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>, linestyle<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'--'</span>, alpha<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.7</span>)</span>
<span id="cb6-11">plt.show()</span></code></pre></div></div>
</details>
</div>
<p><img src="https://jaychung10010.quarto.pub/all-things-bioinformatics/posts/2026-01-18_peptide_affinity_llm_nn/images/cell_10_output.png" class="img-fluid"></p>
<p>The Y values represent normalized binding affinity between the peptides and the MHC II, with higher values representing higher affinities. We can see from the distribution that many pairs have no affinity (Y = 0), while others have variable levels of affinity.</p>
</section>
<section id="peptide-and-mhc-occurrences" class="level3">
<h3 class="anchored" data-anchor-id="peptide-and-mhc-occurrences">Peptide and MHC Occurrences</h3>
<div id="f4bb3b9c" class="cell" data-execution_count="6">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb7-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># How many unique MHC_ID and peptides are there?</span></span>
<span id="cb7-2">unique_mhc <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> train_df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'MHC_ID'</span>].nunique()</span>
<span id="cb7-3">unique_peptides <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> train_df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Peptide'</span>].nunique()</span>
<span id="cb7-4"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f'Number of unique MHC_IDs in training set: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>unique_mhc<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">'</span>)</span>
<span id="cb7-5"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f'Number of unique peptides in training set: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>unique_peptides<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">'</span>)</span></code></pre></div></div>
</details>
</div>
<p><strong>Output:</strong></p>
<pre><code>Number of unique MHC_IDs in training set: 79
Number of unique peptides in training set: 14597</code></pre>
</section>
<section id="mhc-type-distribution" class="level3">
<h3 class="anchored" data-anchor-id="mhc-type-distribution">MHC Type Distribution</h3>
<p>We can also look at the distribution of MHC types in our training data.</p>
<div id="428982f6" class="cell" data-execution_count="7">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb9-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># plot barplot of instances per MHC2 molecule</span></span>
<span id="cb9-2">plt.figure(figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">14</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>))</span>
<span id="cb9-3">train_df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"MHC_ID"</span>].value_counts().sort_values(ascending <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>).plot(kind <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'bar'</span>)</span></code></pre></div></div>
</details>
</div>
<p><img src="https://jaychung10010.quarto.pub/all-things-bioinformatics/posts/2026-01-18_peptide_affinity_llm_nn/images/cell_14_output.png" class="img-fluid"></p>
<p>Some MHCs have many peptide binding data points, while others have only a few. In the next session, we will remove low instance MHC data so that the prediction model has sufficient training data.</p>
</section>
</section>
<section id="extract-and-merge-llm-embeddings" class="level2">
<h2 class="anchored" data-anchor-id="extract-and-merge-llm-embeddings">3. Extract and Merge LLM Embeddings</h2>
<p>To represent the protein sequences for our neural network, we will use <strong>ESM-2 (Evolutionary Scale Modeling)</strong>, a state-of-the-art protein language model. We’ll use the 150M parameter version (<code>esm2_t30_150M_UR50D</code>), which has 640 embedding dimensions.</p>
<div id="9c9bc837" class="cell" data-execution_count="8">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb10-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Only keep MHC_ID that has &gt;= 1000 instances in train data, so that the model has sufficient training data</span></span>
<span id="cb10-2">n_data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> train_df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"MHC_ID"</span>].value_counts()</span>
<span id="cb10-3">keep_mhc_id <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> n_data[n_data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>].index.tolist()</span>
<span id="cb10-4">train_df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> train_df[train_df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"MHC_ID"</span>].isin(keep_mhc_id)].reset_index(drop<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)</span>
<span id="cb10-5">valid_df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> valid_df[valid_df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"MHC_ID"</span>].isin(keep_mhc_id)].reset_index(drop<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)</span>
<span id="cb10-6">test_df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> test_df[test_df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"MHC_ID"</span>].isin(keep_mhc_id)].reset_index(drop<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)</span></code></pre></div></div>
</details>
</div>
<div id="9a90f0bb" class="cell" data-execution_count="9">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb11-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># How many data points in each data set</span></span>
<span id="cb11-2"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Number of data points in the training set: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(train_df)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb11-3"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Number of data points in the validation set: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(valid_df)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb11-4"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Number of data points in the test set: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(test_df)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div></div>
</details>
</div>
<p><strong>Output:</strong></p>
<pre><code>Number of data points in the training set: 82388
Number of data points in the validation set: 11702
Number of data points in the test set: 23410</code></pre>
<div id="cd0d8ed1" class="cell" data-execution_count="10">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb13-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> transformers <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> AutoTokenizer, EsmModel</span>
<span id="cb13-2"></span>
<span id="cb13-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># model checkpoints can be seen here: https://github.com/facebookresearch/esm#available-models-and-datasets-</span></span>
<span id="cb13-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># the one we use has medium complexity and has 640 dimensions</span></span>
<span id="cb13-5"></span>
<span id="cb13-6">model_checkpoint <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"facebook/esm2_t30_150M_UR50D"</span></span>
<span id="cb13-7">tokenizer <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> AutoTokenizer.from_pretrained(model_checkpoint)</span>
<span id="cb13-8">model <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> EsmModel.from_pretrained(model_checkpoint)</span></code></pre></div></div>
</details>
</div>
<p>A note to LLM model selection for this specific task: I’ve tried larger ESM2 model with 1280 or 2560 embedding dimensions, and they did not improve the accuracy of binding prediction. As peptides and MHC lengths are quite short, it is possible that the 640 dimension model has already saturated learnable information for this task.</p>
<div id="43c341db" class="cell" data-execution_count="11">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb14-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Write a function to extract mean embeddings for protein sequences</span></span>
<span id="cb14-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># GPU will significantly speed up the process</span></span>
<span id="cb14-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># This is memory intensive, so we will batch the iteration</span></span>
<span id="cb14-4"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> numpy <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> np</span>
<span id="cb14-5"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> transformers <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> AutoTokenizer, EsmModel</span>
<span id="cb14-6"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> torch</span>
<span id="cb14-7"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> math</span>
<span id="cb14-8"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> tqdm <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> tqdm</span>
<span id="cb14-9"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> pandas <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> pd</span>
<span id="cb14-10"></span>
<span id="cb14-11"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> extract_mean_embedding(</span>
<span id="cb14-12">    sequence: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>],</span>
<span id="cb14-13">    tokenizer: AutoTokenizer,</span>
<span id="cb14-14">    model: EsmModel,</span>
<span id="cb14-15">    device: torch.device <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>,</span>
<span id="cb14-16">    batch_size: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">64</span></span>
<span id="cb14-17">    ) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> pd.DataFrame:</span>
<span id="cb14-18"></span>
<span id="cb14-19">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""Extract mean embeddings for peptide sequences from a LLM model."""</span></span>
<span id="cb14-20"></span>
<span id="cb14-21">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Use GPU when available</span></span>
<span id="cb14-22">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">not</span> device:</span>
<span id="cb14-23">        device <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> torch.device(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cuda"</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> torch.cuda.is_available() <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cpu"</span>)</span>
<span id="cb14-24"></span>
<span id="cb14-25">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Separate sequence list into batches</span></span>
<span id="cb14-26">    n_batches <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> math.ceil(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(sequence) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> batch_size)</span>
<span id="cb14-27">    all_batch_embeddings <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb14-28"></span>
<span id="cb14-29">    model <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> model.to(device) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Move model to the target device</span></span>
<span id="cb14-30">    model.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">eval</span>() <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Set model to evaluation mode</span></span>
<span id="cb14-31"></span>
<span id="cb14-32">    steps <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> tqdm(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(n_batches))</span>
<span id="cb14-33"></span>
<span id="cb14-34">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> steps:</span>
<span id="cb14-35">        steps.set_description(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Processing batch </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">/</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>n_batches<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb14-36">        start <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> i <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> batch_size</span>
<span id="cb14-37">        end <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (i <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> batch_size</span>
<span id="cb14-38">        batch <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> sequence[start:end]</span>
<span id="cb14-39"></span>
<span id="cb14-40">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Tokenize peptide sequence and pad to equal length</span></span>
<span id="cb14-41">        inputs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> tokenizer(batch, return_tensors<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pt"</span>, padding<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)</span>
<span id="cb14-42"></span>
<span id="cb14-43">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Move input to the target device</span></span>
<span id="cb14-44">        inputs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {name: tensor.to(device) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> name, tensor <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> inputs.items()}</span>
<span id="cb14-45"></span>
<span id="cb14-46">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Forward pass through the model without gradient tracking to get mean embeddings</span></span>
<span id="cb14-47">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">with</span> torch.no_grad():</span>
<span id="cb14-48">          batch_mean_embeddings <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> model(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>inputs).last_hidden_state.mean(dim<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>).detach().cpu().numpy()</span>
<span id="cb14-49">        all_batch_embeddings.append(batch_mean_embeddings)</span>
<span id="cb14-50"></span>
<span id="cb14-51">    embeddings <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.DataFrame(np.vstack(all_batch_embeddings))</span>
<span id="cb14-52">    embeddings.columns <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"me_</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>i <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(embeddings.shape[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>])]</span>
<span id="cb14-53">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> embeddings</span></code></pre></div></div>
</details>
</div>
<p>The ESM2 model returns a sequence of embeddings for each token in the input sequence. We take the mean of these embeddings to get a single embedding for the entire sequence. It is also possible to keep the sequence of embeddings for each token, and use other methods to aggregate the embeddings, such as max pooling or attention pooling, or train a 1D Convolutional Neural Network (CNN) to learn the optimal way to aggregate the embeddings. In this case, we will keep the mean embeddings for each sequence.</p>
<div id="19bf016b" class="cell" data-execution_count="12">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb15-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Put all peptide/MHC data into a dictionary</span></span>
<span id="cb15-2">sequence_dict <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {</span>
<span id="cb15-3">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"train_pt"</span>: train_df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Peptide'</span>].tolist(),</span>
<span id="cb15-4">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"valid_pt"</span>: valid_df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Peptide'</span>].tolist(),</span>
<span id="cb15-5">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"test_pt"</span>: test_df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Peptide'</span>].tolist(),</span>
<span id="cb15-6">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"train_mhc"</span>: train_df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'MHC'</span>].tolist(),</span>
<span id="cb15-7">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"valid_mhc"</span>: valid_df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'MHC'</span>].tolist(),</span>
<span id="cb15-8">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"test_mhc"</span>: test_df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'MHC'</span>].tolist()</span>
<span id="cb15-9">}</span>
<span id="cb15-10"></span>
<span id="cb15-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Extract embeddings for all peptide/MHC data</span></span>
<span id="cb15-12">embedding_dict <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {}</span>
<span id="cb15-13"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> data, sequence <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> sequence_dict.items():</span>
<span id="cb15-14">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Extracting embeddings for </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">..."</span>)</span>
<span id="cb15-15">    embedding_dict[data] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> extract_mean_embedding(sequence, tokenizer, model, batch_size<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2560</span>)</span></code></pre></div></div>
</details>
</div>
<p><img src="https://jaychung10010.quarto.pub/all-things-bioinformatics/posts/2026-01-18_peptide_affinity_llm_nn/images/embeddings.png" class="img-fluid"></p>
<p>There are other, more complex ways to combine embeddings from two interacting entities, but for this task, we will use the simple approach of concatenating the embeddings.</p>
<div id="80191143" class="cell" data-execution_count="13">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb16-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Modify column names to add peptide/MHC prefix</span></span>
<span id="cb16-2"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> key, me <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> embedding_dict.items():</span>
<span id="cb16-3">  me.columns <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>key<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>split(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'_'</span>)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">_</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>col<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> col <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> me.columns]</span>
<span id="cb16-4"></span>
<span id="cb16-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Concatenate peptide and MHC mean embeddings and name columns</span></span>
<span id="cb16-6">train_me <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.concat([train_df[[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Peptide'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'MHC_ID'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Y'</span>]],</span>
<span id="cb16-7">                      embedding_dict[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'train_pt'</span>],</span>
<span id="cb16-8">                      embedding_dict[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'train_mhc'</span>]], axis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb16-9">valid_me <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.concat([valid_df[[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Peptide'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'MHC_ID'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Y'</span>]],</span>
<span id="cb16-10">                      embedding_dict[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'valid_pt'</span>],</span>
<span id="cb16-11">                      embedding_dict[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'valid_mhc'</span>]], axis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb16-12">test_me <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.concat([test_df[[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Peptide'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'MHC_ID'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Y'</span>]],</span>
<span id="cb16-13">                     embedding_dict[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'test_pt'</span>],</span>
<span id="cb16-14">                     embedding_dict[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'test_mhc'</span>]], axis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb16-15"></span>
<span id="cb16-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Save data as feather files</span></span>
<span id="cb16-17">model_cp <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>(model.name_or_path).replace(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'/'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'_'</span>)</span>
<span id="cb16-18">df_list <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [(train_me, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'train_me'</span>), (valid_me, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'valid_me'</span>), (test_me, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'test_me'</span>)]</span>
<span id="cb16-19">save_path <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'./data'</span></span>
<span id="cb16-20"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> df, df_name <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> df_list:</span>
<span id="cb16-21">  df.to_feather(os.path.join(save_path, <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f'</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>model_cp<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">_</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>df_name<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">.feather'</span>))</span></code></pre></div></div>
</details>
</div>
<p>Let’s visualize the embeddings using t-SNE to see if there are any patterns in the data.</p>
<div id="8921a256" class="cell" data-execution_count="14">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb17-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Plot t-SNE of sampled peptide-MHC mean embeddings</span></span>
<span id="cb17-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> matplotlib.pyplot <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> plt</span>
<span id="cb17-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> seaborn <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> sns</span>
<span id="cb17-4"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> sklearn.manifold <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> TSNE</span>
<span id="cb17-5"></span>
<span id="cb17-6">sampled_index <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> train_me.sample(n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5000</span>, random_state<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>).index <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># sample 5000 data points for visualization</span></span>
<span id="cb17-7">X_embedded <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> TSNE(n_components<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, learning_rate<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'auto'</span>,</span>
<span id="cb17-8">                  init<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'random'</span>, perplexity<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>).fit_transform(train_me.loc[sampled_index])</span>
<span id="cb17-9"></span>
<span id="cb17-10">plt.figure(figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>))</span>
<span id="cb17-11">plt.scatter(X_embedded[:, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>], X_embedded[:, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>], alpha<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, s<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb17-12">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'t-SNE of Peptide-MHC Embeddings'</span>)</span>
<span id="cb17-13">plt.show()</span></code></pre></div></div>
</details>
</div>
<p><img src="https://jaychung10010.quarto.pub/all-things-bioinformatics/posts/2026-01-18_peptide_affinity_llm_nn/images/cell_29_output.png" class="img-fluid"></p>
<p>In the above t-SNE plot, we can see some cluster structures correlating with higher Y, indicating that the embeddings encode information relating to peptide-MHC affinity.</p>
</section>
<section id="neural-network-training" class="level2">
<h2 class="anchored" data-anchor-id="neural-network-training">4. Neural Network Training</h2>
<p>We will build a MLP that takes the concatenated peptide and MHC embeddings as input, and outputs the predicted affinity.</p>
<section id="dataset-preparation" class="level3">
<h3 class="anchored" data-anchor-id="dataset-preparation">Dataset Preparation</h3>
<p>We convert our extracted embeddings and labels into TensorFlow datasets.</p>
<div id="21489bc9" class="cell" data-execution_count="15">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb18-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Convert data to Tensorflow dataset</span></span>
<span id="cb18-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> tensorflow <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> tf</span>
<span id="cb18-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> pandas <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> pd</span>
<span id="cb18-4"></span>
<span id="cb18-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Compile all feature columns (peptide and MHC embeddings) into a single NumPy array for each dataset</span></span>
<span id="cb18-6">X_train <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> train_me.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">filter</span>(regex<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"^pt|^mhc"</span>).values</span>
<span id="cb18-7">y_train <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> train_me[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Y'</span>].values</span>
<span id="cb18-8"></span>
<span id="cb18-9">X_valid <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> valid_me.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">filter</span>(regex<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"^pt|^mhc"</span>).values</span>
<span id="cb18-10">y_valid <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> valid_me[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Y'</span>].values</span>
<span id="cb18-11"></span>
<span id="cb18-12">X_test <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> test_me.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">filter</span>(regex<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"^pt|^mhc"</span>).values</span>
<span id="cb18-13">y_test <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> test_me[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Y'</span>].values</span>
<span id="cb18-14"></span>
<span id="cb18-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create tf.data.Dataset from these concatenated arrays</span></span>
<span id="cb18-16">train_tfds <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> tf.data.Dataset.from_tensor_slices((X_train, y_train))</span>
<span id="cb18-17"></span>
<span id="cb18-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Shuffle, batch and prefetch the tfds</span></span>
<span id="cb18-19">train_tfds <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> train_tfds.shuffle(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1024</span>, seed<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">42</span>).batch(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">32</span>).prefetch(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb18-20"></span>
<span id="cb18-21">valid_tfds <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> tf.data.Dataset.from_tensor_slices((X_valid, y_valid))</span>
<span id="cb18-22">valid_tfds <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> valid_tfds.batch(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">32</span>).prefetch(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span></code></pre></div></div>
</details>
</div>
</section>
<section id="model-architecture" class="level3">
<h3 class="anchored" data-anchor-id="model-architecture">Model Architecture</h3>
<p>The model consists of dense layers with varying dropout rates to prevent overfitting.</p>
<div id="94725e70" class="cell" data-execution_count="16">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb19-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Define MLP layers</span></span>
<span id="cb19-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Define a helper function for the repetitive dense -&gt; batch norm -&gt; dropout block</span></span>
<span id="cb19-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> make_dense_block(n_neurons, dropout_rate<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>):</span>
<span id="cb19-4">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> tf.keras.Sequential([</span>
<span id="cb19-5">        tf.keras.layers.Dense(n_neurons, activation<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'relu'</span>, kernel_initializer<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'he_normal'</span>),</span>
<span id="cb19-6">        tf.keras.layers.BatchNormalization(),</span>
<span id="cb19-7">        tf.keras.layers.Dropout(dropout_rate)</span>
<span id="cb19-8">    ])</span>
<span id="cb19-9"></span>
<span id="cb19-10">tf.random.set_seed(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">42</span>)</span>
<span id="cb19-11">norm_layer <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> tf.keras.layers.Normalization(input_shape<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>X_train.shape[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>:])</span>
<span id="cb19-12"></span>
<span id="cb19-13">dnn_model <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> tf.keras.Sequential([</span>
<span id="cb19-14">    norm_layer,</span>
<span id="cb19-15">    make_dense_block(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">300</span>),</span>
<span id="cb19-16">    make_dense_block(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">300</span>),</span>
<span id="cb19-17">    make_dense_block(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">300</span>),</span>
<span id="cb19-18">    make_dense_block(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">300</span>),</span>
<span id="cb19-19">    make_dense_block(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">300</span>),</span>
<span id="cb19-20">    tf.keras.layers.Dense(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb19-21">])</span>
<span id="cb19-22">dnn_model.summary()</span></code></pre></div></div>
</details>
</div>
<p><img src="https://jaychung10010.quarto.pub/all-things-bioinformatics/posts/2026-01-18_peptide_affinity_llm_nn/images/model.png" class="img-fluid"></p>
<p>Here I am using a MLP of 5 layers, with batch normalization and dropout after each layer to control for overfitting.</p>
</section>
<section id="training" class="level3">
<h3 class="anchored" data-anchor-id="training">Training</h3>
<p>I used performance scheduling to make sure that the model converges to a good solution. I also used early stopping to further prevent overfitting.</p>
<div id="cba67445" class="cell" data-execution_count="17">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb20-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Performance scheduling of learning rate</span></span>
<span id="cb20-2">lr_scheduler <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> tf.keras.callbacks.ReduceLROnPlateau(factor<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, patience<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>)</span>
<span id="cb20-3"></span>
<span id="cb20-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Early stopping</span></span>
<span id="cb20-5">early_stopping <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> tf.keras.callbacks.EarlyStopping(patience<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, restore_best_weights<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)</span></code></pre></div></div>
</details>
</div>
<div id="8bd3d696" class="cell" data-execution_count="18">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb21" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb21-1">optimizer <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> tf.keras.optimizers.Adam(learning_rate<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e-4</span>)</span>
<span id="cb21-2">dnn_model.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">compile</span>(loss<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'mse'</span>, optimizer<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>optimizer, metrics<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'RootMeanSquaredError'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'R2Score'</span>])</span>
<span id="cb21-3"></span>
<span id="cb21-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Adapt the normalization layer using only the feature tensors from the dataset</span></span>
<span id="cb21-5">norm_layer.adapt(train_tfds.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">map</span>(<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lambda</span> x, y: x))</span>
<span id="cb21-6">tf.random.set_seed(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">42</span>)</span>
<span id="cb21-7">fit_history <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> dnn_model.fit(train_tfds, epochs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>, validation_data<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>valid_tfds,</span>
<span id="cb21-8">                            callbacks<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[lr_scheduler, early_stopping])</span></code></pre></div></div>
</details>
</div>
<p><img src="https://jaychung10010.quarto.pub/all-things-bioinformatics/posts/2026-01-18_peptide_affinity_llm_nn/images/train1.png" class="img-fluid"></p>
</section>
<section id="training-history" class="level3">
<h3 class="anchored" data-anchor-id="training-history">Training History</h3>
<p>Let’s analyze the training performance over epochs.</p>
<div id="5ccbcc60" class="cell" data-execution_count="19">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb22" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb22-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Plot train and validation loss and RMSE across epochs</span></span>
<span id="cb22-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> matplotlib.pyplot <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> plt</span>
<span id="cb22-3"></span>
<span id="cb22-4">fig, ax <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> plt.subplots(nrows<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, ncols<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>))</span>
<span id="cb22-5"></span>
<span id="cb22-6">pd.DataFrame(fit_history.history)[[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'loss'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'val_loss'</span>]].plot(</span>
<span id="cb22-7">    grid<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>, xlabel<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Epoch"</span>, ax<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>ax[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>],</span>
<span id="cb22-8">    style<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"r--"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"b-"</span>])</span>
<span id="cb22-9"></span>
<span id="cb22-10">pd.DataFrame(fit_history.history)[[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'RootMeanSquaredError'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'val_RootMeanSquaredError'</span>]].plot(</span>
<span id="cb22-11">    grid<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>, xlabel<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Epoch"</span>, ax<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>ax[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>],</span>
<span id="cb22-12">    style<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"r--"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"b-"</span>])</span>
<span id="cb22-13"></span>
<span id="cb22-14">pd.DataFrame(fit_history.history)[[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'R2Score'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'val_R2Score'</span>]].plot(</span>
<span id="cb22-15">    grid<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>, xlabel<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Epoch"</span>, ax<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>ax[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>],</span>
<span id="cb22-16">    style<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"r--"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"b-"</span>])</span>
<span id="cb22-17"></span>
<span id="cb22-18">pd.DataFrame(fit_history.history)[[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'learning_rate'</span>]].plot(</span>
<span id="cb22-19">    grid<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>, xlabel<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Epoch"</span>, ax<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>ax[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>],</span>
<span id="cb22-20">    style<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"g-"</span>])</span>
<span id="cb22-21"></span>
<span id="cb22-22">ax[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>].set_ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Loss'</span>)</span>
<span id="cb22-23">ax[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>].set_title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Loss Over Epochs'</span>)</span>
<span id="cb22-24">ax[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>].legend([<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Training Loss'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Validation Loss'</span>])</span>
<span id="cb22-25"></span>
<span id="cb22-26">ax[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>].set_ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'RMSE'</span>)</span>
<span id="cb22-27">ax[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>].set_title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'RMSE Over Epochs'</span>)</span>
<span id="cb22-28">ax[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>].legend([<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Training RMSE'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Validation RMSE'</span>])</span>
<span id="cb22-29"></span>
<span id="cb22-30">ax[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>].set_ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'R2'</span>)</span>
<span id="cb22-31">ax[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>].set_title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'R2 Over Epochs'</span>)</span>
<span id="cb22-32">ax[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>].legend([<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Training R2'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Validation R2'</span>])</span>
<span id="cb22-33"></span>
<span id="cb22-34">ax[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>].set_ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Learning Rate'</span>)</span>
<span id="cb22-35">ax[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>].set_title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Learning Rate Over Epochs'</span>)</span>
<span id="cb22-36"></span>
<span id="cb22-37">plt.tight_layout()</span>
<span id="cb22-38">plt.show()</span></code></pre></div></div>
</details>
</div>
<p><img src="https://jaychung10010.quarto.pub/all-things-bioinformatics/posts/2026-01-18_peptide_affinity_llm_nn/images/cell_40_output.png" class="img-fluid"></p>
<p>As we can see, validation loss and errors have been kept very close to that of training, and they eventually dropped down to very similar levels, suggesting that the model did not overfit.</p>
</section>
</section>
<section id="model-evaluation" class="level2">
<h2 class="anchored" data-anchor-id="model-evaluation">5. Model Evaluation</h2>
<p>Finally, we evaluate the model on the training and test set.</p>
<section id="training-set" class="level3">
<h3 class="anchored" data-anchor-id="training-set">Training Set</h3>
<div id="c0f5b12c" class="cell" data-execution_count="20">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb23" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb23-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Evaluate R-squared of predicted vs actual binding affinity for each MHC type</span></span>
<span id="cb23-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> sklearn.metrics <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> r2_score</span>
<span id="cb23-3"></span>
<span id="cb23-4">y_pred <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> dnn_model.predict(X_train)</span>
<span id="cb23-5">y_true <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> y_train</span>
<span id="cb23-6"></span>
<span id="cb23-7">mhc_id <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> train_me[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'MHC_ID'</span>].values</span>
<span id="cb23-8">r2_df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.DataFrame({<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'MHC_ID'</span>: mhc_id, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Y'</span>: y_true, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Y_pred'</span>: y_pred.flatten()})</span>
<span id="cb23-9"></span>
<span id="cb23-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># remove MHC_ID &lt;= 10 instances</span></span>
<span id="cb23-11">r2_df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> r2_df.groupby(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'MHC_ID'</span>).<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">filter</span>(<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lambda</span> x: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(x) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)</span>
<span id="cb23-12"></span>
<span id="cb23-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calculate R2 and MHC instances</span></span>
<span id="cb23-14">mhc_n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> r2_df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'MHC_ID'</span>].value_counts()</span>
<span id="cb23-15">r2_by_mhc <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> r2_df.groupby(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'MHC_ID'</span>).<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">apply</span>(<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lambda</span> x: r2_score(x[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Y'</span>], x[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Y_pred'</span>]))</span>
<span id="cb23-16"></span>
<span id="cb23-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Join statistics by MHC_ID</span></span>
<span id="cb23-18">r2_df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.concat([mhc_n, r2_by_mhc], axis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb23-19">r2_df.columns <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'n'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'r2'</span>]</span>
<span id="cb23-20">r2_df.sort_values(by<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'r2'</span>, ascending<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>, inplace<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)</span></code></pre></div></div>
</details>
</div>
<div id="4f380c84" class="cell" data-execution_count="21">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb24" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb24-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Plot MHC instance vs prediction accuracy scatter plot</span></span>
<span id="cb24-2">plt.figure(figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>))</span>
<span id="cb24-3">plt.scatter(r2_df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'n'</span>], r2_df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'r2'</span>], alpha<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>)</span>
<span id="cb24-4"></span>
<span id="cb24-5"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> adjustText <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> adjust_text</span>
<span id="cb24-6">texts <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [plt.text(r2_df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'n'</span>].iloc[i], r2_df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'r2'</span>].iloc[i], r2_df.index[i]) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(r2_df))]</span>
<span id="cb24-7">adjust_text(</span>
<span id="cb24-8">  texts, expand<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.1</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.1</span>), arrowprops<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>(arrowstyle<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"-&gt;"</span>, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey"</span>)</span>
<span id="cb24-9">)</span>
<span id="cb24-10"></span>
<span id="cb24-11">plt.xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Number of Instances'</span>)</span>
<span id="cb24-12">plt.ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'R-squared'</span>)</span>
<span id="cb24-13">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Number of MHC Instances vs. R-squared'</span>)</span>
<span id="cb24-14">plt.grid(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>, linestyle<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'--'</span>, alpha<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.7</span>)</span>
<span id="cb24-15">plt.show()</span></code></pre></div></div>
</details>
</div>
<p><img src="https://jaychung10010.quarto.pub/all-things-bioinformatics/posts/2026-01-18_peptide_affinity_llm_nn/images/cell_44_output.png" class="img-fluid"></p>
<p>This plot reveals two key findings: (1) <strong>Prediction accuracy does not correlate with the number of MHC instances</strong>, suggesting that the model’s performance is driven more by the intrinsic biochemical or structural properties of the peptide-MHC pairs than by data volume. Indeed, the model did not necessarily perform better on MHCs that were abundant in the training data (e.g., DRB-0101 is very abundant yet has a relatively low <img src="https://latex.codecogs.com/png.latex?R%5E2">). (2) <strong>Some MHC interactions are inherently easier to predict.</strong> For example, HLA-DPA10201-DPB10101 achieves an <img src="https://latex.codecogs.com/png.latex?R%5E2"> &gt; 0.7. This reinforces the idea that intrinsic properties—or potentially higher-quality experimental data for these specific alleles—are the primary drivers of prediction success.</p>
</section>
<section id="test-set" class="level3">
<h3 class="anchored" data-anchor-id="test-set">Test Set</h3>
<div id="5631496c" class="cell" data-execution_count="22">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb25" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb25-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Evaluate performance on test data</span></span>
<span id="cb25-2">dnn_model.evaluate(X_test, y_test)</span></code></pre></div></div>
</details>
</div>
<p><strong>Test Results:</strong><br>
- R2Score: 0.5357<br>
- RootMeanSquaredError: 0.1793<br>
- loss: 0.0322<br>
</p>
<p>The test RMSE and loss are very close to the training data and even a little better than the validation data, suggesting that the model did not overfit too much.</p>
<div id="ce1b5e7d" class="cell" data-execution_count="23">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb26" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb26-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Plot scatter plot of predicted vs actual on test data</span></span>
<span id="cb26-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> matplotlib.pyplot <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> plt</span>
<span id="cb26-3"></span>
<span id="cb26-4">y_pred <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> dnn_model.predict(X_test)</span>
<span id="cb26-5">y_true <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> y_test</span>
<span id="cb26-6"></span>
<span id="cb26-7">plt.figure(figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>))</span>
<span id="cb26-8">plt.scatter(y_true, y_pred, alpha<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>)</span>
<span id="cb26-9">plt.plot([<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">min</span>(y_true), <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">max</span>(y_true)], [<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">min</span>(y_pred), <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">max</span>(y_pred)], <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'k--'</span>, lw<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb26-10">plt.xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'True Values'</span>)</span>
<span id="cb26-11">plt.ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Predictions'</span>)</span>
<span id="cb26-12">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'True Values vs. Predictions'</span>)</span>
<span id="cb26-13"></span>
<span id="cb26-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Add R-squared on the plot</span></span>
<span id="cb26-15"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> sklearn.metrics <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> r2_score</span>
<span id="cb26-16">r2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> r2_score(y_true, y_pred)</span>
<span id="cb26-17">plt.text(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.95</span>, <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f'R-squared: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>r2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.2f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">'</span>, transform<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>plt.gca().transAxes, fontsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>, verticalalignment<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'top'</span>)</span>
<span id="cb26-18"></span>
<span id="cb26-19">plt.show()</span></code></pre></div></div>
</details>
</div>
<p><img src="https://jaychung10010.quarto.pub/all-things-bioinformatics/posts/2026-01-18_peptide_affinity_llm_nn/images/cell_49_output.png" class="img-fluid"></p>
<p>Overall prediction accuracy is good (<img src="https://latex.codecogs.com/png.latex?R%5E2%20%5Capprox%200.53">). However, aggregate metrics can hide specific strengths. Let’s break down performance by MHC type.</p>
</section>
<section id="performance-by-mhc-type-in-test-set" class="level3">
<h3 class="anchored" data-anchor-id="performance-by-mhc-type-in-test-set">Performance by MHC Type in Test Set</h3>
<p>We calculate the <img src="https://latex.codecogs.com/png.latex?R%5E2"> score specifically for each MHC allele in the test set (filtering for those with &gt;10 instances).</p>
<div id="d6dfdab6" class="cell" data-execution_count="24">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb27" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb27-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Evaluate R-squared of predicted vs actual binding affinity for each MHC type (test data)</span></span>
<span id="cb27-2">y_pred <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> dnn_model.predict(X_test)</span>
<span id="cb27-3">y_true <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> y_test</span>
<span id="cb27-4"></span>
<span id="cb27-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ...code abbreviated for brevity...</span></span></code></pre></div></div>
</details>
</div>
<p><img src="https://jaychung10010.quarto.pub/all-things-bioinformatics/posts/2026-01-18_peptide_affinity_llm_nn/images/cell_51_output.png" class="img-fluid"></p>
<p>Similar to the training set, we can see that prediction accuracy does not correlate with the number of MHC instances, and some MHCs are just easier to predict than others.</p>
<p>For example, looking at <strong>HLA-DPA10201-DPB10101</strong>:</p>
<div id="55cb9c1e" class="cell" data-execution_count="25">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb28" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb28-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Plot HLA-DPA10201-DPB10101 and peptide binding predicted vs actual scatter plot</span></span>
<span id="cb28-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> matplotlib.pyplot <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> plt</span>
<span id="cb28-3"></span>
<span id="cb28-4">y_pred <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> dnn_model.predict(X_test)</span>
<span id="cb28-5">y_true <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> y_test</span>
<span id="cb28-6"></span>
<span id="cb28-7">mhc_id <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> test_me[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'MHC_ID'</span>].values</span>
<span id="cb28-8">target_mhc <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'HLA-DPA10201-DPB10101'</span></span>
<span id="cb28-9">target_index <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.where(mhc_id <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> target_mhc)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]</span>
<span id="cb28-10"></span>
<span id="cb28-11">y_pred <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> y_pred[target_index]</span>
<span id="cb28-12">y_true <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> y_true[target_index]</span>
<span id="cb28-13"></span>
<span id="cb28-14">plt.figure(figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>))</span>
<span id="cb28-15">plt.scatter(y_true, y_pred, alpha<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>)</span>
<span id="cb28-16">plt.plot([<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">min</span>(y_true), <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">max</span>(y_true)], [<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">min</span>(y_pred), <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">max</span>(y_pred)], <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'k--'</span>, lw<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb28-17">plt.xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'True Values'</span>)</span>
<span id="cb28-18">plt.ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Predictions'</span>)</span>
<span id="cb28-19">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'True Values vs. Predictions (HLA-DPA10201-DPB10101)'</span>)</span>
<span id="cb28-20"></span>
<span id="cb28-21"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Add R-squared on the plot</span></span>
<span id="cb28-22"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> sklearn.metrics <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> r2_score</span>
<span id="cb28-23">r2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> r2_score(y_true, y_pred)</span>
<span id="cb28-24">plt.text(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.95</span>, <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f'R-squared: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>r2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.2f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">'</span>, transform<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>plt.gca().transAxes, fontsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>, verticalalignment<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'top'</span>)</span>
<span id="cb28-25"></span>
<span id="cb28-26">plt.show()</span></code></pre></div></div>
</details>
</div>
<p><img src="https://jaychung10010.quarto.pub/all-things-bioinformatics/posts/2026-01-18_peptide_affinity_llm_nn/images/cell_52_output.png" class="img-fluid"></p>
<p>For this specific MHC type, the <img src="https://latex.codecogs.com/png.latex?R%5E2"> is <strong>0.71</strong>, which is significantly better than the global average. This demonstrates that for certain alleles, the model can reliably identify high-affinity binders (e.g., a predicted affinity cutoff of &gt; 0.6 corresponds well to high actual affinity).</p>
</section>
</section>
<section id="conclusion" class="level2">
<h2 class="anchored" data-anchor-id="conclusion">Conclusion</h2>
<p>By leveraging ESM2 embeddings and MLP, we built a pipeline to predict peptide-MHC II binding. This exercise showcases the potential of combining LLMs with machine learning to tackle complex biological challenges. These could range from predicting protein-protein interactions and drug-target binding to forecasting drug responses from gene expression data.</p>
</section>
<section id="references" class="level2">
<h2 class="anchored" data-anchor-id="references">References</h2>
<ul>
<li><a href="https://github.com/facebookresearch/esm">ESM2</a></li>
<li><a href="https://www.tensorflow.org/">TensorFlow</a></li>
<li><a href="https://keras.io/">Keras</a></li>
<li><a href="https://www.oreilly.com/library/view/hands-on-machine-learning/9781492032632/">Hands-on Machine Learning with Scikit-Learn, Keras, and TensorFlow</a></li>
<li><a href="https://github.com/deep-learning-for-biology">Deep Learning for Biology</a></li>
</ul>


</section>

 ]]></description>
  <category>Python</category>
  <category>Deep Learning</category>
  <category>Keras</category>
  <category>ESM2</category>
  <category>Protein Language Models</category>
  <guid>https://jaychung10010.quarto.pub/all-things-bioinformatics/posts/2026-01-18_peptide_affinity_llm_nn/</guid>
  <pubDate>Sun, 18 Jan 2026 08:00:00 GMT</pubDate>
</item>
<item>
  <title>Machine learning for drug sensitivity prediction (Part 3): Training an Elastic Net gene expression model to predict Erlotinib sensitivity</title>
  <dc:creator>Jay Chung</dc:creator>
  <link>https://jaychung10010.quarto.pub/all-things-bioinformatics/posts/2025-12-16_enet_predict_erlotinib/</link>
  <description><![CDATA[ 





<p>In my previous posts (<a href="../../posts/2025-11-29_depmap-multiomics-autoencoder/index.html">part 1</a> and <a href="../../posts/2025-12-03_ae_ml_validation/index.html">part 2</a>), I explored whether using autoencoders (AE) to compress high-dimensional multi-omics data could improve the performance of drug sensitivity prediction models. In my specific context, I showed that using AE did not necessarily improve model performance compared to using a common feature selection method.</p>
<p>In this post, I will use the best-performing model from my previous analyses (elastic net using features selected by Pearson correlation), and apply it to all available DepMap data to generate predictions for cell lines without measured drug sensitivity. I will then see if these predictions provide additional useful insights beyond the measured data.</p>
<p>I will demonstrate how to use R’s Caret package to train an elastic net model with hyperparameter tuning via cross-validation.</p>
<section id="aim-1-apply-the-elastic-net-model-to-all-available-depmap-data-to-generate-erlotinib-sensitivity-predictions" class="level2">
<h2 class="anchored" data-anchor-id="aim-1-apply-the-elastic-net-model-to-all-available-depmap-data-to-generate-erlotinib-sensitivity-predictions">Aim 1: Apply the elastic net model to all available DepMap data to generate Erlotinib sensitivity predictions</h2>
<p>Data location (pre-processed for this post): <a href="https://zenodo.org/records/17970100" class="uri">https://zenodo.org/records/17970100</a></p>
<section id="load-data-and-libraries" class="level3">
<h3 class="anchored" data-anchor-id="load-data-and-libraries">Load data and libraries</h3>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(data.table)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggpubr)</span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(caret)</span>
<span id="cb1-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(doParallel)</span>
<span id="cb1-6"></span>
<span id="cb1-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Load data from the files directory</span></span>
<span id="cb1-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">load</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"files/CCLE_24Q2_GE_match_sample_info.RData"</span>)</span>
<span id="cb1-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">load</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"files/PRISM_24Q2_compound_screen_match_sample_info.RData"</span>)</span>
<span id="cb1-10">cmpd_dat <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read_csv</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"files/Repurposing_Public_24Q2_Extended_Primary_Compound_List.csv"</span>)</span>
<span id="cb1-11"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">load</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"files/sample_info_match_biomarkers.RData"</span>)</span></code></pre></div></div>
</div>
</section>
<section id="preprocessing" class="level3">
<h3 class="anchored" data-anchor-id="preprocessing">Preprocessing</h3>
<p>First, we remove low variance genes and any cell lines containing NAs.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Remove low variance genes</span></span>
<span id="cb2-2">ge_var <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">apply</span>(ccle_ge_match_sam, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, var, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># calculate variance for each gene</span></span>
<span id="cb2-3">ge_dat_hv <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ccle_ge_match_sam[, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">which</span>(ge_var <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">quantile</span>(ge_var, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>))] <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># select high variance genes</span></span>
<span id="cb2-4"></span>
<span id="cb2-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Remove any cell lines (rows) that contains NAs</span></span>
<span id="cb2-6">ge_dat_hv <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ge_dat_hv <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb2-7">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">na.omit</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb2-8">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.data.frame</span>()</span>
<span id="cb2-9"></span>
<span id="cb2-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Match data</span></span>
<span id="cb2-11">match_idx <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">match</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rownames</span>(ge_dat_hv), sample_info<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>StrippedCellLineName)</span>
<span id="cb2-12">sample_info_filt <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> sample_info[match_idx, ]</span>
<span id="cb2-13">prism_dat_match_sam_filt <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> prism_dat_match_sam[match_idx, ]</span>
<span id="cb2-14"></span>
<span id="cb2-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Verify alignment</span></span>
<span id="cb2-16"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">all</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rownames</span>(ge_dat_hv) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> sample_info_filt<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>StrippedCellLineName)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] TRUE</code></pre>
</div>
</div>
<p>Now we prepare the drug response data for Erlotinib.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1">cmpd_id <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> cmpd_dat <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb4-2">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(Drug.Name <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ERLOTINIB"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb4-3">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pull</span>(IDs)</span>
<span id="cb4-4">drug_LFC <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> prism_dat_match_sam_filt <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pull</span>(cmpd_id) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Erlotinib LFC</span></span>
<span id="cb4-5"></span>
<span id="cb4-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Remove NAs in drug response</span></span>
<span id="cb4-7">valid_idx <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">which</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</span>(drug_LFC))</span>
<span id="cb4-8">ge_dat_final <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ge_dat_hv[valid_idx, ]</span>
<span id="cb4-9">drug_LFC_final <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> drug_LFC[valid_idx]</span>
<span id="cb4-10">sample_info_final <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> sample_info_filt[valid_idx, ]</span>
<span id="cb4-11"></span>
<span id="cb4-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Verify alignment</span></span>
<span id="cb4-13"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">all</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rownames</span>(ge_dat_final) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> sample_info_final<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>StrippedCellLineName)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] TRUE</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Check drug response distribution</span></span>
<span id="cb6-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Erlotinib_LFC =</span> drug_LFC_final), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> Erlotinib_LFC)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-3">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_density</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"purple"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.7</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-4">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_classic</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base_size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-5">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Density of Erlotinib LFC"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Erlotinib LFC"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Density"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jaychung10010.quarto.pub/all-things-bioinformatics/posts/2025-12-16_enet_predict_erlotinib/index_files/figure-html/prepare-drug-data-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="feature-selection" class="level3">
<h3 class="anchored" data-anchor-id="feature-selection">Feature Selection</h3>
<p>We select the top predictors correlating with the drug response.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Select top predictors correlating with drug response, Pearson correlation &gt; 0.1</span></span>
<span id="cb7-2">cor_values <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">apply</span>(ge_dat_final, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cor</span>(x, drug_LFC_final, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pearson"</span>))</span>
<span id="cb7-3">cor_values <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(cor_values)</span>
<span id="cb7-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Correlation =</span> cor_values), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> Correlation)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-5">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_density</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"darkgreen"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.7</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-6">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_classic</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base_size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-7">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Density of Gene-Drug Correlation Values"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Absolute Pearson Correlation"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Density"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jaychung10010.quarto.pub/all-things-bioinformatics/posts/2025-12-16_enet_predict_erlotinib/index_files/figure-html/feature-selection-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1">top_predictors <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(cor_values)[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">which</span>(cor_values <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>)]</span>
<span id="cb8-2">ge_dat_final <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ge_dat_final <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">all_of</span>(top_predictors))</span></code></pre></div></div>
</div>
</section>
<section id="elastic-net-hyperparameter-tuning" class="level3">
<h3 class="anchored" data-anchor-id="elastic-net-hyperparameter-tuning">Elastic Net Hyperparameter Tuning</h3>
<p>We perform hyperparameter tuning using <code>caret</code>.</p>
<p><em>Note: The training step is computationally intensive and is not evaluated here. We load the pre-trained model for subsequent analysis.</em></p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Elastic net hyperparameter tuning with caret</span></span>
<span id="cb9-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">detectCores</span>()</span>
<span id="cb9-3">cl <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">makePSOCKcluster</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">22</span>)</span>
<span id="cb9-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">registerDoParallel</span>(cl)</span>
<span id="cb9-5"></span>
<span id="cb9-6">glmnetGrid <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expand.grid</span>(</span>
<span id="cb9-7">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.lambda =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.005</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">length =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>),</span>
<span id="cb9-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.alpha =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">length =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span>
<span id="cb9-9">)</span>
<span id="cb9-10"></span>
<span id="cb9-11"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">524</span>)</span>
<span id="cb9-12">enet_full_model <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">train</span>(ge_dat_final, drug_LFC_final,</span>
<span id="cb9-13">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"glmnet"</span>,</span>
<span id="cb9-14">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">tuneGrid =</span> glmnetGrid,</span>
<span id="cb9-15">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">preProcess =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"center"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"scale"</span>),</span>
<span id="cb9-16">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">metric =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Rsquared"</span>,</span>
<span id="cb9-17">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">trControl =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">trainControl</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"repeatedcv"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">number =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">repeats =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)</span>
<span id="cb9-18">)</span>
<span id="cb9-19"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stopCluster</span>(cl)</span>
<span id="cb9-20"></span>
<span id="cb9-21"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Save the final model (Already saved in files/)</span></span>
<span id="cb9-22"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># save(enet_full_model, file = "files/enet_full_model_erlotinib.RData")</span></span></code></pre></div></div>
</div>
</section>
<section id="model-analysis" class="level3">
<h3 class="anchored" data-anchor-id="model-analysis">Model Analysis</h3>
<p>Let’s examine the tuning results and the best hyperparameters.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Load the pre-trained model</span></span>
<span id="cb10-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">load</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"files/enet_full_model_erlotinib.RData"</span>)</span>
<span id="cb10-3"></span>
<span id="cb10-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># View hyperparameter tuning results</span></span>
<span id="cb10-5">enet_full_model<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>results <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">desc</span>(Rsquared)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> gt<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gt</span>()</span></code></pre></div></div>
<div class="cell-output-display">
<div id="ulycqvjfqd" style="padding-left:0px;padding-right:0px;padding-top:10px;padding-bottom:10px;overflow-x:auto;overflow-y:auto;width:auto;height:auto;">
<style>#ulycqvjfqd table {
  font-family: system-ui, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

#ulycqvjfqd thead, #ulycqvjfqd tbody, #ulycqvjfqd tfoot, #ulycqvjfqd tr, #ulycqvjfqd td, #ulycqvjfqd th {
  border-style: none;
}

#ulycqvjfqd p {
  margin: 0;
  padding: 0;
}

#ulycqvjfqd .gt_table {
  display: table;
  border-collapse: collapse;
  line-height: normal;
  margin-left: auto;
  margin-right: auto;
  color: #333333;
  font-size: 16px;
  font-weight: normal;
  font-style: normal;
  background-color: #FFFFFF;
  width: auto;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #A8A8A8;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #A8A8A8;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
}

#ulycqvjfqd .gt_caption {
  padding-top: 4px;
  padding-bottom: 4px;
}

#ulycqvjfqd .gt_title {
  color: #333333;
  font-size: 125%;
  font-weight: initial;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-color: #FFFFFF;
  border-bottom-width: 0;
}

#ulycqvjfqd .gt_subtitle {
  color: #333333;
  font-size: 85%;
  font-weight: initial;
  padding-top: 3px;
  padding-bottom: 5px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-color: #FFFFFF;
  border-top-width: 0;
}

#ulycqvjfqd .gt_heading {
  background-color: #FFFFFF;
  text-align: center;
  border-bottom-color: #FFFFFF;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#ulycqvjfqd .gt_bottom_border {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#ulycqvjfqd .gt_col_headings {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#ulycqvjfqd .gt_col_heading {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 6px;
  padding-left: 5px;
  padding-right: 5px;
  overflow-x: hidden;
}

#ulycqvjfqd .gt_column_spanner_outer {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  padding-top: 0;
  padding-bottom: 0;
  padding-left: 4px;
  padding-right: 4px;
}

#ulycqvjfqd .gt_column_spanner_outer:first-child {
  padding-left: 0;
}

#ulycqvjfqd .gt_column_spanner_outer:last-child {
  padding-right: 0;
}

#ulycqvjfqd .gt_column_spanner {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 5px;
  overflow-x: hidden;
  display: inline-block;
  width: 100%;
}

#ulycqvjfqd .gt_spanner_row {
  border-bottom-style: hidden;
}

#ulycqvjfqd .gt_group_heading {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  text-align: left;
}

#ulycqvjfqd .gt_empty_group_heading {
  padding: 0.5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: middle;
}

#ulycqvjfqd .gt_from_md > :first-child {
  margin-top: 0;
}

#ulycqvjfqd .gt_from_md > :last-child {
  margin-bottom: 0;
}

#ulycqvjfqd .gt_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  margin: 10px;
  border-top-style: solid;
  border-top-width: 1px;
  border-top-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  overflow-x: hidden;
}

#ulycqvjfqd .gt_stub {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
}

#ulycqvjfqd .gt_stub_row_group {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
  vertical-align: top;
}

#ulycqvjfqd .gt_row_group_first td {
  border-top-width: 2px;
}

#ulycqvjfqd .gt_row_group_first th {
  border-top-width: 2px;
}

#ulycqvjfqd .gt_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#ulycqvjfqd .gt_first_summary_row {
  border-top-style: solid;
  border-top-color: #D3D3D3;
}

#ulycqvjfqd .gt_first_summary_row.thick {
  border-top-width: 2px;
}

#ulycqvjfqd .gt_last_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#ulycqvjfqd .gt_grand_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#ulycqvjfqd .gt_first_grand_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-style: double;
  border-top-width: 6px;
  border-top-color: #D3D3D3;
}

#ulycqvjfqd .gt_last_grand_summary_row_top {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: double;
  border-bottom-width: 6px;
  border-bottom-color: #D3D3D3;
}

#ulycqvjfqd .gt_striped {
  background-color: rgba(128, 128, 128, 0.05);
}

#ulycqvjfqd .gt_table_body {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#ulycqvjfqd .gt_footnotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#ulycqvjfqd .gt_footnote {
  margin: 0px;
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#ulycqvjfqd .gt_sourcenotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#ulycqvjfqd .gt_sourcenote {
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#ulycqvjfqd .gt_left {
  text-align: left;
}

#ulycqvjfqd .gt_center {
  text-align: center;
}

#ulycqvjfqd .gt_right {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

#ulycqvjfqd .gt_font_normal {
  font-weight: normal;
}

#ulycqvjfqd .gt_font_bold {
  font-weight: bold;
}

#ulycqvjfqd .gt_font_italic {
  font-style: italic;
}

#ulycqvjfqd .gt_super {
  font-size: 65%;
}

#ulycqvjfqd .gt_footnote_marks {
  font-size: 75%;
  vertical-align: 0.4em;
  position: initial;
}

#ulycqvjfqd .gt_asterisk {
  font-size: 100%;
  vertical-align: 0;
}

#ulycqvjfqd .gt_indent_1 {
  text-indent: 5px;
}

#ulycqvjfqd .gt_indent_2 {
  text-indent: 10px;
}

#ulycqvjfqd .gt_indent_3 {
  text-indent: 15px;
}

#ulycqvjfqd .gt_indent_4 {
  text-indent: 20px;
}

#ulycqvjfqd .gt_indent_5 {
  text-indent: 25px;
}

#ulycqvjfqd .katex-display {
  display: inline-flex !important;
  margin-bottom: 0.75em !important;
}

#ulycqvjfqd div.Reactable > div.rt-table > div.rt-thead > div.rt-tr.rt-tr-group-header > div.rt-th-group:after {
  height: 0px !important;
}
</style>

<table class="gt_table caption-top table table-sm table-striped small" data-quarto-bootstrap="false">
<thead>
<tr class="gt_col_headings header">
<th id="alpha" class="gt_col_heading gt_columns_bottom_border gt_right" data-quarto-table-cell-role="th" scope="col">alpha</th>
<th id="lambda" class="gt_col_heading gt_columns_bottom_border gt_right" data-quarto-table-cell-role="th" scope="col">lambda</th>
<th id="RMSE" class="gt_col_heading gt_columns_bottom_border gt_right" data-quarto-table-cell-role="th" scope="col">RMSE</th>
<th id="Rsquared" class="gt_col_heading gt_columns_bottom_border gt_right" data-quarto-table-cell-role="th" scope="col">Rsquared</th>
<th id="MAE" class="gt_col_heading gt_columns_bottom_border gt_right" data-quarto-table-cell-role="th" scope="col">MAE</th>
<th id="RMSESD" class="gt_col_heading gt_columns_bottom_border gt_right" data-quarto-table-cell-role="th" scope="col">RMSESD</th>
<th id="RsquaredSD" class="gt_col_heading gt_columns_bottom_border gt_right" data-quarto-table-cell-role="th" scope="col">RsquaredSD</th>
<th id="MAESD" class="gt_col_heading gt_columns_bottom_border gt_right" data-quarto-table-cell-role="th" scope="col">MAESD</th>
</tr>
</thead>
<tbody class="gt_table_body">
<tr class="odd">
<td class="gt_row gt_right" headers="alpha">0.011</td>
<td class="gt_row gt_right" headers="lambda">1.038</td>
<td class="gt_row gt_right" headers="RMSE">0.7246115</td>
<td class="gt_row gt_right" headers="Rsquared">0.3234841</td>
<td class="gt_row gt_right" headers="MAE">0.5678419</td>
<td class="gt_row gt_right" headers="RMSESD">0.06244775</td>
<td class="gt_row gt_right" headers="RsquaredSD">0.09552666</td>
<td class="gt_row gt_right" headers="MAESD">0.05504541</td>
</tr>
<tr class="even">
<td class="gt_row gt_right" headers="alpha">0.011</td>
<td class="gt_row gt_right" headers="lambda">1.211</td>
<td class="gt_row gt_right" headers="RMSE">0.7243195</td>
<td class="gt_row gt_right" headers="Rsquared">0.3231371</td>
<td class="gt_row gt_right" headers="MAE">0.5668798</td>
<td class="gt_row gt_right" headers="RMSESD">0.06237722</td>
<td class="gt_row gt_right" headers="RsquaredSD">0.09620741</td>
<td class="gt_row gt_right" headers="MAESD">0.05451755</td>
</tr>
<tr class="odd">
<td class="gt_row gt_right" headers="alpha">0.011</td>
<td class="gt_row gt_right" headers="lambda">0.866</td>
<td class="gt_row gt_right" headers="RMSE">0.7259216</td>
<td class="gt_row gt_right" headers="Rsquared">0.3227335</td>
<td class="gt_row gt_right" headers="MAE">0.5697183</td>
<td class="gt_row gt_right" headers="RMSESD">0.06251476</td>
<td class="gt_row gt_right" headers="RsquaredSD">0.09492452</td>
<td class="gt_row gt_right" headers="MAESD">0.05557950</td>
</tr>
<tr class="even">
<td class="gt_row gt_right" headers="alpha">0.011</td>
<td class="gt_row gt_right" headers="lambda">1.383</td>
<td class="gt_row gt_right" headers="RMSE">0.7246391</td>
<td class="gt_row gt_right" headers="Rsquared">0.3221409</td>
<td class="gt_row gt_right" headers="MAE">0.5665405</td>
<td class="gt_row gt_right" headers="RMSESD">0.06232075</td>
<td class="gt_row gt_right" headers="RsquaredSD">0.09689575</td>
<td class="gt_row gt_right" headers="MAESD">0.05382773</td>
</tr>
<tr class="odd">
<td class="gt_row gt_right" headers="alpha">0.011</td>
<td class="gt_row gt_right" headers="lambda">1.555</td>
<td class="gt_row gt_right" headers="RMSE">0.7254462</td>
<td class="gt_row gt_right" headers="Rsquared">0.3205662</td>
<td class="gt_row gt_right" headers="MAE">0.5668465</td>
<td class="gt_row gt_right" headers="RMSESD">0.06223333</td>
<td class="gt_row gt_right" headers="RsquaredSD">0.09753524</td>
<td class="gt_row gt_right" headers="MAESD">0.05317660</td>
</tr>
<tr class="even">
<td class="gt_row gt_right" headers="alpha">0.011</td>
<td class="gt_row gt_right" headers="lambda">0.694</td>
<td class="gt_row gt_right" headers="RMSE">0.7287441</td>
<td class="gt_row gt_right" headers="Rsquared">0.3204541</td>
<td class="gt_row gt_right" headers="MAE">0.5731421</td>
<td class="gt_row gt_right" headers="RMSESD">0.06255456</td>
<td class="gt_row gt_right" headers="RsquaredSD">0.09446370</td>
<td class="gt_row gt_right" headers="MAESD">0.05612583</td>
</tr>
<tr class="odd">
<td class="gt_row gt_right" headers="alpha">0.011</td>
<td class="gt_row gt_right" headers="lambda">1.727</td>
<td class="gt_row gt_right" headers="RMSE">0.7265929</td>
<td class="gt_row gt_right" headers="Rsquared">0.3185779</td>
<td class="gt_row gt_right" headers="MAE">0.5676531</td>
<td class="gt_row gt_right" headers="RMSESD">0.06210627</td>
<td class="gt_row gt_right" headers="RsquaredSD">0.09802932</td>
<td class="gt_row gt_right" headers="MAESD">0.05253257</td>
</tr>
<tr class="even">
<td class="gt_row gt_right" headers="alpha">0.022</td>
<td class="gt_row gt_right" headers="lambda">0.694</td>
<td class="gt_row gt_right" headers="RMSE">0.7276708</td>
<td class="gt_row gt_right" headers="Rsquared">0.3185370</td>
<td class="gt_row gt_right" headers="MAE">0.5695071</td>
<td class="gt_row gt_right" headers="RMSESD">0.06120405</td>
<td class="gt_row gt_right" headers="RsquaredSD">0.09481046</td>
<td class="gt_row gt_right" headers="MAESD">0.05397744</td>
</tr>
<tr class="odd">
<td class="gt_row gt_right" headers="alpha">0.022</td>
<td class="gt_row gt_right" headers="lambda">0.522</td>
<td class="gt_row gt_right" headers="RMSE">0.7298551</td>
<td class="gt_row gt_right" headers="Rsquared">0.3180041</td>
<td class="gt_row gt_right" headers="MAE">0.5726883</td>
<td class="gt_row gt_right" headers="RMSESD">0.06144171</td>
<td class="gt_row gt_right" headers="RsquaredSD">0.09315651</td>
<td class="gt_row gt_right" headers="MAESD">0.05466701</td>
</tr>
<tr class="even">
<td class="gt_row gt_right" headers="alpha">0.022</td>
<td class="gt_row gt_right" headers="lambda">0.866</td>
<td class="gt_row gt_right" headers="RMSE">0.7277250</td>
<td class="gt_row gt_right" headers="Rsquared">0.3168255</td>
<td class="gt_row gt_right" headers="MAE">0.5686165</td>
<td class="gt_row gt_right" headers="RMSESD">0.06109334</td>
<td class="gt_row gt_right" headers="RsquaredSD">0.09620112</td>
<td class="gt_row gt_right" headers="MAESD">0.05314454</td>
</tr>
<tr class="odd">
<td class="gt_row gt_right" headers="alpha">0.011</td>
<td class="gt_row gt_right" headers="lambda">1.900</td>
<td class="gt_row gt_right" headers="RMSE">0.7279475</td>
<td class="gt_row gt_right" headers="Rsquared">0.3163917</td>
<td class="gt_row gt_right" headers="MAE">0.5686306</td>
<td class="gt_row gt_right" headers="RMSESD">0.06199278</td>
<td class="gt_row gt_right" headers="RsquaredSD">0.09836348</td>
<td class="gt_row gt_right" headers="MAESD">0.05195792</td>
</tr>
<tr class="even">
<td class="gt_row gt_right" headers="alpha">0.011</td>
<td class="gt_row gt_right" headers="lambda">0.522</td>
<td class="gt_row gt_right" headers="RMSE">0.7339054</td>
<td class="gt_row gt_right" headers="Rsquared">0.3158314</td>
<td class="gt_row gt_right" headers="MAE">0.5786809</td>
<td class="gt_row gt_right" headers="RMSESD">0.06262747</td>
<td class="gt_row gt_right" headers="RsquaredSD">0.09381218</td>
<td class="gt_row gt_right" headers="MAESD">0.05677975</td>
</tr>
<tr class="odd">
<td class="gt_row gt_right" headers="alpha">0.033</td>
<td class="gt_row gt_right" headers="lambda">0.522</td>
<td class="gt_row gt_right" headers="RMSE">0.7303634</td>
<td class="gt_row gt_right" headers="Rsquared">0.3142904</td>
<td class="gt_row gt_right" headers="MAE">0.5715719</td>
<td class="gt_row gt_right" headers="RMSESD">0.06047743</td>
<td class="gt_row gt_right" headers="RsquaredSD">0.09405803</td>
<td class="gt_row gt_right" headers="MAESD">0.05348058</td>
</tr>
<tr class="even">
<td class="gt_row gt_right" headers="alpha">0.011</td>
<td class="gt_row gt_right" headers="lambda">2.072</td>
<td class="gt_row gt_right" headers="RMSE">0.7294330</td>
<td class="gt_row gt_right" headers="Rsquared">0.3141275</td>
<td class="gt_row gt_right" headers="MAE">0.5697778</td>
<td class="gt_row gt_right" headers="RMSESD">0.06194162</td>
<td class="gt_row gt_right" headers="RsquaredSD">0.09867437</td>
<td class="gt_row gt_right" headers="MAESD">0.05146113</td>
</tr>
<tr class="odd">
<td class="gt_row gt_right" headers="alpha">0.022</td>
<td class="gt_row gt_right" headers="lambda">1.038</td>
<td class="gt_row gt_right" headers="RMSE">0.7289549</td>
<td class="gt_row gt_right" headers="Rsquared">0.3140533</td>
<td class="gt_row gt_right" headers="MAE">0.5692525</td>
<td class="gt_row gt_right" headers="RMSESD">0.06118321</td>
<td class="gt_row gt_right" headers="RsquaredSD">0.09735362</td>
<td class="gt_row gt_right" headers="MAESD">0.05229037</td>
</tr>
<tr class="even">
<td class="gt_row gt_right" headers="alpha">0.022</td>
<td class="gt_row gt_right" headers="lambda">0.349</td>
<td class="gt_row gt_right" headers="RMSE">0.7362917</td>
<td class="gt_row gt_right" headers="Rsquared">0.3135250</td>
<td class="gt_row gt_right" headers="MAE">0.5805138</td>
<td class="gt_row gt_right" headers="RMSESD">0.06171108</td>
<td class="gt_row gt_right" headers="RsquaredSD">0.09216744</td>
<td class="gt_row gt_right" headers="MAESD">0.05541844</td>
</tr>
<tr class="odd">
<td class="gt_row gt_right" headers="alpha">0.033</td>
<td class="gt_row gt_right" headers="lambda">0.694</td>
<td class="gt_row gt_right" headers="RMSE">0.7296913</td>
<td class="gt_row gt_right" headers="Rsquared">0.3129244</td>
<td class="gt_row gt_right" headers="MAE">0.5700855</td>
<td class="gt_row gt_right" headers="RMSESD">0.06080031</td>
<td class="gt_row gt_right" headers="RsquaredSD">0.09625073</td>
<td class="gt_row gt_right" headers="MAESD">0.05273691</td>
</tr>
<tr class="even">
<td class="gt_row gt_right" headers="alpha">0.011</td>
<td class="gt_row gt_right" headers="lambda">2.244</td>
<td class="gt_row gt_right" headers="RMSE">0.7309768</td>
<td class="gt_row gt_right" headers="Rsquared">0.3118847</td>
<td class="gt_row gt_right" headers="MAE">0.5711072</td>
<td class="gt_row gt_right" headers="RMSESD">0.06195602</td>
<td class="gt_row gt_right" headers="RsquaredSD">0.09901043</td>
<td class="gt_row gt_right" headers="MAESD">0.05102660</td>
</tr>
<tr class="odd">
<td class="gt_row gt_right" headers="alpha">0.044</td>
<td class="gt_row gt_right" headers="lambda">0.522</td>
<td class="gt_row gt_right" headers="RMSE">0.7306761</td>
<td class="gt_row gt_right" headers="Rsquared">0.3116728</td>
<td class="gt_row gt_right" headers="MAE">0.5711300</td>
<td class="gt_row gt_right" headers="RMSESD">0.06048624</td>
<td class="gt_row gt_right" headers="RsquaredSD">0.09528180</td>
<td class="gt_row gt_right" headers="MAESD">0.05289960</td>
</tr>
<tr class="even">
<td class="gt_row gt_right" headers="alpha">0.033</td>
<td class="gt_row gt_right" headers="lambda">0.349</td>
<td class="gt_row gt_right" headers="RMSE">0.7355536</td>
<td class="gt_row gt_right" headers="Rsquared">0.3116308</td>
<td class="gt_row gt_right" headers="MAE">0.5781206</td>
<td class="gt_row gt_right" headers="RMSESD">0.06063449</td>
<td class="gt_row gt_right" headers="RsquaredSD">0.09156073</td>
<td class="gt_row gt_right" headers="MAESD">0.05410146</td>
</tr>
</tbody>
</table>

</div>
</div>
</div>
<p>The best hyperparameters for the model were alpha = 0.011 and lambda = 1.038. This indicates that the model prefers a nearly ridge regression approach (alpha close to 0) with moderate regularization (lambda value). This makes sense given that the predictors were pre-selected based on correlation with the response, so we want to retain most predictors while controlling for multicollinearity.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Plot tuning results</span></span>
<span id="cb11-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(enet_full_model) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb11-3">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_classic</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base_size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb11-4">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Elastic Net tuning"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jaychung10010.quarto.pub/all-things-bioinformatics/posts/2025-12-16_enet_predict_erlotinib/index_files/figure-html/plot-tuning-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>A major advantage of a linear model like elastic net is its interpretability. The coefficients correspond to how important each gene is for predicting Erlotinib sensitivity.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1">final_coef <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.matrix</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coef</span>(enet_full_model<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>finalModel, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">s =</span> enet_full_model<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>bestTune<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>lambda))[<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, ]</span>
<span id="cb12-2">final_coef_df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb12-3">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Gene =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(final_coef),</span>
<span id="cb12-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Coefficient =</span> final_coef</span>
<span id="cb12-5">)</span>
<span id="cb12-6"></span>
<span id="cb12-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Plot a bar plot for the top 20 sensitivity and top 20 resistance predictor genes</span></span>
<span id="cb12-8">final_coef_sorted <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> final_coef[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">order</span>(final_coef, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">decreasing =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)]</span>
<span id="cb12-9">top20_pos_coef <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(final_coef_sorted[final_coef_sorted <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>], <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>)</span>
<span id="cb12-10">top20_neg_coef <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tail</span>(final_coef_sorted[final_coef_sorted <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>], <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>)</span>
<span id="cb12-11"></span>
<span id="cb12-12">top_coef_df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb12-13">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Gene =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(top20_pos_coef), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(top20_neg_coef)),</span>
<span id="cb12-14">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Coefficient =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(top20_pos_coef, top20_neg_coef),</span>
<span id="cb12-15">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Direction =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Resistance predictors"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(top20_pos_coef)), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sensitivity predictors"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(top20_neg_coef)))</span>
<span id="cb12-16">)</span>
<span id="cb12-17"></span>
<span id="cb12-18"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(top_coef_df, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">reorder</span>(Gene, Coefficient), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> Coefficient, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> Direction)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-19">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_bar</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stat =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"identity"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.7</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-20">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_classic</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base_size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">18</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-21">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(</span>
<span id="cb12-22">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Top 40 predictor genes"</span>,</span>
<span id="cb12-23">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Gene"</span>,</span>
<span id="cb12-24">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Coefficient"</span></span>
<span id="cb12-25">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-26">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_manual</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sensitivity predictors"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"darkgreen"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Resistance predictors"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"darkred"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-27">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(</span>
<span id="cb12-28">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"top"</span>,</span>
<span id="cb12-29">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.text.x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">angle =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">45</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hjust =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>),</span>
<span id="cb12-30">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.text =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)</span>
<span id="cb12-31">    )</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jaychung10010.quarto.pub/all-things-bioinformatics/posts/2025-12-16_enet_predict_erlotinib/index_files/figure-html/coefficients-analysis-1.png" class="img-fluid figure-img" width="864"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="prediction-on-all-cell-lines" class="level3">
<h3 class="anchored" data-anchor-id="prediction-on-all-cell-lines">Prediction on All Cell Lines</h3>
<p>We now use the trained model to predict Erlotinib sensitivity for all CCLE cell lines with available RNA expression data.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Predict on all CCLE cell lines with available RNA expression</span></span>
<span id="cb13-2">all_ccle_ge <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ccle_ge_match_sam <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.data.frame</span>()</span>
<span id="cb13-3">all_ccle_ge <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> all_ccle_ge <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb13-4">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">all_of</span>(top_predictors)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb13-5">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">na.omit</span>() <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 1517 cell lines with complete GE data</span></span>
<span id="cb13-6"></span>
<span id="cb13-7">enet_pred_all_ccle <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predict</span>(enet_full_model, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">newdata =</span> all_ccle_ge)</span>
<span id="cb13-8"></span>
<span id="cb13-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Save predicted Erlotinib LFC for all CCLE cell lines</span></span>
<span id="cb13-10">predicted_erlotinib_df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb13-11">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Cell_Line =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rownames</span>(all_ccle_ge),</span>
<span id="cb13-12">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Predicted_Erlotinib_LFC =</span> enet_pred_all_ccle</span>
<span id="cb13-13">)</span>
<span id="cb13-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># fwrite(predicted_erlotinib_df, "files/predicted_erlotinib_LFC_all_CCLE_cell_lines.csv", sep = ",", row.names = FALSE, quote = FALSE)</span></span></code></pre></div></div>
</div>
</section>
</section>
<section id="aim-2-evaluate-whether-the-predicted-erlotinib-sensitivity-provides-additional-insights-beyond-the-measured-data" class="level2">
<h2 class="anchored" data-anchor-id="aim-2-evaluate-whether-the-predicted-erlotinib-sensitivity-provides-additional-insights-beyond-the-measured-data">Aim 2: Evaluate whether the predicted Erlotinib sensitivity provides additional insights beyond the measured data</h2>
<section id="comparison-of-actual-vs.-predicted" class="level3">
<h3 class="anchored" data-anchor-id="comparison-of-actual-vs.-predicted">Comparison of Actual vs.&nbsp;Predicted</h3>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Load actual and predicted Erlotinib LFC data</span></span>
<span id="cb14-2">actual_erlotinib_df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb14-3">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Cell_Line =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rownames</span>(ge_dat_final),</span>
<span id="cb14-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Actual_Erlotinib_LFC =</span> drug_LFC_final</span>
<span id="cb14-5">)</span>
<span id="cb14-6"></span>
<span id="cb14-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># If we didn't run the prediction above, we could load it:</span></span>
<span id="cb14-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># predicted_erlotinib_df &lt;- fread("files/predicted_erlotinib_LFC_all_CCLE_cell_lines.csv", data.table = FALSE)</span></span>
<span id="cb14-9"></span>
<span id="cb14-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Merge actual and predicted data</span></span>
<span id="cb14-11">erlotinib_compare_df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">merge</span>(actual_erlotinib_df, predicted_erlotinib_df, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Cell_Line"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">all =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb14-12"></span>
<span id="cb14-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Add cancer lineage information</span></span>
<span id="cb14-14">erlotinib_compare_df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> erlotinib_compare_df <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">left_join</span>(sample_info <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(StrippedCellLineName, OncotreeLineage),</span>
<span id="cb14-15">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Cell_Line"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"StrippedCellLineName"</span>)</span>
<span id="cb14-16">)</span>
<span id="cb14-17"></span>
<span id="cb14-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Add EGFR mutation status</span></span>
<span id="cb14-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Note: Using tryCatch or existence check in case file is missing in user environment</span></span>
<span id="cb14-20"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">file.exists</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"files/CCLE_24Q2_HOTMUT_match_sample_info.RData"</span>)) {</span>
<span id="cb14-21">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">load</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"files/CCLE_24Q2_HOTMUT_match_sample_info.RData"</span>)</span>
<span id="cb14-22">    egfr_mutation_status <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> hotmut_dat_match_sam <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb14-23">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rownames_to_column</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">var =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Cell_Line"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb14-24">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(Cell_Line, EGFR) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb14-25">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">EGFR_Mutation_Status =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">recode</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.factor</span>(EGFR), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">0</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Wildtype"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">1</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Heterozygous Mutant"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">2</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Homozygous Mutant"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb14-26">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(Cell_Line, EGFR_Mutation_Status)</span>
<span id="cb14-27">    erlotinib_compare_df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> erlotinib_compare_df <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">left_join</span>(egfr_mutation_status, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Cell_Line"</span>)</span>
<span id="cb14-28">} <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> {</span>
<span id="cb14-29">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">warning</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"files/CCLE_24Q2_HOTMUT_match_sample_info.RData not found. Skipping EGFR analysis."</span>)</span>
<span id="cb14-30">}</span>
<span id="cb14-31"></span>
<span id="cb14-32"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Plot actual vs predicted Erlotinib LFC for cell lines with actual data</span></span>
<span id="cb14-33">plot_df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> erlotinib_compare_df <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</span>(Actual_Erlotinib_LFC))</span>
<span id="cb14-34"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(plot_df, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> Actual_Erlotinib_LFC, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> Predicted_Erlotinib_LFC)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb14-35">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"steelblue"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.7</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb14-36">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_classic</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base_size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb14-37">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb14-38">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(</span>
<span id="cb14-39">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Elastic Net prediction of Erlotinib LFC"</span>,</span>
<span id="cb14-40">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Actual Erlotinib LFC"</span>,</span>
<span id="cb14-41">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Predicted Erlotinib LFC"</span></span>
<span id="cb14-42">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb14-43">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stat_cor</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pearson"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label.x.npc =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"left"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label.y.npc =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"top"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"darkred"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb14-44">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_smooth</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lm"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"darkred"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">se =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jaychung10010.quarto.pub/all-things-bioinformatics/posts/2025-12-16_enet_predict_erlotinib/index_files/figure-html/compare-actual-predicted-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p><strong>Note:</strong> This prediction performance result is evaluated on the training data, so it is expected to be better than on independent test data (overfitting). It is important to always evaluate model performance on independent test data. From my previous post, the prediction performance on independent test data looks like this (not bad but not as good as training data):</p>
<div class="cell" data-layout-align="left">
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-left">
<figure class="figure">
<p><img src="https://jaychung10010.quarto.pub/all-things-bioinformatics/posts/2025-12-16_enet_predict_erlotinib/files/selected_top_RNA_enet_erlotinib_pred_vs_actual_repeat_6.png" class="img-fluid quarto-figure quarto-figure-left figure-img" style="width:60.0%"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="cancer-lineage-distribution" class="level3">
<h3 class="anchored" data-anchor-id="cancer-lineage-distribution">Cancer Lineage Distribution</h3>
<p>We compare the cancer lineages covered by the actual measured data versus the predicted data.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1">actual_lineages <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> erlotinib_compare_df <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb15-2">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</span>(Actual_Erlotinib_LFC)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb15-3">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(Cell_Line, OncotreeLineage) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb15-4">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">distinct</span>()</span>
<span id="cb15-5">predicted_lineages <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> erlotinib_compare_df <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb15-6">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(Cell_Line, OncotreeLineage) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb15-7">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">distinct</span>()</span>
<span id="cb15-8"></span>
<span id="cb15-9">actual_lineage_counts <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> actual_lineages <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb15-10">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(OncotreeLineage) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb15-11">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Count =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n</span>()) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb15-12">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Actual data"</span>)</span>
<span id="cb15-13">predicted_lineage_counts <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> predicted_lineages <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb15-14">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(OncotreeLineage) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb15-15">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Count =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n</span>()) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb15-16">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Predicted data"</span>)</span>
<span id="cb15-17">lineage_counts_df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rbind</span>(actual_lineage_counts, predicted_lineage_counts) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(OncotreeLineage <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>)</span>
<span id="cb15-18"></span>
<span id="cb15-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Make missing lineage in actual data have count 0</span></span>
<span id="cb15-20">all_lineages <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unique</span>(lineage_counts_df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>OncotreeLineage)</span>
<span id="cb15-21"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (lineage <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> all_lineages) {</span>
<span id="cb15-22">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span>(lineage <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> actual_lineage_counts<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>OncotreeLineage)) {</span>
<span id="cb15-23">        lineage_counts_df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rbind</span>(lineage_counts_df, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">OncotreeLineage =</span> lineage, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Count =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Actual data"</span>))</span>
<span id="cb15-24">    }</span>
<span id="cb15-25">}</span>
<span id="cb15-26"></span>
<span id="cb15-27"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(lineage_counts_df, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">reorder</span>(OncotreeLineage, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>Count), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> Count, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> Type)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb15-28">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_bar</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stat =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"identity"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">position =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">position_dodge</span>()) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb15-29">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_classic</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base_size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">18</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb15-30">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(</span>
<span id="cb15-31">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Cancer Lineage Distribution"</span>,</span>
<span id="cb15-32">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Cancer Lineage"</span>,</span>
<span id="cb15-33">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Number of Cell Lines"</span></span>
<span id="cb15-34">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb15-35">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_text</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> Count, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> Type), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">position =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">position_dodge</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.9</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">vjust =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb15-36">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_manual</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Actual data"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"steelblue"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Predicted data"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"darkorange"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb15-37">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_manual</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Actual data"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"steelblue"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Predicted data"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"darkorange"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb15-38">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(</span>
<span id="cb15-39">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"top"</span>,</span>
<span id="cb15-40">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.text.x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">angle =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">45</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hjust =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>),</span>
<span id="cb15-41">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.text =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)</span>
<span id="cb15-42">    )</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jaychung10010.quarto.pub/all-things-bioinformatics/posts/2025-12-16_enet_predict_erlotinib/index_files/figure-html/lineage-distribution-1.png" class="img-fluid figure-img" width="768"></p>
</figure>
</div>
</div>
</div>
<p>You can see here that the predicted data covers many more cancer lineages than the actual data, which may improve the power of downstream analyses.</p>
</section>
<section id="predicted-sensitivity-by-lineage" class="level3">
<h3 class="anchored" data-anchor-id="predicted-sensitivity-by-lineage">Predicted Sensitivity by Lineage</h3>
<p>Next, let’s see if the predicted Erlotinib LFC can better capture differences across cancer lineages.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Plot actual vs predicted Erlotinib LFC boxplot faceted by LFC type and stratified by OncotreeLineage</span></span>
<span id="cb16-2">plot_df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> erlotinib_compare_df <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb16-3">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(Cell_Line, Actual_Erlotinib_LFC, Predicted_Erlotinib_LFC, OncotreeLineage) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb16-4">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pivot_longer</span>(</span>
<span id="cb16-5">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cols =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(Actual_Erlotinib_LFC, Predicted_Erlotinib_LFC),</span>
<span id="cb16-6">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">names_to =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"LFC_Type"</span>,</span>
<span id="cb16-7">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values_to =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Erlotinib_LFC"</span></span>
<span id="cb16-8">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb16-9">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">na.omit</span>()</span>
<span id="cb16-10"></span>
<span id="cb16-11">lineage_counts <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> plot_df <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb16-12">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(OncotreeLineage) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb16-13">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Count =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n</span>()) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb16-14">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(Count <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)</span>
<span id="cb16-15">plot_df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> plot_df <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(OncotreeLineage <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> lineage_counts<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>OncotreeLineage)</span>
<span id="cb16-16"></span>
<span id="cb16-17">lineage_sort_by_predicted_LFC <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> plot_df <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb16-18">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(LFC_Type <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Predicted_Erlotinib_LFC"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb16-19">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(OncotreeLineage) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb16-20">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Median_Predicted_LFC =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">median</span>(Erlotinib_LFC)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb16-21">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(Median_Predicted_LFC) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb16-22">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pull</span>(OncotreeLineage)</span>
<span id="cb16-23"></span>
<span id="cb16-24">plot_df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>OncotreeLineage <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(plot_df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>OncotreeLineage, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</span> lineage_sort_by_predicted_LFC)</span>
<span id="cb16-25"></span>
<span id="cb16-26"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(plot_df, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> OncotreeLineage, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> Erlotinib_LFC, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> LFC_Type)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb16-27">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_jitter</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb16-28">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_boxplot</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">outliers =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey99"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb16-29">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_wrap</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>LFC_Type,</span>
<span id="cb16-30">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scales =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"free_y"</span>,</span>
<span id="cb16-31">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labeller =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as_labeller</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Actual_Erlotinib_LFC =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Actual Erlotinib LFC"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Predicted_Erlotinib_LFC =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Predicted Erlotinib LFC"</span>))</span>
<span id="cb16-32">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb16-33">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_classic</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base_size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">18</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb16-34">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_manual</span>(</span>
<span id="cb16-35">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Actual_Erlotinib_LFC =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"steelblue"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Predicted_Erlotinib_LFC =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"darkorange"</span>),</span>
<span id="cb16-36">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Actual Erlotinib LFC"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Predicted Erlotinib LFC"</span>)</span>
<span id="cb16-37">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb16-38">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(</span>
<span id="cb16-39">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Erlotinib LFC by Cancer Lineage"</span>,</span>
<span id="cb16-40">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Cancer Lineage"</span>,</span>
<span id="cb16-41">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Erlotinib LFC"</span></span>
<span id="cb16-42">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb16-43">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_hline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yintercept =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dashed"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey30"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb16-44">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(</span>
<span id="cb16-45">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.text.x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">angle =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hjust =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>),</span>
<span id="cb16-46">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span></span>
<span id="cb16-47">    )</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jaychung10010.quarto.pub/all-things-bioinformatics/posts/2025-12-16_enet_predict_erlotinib/index_files/figure-html/lineage-sensitivity-1.png" class="img-fluid figure-img" width="960"></p>
</figure>
</div>
</div>
</div>
<p><strong>Result</strong>: Predicted data discovered lineage sensitivity for prostate, cervix, biliary tract; and showed that blood lineages are likely not sensitive.</p>
</section>
<section id="predicted-sensitivity-by-egfr-mutation-status" class="level3">
<h3 class="anchored" data-anchor-id="predicted-sensitivity-by-egfr-mutation-status">Predicted Sensitivity by EGFR Mutation Status</h3>
<p>Finally, let’s see whether the predicted Erlotinib LFC can capture known biological associations, such as sensitivity in EGFR mutant cell lines.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exists</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"egfr_mutation_status"</span>)) {</span>
<span id="cb17-2">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Compare predicted vs actual Erlotinib LFC between EGFR mutant and wildtype cell lines</span></span>
<span id="cb17-3">    plot_df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> erlotinib_compare_df <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb17-4">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(Cell_Line, Actual_Erlotinib_LFC, Predicted_Erlotinib_LFC, EGFR_Mutation_Status) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb17-5">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pivot_longer</span>(</span>
<span id="cb17-6">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cols =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(Actual_Erlotinib_LFC, Predicted_Erlotinib_LFC),</span>
<span id="cb17-7">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">names_to =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"LFC_Type"</span>,</span>
<span id="cb17-8">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values_to =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Erlotinib_LFC"</span></span>
<span id="cb17-9">        ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb17-10">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">na.omit</span>()</span>
<span id="cb17-11"></span>
<span id="cb17-12">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Plot boxplot: EGFR mutant vs wildtype facet by actual vs predicted with test p-value</span></span>
<span id="cb17-13">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(plot_df, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> EGFR_Mutation_Status, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> Erlotinib_LFC, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> EGFR_Mutation_Status)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb17-14">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_boxplot</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">outliers =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb17-15">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_jitter</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb17-16">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_wrap</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>LFC_Type, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labeller =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as_labeller</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Actual_Erlotinib_LFC =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Actual Erlotinib LFC"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Predicted_Erlotinib_LFC =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Predicted Erlotinib LFC"</span>))) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb17-17">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_classic</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base_size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">18</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb17-18">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(</span>
<span id="cb17-19">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Erlotinib LFC by EGFR Mutation Status"</span>,</span>
<span id="cb17-20">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"EGFR Hotspot Mutation Status"</span>,</span>
<span id="cb17-21">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Erlotinib LFC"</span></span>
<span id="cb17-22">        ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb17-23">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_manual</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Wildtype"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"darkgreen"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Heterozygous Mutant"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"darkorange"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Homozygous Mutant"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"darkred"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb17-24">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(</span>
<span id="cb17-25">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span>,</span>
<span id="cb17-26">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.text.x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">angle =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">45</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hjust =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb17-27">        ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb17-28">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stat_compare_means</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> EGFR_Mutation_Status), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label.y.npc =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.9</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>)</span>
<span id="cb17-29">} <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> {</span>
<span id="cb17-30">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"EGFR mutation data not loaded."</span>)</span>
<span id="cb17-31">}</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jaychung10010.quarto.pub/all-things-bioinformatics/posts/2025-12-16_enet_predict_erlotinib/index_files/figure-html/egfr-status-1.png" class="img-fluid figure-img" width="864"></p>
</figure>
</div>
</div>
</div>
<p>It seems that even in the predicted data, there is only a marginal difference in Erlotinib LFC between EGFR mutant and wildtype cell lines. This could be due to the fact that EGFR mutation instances are relatively rare in all of DepMap cell lines.</p>
</section>
</section>
<section id="summary" class="level2">
<h2 class="anchored" data-anchor-id="summary">Summary</h2>
<p>Here I demonstrated how to train an elastic net model using R’s Caret package with hyperparameter tuning via cross-validation from end to end. I showed the top 20 sensitivity and resistance predictor genes identified by the final model. I then applied the final model to all available DepMap cell lines to generate Erlotinib sensitivity predictions.</p>
<p>Finally, I showed that the predicted data can provide additional insights beyond the measured data, such as covering more cancer lineages. However, some known biological associations (e.g.&nbsp;EGFR mutation status) may still be difficult to capture due to data limitations.</p>
<p>Overall, this framework can be useful for generating drug sensitivity predictions for cell lines without measured data, which can aid in drug repurposing and precision oncology efforts.</p>
<section id="references" class="level3">
<h3 class="anchored" data-anchor-id="references">References</h3>
<ul>
<li>DepMap Portal: <a href="https://depmap.org/portal/" class="uri">https://depmap.org/portal/</a></li>
<li>Caret Package Documentation: <a href="https://topepo.github.io/caret/" class="uri">https://topepo.github.io/caret/</a></li>
<li>Applied Predictive Modeling by Kuhn and Johnson: <a href="https://www.springer.com/gp/book/9781461468486" class="uri">https://www.springer.com/gp/book/9781461468486</a></li>
</ul>


</section>
</section>

 ]]></description>
  <category>R</category>
  <category>DepMap</category>
  <category>Machine Learning</category>
  <category>Elastic Net</category>
  <category>Caret</category>
  <guid>https://jaychung10010.quarto.pub/all-things-bioinformatics/posts/2025-12-16_enet_predict_erlotinib/</guid>
  <pubDate>Fri, 26 Dec 2025 08:00:00 GMT</pubDate>
  <media:content url="https://jaychung10010.quarto.pub/all-things-bioinformatics/posts/2025-12-16_enet_predict_erlotinib/files/enet_tuning_plot_erlotinib.png" medium="image" type="image/png" height="108" width="144"/>
</item>
<item>
  <title>Training a Random Forest model with Scikit-Learn on DepMap data</title>
  <dc:creator>Jay Chung</dc:creator>
  <link>https://jaychung10010.quarto.pub/all-things-bioinformatics/posts/2025-12-12_ml_with_scikitlearn/</link>
  <description><![CDATA[ 





<section id="introduction" class="level1">
<h1>Introduction</h1>
<p>In this specific blog post, we will explore the Cancer Dependency Map (DepMap) dataset to understand the relationship between genetic dependencies (CRISPR knockout effects) and gene expression levels. We will use Python’s <code>pandas</code> ecosystem for data loading, exploratory data analysis, and visualization. We will also use <code>scikit-learn</code>’s functions to build a Random Forest predictor pipeline that performs preprocessing, imputes missing values, and tunes hyperparameters. Finally, we will evaluate the model’s performance and extract genes that are most important for predicting gene dependency.</p>
</section>
<section id="data-loading" class="level1">
<h1>Data Loading</h1>
<p>First, we import the necessary libraries and load the datasets. We are using three main datasets:</p>
<ol type="1">
<li><strong>Sample Info</strong>: Metadata about the cell lines (lineage, disease subtype, etc.).</li>
<li><strong>Chronos Data</strong>: CRISPR knockout scores representing gene dependency (lower score = higher dependency).</li>
<li><strong>Gene Expression (GE) Data</strong>: RNA-seq expression levels for various genes.</li>
</ol>
<p>Data can be downloaded from here (modified from DepMap 24Q2): <a href="https://zenodo.org/records/17970100" class="uri">https://zenodo.org/records/17970100</a></p>
<div id="load-libraries-and-data" class="cell" data-execution_count="1">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> pandas <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> pd</span>
<span id="cb1-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> matplotlib.pyplot <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> plt</span>
<span id="cb1-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> numpy <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> np</span>
<span id="cb1-4"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> os</span>
<span id="cb1-5"></span>
<span id="cb1-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Define file paths (relative to the ML_practice directory)</span></span>
<span id="cb1-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Here I have preprocessed the data for this demo so the cell lines (rows) are aligned</span></span>
<span id="cb1-8">sample_info_path <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ML_practice/sample_info.csv"</span></span>
<span id="cb1-9">chronos_path <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ML_practice/chronos_dat.csv"</span></span>
<span id="cb1-10">ge_path <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ML_practice/ccle_ge.csv"</span></span>
<span id="cb1-11"></span>
<span id="cb1-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Load the datasets</span></span>
<span id="cb1-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Sample metadata containing cell line information</span></span>
<span id="cb1-14">sample_data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.read_csv(sample_info_path)</span>
<span id="cb1-15"></span>
<span id="cb1-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Chronos scores: Gene effect scores from CRISPR knockout screens</span></span>
<span id="cb1-17">chronos_dat <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.read_csv(chronos_path)</span>
<span id="cb1-18"></span>
<span id="cb1-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Gene Expression data: mRNA expression levels log2(TPM + 1)</span></span>
<span id="cb1-20">ge_dat <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.read_csv(ge_path)</span></code></pre></div></div>
</div>
</section>
<section id="exploratory-data-analysis" class="level1">
<h1>Exploratory Data Analysis</h1>
<p>Let’s inspect the structure and basic statistics of our datasets.</p>
<section id="sample-information" class="level2">
<h2 class="anchored" data-anchor-id="sample-information">Sample Information</h2>
<p>Checking the metadata to understand the cell lines we are working with.</p>
<div id="inspect-sample-data" class="cell" data-execution_count="2">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Display dataset information: columns, non-null counts, and data types</span></span>
<span id="cb2-2"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sample Data Info:"</span>)</span>
<span id="cb2-3">sample_data.info()</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>Sample Data Info:
&lt;class 'pandas.core.frame.DataFrame'&gt;
RangeIndex: 1959 entries, 0 to 1958
Data columns (total 9 columns):
 #   Column                   Non-Null Count  Dtype 
---  ------                   --------------  ----- 
 0   ModelID                  1959 non-null   object
 1   StrippedCellLineName     1959 non-null   object
 2   CCLEName                 1902 non-null   object
 3   OncotreeLineage          1954 non-null   object
 4   OncotreeSubtype          1959 non-null   object
 5   OncotreePrimaryDisease   1959 non-null   object
 6   LegacySubSubtype         831 non-null    object
 7   LegacyMolecularSubtype   151 non-null    object
 8   PatientMolecularSubtype  138 non-null    object
dtypes: object(9)
memory usage: 137.9+ KB</code></pre>
</div>
</div>
</section>
<section id="chronos-dependency-data" class="level2">
<h2 class="anchored" data-anchor-id="chronos-dependency-data">Chronos (Dependency) Data</h2>
<p>This dataset contains dependency scores for key genes like <em>SMARCA2</em>, <em>SOX10</em>, and <em>KRAS</em>.</p>
<div id="inspect-chronos-data" class="cell" data-execution_count="3">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Display dataset information for Chronos data</span></span>
<span id="cb4-2"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">Chronos Data Info:"</span>)</span>
<span id="cb4-3">chronos_dat.info()</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>
Chronos Data Info:
&lt;class 'pandas.core.frame.DataFrame'&gt;
RangeIndex: 1959 entries, 0 to 1958
Data columns (total 4 columns):
 #   Column     Non-Null Count  Dtype  
---  ------     --------------  -----  
 0   cell_line  1959 non-null   object 
 1   SMARCA2    1150 non-null   float64
 2   SOX10      1150 non-null   float64
 3   KRAS       1150 non-null   float64
dtypes: float64(3), object(1)
memory usage: 61.3+ KB</code></pre>
</div>
</div>
</section>
<section id="gene-expression-data" class="level2">
<h2 class="anchored" data-anchor-id="gene-expression-data">Gene Expression Data</h2>
<p>This dataset provides expression levels for a wide range of genes across the cell lines.</p>
<div id="cell-inspect-ge-data" class="cell" data-execution_count="4">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb6-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Display dataset information for Gene Expression data</span></span>
<span id="cb6-2"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">Gene Expression Data Info:"</span>)</span>
<span id="cb6-3">ge_dat.info()</span>
<span id="cb6-4"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">First 5 rows of Gene Expression Data:"</span>)</span>
<span id="cb6-5">display(ge_dat.head())</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>
Gene Expression Data Info:
&lt;class 'pandas.core.frame.DataFrame'&gt;
RangeIndex: 1959 entries, 0 to 1958
Columns: 19153 entries, TSPAN6 to CDR1
dtypes: float64(19153)
memory usage: 286.3 MB

First 5 rows of Gene Expression Data:</code></pre>
</div>
<div id="inspect-ge-data" class="cell-output cell-output-display">
<div>


<table class="dataframe caption-top table table-sm table-striped small" data-border="1">
<thead>
<tr class="header">
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th">TSPAN6</th>
<th data-quarto-table-cell-role="th">TNMD</th>
<th data-quarto-table-cell-role="th">DPM1</th>
<th data-quarto-table-cell-role="th">SCYL3</th>
<th data-quarto-table-cell-role="th">FIRRM</th>
<th data-quarto-table-cell-role="th">FGR</th>
<th data-quarto-table-cell-role="th">CFH</th>
<th data-quarto-table-cell-role="th">FUCA2</th>
<th data-quarto-table-cell-role="th">GCLC</th>
<th data-quarto-table-cell-role="th">NFYA</th>
<th data-quarto-table-cell-role="th">...</th>
<th data-quarto-table-cell-role="th">SPDYE11</th>
<th data-quarto-table-cell-role="th">H3C2</th>
<th data-quarto-table-cell-role="th">H3C3</th>
<th data-quarto-table-cell-role="th">DUS4L-BCAP29</th>
<th data-quarto-table-cell-role="th">C8orf44-SGK3</th>
<th data-quarto-table-cell-role="th">ELOA3BP</th>
<th data-quarto-table-cell-role="th">NPBWR1</th>
<th data-quarto-table-cell-role="th">ELOA3DP</th>
<th data-quarto-table-cell-role="th">ELOA3P</th>
<th data-quarto-table-cell-role="th">CDR1</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<th data-quarto-table-cell-role="th">0</th>
<td>5.183487</td>
<td>0.000000</td>
<td>7.497612</td>
<td>2.107688</td>
<td>4.217231</td>
<td>0.042644</td>
<td>0.903038</td>
<td>5.722193</td>
<td>4.676944</td>
<td>3.720278</td>
<td>...</td>
<td>0.056584</td>
<td>1.137504</td>
<td>0.000000</td>
<td>1.794936</td>
<td>0.201634</td>
<td>0.000000</td>
<td>0.028569</td>
<td>0.0</td>
<td>0.214125</td>
<td>0.014355</td>
</tr>
<tr class="even">
<th data-quarto-table-cell-role="th">1</th>
<td>0.176323</td>
<td>0.000000</td>
<td>5.702103</td>
<td>1.238787</td>
<td>3.119356</td>
<td>4.141596</td>
<td>0.163499</td>
<td>4.134221</td>
<td>4.111866</td>
<td>2.347666</td>
<td>...</td>
<td>0.000000</td>
<td>1.952334</td>
<td>0.238787</td>
<td>1.516015</td>
<td>0.000000</td>
<td>0.000000</td>
<td>0.028569</td>
<td>0.0</td>
<td>0.000000</td>
<td>0.000000</td>
</tr>
<tr class="odd">
<th data-quarto-table-cell-role="th">2</th>
<td>5.309976</td>
<td>0.084064</td>
<td>7.846117</td>
<td>1.875780</td>
<td>3.894333</td>
<td>0.000000</td>
<td>0.056584</td>
<td>6.666615</td>
<td>4.738768</td>
<td>3.984589</td>
<td>...</td>
<td>0.014355</td>
<td>0.918386</td>
<td>0.000000</td>
<td>1.655352</td>
<td>0.000000</td>
<td>0.028569</td>
<td>0.000000</td>
<td>0.0</td>
<td>0.028569</td>
<td>0.000000</td>
</tr>
<tr class="even">
<th data-quarto-table-cell-role="th">3</th>
<td>2.176323</td>
<td>0.000000</td>
<td>5.454505</td>
<td>2.480265</td>
<td>3.921246</td>
<td>0.887525</td>
<td>4.958843</td>
<td>3.949535</td>
<td>4.877253</td>
<td>4.829850</td>
<td>...</td>
<td>0.014355</td>
<td>0.536053</td>
<td>0.505891</td>
<td>2.643856</td>
<td>0.097611</td>
<td>0.000000</td>
<td>0.000000</td>
<td>0.0</td>
<td>0.000000</td>
<td>0.000000</td>
</tr>
<tr class="odd">
<th data-quarto-table-cell-role="th">4</th>
<td>2.451541</td>
<td>0.000000</td>
<td>5.884842</td>
<td>2.927896</td>
<td>5.299391</td>
<td>0.201634</td>
<td>5.759156</td>
<td>4.150560</td>
<td>5.531069</td>
<td>5.029895</td>
<td>...</td>
<td>0.000000</td>
<td>0.895303</td>
<td>1.350497</td>
<td>2.709291</td>
<td>0.678072</td>
<td>0.000000</td>
<td>0.000000</td>
<td>0.0</td>
<td>0.000000</td>
<td>0.000000</td>
</tr>
</tbody>
</table>

<p>5 rows × 19153 columns</p>
</div>
</div>
</div>
</section>
<section id="data-distribution-and-correlations" class="level2">
<h2 class="anchored" data-anchor-id="data-distribution-and-correlations">Data Distribution and Correlations</h2>
<p>We can look at the overall distribution of the dependency data using a scatter matrix. This helps us spot potential correlations or clusters between different gene dependencies.</p>
<div id="cell-scatter-matrix" class="cell" data-execution_count="5">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb8-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> pandas.plotting <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> scatter_matrix</span>
<span id="cb8-2"></span>
<span id="cb8-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create a scatter matrix to visualize pair-wise relationships in the Chronos dataset</span></span>
<span id="cb8-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># This includes histograms on the diagonal and scatter plots on off-diagonals</span></span>
<span id="cb8-5">scatter_matrix(chronos_dat)</span>
<span id="cb8-6">plt.show()</span></code></pre></div></div>
<div class="cell-output cell-output-display">
<div id="scatter-matrix" class="quarto-figure quarto-figure-center anchored">
<figure class="figure">
<p><img src="https://jaychung10010.quarto.pub/all-things-bioinformatics/posts/2025-12-12_ml_with_scikitlearn/index_files/figure-html/scatter-matrix-output-1.png" width="583" height="432" class="figure-img"></p>
<figcaption>Scatter matrix of Chronos dependency scores for selected genes.</figcaption>
</figure>
</div>
</div>
</div>
<p>Let’s verify the correlations numerically. We drop the ‘cell_line’ column as it is categorical.</p>
<div id="chronos-correlation" class="cell" data-execution_count="6">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb9-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calculate the correlation matrix for the numeric columns in Chronos data</span></span>
<span id="cb9-2">chronos_corr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> chronos_dat.drop(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cell_line"</span>, axis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>).corr()</span>
<span id="cb9-3"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">Correlation Matrix (Chronos Data):"</span>)</span>
<span id="cb9-4"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(chronos_corr)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>
Correlation Matrix (Chronos Data):
          SMARCA2     SOX10      KRAS
SMARCA2  1.000000 -0.032416  0.005902
SOX10   -0.032416  1.000000 -0.052455
KRAS     0.005902 -0.052455  1.000000</code></pre>
</div>
</div>
</section>
</section>
<section id="visualization-dependency-vs.-expression" class="level1">
<h1>Visualization: Dependency vs.&nbsp;Expression</h1>
<p>A key question in cancer biology is whether gene expression predicts dependency. For example, if a cell line highly expresses <em>SOX10</em>, is it more dependent on <em>SOX10</em> for survival (lower Chronos score)?</p>
<p>Let’s visualize the relationship between <em>SOX10</em> dependency (Chronos score) and <em>SOX10</em> gene expression.</p>
<div id="cell-plot-sox10-correlation" class="cell" data-execution_count="7">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb11-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Plot SOX10 Chronos score (x-axis) vs SOX10 Gene Expression (y-axis)</span></span>
<span id="cb11-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Note: Lower Chronos score means higher dependency.</span></span>
<span id="cb11-3">plt.figure(figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>))</span>
<span id="cb11-4">plt.scatter(chronos_dat[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SOX10"</span>], ge_dat[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SOX10"</span>], alpha<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.6</span>)</span>
<span id="cb11-5">plt.xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SOX10 Chronos Score (Dependency)"</span>)</span>
<span id="cb11-6">plt.ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SOX10 Gene Expression"</span>)</span>
<span id="cb11-7">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SOX10: Dependency vs Expression"</span>)</span>
<span id="cb11-8">plt.grid(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>, linestyle<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'--'</span>, alpha<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>)</span>
<span id="cb11-9">plt.show()</span></code></pre></div></div>
<div class="cell-output cell-output-display">
<div id="plot-sox10-correlation" class="quarto-figure quarto-figure-center anchored">
<figure class="figure">
<p><img src="https://jaychung10010.quarto.pub/all-things-bioinformatics/posts/2025-12-12_ml_with_scikitlearn/index_files/figure-html/plot-sox10-correlation-output-1.png" width="651" height="523" class="figure-img"></p>
<figcaption>Scatter plot showing the relationship between SOX10 Gene Expression and SOX10 Dependency (Chronos Score).</figcaption>
</figure>
</div>
</div>
</div>
<p>Finally, let’s calculate the Pearson correlation coefficient between these two variables to quantify the relationship.</p>
<div id="calc-sox10-correlation" class="cell" data-execution_count="8">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb12-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calculate Pearson correlation between SOX10 dependency and expression</span></span>
<span id="cb12-2">sox10_corr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> chronos_dat[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SOX10"</span>].corr(ge_dat[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SOX10"</span>])</span>
<span id="cb12-3"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Pearson Correlation between SOX10 Chronos and Gene Expression: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>sox10_corr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.4f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>Pearson Correlation between SOX10 Chronos and Gene Expression: -0.8491</code></pre>
</div>
</div>
<p>This correlation value suggests a strong relationship between SOX10 gene expression and dependency.</p>
</section>
<section id="machine-learning" class="level1">
<h1>Machine Learning</h1>
<p>Now we will build a machine learning model to predict the <em>SOX10</em> dependency score based on the gene expression profile of the cell lines.</p>
<section id="data-preprocessing" class="level2">
<h2 class="anchored" data-anchor-id="data-preprocessing">Data Preprocessing</h2>
<p>Before training, we need to prepare our data.</p>
<ol type="1">
<li><strong>Alignment</strong>: Ensure the cell lines in the dependency dataset match those in the gene expression dataset.</li>
<li><strong>Merging</strong>: Combined the target variable (<em>SOX10</em> Chronos score) with the feature set (Gene Expression).</li>
<li><strong>Imputation</strong>: Dealing with missing values using K-Nearest Neighbors (KNN) imputation.</li>
</ol>
<div id="data-preprocessing" class="cell" data-execution_count="9">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb14-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Align and merge data</span></span>
<span id="cb14-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The rows are already pre-aligned by cell line index</span></span>
<span id="cb14-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Adding target variable to the dataframe for alignment</span></span>
<span id="cb14-4">ge_dat <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.read_csv(ge_path)</span>
<span id="cb14-5">ge_dat[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SOX10_chronos"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> chronos_dat[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SOX10"</span>]</span>
<span id="cb14-6"></span>
<span id="cb14-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Remove rows where the target (SOX10_chronos) is NaN, as we can't train/test on them</span></span>
<span id="cb14-8">ge_dat_clean <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ge_dat.dropna(subset<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SOX10_chronos"</span>])</span>
<span id="cb14-9"></span>
<span id="cb14-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Separate features (X) and target (y)</span></span>
<span id="cb14-11">X <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ge_dat_clean.drop(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SOX10_chronos"</span>, axis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb14-12">y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ge_dat_clean[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SOX10_chronos"</span>]</span>
<span id="cb14-13"></span>
<span id="cb14-14"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Data shape after removing missing targets: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>X<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>shape<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>Data shape after removing missing targets: (1150, 19153)</code></pre>
</div>
</div>
<p>We will handle missing values in the features (Gene Expression) using KNN Imputation within our modeling pipeline.</p>
</section>
<section id="model-training" class="level2">
<h2 class="anchored" data-anchor-id="model-training">Model Training</h2>
<p>We will use a <strong>Random Forest Regressor</strong> to predict the dependency score.</p>
<section id="data-splitting" class="level3">
<h3 class="anchored" data-anchor-id="data-splitting">Data Splitting</h3>
<p>First, we split the data into training and testing sets (80% train, 20% test).</p>
<div id="train-test-split" class="cell" data-execution_count="10">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb16-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> sklearn.model_selection <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> train_test_split</span>
<span id="cb16-2"></span>
<span id="cb16-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Split data into training and testing set</span></span>
<span id="cb16-4">X_train, X_test, y_train, y_test <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> train_test_split(X, y, test_size<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>, random_state<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">42</span>)</span>
<span id="cb16-5"></span>
<span id="cb16-6"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Training samples: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>X_train<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>shape[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb16-7"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Testing samples: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>X_test<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>shape[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>Training samples: 920
Testing samples: 230</code></pre>
</div>
</div>
</section>
<section id="pipeline-and-hyperparameter-tuning" class="level3">
<h3 class="anchored" data-anchor-id="pipeline-and-hyperparameter-tuning">Pipeline and Hyperparameter Tuning</h3>
<p>We create a pipeline that: 1. Imputes missing values using <code>KNNImputer</code>. 2. Scales features using <code>StandardScaler</code> (optional for RF but good practice). 3. Trains a <code>RandomForestRegressor</code>.</p>
<p>We will use <code>RandomizedSearchCV</code> to find the best hyperparameters (e.g., <code>max_features</code> for the Random Forest).</p>
<div id="model-training" class="cell" data-message="false" data-execution_count="11">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb18-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> sklearn.ensemble <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> RandomForestRegressor</span>
<span id="cb18-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> sklearn.model_selection <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> RandomizedSearchCV</span>
<span id="cb18-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> scipy.stats <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> randint</span>
<span id="cb18-4"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> sklearn.pipeline <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> Pipeline</span>
<span id="cb18-5"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> sklearn.preprocessing <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> StandardScaler</span>
<span id="cb18-6"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> sklearn.impute <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> KNNImputer</span>
<span id="cb18-7"></span>
<span id="cb18-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create a pipeline</span></span>
<span id="cb18-9">pipeline <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Pipeline([</span>
<span id="cb18-10">    (<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"imputer"</span>, KNNImputer(n_neighbors<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)),</span>
<span id="cb18-11">    (<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"scaler"</span>, StandardScaler()),</span>
<span id="cb18-12">    (<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rf"</span>, RandomForestRegressor(random_state<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">42</span>))</span>
<span id="cb18-13">])</span>
<span id="cb18-14"></span>
<span id="cb18-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Define hyperparameter search space</span></span>
<span id="cb18-16">param_dist <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {</span>
<span id="cb18-17">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'rf__max_features'</span>: randint(low<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, high<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5000</span>), <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Number of features to consider at each split</span></span>
<span id="cb18-18">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'rf__n_estimators'</span>: randint(low<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>, high<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Number of trees in the forest</span></span>
<span id="cb18-19">}</span>
<span id="cb18-20"></span>
<span id="cb18-21"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Initialize RandomizedSearchCV</span></span>
<span id="cb18-22"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># n_iter=5 to keep run time reasonable for this demo</span></span>
<span id="cb18-23">rnd_search <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> RandomizedSearchCV(</span>
<span id="cb18-24">    pipeline, </span>
<span id="cb18-25">    param_distributions<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>param_dist, </span>
<span id="cb18-26">    n_iter<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Number of parameter settings that are sampled</span></span>
<span id="cb18-27">    cv<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Number of folds in cross-validation</span></span>
<span id="cb18-28">    random_state<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">42</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Random state for reproducibility</span></span>
<span id="cb18-29">    scoring<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"neg_root_mean_squared_error"</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Scoring metric</span></span>
<span id="cb18-30">    n_jobs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Use all available CPU cores</span></span>
<span id="cb18-31">)</span>
<span id="cb18-32"></span>
<span id="cb18-33"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Fit the model</span></span>
<span id="cb18-34"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Training Random Forest model..."</span>)</span>
<span id="cb18-35">rnd_search.fit(X_train, y_train)</span>
<span id="cb18-36"></span>
<span id="cb18-37"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Best RMSE: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>rnd_search<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>best_score_<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.4f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb18-38"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Best Parameters: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>rnd_search<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>best_params_<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb18-39"></span>
<span id="cb18-40"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Get the best model</span></span>
<span id="cb18-41">final_model <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rnd_search.best_estimator_</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>Training Random Forest model...</code></pre>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>Best RMSE: 0.2097
Best Parameters: {'rf__max_features': 3019, 'rf__n_estimators': 180}</code></pre>
</div>
</div>
</section>
</section>
<section id="model-evaluation" class="level2">
<h2 class="anchored" data-anchor-id="model-evaluation">Model Evaluation</h2>
<p>Now we evaluate the model’s performance on the unseen test set and investigate which genes are most important for predicting <em>SOX10</em> dependency.</p>
<section id="feature-importance" class="level3">
<h3 class="anchored" data-anchor-id="feature-importance">Feature Importance</h3>
<p>Which genes’ expression levels are most predictive of <em>SOX10</em> dependency?</p>
<div id="cell-feature-importance" class="cell" data-execution_count="12">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb21" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb21-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Extract feature importances</span></span>
<span id="cb21-2">rf_model <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> final_model.named_steps[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'rf'</span>]</span>
<span id="cb21-3">importances <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rf_model.feature_importances_</span>
<span id="cb21-4">feature_names <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> X.columns</span>
<span id="cb21-5"></span>
<span id="cb21-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create a dataframe for visualization</span></span>
<span id="cb21-7">feat_importances <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.Series(importances, index<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>feature_names)</span>
<span id="cb21-8">top_features <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> feat_importances.nlargest(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>)</span>
<span id="cb21-9"></span>
<span id="cb21-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Plot top 20 features</span></span>
<span id="cb21-11">plt.figure(figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>))</span>
<span id="cb21-12">top_features.plot(kind<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'bar'</span>)</span>
<span id="cb21-13">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Top 20 Feature Importances"</span>)</span>
<span id="cb21-14">plt.ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Importance"</span>)</span>
<span id="cb21-15">plt.xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Gene"</span>)</span>
<span id="cb21-16">plt.xticks(rotation<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">45</span>, ha<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'right'</span>)</span>
<span id="cb21-17">plt.show()</span></code></pre></div></div>
<div class="cell-output cell-output-display">
<div id="feature-importance" class="quarto-figure quarto-figure-center anchored">
<figure class="figure">
<p><img src="https://jaychung10010.quarto.pub/all-things-bioinformatics/posts/2025-12-12_ml_with_scikitlearn/index_files/figure-html/feature-importance-output-1.png" width="821" height="563" class="figure-img"></p>
<figcaption>Top 20 most important features (genes) for predicting SOX10 dependency.</figcaption>
</figure>
</div>
</div>
</div>
<p>As expected, SOX10 gene expression is the most important feature for predicting <em>SOX10</em> dependency.</p>
</section>
<section id="prediction-performance" class="level3">
<h3 class="anchored" data-anchor-id="prediction-performance">Prediction Performance</h3>
<p>We calculate the Root Mean Squared Error (RMSE) on the test set and visualize the predictions vs actual values.</p>
<div id="cell-model-evaluation" class="cell" data-execution_count="13">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb22" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb22-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> sklearn.metrics <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> mean_squared_error</span>
<span id="cb22-2"></span>
<span id="cb22-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Predict on test set</span></span>
<span id="cb22-4">test_predictions <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> final_model.predict(X_test)</span>
<span id="cb22-5"></span>
<span id="cb22-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calculate RMSE</span></span>
<span id="cb22-7">test_mse <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> mean_squared_error(y_test, test_predictions)</span>
<span id="cb22-8">test_rmse <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.sqrt(test_mse)</span>
<span id="cb22-9"></span>
<span id="cb22-10"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Test Set RMSE: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>test_rmse<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.4f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb22-11"></span>
<span id="cb22-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Plot Predicted vs Expected</span></span>
<span id="cb22-13">plt.figure(figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>))</span>
<span id="cb22-14">plt.scatter(y_test, test_predictions, alpha<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.6</span>)</span>
<span id="cb22-15">plt.plot([y.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">min</span>(), y.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">max</span>()], [y.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">min</span>(), y.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">max</span>()], <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'k--'</span>, lw<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Identity line</span></span>
<span id="cb22-16">plt.xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Expected Score (True)"</span>)</span>
<span id="cb22-17">plt.ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Predicted Score"</span>)</span>
<span id="cb22-18">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Test Set: Predicted vs Expected"</span>)</span>
<span id="cb22-19">plt.grid(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>, linestyle<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'--'</span>, alpha<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>)</span>
<span id="cb22-20">plt.show()</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>Test Set RMSE: 0.2139</code></pre>
</div>
<div class="cell-output cell-output-display">
<div id="model-evaluation" class="quarto-figure quarto-figure-center anchored">
<figure class="figure">
<p><img src="https://jaychung10010.quarto.pub/all-things-bioinformatics/posts/2025-12-12_ml_with_scikitlearn/index_files/figure-html/model-evaluation-output-2.png" width="675" height="523" class="figure-img"></p>
<figcaption>Predicted vs Expected SOX10 Chronos Scores on Test Set.</figcaption>
</figure>
</div>
</div>
</div>
<p>We can see that the model predicts a more bimodal distribution of scores than the actual data, although it does somewhat differentiate between high and low dependency populations. To improve the model, we could try to tune the hyperparameters further, or try different feature selection methods.</p>
</section>
</section>
</section>
<section id="references" class="level1">
<h1>References</h1>
<ul>
<li><a href="https://scikit-learn.org/stable/index.html">Scikit-Learn Documentation</a></li>
<li><a href="https://pandas.pydata.org/pandas-docs/stable/index.html">Pandas Documentation</a></li>
<li><a href="https://matplotlib.org/stable/index.html">Matplotlib Documentation</a></li>
<li><a href="https://depmap.org/portal/">DepMap Portal</a></li>
<li><a href="https://www.oreilly.com/library/view/hands-on-machine-learning/9781098125967/">Hands-on Machine Learning with Scikit-Learn, Keras, and TensorFlow (3rd Edition)</a></li>
</ul>


</section>

 ]]></description>
  <category>Python</category>
  <category>Machine Learning</category>
  <category>DepMap</category>
  <category>Random Forest</category>
  <category>Scikit-Learn</category>
  <category>Pandas</category>
  <guid>https://jaychung10010.quarto.pub/all-things-bioinformatics/posts/2025-12-12_ml_with_scikitlearn/</guid>
  <pubDate>Mon, 15 Dec 2025 08:00:00 GMT</pubDate>
</item>
<item>
  <title>Machine learning for drug sensitivity prediction (Part 2): will deep features improve accuracy?</title>
  <dc:creator>Jay Chung</dc:creator>
  <link>https://jaychung10010.quarto.pub/all-things-bioinformatics/posts/2025-12-03_ae_ml_validation/</link>
  <description><![CDATA[ 





<p>In <a href="../../posts/2025-11-29_depmap-multiomics-autoencoder/index.html">Part 1</a>, I explored using autoencoders (AE) to compress high-dimensional DepMap omics data into deep features. I visualized these features using UMAP and observed that they preserved biological signals related to cell lineage.</p>
<p>In this post (Part 2), I will evaluate whether these deep features actually improve drug sensitivity prediction accuracy compared to using either the full RNA expression data or a feature-selected subset of RNA expression data. The motivation is that deep features may capture complex, non-linear relationships in the data while reducing dimensionality, potentially enhancing model performance. We will focus on two drugs with known biomarkers but with different mechanisms of action: <strong>Erlotinib</strong> (an EGFR inhibitor) and <strong>JQ1</strong> (a BET inhibitor).</p>
<section id="setup-and-libraries" class="level2">
<h2 class="anchored" data-anchor-id="setup-and-libraries">Setup and Libraries</h2>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(data.table)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggpubr)</span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(caret)</span>
<span id="cb1-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(doParallel)</span></code></pre></div></div>
</div>
</section>
<section id="methods-machine-learning-models" class="level2">
<h2 class="anchored" data-anchor-id="methods-machine-learning-models">Methods: Machine Learning Models</h2>
<p>We will use two machine learning algorithms to predict drug sensitivity (log2 fold change) from the DepMap PRISM drug screen data:</p>
<ol type="1">
<li><strong>Random Forest (RF)</strong>: A tree-based ensemble method that can capture non-linear relationships.<br>
</li>
<li><strong>Elastic Net</strong>: A linear regression method with <img src="https://latex.codecogs.com/png.latex?l1"> and <img src="https://latex.codecogs.com/png.latex?l2"> regularization to prevent overfitting.</li>
</ol>
<p>We are using the R Caret package for model training and hyperparameter tuning. Caret is a versatile package that provides a unified interface for training various machine learning models with built-in cross-validation and hyperparameter tuning. Documentation can be found here: <a href="https://topepo.github.io/caret/" class="uri">https://topepo.github.io/caret/</a>.</p>
<p>We will evaluate performance using <strong>R-squared (<img src="https://latex.codecogs.com/png.latex?R%5E2">)</strong> on six independent test-train sets across multiple random splits.</p>
<section id="prediction-functions" class="level3">
<h3 class="anchored" data-anchor-id="prediction-functions">Prediction Functions</h3>
<p>We define helper functions to perform repeated train-test splits and model training.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Random Forest Prediction Function</span></span>
<span id="cb2-2">rf_drug_prediction <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(feature_dat, drug_response, output_file_prefix, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n_repeats =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>) {</span>
<span id="cb2-3">    r2_values <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>()</span>
<span id="cb2-4">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>n_repeats) {</span>
<span id="cb2-5">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">524</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> i)</span>
<span id="cb2-6">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 80-20 Train-Test Split</span></span>
<span id="cb2-7">        train_idx <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sample</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(feature_dat), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.8</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(feature_dat)), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">replace =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb2-8">        test_idx <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">setdiff</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(feature_dat), train_idx)</span>
<span id="cb2-9"></span>
<span id="cb2-10">        train_x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> feature_dat[train_idx, ]</span>
<span id="cb2-11">        train_y <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> drug_response[train_idx]</span>
<span id="cb2-12">        test_x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> feature_dat[test_idx, ]</span>
<span id="cb2-13">        test_y <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> drug_response[test_idx]</span>
<span id="cb2-14"></span>
<span id="cb2-15">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Train Random Forest with caret</span></span>
<span id="cb2-16">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># caret will perform tuning for all available hyperparameters, and automatically pick the best model (highest R2 on cross-validation)</span></span>
<span id="cb2-17">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">524</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> i)</span>
<span id="cb2-18">        rfTune <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">train</span>(train_x, train_y,</span>
<span id="cb2-19">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rf"</span>,</span>
<span id="cb2-20">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">importance =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>,</span>
<span id="cb2-21">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ntree =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>,</span>
<span id="cb2-22">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">preProcess =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"center"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"scale"</span>),</span>
<span id="cb2-23">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">metric =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Rsquared"</span>,</span>
<span id="cb2-24">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">tuneLength =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># tune over 10 different mtry values</span></span>
<span id="cb2-25">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">trControl =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">trainControl</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"repeatedcv"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">number =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">repeats =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 10-fold cross-validation repeated 3 times</span></span>
<span id="cb2-26">        )</span>
<span id="cb2-27"></span>
<span id="cb2-28">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Predict on Test Set</span></span>
<span id="cb2-29">        rf_pred <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predict</span>(rfTune, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">newdata =</span> test_x)</span>
<span id="cb2-30">        r2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cor</span>(rf_pred, test_y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pearson"</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span></span>
<span id="cb2-31">        r2_values <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(r2_values, r2)</span>
<span id="cb2-32">    }</span>
<span id="cb2-33"></span>
<span id="cb2-34">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Return R2 values</span></span>
<span id="cb2-35">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Repeat =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>n_repeats, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">R2 =</span> r2_values))</span>
<span id="cb2-36">}</span>
<span id="cb2-37"></span>
<span id="cb2-38"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Elastic Net Prediction Function</span></span>
<span id="cb2-39">enet_drug_prediction <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(feature_dat, drug_response, output_file_prefix, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n_repeats =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>) {</span>
<span id="cb2-40">    r2_values <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>()</span>
<span id="cb2-41">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>n_repeats) {</span>
<span id="cb2-42">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">524</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> i)</span>
<span id="cb2-43">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 80-20 Train-Test Split</span></span>
<span id="cb2-44">        train_idx <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sample</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(feature_dat), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.8</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(feature_dat)), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">replace =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb2-45">        test_idx <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">setdiff</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(feature_dat), train_idx)</span>
<span id="cb2-46"></span>
<span id="cb2-47">        train_x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> feature_dat[train_idx, ]</span>
<span id="cb2-48">        train_y <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> drug_response[train_idx]</span>
<span id="cb2-49">        test_x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> feature_dat[test_idx, ]</span>
<span id="cb2-50">        test_y <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> drug_response[test_idx]</span>
<span id="cb2-51"></span>
<span id="cb2-52">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Train Elastic Net</span></span>
<span id="cb2-53">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">524</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> i)</span>
<span id="cb2-54">        enetTune <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">train</span>(train_x, train_y,</span>
<span id="cb2-55">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"glmnet"</span>,</span>
<span id="cb2-56">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">preProcess =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"center"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"scale"</span>),</span>
<span id="cb2-57">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">metric =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Rsquared"</span>,</span>
<span id="cb2-58">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">tuneLength =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># tune over 10 X 10 different alpha/lambda combinations</span></span>
<span id="cb2-59">            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># alpha: mixing parameter (0 = ridge, 1 = lasso)</span></span>
<span id="cb2-60">            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># lambda: regularization strength</span></span>
<span id="cb2-61">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">trControl =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">trainControl</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"repeatedcv"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">number =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">repeats =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 10-fold cross-validation repeated 3 times</span></span>
<span id="cb2-62">        )</span>
<span id="cb2-63"></span>
<span id="cb2-64">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Predict on Test Set</span></span>
<span id="cb2-65">        enet_pred <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predict</span>(enetTune, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">newdata =</span> test_x)</span>
<span id="cb2-66">        r2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cor</span>(enet_pred, test_y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pearson"</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span></span>
<span id="cb2-67">        r2_values <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(r2_values, r2)</span>
<span id="cb2-68">    }</span>
<span id="cb2-69"></span>
<span id="cb2-70">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Return R2 values</span></span>
<span id="cb2-71">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Repeat =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>n_repeats, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">R2 =</span> r2_values))</span>
<span id="cb2-72">}</span></code></pre></div></div>
</div>
</section>
</section>
<section id="analysis-1-erlotinib-prediction" class="level2">
<h2 class="anchored" data-anchor-id="analysis-1-erlotinib-prediction">Analysis 1: Erlotinib Prediction</h2>
<p>Erlotinib is an EGFR inhibitor used in cancer treatment, which is a type of targeted cancer therapy used primarily to treat certain types of non-small cell lung cancer and pancreatic cancer.</p>
<section id="data-preparation" class="level3">
<h3 class="anchored" data-anchor-id="data-preparation">Data Preparation</h3>
<p>We load the data and prepare four feature sets:</p>
<ol type="1">
<li><strong>Full RNA</strong>: All high-variance genes.<br>
</li>
<li><strong>RNA AE</strong>: Deep features from RNA autoencoder.<br>
</li>
<li><strong>Multi-omics AE</strong>: Deep features from multi-omics autoencoder (including RNA, mutation, and CRISPR).<br>
</li>
<li><strong>Selected Top RNA</strong>: Top ~4000 gene expressions most correlated with Erlotinib response (a common feature selection strategy).</li>
</ol>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Load Data</span></span>
<span id="cb3-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">load</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CCLE_24Q2_GE_match_sample_info.RData"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># RNA expression in log2(TPM+1)</span></span>
<span id="cb3-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">load</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"PRISM_24Q2_compound_screen_match_sample_info.RData"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># PRISM drug response data in log2 fold change (LFC)</span></span>
<span id="cb3-4">cmpd_dat <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read_csv</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Repurposing_Public_24Q2_Extended_Primary_Compound_List.csv"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Compound metadata</span></span>
<span id="cb3-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">load</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sample_info_match_biomarkers.RData"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Sample info with DepMap IDs</span></span>
<span id="cb3-6"></span>
<span id="cb3-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ... (Data filtering and matching code similar to Part 1) ...</span></span>
<span id="cb3-8"></span>
<span id="cb3-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Get Erlotinib Response</span></span>
<span id="cb3-10">cmpd_id <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> cmpd_dat <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-11">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(Drug.Name <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ERLOTINIB"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-12">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pull</span>(IDs)</span>
<span id="cb3-13">drug_LFC <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> prism_dat_match_sam <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pull</span>(cmpd_id)</span></code></pre></div></div>
</div>
<p>Let’s take a look at the PRISM drug screen data. Rows are cell lines and columns are compounds. Values are log2 fold change (LFC) in viability after drug treatment. You can see that there are a lot of NAs here, as many cell lines were not screened with all compounds.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">load</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"PRISM_24Q2_compound_screen_match_sample_info.RData"</span>)</span>
<span id="cb4-2">prism_dat_match_sam[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> gt<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gt</span>()</span></code></pre></div></div>
<div class="cell-output-display">
<div id="biainsuzjn" style="padding-left:0px;padding-right:0px;padding-top:10px;padding-bottom:10px;overflow-x:auto;overflow-y:auto;width:auto;height:auto;">
<style>#biainsuzjn table {
  font-family: system-ui, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

#biainsuzjn thead, #biainsuzjn tbody, #biainsuzjn tfoot, #biainsuzjn tr, #biainsuzjn td, #biainsuzjn th {
  border-style: none;
}

#biainsuzjn p {
  margin: 0;
  padding: 0;
}

#biainsuzjn .gt_table {
  display: table;
  border-collapse: collapse;
  line-height: normal;
  margin-left: auto;
  margin-right: auto;
  color: #333333;
  font-size: 16px;
  font-weight: normal;
  font-style: normal;
  background-color: #FFFFFF;
  width: auto;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #A8A8A8;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #A8A8A8;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
}

#biainsuzjn .gt_caption {
  padding-top: 4px;
  padding-bottom: 4px;
}

#biainsuzjn .gt_title {
  color: #333333;
  font-size: 125%;
  font-weight: initial;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-color: #FFFFFF;
  border-bottom-width: 0;
}

#biainsuzjn .gt_subtitle {
  color: #333333;
  font-size: 85%;
  font-weight: initial;
  padding-top: 3px;
  padding-bottom: 5px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-color: #FFFFFF;
  border-top-width: 0;
}

#biainsuzjn .gt_heading {
  background-color: #FFFFFF;
  text-align: center;
  border-bottom-color: #FFFFFF;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#biainsuzjn .gt_bottom_border {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#biainsuzjn .gt_col_headings {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#biainsuzjn .gt_col_heading {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 6px;
  padding-left: 5px;
  padding-right: 5px;
  overflow-x: hidden;
}

#biainsuzjn .gt_column_spanner_outer {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  padding-top: 0;
  padding-bottom: 0;
  padding-left: 4px;
  padding-right: 4px;
}

#biainsuzjn .gt_column_spanner_outer:first-child {
  padding-left: 0;
}

#biainsuzjn .gt_column_spanner_outer:last-child {
  padding-right: 0;
}

#biainsuzjn .gt_column_spanner {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 5px;
  overflow-x: hidden;
  display: inline-block;
  width: 100%;
}

#biainsuzjn .gt_spanner_row {
  border-bottom-style: hidden;
}

#biainsuzjn .gt_group_heading {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  text-align: left;
}

#biainsuzjn .gt_empty_group_heading {
  padding: 0.5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: middle;
}

#biainsuzjn .gt_from_md > :first-child {
  margin-top: 0;
}

#biainsuzjn .gt_from_md > :last-child {
  margin-bottom: 0;
}

#biainsuzjn .gt_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  margin: 10px;
  border-top-style: solid;
  border-top-width: 1px;
  border-top-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  overflow-x: hidden;
}

#biainsuzjn .gt_stub {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
}

#biainsuzjn .gt_stub_row_group {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
  vertical-align: top;
}

#biainsuzjn .gt_row_group_first td {
  border-top-width: 2px;
}

#biainsuzjn .gt_row_group_first th {
  border-top-width: 2px;
}

#biainsuzjn .gt_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#biainsuzjn .gt_first_summary_row {
  border-top-style: solid;
  border-top-color: #D3D3D3;
}

#biainsuzjn .gt_first_summary_row.thick {
  border-top-width: 2px;
}

#biainsuzjn .gt_last_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#biainsuzjn .gt_grand_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#biainsuzjn .gt_first_grand_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-style: double;
  border-top-width: 6px;
  border-top-color: #D3D3D3;
}

#biainsuzjn .gt_last_grand_summary_row_top {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: double;
  border-bottom-width: 6px;
  border-bottom-color: #D3D3D3;
}

#biainsuzjn .gt_striped {
  background-color: rgba(128, 128, 128, 0.05);
}

#biainsuzjn .gt_table_body {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#biainsuzjn .gt_footnotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#biainsuzjn .gt_footnote {
  margin: 0px;
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#biainsuzjn .gt_sourcenotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#biainsuzjn .gt_sourcenote {
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#biainsuzjn .gt_left {
  text-align: left;
}

#biainsuzjn .gt_center {
  text-align: center;
}

#biainsuzjn .gt_right {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

#biainsuzjn .gt_font_normal {
  font-weight: normal;
}

#biainsuzjn .gt_font_bold {
  font-weight: bold;
}

#biainsuzjn .gt_font_italic {
  font-style: italic;
}

#biainsuzjn .gt_super {
  font-size: 65%;
}

#biainsuzjn .gt_footnote_marks {
  font-size: 75%;
  vertical-align: 0.4em;
  position: initial;
}

#biainsuzjn .gt_asterisk {
  font-size: 100%;
  vertical-align: 0;
}

#biainsuzjn .gt_indent_1 {
  text-indent: 5px;
}

#biainsuzjn .gt_indent_2 {
  text-indent: 10px;
}

#biainsuzjn .gt_indent_3 {
  text-indent: 15px;
}

#biainsuzjn .gt_indent_4 {
  text-indent: 20px;
}

#biainsuzjn .gt_indent_5 {
  text-indent: 25px;
}

#biainsuzjn .katex-display {
  display: inline-flex !important;
  margin-bottom: 0.75em !important;
}

#biainsuzjn div.Reactable > div.rt-table > div.rt-thead > div.rt-tr.rt-tr-group-header > div.rt-th-group:after {
  height: 0px !important;
}
</style>

<table class="gt_table caption-top table table-sm table-striped small" data-quarto-bootstrap="false">
<thead>
<tr class="gt_col_headings header">
<th id="BRD:BRD-A00047421-001-01-7" class="gt_col_heading gt_columns_bottom_border gt_right" data-quarto-table-cell-role="th" scope="col">BRD:BRD-A00047421-001-01-7</th>
<th id="BRD:BRD-A00055058-001-01-0" class="gt_col_heading gt_columns_bottom_border gt_right" data-quarto-table-cell-role="th" scope="col">BRD:BRD-A00055058-001-01-0</th>
<th id="BRD:BRD-A00077618-236-07-6" class="gt_col_heading gt_columns_bottom_border gt_right" data-quarto-table-cell-role="th" scope="col">BRD:BRD-A00077618-236-07-6</th>
<th id="BRD:BRD-A00092689-236-04-9" class="gt_col_heading gt_columns_bottom_border gt_right" data-quarto-table-cell-role="th" scope="col">BRD:BRD-A00092689-236-04-9</th>
<th id="BRD:BRD-A00100033-001-08-9" class="gt_col_heading gt_columns_bottom_border gt_right" data-quarto-table-cell-role="th" scope="col">BRD:BRD-A00100033-001-08-9</th>
</tr>
</thead>
<tbody class="gt_table_body">
<tr class="odd">
<td class="gt_row gt_right" headers="BRD:BRD-A00047421-001-01-7">-1.207281</td>
<td class="gt_row gt_right" headers="BRD:BRD-A00055058-001-01-0">0.5157434</td>
<td class="gt_row gt_right" headers="BRD:BRD-A00077618-236-07-6">-0.01557664</td>
<td class="gt_row gt_right" headers="BRD:BRD-A00092689-236-04-9">-0.39512253</td>
<td class="gt_row gt_right" headers="BRD:BRD-A00100033-001-08-9">-0.4493321</td>
</tr>
<tr class="even">
<td class="gt_row gt_right" headers="BRD:BRD-A00047421-001-01-7">-4.231563</td>
<td class="gt_row gt_right" headers="BRD:BRD-A00055058-001-01-0">NA</td>
<td class="gt_row gt_right" headers="BRD:BRD-A00077618-236-07-6">NA</td>
<td class="gt_row gt_right" headers="BRD:BRD-A00092689-236-04-9">-0.53837559</td>
<td class="gt_row gt_right" headers="BRD:BRD-A00100033-001-08-9">NA</td>
</tr>
<tr class="odd">
<td class="gt_row gt_right" headers="BRD:BRD-A00047421-001-01-7">NA</td>
<td class="gt_row gt_right" headers="BRD:BRD-A00055058-001-01-0">NA</td>
<td class="gt_row gt_right" headers="BRD:BRD-A00077618-236-07-6">NA</td>
<td class="gt_row gt_right" headers="BRD:BRD-A00092689-236-04-9">NA</td>
<td class="gt_row gt_right" headers="BRD:BRD-A00100033-001-08-9">NA</td>
</tr>
<tr class="even">
<td class="gt_row gt_right" headers="BRD:BRD-A00047421-001-01-7">-3.860672</td>
<td class="gt_row gt_right" headers="BRD:BRD-A00055058-001-01-0">NA</td>
<td class="gt_row gt_right" headers="BRD:BRD-A00077618-236-07-6">NA</td>
<td class="gt_row gt_right" headers="BRD:BRD-A00092689-236-04-9">0.30697134</td>
<td class="gt_row gt_right" headers="BRD:BRD-A00100033-001-08-9">NA</td>
</tr>
<tr class="odd">
<td class="gt_row gt_right" headers="BRD:BRD-A00047421-001-01-7">-2.271411</td>
<td class="gt_row gt_right" headers="BRD:BRD-A00055058-001-01-0">NA</td>
<td class="gt_row gt_right" headers="BRD:BRD-A00077618-236-07-6">NA</td>
<td class="gt_row gt_right" headers="BRD:BRD-A00092689-236-04-9">0.03509603</td>
<td class="gt_row gt_right" headers="BRD:BRD-A00100033-001-08-9">NA</td>
</tr>
</tbody>
</table>

</div>
</div>
</div>
<p>For brevity, we skip the data cleaning steps here. After filtering for missing data and matching cell lines across datasets, we obtain the final datasets for modeling. We also skip the codes for selecting the top RNA genes correlated with Erlotinib response - it is a simple apply function calculating Pearson correlation for each gene across the transcriptome, and selecting the top genes.</p>
</section>
<section id="model-training-erlotinib" class="level3">
<h3 class="anchored" data-anchor-id="model-training-erlotinib">Model Training (Erlotinib)</h3>
<p>We train models on all four feature sets.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Parallel Processing</span></span>
<span id="cb5-2">cl <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">makePSOCKcluster</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">22</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Adjust number of cores as needed</span></span>
<span id="cb5-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">registerDoParallel</span>(cl)</span>
<span id="cb5-4"></span>
<span id="cb5-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 1. Full RNA</span></span>
<span id="cb5-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rf_drug_prediction</span>(ge_dat_final, drug_LFC_final, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"full_RNA"</span>)</span>
<span id="cb5-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">enet_drug_prediction</span>(ge_dat_final, drug_LFC_final, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"full_RNA"</span>)</span>
<span id="cb5-8"></span>
<span id="cb5-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 2. RNA AE Features</span></span>
<span id="cb5-10"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rf_drug_prediction</span>(RNA_AE_features_final, drug_LFC_final, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RNA_AE"</span>)</span>
<span id="cb5-11"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">enet_drug_prediction</span>(RNA_AE_features_final, drug_LFC_final, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RNA_AE"</span>)</span>
<span id="cb5-12"></span>
<span id="cb5-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 3. Multi-omics AE Features</span></span>
<span id="cb5-14"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rf_drug_prediction</span>(MultiOmics_AE_features_final, drug_LFC_final, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"MultiOmics_AE"</span>)</span>
<span id="cb5-15"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">enet_drug_prediction</span>(MultiOmics_AE_features_final, drug_LFC_final, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"MultiOmics_AE"</span>)</span>
<span id="cb5-16"></span>
<span id="cb5-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 4. Selected Top RNA</span></span>
<span id="cb5-18"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rf_drug_prediction</span>(ge_dat_selected, drug_LFC_final, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"selected_top_RNA"</span>)</span>
<span id="cb5-19"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">enet_drug_prediction</span>(ge_dat_selected, drug_LFC_final, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"selected_top_RNA"</span>)</span>
<span id="cb5-20"></span>
<span id="cb5-21"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stopCluster</span>(cl)</span></code></pre></div></div>
</div>
</section>
<section id="results-erlotinib" class="level3">
<h3 class="anchored" data-anchor-id="results-erlotinib">Results: Erlotinib</h3>
<p>Let’s look at the performance (<img src="https://latex.codecogs.com/png.latex?R%5E2">) of the different models.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Load results (code omitted for brevity, loading CSVs from results folder)</span></span>
<span id="cb6-2"></span>
<span id="cb6-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Plot Random Forest Results</span></span>
<span id="cb6-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(rf_r2_df, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> Model, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> R2, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> Model, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> Model)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-5">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_boxplot</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">outliers =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-6">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_jitter</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-7">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_classic</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base_size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-8">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Random Forest Erlotinib LFC Prediction R2"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Model"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"R2"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-9">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span>)</span>
<span id="cb6-10"></span>
<span id="cb6-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Plot Elastic Net Results</span></span>
<span id="cb6-12"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(enet_r2_df, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> Model, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> R2, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> Model, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> Model)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-13">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_boxplot</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">outliers =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-14">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_jitter</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-15">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_classic</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base_size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-16">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Elastic Net Erlotinib LFC Prediction R2"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Model"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"R2"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-17">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span>)</span></code></pre></div></div>
</div>
<div class="cell" data-layout-align="center">
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jaychung10010.quarto.pub/all-things-bioinformatics/posts/2025-12-03_ae_ml_validation/rf_erlotinib_prediction_r2_summary.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:70.0%"></p>
</figure>
</div>
</div>
</div>
<div class="cell" data-layout-align="center">
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jaychung10010.quarto.pub/all-things-bioinformatics/posts/2025-12-03_ae_ml_validation/enet_erlotinib_prediction_r2_summary.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:70.0%"></p>
</figure>
</div>
</div>
</div>
<p>This result is a bit surprising! In both models, the selected top RNA genes outperform all other feature sets. The full RNA set ranks second, while the AE features perform worse. The multi-omics AE features do not seem to add value in this case, and it performs even worse than the RNA AE features alone. I’ll speculate why this might be the case in the summary section. Is this specific to Erlotinib, or a general trend? Let’s check with another drug.</p>
</section>
</section>
<section id="analysis-2-jq1-prediction" class="level2">
<h2 class="anchored" data-anchor-id="analysis-2-jq1-prediction">Analysis 2: JQ1 Prediction</h2>
<p>Now let’s try to predict sensitivity to JQ1, a BET inhibitor that has a very different mechanism of action compared to Erlotinib.</p>
<section id="model-training-jq1" class="level3">
<h3 class="anchored" data-anchor-id="model-training-jq1">Model Training (JQ1)</h3>
<p>We repeat the same process for JQ1.</p>
</section>
<section id="results-jq1" class="level3">
<h3 class="anchored" data-anchor-id="results-jq1">Results: JQ1</h3>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ... (Similar plotting code as Erlotinib) ...</span></span></code></pre></div></div>
</div>
<div class="cell" data-layout-align="center">
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jaychung10010.quarto.pub/all-things-bioinformatics/posts/2025-12-03_ae_ml_validation/rf_JQ1_prediction_r2_summary.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:70.0%"></p>
</figure>
</div>
</div>
</div>
<div class="cell" data-layout-align="center">
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jaychung10010.quarto.pub/all-things-bioinformatics/posts/2025-12-03_ae_ml_validation/enet_JQ1_prediction_r2_summary.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:70.0%"></p>
</figure>
</div>
</div>
</div>
<p>So the conclusion is pretty much the same for JQ1 as well. The selected top RNA genes outperform all other feature sets, followed by the full RNA set. The AE features again perform worse, and adding multi-omics data does not help.</p>
</section>
</section>
<section id="summary-and-conclusions" class="level2">
<h2 class="anchored" data-anchor-id="summary-and-conclusions">Summary and Conclusions</h2>
<p>Contrary to my initial hypothesis, the deep features extracted from autoencoders did not improve drug sensitivity prediction accuracy for either Erlotinib or JQ1. Instead, using a feature-selected subset of RNA expression data yielded the best performance, followed by using the full RNA expression data. The AE features, both RNA-only and multi-omics, underperformed in comparison.</p>
<p>So why is this the case? Here are some possible explanations:</p>
<ol type="1">
<li><p><strong>Information Loss</strong>: The AE compression may have discarded important predictive information present in the original RNA expression data. Rather than preserving features relevant for drug response, the AE might have prioritized reconstructing general patterns, which in this case is likely the lineage programs between different cell lines. A possible remedy could be to use supervised or semi-supervised autoencoders that incorporate drug response information during training. I also have not experimented with different AE architectures, latent dimensions, or training strategies that might better capture drug response signals.</p></li>
<li><p><strong>Multi-omics Integration Challenges</strong>: Combining multiple omics data types (RNA, mutation, CRISPR) into a single AE may introduce noise or conflicting signals that obscure relevant features for drug response. Each omics type has different sparsity and scales, making it difficult for a single AE to effectively learn a unified representation. More sophisticated integration methods or separate AEs for each omics type followed by feature fusion might yield better results.</p></li>
<li><p><strong>Drug-Specific Biology</strong>: Assuming that the AE mainly captures broad biological variation (e.g., lineage), it may not align well with the specific molecular mechanisms driving sensitivity to Erlotinib and JQ1. For example, one of the key biomarkers for Erlotinib sensitivity - EGFR hotspot mutations - are actually quite rare across the cell lines (I’ve counted around 10), and may not be well represented in the AE features. I also did not explore including other omics data types (e.g., proteomics, methylation) that might be relevant for these drugs. Finally, only two drugs were tested here; results may vary for other drugs with different mechanisms of action.</p></li>
<li><p><strong>Limitation of ML Models</strong>: The Random Forest and Elastic Net models used here may not fully leverage the complex representations learned by the AEs. More advanced models (e.g., deep neural networks) that can capture non-linear relationships in the AE feature space might yield different results. However, one significant drawback of deep learning is that it generally requires much larger training datasets to avoid overfitting, which may not be feasible with the limited number of cell lines available in a regular drug screen.</p></li>
</ol>
<p>Interestingly, between the 2 models, the Elastic Net seemed to slightly outperform Random Forest in most cases. Given that the training time for Elastic Net is significantly shorter than Random Forest, this suggests that for these specific drug response predictions, simpler linear models with regularization may be more effective than more complex non-linear models.</p>
<p>So there you have it! In the next and final part of this series, I will revisit the concept I introduced in my first post: enhancing statistical power of drug biomarker detection by ML. I will use the best performed model (selected top RNA with Elastic Net) to expand drug prediction to all DepMap available cell lines (e.g., there are only ~500 cell lines screened for Erlotinib, but DepMap has close to 2000 cell lines), and see if we can better recover known sensitivity biomarkers.</p>
</section>
<section id="references" class="level2">
<h2 class="anchored" data-anchor-id="references">References</h2>
<ul>
<li>DepMap Portal: <a href="https://depmap.org/portal/" class="uri">https://depmap.org/portal/</a></li>
<li>Caret Package Documentation: <a href="https://topepo.github.io/caret/" class="uri">https://topepo.github.io/caret/</a></li>
<li>Applied Predictive Modeling by Kuhn and Johnson: <a href="https://www.springer.com/gp/book/9781461468486" class="uri">https://www.springer.com/gp/book/9781461468486</a></li>
<li>An Introduction to Statistical Learning by James et al.: <a href="https://www.statlearning.com/" class="uri">https://www.statlearning.com/</a></li>
</ul>


</section>

 ]]></description>
  <category>R</category>
  <category>DepMap</category>
  <category>Autoencoder</category>
  <category>Multi-omics</category>
  <category>Machine Learning</category>
  <category>Random Forest</category>
  <category>Elastic Net</category>
  <category>Caret</category>
  <guid>https://jaychung10010.quarto.pub/all-things-bioinformatics/posts/2025-12-03_ae_ml_validation/</guid>
  <pubDate>Mon, 08 Dec 2025 08:00:00 GMT</pubDate>
</item>
<item>
  <title>Machine learning for drug sensitivity prediction (Part 1): using autoencoders to extract deep features from DepMap OMICS</title>
  <dc:creator>Jay Chung</dc:creator>
  <link>https://jaychung10010.quarto.pub/all-things-bioinformatics/posts/2025-11-29_depmap-multiomics-autoencoder/</link>
  <description><![CDATA[ 





<p>During my time in the industry, I have analyzed quite a few cell panel screens aiming to identify biomarkers of drug sensitivity. These screens are often carried out by CROs, and due to the costs associated with these experiments, the number of cell lines screened is often limited. Very often the average number of cell lines in each lineage is less than 10, which limits the power of biomarker detection. One way to mitigate this issue is to train a machine learning model based on the available cell lines and their corresponding DepMap omics data, and then use the trained model to predict sensitivity across thousands of DepMap cell lines. This expanded set of predicted sensitivities can then be used for biomarker detection with improved power.</p>
<p>In this post, I want to explore whether using autoencoders (AE) to compress high-dimensional omics data into deep features can improve drug sensitivity prediction accuracy. The concept can be summarized in the figure below:</p>
<div class="cell" data-layout-align="center">
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jaychung10010.quarto.pub/all-things-bioinformatics/posts/2025-11-29_depmap-multiomics-autoencoder/concept1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:80.0%"></p>
</figure>
</div>
</div>
</div>
<p>I demonstrate how to use AE to compress DepMap RNA expression data alone and multi-omics data (RNA expression + mutation + CRISPR dependency scores) into deep features. I then visualize and compare the embeddings from RNA alone vs.&nbsp;multi-omics data. In future work (part 2), I will compare the drug sensitivity prediction accuracy using deep features vs.&nbsp;full omics data.</p>
<p>These concepts can be summarized in Component 1 (AE embedding) and 2 (ML training) in the following figures:</p>
<p>Component 1: Autoencoder embedding</p>
<div class="cell" data-layout-align="center">
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jaychung10010.quarto.pub/all-things-bioinformatics/posts/2025-11-29_depmap-multiomics-autoencoder/Autoencoder_workflow.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:80.0%"></p>
</figure>
</div>
</div>
</div>
<p>Component 2: ML training and prediction</p>
<div class="cell" data-layout-align="center">
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jaychung10010.quarto.pub/all-things-bioinformatics/posts/2025-11-29_depmap-multiomics-autoencoder/ML_workflow.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:80.0%"></p>
</figure>
</div>
</div>
</div>
<section id="introduction" class="level2">
<h2 class="anchored" data-anchor-id="introduction">Introduction</h2>
<p><strong>Background</strong>: Cell panel drug screens are very useful during drug development in identifying sensitive biomarkers and understanding the mechanism-of-action; however, it can be costly and often result in only up to hundreds of cell lines available, limiting the power of biomarker detection. By leveraging machine learning (ML) approaches, one can expand sensitivity prediction to thousands of DepMap cell lines and thus improve the power of biomarker detection.</p>
<p><strong>Problem</strong>: DepMap OMICS data are high-dimensional, and such high-dimensional data may hinder ML prediction due to overfitting problems and noisy data.</p>
<p><strong>Proposal</strong>: By leveraging AutoEncoder (AE), a deep learning approach, high-dimensional OMICS data can be compressed into Deep Features (DF), thus overcoming the problem of overfitting and resulting in better drug sensitivity prediction accuracy. Unlike traditional dimensionality reduction methods (e.g., PCA), AE can capture non-linear relationships in the data, leading to more informative embeddings.</p>
</section>
<section id="setup-and-libraries" class="level2">
<h2 class="anchored" data-anchor-id="setup-and-libraries">Setup and Libraries</h2>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(data.table)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggpubr)</span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(h2o)</span>
<span id="cb1-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(umap)</span>
<span id="cb1-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggrepel)</span>
<span id="cb1-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(microViz)</span></code></pre></div></div>
</div>
</section>
<section id="aim-1-rna-expression-autoencoder" class="level2">
<h2 class="anchored" data-anchor-id="aim-1-rna-expression-autoencoder">Aim 1: RNA Expression Autoencoder</h2>
<p>First, we use an autoencoder to embed cell lines based on RNA expression data alone.</p>
<section id="data-loading-and-preprocessing" class="level3">
<h3 class="anchored" data-anchor-id="data-loading-and-preprocessing">Data Loading and Preprocessing</h3>
<p>We filter for high-variance genes to reduce noise and dimensionality before feeding the data into the autoencoder.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># load data: RNA expression</span></span>
<span id="cb2-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Note: these RData files contain preprocessed data matrices with matched sample information</span></span>
<span id="cb2-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># DepMap data can be downloaded from https://depmap.org/portal/download/</span></span>
<span id="cb2-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Here I am using the 24Q2 release</span></span>
<span id="cb2-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">load</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CCLE_24Q2_GE_match_sample_info.RData"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># rows are cell lines, columns are genes, values are RNA-seq log2(TPM+1)</span></span>
<span id="cb2-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">load</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sample_info_match_biomarkers.RData"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># sample info with lineage annotations</span></span>
<span id="cb2-7"></span>
<span id="cb2-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># remove low variance genes</span></span>
<span id="cb2-9">ge_var <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">apply</span>(ccle_ge_match_sam, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, var, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb2-10">ge_dat_hv <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ccle_ge_match_sam[, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">which</span>(ge_var <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">quantile</span>(ge_var, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>))] <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># select high variance genes (top 80th percentile)</span></span>
<span id="cb2-11"></span>
<span id="cb2-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># remove any cell lines (rows) that contains NAs</span></span>
<span id="cb2-13">ge_dat_hv <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ge_dat_hv <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">na.omit</span>()</span>
<span id="cb2-14"></span>
<span id="cb2-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># standardize the data</span></span>
<span id="cb2-16">ge_dat_hv <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale</span>(ge_dat_hv) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.data.frame</span>()</span></code></pre></div></div>
</div>
<p>Let’s see what the RNA expression data looks like:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">ge_dat_hv[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-2">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rownames_to_column</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CellLine"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-3">    gt<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gt</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rowname_col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CellLine"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div id="hdkwwohzar" style="padding-left:0px;padding-right:0px;padding-top:10px;padding-bottom:10px;overflow-x:auto;overflow-y:auto;width:auto;height:auto;">
<style>#hdkwwohzar table {
  font-family: system-ui, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

#hdkwwohzar thead, #hdkwwohzar tbody, #hdkwwohzar tfoot, #hdkwwohzar tr, #hdkwwohzar td, #hdkwwohzar th {
  border-style: none;
}

#hdkwwohzar p {
  margin: 0;
  padding: 0;
}

#hdkwwohzar .gt_table {
  display: table;
  border-collapse: collapse;
  line-height: normal;
  margin-left: auto;
  margin-right: auto;
  color: #333333;
  font-size: 16px;
  font-weight: normal;
  font-style: normal;
  background-color: #FFFFFF;
  width: auto;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #A8A8A8;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #A8A8A8;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
}

#hdkwwohzar .gt_caption {
  padding-top: 4px;
  padding-bottom: 4px;
}

#hdkwwohzar .gt_title {
  color: #333333;
  font-size: 125%;
  font-weight: initial;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-color: #FFFFFF;
  border-bottom-width: 0;
}

#hdkwwohzar .gt_subtitle {
  color: #333333;
  font-size: 85%;
  font-weight: initial;
  padding-top: 3px;
  padding-bottom: 5px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-color: #FFFFFF;
  border-top-width: 0;
}

#hdkwwohzar .gt_heading {
  background-color: #FFFFFF;
  text-align: center;
  border-bottom-color: #FFFFFF;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#hdkwwohzar .gt_bottom_border {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#hdkwwohzar .gt_col_headings {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#hdkwwohzar .gt_col_heading {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 6px;
  padding-left: 5px;
  padding-right: 5px;
  overflow-x: hidden;
}

#hdkwwohzar .gt_column_spanner_outer {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  padding-top: 0;
  padding-bottom: 0;
  padding-left: 4px;
  padding-right: 4px;
}

#hdkwwohzar .gt_column_spanner_outer:first-child {
  padding-left: 0;
}

#hdkwwohzar .gt_column_spanner_outer:last-child {
  padding-right: 0;
}

#hdkwwohzar .gt_column_spanner {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 5px;
  overflow-x: hidden;
  display: inline-block;
  width: 100%;
}

#hdkwwohzar .gt_spanner_row {
  border-bottom-style: hidden;
}

#hdkwwohzar .gt_group_heading {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  text-align: left;
}

#hdkwwohzar .gt_empty_group_heading {
  padding: 0.5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: middle;
}

#hdkwwohzar .gt_from_md > :first-child {
  margin-top: 0;
}

#hdkwwohzar .gt_from_md > :last-child {
  margin-bottom: 0;
}

#hdkwwohzar .gt_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  margin: 10px;
  border-top-style: solid;
  border-top-width: 1px;
  border-top-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  overflow-x: hidden;
}

#hdkwwohzar .gt_stub {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
}

#hdkwwohzar .gt_stub_row_group {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
  vertical-align: top;
}

#hdkwwohzar .gt_row_group_first td {
  border-top-width: 2px;
}

#hdkwwohzar .gt_row_group_first th {
  border-top-width: 2px;
}

#hdkwwohzar .gt_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#hdkwwohzar .gt_first_summary_row {
  border-top-style: solid;
  border-top-color: #D3D3D3;
}

#hdkwwohzar .gt_first_summary_row.thick {
  border-top-width: 2px;
}

#hdkwwohzar .gt_last_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#hdkwwohzar .gt_grand_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#hdkwwohzar .gt_first_grand_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-style: double;
  border-top-width: 6px;
  border-top-color: #D3D3D3;
}

#hdkwwohzar .gt_last_grand_summary_row_top {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: double;
  border-bottom-width: 6px;
  border-bottom-color: #D3D3D3;
}

#hdkwwohzar .gt_striped {
  background-color: rgba(128, 128, 128, 0.05);
}

#hdkwwohzar .gt_table_body {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#hdkwwohzar .gt_footnotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#hdkwwohzar .gt_footnote {
  margin: 0px;
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#hdkwwohzar .gt_sourcenotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#hdkwwohzar .gt_sourcenote {
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#hdkwwohzar .gt_left {
  text-align: left;
}

#hdkwwohzar .gt_center {
  text-align: center;
}

#hdkwwohzar .gt_right {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

#hdkwwohzar .gt_font_normal {
  font-weight: normal;
}

#hdkwwohzar .gt_font_bold {
  font-weight: bold;
}

#hdkwwohzar .gt_font_italic {
  font-style: italic;
}

#hdkwwohzar .gt_super {
  font-size: 65%;
}

#hdkwwohzar .gt_footnote_marks {
  font-size: 75%;
  vertical-align: 0.4em;
  position: initial;
}

#hdkwwohzar .gt_asterisk {
  font-size: 100%;
  vertical-align: 0;
}

#hdkwwohzar .gt_indent_1 {
  text-indent: 5px;
}

#hdkwwohzar .gt_indent_2 {
  text-indent: 10px;
}

#hdkwwohzar .gt_indent_3 {
  text-indent: 15px;
}

#hdkwwohzar .gt_indent_4 {
  text-indent: 20px;
}

#hdkwwohzar .gt_indent_5 {
  text-indent: 25px;
}

#hdkwwohzar .katex-display {
  display: inline-flex !important;
  margin-bottom: 0.75em !important;
}

#hdkwwohzar div.Reactable > div.rt-table > div.rt-thead > div.rt-tr.rt-tr-group-header > div.rt-th-group:after {
  height: 0px !important;
}
</style>

<table class="gt_table caption-top table table-sm table-striped small" data-quarto-bootstrap="false">
<thead>
<tr class="gt_col_headings header">
<th id="a::stub" class="gt_col_heading gt_columns_bottom_border gt_left" data-quarto-table-cell-role="th" scope="col"></th>
<th id="TSPAN6" class="gt_col_heading gt_columns_bottom_border gt_right" data-quarto-table-cell-role="th" scope="col">TSPAN6</th>
<th id="DPM1" class="gt_col_heading gt_columns_bottom_border gt_right" data-quarto-table-cell-role="th" scope="col">DPM1</th>
<th id="FIRRM" class="gt_col_heading gt_columns_bottom_border gt_right" data-quarto-table-cell-role="th" scope="col">FIRRM</th>
<th id="FGR" class="gt_col_heading gt_columns_bottom_border gt_right" data-quarto-table-cell-role="th" scope="col">FGR</th>
<th id="CFH" class="gt_col_heading gt_columns_bottom_border gt_right" data-quarto-table-cell-role="th" scope="col">CFH</th>
</tr>
</thead>
<tbody class="gt_table_body">
<tr class="odd">
<th id="stub_1_1" class="gt_row gt_left gt_stub" data-quarto-table-cell-role="th" scope="row">NIHOVCAR3</th>
<td class="gt_row gt_right" headers="stub_1_1 TSPAN6">1.0926276</td>
<td class="gt_row gt_right" headers="stub_1_1 DPM1">1.5011722</td>
<td class="gt_row gt_right" headers="stub_1_1 FIRRM">0.6858109</td>
<td class="gt_row gt_right" headers="stub_1_1 FGR">-0.3170060</td>
<td class="gt_row gt_right" headers="stub_1_1 CFH">-0.5737765</td>
</tr>
<tr class="even">
<th id="stub_1_2" class="gt_row gt_left gt_stub" data-quarto-table-cell-role="th" scope="row">HL60</th>
<td class="gt_row gt_right" headers="stub_1_2 TSPAN6">-1.9693314</td>
<td class="gt_row gt_right" headers="stub_1_2 DPM1">-1.2610758</td>
<td class="gt_row gt_right" headers="stub_1_2 FIRRM">-0.6848542</td>
<td class="gt_row gt_right" headers="stub_1_2 FGR">3.0178580</td>
<td class="gt_row gt_right" headers="stub_1_2 CFH">-0.8997009</td>
</tr>
<tr class="odd">
<th id="stub_1_3" class="gt_row gt_left gt_stub" data-quarto-table-cell-role="th" scope="row">CACO2</th>
<td class="gt_row gt_right" headers="stub_1_3 TSPAN6">1.1699782</td>
<td class="gt_row gt_right" headers="stub_1_3 DPM1">2.0373191</td>
<td class="gt_row gt_right" headers="stub_1_3 FIRRM">0.2826820</td>
<td class="gt_row gt_right" headers="stub_1_3 FGR">-0.3517010</td>
<td class="gt_row gt_right" headers="stub_1_3 CFH">-0.9468198</td>
</tr>
<tr class="even">
<th id="stub_1_4" class="gt_row gt_left gt_stub" data-quarto-table-cell-role="th" scope="row">HEL</th>
<td class="gt_row gt_right" headers="stub_1_4 TSPAN6">-0.7463001</td>
<td class="gt_row gt_right" headers="stub_1_4 DPM1">-1.6419864</td>
<td class="gt_row gt_right" headers="stub_1_4 FIRRM">0.3162823</td>
<td class="gt_row gt_right" headers="stub_1_4 FGR">0.3703802</td>
<td class="gt_row gt_right" headers="stub_1_4 CFH">1.2136680</td>
</tr>
<tr class="odd">
<th id="stub_1_5" class="gt_row gt_left gt_stub" data-quarto-table-cell-role="th" scope="row">HEL9217</th>
<td class="gt_row gt_right" headers="stub_1_5 TSPAN6">-0.5779999</td>
<td class="gt_row gt_right" headers="stub_1_5 DPM1">-0.9799468</td>
<td class="gt_row gt_right" headers="stub_1_5 FIRRM">2.0368574</td>
<td class="gt_row gt_right" headers="stub_1_5 FGR">-0.1876538</td>
<td class="gt_row gt_right" headers="stub_1_5 CFH">1.5663762</td>
</tr>
</tbody>
</table>

</div>
</div>
</div>
<p>Let’s see what the sample info looks like:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1">sample_info[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, ] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> gt<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gt</span>()</span></code></pre></div></div>
<div class="cell-output-display">
<div id="iibertzeff" style="padding-left:0px;padding-right:0px;padding-top:10px;padding-bottom:10px;overflow-x:auto;overflow-y:auto;width:auto;height:auto;">
<style>#iibertzeff table {
  font-family: system-ui, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

#iibertzeff thead, #iibertzeff tbody, #iibertzeff tfoot, #iibertzeff tr, #iibertzeff td, #iibertzeff th {
  border-style: none;
}

#iibertzeff p {
  margin: 0;
  padding: 0;
}

#iibertzeff .gt_table {
  display: table;
  border-collapse: collapse;
  line-height: normal;
  margin-left: auto;
  margin-right: auto;
  color: #333333;
  font-size: 16px;
  font-weight: normal;
  font-style: normal;
  background-color: #FFFFFF;
  width: auto;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #A8A8A8;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #A8A8A8;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
}

#iibertzeff .gt_caption {
  padding-top: 4px;
  padding-bottom: 4px;
}

#iibertzeff .gt_title {
  color: #333333;
  font-size: 125%;
  font-weight: initial;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-color: #FFFFFF;
  border-bottom-width: 0;
}

#iibertzeff .gt_subtitle {
  color: #333333;
  font-size: 85%;
  font-weight: initial;
  padding-top: 3px;
  padding-bottom: 5px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-color: #FFFFFF;
  border-top-width: 0;
}

#iibertzeff .gt_heading {
  background-color: #FFFFFF;
  text-align: center;
  border-bottom-color: #FFFFFF;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#iibertzeff .gt_bottom_border {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#iibertzeff .gt_col_headings {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#iibertzeff .gt_col_heading {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 6px;
  padding-left: 5px;
  padding-right: 5px;
  overflow-x: hidden;
}

#iibertzeff .gt_column_spanner_outer {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  padding-top: 0;
  padding-bottom: 0;
  padding-left: 4px;
  padding-right: 4px;
}

#iibertzeff .gt_column_spanner_outer:first-child {
  padding-left: 0;
}

#iibertzeff .gt_column_spanner_outer:last-child {
  padding-right: 0;
}

#iibertzeff .gt_column_spanner {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 5px;
  overflow-x: hidden;
  display: inline-block;
  width: 100%;
}

#iibertzeff .gt_spanner_row {
  border-bottom-style: hidden;
}

#iibertzeff .gt_group_heading {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  text-align: left;
}

#iibertzeff .gt_empty_group_heading {
  padding: 0.5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: middle;
}

#iibertzeff .gt_from_md > :first-child {
  margin-top: 0;
}

#iibertzeff .gt_from_md > :last-child {
  margin-bottom: 0;
}

#iibertzeff .gt_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  margin: 10px;
  border-top-style: solid;
  border-top-width: 1px;
  border-top-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  overflow-x: hidden;
}

#iibertzeff .gt_stub {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
}

#iibertzeff .gt_stub_row_group {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
  vertical-align: top;
}

#iibertzeff .gt_row_group_first td {
  border-top-width: 2px;
}

#iibertzeff .gt_row_group_first th {
  border-top-width: 2px;
}

#iibertzeff .gt_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#iibertzeff .gt_first_summary_row {
  border-top-style: solid;
  border-top-color: #D3D3D3;
}

#iibertzeff .gt_first_summary_row.thick {
  border-top-width: 2px;
}

#iibertzeff .gt_last_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#iibertzeff .gt_grand_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#iibertzeff .gt_first_grand_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-style: double;
  border-top-width: 6px;
  border-top-color: #D3D3D3;
}

#iibertzeff .gt_last_grand_summary_row_top {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: double;
  border-bottom-width: 6px;
  border-bottom-color: #D3D3D3;
}

#iibertzeff .gt_striped {
  background-color: rgba(128, 128, 128, 0.05);
}

#iibertzeff .gt_table_body {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#iibertzeff .gt_footnotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#iibertzeff .gt_footnote {
  margin: 0px;
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#iibertzeff .gt_sourcenotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#iibertzeff .gt_sourcenote {
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#iibertzeff .gt_left {
  text-align: left;
}

#iibertzeff .gt_center {
  text-align: center;
}

#iibertzeff .gt_right {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

#iibertzeff .gt_font_normal {
  font-weight: normal;
}

#iibertzeff .gt_font_bold {
  font-weight: bold;
}

#iibertzeff .gt_font_italic {
  font-style: italic;
}

#iibertzeff .gt_super {
  font-size: 65%;
}

#iibertzeff .gt_footnote_marks {
  font-size: 75%;
  vertical-align: 0.4em;
  position: initial;
}

#iibertzeff .gt_asterisk {
  font-size: 100%;
  vertical-align: 0;
}

#iibertzeff .gt_indent_1 {
  text-indent: 5px;
}

#iibertzeff .gt_indent_2 {
  text-indent: 10px;
}

#iibertzeff .gt_indent_3 {
  text-indent: 15px;
}

#iibertzeff .gt_indent_4 {
  text-indent: 20px;
}

#iibertzeff .gt_indent_5 {
  text-indent: 25px;
}

#iibertzeff .katex-display {
  display: inline-flex !important;
  margin-bottom: 0.75em !important;
}

#iibertzeff div.Reactable > div.rt-table > div.rt-thead > div.rt-tr.rt-tr-group-header > div.rt-th-group:after {
  height: 0px !important;
}
</style>

<table class="gt_table caption-top table table-sm table-striped small" data-quarto-bootstrap="false">
<thead>
<tr class="gt_col_headings header">
<th id="ModelID" class="gt_col_heading gt_columns_bottom_border gt_left" data-quarto-table-cell-role="th" scope="col">ModelID</th>
<th id="StrippedCellLineName" class="gt_col_heading gt_columns_bottom_border gt_left" data-quarto-table-cell-role="th" scope="col">StrippedCellLineName</th>
<th id="CCLEName" class="gt_col_heading gt_columns_bottom_border gt_left" data-quarto-table-cell-role="th" scope="col">CCLEName</th>
<th id="OncotreeLineage" class="gt_col_heading gt_columns_bottom_border gt_left" data-quarto-table-cell-role="th" scope="col">OncotreeLineage</th>
<th id="OncotreeSubtype" class="gt_col_heading gt_columns_bottom_border gt_left" data-quarto-table-cell-role="th" scope="col">OncotreeSubtype</th>
<th id="OncotreePrimaryDisease" class="gt_col_heading gt_columns_bottom_border gt_left" data-quarto-table-cell-role="th" scope="col">OncotreePrimaryDisease</th>
<th id="LegacySubSubtype" class="gt_col_heading gt_columns_bottom_border gt_left" data-quarto-table-cell-role="th" scope="col">LegacySubSubtype</th>
<th id="LegacyMolecularSubtype" class="gt_col_heading gt_columns_bottom_border gt_right" data-quarto-table-cell-role="th" scope="col">LegacyMolecularSubtype</th>
<th id="PatientMolecularSubtype" class="gt_col_heading gt_columns_bottom_border gt_left" data-quarto-table-cell-role="th" scope="col">PatientMolecularSubtype</th>
</tr>
</thead>
<tbody class="gt_table_body">
<tr class="odd">
<td class="gt_row gt_left" headers="ModelID">ACH-000001</td>
<td class="gt_row gt_left" headers="StrippedCellLineName">NIHOVCAR3</td>
<td class="gt_row gt_left" headers="CCLEName">NIHOVCAR3_OVARY</td>
<td class="gt_row gt_left" headers="OncotreeLineage">Ovary/Fallopian Tube</td>
<td class="gt_row gt_left" headers="OncotreeSubtype">High-Grade Serous Ovarian Cancer</td>
<td class="gt_row gt_left" headers="OncotreePrimaryDisease">Ovarian Epithelial Tumor</td>
<td class="gt_row gt_left" headers="LegacySubSubtype">high_grade_serous</td>
<td class="gt_row gt_right" headers="LegacyMolecularSubtype"></td>
<td class="gt_row gt_left" headers="PatientMolecularSubtype"></td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers="ModelID">ACH-000002</td>
<td class="gt_row gt_left" headers="StrippedCellLineName">HL60</td>
<td class="gt_row gt_left" headers="CCLEName">HL60_HAEMATOPOIETIC_AND_LYMPHOID_TISSUE</td>
<td class="gt_row gt_left" headers="OncotreeLineage">Myeloid</td>
<td class="gt_row gt_left" headers="OncotreeSubtype">Acute Myeloid Leukemia</td>
<td class="gt_row gt_left" headers="OncotreePrimaryDisease">Acute Myeloid Leukemia</td>
<td class="gt_row gt_left" headers="LegacySubSubtype">M3</td>
<td class="gt_row gt_right" headers="LegacyMolecularSubtype"></td>
<td class="gt_row gt_left" headers="PatientMolecularSubtype">TP53(del), CDKN2A and NRAS mutations [PubMed=288488], No PML-RARA fusion</td>
</tr>
<tr class="odd">
<td class="gt_row gt_left" headers="ModelID">ACH-000003</td>
<td class="gt_row gt_left" headers="StrippedCellLineName">CACO2</td>
<td class="gt_row gt_left" headers="CCLEName">CACO2_LARGE_INTESTINE</td>
<td class="gt_row gt_left" headers="OncotreeLineage">Bowel</td>
<td class="gt_row gt_left" headers="OncotreeSubtype">Colon Adenocarcinoma</td>
<td class="gt_row gt_left" headers="OncotreePrimaryDisease">Colorectal Adenocarcinoma</td>
<td class="gt_row gt_left" headers="LegacySubSubtype"></td>
<td class="gt_row gt_right" headers="LegacyMolecularSubtype"></td>
<td class="gt_row gt_left" headers="PatientMolecularSubtype"></td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers="ModelID">ACH-000004</td>
<td class="gt_row gt_left" headers="StrippedCellLineName">HEL</td>
<td class="gt_row gt_left" headers="CCLEName">HEL_HAEMATOPOIETIC_AND_LYMPHOID_TISSUE</td>
<td class="gt_row gt_left" headers="OncotreeLineage">Myeloid</td>
<td class="gt_row gt_left" headers="OncotreeSubtype">Acute Myeloid Leukemia</td>
<td class="gt_row gt_left" headers="OncotreePrimaryDisease">Acute Myeloid Leukemia</td>
<td class="gt_row gt_left" headers="LegacySubSubtype">M6</td>
<td class="gt_row gt_right" headers="LegacyMolecularSubtype"></td>
<td class="gt_row gt_left" headers="PatientMolecularSubtype">JAK2 and TP53 mutations,</td>
</tr>
<tr class="odd">
<td class="gt_row gt_left" headers="ModelID">ACH-000005</td>
<td class="gt_row gt_left" headers="StrippedCellLineName">HEL9217</td>
<td class="gt_row gt_left" headers="CCLEName">HEL9217_HAEMATOPOIETIC_AND_LYMPHOID_TISSUE</td>
<td class="gt_row gt_left" headers="OncotreeLineage">Myeloid</td>
<td class="gt_row gt_left" headers="OncotreeSubtype">Acute Myeloid Leukemia</td>
<td class="gt_row gt_left" headers="OncotreePrimaryDisease">Acute Myeloid Leukemia</td>
<td class="gt_row gt_left" headers="LegacySubSubtype">M6</td>
<td class="gt_row gt_right" headers="LegacyMolecularSubtype"></td>
<td class="gt_row gt_left" headers="PatientMolecularSubtype">JAK2 and TP53 mutations</td>
</tr>
</tbody>
</table>

</div>
</div>
</div>
</section>
<section id="model-training" class="level3">
<h3 class="anchored" data-anchor-id="model-training">Model Training</h3>
<p>We perform a hyperparameter grid search to find the optimal autoencoder architecture.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Hyperparameter search grid to find best AE model</span></span>
<span id="cb5-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">h2o.init</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nthreads =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">max_mem_size =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"60G"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># initialize H2O with 20 threads and 60GB memory</span></span>
<span id="cb5-3">rna_h2o <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.h2o</span>(ge_dat_hv)</span>
<span id="cb5-4"></span>
<span id="cb5-5">hyper_grid <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hidden =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(</span>
<span id="cb5-6">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>),</span>
<span id="cb5-7">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>),</span>
<span id="cb5-8">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>),</span>
<span id="cb5-9">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>),</span>
<span id="cb5-10">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2000</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2000</span>),</span>
<span id="cb5-11">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">300</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>)</span>
<span id="cb5-12">))</span>
<span id="cb5-13"></span>
<span id="cb5-14">ae_grid <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">h2o.grid</span>(</span>
<span id="cb5-15">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">algorithm =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"deeplearning"</span>,</span>
<span id="cb5-16">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colnames</span>(rna_h2o),</span>
<span id="cb5-17">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">training_frame =</span> rna_h2o,</span>
<span id="cb5-18">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">grid_id =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RNA_ae1"</span>,</span>
<span id="cb5-19">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">autoencoder =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>,</span>
<span id="cb5-20">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">activation =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"TanhWithDropout"</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># I've also tried "RectifierWithDropout" but seemed to encounter exploding gradients</span></span>
<span id="cb5-21">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hyper_params =</span> hyper_grid,</span>
<span id="cb5-22">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nesterov_accelerated_gradient =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>,</span>
<span id="cb5-23">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">epochs =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>,</span>
<span id="cb5-24">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stopping_rounds =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>,</span>
<span id="cb5-25">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">seed =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">524</span></span>
<span id="cb5-26">)</span>
<span id="cb5-27"></span>
<span id="cb5-28"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># get grid results, sorted by reconstruction error (MSE)</span></span>
<span id="cb5-29"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">h2o.getGrid</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RNA_ae1"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sort_by =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mse"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">decreasing =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb5-30"></span>
<span id="cb5-31"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># AE learning with best parameters (selected from grid search)</span></span>
<span id="cb5-32">ae_model <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">h2o.deeplearning</span>(</span>
<span id="cb5-33">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colnames</span>(rna_h2o),</span>
<span id="cb5-34">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">training_frame =</span> rna_h2o,</span>
<span id="cb5-35">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">autoencoder =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>,</span>
<span id="cb5-36">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hidden =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>),</span>
<span id="cb5-37">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hidden_dropout_ratios =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>),</span>
<span id="cb5-38">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">activation =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"TanhWithDropout"</span>,</span>
<span id="cb5-39">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">epochs =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>,</span>
<span id="cb5-40">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stopping_rounds =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>,</span>
<span id="cb5-41">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nesterov_accelerated_gradient =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>,</span>
<span id="cb5-42">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">seed =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">524</span></span>
<span id="cb5-43">)</span>
<span id="cb5-44"></span>
<span id="cb5-45"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># extract the compressed features</span></span>
<span id="cb5-46">compressed_features <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">h2o.deepfeatures</span>(ae_model, rna_h2o, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">layer =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.data.frame</span>() <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># get deep features from the middle layer</span></span>
<span id="cb5-47"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rownames</span>(compressed_features) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rownames</span>(ge_dat_hv)</span>
<span id="cb5-48"></span>
<span id="cb5-49"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Save results</span></span>
<span id="cb5-50"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fwrite</span>(compressed_features, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">file =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"./results/output_files/RNA_AE_compressed_features.csv"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sep =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">","</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">row.names =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb5-51"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">h2o.shutdown</span>()</span></code></pre></div></div>
</div>
<p>I want to point out that the choice of architecture (number of layers and nodes) and hyperparameters (dropout rates, activation functions) can significantly impact the quality of the learned embeddings. For example, using batch normalization layers or experimenting with different activation functions (e.g., ReLU, Leaky ReLU) might yield better results depending on the data characteristics. Readers interested in this topic should explore more sophisticated MLP training platform like Keras and TensorFlow.</p>
</section>
</section>
<section id="aim-2-multi-omics-autoencoder" class="level2">
<h2 class="anchored" data-anchor-id="aim-2-multi-omics-autoencoder">Aim 2: Multi-omics Autoencoder</h2>
<p>Next, we integrate RNA expression, mutation data (damaging, hotspot), and CRISPR dependency scores.</p>
<section id="data-integration" class="level3">
<h3 class="anchored" data-anchor-id="data-integration">Data Integration</h3>
<p>We process each data type separately (filtering, standardizing) and then combine them into a single multi-omics dataset.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># load data (all rows are cell lines and columns are genes)</span></span>
<span id="cb6-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">load</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CCLE_24Q2_GE_match_sample_info.RData"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># RNA expression</span></span>
<span id="cb6-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">load</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"DepMap_24Q2_Chronos_match_sample_info.RData"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># CRISPR dependency scores (Chronos)</span></span>
<span id="cb6-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">load</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CCLE_24Q2_DAMMUT_match_sample_info.RData"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Damaging mutations (0: non-mutated, 1: mutated heterozygous, 2: mutated homozygous)</span></span>
<span id="cb6-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">load</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CCLE_24Q2_HOTMUT_match_sample_info.RData"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Hotspot mutations (0: non-mutated, 1: mutated heterozygous, 2: mutated homozygous)</span></span>
<span id="cb6-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">load</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sample_info_match_biomarkers.RData"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># sample info</span></span>
<span id="cb6-7"></span>
<span id="cb6-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># RNA processing</span></span>
<span id="cb6-9">ge_var <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">apply</span>(ccle_ge_match_sam, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, var, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb6-10">ge_dat_hv <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ccle_ge_match_sam[, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">which</span>(ge_var <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">quantile</span>(ge_var, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>))]</span>
<span id="cb6-11">ge_dat_hv <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale</span>(ge_dat_hv) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.data.frame</span>()</span>
<span id="cb6-12"></span>
<span id="cb6-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Mutation processing</span></span>
<span id="cb6-14">dam_mut_dat_filt <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> dammut_dat_match_sam[, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">which</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">apply</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">na.omit</span>(dammut_dat_match_sam), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(x <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>)] <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># filter genes with &gt;20 mutated events across cell lines</span></span>
<span id="cb6-15">hot_mut_dat_filt <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> hotmut_dat_match_sam[, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">which</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">apply</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">na.omit</span>(hotmut_dat_match_sam), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(x <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>)]</span>
<span id="cb6-16"></span>
<span id="cb6-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># CRISPR processing</span></span>
<span id="cb6-18">crispr_var <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">apply</span>(chronos_dat_match_sam, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, var, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb6-19">crispr_dat_hv <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> chronos_dat_match_sam[, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">which</span>(crispr_var <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">quantile</span>(crispr_var, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>))]</span>
<span id="cb6-20">crispr_dat_hv <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale</span>(crispr_dat_hv) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.data.frame</span>()</span>
<span id="cb6-21"></span>
<span id="cb6-22"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Combine data</span></span>
<span id="cb6-23"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colnames</span>(ge_dat_hv) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colnames</span>(ge_dat_hv), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"_RNA"</span>)</span>
<span id="cb6-24"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colnames</span>(dam_mut_dat_filt) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colnames</span>(dam_mut_dat_filt), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"_DAMMUT"</span>)</span>
<span id="cb6-25"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colnames</span>(hot_mut_dat_filt) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colnames</span>(hot_mut_dat_filt), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"_HOTMUT"</span>)</span>
<span id="cb6-26"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colnames</span>(crispr_dat_hv) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colnames</span>(crispr_dat_hv), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"_CRISPR"</span>)</span>
<span id="cb6-27"></span>
<span id="cb6-28">multi_omics_dat <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cbind</span>(ge_dat_hv, dam_mut_dat_filt, hot_mut_dat_filt, crispr_dat_hv)</span>
<span id="cb6-29"></span>
<span id="cb6-30"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Remove rows with too many NAs</span></span>
<span id="cb6-31">multi_omics_dat <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> multi_omics_dat[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rowSums</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</span>(multi_omics_dat)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ncol</span>(multi_omics_dat) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.8</span>, ] <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># keep rows with &lt;=80% NAs</span></span></code></pre></div></div>
</div>
</section>
<section id="multi-omics-model-training" class="level3">
<h3 class="anchored" data-anchor-id="multi-omics-model-training">Multi-omics Model Training</h3>
<p>We train another autoencoder on this combined dataset.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">h2o.init</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nthreads =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">max_mem_size =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"60G"</span>)</span>
<span id="cb7-2">rna_h2o <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.h2o</span>(multi_omics_dat)</span>
<span id="cb7-3"></span>
<span id="cb7-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># (Grid search code omitted for brevity, similar to Aim 1)</span></span>
<span id="cb7-5"></span>
<span id="cb7-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># AE learning with best parameters</span></span>
<span id="cb7-7">ae_model <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">h2o.deeplearning</span>(</span>
<span id="cb7-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colnames</span>(rna_h2o),</span>
<span id="cb7-9">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">training_frame =</span> rna_h2o,</span>
<span id="cb7-10">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">autoencoder =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>,</span>
<span id="cb7-11">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hidden =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">300</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>),</span>
<span id="cb7-12">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hidden_dropout_ratios =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>),</span>
<span id="cb7-13">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">activation =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"TanhWithDropout"</span>,</span>
<span id="cb7-14">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">epochs =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>,</span>
<span id="cb7-15">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stopping_rounds =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>,</span>
<span id="cb7-16">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nesterov_accelerated_gradient =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>,</span>
<span id="cb7-17">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">seed =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">524</span></span>
<span id="cb7-18">)</span>
<span id="cb7-19"></span>
<span id="cb7-20">compressed_features <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">h2o.deepfeatures</span>(ae_model, rna_h2o, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">layer =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.data.frame</span>() <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># here we take 300-dim deep features</span></span>
<span id="cb7-21"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rownames</span>(compressed_features) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rownames</span>(multi_omics_dat)</span>
<span id="cb7-22"></span>
<span id="cb7-23"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fwrite</span>(compressed_features, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">file =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"./results/output_files/MultiOmics_AE_compressed_features.csv"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sep =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">","</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">row.names =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb7-24"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">h2o.shutdown</span>()</span></code></pre></div></div>
</div>
</section>
</section>
<section id="aim-3-visualization-and-comparison" class="level2">
<h2 class="anchored" data-anchor-id="aim-3-visualization-and-comparison">Aim 3: Visualization and Comparison</h2>
<p>Finally, we visualize the embeddings using UMAP and compare the clustering with known lineages.<br>
Here we are testing if the deep features preserve and/or enrich biological signals present in the original data.</p>
<section id="full-rna-umap-without-autoencoder" class="level3">
<h3 class="anchored" data-anchor-id="full-rna-umap-without-autoencoder">Full RNA UMAP (without Autoencoder)</h3>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># load data</span></span>
<span id="cb8-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">load</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CCLE_24Q2_GE_match_sample_info.RData"</span>)</span>
<span id="cb8-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">load</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sample_info_match_biomarkers.RData"</span>)</span>
<span id="cb8-4"></span>
<span id="cb8-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># remove low variance genes</span></span>
<span id="cb8-6">ge_var <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">apply</span>(ccle_ge_match_sam, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, var, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># calculate variance for each gene</span></span>
<span id="cb8-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(ge_var <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">quantile</span>(ge_var, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>)) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># how many genes passed variance cutoff: 15322</span></span>
<span id="cb8-8">ge_dat_hv <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ccle_ge_match_sam[, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">which</span>(ge_var <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">quantile</span>(ge_var, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>))] <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># select high variance genes</span></span>
<span id="cb8-9"></span>
<span id="cb8-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># standardize the data and match with sample info</span></span>
<span id="cb8-11">ge_dat_hv <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale</span>(ge_dat_hv) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb8-12">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.data.frame</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb8-13">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">na.omit</span>()</span>
<span id="cb8-14">sample_info <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> sample_info[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">match</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rownames</span>(ge_dat_hv), sample_info<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>StrippedCellLineName), ]</span>
<span id="cb8-15"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">all</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rownames</span>(ge_dat_hv) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> sample_info<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>StrippedCellLineName)</span>
<span id="cb8-16"></span>
<span id="cb8-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Run UMAP</span></span>
<span id="cb8-18"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(umap)</span>
<span id="cb8-19">umap_dat <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">umap</span>(</span>
<span id="cb8-20">    ge_dat_hv,</span>
<span id="cb8-21">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n_components =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>,</span>
<span id="cb8-22">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n_neighbors =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>,</span>
<span id="cb8-23">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">random_state =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">524</span>,</span>
<span id="cb8-24">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">verbose =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>,</span>
<span id="cb8-25">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n_threads =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span></span>
<span id="cb8-26">)</span>
<span id="cb8-27">umap_dat <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.data.frame</span>(umap_dat<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>layout)</span>
<span id="cb8-28">umap_dat<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>cell_line <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> sample_info<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>StrippedCellLineName</span>
<span id="cb8-29">umap_dat<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>lineage <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> sample_info<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>OncotreeLineage</span>
<span id="cb8-30">umap_dat<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>subtype <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> sample_info<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>OncotreeSubtype</span>
<span id="cb8-31"></span>
<span id="cb8-32"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Plot UMAP embedding colored by lineage</span></span>
<span id="cb8-33"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(umap_dat, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(V1, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>V2, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> lineage, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> lineage)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-34">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-35">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_manual</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">distinct_palette</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unique</span>(umap_dat<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>lineage)), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pal =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"brewerPlus"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">add =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lightgrey"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-36">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_manual</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">distinct_palette</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unique</span>(umap_dat<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>lineage)), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pal =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"brewerPlus"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">add =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lightgrey"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-37">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_classic</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base_size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-38">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># add lineage labels at the center of each lineage cluster</span></span>
<span id="cb8-39">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_label_repel</span>(</span>
<span id="cb8-40">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> umap_dat <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb8-41">            <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(lineage) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb8-42">            <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarize</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">V1 =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">median</span>(V1), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">V2 =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">median</span>(V2)),</span>
<span id="cb8-43">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> lineage, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> lineage),</span>
<span id="cb8-44">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>,</span>
<span id="cb8-45">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># color = "black",</span></span>
<span id="cb8-46">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"white"</span>,</span>
<span id="cb8-47">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.7</span>,</span>
<span id="cb8-48">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">show.legend =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>,</span>
<span id="cb8-49">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">max.overlaps =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb8-50">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-51">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(</span>
<span id="cb8-52">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span>,</span>
<span id="cb8-53">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>),</span>
<span id="cb8-54">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.text =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)</span>
<span id="cb8-55">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-56">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(</span>
<span id="cb8-57">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"UMAP1"</span>,</span>
<span id="cb8-58">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"UMAP2"</span>,</span>
<span id="cb8-59">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"UMAP of full CCLE RNA expression (no AE)"</span></span>
<span id="cb8-60">    )</span></code></pre></div></div>
</div>
<div class="cell" data-layout-align="center">
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jaychung10010.quarto.pub/all-things-bioinformatics/posts/2025-11-29_depmap-multiomics-autoencoder/CCLE_RNA_no_AE_UMAP_lineage.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:80.0%"></p>
</figure>
</div>
</div>
</div>
<p>As you can see, the full RNA data UMAP already shows some clustering by lineage, indicating that the transcriptomic profiles capture biological differences among cell lines.</p>
</section>
<section id="rna-deep-features-umap" class="level3">
<h3 class="anchored" data-anchor-id="rna-deep-features-umap">RNA deep features UMAP</h3>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Load compressed features</span></span>
<span id="cb9-2">RNA_AE_compressed_features <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fread</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"./results/output_files/RNA_AE_compressed_features.csv"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data.table =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb9-3">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">column_to_rownames</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">var =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"V1"</span>)</span>
<span id="cb9-4">RNA_AE_compressed_features <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> RNA_AE_compressed_features[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rowSums</span>(RNA_AE_compressed_features) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, ]</span>
<span id="cb9-5"></span>
<span id="cb9-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Match with sample info</span></span>
<span id="cb9-7">sample_info <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> sample_info[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">match</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rownames</span>(RNA_AE_compressed_features), sample_info<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>StrippedCellLineName), ]</span>
<span id="cb9-8"></span>
<span id="cb9-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Run UMAP</span></span>
<span id="cb9-10">umap_dat <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">umap</span>(RNA_AE_compressed_features, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n_components =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n_neighbors =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">random_state =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">524</span>)</span>
<span id="cb9-11">umap_dat <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.data.frame</span>(umap_dat<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>layout)</span>
<span id="cb9-12">umap_dat<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>lineage <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> sample_info<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>OncotreeLineage</span>
<span id="cb9-13"></span>
<span id="cb9-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Plot</span></span>
<span id="cb9-15"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(umap_dat, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(V1, V2, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> lineage, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> lineage)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-16">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-17">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_manual</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">distinct_palette</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unique</span>(umap_dat<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>lineage)), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pal =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"brewerPlus"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">add =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lightgrey"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-18">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_manual</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">distinct_palette</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unique</span>(umap_dat<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>lineage)), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pal =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"brewerPlus"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">add =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lightgrey"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-19">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_classic</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base_size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-20">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_label_repel</span>(</span>
<span id="cb9-21">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> umap_dat <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(lineage) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarize</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">V1 =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">median</span>(V1), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">V2 =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">median</span>(V2)),</span>
<span id="cb9-22">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> lineage, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> lineage),</span>
<span id="cb9-23">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"white"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.7</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">show.legend =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">max.overlaps =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb9-24">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-25">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-26">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"UMAP of AE compressed CCLE RNA expression"</span>)</span></code></pre></div></div>
</div>
<div class="cell" data-layout-align="center">
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jaychung10010.quarto.pub/all-things-bioinformatics/posts/2025-11-29_depmap-multiomics-autoencoder/CCLE_RNA_AE_UMAP_lineage.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:80.0%"></p>
</figure>
</div>
</div>
</div>
<p>The RNA deep features UMAP also shows clustering by lineage, similar to the full RNA data, indicating that the autoencoder effectively preserves the biological signal in a lower-dimensional space.</p>
</section>
<section id="multi-omics-deep-features-umap" class="level3">
<h3 class="anchored" data-anchor-id="multi-omics-deep-features-umap">Multi-omics deep features UMAP</h3>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Load compressed features</span></span>
<span id="cb10-2">MultiOmics_AE_compressed_features <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fread</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"./results/output_files/MultiOmics_AE_compressed_features.csv"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data.table =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb10-3">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">column_to_rownames</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">var =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"V1"</span>)</span>
<span id="cb10-4">MultiOmics_AE_compressed_features <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> MultiOmics_AE_compressed_features[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rowSums</span>(MultiOmics_AE_compressed_features) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, ]</span>
<span id="cb10-5"></span>
<span id="cb10-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Match with sample info</span></span>
<span id="cb10-7">sample_info <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> sample_info[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">match</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rownames</span>(MultiOmics_AE_compressed_features), sample_info<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>StrippedCellLineName), ]</span>
<span id="cb10-8"></span>
<span id="cb10-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Run UMAP</span></span>
<span id="cb10-10">umap_dat <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">umap</span>(MultiOmics_AE_compressed_features, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n_components =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n_neighbors =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">random_state =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">524</span>)</span>
<span id="cb10-11">umap_dat <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.data.frame</span>(umap_dat<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>layout)</span>
<span id="cb10-12">umap_dat<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>lineage <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> sample_info<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>OncotreeLineage</span>
<span id="cb10-13"></span>
<span id="cb10-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Plot</span></span>
<span id="cb10-15"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(umap_dat, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>V1, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>V2, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> lineage, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> lineage)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb10-16">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb10-17">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_manual</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">distinct_palette</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unique</span>(umap_dat<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>lineage)), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pal =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"brewerPlus"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">add =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lightgrey"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb10-18">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_manual</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">distinct_palette</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unique</span>(umap_dat<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>lineage)), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pal =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"brewerPlus"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">add =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lightgrey"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb10-19">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_classic</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base_size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb10-20">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_label_repel</span>(</span>
<span id="cb10-21">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> umap_dat <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(lineage) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarize</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">V1 =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">median</span>(V1), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">V2 =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">median</span>(V2)),</span>
<span id="cb10-22">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> lineage, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> lineage),</span>
<span id="cb10-23">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"white"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.7</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">show.legend =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">max.overlaps =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb10-24">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb10-25">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb10-26">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"UMAP of AE compressed CCLE Multi-omics"</span>)</span></code></pre></div></div>
</div>
<div class="cell" data-layout-align="center">
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jaychung10010.quarto.pub/all-things-bioinformatics/posts/2025-11-29_depmap-multiomics-autoencoder/CCLE_MultiOmics_AE_UMAP_lineage.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:80.0%"></p>
</figure>
</div>
</div>
</div>
<p>The AE embedded multi-OMICS appears to increase the granularity of cell line clustering by lineage.</p>
</section>
</section>
<section id="conclusions" class="level2">
<h2 class="anchored" data-anchor-id="conclusions">Conclusions</h2>
<p>It appears that both RNA alone and multi-omics autoencoder embeddings preserve biological signals related to cell lineage. The multi-omics embedding seems to provide even richer structure, potentially capturing more nuanced cell states. But whether these embeddings lead to improved drug sensitivity prediction accuracy remains to be tested.</p>
<p>In the next post, I will compare ML prediction accuracy on independent test data using deep features vs.&nbsp;full omics data.</p>
</section>
<section id="references" class="level2">
<h2 class="anchored" data-anchor-id="references">References</h2>
<ul>
<li>DepMap Portal: <a href="https://depmap.org/portal/" class="uri">https://depmap.org/portal/</a></li>
<li>H2O Documentation: <a href="https://docs.h2o.ai/h2o/latest-stable/h2o-docs/data-science/deep-learning.html" class="uri">https://docs.h2o.ai/h2o/latest-stable/h2o-docs/data-science/deep-learning.html</a></li>
<li>Hands-On Machine Learning with R: <a href="https://bradleyboehmke.github.io/HOML/" class="uri">https://bradleyboehmke.github.io/HOML/</a></li>
</ul>


</section>

 ]]></description>
  <category>R</category>
  <category>DepMap</category>
  <category>Autoencoder</category>
  <category>Multi-omics</category>
  <category>Machine Learning</category>
  <guid>https://jaychung10010.quarto.pub/all-things-bioinformatics/posts/2025-11-29_depmap-multiomics-autoencoder/</guid>
  <pubDate>Mon, 01 Dec 2025 08:00:00 GMT</pubDate>
  <media:content url="https://jaychung10010.quarto.pub/all-things-bioinformatics/posts/2025-11-29_depmap-multiomics-autoencoder/CCLE_MultiOmics_AE_UMAP_lineage.png" medium="image" type="image/png" height="115" width="144"/>
</item>
</channel>
</rss>
