File size: 14,115 Bytes
0d7438c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "3ff89209",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T05:47:07.682892Z",
     "iopub.status.busy": "2025-03-25T05:47:07.682779Z",
     "iopub.status.idle": "2025-03-25T05:47:07.849334Z",
     "shell.execute_reply": "2025-03-25T05:47:07.849013Z"
    }
   },
   "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 = \"Huntingtons_Disease\"\n",
    "cohort = \"GSE95843\"\n",
    "\n",
    "# Input paths\n",
    "in_trait_dir = \"../../input/GEO/Huntingtons_Disease\"\n",
    "in_cohort_dir = \"../../input/GEO/Huntingtons_Disease/GSE95843\"\n",
    "\n",
    "# Output paths\n",
    "out_data_file = \"../../output/preprocess/Huntingtons_Disease/GSE95843.csv\"\n",
    "out_gene_data_file = \"../../output/preprocess/Huntingtons_Disease/gene_data/GSE95843.csv\"\n",
    "out_clinical_data_file = \"../../output/preprocess/Huntingtons_Disease/clinical_data/GSE95843.csv\"\n",
    "json_path = \"../../output/preprocess/Huntingtons_Disease/cohort_info.json\"\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f2e4b034",
   "metadata": {},
   "source": [
    "### Step 1: Initial Data Loading"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "7de7bbdb",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T05:47:07.850713Z",
     "iopub.status.busy": "2025-03-25T05:47:07.850570Z",
     "iopub.status.idle": "2025-03-25T05:47:07.976322Z",
     "shell.execute_reply": "2025-03-25T05:47:07.976013Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Background Information:\n",
      "!Series_title\t\"Gene expression from embryoid bodies derived from HBG3 HB9 ES cells\"\n",
      "!Series_summary\t\"This SuperSeries is composed of the SubSeries listed below.\"\n",
      "!Series_overall_design\t\"Refer to individual Series\"\n",
      "Sample Characteristics Dictionary:\n",
      "{0: ['strain: F1 progeny of two different strains (C57BL/6 and SJL)', 'strain: B6CBA-Tg(HDexon1)62Gpb/3J'], 1: ['time point: 9 weeks cultures', 'time point: 10 weeks cultures', \"treatment: plasma from a Huntington's disease mouse model\"]}\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": "33dc60b8",
   "metadata": {},
   "source": [
    "### Step 2: Dataset Analysis and Clinical Feature Extraction"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "545b1d9c",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T05:47:07.977859Z",
     "iopub.status.busy": "2025-03-25T05:47:07.977606Z",
     "iopub.status.idle": "2025-03-25T05:47:08.000304Z",
     "shell.execute_reply": "2025-03-25T05:47:07.999978Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 1. Gene Expression Data Availability\n",
    "# Based on the background information and sample characteristics, it appears this dataset contains\n",
    "# information about amyloid beta levels, but not gene expression data\n",
    "is_gene_available = False\n",
    "\n",
    "# 2. Variable Availability and Data Type Conversion\n",
    "# Looking at the sample characteristics dictionary, we don't see any clear information about \n",
    "# Huntington's Disease, age, or gender\n",
    "\n",
    "# 2.1 Data Availability\n",
    "trait_row = None  # No information about Huntington's Disease in the samples\n",
    "age_row = None    # No age information available\n",
    "gender_row = None # No gender information available\n",
    "\n",
    "# 2.2 Data Type Conversion (defining functions even though data is unavailable)\n",
    "def convert_trait(value):\n",
    "    \"\"\"Convert trait value to binary (0 for control, 1 for Huntington's Disease)\"\"\"\n",
    "    if value is None:\n",
    "        return None\n",
    "    value = value.lower() if isinstance(value, str) else str(value).lower()\n",
    "    if \":\" in value:\n",
    "        value = value.split(\":\", 1)[1].strip()\n",
    "    if \"disease\" in value or \"patient\" in value or \"case\" in value or \"huntington\" in value or \"hd\" in value:\n",
    "        return 1\n",
    "    elif \"control\" in value or \"healthy\" in value or \"normal\" in value:\n",
    "        return 0\n",
    "    return None\n",
    "\n",
    "def convert_age(value):\n",
    "    \"\"\"Convert age value to continuous\"\"\"\n",
    "    if value is None:\n",
    "        return None\n",
    "    if \":\" in value:\n",
    "        value = value.split(\":\", 1)[1].strip()\n",
    "    try:\n",
    "        return float(value)\n",
    "    except:\n",
    "        return None\n",
    "\n",
    "def convert_gender(value):\n",
    "    \"\"\"Convert gender value to binary (0 for female, 1 for male)\"\"\"\n",
    "    if value is None:\n",
    "        return None\n",
    "    value = value.lower() if isinstance(value, str) else str(value).lower()\n",
    "    if \":\" in value:\n",
    "        value = value.split(\":\", 1)[1].strip()\n",
    "    if \"female\" in value or \"f\" == value.strip():\n",
    "        return 0\n",
    "    elif \"male\" in value or \"m\" == value.strip():\n",
    "        return 1\n",
    "    return None\n",
    "\n",
    "# 3. Save Metadata\n",
    "# We'll set is_trait_available to False as we couldn't find trait information\n",
    "is_trait_available = trait_row is not None\n",
    "validate_and_save_cohort_info(is_final=False, cohort=cohort, info_path=json_path, \n",
    "                             is_gene_available=is_gene_available, \n",
    "                             is_trait_available=is_trait_available)\n",
    "\n",
    "# 4. Clinical Feature Extraction - Skip this step as trait_row is None\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "88d4e288",
   "metadata": {},
   "source": [
    "### Step 3: Gene Data Extraction"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "dbcd1c7c",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T05:47:08.001603Z",
     "iopub.status.busy": "2025-03-25T05:47:08.001500Z",
     "iopub.status.idle": "2025-03-25T05:47:08.206862Z",
     "shell.execute_reply": "2025-03-25T05:47:08.206481Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Matrix file found: ../../input/GEO/Huntingtons_Disease/GSE95843/GSE95843-GPL23148_series_matrix.txt.gz\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Gene data shape: (19620, 65)\n",
      "First 20 gene/probe identifiers:\n",
      "Index(['a', 'A030009H04Rik', 'A130010J15Rik', 'A130023I24Rik', 'A1bg', 'A1cf',\n",
      "       'A230006K03Rik', 'A230046K03Rik', 'A230050P20Rik', 'A230051G13Rik',\n",
      "       'A230051N06Rik', 'A230083G16Rik', 'A2ld1', 'A2m', 'A330021E22Rik',\n",
      "       'A330049M08Rik', 'A330070K13Rik', 'A3galt2', 'A430005L14Rik',\n",
      "       'A430033K04Rik'],\n",
      "      dtype='object', name='ID')\n"
     ]
    }
   ],
   "source": [
    "# 1. Get the SOFT and matrix file paths again \n",
    "soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)\n",
    "print(f\"Matrix file found: {matrix_file}\")\n",
    "\n",
    "# 2. Use the get_genetic_data function from the library to get the gene_data\n",
    "try:\n",
    "    gene_data = get_genetic_data(matrix_file)\n",
    "    print(f\"Gene data shape: {gene_data.shape}\")\n",
    "    \n",
    "    # 3. Print the first 20 row IDs (gene or probe identifiers)\n",
    "    print(\"First 20 gene/probe identifiers:\")\n",
    "    print(gene_data.index[:20])\n",
    "except Exception as e:\n",
    "    print(f\"Error extracting gene data: {e}\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e6ea8fda",
   "metadata": {},
   "source": [
    "### Step 4: Gene Identifier Review"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "ab3add52",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T05:47:08.208307Z",
     "iopub.status.busy": "2025-03-25T05:47:08.208190Z",
     "iopub.status.idle": "2025-03-25T05:47:08.210136Z",
     "shell.execute_reply": "2025-03-25T05:47:08.209839Z"
    }
   },
   "outputs": [],
   "source": [
    "# Based on the provided output, the gene identifiers appear to be human gene symbols.\n",
    "# These are standard gene symbols like A1BG, A2M, AAAS, etc. that match official human gene nomenclature.\n",
    "# They are not probe IDs (which would typically be numeric or have manufacturer prefixes)\n",
    "# and they are not other types of identifiers that would require mapping.\n",
    "\n",
    "requires_gene_mapping = False\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b08f86be",
   "metadata": {},
   "source": [
    "### Step 5: Data Normalization and Linking"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "939f6a7f",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T05:47:08.211437Z",
     "iopub.status.busy": "2025-03-25T05:47:08.211334Z",
     "iopub.status.idle": "2025-03-25T05:47:09.069110Z",
     "shell.execute_reply": "2025-03-25T05:47:09.068706Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Normalizing gene symbols...\n",
      "Gene data shape after normalization: (14859, 65)\n",
      "First 10 normalized gene symbols:\n",
      "Index(['A1BG', 'A1CF', 'A2M', 'A3GALT2', 'A4GALT', 'A4GNT', 'AAAS', 'AACS',\n",
      "       'AADAC', 'AADACL3'],\n",
      "      dtype='object', name='ID')\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Normalized gene data saved to: ../../output/preprocess/Huntingtons_Disease/gene_data/GSE95843.csv\n",
      "\n",
      "Validating cohort usability...\n",
      "Dataset usability: False\n",
      "Dataset lacks trait information for Huntington's Disease, so no linked data will be saved.\n"
     ]
    }
   ],
   "source": [
    "# Based on previous steps, this dataset does not contain Huntington's Disease trait information\n",
    "# and trait_row was found to be None, so we need to adjust our approach\n",
    "\n",
    "# 1. First, normalize the gene symbols in the gene data we extracted in Step 3\n",
    "print(\"Normalizing gene symbols...\")\n",
    "# Make sure output directory exists\n",
    "os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True)\n",
    "\n",
    "# Normalize gene symbols directly from gene_data variable obtained in Step 3\n",
    "normalized_gene_data = normalize_gene_symbols_in_index(gene_data)\n",
    "print(f\"Gene data shape after normalization: {normalized_gene_data.shape}\")\n",
    "print(\"First 10 normalized gene symbols:\")\n",
    "print(normalized_gene_data.index[:10])\n",
    "\n",
    "# Save the normalized gene data\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. Since we don't have trait data, we need to prepare a proper dataframe for validation\n",
    "# and set is_biased=True since without trait data, the dataset is biased/unusable for trait analysis\n",
    "dummy_df = normalized_gene_data.iloc[:5, :5].reset_index()  # Create small sample for efficiency\n",
    "is_biased = True  # Without trait data, the dataset is considered biased/unusable\n",
    "\n",
    "print(\"\\nValidating cohort usability...\")\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,  # We did find gene expression data\n",
    "    is_trait_available=False,  # Previous steps found no trait information\n",
    "    is_biased=is_biased,  # Dataset is biased without trait data\n",
    "    df=dummy_df,  # Use a small sample of the data for validation\n",
    "    note=\"This dataset contains gene expression data but lacks Huntington's Disease trait information.\"\n",
    ")\n",
    "\n",
    "print(f\"Dataset usability: {is_usable}\")\n",
    "print(\"Dataset lacks trait information for Huntington's Disease, so no linked data will be 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
}