File size: 21,710 Bytes
f88156f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a276f199",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T08:14:25.391379Z",
     "iopub.status.busy": "2025-03-25T08:14:25.391278Z",
     "iopub.status.idle": "2025-03-25T08:14:25.548221Z",
     "shell.execute_reply": "2025-03-25T08:14:25.547879Z"
    }
   },
   "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 = \"Cervical_Cancer\"\n",
    "cohort = \"GSE163114\"\n",
    "\n",
    "# Input paths\n",
    "in_trait_dir = \"../../input/GEO/Cervical_Cancer\"\n",
    "in_cohort_dir = \"../../input/GEO/Cervical_Cancer/GSE163114\"\n",
    "\n",
    "# Output paths\n",
    "out_data_file = \"../../output/preprocess/Cervical_Cancer/GSE163114.csv\"\n",
    "out_gene_data_file = \"../../output/preprocess/Cervical_Cancer/gene_data/GSE163114.csv\"\n",
    "out_clinical_data_file = \"../../output/preprocess/Cervical_Cancer/clinical_data/GSE163114.csv\"\n",
    "json_path = \"../../output/preprocess/Cervical_Cancer/cohort_info.json\"\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "81317870",
   "metadata": {},
   "source": [
    "### Step 1: Initial Data Loading"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "c874ddcf",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T08:14:25.549640Z",
     "iopub.status.busy": "2025-03-25T08:14:25.549501Z",
     "iopub.status.idle": "2025-03-25T08:14:25.645536Z",
     "shell.execute_reply": "2025-03-25T08:14:25.645246Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Background Information:\n",
      "!Series_title\t\"Ki-67 promotes carcinogenesis by enabling global transcriptional programmes\"\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: HeLa'], 1: ['lentivirus: shRNA control', 'lentivirus: shRNA Ki-67']}\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": "ac59173d",
   "metadata": {},
   "source": [
    "### Step 2: Dataset Analysis and Clinical Feature Extraction"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "b05204a7",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T08:14:25.646617Z",
     "iopub.status.busy": "2025-03-25T08:14:25.646513Z",
     "iopub.status.idle": "2025-03-25T08:14:25.651240Z",
     "shell.execute_reply": "2025-03-25T08:14:25.650921Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Clinical data file not found at ../../input/GEO/Cervical_Cancer/GSE163114/clinical_data.csv\n",
      "This dataset may require manual inspection.\n"
     ]
    }
   ],
   "source": [
    "# 1. Gene Expression Data Availability\n",
    "# The dataset appears to be from a HeLa cell line with shRNA experiments\n",
    "# As a SuperSeries with SubSeries, it likely contains gene expression data\n",
    "is_gene_available = True\n",
    "\n",
    "# 2. Variable Availability and Data Type Conversion\n",
    "# 2.1 Data Availability\n",
    "# For trait: Looking at row 1, it shows two different treatments (shRNA control vs shRNA Ki-67)\n",
    "# This is the experimental condition and should be our trait of interest\n",
    "trait_row = 1\n",
    "\n",
    "# Age and gender: Not available in this cell line dataset\n",
    "age_row = None\n",
    "gender_row = None\n",
    "\n",
    "# 2.2 Data Type Conversion Functions\n",
    "def convert_trait(value):\n",
    "    \"\"\"\n",
    "    Convert shRNA treatment information to a binary indicator.\n",
    "    shRNA control = 0, shRNA Ki-67 = 1\n",
    "    \"\"\"\n",
    "    if value is None:\n",
    "        return None\n",
    "    \n",
    "    # Extract the value after the colon if present\n",
    "    if ':' in value:\n",
    "        value = value.split(':', 1)[1].strip()\n",
    "    \n",
    "    value = value.lower()\n",
    "    if 'control' in value:\n",
    "        return 0\n",
    "    elif 'ki-67' in value:\n",
    "        return 1\n",
    "    else:\n",
    "        return None\n",
    "\n",
    "def convert_age(value):\n",
    "    # Not applicable for this dataset\n",
    "    return None\n",
    "\n",
    "def convert_gender(value):\n",
    "    # Not applicable for this dataset\n",
    "    return None\n",
    "\n",
    "# 3. Save Metadata\n",
    "is_trait_available = trait_row is not None\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",
    "if trait_row is not None:\n",
    "    try:\n",
    "        # For GEO data, we need to load the clinical data file that was created in a previous step\n",
    "        # Attempt to find the clinical data file in the expected location\n",
    "        clinical_data_path = os.path.join(in_cohort_dir, \"clinical_data.csv\")\n",
    "        \n",
    "        if os.path.exists(clinical_data_path):\n",
    "            clinical_data = pd.read_csv(clinical_data_path)\n",
    "            \n",
    "            # Extract clinical features\n",
    "            selected_clinical_df = geo_select_clinical_features(\n",
    "                clinical_df=clinical_data,\n",
    "                trait=trait,\n",
    "                trait_row=trait_row,\n",
    "                convert_trait=convert_trait,\n",
    "                age_row=age_row,\n",
    "                convert_age=convert_age,\n",
    "                gender_row=gender_row,\n",
    "                convert_gender=convert_gender\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",
    "            # Ensure the output directory exists\n",
    "            os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)\n",
    "            \n",
    "            # Save to CSV\n",
    "            selected_clinical_df.to_csv(out_clinical_data_file, index=False)\n",
    "            print(f\"Clinical data saved to {out_clinical_data_file}\")\n",
    "        else:\n",
    "            print(f\"Clinical data file not found at {clinical_data_path}\")\n",
    "            print(\"This dataset may require manual inspection.\")\n",
    "    except Exception as e:\n",
    "        print(f\"Error processing clinical data: {e}\")\n",
    "        print(\"This dataset may require special handling.\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f28d5bed",
   "metadata": {},
   "source": [
    "### Step 3: Gene Data Extraction"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "3485509b",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T08:14:25.652420Z",
     "iopub.status.busy": "2025-03-25T08:14:25.652319Z",
     "iopub.status.idle": "2025-03-25T08:14:25.753806Z",
     "shell.execute_reply": "2025-03-25T08:14:25.753429Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Index(['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13',\n",
      "       '14', '15', '16', '17', '18', '19', '20'],\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": "c1ee14a2",
   "metadata": {},
   "source": [
    "### Step 4: Gene Identifier Review"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "0e3585b8",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T08:14:25.755100Z",
     "iopub.status.busy": "2025-03-25T08:14:25.754989Z",
     "iopub.status.idle": "2025-03-25T08:14:25.756818Z",
     "shell.execute_reply": "2025-03-25T08:14:25.756549Z"
    }
   },
   "outputs": [],
   "source": [
    "# The index values ['1', '2', '3', '4', ...] appear to be simple numeric identifiers,\n",
    "# not standard human gene symbols which typically follow formats like \"TP53\", \"BRCA1\", etc.\n",
    "# These are likely probe IDs or some other platform-specific identifiers that need to be\n",
    "# mapped to proper gene symbols for biological interpretation.\n",
    "\n",
    "# Based on my biomedical knowledge, these are not human gene symbols and require mapping\n",
    "\n",
    "requires_gene_mapping = True\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "10ab462a",
   "metadata": {},
   "source": [
    "### Step 5: Gene Annotation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "c98399e9",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T08:14:25.757902Z",
     "iopub.status.busy": "2025-03-25T08:14:25.757806Z",
     "iopub.status.idle": "2025-03-25T08:14:27.521243Z",
     "shell.execute_reply": "2025-03-25T08:14:27.520870Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Gene annotation preview:\n",
      "{'ID': ['1', '2', '3', '4', '5'], 'COL': ['192', '192', '192', '192', '192'], 'ROW': [328.0, 326.0, 324.0, 322.0, 320.0], 'NAME': ['GE_BrightCorner', 'DarkCorner', 'DarkCorner', 'A_23_P117082', 'A_33_P3246448'], 'SPOT_ID': ['CONTROL', 'CONTROL', 'CONTROL', 'A_23_P117082', 'A_33_P3246448'], 'CONTROL_TYPE': ['pos', 'pos', 'pos', 'FALSE', 'FALSE'], 'REFSEQ': [nan, nan, nan, 'NM_015987', 'NM_080671'], 'GB_ACC': [nan, nan, nan, 'NM_015987', 'NM_080671'], 'LOCUSLINK_ID': [nan, nan, nan, 50865.0, 23704.0], 'GENE_SYMBOL': [nan, nan, nan, 'HEBP1', 'KCNE4'], 'GENE_NAME': [nan, nan, nan, 'heme binding protein 1', 'potassium voltage-gated channel, Isk-related family, member 4'], 'UNIGENE_ID': [nan, nan, nan, 'Hs.642618', 'Hs.348522'], 'ENSEMBL_ID': [nan, nan, nan, 'ENST00000014930', 'ENST00000281830'], 'ACCESSION_STRING': [nan, nan, nan, 'ref|NM_015987|ens|ENST00000014930|gb|AF117615|gb|BC016277', 'ref|NM_080671|ens|ENST00000281830|tc|THC2655788'], 'CHROMOSOMAL_LOCATION': [nan, nan, nan, 'chr12:13127906-13127847', 'chr2:223920197-223920256'], 'CYTOBAND': [nan, nan, nan, 'hs|12p13.1', 'hs|2q36.1'], 'DESCRIPTION': [nan, nan, nan, 'Homo sapiens heme binding protein 1 (HEBP1), mRNA [NM_015987]', 'Homo sapiens potassium voltage-gated channel, Isk-related family, member 4 (KCNE4), mRNA [NM_080671]'], 'GO_ID': [nan, nan, nan, 'GO:0005488(binding)|GO:0005576(extracellular region)|GO:0005737(cytoplasm)|GO:0005739(mitochondrion)|GO:0005829(cytosol)|GO:0007623(circadian rhythm)|GO:0020037(heme binding)', 'GO:0005244(voltage-gated ion channel activity)|GO:0005249(voltage-gated potassium channel activity)|GO:0006811(ion transport)|GO:0006813(potassium ion transport)|GO:0016020(membrane)|GO:0016021(integral to membrane)|GO:0016324(apical plasma membrane)'], 'SEQUENCE': [nan, nan, nan, 'AAGGGGGAAAATGTGATTTGTGCCTGATCTTTCATCTGTGATTCTTATAAGAGCTTTGTC', 'GCAAGTCTCTCTGCACCTATTAAAAAGTGATGTATATACTTCCTTCTTATTCTGTTGAGT']}\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": "8dcf59c3",
   "metadata": {},
   "source": [
    "### Step 6: Gene Identifier Mapping"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "731cf145",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T08:14:27.522691Z",
     "iopub.status.busy": "2025-03-25T08:14:27.522479Z",
     "iopub.status.idle": "2025-03-25T08:14:27.669376Z",
     "shell.execute_reply": "2025-03-25T08:14:27.669009Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Gene expression data after mapping (first 5 rows):\n",
      "          GSM4950455  GSM4950456  GSM4950457  GSM4950458  GSM4950459  \\\n",
      "Gene                                                                   \n",
      "A1BG      132.083288  129.833098   95.850230  100.615695  124.773355   \n",
      "A1BG-AS1   16.155110   14.861680    4.716991   11.106840    8.644419   \n",
      "A1CF      212.139998  173.847205   54.092575  156.444192  106.723514   \n",
      "A2LD1     244.871530  180.816300  109.013836  173.861860  247.707170   \n",
      "A2M       133.594765  128.883865   62.684670  123.532323   81.277947   \n",
      "\n",
      "          GSM4950460  GSM4950461  GSM4950462  GSM4950463  GSM4950464  \\\n",
      "Gene                                                                   \n",
      "A1BG      109.751668  108.931347   92.967957  127.500770  106.803252   \n",
      "A1BG-AS1    4.350618   10.533510    7.061245   13.553190   10.343080   \n",
      "A1CF       76.701249  157.344856  122.121485  154.139703  196.274230   \n",
      "A2LD1     145.602781  206.909400  220.527440  229.859650  200.153210   \n",
      "A2M        55.676736  104.865621   83.508082  146.794847  133.055046   \n",
      "\n",
      "          GSM4950465  GSM4950466  \n",
      "Gene                              \n",
      "A1BG       94.078066  122.938080  \n",
      "A1BG-AS1   13.383460   15.715330  \n",
      "A1CF      105.685073  173.692618  \n",
      "A2LD1     177.095571  185.686890  \n",
      "A2M        66.040354  151.910271  \n"
     ]
    }
   ],
   "source": [
    "# 1. Based on the output, we need to identify which columns to use for gene mapping\n",
    "# Looking at the gene annotation preview, we can see:\n",
    "# - 'ID' contains the numeric identifiers matching the gene expression data index\n",
    "# - 'GENE_SYMBOL' contains the actual gene symbols (e.g., HEBP1, KCNE4)\n",
    "\n",
    "# 2. Extract the gene mapping dataframe from gene annotation\n",
    "gene_mapping = get_gene_mapping(\n",
    "    annotation=gene_annotation,\n",
    "    prob_col='ID',  # Gene identifiers column\n",
    "    gene_col='GENE_SYMBOL'  # Gene symbols column\n",
    ")\n",
    "\n",
    "# 3. Convert probe-level measurements to gene expression data by applying gene mapping\n",
    "gene_data = apply_gene_mapping(\n",
    "    expression_df=gene_data,\n",
    "    mapping_df=gene_mapping\n",
    ")\n",
    "\n",
    "# Print first few rows of the gene data after mapping to verify\n",
    "print(\"Gene expression data after mapping (first 5 rows):\")\n",
    "print(gene_data.head(5))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b5eaf74d",
   "metadata": {},
   "source": [
    "### Step 7: Data Normalization and Linking"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "923e2e1d",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T08:14:27.670752Z",
     "iopub.status.busy": "2025-03-25T08:14:27.670646Z",
     "iopub.status.idle": "2025-03-25T08:14:34.767139Z",
     "shell.execute_reply": "2025-03-25T08:14:34.766559Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Normalized gene data saved to ../../output/preprocess/Cervical_Cancer/gene_data/GSE163114.csv\n",
      "Clinical data saved to ../../output/preprocess/Cervical_Cancer/clinical_data/GSE163114.csv\n",
      "Linked data shape: (12, 19848)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "For the feature 'Cervical_Cancer', the least common label is '0.0' with 6 occurrences. This represents 50.00% of the dataset.\n",
      "The distribution of the feature 'Cervical_Cancer' in this dataset is fine.\n",
      "\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Linked data saved to ../../output/preprocess/Cervical_Cancer/GSE163114.csv\n"
     ]
    }
   ],
   "source": [
    "# 1. Normalize the obtained gene data with the 'normalize_gene_symbols_in_index' function from the library.\n",
    "normalized_gene_data = normalize_gene_symbols_in_index(gene_data)\n",
    "os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True)\n",
    "normalized_gene_data.to_csv(out_gene_data_file)\n",
    "print(f\"Normalized gene data saved to {out_gene_data_file}\")\n",
    "\n",
    "# Extract clinical features directly from the matrix file data (reuse the data from previous steps)\n",
    "background_info, clinical_data = get_background_and_clinical_data(matrix_file)\n",
    "\n",
    "# Use the correct trait conversion function from Step 2\n",
    "def convert_trait(value):\n",
    "    \"\"\"\n",
    "    Convert shRNA treatment information to a binary indicator.\n",
    "    shRNA control = 0, shRNA Ki-67 = 1\n",
    "    \"\"\"\n",
    "    if value is None:\n",
    "        return None\n",
    "    \n",
    "    # Extract the value after the colon if present\n",
    "    if ':' in value:\n",
    "        value = value.split(':', 1)[1].strip()\n",
    "    \n",
    "    value = value.lower()\n",
    "    if 'control' in value:\n",
    "        return 0\n",
    "    elif 'ki-67' in value:\n",
    "        return 1\n",
    "    else:\n",
    "        return None\n",
    "\n",
    "# Extract clinical features using the trait_row identified in Step 2\n",
    "selected_clinical_df = geo_select_clinical_features(\n",
    "    clinical_df=clinical_data,\n",
    "    trait=trait,\n",
    "    trait_row=1,  # Using row 1 which contains shRNA treatment information\n",
    "    convert_trait=convert_trait\n",
    ")\n",
    "\n",
    "# Save clinical data\n",
    "os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)\n",
    "selected_clinical_df.to_csv(out_clinical_data_file)\n",
    "print(f\"Clinical data saved to {out_clinical_data_file}\")\n",
    "\n",
    "# Now link the clinical and genetic data\n",
    "linked_data = geo_link_clinical_genetic_data(selected_clinical_df, normalized_gene_data)\n",
    "print(\"Linked data shape:\", linked_data.shape)\n",
    "\n",
    "# Handle missing values in the linked data\n",
    "linked_data = handle_missing_values(linked_data, trait)\n",
    "\n",
    "# 4. Determine whether the trait and some demographic features are severely biased, and remove biased features.\n",
    "is_trait_biased, unbiased_linked_data = judge_and_remove_biased_features(linked_data, trait)\n",
    "\n",
    "# 5. Conduct quality check and save the cohort information.\n",
    "is_usable = validate_and_save_cohort_info(\n",
    "    is_final=True, \n",
    "    cohort=cohort, \n",
    "    info_path=json_path, \n",
    "    is_gene_available=True, \n",
    "    is_trait_available=True, \n",
    "    is_biased=is_trait_biased, \n",
    "    df=unbiased_linked_data,\n",
    "    note=\"HeLa cell line samples treated with shRNA control vs. shRNA Ki-67 knockdown.\"\n",
    ")\n",
    "\n",
    "# 6. If the linked data is usable, save it as a CSV file to 'out_data_file'.\n",
    "if is_usable:\n",
    "    os.makedirs(os.path.dirname(out_data_file), exist_ok=True)\n",
    "    unbiased_linked_data.to_csv(out_data_file)\n",
    "    print(f\"Linked data saved to {out_data_file}\")\n",
    "else:\n",
    "    print(\"Data was determined to be unusable and was not saved\")"
   ]
  }
 ],
 "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
}