File size: 22,681 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
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "cfb1633f",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T06:52:05.705101Z",
     "iopub.status.busy": "2025-03-25T06:52:05.704857Z",
     "iopub.status.idle": "2025-03-25T06:52:05.869933Z",
     "shell.execute_reply": "2025-03-25T06:52:05.869601Z"
    }
   },
   "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 = \"Autism_spectrum_disorder_(ASD)\"\n",
    "cohort = \"GSE148450\"\n",
    "\n",
    "# Input paths\n",
    "in_trait_dir = \"../../input/GEO/Autism_spectrum_disorder_(ASD)\"\n",
    "in_cohort_dir = \"../../input/GEO/Autism_spectrum_disorder_(ASD)/GSE148450\"\n",
    "\n",
    "# Output paths\n",
    "out_data_file = \"../../output/preprocess/Autism_spectrum_disorder_(ASD)/GSE148450.csv\"\n",
    "out_gene_data_file = \"../../output/preprocess/Autism_spectrum_disorder_(ASD)/gene_data/GSE148450.csv\"\n",
    "out_clinical_data_file = \"../../output/preprocess/Autism_spectrum_disorder_(ASD)/clinical_data/GSE148450.csv\"\n",
    "json_path = \"../../output/preprocess/Autism_spectrum_disorder_(ASD)/cohort_info.json\"\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b15df714",
   "metadata": {},
   "source": [
    "### Step 1: Initial Data Loading"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "b4cff5b0",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T06:52:05.871331Z",
     "iopub.status.busy": "2025-03-25T06:52:05.871191Z",
     "iopub.status.idle": "2025-03-25T06:52:06.267595Z",
     "shell.execute_reply": "2025-03-25T06:52:06.267303Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Background Information:\n",
      "!Series_title\t\"Expression changes in immune and epigenetic gene pathways associated with nutritional metabolites in maternal blood from pregnancies resulting in autism and atypical neurodevelopment\"\n",
      "!Series_summary\t\"The prenatal period is a critical window to study factors involved in the development of autism spectrum disorder (ASD). Environmental factors, especially in utero nutrition, can interact with genetic risk for ASD, but how specific prenatal nutrients in mothers of children later diagnosed with ASD or non-typical development (Non-TD) associate with gestational gene expression events is poorly understood. Maternal blood collected prospectively during pregnancy provides a new opportunity to gain insights into nutritional effects on gene pathways and neurodevelopment. Using differential gene expression analysis, six transcripts associated with four genes (TGR-AS1, SQSTM1, HLA-C and RFESD) showed genome-wide significance (FDR q < 0.05) with child outcomes. Genes nominally differentially expressed compared to TD (p < 0.05) specifically in ASD, but not Non-TD, significantly overlapped with seven high confidence ASD genes. 218 transcripts in common to ASD and Non-TD differential expression compared to TD were significantly enriched for functions in immune response to interferon-gamma, apoptosis, and metal ion transport. WGCNA identified co-expressed gene modules significantly correlated with 5-MeTHF, folic acid, DMG and betaine. A module enriched in DNA methylation functions showed a protective association with folic acid/5-MeTHF concentrations and ASD risk. Independent of child outcome, maternal plasma betaine and DMG concentrations associated with a block of co-expressed genes enriched for adaptive immune, histone modification, and RNA processing functions. These results support the premise that the prenatal maternal blood transcriptome is a sensitive indicator of gestational nutrition and children’s later neurodevelopmental outcomes.\"\n",
      "!Series_overall_design\t\"Genome-wide transcriptome levels were measured using microarrays in 300 maternal blood samples (including 214 mother given consent forms) from all three trimesters in the Markers of Autism Risk in Babies -Learning Early Signs (MARBLES) study. Sixteen different nutrient metabolites, including folic acid, betaine, 5’-methyltretrahydrofolate (5-MeTHF), and dimethylglycine (DMG) were measured in the same samples. Differential expression analysis and weighted gene correlation network analysis (WGCNA) compared gene expression between children later diagnosed as ASD and typical development (TD), Non-TD and TD, and to nutrient metabolites.\"\n",
      "Sample Characteristics Dictionary:\n",
      "{0: ['diagnosis: ASD', 'diagnosis: NonTD', 'diagnosis: TD'], 1: ['Sex: M', 'Sex: F'], 2: ['study: MARBLES'], 3: ['array batch: 1', 'array batch: 3', 'array batch: 2'], 4: ['nutrient metabolites: Y', 'nutrient metabolites: N']}\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": "2440afe6",
   "metadata": {},
   "source": [
    "### Step 2: Dataset Analysis and Clinical Feature Extraction"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "d1436299",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T06:52:06.268720Z",
     "iopub.status.busy": "2025-03-25T06:52:06.268614Z",
     "iopub.status.idle": "2025-03-25T06:52:06.273098Z",
     "shell.execute_reply": "2025-03-25T06:52:06.272815Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Metadata validation completed. Clinical feature extraction skipped due to missing clinical data file.\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import os\n",
    "import json\n",
    "from typing import Callable, Optional, Dict, Any, List, Union\n",
    "\n",
    "# 1. Gene Expression Data Availability\n",
    "# Based on the background information, this dataset contains genome-wide transcriptome levels \n",
    "# measured using microarrays, which is gene expression data\n",
    "is_gene_available = True\n",
    "\n",
    "# 2. Variable Availability and Data Type Conversion\n",
    "\n",
    "# 2.1 Data Availability\n",
    "# For trait (ASD), the diagnosis is in row 0\n",
    "trait_row = 0\n",
    "\n",
    "# For gender, the Sex is in row 1\n",
    "gender_row = 1\n",
    "\n",
    "# For age, there is no age information in the sample characteristics\n",
    "age_row = None\n",
    "\n",
    "# 2.2 Data Type Conversion\n",
    "def convert_trait(value: str) -> Optional[int]:\n",
    "    \"\"\"Convert trait value to binary (0/1).\"\"\"\n",
    "    if not isinstance(value, str):\n",
    "        return None\n",
    "    value = value.lower().split(': ')[-1].strip()\n",
    "    if 'asd' in value:\n",
    "        return 1  # ASD case\n",
    "    elif 'td' in value and 'nontd' not in value and 'non-td' not in value:  # Typical development\n",
    "        return 0  # Control\n",
    "    elif 'nontd' in value or 'non-td' in value:  # Non-typical development, not clear if this should be case or control\n",
    "        return None  # Exclude these samples as they're neither case nor control\n",
    "    return None\n",
    "\n",
    "def convert_gender(value: str) -> Optional[int]:\n",
    "    \"\"\"Convert gender value to binary (0=female, 1=male).\"\"\"\n",
    "    if not isinstance(value, str):\n",
    "        return None\n",
    "    value = value.lower().split(': ')[-1].strip()\n",
    "    if 'm' in value:\n",
    "        return 1  # Male\n",
    "    elif 'f' in value:\n",
    "        return 0  # Female\n",
    "    return None\n",
    "\n",
    "def convert_age(value: str) -> Optional[float]:\n",
    "    \"\"\"Placeholder for age conversion, not used in 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",
    "# We cannot proceed with clinical feature extraction since we don't have access to\n",
    "# the actual clinical data file with per-sample information.\n",
    "# The sample characteristics dictionary only shows unique possible values, not actual sample data.\n",
    "# If we had the clinical data file, we would use geo_select_clinical_features as planned.\n",
    "\n",
    "print(\"Metadata validation completed. Clinical feature extraction skipped due to missing clinical data file.\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0a87f964",
   "metadata": {},
   "source": [
    "### Step 3: Gene Data Extraction"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "84f2972c",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T06:52:06.274144Z",
     "iopub.status.busy": "2025-03-25T06:52:06.274044Z",
     "iopub.status.idle": "2025-03-25T06:52:06.994493Z",
     "shell.execute_reply": "2025-03-25T06:52:06.994123Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Index(['A1BG', 'A1BG-AS1', 'A1CF', 'A2M', 'A2M-AS1', 'A2ML1', 'A2ML1-AS1',\n",
      "       'A2ML1-AS2', 'A2MP1', 'A4GALT', 'A4GNT', 'AA06', 'AAAS', 'AACS',\n",
      "       'AACSP1', 'AADAC', 'AADACL2', 'AADACL2-AS1', 'AADACL3', 'AADACL4'],\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": "dadd5900",
   "metadata": {},
   "source": [
    "### Step 4: Gene Identifier Review"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "5fc5c683",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T06:52:06.995727Z",
     "iopub.status.busy": "2025-03-25T06:52:06.995609Z",
     "iopub.status.idle": "2025-03-25T06:52:06.997473Z",
     "shell.execute_reply": "2025-03-25T06:52:06.997196Z"
    }
   },
   "outputs": [],
   "source": [
    "# These appear to be standard human gene symbols\n",
    "# A1BG = Alpha-1-B Glycoprotein\n",
    "# A1BG-AS1 = A1BG Antisense RNA 1\n",
    "# A2M = Alpha-2-Macroglobulin\n",
    "# And so on\n",
    "\n",
    "requires_gene_mapping = False\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "51ecf0ec",
   "metadata": {},
   "source": [
    "### Step 5: Data Normalization and Linking"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "8da0e985",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T06:52:06.998539Z",
     "iopub.status.busy": "2025-03-25T06:52:06.998441Z",
     "iopub.status.idle": "2025-03-25T06:52:22.548490Z",
     "shell.execute_reply": "2025-03-25T06:52:22.548108Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Preview of selected clinical features:\n",
      "{'GSM4471661': [1.0, 1.0], 'GSM4471662': [1.0, 1.0], 'GSM4471663': [1.0, 1.0], 'GSM4471664': [1.0, 1.0], 'GSM4471665': [1.0, 1.0], 'GSM4471666': [1.0, 1.0], 'GSM4471667': [1.0, 1.0], 'GSM4471668': [1.0, 1.0], 'GSM4471669': [1.0, 1.0], 'GSM4471670': [1.0, 0.0], 'GSM4471671': [1.0, 0.0], 'GSM4471672': [1.0, 1.0], 'GSM4471673': [1.0, 1.0], 'GSM4471674': [1.0, 1.0], 'GSM4471675': [1.0, 1.0], 'GSM4471676': [1.0, 1.0], 'GSM4471677': [1.0, 1.0], 'GSM4471678': [1.0, 1.0], 'GSM4471679': [1.0, 1.0], 'GSM4471680': [1.0, 1.0], 'GSM4471681': [1.0, 0.0], 'GSM4471682': [1.0, 1.0], 'GSM4471683': [1.0, 1.0], 'GSM4471684': [1.0, 1.0], 'GSM4471685': [1.0, 1.0], 'GSM4471686': [1.0, 0.0], 'GSM4471687': [1.0, 1.0], 'GSM4471688': [1.0, 1.0], 'GSM4471689': [1.0, 1.0], 'GSM4471690': [1.0, 1.0], 'GSM4471691': [1.0, 0.0], 'GSM4471692': [1.0, 0.0], 'GSM4471693': [1.0, 1.0], 'GSM4471694': [1.0, 0.0], 'GSM4471695': [1.0, 0.0], 'GSM4471696': [1.0, 1.0], 'GSM4471697': [1.0, 1.0], 'GSM4471698': [1.0, 1.0], 'GSM4471699': [1.0, 1.0], 'GSM4471700': [1.0, 0.0], 'GSM4471701': [1.0, 0.0], 'GSM4471702': [1.0, 0.0], 'GSM4471703': [1.0, 1.0], 'GSM4471704': [1.0, 1.0], 'GSM4471705': [1.0, 1.0], 'GSM4471706': [1.0, 1.0], 'GSM4471707': [1.0, 1.0], 'GSM4471708': [1.0, 0.0], 'GSM4471709': [1.0, 0.0], 'GSM4471710': [1.0, 0.0], 'GSM4471711': [1.0, 0.0], 'GSM4471712': [1.0, 0.0], 'GSM4471713': [1.0, 1.0], 'GSM4471714': [1.0, 0.0], 'GSM4471715': [1.0, 1.0], 'GSM4471716': [1.0, 1.0], 'GSM4471717': [nan, 1.0], 'GSM4471718': [nan, 0.0], 'GSM4471719': [nan, 0.0], 'GSM4471720': [nan, 1.0], 'GSM4471721': [nan, 1.0], 'GSM4471722': [nan, 1.0], 'GSM4471723': [nan, 1.0], 'GSM4471724': [nan, 0.0], 'GSM4471725': [nan, 0.0], 'GSM4471726': [nan, 0.0], 'GSM4471727': [nan, 1.0], 'GSM4471728': [nan, 1.0], 'GSM4471729': [nan, 0.0], 'GSM4471730': [nan, 0.0], 'GSM4471731': [nan, 1.0], 'GSM4471732': [nan, 1.0], 'GSM4471733': [nan, 0.0], 'GSM4471734': [nan, 0.0], 'GSM4471735': [nan, 1.0], 'GSM4471736': [nan, 1.0], 'GSM4471737': [nan, 1.0], 'GSM4471738': [nan, 1.0], 'GSM4471739': [nan, 0.0], 'GSM4471740': [nan, 0.0], 'GSM4471741': [nan, 1.0], 'GSM4471742': [nan, 0.0], 'GSM4471743': [nan, 1.0], 'GSM4471744': [nan, 1.0], 'GSM4471745': [nan, 1.0], 'GSM4471746': [nan, 1.0], 'GSM4471747': [nan, 1.0], 'GSM4471748': [nan, 1.0], 'GSM4471749': [nan, 0.0], 'GSM4471750': [nan, 0.0], 'GSM4471751': [nan, 0.0], 'GSM4471752': [nan, 1.0], 'GSM4471753': [nan, 1.0], 'GSM4471754': [nan, 1.0], 'GSM4471755': [nan, 0.0], 'GSM4471756': [nan, 0.0], 'GSM4471757': [nan, 0.0], 'GSM4471758': [nan, 1.0], 'GSM4471759': [nan, 1.0], 'GSM4471760': [nan, 1.0], 'GSM4471761': [nan, 1.0], 'GSM4471762': [nan, 0.0], 'GSM4471763': [nan, 1.0], 'GSM4471764': [nan, 1.0], 'GSM4471765': [nan, 1.0], 'GSM4471766': [nan, 0.0], 'GSM4471767': [nan, 0.0], 'GSM4471768': [nan, 0.0], 'GSM4471769': [nan, 0.0], 'GSM4471770': [nan, 0.0], 'GSM4471771': [nan, 1.0], 'GSM4471772': [0.0, 1.0], 'GSM4471773': [0.0, 1.0], 'GSM4471774': [0.0, 1.0], 'GSM4471775': [0.0, 1.0], 'GSM4471776': [0.0, 1.0], 'GSM4471777': [0.0, 1.0], 'GSM4471778': [0.0, 0.0], 'GSM4471779': [0.0, 0.0], 'GSM4471780': [0.0, 0.0], 'GSM4471781': [0.0, 0.0], 'GSM4471782': [0.0, 1.0], 'GSM4471783': [0.0, 1.0], 'GSM4471784': [0.0, 0.0], 'GSM4471785': [0.0, 0.0], 'GSM4471786': [0.0, 0.0], 'GSM4471787': [0.0, 1.0], 'GSM4471788': [0.0, 1.0], 'GSM4471789': [0.0, 1.0], 'GSM4471790': [0.0, 1.0], 'GSM4471791': [0.0, 0.0], 'GSM4471792': [0.0, 0.0], 'GSM4471793': [0.0, 1.0], 'GSM4471794': [0.0, 1.0], 'GSM4471795': [0.0, 1.0], 'GSM4471796': [0.0, 1.0], 'GSM4471797': [0.0, 1.0], 'GSM4471798': [0.0, 0.0], 'GSM4471799': [0.0, 1.0], 'GSM4471800': [0.0, 1.0], 'GSM4471801': [0.0, 0.0], 'GSM4471802': [0.0, 0.0], 'GSM4471803': [0.0, 0.0], 'GSM4471804': [0.0, 0.0], 'GSM4471805': [0.0, 0.0], 'GSM4471806': [0.0, 1.0], 'GSM4471807': [0.0, 0.0], 'GSM4471808': [0.0, 1.0], 'GSM4471809': [0.0, 0.0], 'GSM4471810': [0.0, 1.0], 'GSM4471811': [0.0, 1.0], 'GSM4471812': [0.0, 0.0], 'GSM4471813': [0.0, 1.0], 'GSM4471814': [0.0, 1.0], 'GSM4471815': [0.0, 1.0], 'GSM4471816': [0.0, 0.0], 'GSM4471817': [0.0, 0.0], 'GSM4471818': [0.0, 1.0], 'GSM4471819': [0.0, 0.0], 'GSM4471820': [0.0, 0.0], 'GSM4471821': [0.0, 1.0], 'GSM4471822': [0.0, 1.0], 'GSM4471823': [0.0, 0.0], 'GSM4471824': [0.0, 0.0], 'GSM4471825': [0.0, 0.0], 'GSM4471826': [0.0, 0.0], 'GSM4471827': [0.0, 0.0], 'GSM4471828': [0.0, 1.0], 'GSM4471829': [0.0, 0.0], 'GSM4471830': [0.0, 1.0], 'GSM4471831': [0.0, 0.0], 'GSM4471832': [0.0, 0.0], 'GSM4471833': [0.0, 1.0], 'GSM4471834': [0.0, 1.0], 'GSM4471835': [0.0, 1.0], 'GSM4471836': [0.0, 1.0], 'GSM4471837': [0.0, 0.0], 'GSM4471838': [0.0, 0.0], 'GSM4471839': [0.0, 1.0], 'GSM4471840': [0.0, 0.0], 'GSM4471841': [0.0, 0.0], 'GSM4471842': [0.0, 0.0], 'GSM4471843': [0.0, 0.0], 'GSM4471844': [0.0, 0.0], 'GSM4471845': [0.0, 1.0], 'GSM4471846': [0.0, 0.0], 'GSM4471847': [0.0, 0.0], 'GSM4471848': [0.0, 1.0], 'GSM4471849': [0.0, 1.0], 'GSM4471850': [0.0, 1.0], 'GSM4471851': [0.0, 1.0], 'GSM4471852': [0.0, 0.0], 'GSM4471853': [0.0, 1.0], 'GSM4471854': [0.0, 1.0], 'GSM4471855': [0.0, 0.0], 'GSM4471856': [0.0, 0.0], 'GSM4471857': [0.0, 1.0], 'GSM4471858': [0.0, 0.0], 'GSM4471859': [0.0, 0.0], 'GSM4471860': [0.0, 1.0]}\n",
      "Clinical data saved to ../../output/preprocess/Autism_spectrum_disorder_(ASD)/clinical_data/GSE148450.csv\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Normalized gene data saved to ../../output/preprocess/Autism_spectrum_disorder_(ASD)/gene_data/GSE148450.csv\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "For the feature 'Autism_spectrum_disorder_(ASD)', the least common label is '1.0' with 56 occurrences. This represents 35.22% of the dataset.\n",
      "For the feature 'Gender', the least common label is '0.0' with 68 occurrences. This represents 42.77% of the dataset.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Linked data saved to ../../output/preprocess/Autism_spectrum_disorder_(ASD)/GSE148450.csv\n"
     ]
    }
   ],
   "source": [
    "# 1. We need to first create the selected_clinical_df using clinical_data from Step 1\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",
    "preview = preview_df(selected_clinical_df)\n",
    "print(\"Preview of selected clinical features:\")\n",
    "print(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)\n",
    "print(f\"Clinical data saved to {out_clinical_data_file}\")\n",
    "\n",
    "# 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",
    "# Create directory if it doesn't exist\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 with the 'geo_link_clinical_genetic_data' function from the library.\n",
    "linked_data = geo_link_clinical_genetic_data(selected_clinical_df, normalized_gene_data)\n",
    "\n",
    "# 3. Handle missing values in the linked data\n",
    "linked_data = handle_missing_values(linked_data, trait)\n",
    "\n",
    "# 4. Determine whether the trait and demographic features are severely 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",
    "# 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",
    "        unbiased_linked_data = unbiased_linked_data.drop(columns=['Age'])\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",
    "        unbiased_linked_data = unbiased_linked_data.drop(columns=['Gender'])\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=\"Dataset contains gene expression data from iPSC-derived neurons of ASD patients and unaffected siblings.\"\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(\"The dataset was determined to be not usable for analysis.\")"
   ]
  }
 ],
 "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
}