File size: 28,913 Bytes
6bc7e45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "bf7a53a4",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T06:20:32.199795Z",
     "iopub.status.busy": "2025-03-25T06:20:32.199662Z",
     "iopub.status.idle": "2025-03-25T06:20:32.366584Z",
     "shell.execute_reply": "2025-03-25T06:20:32.366259Z"
    }
   },
   "outputs": [],
   "source": [
    "import sys\n",
    "import os\n",
    "sys.path.append(os.path.abspath(os.path.join(os.getcwd(), '../..')))\n",
    "\n",
    "# Path Configuration\n",
    "from tools.preprocess import *\n",
    "\n",
    "# Processing context\n",
    "trait = \"Acute_Myeloid_Leukemia\"\n",
    "cohort = \"GSE99612\"\n",
    "\n",
    "# Input paths\n",
    "in_trait_dir = \"../../input/GEO/Acute_Myeloid_Leukemia\"\n",
    "in_cohort_dir = \"../../input/GEO/Acute_Myeloid_Leukemia/GSE99612\"\n",
    "\n",
    "# Output paths\n",
    "out_data_file = \"../../output/preprocess/Acute_Myeloid_Leukemia/GSE99612.csv\"\n",
    "out_gene_data_file = \"../../output/preprocess/Acute_Myeloid_Leukemia/gene_data/GSE99612.csv\"\n",
    "out_clinical_data_file = \"../../output/preprocess/Acute_Myeloid_Leukemia/clinical_data/GSE99612.csv\"\n",
    "json_path = \"../../output/preprocess/Acute_Myeloid_Leukemia/cohort_info.json\"\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4ceb8255",
   "metadata": {},
   "source": [
    "### Step 1: Initial Data Loading"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "5dae7d17",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T06:20:32.368067Z",
     "iopub.status.busy": "2025-03-25T06:20:32.367912Z",
     "iopub.status.idle": "2025-03-25T06:20:32.479627Z",
     "shell.execute_reply": "2025-03-25T06:20:32.479320Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Background Information:\n",
      "!Series_title\t\"The effect of dietary fibre exposure on gene expression profiles in Caco-2 and THP-1 cells\"\n",
      "!Series_summary\t\"This SuperSeries is composed of the SubSeries listed below.\"\n",
      "!Series_overall_design\t\"Refer to individual Series\"\n",
      "Sample Characteristics Dictionary:\n",
      "{0: ['cell line: Caco-2', 'cell line: THP-1'], 1: ['Sex: male', 'cell type: macrophage'], 2: ['treatment: medium', 'treatment: Novelose 500 ug/ml', 'treatment: Inulin-chicory 500 ug/ml', 'treatment: Resistant starch corn 500 ug/ml', 'treatment: Sugar beet pectin 500 ug/ml', 'treatment: Beta-glucan oat medium viscosity 500 ug/ml', 'treatment: GOS 500 ug/ml', 'treatment: LPS 11.85 pg/ml', 'Sex: male'], 3: ['tumor origin: Caucasian colon adenocarcinoma', 'patient age: 1 year infant'], 4: ['passage number: 30-60', 'tumor origin: acute monocytic leukemia'], 5: ['days of differentiation on tranwells: 21', 'treatment: medium', 'treatment: LPS 11.85 pg/ml', 'treatment: Novelose 500 ug/ml', 'treatment: Inulin-chicory 500 ug/ml', 'treatment: Resistant starch corn 500 ug/ml', 'treatment: Sugar beet pectin 500 ug/ml', 'treatment: beta-glucan oat medium viscosity 500 ug/ml', 'treatment: GOS 500 ug/ml'], 6: [nan, 'passage number: passage 20-40'], 7: [nan, 'days of differentiation on tranwells: 4 day differentiated']}\n"
     ]
    }
   ],
   "source": [
    "from tools.preprocess import *\n",
    "# 1. Identify the paths to the SOFT file and the matrix file\n",
    "soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)\n",
    "\n",
    "# 2. Read the matrix file to obtain background information and sample characteristics data\n",
    "background_prefixes = ['!Series_title', '!Series_summary', '!Series_overall_design']\n",
    "clinical_prefixes = ['!Sample_geo_accession', '!Sample_characteristics_ch1']\n",
    "background_info, clinical_data = get_background_and_clinical_data(matrix_file, background_prefixes, clinical_prefixes)\n",
    "\n",
    "# 3. Obtain the sample characteristics dictionary from the clinical dataframe\n",
    "sample_characteristics_dict = get_unique_values_by_row(clinical_data)\n",
    "\n",
    "# 4. Explicitly print out all the background information and the sample characteristics dictionary\n",
    "print(\"Background Information:\")\n",
    "print(background_info)\n",
    "print(\"Sample Characteristics Dictionary:\")\n",
    "print(sample_characteristics_dict)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8d31c484",
   "metadata": {},
   "source": [
    "### Step 2: Dataset Analysis and Clinical Feature Extraction"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "a8599c5a",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T06:20:32.480985Z",
     "iopub.status.busy": "2025-03-25T06:20:32.480875Z",
     "iopub.status.idle": "2025-03-25T06:20:32.484883Z",
     "shell.execute_reply": "2025-03-25T06:20:32.484603Z"
    }
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import os\n",
    "import json\n",
    "from typing import Callable, Dict, Any, Optional\n",
    "import numpy as np\n",
    "\n",
    "# 1. Gene Expression Data Availability\n",
    "# Based on the background information, this appears to be a cell line experiment comparing\n",
    "# Caco-2 and THP-1 cells with various treatments. While it does contain gene expression data,\n",
    "# it's not suitable for our study on human AML patients.\n",
    "is_gene_available = True  # The dataset likely contains gene expression data\n",
    "\n",
    "# 2. Variable Availability and Data Type Conversion\n",
    "# This dataset doesn't contain patient-level clinical data about AML.\n",
    "# It's comparing different cell lines with different treatments.\n",
    "\n",
    "# The dataset doesn't contain usable trait data for our purposes (AML vs non-AML in humans)\n",
    "trait_row = None  # No suitable trait data for human patients\n",
    "\n",
    "# Age data isn't patient age but refers to the original cell line source\n",
    "age_row = None  # No suitable age data for human patients\n",
    "\n",
    "# Sex data doesn't represent individual patients\n",
    "gender_row = None  # No suitable gender data for human patients\n",
    "\n",
    "# No need to define conversion functions since we won't use them\n",
    "\n",
    "# 3. Save Metadata\n",
    "# Since this is a cell line experiment, not patient data, it's not suitable for our study\n",
    "is_trait_available = trait_row is not None  # This will be False\n",
    "\n",
    "# Validate and save cohort information\n",
    "validate_and_save_cohort_info(\n",
    "    is_final=False,\n",
    "    cohort=cohort,\n",
    "    info_path=json_path,\n",
    "    is_gene_available=is_gene_available,\n",
    "    is_trait_available=is_trait_available\n",
    ")\n",
    "\n",
    "# 4. Clinical Feature Extraction\n",
    "# Since trait_row is None, we skip this step\n",
    "if trait_row is not None:\n",
    "    # This code won't execute since trait_row is None\n",
    "    try:\n",
    "        clinical_data = pd.read_csv(f\"{in_cohort_dir}/clinical_data.csv\", index_col=0)\n",
    "        \n",
    "        selected_clinical_df = geo_select_clinical_features(\n",
    "            clinical_df=clinical_data,\n",
    "            trait=trait,\n",
    "            trait_row=trait_row,\n",
    "            convert_trait=lambda x: None,  # Placeholder since we won't use it\n",
    "            age_row=age_row,\n",
    "            convert_age=None,\n",
    "            gender_row=gender_row,\n",
    "            convert_gender=None\n",
    "        )\n",
    "        \n",
    "        # Preview the DataFrame\n",
    "        preview = preview_df(selected_clinical_df)\n",
    "        print(\"Preview of selected clinical features:\")\n",
    "        print(preview)\n",
    "        \n",
    "        # Create output directory if it doesn't exist\n",
    "        os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)\n",
    "        \n",
    "        # Save the DataFrame to CSV\n",
    "        selected_clinical_df.to_csv(out_clinical_data_file)\n",
    "        print(f\"Clinical data saved to {out_clinical_data_file}\")\n",
    "    except Exception as e:\n",
    "        print(f\"Error during clinical feature extraction: {e}\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1e75d79f",
   "metadata": {},
   "source": [
    "### Step 3: Gene Data Extraction"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "292bc8ff",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T06:20:32.486056Z",
     "iopub.status.busy": "2025-03-25T06:20:32.485944Z",
     "iopub.status.idle": "2025-03-25T06:20:32.634408Z",
     "shell.execute_reply": "2025-03-25T06:20:32.634014Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Index(['7892501', '7892502', '7892503', '7892504', '7892505', '7892506',\n",
      "       '7892507', '7892508', '7892509', '7892510', '7892511', '7892512',\n",
      "       '7892513', '7892514', '7892515', '7892516', '7892517', '7892518',\n",
      "       '7892519', '7892520'],\n",
      "      dtype='object', name='ID')\n"
     ]
    }
   ],
   "source": [
    "# 1. Use the get_genetic_data function from the library to get the gene_data from the matrix_file previously defined.\n",
    "gene_data = get_genetic_data(matrix_file)\n",
    "\n",
    "# 2. Print the first 20 row IDs (gene or probe identifiers) for future observation.\n",
    "print(gene_data.index[:20])\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2038a547",
   "metadata": {},
   "source": [
    "### Step 4: Gene Identifier Review"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "4738029d",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T06:20:32.635741Z",
     "iopub.status.busy": "2025-03-25T06:20:32.635618Z",
     "iopub.status.idle": "2025-03-25T06:20:32.637541Z",
     "shell.execute_reply": "2025-03-25T06:20:32.637262Z"
    }
   },
   "outputs": [],
   "source": [
    "# Review of gene identifiers in the gene expression data\n",
    "# The identifiers appear to be numerical codes (like 7892501, 7892502, etc.)\n",
    "# These are likely probe IDs rather than standard human gene symbols\n",
    "# Human gene symbols would be alphanumeric (like BRCA1, TP53, etc.)\n",
    "# Therefore, these identifiers will need to be mapped to gene symbols\n",
    "\n",
    "requires_gene_mapping = True\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eab5473d",
   "metadata": {},
   "source": [
    "### Step 5: Gene Annotation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "d7fa1690",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T06:20:32.638676Z",
     "iopub.status.busy": "2025-03-25T06:20:32.638572Z",
     "iopub.status.idle": "2025-03-25T06:20:35.292996Z",
     "shell.execute_reply": "2025-03-25T06:20:35.292610Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Gene annotation preview:\n",
      "{'ID': ['7896736', '7896738', '7896740', '7896742', '7896744'], 'GB_LIST': [nan, nan, 'NM_001005240,NM_001004195,NM_001005484,BC136848,BC136907', 'BC118988,AL137655', 'NM_001005277,NM_001005221,NM_001005224,NM_001005504,BC137547'], 'SPOT_ID': ['chr1:53049-54936', 'chr1:63015-63887', 'chr1:69091-70008', 'chr1:334129-334296', 'chr1:367659-368597'], 'seqname': ['chr1', 'chr1', 'chr1', 'chr1', 'chr1'], 'RANGE_GB': ['NC_000001.10', 'NC_000001.10', 'NC_000001.10', 'NC_000001.10', 'NC_000001.10'], 'RANGE_STRAND': ['+', '+', '+', '+', '+'], 'RANGE_START': [53049.0, 63015.0, 69091.0, 334129.0, 367659.0], 'RANGE_STOP': [54936.0, 63887.0, 70008.0, 334296.0, 368597.0], 'total_probes': [7.0, 31.0, 24.0, 6.0, 36.0], 'gene_assignment': ['---', '---', 'NM_001005240 // OR4F17 // olfactory receptor, family 4, subfamily F, member 17 // 19p13.3 // 81099 /// NM_001004195 // OR4F4 // olfactory receptor, family 4, subfamily F, member 4 // 15q26.3 // 26682 /// NM_001005484 // OR4F5 // olfactory receptor, family 4, subfamily F, member 5 // 1p36.33 // 79501 /// ENST00000318050 // OR4F17 // olfactory receptor, family 4, subfamily F, member 17 // 19p13.3 // 81099 /// ENST00000335137 // OR4F4 // olfactory receptor, family 4, subfamily F, member 4 // 15q26.3 // 26682 /// ENST00000326183 // OR4F5 // olfactory receptor, family 4, subfamily F, member 5 // 1p36.33 // 79501 /// BC136848 // OR4F17 // olfactory receptor, family 4, subfamily F, member 17 // 19p13.3 // 81099 /// BC136907 // OR4F4 // olfactory receptor, family 4, subfamily F, member 4 // 15q26.3 // 26682 /// ENST00000442916 // OR4F17 // olfactory receptor, family 4, subfamily F, member 17 // 19p13.3 // 81099', 'ENST00000388975 // SEPT14 // septin 14 // 7p11.2 // 346288 /// BC118988 // NCRNA00266 // non-protein coding RNA 266 // --- // 140849 /// AL137655 // LOC100134822 // similar to hCG1739109 // --- // 100134822', 'NM_001005277 // OR4F16 // olfactory receptor, family 4, subfamily F, member 16 // 1p36.33 // 81399 /// NM_001005221 // OR4F29 // olfactory receptor, family 4, subfamily F, member 29 // 1p36.33 // 729759 /// NM_001005224 // OR4F3 // olfactory receptor, family 4, subfamily F, member 3 // 5q35.3 // 26683 /// NM_001005504 // OR4F21 // olfactory receptor, family 4, subfamily F, member 21 // 8p23.3 // 441308 /// ENST00000320901 // OR4F21 // olfactory receptor, family 4, subfamily F, member 21 // 8p23.3 // 441308 /// BC137547 // OR4F3 // olfactory receptor, family 4, subfamily F, member 3 // 5q35.3 // 26683 /// BC137547 // OR4F16 // olfactory receptor, family 4, subfamily F, member 16 // 1p36.33 // 81399 /// BC137547 // OR4F29 // olfactory receptor, family 4, subfamily F, member 29 // 1p36.33 // 729759'], 'mrna_assignment': ['---', 'ENST00000328113 // ENSEMBL // cdna:pseudogene chromosome:GRCh37:15:102467008:102467910:-1 gene:ENSG00000183909 // chr1 // 100 // 100 // 31 // 31 // 0 /// ENST00000318181 // ENSEMBL // cdna:pseudogene chromosome:GRCh37:19:104601:105256:1 gene:ENSG00000176705 // chr1 // 100 // 100 // 31 // 31 // 0 /// ENST00000492842 // ENSEMBL // cdna:pseudogene chromosome:GRCh37:1:62948:63887:1 gene:ENSG00000240361 // chr1 // 100 // 100 // 31 // 31 // 0', 'NM_001005240 // RefSeq // Homo sapiens olfactory receptor, family 4, subfamily F, member 17 (OR4F17), mRNA. // chr1 // 100 // 100 // 24 // 24 // 0 /// NM_001004195 // RefSeq // Homo sapiens olfactory receptor, family 4, subfamily F, member 4 (OR4F4), mRNA. // chr1 // 100 // 100 // 24 // 24 // 0 /// NM_001005484 // RefSeq // Homo sapiens olfactory receptor, family 4, subfamily F, member 5 (OR4F5), mRNA. // chr1 // 100 // 100 // 24 // 24 // 0 /// ENST00000318050 // ENSEMBL // Olfactory receptor 4F17 gene:ENSG00000176695 // chr1 // 100 // 100 // 24 // 24 // 0 /// ENST00000335137 // ENSEMBL // Olfactory receptor 4F4 gene:ENSG00000186092 // chr1 // 100 // 100 // 24 // 24 // 0 /// ENST00000326183 // ENSEMBL // Olfactory receptor 4F5 gene:ENSG00000177693 // chr1 // 100 // 100 // 24 // 24 // 0 /// BC136848 // GenBank // Homo sapiens olfactory receptor, family 4, subfamily F, member 17, mRNA (cDNA clone MGC:168462 IMAGE:9020839), complete cds. // chr1 // 100 // 100 // 24 // 24 // 0 /// BC136907 // GenBank // Homo sapiens olfactory receptor, family 4, subfamily F, member 4, mRNA (cDNA clone MGC:168521 IMAGE:9020898), complete cds. // chr1 // 100 // 100 // 24 // 24 // 0 /// ENST00000442916 // ENSEMBL // OR4F4 (Fragment) gene:ENSG00000176695 // chr1 // 100 // 88 // 21 // 21 // 0', 'ENST00000388975 // ENSEMBL // Septin-14 gene:ENSG00000154997 // chr1 // 50 // 100 // 3 // 6 // 0 /// BC118988 // GenBank // Homo sapiens chromosome 20 open reading frame 69, mRNA (cDNA clone MGC:141807 IMAGE:40035995), complete cds. // chr1 // 100 // 100 // 6 // 6 // 0 /// AL137655 // GenBank // Homo sapiens mRNA; cDNA DKFZp434B2016 (from clone DKFZp434B2016). // chr1 // 100 // 100 // 6 // 6 // 0 /// ENST00000428915 // ENSEMBL // cdna:known chromosome:GRCh37:10:38742109:38755311:1 gene:ENSG00000203496 // chr1 // 100 // 100 // 6 // 6 // 0 /// ENST00000455207 // ENSEMBL // cdna:known chromosome:GRCh37:1:334129:446155:1 gene:ENSG00000224813 // chr1 // 100 // 100 // 6 // 6 // 0 /// ENST00000455464 // ENSEMBL // cdna:known chromosome:GRCh37:1:334140:342806:1 gene:ENSG00000224813 // chr1 // 100 // 100 // 6 // 6 // 0 /// ENST00000440200 // ENSEMBL // cdna:known chromosome:GRCh37:1:536816:655580:-1 gene:ENSG00000230021 // chr1 // 100 // 100 // 6 // 6 // 0 /// ENST00000279067 // ENSEMBL // cdna:known chromosome:GRCh37:20:62921738:62934912:1 gene:ENSG00000149656 // chr1 // 100 // 100 // 6 // 6 // 0 /// ENST00000499986 // ENSEMBL // cdna:known chromosome:GRCh37:5:180717576:180761371:1 gene:ENSG00000248628 // chr1 // 100 // 100 // 6 // 6 // 0 /// ENST00000436899 // ENSEMBL // cdna:known chromosome:GRCh37:6:131910:144885:-1 gene:ENSG00000170590 // chr1 // 100 // 100 // 6 // 6 // 0 /// ENST00000432557 // ENSEMBL // cdna:known chromosome:GRCh37:8:132324:150572:-1 gene:ENSG00000250210 // chr1 // 100 // 100 // 6 // 6 // 0 /// ENST00000523795 // ENSEMBL // cdna:known chromosome:GRCh37:8:141690:150563:-1 gene:ENSG00000250210 // chr1 // 100 // 100 // 6 // 6 // 0 /// ENST00000490482 // ENSEMBL // cdna:known chromosome:GRCh37:8:149942:163324:-1 gene:ENSG00000223508 // chr1 // 100 // 100 // 6 // 6 // 0 /// ENST00000307499 // ENSEMBL // cdna:known supercontig::GL000227.1:57780:70752:-1 gene:ENSG00000229450 // chr1 // 100 // 100 // 6 // 6 // 0 /// ENST00000441245 // ENSEMBL // cdna:known chromosome:GRCh37:1:637316:655530:-1 gene:ENSG00000230021 // chr1 // 100 // 67 // 4 // 4 // 0 /// ENST00000425473 // ENSEMBL // cdna:known chromosome:GRCh37:20:62926294:62944485:1 gene:ENSG00000149656 // chr1 // 100 // 67 // 4 // 4 // 0 /// ENST00000471248 // ENSEMBL // cdna:known chromosome:GRCh37:1:110953:129173:-1 gene:ENSG00000238009 // chr1 // 75 // 67 // 3 // 4 // 0', 'NM_001005277 // RefSeq // Homo sapiens olfactory receptor, family 4, subfamily F, member 16 (OR4F16), mRNA. // chr1 // 100 // 100 // 36 // 36 // 0 /// NM_001005221 // RefSeq // Homo sapiens olfactory receptor, family 4, subfamily F, member 29 (OR4F29), mRNA. // chr1 // 100 // 100 // 36 // 36 // 0 /// NM_001005224 // RefSeq // Homo sapiens olfactory receptor, family 4, subfamily F, member 3 (OR4F3), mRNA. // chr1 // 100 // 100 // 36 // 36 // 0 /// NM_001005504 // RefSeq // Homo sapiens olfactory receptor, family 4, subfamily F, member 21 (OR4F21), mRNA. // chr1 // 89 // 100 // 32 // 36 // 0 /// ENST00000320901 // ENSEMBL // Olfactory receptor 4F21 gene:ENSG00000176269 // chr1 // 89 // 100 // 32 // 36 // 0 /// BC137547 // GenBank // Homo sapiens olfactory receptor, family 4, subfamily F, member 3, mRNA (cDNA clone MGC:169170 IMAGE:9021547), complete cds. // chr1 // 100 // 100 // 36 // 36 // 0 /// ENST00000426406 // ENSEMBL // cdna:known chromosome:GRCh37:1:367640:368634:1 gene:ENSG00000235249 // chr1 // 100 // 100 // 36 // 36 // 0 /// ENST00000332831 // ENSEMBL // cdna:known chromosome:GRCh37:1:621096:622034:-1 gene:ENSG00000185097 // chr1 // 100 // 100 // 36 // 36 // 0 /// ENST00000456475 // ENSEMBL // cdna:known chromosome:GRCh37:5:180794269:180795263:1 gene:ENSG00000230178 // chr1 // 100 // 100 // 36 // 36 // 0 /// ENST00000521196 // ENSEMBL // cdna:known chromosome:GRCh37:11:86612:87605:-1 gene:ENSG00000224777 // chr1 // 78 // 100 // 28 // 36 // 0'], 'category': ['---', 'main', 'main', 'main', 'main']}\n"
     ]
    }
   ],
   "source": [
    "# 1. Use the 'get_gene_annotation' function from the library to get gene annotation data from the SOFT file.\n",
    "gene_annotation = get_gene_annotation(soft_file)\n",
    "\n",
    "# 2. Use the 'preview_df' function from the library to preview the data and print out the results.\n",
    "print(\"Gene annotation preview:\")\n",
    "print(preview_df(gene_annotation))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fce6b800",
   "metadata": {},
   "source": [
    "### Step 6: Gene Identifier Mapping"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "b963275e",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T06:20:35.294415Z",
     "iopub.status.busy": "2025-03-25T06:20:35.294287Z",
     "iopub.status.idle": "2025-03-25T06:20:36.480067Z",
     "shell.execute_reply": "2025-03-25T06:20:36.479665Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Gene mapping sample (first 5 rows):\n",
      "        ID                                               Gene\n",
      "0  7896736                                                ---\n",
      "1  7896738                                                ---\n",
      "2  7896740  NM_001005240 // OR4F17 // olfactory receptor, ...\n",
      "3  7896742  ENST00000388975 // SEPT14 // septin 14 // 7p11...\n",
      "4  7896744  NM_001005277 // OR4F16 // olfactory receptor, ...\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Gene expression data shape after mapping: (56391, 48)\n",
      "\n",
      "First 5 genes and expression values:\n",
      "      GSM2648543  GSM2648544  GSM2648545  GSM2648546  GSM2648547  GSM2648548  \\\n",
      "Gene                                                                           \n",
      "A-     38.716029   37.128762   37.511108   37.623609   38.177840   38.209748   \n",
      "A-2     2.658533    2.637381    2.639023    2.650405    2.663684    2.659015   \n",
      "A-52    3.651613    3.645483    3.609023    3.648133    3.642320    3.644010   \n",
      "A-E     0.482038    0.478818    0.496260    0.467780    0.487119    0.477848   \n",
      "A-I    11.601787   11.668377   11.653517   11.683830   11.639600   11.745367   \n",
      "\n",
      "      GSM2648549  GSM2648550  GSM2648551  GSM2648552  ...  GSM2648581  \\\n",
      "Gene                                                  ...               \n",
      "A-     37.516400   37.700195   38.048053   38.039465  ...   34.037491   \n",
      "A-2     2.636998    2.577943    2.640939    2.644001  ...    2.159168   \n",
      "A-52    3.653870    3.661173    3.648297    3.637170  ...    3.873297   \n",
      "A-E     0.485932    0.487208    0.481296    0.490885  ...    0.465458   \n",
      "A-I    11.681123   11.744437   11.692613   11.750230  ...    7.384977   \n",
      "\n",
      "      GSM2648582  GSM2648583  GSM2648584  GSM2648585  GSM2648586  GSM2648587  \\\n",
      "Gene                                                                           \n",
      "A-     34.163132   34.643837   33.520664   34.734178   34.441331   34.438711   \n",
      "A-2     2.225617    2.162100    2.881895    2.353087    2.260880    2.632220   \n",
      "A-52    3.872083    3.866130    3.837077    3.851257    3.880483    3.855293   \n",
      "A-E     0.467714    0.473606    0.462785    0.457551    0.463859    0.457299   \n",
      "A-I     7.316712    7.451579    7.439498    7.377272    7.488927    7.351624   \n",
      "\n",
      "      GSM2648588  GSM2648589  GSM2648590  \n",
      "Gene                                      \n",
      "A-     35.021535   34.479051   34.669246  \n",
      "A-2     2.324638    2.213350    2.181451  \n",
      "A-52    3.873340    3.851763    3.841573  \n",
      "A-E     0.463503    0.469056    0.461046  \n",
      "A-I     7.375039    7.332966    7.421468  \n",
      "\n",
      "[5 rows x 48 columns]\n",
      "\n",
      "Gene expression data shape after normalization: (20124, 48)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Gene expression data saved to ../../output/preprocess/Acute_Myeloid_Leukemia/gene_data/GSE99612.csv\n"
     ]
    }
   ],
   "source": [
    "# 1. Identify the columns for probe IDs and gene symbols\n",
    "probe_col = 'ID'  # This contains the probe identifiers like '7896736'\n",
    "gene_col = 'gene_assignment'  # This contains gene symbol information\n",
    "\n",
    "# 2. Get gene mapping using the function from the library\n",
    "mapping_df = get_gene_mapping(gene_annotation, probe_col, gene_col)\n",
    "\n",
    "# Print a sample of the mapping to verify structure\n",
    "print(\"Gene mapping sample (first 5 rows):\")\n",
    "print(mapping_df.head())\n",
    "\n",
    "# 3. Apply gene mapping to convert probe-level measurements to gene-level expression data\n",
    "gene_data = apply_gene_mapping(gene_data, mapping_df)\n",
    "\n",
    "# Print the shape and first few rows of the resulting gene expression dataframe\n",
    "print(\"\\nGene expression data shape after mapping:\", gene_data.shape)\n",
    "print(\"\\nFirst 5 genes and expression values:\")\n",
    "print(gene_data.head())\n",
    "\n",
    "# Normalize gene symbols to ensure consistency and handle synonyms\n",
    "gene_data = normalize_gene_symbols_in_index(gene_data)\n",
    "\n",
    "# Print the shape after normalization\n",
    "print(\"\\nGene expression data shape after normalization:\", gene_data.shape)\n",
    "\n",
    "# Save gene data to output file\n",
    "os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True)\n",
    "gene_data.to_csv(out_gene_data_file)\n",
    "print(f\"\\nGene expression data saved to {out_gene_data_file}\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "85a7e1d8",
   "metadata": {},
   "source": [
    "### Step 7: Data Normalization and Linking"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "59b46b74",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T06:20:36.481494Z",
     "iopub.status.busy": "2025-03-25T06:20:36.481362Z",
     "iopub.status.idle": "2025-03-25T06:20:36.571252Z",
     "shell.execute_reply": "2025-03-25T06:20:36.570942Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Abnormality detected in the cohort: GSE99612. Preprocessing failed.\n",
      "Dataset correctly identified as not usable for human AML trait-gene association study.\n"
     ]
    }
   ],
   "source": [
    "# 1. Gene data was already normalized and saved in Step 6\n",
    "# No need to normalize again as it was done in Step 6\n",
    "\n",
    "# 2-6. Since there's no clinical data available for this dataset (trait_row was None in Step 2),\n",
    "# we can't link clinical and genetic data\n",
    "# Instead, we should finalize the cohort information to reflect this limitation\n",
    "\n",
    "# Get a small sample of the normalized gene data for the validation function\n",
    "if 'normalized_gene_data' not in locals():\n",
    "    # Load the saved gene data if not already in memory\n",
    "    try:\n",
    "        normalized_gene_data = pd.read_csv(out_gene_data_file, index_col=0)\n",
    "    except:\n",
    "        normalized_gene_data = gene_data  # Use the gene_data from previous step if file not found\n",
    "\n",
    "# Create a minimal dataframe with the gene data structure and add a dummy trait column\n",
    "minimal_df = pd.DataFrame(index=normalized_gene_data.columns)\n",
    "minimal_df[trait] = None  # Add trait column with null values\n",
    "\n",
    "# Note for the validation function explaining why this dataset isn't usable\n",
    "note = \"This dataset contains gene expression from cell lines (Caco-2 and THP-1) with various treatments, not human patient data for AML studies.\"\n",
    "\n",
    "# Final validation - mark as not usable for trait analysis\n",
    "is_trait_available = False\n",
    "is_gene_available = True\n",
    "is_biased = False  # Explicitly set to False since there's no trait data to evaluate bias\n",
    "\n",
    "is_usable = validate_and_save_cohort_info(\n",
    "    is_final=True,\n",
    "    cohort=cohort,\n",
    "    info_path=json_path,\n",
    "    is_gene_available=is_gene_available,\n",
    "    is_trait_available=is_trait_available,\n",
    "    is_biased=is_biased,\n",
    "    df=minimal_df,\n",
    "    note=note\n",
    ")\n",
    "\n",
    "# We do not save linked_data to out_data_file because this dataset is not usable for the study\n",
    "if is_usable:\n",
    "    print(\"WARNING: This dataset was unexpectedly marked as usable, which conflicts with previous findings.\")\n",
    "else:\n",
    "    print(\"Dataset correctly identified as not usable for human AML trait-gene association study.\")"
   ]
  }
 ],
 "metadata": {
  "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.10.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}