File size: 21,112 Bytes
736e4a0 |
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 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "97b9a348",
"metadata": {
"execution": {
"iopub.execute_input": "2025-03-25T06:32:10.911309Z",
"iopub.status.busy": "2025-03-25T06:32:10.911087Z",
"iopub.status.idle": "2025-03-25T06:32:11.078444Z",
"shell.execute_reply": "2025-03-25T06:32:11.078091Z"
}
},
"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 = \"Anxiety_disorder\"\n",
"cohort = \"GSE94119\"\n",
"\n",
"# Input paths\n",
"in_trait_dir = \"../../input/GEO/Anxiety_disorder\"\n",
"in_cohort_dir = \"../../input/GEO/Anxiety_disorder/GSE94119\"\n",
"\n",
"# Output paths\n",
"out_data_file = \"../../output/preprocess/Anxiety_disorder/GSE94119.csv\"\n",
"out_gene_data_file = \"../../output/preprocess/Anxiety_disorder/gene_data/GSE94119.csv\"\n",
"out_clinical_data_file = \"../../output/preprocess/Anxiety_disorder/clinical_data/GSE94119.csv\"\n",
"json_path = \"../../output/preprocess/Anxiety_disorder/cohort_info.json\"\n"
]
},
{
"cell_type": "markdown",
"id": "d177aa0c",
"metadata": {},
"source": [
"### Step 1: Initial Data Loading"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "5858111e",
"metadata": {
"execution": {
"iopub.execute_input": "2025-03-25T06:32:11.079893Z",
"iopub.status.busy": "2025-03-25T06:32:11.079744Z",
"iopub.status.idle": "2025-03-25T06:32:11.171697Z",
"shell.execute_reply": "2025-03-25T06:32:11.171393Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Background Information:\n",
"!Series_title\t\"Gene expression and response to psychological therapy\"\n",
"!Series_summary\t\"This study represents the first investigation of genome-wide expression profiles with respect to psychological treatment outcome. Participants (n=102) with panic disorder or specific phobia received exposure-based CBT. Treatment outcome was defined as percentage reduction from baseline in clinician-rated severity of their primary anxiety diagnosis at post-treatment and six month follow-up. Gene expression was determined from whole blood samples at 3 time-points using the Illumina HT-12v4 BeadChip microarray. No changes in gene expression were significantly associated with treatment outcomes when correcting for multiple testing (q<0.05), although a small number of genes showed a suggestive association with treatment outcome (q<0.5, n=20). Study reports suggestive evidence for the role of a small number of genes in treatment outcome. Although preliminary, the findings contribute to a growing body of research suggesting that response to psychological therapies may be associated with changes at a biological level.\"\n",
"!Series_overall_design\t\"Whole blood RNA was collected from patients (n=102) receiving exposure-based CBT at pre- and post-treatment and at follow-up, for investigation of association with therapy outcome. Includes 9 technical replicates.\"\n",
"Sample Characteristics Dictionary:\n",
"{0: ['gender: FEMALE', 'gender: MALE'], 1: ['tissue: Blood'], 2: ['timepoint: pre', 'timepoint: post', 'timepoint: follow-up']}\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": "6870bcc5",
"metadata": {},
"source": [
"### Step 2: Dataset Analysis and Clinical Feature Extraction"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "24a791d5",
"metadata": {
"execution": {
"iopub.execute_input": "2025-03-25T06:32:11.172835Z",
"iopub.status.busy": "2025-03-25T06:32:11.172725Z",
"iopub.status.idle": "2025-03-25T06:32:11.178179Z",
"shell.execute_reply": "2025-03-25T06:32:11.177886Z"
}
},
"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, this is a microarray study using Illumina HT-12v4 BeadChip\n",
"# for gene expression profiling, so gene expression data should be available\n",
"is_gene_available = True\n",
"\n",
"# 2. Variable Availability and Data Type Conversion\n",
"# 2.1 Data Availability\n",
"\n",
"# For trait (anxiety disorder):\n",
"# The data doesn't explicitly state anxiety disorder status in the characteristics dictionary,\n",
"# but from the background information, we know all participants have either panic disorder or \n",
"# specific phobia, which are types of anxiety disorders. \n",
"# But there's no row key that distinguishes between different anxiety disorders or severity.\n",
"trait_row = None\n",
"\n",
"# For age:\n",
"# There's no age information in the sample characteristics dictionary\n",
"age_row = None\n",
"\n",
"# For gender:\n",
"# Gender is available at index 0\n",
"gender_row = 0\n",
"\n",
"# 2.2 Data Type Conversion\n",
"\n",
"# Since trait data is not available in a usable form for our analysis\n",
"def convert_trait(value):\n",
" return None\n",
"\n",
"# Since age data is not available\n",
"def convert_age(value):\n",
" return None\n",
"\n",
"# Convert gender to binary (0 for female, 1 for male)\n",
"def convert_gender(value):\n",
" if value is None:\n",
" return None\n",
" \n",
" if ':' in value:\n",
" value = value.split(':')[1].strip()\n",
" \n",
" if value.upper() == 'FEMALE':\n",
" return 0\n",
" elif value.upper() == 'MALE':\n",
" return 1\n",
" else:\n",
" return None\n",
"\n",
"# 3. Save Metadata\n",
"# Determine if trait data is available (trait_row is not None)\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 None, this dataset doesn't have the necessary trait data for our analysis,\n",
"# so we skip the clinical feature extraction step\n"
]
},
{
"cell_type": "markdown",
"id": "3a1009c9",
"metadata": {},
"source": [
"### Step 3: Gene Data Extraction"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "ba2fe0ec",
"metadata": {
"execution": {
"iopub.execute_input": "2025-03-25T06:32:11.179217Z",
"iopub.status.busy": "2025-03-25T06:32:11.179111Z",
"iopub.status.idle": "2025-03-25T06:32:11.352214Z",
"shell.execute_reply": "2025-03-25T06:32:11.351740Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"First 20 gene/probe identifiers:\n",
"Index(['ILMN_1343291', 'ILMN_1343295', 'ILMN_1651228', 'ILMN_1651254',\n",
" 'ILMN_1651262', 'ILMN_1651315', 'ILMN_1651347', 'ILMN_1651378',\n",
" 'ILMN_1651405', 'ILMN_1651680', 'ILMN_1651692', 'ILMN_1651705',\n",
" 'ILMN_1651719', 'ILMN_1651735', 'ILMN_1651788', 'ILMN_1651799',\n",
" 'ILMN_1651826', 'ILMN_1651832', 'ILMN_1651850', 'ILMN_1651886'],\n",
" dtype='object', name='ID')\n",
"\n",
"Gene data dimensions: 4381 genes × 315 samples\n"
]
}
],
"source": [
"# 1. Re-identify the SOFT and matrix files to ensure we have the correct paths\n",
"soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)\n",
"\n",
"# 2. Extract the gene expression 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)\n",
"print(\"\\nFirst 20 gene/probe identifiers:\")\n",
"print(gene_data.index[:20])\n",
"\n",
"# 4. Print the dimensions of the gene expression data\n",
"print(f\"\\nGene data dimensions: {gene_data.shape[0]} genes × {gene_data.shape[1]} samples\")\n",
"\n",
"# Note: we keep is_gene_available as True since we successfully extracted gene expression data\n",
"is_gene_available = True\n"
]
},
{
"cell_type": "markdown",
"id": "1c35b8ee",
"metadata": {},
"source": [
"### Step 4: Gene Identifier Review"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "da1cd796",
"metadata": {
"execution": {
"iopub.execute_input": "2025-03-25T06:32:11.353691Z",
"iopub.status.busy": "2025-03-25T06:32:11.353572Z",
"iopub.status.idle": "2025-03-25T06:32:11.355482Z",
"shell.execute_reply": "2025-03-25T06:32:11.355189Z"
}
},
"outputs": [],
"source": [
"# Reviewing the gene identifiers\n",
"\n",
"# The identifiers starting with \"ILMN_\" are Illumina probe IDs, not direct human gene symbols\n",
"# These are probe identifiers used in Illumina microarray platforms and need to be mapped to human gene symbols\n",
"# for proper biological interpretation and cross-platform compatibility\n",
"\n",
"requires_gene_mapping = True\n"
]
},
{
"cell_type": "markdown",
"id": "cdde4ce1",
"metadata": {},
"source": [
"### Step 5: Gene Annotation"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "ebc0e296",
"metadata": {
"execution": {
"iopub.execute_input": "2025-03-25T06:32:11.356699Z",
"iopub.status.busy": "2025-03-25T06:32:11.356594Z",
"iopub.status.idle": "2025-03-25T06:32:15.013158Z",
"shell.execute_reply": "2025-03-25T06:32:15.012763Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Gene annotation preview:\n",
"{'ID': ['ILMN_1343048', 'ILMN_1343049', 'ILMN_1343050', 'ILMN_1343052', 'ILMN_1343059'], 'Species': [nan, nan, nan, nan, nan], 'Source': [nan, nan, nan, nan, nan], 'Search_Key': [nan, nan, nan, nan, nan], 'Transcript': [nan, nan, nan, nan, nan], 'ILMN_Gene': [nan, nan, nan, nan, nan], 'Source_Reference_ID': [nan, nan, nan, nan, nan], 'RefSeq_ID': [nan, nan, nan, nan, nan], 'Unigene_ID': [nan, nan, nan, nan, nan], 'Entrez_Gene_ID': [nan, nan, nan, nan, nan], 'GI': [nan, nan, nan, nan, nan], 'Accession': [nan, nan, nan, nan, nan], 'Symbol': ['phage_lambda_genome', 'phage_lambda_genome', 'phage_lambda_genome:low', 'phage_lambda_genome:low', 'thrB'], 'Protein_Product': [nan, nan, nan, nan, 'thrB'], 'Probe_Id': [nan, nan, nan, nan, nan], 'Array_Address_Id': [5090180.0, 6510136.0, 7560739.0, 1450438.0, 1240647.0], 'Probe_Type': [nan, nan, nan, nan, nan], 'Probe_Start': [nan, nan, nan, nan, nan], 'SEQUENCE': ['GAATAAAGAACAATCTGCTGATGATCCCTCCGTGGATCTGATTCGTGTAA', 'CCATGTGATACGAGGGCGCGTAGTTTGCATTATCGTTTTTATCGTTTCAA', 'CCGACAGATGTATGTAAGGCCAACGTGCTCAAATCTTCATACAGAAAGAT', 'TCTGTCACTGTCAGGAAAGTGGTAAAACTGCAACTCAATTACTGCAATGC', 'CTTGTGCCTGAGCTGTCAAAAGTAGAGCACGTCGCCGAGATGAAGGGCGC'], 'Chromosome': [nan, nan, nan, nan, nan], 'Probe_Chr_Orientation': [nan, nan, nan, nan, nan], 'Probe_Coordinates': [nan, nan, nan, nan, nan], 'Cytoband': [nan, nan, nan, nan, nan], 'Definition': [nan, nan, nan, nan, nan], 'Ontology_Component': [nan, nan, nan, nan, nan], 'Ontology_Process': [nan, nan, nan, nan, nan], 'Ontology_Function': [nan, nan, nan, nan, nan], 'Synonyms': [nan, nan, nan, nan, nan], 'Obsolete_Probe_Id': [nan, nan, nan, nan, nan], 'GB_ACC': [nan, nan, nan, nan, nan]}\n"
]
}
],
"source": [
"# 1. First get the file paths using geo_get_relevant_filepaths function\n",
"soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)\n",
"\n",
"# 2. Use the 'get_gene_annotation' function from the library to get gene annotation data from the SOFT file.\n",
"gene_annotation = get_gene_annotation(soft_file)\n",
"\n",
"# 3. Use the 'preview_df' function from the library to preview the data and print out the results.\n",
"print(\"Gene annotation preview:\")\n",
"print(preview_df(gene_annotation))\n"
]
},
{
"cell_type": "markdown",
"id": "e8e0dadf",
"metadata": {},
"source": [
"### Step 6: Gene Identifier Mapping"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "b56bc108",
"metadata": {
"execution": {
"iopub.execute_input": "2025-03-25T06:32:15.014550Z",
"iopub.status.busy": "2025-03-25T06:32:15.014425Z",
"iopub.status.idle": "2025-03-25T06:32:15.142288Z",
"shell.execute_reply": "2025-03-25T06:32:15.141937Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Gene mapping preview:\n",
"{'ID': ['ILMN_1343048', 'ILMN_1343049', 'ILMN_1343050', 'ILMN_1343052', 'ILMN_1343059'], 'Gene': ['phage_lambda_genome', 'phage_lambda_genome', 'phage_lambda_genome:low', 'phage_lambda_genome:low', 'thrB']}\n",
"\n",
"Gene data after mapping preview:\n",
"Shape: (3270, 315)\n",
"First 5 gene symbols: ['AARS', 'AATF', 'ABCF1', 'ABHD5', 'ABI1']\n"
]
}
],
"source": [
"# Identify the columns in the gene annotation dataframe\n",
"# For the gene identifiers, we need 'ID' column which contains the ILMN_* probe IDs\n",
"# For gene symbols, we need the 'Symbol' column\n",
"\n",
"# 1. Create a gene mapping dataframe with the probe IDs and gene symbols\n",
"gene_mapping = get_gene_mapping(gene_annotation, prob_col='ID', gene_col='Symbol')\n",
"\n",
"# 2. Print a preview of the mapping dataframe to verify\n",
"print(\"Gene mapping preview:\")\n",
"print(preview_df(gene_mapping))\n",
"\n",
"# 3. Apply the gene mapping to convert the probe-level gene expression data to gene-level data\n",
"gene_data = apply_gene_mapping(gene_data, gene_mapping)\n",
"\n",
"# 4. Print a preview of the gene expression data after mapping\n",
"print(\"\\nGene data after mapping preview:\")\n",
"print(f\"Shape: {gene_data.shape}\")\n",
"print(f\"First 5 gene symbols: {list(gene_data.index[:5])}\")\n"
]
},
{
"cell_type": "markdown",
"id": "6ccaa06f",
"metadata": {},
"source": [
"### Step 7: Data Normalization and Linking"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "d22caa05",
"metadata": {
"execution": {
"iopub.execute_input": "2025-03-25T06:32:15.143700Z",
"iopub.status.busy": "2025-03-25T06:32:15.143577Z",
"iopub.status.idle": "2025-03-25T06:32:15.898744Z",
"shell.execute_reply": "2025-03-25T06:32:15.898403Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Gene data after normalization: shape (3207, 315)\n",
"First 5 normalized gene symbols: ['AARS1', 'AATF', 'ABCF1', 'ABHD5', 'ABI1']\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Normalized gene data saved to ../../output/preprocess/Anxiety_disorder/gene_data/GSE94119.csv\n",
"Clinical data saved to ../../output/preprocess/Anxiety_disorder/clinical_data/GSE94119.csv\n",
"Linked data shape: (315, 3208)\n",
"Trait data is unavailable in this dataset.\n",
"Dataset deemed not usable for anxiety disorder association studies due to missing trait measurements.\n"
]
}
],
"source": [
"# 1. Normalize gene symbols in the gene expression data\n",
"try:\n",
" # Use the NCBI gene synonym information to normalize gene symbols\n",
" normalized_gene_data = normalize_gene_symbols_in_index(gene_data)\n",
" print(f\"Gene data after normalization: shape {normalized_gene_data.shape}\")\n",
" print(f\"First 5 normalized gene symbols: {normalized_gene_data.index[:5].tolist()}\")\n",
" \n",
" # Save the normalized gene data\n",
" os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True)\n",
" normalized_gene_data.to_csv(out_gene_data_file)\n",
" print(f\"Normalized gene data saved to {out_gene_data_file}\")\n",
" \n",
" # Use the normalized gene data for subsequent steps\n",
" gene_data_final = normalized_gene_data\n",
"except Exception as e:\n",
" print(f\"Error during normalization: {e}\")\n",
" print(\"Using original gene data instead.\")\n",
" gene_data_final = gene_data\n",
" \n",
" # Save the original gene data\n",
" os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True)\n",
" gene_data_final.to_csv(out_gene_data_file)\n",
" print(f\"Original gene data saved to {out_gene_data_file}\")\n",
"\n",
"# 2. Create clinical data with gender information (since trait data is unavailable)\n",
"if gender_row is not None:\n",
" # Create a DataFrame with just gender information\n",
" gender_data = get_feature_data(clinical_data, gender_row, 'Gender', convert_gender)\n",
" clinical_df = gender_data\n",
" \n",
" # Save clinical data\n",
" os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)\n",
" 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\n",
" linked_data = geo_link_clinical_genetic_data(clinical_df, gene_data_final)\n",
" print(f\"Linked data shape: {linked_data.shape}\")\n",
"else:\n",
" print(\"No clinical features available to link with gene expression data.\")\n",
" # Create a minimal DataFrame with gene expression data\n",
" linked_data = gene_data_final.T # Transpose to have samples as rows\n",
"\n",
"# 3. Since trait data is unavailable, we can't perform trait-specific operations\n",
"# but we can still handle missing values in the gene expression data\n",
"is_trait_available = False\n",
"print(\"Trait data is unavailable in this dataset.\")\n",
"\n",
"# 4. Since trait data is unavailable, the dataset is not usable for trait association studies\n",
"is_biased = True # Not applicable since trait is unavailable\n",
"\n",
"# 5. Validate and save cohort info\n",
"note = \"This dataset contains human anxiety disorder gene expression data, but lacks specific anxiety disorder trait measurements (e.g., severity scores) for association studies.\"\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=is_trait_available,\n",
" is_biased=is_biased,\n",
" df=linked_data,\n",
" note=note\n",
")\n",
"\n",
"# 6. Don't save linked data as it's not usable for trait association studies\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 deemed not usable for anxiety disorder association studies due to missing trait measurements.\")"
]
}
],
"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
}
|