File size: 35,680 Bytes
e4183cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a4104299",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T08:36:11.101929Z",
     "iopub.status.busy": "2025-03-25T08:36:11.101699Z",
     "iopub.status.idle": "2025-03-25T08:36:11.267036Z",
     "shell.execute_reply": "2025-03-25T08:36:11.266645Z"
    }
   },
   "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 = \"Cystic_Fibrosis\"\n",
    "cohort = \"GSE67698\"\n",
    "\n",
    "# Input paths\n",
    "in_trait_dir = \"../../input/GEO/Cystic_Fibrosis\"\n",
    "in_cohort_dir = \"../../input/GEO/Cystic_Fibrosis/GSE67698\"\n",
    "\n",
    "# Output paths\n",
    "out_data_file = \"../../output/preprocess/Cystic_Fibrosis/GSE67698.csv\"\n",
    "out_gene_data_file = \"../../output/preprocess/Cystic_Fibrosis/gene_data/GSE67698.csv\"\n",
    "out_clinical_data_file = \"../../output/preprocess/Cystic_Fibrosis/clinical_data/GSE67698.csv\"\n",
    "json_path = \"../../output/preprocess/Cystic_Fibrosis/cohort_info.json\"\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b6f70ad0",
   "metadata": {},
   "source": [
    "### Step 1: Initial Data Loading"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "8ad100c8",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T08:36:11.268275Z",
     "iopub.status.busy": "2025-03-25T08:36:11.268127Z",
     "iopub.status.idle": "2025-03-25T08:36:11.458766Z",
     "shell.execute_reply": "2025-03-25T08:36:11.458203Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Background Information:\n",
      "!Series_title\t\"Transcriptional profiling of CFBE41o-ΔF508 cells and CFBE41o−CFTR cells.\"\n",
      "!Series_summary\t\"Cystic fibrosis bronchial epithelial (CFBE41o-ΔF508) cells subjected to 23 bio-active small molecules including vehicle controls, at low temperature and untreated cells. Untreated Cystic fibrosis bronchial epithelial cells (CFBE41o−CFTR) are also included.\"\n",
      "!Series_overall_design\t\"Two-colors Dye-swap, two or three biological replicates with two technical replicates each and DMSO (vehicle control) were included in each hybridization batch.\"\n",
      "Sample Characteristics Dictionary:\n",
      "{0: ['cell line: polarized CFBE41o-cell line'], 1: ['transduction: TranzVector lentivectors containing deltaF508 CFTR (CFBE41o-deltaF508CFTR)', 'transduction: TranzVector lentivectors containing wildtype CFTR (CFBE41o-CFTR)']}\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": "9daf3bea",
   "metadata": {},
   "source": [
    "### Step 2: Dataset Analysis and Clinical Feature Extraction"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "d5e192f3",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T08:36:11.460517Z",
     "iopub.status.busy": "2025-03-25T08:36:11.460407Z",
     "iopub.status.idle": "2025-03-25T08:36:11.488249Z",
     "shell.execute_reply": "2025-03-25T08:36:11.487750Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Preview of selected clinical features:\n",
      "{'GSM1946634': [1.0], 'GSM1946635': [1.0], 'GSM1946636': [1.0], 'GSM1946637': [1.0], 'GSM1946638': [1.0], 'GSM1946639': [1.0], 'GSM1946640': [1.0], 'GSM1946641': [1.0], 'GSM1946642': [1.0], 'GSM1946643': [1.0], 'GSM1946644': [1.0], 'GSM1946645': [1.0], 'GSM1946646': [1.0], 'GSM1946647': [1.0], 'GSM1946648': [1.0], 'GSM1946649': [1.0], 'GSM1946650': [1.0], 'GSM1946651': [1.0], 'GSM1946652': [1.0], 'GSM1946653': [1.0], 'GSM1946654': [1.0], 'GSM1946655': [1.0], 'GSM1946656': [1.0], 'GSM1946657': [1.0], 'GSM1946658': [1.0], 'GSM1946659': [1.0], 'GSM1946660': [1.0], 'GSM1946661': [1.0], 'GSM1946662': [1.0], 'GSM1946663': [1.0], 'GSM1946664': [1.0], 'GSM1946665': [1.0], 'GSM1946666': [1.0], 'GSM1946667': [1.0], 'GSM1946668': [1.0], 'GSM1946669': [1.0], 'GSM1946670': [1.0], 'GSM1946671': [1.0], 'GSM1946672': [1.0], 'GSM1946673': [1.0], 'GSM1946674': [1.0], 'GSM1946675': [1.0], 'GSM1946676': [1.0], 'GSM1946677': [1.0], 'GSM1946678': [1.0], 'GSM1946679': [1.0], 'GSM1946680': [1.0], 'GSM1946681': [1.0], 'GSM1946682': [1.0], 'GSM1946683': [1.0], 'GSM1946684': [1.0], 'GSM1946685': [1.0], 'GSM1946686': [0.0], 'GSM1946687': [0.0], 'GSM1946688': [0.0], 'GSM1946689': [0.0], 'GSM1946690': [1.0], 'GSM1946691': [1.0], 'GSM1946692': [1.0], 'GSM1946693': [1.0], 'GSM1946694': [1.0], 'GSM1946695': [1.0], 'GSM1946696': [1.0], 'GSM1946697': [1.0], 'GSM1946698': [1.0], 'GSM1946699': [1.0], 'GSM1946700': [1.0], 'GSM1946701': [1.0], 'GSM1946702': [1.0], 'GSM1946703': [1.0], 'GSM1946704': [1.0], 'GSM1946705': [1.0], 'GSM1946706': [1.0], 'GSM1946707': [1.0], 'GSM1946708': [1.0], 'GSM1946709': [1.0], 'GSM1946710': [1.0], 'GSM1946711': [1.0], 'GSM1946712': [1.0], 'GSM1946713': [1.0], 'GSM1946714': [1.0], 'GSM1946715': [1.0], 'GSM1946716': [1.0], 'GSM1946717': [1.0], 'GSM1946718': [1.0], 'GSM1946719': [1.0], 'GSM1946720': [1.0], 'GSM1946721': [1.0], 'GSM1946722': [1.0], 'GSM1946723': [1.0], 'GSM1946724': [1.0], 'GSM1946725': [1.0], 'GSM1946726': [1.0], 'GSM1946727': [1.0], 'GSM1946728': [1.0], 'GSM1946729': [1.0], 'GSM1946730': [1.0], 'GSM1946731': [1.0], 'GSM1946732': [1.0], 'GSM1946733': [1.0], 'GSM1946734': [1.0], 'GSM1946735': [1.0], 'GSM1946736': [1.0], 'GSM1946737': [1.0], 'GSM1946738': [1.0], 'GSM1946739': [1.0], 'GSM1946740': [1.0], 'GSM1946741': [1.0], 'GSM1946742': [1.0], 'GSM1946743': [1.0], 'GSM1946744': [1.0], 'GSM1946745': [1.0], 'GSM1946746': [1.0], 'GSM1946747': [1.0], 'GSM1946748': [1.0], 'GSM1946749': [1.0], 'GSM1946750': [1.0], 'GSM1946751': [1.0], 'GSM1946752': [1.0], 'GSM1946753': [1.0], 'GSM1946754': [1.0], 'GSM1946755': [1.0]}\n",
      "Clinical data saved to ../../output/preprocess/Cystic_Fibrosis/clinical_data/GSE67698.csv\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "import pandas as pd\n",
    "import json\n",
    "from typing import Callable, Optional, Dict, Any\n",
    "\n",
    "# 1. Assess gene expression data availability\n",
    "# Based on the background information, this dataset contains transcriptional profiling data\n",
    "# which indicates gene expression data is available\n",
    "is_gene_available = True\n",
    "\n",
    "# 2.1 Identify rows for trait, age, and gender\n",
    "# From the sample characteristics, we can see information about cell line and transduction type\n",
    "# The transduction type (row 1) indicates whether cells have deltaF508 CFTR mutation or wildtype CFTR\n",
    "# This can be used as our trait (presence of cystic fibrosis mutation)\n",
    "trait_row = 1\n",
    "# Age and gender information are not available in this dataset\n",
    "age_row = None\n",
    "gender_row = None\n",
    "\n",
    "# 2.2 Define conversion functions\n",
    "def convert_trait(value):\n",
    "    \"\"\"\n",
    "    Convert CF status based on transduction information\n",
    "    deltaF508 CFTR = 1 (CF mutation present)\n",
    "    wildtype CFTR = 0 (No CF mutation)\n",
    "    \"\"\"\n",
    "    if value is None:\n",
    "        return None\n",
    "        \n",
    "    if isinstance(value, str) and \":\" in value:\n",
    "        value = value.split(\":\", 1)[1].strip()\n",
    "    \n",
    "    if isinstance(value, str):\n",
    "        if \"deltaF508\" in value:\n",
    "            return 1  # CF mutation\n",
    "        elif \"wildtype CFTR\" in value:\n",
    "            return 0  # No CF mutation\n",
    "    \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 - perform initial filtering\n",
    "# Check if trait data is available (trait_row is not None)\n",
    "is_trait_available = trait_row is not None\n",
    "\n",
    "# Initial validation to check if dataset passes basic requirements\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. Extract clinical features if trait data is available\n",
    "if trait_row is not None:\n",
    "    # Make sure the output directory exists\n",
    "    os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)\n",
    "    \n",
    "    # Access the clinical data that was loaded in a previous step\n",
    "    # This is typically a DataFrame with samples as rows and characteristic indices as columns\n",
    "    # We'll assume clinical_data exists from previous steps\n",
    "    \n",
    "    # Extract clinical features\n",
    "    selected_clinical_df = geo_select_clinical_features(\n",
    "        clinical_df=clinical_data,  # Use existing clinical_data from previous step\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 selected clinical features\n",
    "    preview = preview_df(selected_clinical_df)\n",
    "    print(\"Preview of selected clinical features:\")\n",
    "    print(preview)\n",
    "    \n",
    "    # Save the clinical data to CSV\n",
    "    selected_clinical_df.to_csv(out_clinical_data_file)\n",
    "    print(f\"Clinical data saved to {out_clinical_data_file}\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "68cf1d95",
   "metadata": {},
   "source": [
    "### Step 3: Gene Data Extraction"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "077fc49c",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T08:36:11.489917Z",
     "iopub.status.busy": "2025-03-25T08:36:11.489809Z",
     "iopub.status.idle": "2025-03-25T08:36:11.889760Z",
     "shell.execute_reply": "2025-03-25T08:36:11.889120Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Found data marker at line 69\n",
      "Header line: \"ID_REF\"\t\"GSM1946634\"\t\"GSM1946635\"\t\"GSM1946636\"\t\"GSM1946637\"\t\"GSM1946638\"\t\"GSM1946639\"\t\"GSM1946640\"\t\"GSM1946641\"\t\"GSM1946642\"\t\"GSM1946643\"\t\"GSM1946644\"\t\"GSM1946645\"\t\"GSM1946646\"\t\"GSM1946647\"\t\"GSM1946648\"\t\"GSM1946649\"\t\"GSM1946650\"\t\"GSM1946651\"\t\"GSM1946652\"\t\"GSM1946653\"\t\"GSM1946654\"\t\"GSM1946655\"\t\"GSM1946656\"\t\"GSM1946657\"\t\"GSM1946658\"\t\"GSM1946659\"\t\"GSM1946660\"\t\"GSM1946661\"\t\"GSM1946662\"\t\"GSM1946663\"\t\"GSM1946664\"\t\"GSM1946665\"\t\"GSM1946666\"\t\"GSM1946667\"\t\"GSM1946668\"\t\"GSM1946669\"\t\"GSM1946670\"\t\"GSM1946671\"\t\"GSM1946672\"\t\"GSM1946673\"\t\"GSM1946674\"\t\"GSM1946675\"\t\"GSM1946676\"\t\"GSM1946677\"\t\"GSM1946678\"\t\"GSM1946679\"\t\"GSM1946680\"\t\"GSM1946681\"\t\"GSM1946682\"\t\"GSM1946683\"\t\"GSM1946684\"\t\"GSM1946685\"\t\"GSM1946686\"\t\"GSM1946687\"\t\"GSM1946688\"\t\"GSM1946689\"\t\"GSM1946690\"\t\"GSM1946691\"\t\"GSM1946692\"\t\"GSM1946693\"\t\"GSM1946694\"\t\"GSM1946695\"\t\"GSM1946696\"\t\"GSM1946697\"\t\"GSM1946698\"\t\"GSM1946699\"\t\"GSM1946700\"\t\"GSM1946701\"\t\"GSM1946702\"\t\"GSM1946703\"\t\"GSM1946704\"\t\"GSM1946705\"\t\"GSM1946706\"\t\"GSM1946707\"\t\"GSM1946708\"\t\"GSM1946709\"\t\"GSM1946710\"\t\"GSM1946711\"\t\"GSM1946712\"\t\"GSM1946713\"\t\"GSM1946714\"\t\"GSM1946715\"\t\"GSM1946716\"\t\"GSM1946717\"\t\"GSM1946718\"\t\"GSM1946719\"\t\"GSM1946720\"\t\"GSM1946721\"\t\"GSM1946722\"\t\"GSM1946723\"\t\"GSM1946724\"\t\"GSM1946725\"\t\"GSM1946726\"\t\"GSM1946727\"\t\"GSM1946728\"\t\"GSM1946729\"\t\"GSM1946730\"\t\"GSM1946731\"\t\"GSM1946732\"\t\"GSM1946733\"\t\"GSM1946734\"\t\"GSM1946735\"\t\"GSM1946736\"\t\"GSM1946737\"\t\"GSM1946738\"\t\"GSM1946739\"\t\"GSM1946740\"\t\"GSM1946741\"\t\"GSM1946742\"\t\"GSM1946743\"\t\"GSM1946744\"\t\"GSM1946745\"\t\"GSM1946746\"\t\"GSM1946747\"\t\"GSM1946748\"\t\"GSM1946749\"\t\"GSM1946750\"\t\"GSM1946751\"\t\"GSM1946752\"\t\"GSM1946753\"\t\"GSM1946754\"\t\"GSM1946755\"\n",
      "First data line: \"A_23_P100001\"\t-2.8787\t-2.5598\t-2.5796\t-2.5329\t-0.2065\t-0.6002\t-2.8878\t-3.1946\t-0.0517\t0.0390\t-3.1205\t-2.7660\t-3.3119\t-2.3934\t-3.4219\t-2.5577\t-2.7781\t-2.8961\t-2.4142\t-2.5723\t-1.8035\t-1.9974\t-2.6340\t-2.1938\t-3.3447\t-3.7593\t-2.3820\t-3.2490\t-2.7328\t-2.7992\t-3.5729\t-4.0990\t-2.2118\t-2.2726\t-3.3225\t-3.5793\t-2.3955\t-3.0237\t-3.0172\t-3.3335\t-2.6537\t-2.7303\t-1.9556\t-2.0283\t-1.8409\t-1.7097\t-1.6155\t-1.7088\t-2.6423\t-3.2713\t-2.5211\t-2.0271\t1.1176\t0.4275\t0.2648\t0.2237\t-2.9698\t-2.6803\t-1.9177\t-2.3395\t-1.7564\t-1.4396\t-2.5793\t-2.2490\t-2.6141\t-2.8285\t-2.5020\t-2.3917\t-2.5550\t-2.2365\t-3.1108\t-3.0147\t-3.2419\t-3.3621\t-2.8760\t-3.0787\t-2.2880\t-2.3686\t-2.6394\t-2.7403\t-2.1537\t-2.5304\t-2.6259\t-2.2200\t-2.3386\t-2.8193\t-2.9143\t-3.0036\t-2.8826\t-3.8299\t-2.4721\t-2.7392\t-3.1828\t-3.2706\t-2.6757\t-2.6363\t-2.8124\t-3.0037\t-2.8452\t-3.2319\t-3.1110\t-2.8970\t-2.4607\t-2.5720\t-3.5185\t-4.4907\t-2.5415\t-2.7348\t-2.8109\t-2.9797\t-3.2431\t-4.0171\t-2.6663\t-2.2648\t-1.4216\t-2.3937\t-2.6968\t-2.3184\t-1.9126\t-2.0352\t-3.3332\t-4.1652\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Index(['A_23_P100001', 'A_23_P100011', 'A_23_P100022', 'A_23_P100056',\n",
      "       'A_23_P100074', 'A_23_P100092', 'A_23_P100103', 'A_23_P100111',\n",
      "       'A_23_P100127', 'A_23_P100133', 'A_23_P100141', 'A_23_P100156',\n",
      "       'A_23_P100177', 'A_23_P100189', 'A_23_P100196', 'A_23_P100203',\n",
      "       'A_23_P100220', 'A_23_P100240', 'A_23_P10025', 'A_23_P100263'],\n",
      "      dtype='object', name='ID')\n"
     ]
    }
   ],
   "source": [
    "# 1. Get the file paths for the SOFT file and matrix file\n",
    "soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)\n",
    "\n",
    "# 2. First, let's examine the structure of the matrix file to understand its format\n",
    "import gzip\n",
    "\n",
    "# Peek at the first few lines of the file to understand its structure\n",
    "with gzip.open(matrix_file, 'rt') as file:\n",
    "    # Read first 100 lines to find the header structure\n",
    "    for i, line in enumerate(file):\n",
    "        if '!series_matrix_table_begin' in line:\n",
    "            print(f\"Found data marker at line {i}\")\n",
    "            # Read the next line which should be the header\n",
    "            header_line = next(file)\n",
    "            print(f\"Header line: {header_line.strip()}\")\n",
    "            # And the first data line\n",
    "            first_data_line = next(file)\n",
    "            print(f\"First data line: {first_data_line.strip()}\")\n",
    "            break\n",
    "        if i > 100:  # Limit search to first 100 lines\n",
    "            print(\"Matrix table marker not found in first 100 lines\")\n",
    "            break\n",
    "\n",
    "# 3. Now try to get the genetic data with better error handling\n",
    "try:\n",
    "    gene_data = get_genetic_data(matrix_file)\n",
    "    print(gene_data.index[:20])\n",
    "except KeyError as e:\n",
    "    print(f\"KeyError: {e}\")\n",
    "    \n",
    "    # Alternative approach: manually extract the data\n",
    "    print(\"\\nTrying alternative approach to read the gene data:\")\n",
    "    with gzip.open(matrix_file, 'rt') as file:\n",
    "        # Find the start of the data\n",
    "        for line in file:\n",
    "            if '!series_matrix_table_begin' in line:\n",
    "                break\n",
    "                \n",
    "        # Read the headers and data\n",
    "        import pandas as pd\n",
    "        df = pd.read_csv(file, sep='\\t', index_col=0)\n",
    "        print(f\"Column names: {df.columns[:5]}\")\n",
    "        print(f\"First 20 row IDs: {df.index[:20]}\")\n",
    "        gene_data = df\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f57e69d0",
   "metadata": {},
   "source": [
    "### Step 4: Gene Identifier Review"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "6063f53f",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T08:36:11.891524Z",
     "iopub.status.busy": "2025-03-25T08:36:11.891404Z",
     "iopub.status.idle": "2025-03-25T08:36:11.893817Z",
     "shell.execute_reply": "2025-03-25T08:36:11.893373Z"
    }
   },
   "outputs": [],
   "source": [
    "# Based on the gene identifiers, these appear to be Agilent microarray probe IDs (starting with A_23_P),\n",
    "# not standard human gene symbols. These will need to be mapped to official gene symbols.\n",
    "\n",
    "requires_gene_mapping = True\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ad5198d7",
   "metadata": {},
   "source": [
    "### Step 5: Gene Annotation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "a382c4f3",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T08:36:11.895466Z",
     "iopub.status.busy": "2025-03-25T08:36:11.895361Z",
     "iopub.status.idle": "2025-03-25T08:36:20.832272Z",
     "shell.execute_reply": "2025-03-25T08:36:20.831540Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Gene annotation preview:\n",
      "{'ID': ['A_23_P100001', 'A_23_P100011', 'A_23_P100022', 'A_23_P100056', 'A_23_P100074'], 'SPOT_ID': ['A_23_P100001', 'A_23_P100011', 'A_23_P100022', 'A_23_P100056', 'A_23_P100074'], 'CONTROL_TYPE': ['FALSE', 'FALSE', 'FALSE', 'FALSE', 'FALSE'], 'REFSEQ': ['NM_207446', 'NM_005829', 'NM_014848', 'NM_194272', 'NM_020371'], 'GB_ACC': ['NM_207446', 'NM_005829', 'NM_014848', 'NM_194272', 'NM_020371'], 'GENE': [400451.0, 10239.0, 9899.0, 348093.0, 57099.0], 'GENE_SYMBOL': ['FAM174B', 'AP3S2', 'SV2B', 'RBPMS2', 'AVEN'], 'GENE_NAME': ['family with sequence similarity 174, member B', 'adaptor-related protein complex 3, sigma 2 subunit', 'synaptic vesicle glycoprotein 2B', 'RNA binding protein with multiple splicing 2', 'apoptosis, caspase activation inhibitor'], 'UNIGENE_ID': ['Hs.27373', 'Hs.632161', 'Hs.21754', 'Hs.436518', 'Hs.555966'], 'ENSEMBL_ID': ['ENST00000557398', nan, 'ENST00000557410', 'ENST00000300069', 'ENST00000306730'], 'TIGR_ID': [nan, nan, nan, nan, nan], 'ACCESSION_STRING': ['ref|NM_207446|ens|ENST00000557398|ens|ENST00000553393|ens|ENST00000327355', 'ref|NM_005829|ref|NM_001199058|ref|NR_023361|ref|NR_037582', 'ref|NM_014848|ref|NM_001167580|ens|ENST00000557410|ens|ENST00000330276', 'ref|NM_194272|ens|ENST00000300069|gb|AK127873|gb|AK124123', 'ref|NM_020371|ens|ENST00000306730|gb|AF283508|gb|BC010488'], 'CHROMOSOMAL_LOCATION': ['chr15:93160848-93160789', 'chr15:90378743-90378684', 'chr15:91838329-91838388', 'chr15:65032375-65032316', 'chr15:34158739-34158680'], 'CYTOBAND': ['hs|15q26.1', 'hs|15q26.1', 'hs|15q26.1', 'hs|15q22.31', 'hs|15q14'], 'DESCRIPTION': ['Homo sapiens family with sequence similarity 174, member B (FAM174B), mRNA [NM_207446]', 'Homo sapiens adaptor-related protein complex 3, sigma 2 subunit (AP3S2), transcript variant 1, mRNA [NM_005829]', 'Homo sapiens synaptic vesicle glycoprotein 2B (SV2B), transcript variant 1, mRNA [NM_014848]', 'Homo sapiens RNA binding protein with multiple splicing 2 (RBPMS2), mRNA [NM_194272]', 'Homo sapiens apoptosis, caspase activation inhibitor (AVEN), mRNA [NM_020371]'], 'GO_ID': ['GO:0016020(membrane)|GO:0016021(integral to membrane)', 'GO:0005794(Golgi apparatus)|GO:0006886(intracellular protein transport)|GO:0008565(protein transporter activity)|GO:0016020(membrane)|GO:0016192(vesicle-mediated transport)|GO:0030117(membrane coat)|GO:0030659(cytoplasmic vesicle membrane)|GO:0031410(cytoplasmic vesicle)', 'GO:0001669(acrosomal vesicle)|GO:0006836(neurotransmitter transport)|GO:0016020(membrane)|GO:0016021(integral to membrane)|GO:0022857(transmembrane transporter activity)|GO:0030054(cell junction)|GO:0030672(synaptic vesicle membrane)|GO:0031410(cytoplasmic vesicle)|GO:0045202(synapse)', 'GO:0000166(nucleotide binding)|GO:0003676(nucleic acid binding)', 'GO:0005515(protein binding)|GO:0005622(intracellular)|GO:0005624(membrane fraction)|GO:0006915(apoptosis)|GO:0006916(anti-apoptosis)|GO:0012505(endomembrane system)|GO:0016020(membrane)'], 'SEQUENCE': ['ATCTCATGGAAAAGCTGGATTCCTCTGCCTTACGCAGAAACACCCGGGCTCCATCTGCCA', 'TCAAGTATTGGCCTGACATAGAGTCCTTAAGACAAGCAAAGACAAGCAAGGCAAGCACGT', 'ATGTCGGCTGTGGAGGGTTAAAGGGATGAGGCTTTCCTTTGTTTAGCAAATCTGTTCACA', 'CCCTGTCAGATAAGTTTAATGTTTAGTTTGAGGCATGAAGAAGAAAAGGGTTTCCATTCT', 'GACCAGCCAGTTTACAAGCATGTCTCAAGCTAGTGTGTTCCATTATGCTCACAGCAGTAA']}\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": "eab7c3d6",
   "metadata": {},
   "source": [
    "### Step 6: Gene Identifier Mapping"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "50cce18a",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T08:36:20.834296Z",
     "iopub.status.busy": "2025-03-25T08:36:20.834137Z",
     "iopub.status.idle": "2025-03-25T08:36:22.307235Z",
     "shell.execute_reply": "2025-03-25T08:36:22.306745Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Gene expression data (after mapping):\n",
      "Shape: (18488, 122)\n",
      "First 5 gene symbols: ['A1BG', 'A1BG-AS1', 'A1CF', 'A2LD1', 'A2M']\n",
      "First 5 sample columns: ['GSM1946634', 'GSM1946635', 'GSM1946636', 'GSM1946637', 'GSM1946638']\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Gene expression data saved to ../../output/preprocess/Cystic_Fibrosis/gene_data/GSE67698.csv\n"
     ]
    }
   ],
   "source": [
    "# 1. Identify which columns to use for mapping\n",
    "# From the preview, we can see that the 'ID' column contains the probe IDs (A_23_P...)\n",
    "# and 'GENE_SYMBOL' contains the gene symbols we want to map to\n",
    "probe_col = 'ID'  # Column with probe identifiers\n",
    "gene_col = 'GENE_SYMBOL'  # Column with gene symbols\n",
    "\n",
    "# 2. Get gene mapping dataframe by extracting the probe and gene symbol columns\n",
    "mapping_df = get_gene_mapping(gene_annotation, probe_col, gene_col)\n",
    "\n",
    "# 3. Convert probe-level measurements to gene expression data\n",
    "# This will handle the many-to-many mapping between probes and genes\n",
    "gene_data = apply_gene_mapping(gene_data, mapping_df)\n",
    "\n",
    "# Print preview of the mapped gene expression data\n",
    "print(\"Gene expression data (after mapping):\")\n",
    "print(f\"Shape: {gene_data.shape}\")\n",
    "print(f\"First 5 gene symbols: {gene_data.index[:5].tolist()}\")\n",
    "print(f\"First 5 sample columns: {gene_data.columns[:5].tolist()}\")\n",
    "\n",
    "# Save the gene expression data\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\"Gene expression data saved to {out_gene_data_file}\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c3edc0fc",
   "metadata": {},
   "source": [
    "### Step 7: Data Normalization and Linking"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "0e4f0bc2",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T08:36:22.308624Z",
     "iopub.status.busy": "2025-03-25T08:36:22.308488Z",
     "iopub.status.idle": "2025-03-25T08:36:28.666175Z",
     "shell.execute_reply": "2025-03-25T08:36:28.665735Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Normalized gene data shape: (18247, 122)\n",
      "First few genes with their expression values after normalization:\n",
      "          GSM1946634  GSM1946635  GSM1946636  GSM1946637  GSM1946638  \\\n",
      "Gene                                                                   \n",
      "A1BG          0.7790      0.3736     -0.7298     -0.5658      0.7993   \n",
      "A1BG-AS1      1.4335      1.1881      0.9390      0.7998      1.9718   \n",
      "A1CF         -8.6711     -8.6980     -8.1731     -9.8930     -6.6285   \n",
      "A2M          -5.8238     -5.7392     -5.8264     -7.6944     -5.6462   \n",
      "A2ML1        -0.9369     -0.8575     -0.6521     -0.4720      2.5128   \n",
      "\n",
      "          GSM1946639  GSM1946640  GSM1946641  GSM1946642  GSM1946643  ...  \\\n",
      "Gene                                                                  ...   \n",
      "A1BG         -0.4768     -1.2291     -1.4813      0.8968      0.5161  ...   \n",
      "A1BG-AS1      1.2580      0.9186      0.8031      1.7652      1.5084  ...   \n",
      "A1CF         -3.9316    -10.1926    -10.9090     -7.0169     -7.6412  ...   \n",
      "A2M          -5.8414     -7.3668     -9.7768     -6.5750     -6.7933  ...   \n",
      "A2ML1         1.7026     -1.2058     -1.0400      3.4189      3.6057  ...   \n",
      "\n",
      "          GSM1946746  GSM1946747  GSM1946748  GSM1946749  GSM1946750  \\\n",
      "Gene                                                                   \n",
      "A1BG          0.9995      1.1157     -0.9717     -2.3244     -1.4169   \n",
      "A1BG-AS1      1.7237      1.3286      1.3644      0.5460      1.1454   \n",
      "A1CF         -8.8023     -8.5343     -8.8704     -7.6491     -9.6272   \n",
      "A2M          -6.0530     -6.1823     -6.1065     -7.3115     -7.3060   \n",
      "A2ML1        -1.1442     -0.9833      0.5798      0.0510     -0.9032   \n",
      "\n",
      "          GSM1946751  GSM1946752  GSM1946753  GSM1946754  GSM1946755  \n",
      "Gene                                                                  \n",
      "A1BG         -1.3968      1.0533      0.8462     -2.0529     -3.0817  \n",
      "A1BG-AS1      0.9398      1.6896      1.3775      0.5250      0.1934  \n",
      "A1CF        -10.4649     -6.7870     -8.6401     -8.9628    -11.2042  \n",
      "A2M          -8.2849     -5.4245     -6.3632     -5.4934     -8.9906  \n",
      "A2ML1        -1.1459      1.3318      1.9127     -1.6521     -0.8781  \n",
      "\n",
      "[5 rows x 122 columns]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Normalized gene data saved to ../../output/preprocess/Cystic_Fibrosis/gene_data/GSE67698.csv\n",
      "Clinical data preview:\n",
      "{'GSM1946634': [1.0], 'GSM1946635': [1.0], 'GSM1946636': [1.0], 'GSM1946637': [1.0], 'GSM1946638': [1.0], 'GSM1946639': [1.0], 'GSM1946640': [1.0], 'GSM1946641': [1.0], 'GSM1946642': [1.0], 'GSM1946643': [1.0], 'GSM1946644': [1.0], 'GSM1946645': [1.0], 'GSM1946646': [1.0], 'GSM1946647': [1.0], 'GSM1946648': [1.0], 'GSM1946649': [1.0], 'GSM1946650': [1.0], 'GSM1946651': [1.0], 'GSM1946652': [1.0], 'GSM1946653': [1.0], 'GSM1946654': [1.0], 'GSM1946655': [1.0], 'GSM1946656': [1.0], 'GSM1946657': [1.0], 'GSM1946658': [1.0], 'GSM1946659': [1.0], 'GSM1946660': [1.0], 'GSM1946661': [1.0], 'GSM1946662': [1.0], 'GSM1946663': [1.0], 'GSM1946664': [1.0], 'GSM1946665': [1.0], 'GSM1946666': [1.0], 'GSM1946667': [1.0], 'GSM1946668': [1.0], 'GSM1946669': [1.0], 'GSM1946670': [1.0], 'GSM1946671': [1.0], 'GSM1946672': [1.0], 'GSM1946673': [1.0], 'GSM1946674': [1.0], 'GSM1946675': [1.0], 'GSM1946676': [1.0], 'GSM1946677': [1.0], 'GSM1946678': [1.0], 'GSM1946679': [1.0], 'GSM1946680': [1.0], 'GSM1946681': [1.0], 'GSM1946682': [1.0], 'GSM1946683': [1.0], 'GSM1946684': [1.0], 'GSM1946685': [1.0], 'GSM1946686': [0.0], 'GSM1946687': [0.0], 'GSM1946688': [0.0], 'GSM1946689': [0.0], 'GSM1946690': [1.0], 'GSM1946691': [1.0], 'GSM1946692': [1.0], 'GSM1946693': [1.0], 'GSM1946694': [1.0], 'GSM1946695': [1.0], 'GSM1946696': [1.0], 'GSM1946697': [1.0], 'GSM1946698': [1.0], 'GSM1946699': [1.0], 'GSM1946700': [1.0], 'GSM1946701': [1.0], 'GSM1946702': [1.0], 'GSM1946703': [1.0], 'GSM1946704': [1.0], 'GSM1946705': [1.0], 'GSM1946706': [1.0], 'GSM1946707': [1.0], 'GSM1946708': [1.0], 'GSM1946709': [1.0], 'GSM1946710': [1.0], 'GSM1946711': [1.0], 'GSM1946712': [1.0], 'GSM1946713': [1.0], 'GSM1946714': [1.0], 'GSM1946715': [1.0], 'GSM1946716': [1.0], 'GSM1946717': [1.0], 'GSM1946718': [1.0], 'GSM1946719': [1.0], 'GSM1946720': [1.0], 'GSM1946721': [1.0], 'GSM1946722': [1.0], 'GSM1946723': [1.0], 'GSM1946724': [1.0], 'GSM1946725': [1.0], 'GSM1946726': [1.0], 'GSM1946727': [1.0], 'GSM1946728': [1.0], 'GSM1946729': [1.0], 'GSM1946730': [1.0], 'GSM1946731': [1.0], 'GSM1946732': [1.0], 'GSM1946733': [1.0], 'GSM1946734': [1.0], 'GSM1946735': [1.0], 'GSM1946736': [1.0], 'GSM1946737': [1.0], 'GSM1946738': [1.0], 'GSM1946739': [1.0], 'GSM1946740': [1.0], 'GSM1946741': [1.0], 'GSM1946742': [1.0], 'GSM1946743': [1.0], 'GSM1946744': [1.0], 'GSM1946745': [1.0], 'GSM1946746': [1.0], 'GSM1946747': [1.0], 'GSM1946748': [1.0], 'GSM1946749': [1.0], 'GSM1946750': [1.0], 'GSM1946751': [1.0], 'GSM1946752': [1.0], 'GSM1946753': [1.0], 'GSM1946754': [1.0], 'GSM1946755': [1.0]}\n",
      "Linked data shape (before handling missing values): (122, 18248)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Data after handling missing values: (122, 18248)\n",
      "For the feature 'Cystic_Fibrosis', the least common label is '0.0' with 4 occurrences. This represents 3.28% of the dataset.\n",
      "Data was determined to be unusable and was not saved\n"
     ]
    }
   ],
   "source": [
    "# 1. Normalize gene symbols in the gene expression data\n",
    "normalized_gene_data = normalize_gene_symbols_in_index(gene_data)\n",
    "print(f\"Normalized gene data shape: {normalized_gene_data.shape}\")\n",
    "print(\"First few genes with their expression values after normalization:\")\n",
    "print(normalized_gene_data.head())\n",
    "\n",
    "# Save the normalized 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",
    "# 2. Link the clinical and genetic data\n",
    "# Use the clinical data that was already processed and saved in previous steps\n",
    "selected_clinical_df = pd.read_csv(out_clinical_data_file, index_col=0)\n",
    "print(\"Clinical data preview:\")\n",
    "print(preview_df(selected_clinical_df))\n",
    "\n",
    "# Link the clinical and genetic data\n",
    "linked_data = geo_link_clinical_genetic_data(selected_clinical_df, normalized_gene_data)\n",
    "print(f\"Linked data shape (before handling missing values): {linked_data.shape}\")\n",
    "\n",
    "# 3. Handle missing values in the linked data\n",
    "linked_data = handle_missing_values(linked_data, trait)\n",
    "print(f\"Data after handling missing values: {linked_data.shape}\")\n",
    "\n",
    "# 4. Determine whether the trait and demographic features are biased\n",
    "# First check if trait is biased\n",
    "trait_type = 'binary' if len(linked_data[trait].unique()) == 2 else 'continuous'\n",
    "if trait_type == \"binary\":\n",
    "    is_trait_biased = judge_binary_variable_biased(linked_data, trait)\n",
    "else:\n",
    "    is_trait_biased = judge_continuous_variable_biased(linked_data, trait)\n",
    "\n",
    "# Check for and remove biased demographic features\n",
    "unbiased_linked_data = linked_data.copy()\n",
    "if \"Age\" in unbiased_linked_data.columns:\n",
    "    age_biased = judge_continuous_variable_biased(unbiased_linked_data, 'Age')\n",
    "    if age_biased:\n",
    "        print(f\"The distribution of the feature \\'Age\\' in this dataset is severely biased.\\n\")\n",
    "        unbiased_linked_data = unbiased_linked_data.drop(columns='Age')\n",
    "    else:\n",
    "        print(f\"The distribution of the feature \\'Age\\' in this dataset is fine.\\n\")\n",
    "\n",
    "if \"Gender\" in unbiased_linked_data.columns:\n",
    "    gender_biased = judge_binary_variable_biased(unbiased_linked_data, 'Gender')\n",
    "    if gender_biased:\n",
    "        print(f\"The distribution of the feature \\'Gender\\' in this dataset is severely biased.\\n\")\n",
    "        unbiased_linked_data = unbiased_linked_data.drop(columns='Gender')\n",
    "    else:\n",
    "        print(f\"The distribution of the feature \\'Gender\\' in this dataset is fine.\\n\")\n",
    "\n",
    "# 5. Conduct final quality validation and save 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=f\"Dataset contains gene expression data comparing CFBE41o-ΔF508 (CF) cells with CFBE41o−CFTR (control) cells.\"\n",
    ")\n",
    "\n",
    "# 6. If the linked data is usable, save it\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
}