File size: 19,939 Bytes
92d2f89
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "e6332184",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T06:23:58.899890Z",
     "iopub.status.busy": "2025-03-25T06:23:58.899783Z",
     "iopub.status.idle": "2025-03-25T06:23:59.065214Z",
     "shell.execute_reply": "2025-03-25T06:23:59.064871Z"
    }
   },
   "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 = \"Allergies\"\n",
    "cohort = \"GSE205151\"\n",
    "\n",
    "# Input paths\n",
    "in_trait_dir = \"../../input/GEO/Allergies\"\n",
    "in_cohort_dir = \"../../input/GEO/Allergies/GSE205151\"\n",
    "\n",
    "# Output paths\n",
    "out_data_file = \"../../output/preprocess/Allergies/GSE205151.csv\"\n",
    "out_gene_data_file = \"../../output/preprocess/Allergies/gene_data/GSE205151.csv\"\n",
    "out_clinical_data_file = \"../../output/preprocess/Allergies/clinical_data/GSE205151.csv\"\n",
    "json_path = \"../../output/preprocess/Allergies/cohort_info.json\"\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "80178eff",
   "metadata": {},
   "source": [
    "### Step 1: Initial Data Loading"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "698ac1fb",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T06:23:59.066656Z",
     "iopub.status.busy": "2025-03-25T06:23:59.066514Z",
     "iopub.status.idle": "2025-03-25T06:23:59.094086Z",
     "shell.execute_reply": "2025-03-25T06:23:59.093793Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Background Information:\n",
      "!Series_title\t\"Functional Immunophenotyping of Children with Critical Status Asthmaticus Identifies Differential Gene Expression Responses in Neutrophils Exposed to a Poly(I:C) Stimulus\"\n",
      "!Series_summary\t\"We determined whether we could identify clusters of children with critical asthma by functional immunophenotyping using an intracellular viral analog stimulus.\"\n",
      "!Series_summary\t\"We performed a single-center, prospective, observational cohort study of 43 children ages 6 – 17 years admitted to a pediatric intensive care unit for an asthma attack between July 2019 to February 2021.\"\n",
      "!Series_overall_design\t\"Neutrophils were isolated from children, stimulated overnight with LyoVec poly(I:C), and mRNA was analyzed using a targeted Nanostring immunology array. Network analysis of the differentially expressed transcripts for the paired LyoVec poly(I:C) samples was performed.\"\n",
      "Sample Characteristics Dictionary:\n",
      "{0: ['polyic_stimulation: Unstimulated', 'polyic_stimulation: Stimulated', 'polyic_stimulation: No'], 1: ['cluster: 1', 'cluster: 2', nan]}\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": "c453aba9",
   "metadata": {},
   "source": [
    "### Step 2: Dataset Analysis and Clinical Feature Extraction"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "59a13bc6",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T06:23:59.095086Z",
     "iopub.status.busy": "2025-03-25T06:23:59.094980Z",
     "iopub.status.idle": "2025-03-25T06:23:59.099737Z",
     "shell.execute_reply": "2025-03-25T06:23:59.099466Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Clinical data file not found. Unable to extract clinical features.\n"
     ]
    }
   ],
   "source": [
    "# 1. Gene Expression Data Availability\n",
    "# Based on the background information, this dataset contains gene expression data (mRNA analyzed using Nanostring immunology array)\n",
    "is_gene_available = True\n",
    "\n",
    "# 2. Variable Availability and Data Type Conversion\n",
    "# 2.1 Data Availability\n",
    "# Looking at the Sample Characteristics Dictionary, we have:\n",
    "# - Key 0: 'polyic_stimulation' (Unstimulated, Stimulated, No)\n",
    "# - Key 1: 'cluster' (1, 2, nan)\n",
    "\n",
    "# For the allergy trait (asthma in this case), we can use the 'cluster' field\n",
    "# The study mentions clusters of children with critical asthma\n",
    "trait_row = 1\n",
    "\n",
    "# Age and gender are not available in the sample characteristics dictionary\n",
    "age_row = None\n",
    "gender_row = None\n",
    "\n",
    "# 2.2 Data Type Conversion Functions\n",
    "def convert_trait(value):\n",
    "    \"\"\"Convert trait (cluster) to binary value (0 or 1)\"\"\"\n",
    "    if pd.isna(value):\n",
    "        return None\n",
    "    \n",
    "    # Extract the value after the colon and strip whitespace\n",
    "    if ':' in value:\n",
    "        value = value.split(':', 1)[1].strip()\n",
    "    \n",
    "    # Convert cluster values to binary (0 for cluster 1, 1 for cluster 2)\n",
    "    try:\n",
    "        cluster = int(value)\n",
    "        if cluster == 1:\n",
    "            return 0\n",
    "        elif cluster == 2:\n",
    "            return 1\n",
    "        else:\n",
    "            return None\n",
    "    except (ValueError, TypeError):\n",
    "        return None\n",
    "\n",
    "def convert_age(value):\n",
    "    \"\"\"Convert age to continuous value (not used in this dataset)\"\"\"\n",
    "    return None\n",
    "\n",
    "def convert_gender(value):\n",
    "    \"\"\"Convert gender to binary value (not used in this dataset)\"\"\"\n",
    "    return None\n",
    "\n",
    "# 3. Save Metadata\n",
    "# Determine trait data availability\n",
    "is_trait_available = trait_row is not None\n",
    "\n",
    "# Validate and save cohort info\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 not None, we need to extract clinical features\n",
    "if trait_row is not None:\n",
    "    try:\n",
    "        # Look for the sample characteristics data which should be available from previous steps\n",
    "        # Each cohort typically has a characteristics.csv file from GEO processing\n",
    "        clinical_data_file = os.path.join(in_cohort_dir, \"characteristics.csv\")\n",
    "        clinical_data = pd.read_csv(clinical_data_file, index_col=0)\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 extracted clinical features\n",
    "        clinical_preview = preview_df(selected_clinical_df)\n",
    "        print(\"Clinical Data Preview:\")\n",
    "        print(clinical_preview)\n",
    "        \n",
    "        # Save the 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, index=False)\n",
    "        print(f\"Clinical data saved to {out_clinical_data_file}\")\n",
    "    except FileNotFoundError:\n",
    "        print(f\"Clinical data file not found. Unable to extract clinical features.\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "98adb4d2",
   "metadata": {},
   "source": [
    "### Step 3: Gene Data Extraction"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "dd22eac2",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T06:23:59.100733Z",
     "iopub.status.busy": "2025-03-25T06:23:59.100634Z",
     "iopub.status.idle": "2025-03-25T06:23:59.118507Z",
     "shell.execute_reply": "2025-03-25T06:23:59.118228Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "First 20 gene/probe identifiers:\n",
      "Index(['ABCB1', 'ABCF1', 'ABL1', 'ADA', 'AHR', 'AICDA', 'AIRE', 'ALAS1', 'APP',\n",
      "       'ARG1', 'ARG2', 'ARHGDIB', 'ATG10', 'ATG12', 'ATG16L1', 'ATG5', 'ATG7',\n",
      "       'ATM', 'B2M', 'B3GAT1'],\n",
      "      dtype='object', name='ID')\n"
     ]
    }
   ],
   "source": [
    "# 1. First get the file paths again to access the matrix file\n",
    "soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)\n",
    "\n",
    "# 2. Use the get_genetic_data function from the library to get the gene_data from the matrix_file\n",
    "gene_data = get_genetic_data(matrix_file)\n",
    "\n",
    "# 3. Print the first 20 row IDs (gene or probe identifiers) for future observation\n",
    "print(\"First 20 gene/probe identifiers:\")\n",
    "print(gene_data.index[:20])\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "39d9de18",
   "metadata": {},
   "source": [
    "### Step 4: Gene Identifier Review"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "3becceb1",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T06:23:59.119487Z",
     "iopub.status.busy": "2025-03-25T06:23:59.119387Z",
     "iopub.status.idle": "2025-03-25T06:23:59.121049Z",
     "shell.execute_reply": "2025-03-25T06:23:59.120785Z"
    }
   },
   "outputs": [],
   "source": [
    "# These identifiers appear to be standard human gene symbols (like ABCB1, ATG5, B2M)\n",
    "# They follow the standard HGNC gene nomenclature and are recognizable as common human genes\n",
    "# No mapping is needed as they are already in the preferred format\n",
    "\n",
    "requires_gene_mapping = False\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a1fe33fb",
   "metadata": {},
   "source": [
    "### Step 5: Data Normalization and Linking"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "274cb43b",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T06:23:59.122023Z",
     "iopub.status.busy": "2025-03-25T06:23:59.121919Z",
     "iopub.status.idle": "2025-03-25T06:23:59.375666Z",
     "shell.execute_reply": "2025-03-25T06:23:59.375301Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Normalizing gene symbols...\n",
      "Gene data shape after normalization: (576, 144)\n",
      "Normalized gene data saved to ../../output/preprocess/Allergies/gene_data/GSE205151.csv\n",
      "Loading the original clinical data...\n",
      "Extracting clinical features...\n",
      "Clinical data preview:\n",
      "{'GSM6205808': [0.0], 'GSM6205809': [0.0], 'GSM6205810': [1.0], 'GSM6205811': [1.0], 'GSM6205812': [0.0], 'GSM6205813': [0.0], 'GSM6205814': [1.0], 'GSM6205815': [1.0], 'GSM6205816': [1.0], 'GSM6205817': [1.0], 'GSM6205818': [1.0], 'GSM6205819': [1.0], 'GSM6205820': [0.0], 'GSM6205821': [0.0], 'GSM6205822': [1.0], 'GSM6205823': [1.0], 'GSM6205824': [1.0], 'GSM6205825': [1.0], 'GSM6205826': [1.0], 'GSM6205827': [1.0], 'GSM6205828': [0.0], 'GSM6205829': [0.0], 'GSM6205830': [1.0], 'GSM6205831': [1.0], 'GSM6205832': [1.0], 'GSM6205833': [1.0], 'GSM6205834': [0.0], 'GSM6205835': [0.0], 'GSM6205836': [0.0], 'GSM6205837': [0.0], 'GSM6205838': [0.0], 'GSM6205839': [0.0], 'GSM6205840': [1.0], 'GSM6205841': [1.0], 'GSM6205842': [0.0], 'GSM6205843': [0.0], 'GSM6205844': [1.0], 'GSM6205845': [1.0], 'GSM6205846': [1.0], 'GSM6205847': [1.0], 'GSM6205848': [0.0], 'GSM6205849': [0.0], 'GSM6205850': [0.0], 'GSM6205851': [0.0], 'GSM6205852': [0.0], 'GSM6205853': [0.0], 'GSM6205854': [0.0], 'GSM6205855': [0.0], 'GSM6205856': [0.0], 'GSM6205857': [0.0], 'GSM6205858': [1.0], 'GSM6205859': [1.0], 'GSM6205860': [0.0], 'GSM6205861': [0.0], 'GSM6205862': [0.0], 'GSM6205863': [0.0], 'GSM6205864': [0.0], 'GSM6205865': [0.0], 'GSM6205866': [0.0], 'GSM6205867': [0.0], 'GSM6205868': [0.0], 'GSM6205869': [0.0], 'GSM6205870': [0.0], 'GSM6205871': [0.0], 'GSM6205872': [1.0], 'GSM6205873': [1.0], 'GSM6205874': [1.0], 'GSM6205875': [1.0], 'GSM6205876': [1.0], 'GSM6205877': [1.0], 'GSM6205878': [1.0], 'GSM6205879': [1.0], 'GSM6205880': [1.0], 'GSM6205881': [1.0], 'GSM6205882': [0.0], 'GSM6205883': [0.0], 'GSM6205884': [1.0], 'GSM6205885': [1.0], 'GSM6205886': [1.0], 'GSM6205887': [1.0], 'GSM6205888': [1.0], 'GSM6205889': [1.0], 'GSM6205890': [1.0], 'GSM6205891': [1.0], 'GSM6205892': [0.0], 'GSM6205893': [0.0], 'GSM6205894': [1.0], 'GSM6205895': [1.0], 'GSM6205896': [0.0], 'GSM6205897': [0.0], 'GSM6205898': [1.0], 'GSM6205899': [1.0], 'GSM6205900': [0.0], 'GSM6205901': [0.0], 'GSM6205902': [1.0], 'GSM6205903': [1.0], 'GSM6205904': [0.0], 'GSM6205905': [1.0], 'GSM6205906': [0.0], 'GSM6205907': [1.0], 'GSM6205908': [0.0], 'GSM6205909': [1.0], 'GSM6205910': [1.0], 'GSM6205911': [1.0], 'GSM6205912': [1.0], 'GSM6205913': [1.0], 'GSM6205914': [0.0], 'GSM6205915': [1.0], 'GSM6205916': [1.0], 'GSM6205917': [1.0], 'GSM6205918': [0.0], 'GSM6205919': [0.0], 'GSM6205920': [0.0], 'GSM6205921': [1.0], 'GSM6205922': [0.0], 'GSM6205923': [0.0], 'GSM6205924': [0.0], 'GSM6205925': [nan], 'GSM6205926': [0.0], 'GSM6205927': [0.0], 'GSM6205928': [0.0], 'GSM6205929': [0.0], 'GSM6205930': [1.0], 'GSM6205931': [0.0], 'GSM6205932': [0.0], 'GSM6205933': [0.0], 'GSM6205934': [0.0], 'GSM6205935': [1.0], 'GSM6205936': [0.0], 'GSM6205937': [1.0], 'GSM6205938': [1.0], 'GSM6205939': [1.0], 'GSM6205940': [1.0], 'GSM6205941': [1.0], 'GSM6205942': [1.0], 'GSM6205943': [1.0], 'GSM6205944': [1.0], 'GSM6205945': [0.0], 'GSM6205946': [0.0], 'GSM6205947': [1.0], 'GSM6205948': [0.0], 'GSM6205949': [1.0], 'GSM6205950': [0.0], 'GSM6205951': [1.0]}\n",
      "Clinical data saved to ../../output/preprocess/Allergies/clinical_data/GSE205151.csv\n",
      "Linking clinical and genetic data...\n",
      "Linked data shape: (144, 577)\n",
      "Handling missing values...\n",
      "Linked data shape after handling missing values: (143, 577)\n",
      "Checking for bias in trait distribution...\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "For the feature 'Allergies', the least common label is '0.0' with 69 occurrences. This represents 48.25% of the dataset.\n",
      "The distribution of the feature 'Allergies' in this dataset is fine.\n",
      "\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Dataset usability: True\n",
      "Linked data saved to ../../output/preprocess/Allergies/GSE205151.csv\n"
     ]
    }
   ],
   "source": [
    "# 1. Normalize gene symbols in the gene expression data\n",
    "print(\"Normalizing gene symbols...\")\n",
    "normalized_gene_data = normalize_gene_symbols_in_index(gene_data)\n",
    "print(f\"Gene data shape after normalization: {normalized_gene_data.shape}\")\n",
    "\n",
    "# Save the normalized gene data to a CSV file\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",
    "print(\"Loading the original clinical data...\")\n",
    "# Get the matrix file again to ensure we have the proper data\n",
    "soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)\n",
    "background_info, clinical_data = get_background_and_clinical_data(matrix_file)\n",
    "\n",
    "print(\"Extracting clinical features...\")\n",
    "# Use the clinical_data obtained directly from the matrix file\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",
    "print(\"Clinical data preview:\")\n",
    "print(preview_df(selected_clinical_df))\n",
    "\n",
    "# Save the clinical data to a CSV file\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",
    "# Link clinical and genetic data using the normalized gene data\n",
    "print(\"Linking clinical and genetic data...\")\n",
    "linked_data = geo_link_clinical_genetic_data(selected_clinical_df, normalized_gene_data)\n",
    "print(f\"Linked data shape: {linked_data.shape}\")\n",
    "\n",
    "# 3. Handle missing values in the linked data\n",
    "print(\"Handling missing values...\")\n",
    "linked_data = handle_missing_values(linked_data, trait)\n",
    "print(f\"Linked data shape after handling missing values: {linked_data.shape}\")\n",
    "\n",
    "# 4. Check if trait is biased\n",
    "print(\"Checking for bias in trait distribution...\")\n",
    "is_biased, linked_data = judge_and_remove_biased_features(linked_data, trait)\n",
    "\n",
    "# 5. Final validation\n",
    "note = \"Dataset contains gene expression data from patients with Essential Thrombocythemia (ET), Polycythemia Vera (PV), and Primary Myelofibrosis (PMF).\"\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=linked_data,\n",
    "    note=note\n",
    ")\n",
    "\n",
    "print(f\"Dataset usability: {is_usable}\")\n",
    "\n",
    "# 6. Save linked data if usable\n",
    "if is_usable:\n",
    "    os.makedirs(os.path.dirname(out_data_file), exist_ok=True)\n",
    "    linked_data.to_csv(out_data_file)\n",
    "    print(f\"Linked data saved to {out_data_file}\")\n",
    "else:\n",
    "    print(\"Dataset is not usable for trait-gene association studies due to bias or other issues.\")"
   ]
  }
 ],
 "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
}