File size: 29,574 Bytes
58f02a5 |
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 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "9ed4b723",
"metadata": {
"execution": {
"iopub.execute_input": "2025-03-25T06:13:43.817050Z",
"iopub.status.busy": "2025-03-25T06:13:43.816929Z",
"iopub.status.idle": "2025-03-25T06:13:43.979757Z",
"shell.execute_reply": "2025-03-25T06:13:43.979238Z"
}
},
"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 = \"Polycystic_Ovary_Syndrome\"\n",
"cohort = \"GSE151158\"\n",
"\n",
"# Input paths\n",
"in_trait_dir = \"../../input/GEO/Polycystic_Ovary_Syndrome\"\n",
"in_cohort_dir = \"../../input/GEO/Polycystic_Ovary_Syndrome/GSE151158\"\n",
"\n",
"# Output paths\n",
"out_data_file = \"../../output/preprocess/Polycystic_Ovary_Syndrome/GSE151158.csv\"\n",
"out_gene_data_file = \"../../output/preprocess/Polycystic_Ovary_Syndrome/gene_data/GSE151158.csv\"\n",
"out_clinical_data_file = \"../../output/preprocess/Polycystic_Ovary_Syndrome/clinical_data/GSE151158.csv\"\n",
"json_path = \"../../output/preprocess/Polycystic_Ovary_Syndrome/cohort_info.json\"\n"
]
},
{
"cell_type": "markdown",
"id": "5f881720",
"metadata": {},
"source": [
"### Step 1: Initial Data Loading"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "6975f6d3",
"metadata": {
"execution": {
"iopub.execute_input": "2025-03-25T06:13:43.981476Z",
"iopub.status.busy": "2025-03-25T06:13:43.981293Z",
"iopub.status.idle": "2025-03-25T06:13:44.008949Z",
"shell.execute_reply": "2025-03-25T06:13:44.008482Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Background Information:\n",
"!Series_title\t\"Transcriptional analysis of non-fibrotic NAFLD progression\"\n",
"!Series_summary\t\"Background & Aims: Non-alcoholic steatohepatitis (NASH), a subtype of non-alcoholic fatty liver disease (NAFLD) that can lead to fibrosis, cirrhosis, and hepatocellular carcinoma, is characterized by hepatic inflammation. Despite evolving therapies aimed to ameliorate inflammation in NASH, the transcriptional changes that lead to inflammation progression in NAFLD remain poorly understood. The aim of this study is to define transcriptional changes in early, non-fibrotic NAFLD using a biopsy-proven non-fibrotic NAFLD cohort. Methods: We extracted RNA from liver tissue of 40 patients with biopsy-proven NAFLD based on NAFLD Activity Score (NAS) (23 with NAS ≤3, 17 with NAS ≥5) and 21 healthy controls and compared changes in expression of 594 genes involved in innate immune function. Results: Compared to healthy controls, NAFLD patients with NAS ≥5 had differential expression of 211 genes, while those with NAS ≤3 had differential expression of only 14 genes. Notably, osteopontin (SPP1) (3.74-fold in NAS ≤3, 8.28-fold in NAS ≥5) and CXCL10 (2.27-fold in NAS ≤3, 8.28-fold in NAS ≥5) gene expression were significantly upregulated with histologic progression of NAFLD.\"\n",
"!Series_overall_design\t\"We extracted RNA from liver tissue of 40 patients with biopsy-proven NAFLD based on NAFLD Activity Score (NAS) (23 with NAS ≤3, 17 with NAS ≥5) and 21 healthy controls (protocol biopsy obtained during living liver donation) and compared changes in expression of 594 genes involved in innate immune function\"\n",
"Sample Characteristics Dictionary:\n",
"{0: ['tissue: liver', 'sample type: blank'], 1: ['age: 53', 'age: 40', 'age: 51', 'age: 36', 'age: 44', 'age: 60', 'age: 31', 'age: 41', 'age: 55', 'age: 15', 'age: 57', 'age: 56', 'age: 34', 'age: 43', 'age: 49', 'age: 52', 'age: 35', 'age: 42', 'age: 33', 'age: 48', 'age: 47', 'age: 65', 'age: 59', 'age: 61', 'age: 28', 'age: 46', 'age: 25', 'age: 27', 'age: 54', 'age: 37'], 2: ['Sex: F', 'Sex: M', nan], 3: ['ethnicity: White', 'ethnicity: Hispanic', 'ethnicity: AA', nan], 4: ['bmi: 28.4', 'bmi: 37.8', 'bmi: 33.1', 'bmi: 39.6', 'bmi: 31.5', 'bmi: 29.9', 'bmi: 39.9', 'bmi: 33.3', 'bmi: 41.1', 'bmi: 62.9', 'bmi: 47.6', 'bmi: 31.7', 'bmi: 53.4', 'bmi: 31.4', 'bmi: 23.9', 'bmi: 22.4', 'bmi: 23.7', 'bmi: 28', 'bmi: 27.8', 'bmi: 37.7', 'bmi: 36.1', 'bmi: 36.7', 'bmi: 39.4', 'bmi: 36.8', 'bmi: 29.2', 'bmi: 35.2', 'bmi: 38.4', 'bmi: 30.8', 'bmi: 29', 'bmi: 47.8'], 5: ['dm2: N', 'dm2: Y', nan], 6: ['insulin: N', 'insulin: Y', nan], 7: ['hypertension: N', 'hypertension: Y', nan], 8: ['hyperlipidemia: N', 'hyperlipidemia: Y', nan], 9: ['statin/fibrate: N', 'statin/fibrate: Y', nan], 10: ['osa: N', 'osa: Y', nan], 11: ['pcos: N', nan], 12: ['hypothyroidism: N', 'hypothyroidism: Y', nan], 13: ['cardiovascular disease: N', 'cardiovascular disease: Y', nan], 14: ['ast (units/l): 77', 'ast (units/l): 66', 'ast (units/l): 64', 'ast (units/l): 68', 'ast (units/l): 21', 'ast (units/l): 51', 'ast (units/l): 174', 'ast (units/l): 58', 'ast (units/l): 45', 'ast (units/l): 19', 'ast (units/l): 41', 'ast (units/l): 24', 'ast (units/l): 49', 'ast (units/l): 26', 'ast (units/l): 16', 'ast (units/l): 15', 'ast (units/l): 17', 'ast (units/l): 20', 'ast (units/l): 27', 'ast (units/l): 305', 'ast (units/l): 43', 'ast (units/l): 75', 'ast (units/l): 67', 'ast (units/l): 118', 'ast (units/l): 69', 'ast (units/l): 59', 'ast (units/l): 31', 'ast (units/l): 18', 'ast (units/l): 33', 'ast (units/l): 37'], 15: ['alt (units/l): 129', 'alt (units/l): 123', 'alt (units/l): 84', 'alt (units/l): 120', 'alt (units/l): 28', 'alt (units/l): 88', 'alt (units/l): 429', 'alt (units/l): 66', 'alt (units/l): 26', 'alt (units/l): 40', 'alt (units/l): 46', 'alt (units/l): 94', 'alt (units/l): 72', 'alt (units/l): 17', 'alt (units/l): 12', 'alt (units/l): 27', 'alt (units/l): 3', 'alt (units/l): 16', 'alt (units/l): 70', 'alt (units/l): 301', 'alt (units/l): 6', 'alt (units/l): 102', 'alt (units/l): 97', 'alt (units/l): 110', 'alt (units/l): 89', 'alt (units/l): 44', 'alt (units/l): 42', 'alt (units/l): 33', 'alt (units/l): 52', 'alt (units/l): 31'], 16: ['alkaline phosphatase (units/l): 114', 'alkaline phosphatase (units/l): 60', 'alkaline phosphatase (units/l): 91', 'alkaline phosphatase (units/l): 130', 'alkaline phosphatase (units/l): 120', 'alkaline phosphatase (units/l): 58', 'alkaline phosphatase (units/l): 78', 'alkaline phosphatase (units/l): 83', 'alkaline phosphatase (units/l): 89', 'alkaline phosphatase (units/l): 95', 'alkaline phosphatase (units/l): 150', 'alkaline phosphatase (units/l): 131', 'alkaline phosphatase (units/l): 52', 'alkaline phosphatase (units/l): 72', 'alkaline phosphatase (units/l): 65', 'alkaline phosphatase (units/l): 94', 'alkaline phosphatase (units/l): 62', 'alkaline phosphatase (units/l): 105', 'alkaline phosphatase (units/l): 71', 'alkaline phosphatase (units/l): 76', 'alkaline phosphatase (units/l): 74', 'alkaline phosphatase (units/l): 90', 'nas: Steatosis: 2', 'alkaline phosphatase (units/l): 117', 'alkaline phosphatase (units/l): 48', 'alkaline phosphatase (units/l): 41', 'alkaline phosphatase (units/l): 93', 'alkaline phosphatase (units/l): 46', 'alkaline phosphatase (units/l): 67', 'alkaline phosphatase (units/l): 66'], 17: ['total bilirubin (mg/dl): 0.4', 'total bilirubin (mg/dl): 0.7', 'total bilirubin (mg/dl): 1.1', 'total bilirubin (mg/dl): 0.6', 'total bilirubin (mg/dl): 0.5', 'total bilirubin (mg/dl): 1', 'total bilirubin (mg/dl): 0.3', 'total bilirubin (mg/dl): 0.8', 'total bilirubin (mg/dl): 1.4', 'total bilirubin (mg/dl): 1.5', 'nas: Ballooning: 2', 'total bilirubin (mg/dl): 0.2', 'total bilirubin (mg/dl): 0.9', nan], 18: ['albumin (g/dl): 4.3', 'albumin (g/dl): 4.4', 'albumin (g/dl): 4.2', 'albumin (g/dl): 4.7', 'albumin (g/dl): 4', 'albumin (g/dl): 5.2', 'albumin (g/dl): 4.1', 'albumin (g/dl): 4.5', 'albumin (g/dl): 3.5', 'albumin (g/dl): 3.6', 'albumin (g/dl): 3.8', 'nas: Lobular inflammation: 1', 'albumin (g/dl): 3.9', 'albumin (g/dl): 3.7', 'albumin (g/dl): 3.2', 'albumin (g/dl): 4.9', 'albumin (g/dl): 4.6', nan], 19: ['total protein (g/dl): 8.2', 'total protein (g/dl): 7.7', 'total protein (g/dl): 7.2', 'total protein (g/dl): 8', 'total protein (g/dl): 7.6', 'total protein (g/dl): 8.7', 'total protein (g/dl): 7.1', 'total protein (g/dl): 7.9', 'total protein (g/dl): 6.8', 'total protein (g/dl): 6.5', 'total protein (g/dl): 7', 'total protein (g/dl): 7.3', 'total protein (g/dl): 6.6', 'total protein (g/dl): 7.5', 'nas: Total score: 5', 'total protein (g/dl): 7.8', 'total protein (g/dl): 7.4', 'total protein (g/dl): 6.9', 'total protein (g/dl): 8.1', 'total protein (g/dl): 8.4', 'total protein (g/dl): 6.3', 'total protein (g/dl): 6.7', nan], 20: ['nas: Steatosis: 1', 'nas: Steatosis: 2', 'nas: Steatosis: 3', 'nas: Steatosis: 0', nan], 21: ['nas: Ballooning: 2', 'nas: Ballooning: 1', 'nas: Ballooning: 0', nan], 22: ['nas: Lobular inflammation: 2', 'nas: Lobular inflammation: 1', 'nas: Lobular inflammation: 0', 'nas: Lobular inflammation: 3', nan], 23: ['nas: Total score: 5', 'nas: Total score: 6', 'nas: Total score: 3', 'nas: Total score: 0', nan, 'nas: Total score: 2']}\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": "3c88b184",
"metadata": {},
"source": [
"### Step 2: Dataset Analysis and Clinical Feature Extraction"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "e0465093",
"metadata": {
"execution": {
"iopub.execute_input": "2025-03-25T06:13:44.010543Z",
"iopub.status.busy": "2025-03-25T06:13:44.010398Z",
"iopub.status.idle": "2025-03-25T06:13:44.016341Z",
"shell.execute_reply": "2025-03-25T06:13:44.015894Z"
}
},
"outputs": [],
"source": [
"import pandas as pd\n",
"import numpy as np\n",
"import os\n",
"import re\n",
"\n",
"# 1. Gene Expression Data Availability\n",
"# Based on the background information and series title, this appears to be transcriptional analysis\n",
"# which suggests gene expression data is available\n",
"is_gene_available = True\n",
"\n",
"# 2. Variable Availability and Data Type Conversion\n",
"\n",
"# 2.1 Data Availability\n",
"# For Polycystic Ovary Syndrome (PCOS), I found row 11 with 'pcos: N' values\n",
"trait_row = 11\n",
"\n",
"# For age, I found row 1 with age values\n",
"age_row = 1\n",
"\n",
"# For gender, I found row 2 with 'Sex: F' and 'Sex: M' values\n",
"gender_row = 2\n",
"\n",
"# 2.2 Data Type Conversion Functions\n",
"\n",
"def convert_trait(value):\n",
" \"\"\"Convert PCOS trait values to binary (0 or 1)\"\"\"\n",
" if pd.isna(value):\n",
" return None\n",
" \n",
" # Extract value after colon\n",
" if \":\" in value:\n",
" value = value.split(\":\", 1)[1].strip()\n",
" \n",
" # PCOS: Y would be 1, PCOS: N would be 0\n",
" if value.upper() == 'Y':\n",
" return 1\n",
" elif value.upper() == 'N':\n",
" return 0\n",
" else:\n",
" return None\n",
"\n",
"def convert_age(value):\n",
" \"\"\"Convert age to a continuous value\"\"\"\n",
" if pd.isna(value):\n",
" return None\n",
" \n",
" # Extract value after colon\n",
" if \":\" in value:\n",
" value = value.split(\":\", 1)[1].strip()\n",
" \n",
" try:\n",
" return float(value)\n",
" except:\n",
" return None\n",
"\n",
"def convert_gender(value):\n",
" \"\"\"Convert gender to binary (0 for female, 1 for male)\"\"\"\n",
" if pd.isna(value):\n",
" return None\n",
" \n",
" # Extract value after colon\n",
" if \":\" in value:\n",
" value = value.split(\":\", 1)[1].strip()\n",
" \n",
" if value.upper() == 'F':\n",
" return 0\n",
" elif value.upper() == 'M':\n",
" return 1\n",
" else:\n",
" return None\n",
"\n",
"# 3. Save Metadata\n",
"# Trait data is available if trait_row is not None\n",
"is_trait_available = trait_row is not None\n",
"\n",
"# Initial filtering\n",
"validate_and_save_cohort_info(\n",
" is_final=False,\n",
" cohort=cohort,\n",
" info_path=json_path,\n",
" is_gene_available=is_gene_available,\n",
" is_trait_available=is_trait_available\n",
")\n",
"\n",
"# 4. Clinical Feature Extraction\n",
"if trait_row is not None:\n",
" # Load the clinical data\n",
" clinical_data_path = os.path.join(in_cohort_dir, \"clinical_data.csv\")\n",
" if os.path.exists(clinical_data_path):\n",
" clinical_data = pd.read_csv(clinical_data_path)\n",
" \n",
" # Extract clinical features\n",
" clinical_features = 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 data\n",
" preview = preview_df(clinical_features)\n",
" print(\"Clinical Features Preview:\")\n",
" print(preview)\n",
" \n",
" # Create directory if it doesn't exist\n",
" os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)\n",
" \n",
" # Save as CSV\n",
" clinical_features.to_csv(out_clinical_data_file, index=False)\n",
" print(f\"Clinical data saved to: {out_clinical_data_file}\")\n"
]
},
{
"cell_type": "markdown",
"id": "55309289",
"metadata": {},
"source": [
"### Step 3: Gene Data Extraction"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "9540a2cb",
"metadata": {
"execution": {
"iopub.execute_input": "2025-03-25T06:13:44.017814Z",
"iopub.status.busy": "2025-03-25T06:13:44.017702Z",
"iopub.status.idle": "2025-03-25T06:13:44.031675Z",
"shell.execute_reply": "2025-03-25T06:13:44.031202Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Matrix file found: ../../input/GEO/Polycystic_Ovary_Syndrome/GSE151158/GSE151158_series_matrix.txt.gz\n",
"Gene data shape: (618, 66)\n",
"First 20 gene/probe identifiers:\n",
"Index(['ABCB1', 'ABCF1', 'ABL1', 'ADA', 'AHR', 'AICDA', 'AIRE', 'ALAS1', 'APP',\n",
" 'AREG', 'ARG1', 'ARG2', 'ARHGDIB', 'ATG10', 'ATG12', 'ATG16L1', 'ATG5',\n",
" 'ATG7', 'ATM', 'B2M'],\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": "a499cb19",
"metadata": {},
"source": [
"### Step 4: Gene Identifier Review"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "ceebf6a5",
"metadata": {
"execution": {
"iopub.execute_input": "2025-03-25T06:13:44.033138Z",
"iopub.status.busy": "2025-03-25T06:13:44.033028Z",
"iopub.status.idle": "2025-03-25T06:13:44.035147Z",
"shell.execute_reply": "2025-03-25T06:13:44.034721Z"
}
},
"outputs": [],
"source": [
"# These identifiers (ABCB1, ABCF1, etc.) are indeed standard human gene symbols.\n",
"# No additional mapping is required as they are already in the proper format for human genes.\n",
"\n",
"requires_gene_mapping = False\n"
]
},
{
"cell_type": "markdown",
"id": "403ee1a2",
"metadata": {},
"source": [
"### Step 5: Data Normalization and Linking"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "781ca83b",
"metadata": {
"execution": {
"iopub.execute_input": "2025-03-25T06:13:44.036666Z",
"iopub.status.busy": "2025-03-25T06:13:44.036558Z",
"iopub.status.idle": "2025-03-25T06:13:44.263213Z",
"shell.execute_reply": "2025-03-25T06:13:44.262644Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Clinical data saved to: ../../output/preprocess/Polycystic_Ovary_Syndrome/clinical_data/GSE151158.csv\n",
"Clinical data preview:\n",
"{'GSM4567420': [0.0, 0.0], 'GSM4567421': [0.0, 0.0], 'GSM4567422': [0.0, 0.0], 'GSM4567423': [0.0, 0.0], 'GSM4567424': [0.0, 0.0], 'GSM4567425': [0.0, 0.0], 'GSM4567426': [0.0, 0.0], 'GSM4567427': [0.0, 0.0], 'GSM4567428': [0.0, 0.0], 'GSM4567429': [0.0, 0.0], 'GSM4567430': [0.0, 0.0], 'GSM4567431': [0.0, 0.0], 'GSM4567432': [0.0, 0.0], 'GSM4567433': [0.0, 0.0], 'GSM4567434': [0.0, 0.0], 'GSM4567435': [0.0, 0.0], 'GSM4567436': [0.0, 0.0], 'GSM4567437': [0.0, 0.0], 'GSM4567438': [0.0, 0.0], 'GSM4567439': [0.0, 0.0], 'GSM4567440': [0.0, 0.0], 'GSM4567441': [0.0, 0.0], 'GSM4567442': [0.0, 0.0], 'GSM4567443': [0.0, 0.0], 'GSM4567444': [0.0, 0.0], 'GSM4567445': [0.0, 0.0], 'GSM4567446': [0.0, 0.0], 'GSM4567447': [0.0, 0.0], 'GSM4567448': [0.0, 0.0], 'GSM4567449': [0.0, 0.0], 'GSM4567450': [0.0, 0.0], 'GSM4567451': [0.0, 0.0], 'GSM4567452': [0.0, 0.0], 'GSM4567453': [0.0, 0.0], 'GSM4567454': [0.0, 0.0], 'GSM4567455': [0.0, 0.0], 'GSM4567456': [0.0, 0.0], 'GSM4567457': [0.0, 0.0], 'GSM4567458': [0.0, 0.0], 'GSM4567459': [0.0, 0.0], 'GSM4567460': [0.0, 0.0], 'GSM4567461': [0.0, 0.0], 'GSM4567462': [0.0, 0.0], 'GSM4567463': [0.0, 0.0], 'GSM4567464': [0.0, 0.0], 'GSM4567465': [0.0, 0.0], 'GSM4567466': [0.0, 0.0], 'GSM4567467': [0.0, 0.0], 'GSM4567468': [0.0, 0.0], 'GSM4567469': [0.0, 0.0], 'GSM4567470': [0.0, 0.0], 'GSM4567471': [0.0, 0.0], 'GSM4567472': [0.0, 0.0], 'GSM4567473': [0.0, 0.0], 'GSM4567474': [0.0, 0.0], 'GSM4567475': [0.0, 0.0], 'GSM4567476': [0.0, 0.0], 'GSM4567477': [0.0, 0.0], 'GSM4567478': [0.0, 0.0], 'GSM4567479': [0.0, 0.0], 'GSM4567480': [0.0, 0.0], 'GSM4567481': [nan, 0.0], 'GSM4567482': [nan, 0.0], 'GSM4567483': [nan, 0.0], 'GSM4567484': [nan, 0.0], 'GSM4567485': [nan, 0.0]}\n",
"\n",
"Normalizing gene symbols...\n",
"Gene data shape after normalization: (583, 66)\n",
"First 10 normalized gene identifiers:\n",
"Index(['ABCB1', 'ABCF1', 'ABL1', 'ACKR4', 'ADA', 'ADGRE2', 'AHR', 'AICDA',\n",
" 'AIRE', 'ALAS1'],\n",
" dtype='object', name='ID')\n",
"Normalized gene data saved to: ../../output/preprocess/Polycystic_Ovary_Syndrome/gene_data/GSE151158.csv\n",
"\n",
"Linking clinical and genetic data...\n",
"Linked data shape: (66, 585)\n",
"Linked data preview (first 5 rows, 5 columns):\n",
" Polycystic_Ovary_Syndrome Gender ABCB1 ABCF1 ABL1\n",
"GSM4567420 0.0 0.0 1264.86 697.01 435.43\n",
"GSM4567421 0.0 0.0 1958.63 665.75 398.78\n",
"GSM4567422 0.0 0.0 1592.80 592.85 458.84\n",
"GSM4567423 0.0 0.0 1421.80 659.55 493.41\n",
"GSM4567424 0.0 0.0 1661.67 737.55 562.18\n",
"\n",
"Handling missing values...\n",
"Samples with missing trait values: 5 out of 66\n",
"Genes with ≤20% missing values: 583 out of 583\n",
"Samples with ≤5% missing gene values: 66 out of 66\n",
"Linked data shape after handling missing values: (61, 585)\n",
"\n",
"Checking for bias in dataset features...\n",
"Quartiles for 'Polycystic_Ovary_Syndrome':\n",
" 25%: 0.0\n",
" 50% (Median): 0.0\n",
" 75%: 0.0\n",
"Min: 0.0\n",
"Max: 0.0\n",
"The distribution of the feature 'Polycystic_Ovary_Syndrome' in this dataset is severely biased.\n",
"\n",
"For the feature 'Gender', the least common label is '0.0' with 61 occurrences. This represents 100.00% of the dataset.\n",
"The distribution of the feature 'Gender' in this dataset is severely biased.\n",
"\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"A new JSON file was created at: ../../output/preprocess/Polycystic_Ovary_Syndrome/cohort_info.json\n",
"Dataset deemed not usable for associative studies. Linked data not saved.\n"
]
}
],
"source": [
"# 1. First, extract and save the clinical data since it's missing\n",
"# Get the SOFT and matrix file paths\n",
"soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)\n",
"\n",
"# Get the background info and clinical data again\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",
"# Define the conversion functions from Step 2\n",
"def convert_trait(value):\n",
" \"\"\"Convert PCOS trait to binary (0 = control, 1 = PCOS)\"\"\"\n",
" if pd.isna(value):\n",
" return None\n",
" \n",
" # Extract the value after the colon if it exists\n",
" if ':' in value:\n",
" value = value.split(':', 1)[1].strip()\n",
" \n",
" # Convert to binary\n",
" if 'PCOS' in value:\n",
" return 1\n",
" else:\n",
" return 0\n",
"\n",
"def convert_gender(value):\n",
" \"\"\"Convert gender to binary (0 = female, 1 = male)\n",
" Note: In this context, we're dealing with biological sex rather than gender identity\n",
" Female-to-male transsexuals are biologically female (0)\"\"\"\n",
" if pd.isna(value):\n",
" return None\n",
" \n",
" # Extract the value after the colon if it exists\n",
" if ':' in value:\n",
" value = value.split(':', 1)[1].strip()\n",
" \n",
" # Female is 0, Male is 1\n",
" if 'female' in value.lower():\n",
" return 0\n",
" elif 'male' in value.lower() and 'female to male' not in value.lower():\n",
" return 1\n",
" else:\n",
" return 0 # Female to male transsexuals are recorded as female (0) biologically\n",
"\n",
"# Extract clinical features with the correct row indices from previous steps\n",
"trait_row = 1 # Contains \"disease state: PCOS\"\n",
"gender_row = 0 # Contains gender information\n",
"age_row = None # Age information is not available in this dataset\n",
"\n",
"# Process and save clinical data\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=None,\n",
" gender_row=gender_row,\n",
" convert_gender=convert_gender\n",
")\n",
"\n",
"# Create directory if it doesn't exist\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",
"print(\"Clinical data preview:\")\n",
"print(preview_df(selected_clinical_df))\n",
"\n",
"# 2. Normalize gene symbols using synonym information from NCBI\n",
"print(\"\\nNormalizing gene symbols...\")\n",
"gene_data = normalize_gene_symbols_in_index(gene_data)\n",
"print(f\"Gene data shape after normalization: {gene_data.shape}\")\n",
"print(\"First 10 normalized gene identifiers:\")\n",
"print(gene_data.index[:10])\n",
"\n",
"# Save the normalized gene 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\"Normalized gene data saved to: {out_gene_data_file}\")\n",
"\n",
"# 3. Link clinical and genetic data\n",
"print(\"\\nLinking clinical and genetic data...\")\n",
"linked_data = geo_link_clinical_genetic_data(selected_clinical_df, gene_data)\n",
"print(f\"Linked data shape: {linked_data.shape}\")\n",
"print(\"Linked data preview (first 5 rows, 5 columns):\")\n",
"if linked_data.shape[0] > 0 and linked_data.shape[1] > 5:\n",
" print(linked_data.iloc[:5, :5])\n",
"else:\n",
" print(linked_data)\n",
"\n",
"# 4. Handle missing values\n",
"print(\"\\nHandling missing values...\")\n",
"# First check how many samples have missing trait values\n",
"if trait in linked_data.columns:\n",
" missing_trait = linked_data[trait].isna().sum()\n",
" print(f\"Samples with missing trait values: {missing_trait} out of {len(linked_data)}\")\n",
"\n",
"# Check gene missing value percentages\n",
"gene_cols = [col for col in linked_data.columns if col not in [trait, 'Age', 'Gender']]\n",
"gene_missing_pct = linked_data[gene_cols].isna().mean()\n",
"genes_to_keep = gene_missing_pct[gene_missing_pct <= 0.2].index\n",
"print(f\"Genes with ≤20% missing values: {len(genes_to_keep)} out of {len(gene_cols)}\")\n",
"\n",
"# Check sample missing value percentages\n",
"if len(gene_cols) > 0:\n",
" sample_missing_pct = linked_data[gene_cols].isna().mean(axis=1)\n",
" samples_to_keep = sample_missing_pct[sample_missing_pct <= 0.05].index\n",
" print(f\"Samples with ≤5% missing gene values: {len(samples_to_keep)} out of {len(linked_data)}\")\n",
"\n",
"# Apply missing value handling\n",
"linked_data_clean = handle_missing_values(linked_data, trait)\n",
"print(f\"Linked data shape after handling missing values: {linked_data_clean.shape}\")\n",
"\n",
"# 5. Check for bias in the dataset\n",
"print(\"\\nChecking for bias in dataset features...\")\n",
"trait_biased, linked_data_clean = judge_and_remove_biased_features(linked_data_clean, trait)\n",
"\n",
"# 6. Conduct final quality validation\n",
"note = \"This dataset contains gene expression data from ovary biopsies of women with PCOS and female-to-male transsexual individuals, focusing on LH-induced gene expression.\"\n",
"is_gene_available = len(gene_data) > 0\n",
"is_trait_available = trait in linked_data.columns\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=trait_biased,\n",
" df=linked_data_clean,\n",
" note=note\n",
")\n",
"\n",
"# 7. Save the linked data if it's usable\n",
"if is_usable and linked_data_clean.shape[0] > 0:\n",
" os.makedirs(os.path.dirname(out_data_file), exist_ok=True)\n",
" linked_data_clean.to_csv(out_data_file, index=True)\n",
" print(f\"Linked data saved to {out_data_file}\")\n",
"else:\n",
" print(\"Dataset deemed not usable for associative studies. Linked data 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
}
|