File size: 8,621 Bytes
0b11a42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/nfs/home/yat_ldap/conda/envs/hbdx/envs/transforna/lib/python3.9/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
      "  from .autonotebook import tqdm as notebook_tqdm\n"
     ]
    }
   ],
   "source": [
    "from transforna import load,predict_transforna_all_models,predict_transforna,fold_sequences\n",
    "models_path = '/nfs/home/yat_ldap/VS_Projects/TransfoRNA-Framework/models/tcga/'\n",
    "lc_path = '/media/ftp_share/hbdx/annotation/feature_annotation/ANNOTATION/HBDxBase_annotation/TransfoRNA/compare_binning_strategies/v05/2024-04-19__230126_LC_DI_HB_GEL_v23.01.00/sRNA_anno_aggregated_on_seq.csv'\n",
    "tcga_path = '/media/ftp_share/hbdx/data_for_upload/TransfoRNA/data/TCGA__ngs__miRNA_log2RPM-24.04.0__var.csv'\n",
    "\n",
    "tcga_df = load(tcga_path)\n",
    "lc_df = load(lc_path)\n",
    "\n",
    "lc_df = lc_df[lc_df.sequence.str.len() <= 30]\n",
    "\n",
    "all_seqs = lc_df.sequence.tolist()+tcga_df.sequence.tolist()\n",
    "\n",
    "mapping_dict_path = '/media/ftp_share/hbdx/data_for_upload/TransfoRNA//data/subclass_to_annotation.json'\n",
    "mapping_dict = load(mapping_dict_path)\n",
    " "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "predictions = predict_transforna_all_models(all_seqs,trained_on='full',path_to_models=models_path)\n",
    "predictions.to_csv('predictions_lc_tcga.csv',index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "#read predictions\n",
    "predictions = load('predictions_lc_tcga.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "umaps = {}\n",
    "models = predictions['Model'].unique()\n",
    "for model in models:\n",
    "    if model == 'Ensemble':\n",
    "        continue\n",
    "    #get predictions\n",
    "    model_predictions = predictions[predictions['Model']==model]\n",
    "    #get is familiar rows\n",
    "    familiar_df = model_predictions[model_predictions['Is Familiar?']==True]\n",
    "    #get umap\n",
    "    umap_df = predict_transforna(model_predictions['Sequence'].tolist(),model=model,trained_on='full',path_to_models=models_path,umap_flag=True)\n",
    "    umaps[model] = umap_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "import plotly.express as px\n",
    "import numpy as np\n",
    "mcs = np.unique(umaps['Seq']['Net-Label'].map(mapping_dict))\n",
    "#filter out the classes that contain ;\n",
    "mcs = [mc for mc in mcs if ';' not in mc]\n",
    "colors = px.colors.qualitative.Plotly\n",
    "color_mapping = dict(zip(mcs,colors))\n",
    "for model,umap_df in umaps.items():\n",
    "    umap_df['Major Class'] = umap_df['Net-Label'].map(mapping_dict)\n",
    "    umap_df_copy = umap_df.copy()\n",
    "    #remove rows with Major Class containing ;\n",
    "    umap_df = umap_df[~umap_df['Major Class'].str.contains(';')]\n",
    "    fig = px.scatter(umap_df,x='UMAP1',y='UMAP2',color='Major Class',hover_data\n",
    "        =['Sequence'],title=model,\\\n",
    "                            width = 800, height=800,color_discrete_map=color_mapping)\n",
    "    fig.update_traces(marker=dict(size=1))\n",
    "    #white background\n",
    "    fig.update_layout(plot_bgcolor='rgba(0,0,0,0)')\n",
    "    #only show UMAP1 from 4.3 to 11\n",
    "    fig.update_xaxes(range=[4.3,11])\n",
    "    #and UMAP2 from -2.3 to 6.8\n",
    "    fig.update_yaxes(range=[-2.3,6.8])\n",
    "    #fig.show()\n",
    "    fig.write_image(f'lc_figures/lc_tcga_umap_selected_{model}.png')\n",
    "    fig.write_image(f'lc_figures/lc_tcga_umap_selected_{model}.svg')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import plotly.express as px\n",
    "import numpy as np\n",
    "mcs = np.unique(umaps['Seq']['Net-Label'].map(mapping_dict))\n",
    "#filter out the classes that contain ;\n",
    "mcs = [mc for mc in mcs if ';' not in mc]\n",
    "colors = px.colors.qualitative.Plotly + px.colors.qualitative.Light24\n",
    "color_mapping = dict(zip(mcs,colors))\n",
    "for model,umap_df in umaps.items():\n",
    "    umap_df['Major Class'] = umap_df['Net-Label'].map(mapping_dict)\n",
    "    umap_df_copy = umap_df.copy()\n",
    "    #remove rows with Major Class containing ;\n",
    "    umap_df = umap_df[~umap_df['Major Class'].str.contains(';')]\n",
    "    fig = px.scatter(umap_df,x='UMAP1',y='UMAP2',color='Major Class',hover_data\n",
    "        =['Sequence'],title=model,\\\n",
    "                            width = 800, height=800,color_discrete_map=color_mapping)\n",
    "    fig.update_traces(marker=dict(size=1))\n",
    "    #white background\n",
    "    fig.update_layout(plot_bgcolor='rgba(0,0,0,0)')\n",
    "    #fig.show()\n",
    "    fig.write_image(f'lc_figures/lc_tcga_umap_{model}.png')\n",
    "    fig.write_image(f'lc_figures/lc_tcga_umap_{model}.svg')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#plot umap using px.scatter for each model\n",
    "import plotly.express as px\n",
    "import numpy as np\n",
    "mcs = np.unique(umaps['Seq']['Net-Label'].map(mapping_dict))\n",
    "#filter out the classes that contain ;\n",
    "mcs = [mc for mc in mcs if ';' not in mc]\n",
    "colors = px.colors.qualitative.Plotly\n",
    "color_mapping = dict(zip(mcs,colors))\n",
    "umap_df = umaps['Seq']\n",
    "umap_df['Major Class'] = umap_df['Net-Label'].map(mapping_dict)\n",
    "umap_df_copy = umap_df.copy()\n",
    "#display points contained within the circle at center (7.9,2.5) and radius 4.3\n",
    "umap_df_copy['distance'] = np.sqrt((umap_df_copy['UMAP1']-7.9)**2+(umap_df_copy['UMAP2']-2.5)**2)\n",
    "umap_df_copy = umap_df_copy[umap_df_copy['distance']<=4.3]\n",
    "#remove rows with Major Class containing ;\n",
    "umap_df_copy = umap_df_copy[~umap_df_copy['Major Class'].str.contains(';')]\n",
    "fig = px.scatter(umap_df_copy,x='UMAP1',y='UMAP2',color='Major Class',hover_data\n",
    "    =['Sequence'],title=model,\\\n",
    "                        width = 800, height=800,color_discrete_map=color_mapping)\n",
    "fig.update_traces(marker=dict(size=1))\n",
    "#white background\n",
    "fig.update_layout(plot_bgcolor='rgba(0,0,0,0)')\n",
    "fig.show()\n",
    "#fig.write_image(f'lc_figures/lc_tcga_umap_selected_{model}.png')\n",
    "#fig.write_image(f'lc_figures/lc_tcga_umap_selected_{model}.svg')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#plot\n",
    "sec_struct = fold_sequences(model_predictions['Sequence'].tolist())['structure_37']\n",
    "#sec struct ratio is calculated as the number of non '.' characters divided by the length of the sequence\n",
    "sec_struct_ratio = sec_struct.apply(lambda x: (len(x)-x.count('.'))/len(x))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [],
   "source": [
    "umap_df =  umaps['Seq-Struct']\n",
    "fig = px.scatter(umap_df,x='UMAP1',y='UMAP2',color=sec_struct_ratio,hover_data=['Sequence'],title=model,\\\n",
    "                            width = 800, height=800,color_continuous_scale='Viridis')\n",
    "fig.update_traces(marker=dict(size=1))\n",
    "fig.update_layout(plot_bgcolor='rgba(0,0,0,0)')\n",
    "#save\n",
    "fig.write_image(f'lc_figures/lc_tcga_umap_{model}_dot_bracket.png')\n",
    "fig.write_image(f'lc_figures/lc_tcga_umap_{model}_dot_bracket.svg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "transforna",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}