{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "4efab7e1", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T04:07:46.490154Z", "iopub.status.busy": "2025-03-25T04:07:46.490041Z", "iopub.status.idle": "2025-03-25T04:07:46.660154Z", "shell.execute_reply": "2025-03-25T04:07:46.659799Z" } }, "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 = \"Substance_Use_Disorder\"\n", "cohort = \"GSE148375\"\n", "\n", "# Input paths\n", "in_trait_dir = \"../../input/GEO/Substance_Use_Disorder\"\n", "in_cohort_dir = \"../../input/GEO/Substance_Use_Disorder/GSE148375\"\n", "\n", "# Output paths\n", "out_data_file = \"../../output/preprocess/Substance_Use_Disorder/GSE148375.csv\"\n", "out_gene_data_file = \"../../output/preprocess/Substance_Use_Disorder/gene_data/GSE148375.csv\"\n", "out_clinical_data_file = \"../../output/preprocess/Substance_Use_Disorder/clinical_data/GSE148375.csv\"\n", "json_path = \"../../output/preprocess/Substance_Use_Disorder/cohort_info.json\"\n" ] }, { "cell_type": "markdown", "id": "c3bf29b8", "metadata": {}, "source": [ "### Step 1: Initial Data Loading" ] }, { "cell_type": "code", "execution_count": 2, "id": "9a603c09", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T04:07:46.661652Z", "iopub.status.busy": "2025-03-25T04:07:46.661487Z", "iopub.status.idle": "2025-03-25T04:07:46.731861Z", "shell.execute_reply": "2025-03-25T04:07:46.731521Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Background Information:\n", "!Series_title\t\"An Exome-Wide Association Study Identifies New Susceptibility Loci for the Risk of Nicotine Dependence in African-American Populations\"\n", "!Series_summary\t\"Cigarette smoking is one of the largest causes of preventable death worldwide. Smoking behaviors, including age at smoking initiation (ASI), smoking dependence (SD), and smoking cessation (SC), are all complex phenotypes determined by both genetic and environmental factors as well as their interactions. To identify susceptibility loci for each smoking phenotype, numerous studies have been conducted, with approaches including genome-wide linkage scans, candidate gene-based association analysis, and genome-wide association study (GWAS). Therefore, we conducted an exome-wide association study to identify new susceptibility loci for the risk of nicotine dependence in African-American populations.\"\n", "!Series_overall_design\t\"To reveal the molecular mechanism underling each smoking phenotype, we used high-throughput approaches such as exome-based association study to identify genetic variants that contribute to nicotine dependence and other smoking-related phenotypes. First, we evaluated each common variant individually with a univariate statistic; i.e., logistic and linear regression models. Second, rare variants were grouped by genomic regions and analysed using burden tests, i.e., the Weighted Sum Statistic (WSS). Third, we tested for combined effects of rare and common variants with a unified statistical test that allows both types of variants to contribute fully to the overall test statistic.\"\n", "Sample Characteristics Dictionary:\n", "{0: ['ethnicity: African-American'], 1: ['age: 39', 'age: 42', 'age: 32', 'age: 33', 'age: 48', 'age: 29', 'age: 46', 'age: 53', 'age: 24', 'age: 50', 'age: 27', 'age: 17', 'age: 16', 'age: 19', 'age: 61', 'age: 37', 'age: 38', 'age: 35', 'age: 52', 'age: 25', 'age: 47', 'age: 22', 'age: 21', 'age: 28', 'age: 55', 'age: 57', 'age: 58', 'age: 36', 'age: 41', 'age: 54'], 2: ['gender: Male', 'gender: Female'], 3: ['cpd: 20', 'cpd: 30', 'cpd: 40', 'cpd: 10', 'cpd: -9', 'cpd: 15', 'cpd: 13', 'cpd: 5', 'cpd: 35', 'cpd: 7', 'cpd: 8', 'cpd: 3', 'cpd: 12', 'cpd: 26', 'cpd: 18', 'cpd: 1', 'cpd: 25', 'cpd: 16', 'cpd: 14', 'cpd: 0', 'cpd: 60', 'cpd: 27', 'cpd: 19', 'cpd: 50', 'cpd: 21', 'cpd: 22', 'cpd: 23', 'cpd: 45', 'cpd: 24', 'cpd: 28'], 4: ['hsi: 4', 'hsi: 5', 'hsi: 6', 'hsi: 3', 'hsi: 2', 'hsi: -9', 'hsi: 0', 'hsi: 1'], 5: ['ftnd: 7', 'ftnd: 9', 'ftnd: 5', 'ftnd: 4', 'ftnd: -9', 'ftnd: 8', 'ftnd: 1', 'ftnd: 2', 'ftnd: 6', 'ftnd: 3', 'ftnd: 10', 'ftnd: 0'], 6: ['smoking_status: Smoker', 'smoking_status: Non-smoker', 'smoking_status: Ex-smoker'], 7: ['tissue: Blood']}\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": "8470d543", "metadata": {}, "source": [ "### Step 2: Dataset Analysis and Clinical Feature Extraction" ] }, { "cell_type": "code", "execution_count": 3, "id": "854d341b", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T04:07:46.733126Z", "iopub.status.busy": "2025-03-25T04:07:46.732992Z", "iopub.status.idle": "2025-03-25T04:07:46.997278Z", "shell.execute_reply": "2025-03-25T04:07:46.996912Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Preview of selected clinical features:\n", "{'GSM4463148': [1.0, 39.0, 1.0], 'GSM4463149': [1.0, 42.0, 1.0], 'GSM4463150': [1.0, 32.0, 0.0], 'GSM4463151': [1.0, 33.0, 1.0], 'GSM4463152': [1.0, 48.0, 0.0], 'GSM4463153': [1.0, 33.0, 1.0], 'GSM4463154': [1.0, 29.0, 0.0], 'GSM4463155': [0.0, 46.0, 0.0], 'GSM4463156': [1.0, 46.0, 0.0], 'GSM4463157': [1.0, 53.0, 0.0], 'GSM4463158': [1.0, 24.0, 1.0], 'GSM4463159': [1.0, 50.0, 1.0], 'GSM4463160': [1.0, 42.0, 0.0], 'GSM4463161': [1.0, 39.0, 0.0], 'GSM4463162': [1.0, 27.0, 1.0], 'GSM4463163': [0.0, 24.0, 0.0], 'GSM4463164': [1.0, 17.0, 1.0], 'GSM4463165': [0.0, 16.0, 0.0], 'GSM4463166': [1.0, 19.0, 1.0], 'GSM4463167': [1.0, 42.0, 1.0], 'GSM4463168': [1.0, 61.0, 1.0], 'GSM4463169': [1.0, 42.0, 1.0], 'GSM4463170': [1.0, 46.0, 0.0], 'GSM4463171': [1.0, 53.0, 1.0], 'GSM4463172': [1.0, 37.0, 0.0], 'GSM4463173': [1.0, 38.0, 1.0], 'GSM4463174': [1.0, 35.0, 0.0], 'GSM4463175': [1.0, 29.0, 0.0], 'GSM4463176': [1.0, 24.0, 0.0], 'GSM4463177': [1.0, 52.0, 1.0], 'GSM4463178': [1.0, 25.0, 0.0], 'GSM4463179': [1.0, 47.0, 1.0], 'GSM4463180': [1.0, 22.0, 1.0], 'GSM4463181': [1.0, 27.0, 1.0], 'GSM4463182': [1.0, 21.0, 0.0], 'GSM4463183': [1.0, 28.0, 0.0], 'GSM4463184': [1.0, 50.0, 0.0], 'GSM4463185': [1.0, 55.0, 1.0], 'GSM4463186': [1.0, 47.0, 0.0], 'GSM4463187': [1.0, 57.0, 0.0], 'GSM4463188': [1.0, 27.0, 1.0], 'GSM4463189': [1.0, 58.0, 0.0], 'GSM4463190': [0.0, 36.0, 0.0], 'GSM4463191': [1.0, 41.0, 0.0], 'GSM4463192': [1.0, 46.0, 1.0], 'GSM4463193': [1.0, 54.0, 1.0], 'GSM4463194': [1.0, 52.0, 1.0], 'GSM4463195': [1.0, 37.0, 1.0], 'GSM4463196': [1.0, 31.0, 0.0], 'GSM4463197': [1.0, 28.0, 0.0], 'GSM4463198': [1.0, 25.0, 0.0], 'GSM4463199': [1.0, 24.0, 0.0], 'GSM4463200': [1.0, 34.0, 0.0], 'GSM4463201': [1.0, 34.0, 0.0], 'GSM4463202': [1.0, 56.0, 0.0], 'GSM4463203': [1.0, 31.0, 0.0], 'GSM4463204': [0.0, 44.0, 1.0], 'GSM4463205': [1.0, 29.0, 1.0], 'GSM4463206': [1.0, 49.0, 1.0], 'GSM4463207': [1.0, 45.0, 0.0], 'GSM4463208': [0.0, 59.0, 1.0], 'GSM4463209': [1.0, 35.0, 0.0], 'GSM4463210': [1.0, 24.0, 1.0], 'GSM4463211': [1.0, 21.0, 1.0], 'GSM4463212': [1.0, 21.0, 1.0], 'GSM4463213': [1.0, 44.0, 0.0], 'GSM4463214': [1.0, 35.0, 1.0], 'GSM4463215': [1.0, 27.0, 0.0], 'GSM4463216': [1.0, 44.0, 1.0], 'GSM4463217': [1.0, 24.0, 0.0], 'GSM4463218': [0.0, 20.0, 1.0], 'GSM4463219': [1.0, 20.0, 0.0], 'GSM4463220': [1.0, 27.0, 0.0], 'GSM4463221': [0.0, 25.0, 0.0], 'GSM4463222': [0.0, 24.0, 0.0], 'GSM4463224': [1.0, 21.0, 0.0], 'GSM4463225': [1.0, 48.0, 1.0], 'GSM4463226': [1.0, 28.0, 1.0], 'GSM4463227': [1.0, 22.0, 1.0], 'GSM4463228': [1.0, 54.0, 1.0], 'GSM4463230': [1.0, 42.0, 1.0], 'GSM4463231': [1.0, 29.0, 1.0], 'GSM4463232': [1.0, 26.0, 0.0], 'GSM4463233': [1.0, 44.0, 1.0], 'GSM4463235': [0.0, 46.0, 1.0], 'GSM4463236': [1.0, 28.0, 0.0], 'GSM4463237': [1.0, 29.0, 0.0], 'GSM4463238': [1.0, 46.0, 0.0], 'GSM4463240': [0.0, 19.0, 0.0], 'GSM4463241': [1.0, 37.0, 0.0], 'GSM4463242': [1.0, 44.0, 0.0], 'GSM4463243': [1.0, 43.0, 0.0], 'GSM4463245': [1.0, 45.0, 0.0], 'GSM4463246': [0.0, 24.0, 0.0], 'GSM4463247': [1.0, 24.0, 0.0], 'GSM4463248': [1.0, 29.0, 1.0], 'GSM4463250': [1.0, 27.0, 0.0], 'GSM4463251': [1.0, 27.0, 0.0], 'GSM4463252': [1.0, 25.0, 0.0], 'GSM4463254': [1.0, 48.0, 1.0], 'GSM4463255': [1.0, 41.0, 0.0], 'GSM4463256': [1.0, 22.0, 1.0], 'GSM4463257': [1.0, 33.0, 0.0], 'GSM4463258': [1.0, 31.0, 1.0], 'GSM4463259': [1.0, 27.0, 1.0], 'GSM4463260': [1.0, 25.0, 1.0], 'GSM4463261': [1.0, 27.0, 0.0], 'GSM4463262': [1.0, 20.0, 1.0], 'GSM4463263': [1.0, 21.0, 0.0], 'GSM4463264': [1.0, 24.0, 0.0], 'GSM4463265': [0.0, 23.0, 0.0], 'GSM4463266': [1.0, 26.0, 0.0], 'GSM4463267': [1.0, 21.0, 0.0], 'GSM4463268': [0.0, 17.0, 0.0], 'GSM4463270': [1.0, 31.0, 0.0], 'GSM4463271': [0.0, 24.0, 0.0], 'GSM4463273': [1.0, 27.0, 0.0], 'GSM4463274': [1.0, 22.0, 0.0], 'GSM4463276': [0.0, 28.0, 0.0], 'GSM4463277': [1.0, 27.0, 0.0], 'GSM4463279': [1.0, 27.0, 0.0], 'GSM4463280': [0.0, 22.0, 0.0], 'GSM4463282': [1.0, 40.0, 1.0], 'GSM4463283': [1.0, 52.0, 0.0], 'GSM4463285': [1.0, 42.0, 0.0], 'GSM4463286': [1.0, 22.0, 0.0], 'GSM4463288': [1.0, 23.0, 0.0], 'GSM4463289': [0.0, 23.0, 0.0], 'GSM4463291': [1.0, 24.0, 0.0], 'GSM4463292': [1.0, 33.0, 1.0], 'GSM4463294': [1.0, 49.0, 0.0], 'GSM4463296': [0.0, 56.0, 0.0], 'GSM4463297': [0.0, 27.0, 0.0], 'GSM4463299': [1.0, 58.0, 0.0], 'GSM4463300': [1.0, 40.0, 0.0], 'GSM4463302': [1.0, 38.0, 0.0], 'GSM4463304': [1.0, 18.0, 1.0], 'GSM4463305': [0.0, 17.0, 0.0], 'GSM4463307': [0.0, 37.0, 0.0], 'GSM4463308': [1.0, 34.0, 1.0], 'GSM4463310': [0.0, 35.0, 1.0], 'GSM4463311': [1.0, 54.0, 1.0], 'GSM4463313': [1.0, 39.0, 1.0], 'GSM4463314': [0.0, 43.0, 1.0], 'GSM4463316': [1.0, 59.0, 0.0], 'GSM4463317': [1.0, 41.0, 1.0], 'GSM4463319': [0.0, 34.0, 0.0], 'GSM4463320': [1.0, 38.0, 1.0], 'GSM4463322': [1.0, 30.0, 0.0], 'GSM4463323': [1.0, 53.0, 0.0], 'GSM4463325': [1.0, 48.0, 0.0], 'GSM4463327': [1.0, 47.0, 1.0], 'GSM4463328': [1.0, 33.0, 1.0], 'GSM4463330': [1.0, 29.0, 1.0], 'GSM4463331': [1.0, 29.0, 0.0], 'GSM4463333': [1.0, 56.0, 1.0], 'GSM4463334': [1.0, 46.0, 1.0], 'GSM4463335': [1.0, 35.0, 1.0], 'GSM4463336': [1.0, 30.0, 1.0], 'GSM4463337': [1.0, 39.0, 1.0], 'GSM4463338': [1.0, 44.0, 1.0], 'GSM4463339': [1.0, 43.0, 1.0], 'GSM4463340': [1.0, 37.0, 1.0], 'GSM4463341': [1.0, 46.0, 1.0], 'GSM4463342': [1.0, 36.0, 1.0], 'GSM4463343': [1.0, 49.0, 1.0], 'GSM4463344': [1.0, 38.0, 0.0], 'GSM4463345': [1.0, 22.0, 0.0], 'GSM4463346': [1.0, 50.0, 1.0], 'GSM4463347': [1.0, 30.0, 1.0], 'GSM4463348': [1.0, 41.0, 1.0], 'GSM4463349': [1.0, 47.0, 1.0], 'GSM4463350': [1.0, 25.0, 1.0], 'GSM4463351': [1.0, 25.0, 1.0], 'GSM4463352': [1.0, 48.0, 0.0], 'GSM4463353': [1.0, 46.0, 1.0], 'GSM4463354': [1.0, 38.0, 1.0], 'GSM4463355': [1.0, 26.0, 1.0], 'GSM4463356': [0.0, 32.0, 1.0], 'GSM4463357': [0.0, 23.0, 0.0], 'GSM4463358': [0.0, 29.0, 1.0], 'GSM4463359': [0.0, 26.0, 1.0], 'GSM4463360': [0.0, 54.0, 1.0], 'GSM4463361': [0.0, 44.0, 0.0], 'GSM4463362': [1.0, 54.0, 1.0], 'GSM4463363': [1.0, 55.0, 1.0], 'GSM4463364': [1.0, 44.0, 1.0], 'GSM4463365': [1.0, 51.0, 1.0], 'GSM4463366': [1.0, 47.0, 1.0], 'GSM4463367': [1.0, 31.0, 0.0], 'GSM4463368': [1.0, 43.0, 0.0], 'GSM4463369': [1.0, 54.0, 1.0], 'GSM4463370': [1.0, 45.0, 1.0], 'GSM4463371': [1.0, 49.0, 1.0], 'GSM4463372': [1.0, 40.0, 0.0], 'GSM4463373': [1.0, 48.0, 0.0], 'GSM4463374': [1.0, 55.0, 1.0], 'GSM4463375': [1.0, 24.0, 1.0], 'GSM4463376': [1.0, 43.0, 1.0], 'GSM4463377': [1.0, 48.0, 1.0]}\n", "Clinical data saved to ../../output/preprocess/Substance_Use_Disorder/clinical_data/GSE148375.csv\n" ] } ], "source": [ "# 1. Gene Expression Data Availability\n", "# From background information, this dataset seems to be a genetic association study focusing on variants, \n", "# not gene expression. It specifically mentions \"exome-wide association study\" and analyzing genetic variants.\n", "is_gene_available = False\n", "\n", "# 2. Variable Availability and Data Type Conversion\n", "# 2.1 Data Availability\n", "# Looking at the sample characteristics dictionary:\n", "\n", "# Trait (Substance Use Disorder - specifically nicotine dependence in this case)\n", "# Key 6 contains 'smoking_status' which can be used as our trait variable\n", "trait_row = 6 \n", "\n", "# Age data is available in key 1\n", "age_row = 1\n", "\n", "# Gender data is available in key 2\n", "gender_row = 2\n", "\n", "# 2.2 Data Type Conversion Functions\n", "def convert_trait(value):\n", " \"\"\"Convert smoking status to binary trait values.\"\"\"\n", " if not isinstance(value, str):\n", " return None\n", " # Extract value part after colon\n", " if ':' in value:\n", " value = value.split(':', 1)[1].strip()\n", " \n", " if value == 'Smoker':\n", " return 1 # Has nicotine dependence\n", " elif value in ['Non-smoker', 'Ex-smoker']:\n", " return 0 # Does not have current nicotine dependence\n", " else:\n", " return None\n", " \n", "def convert_age(value):\n", " \"\"\"Convert age to numerical continuous values.\"\"\"\n", " if not isinstance(value, str):\n", " return None\n", " # Extract value part after colon\n", " if ':' in value:\n", " value = value.split(':', 1)[1].strip()\n", " \n", " try:\n", " # Skip placeholder values like -9\n", " if value == '-9':\n", " return None\n", " return float(value)\n", " except (ValueError, TypeError):\n", " return None\n", "\n", "def convert_gender(value):\n", " \"\"\"Convert gender to binary values (0 for female, 1 for male).\"\"\"\n", " if not isinstance(value, str):\n", " return None\n", " # Extract value part after colon\n", " if ':' in value:\n", " value = value.split(':', 1)[1].strip()\n", " \n", " if value.lower() == 'male':\n", " return 1\n", " elif value.lower() == 'female':\n", " return 0\n", " else:\n", " return None\n", "\n", "# 3. Save Metadata - Initial Filtering\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", "# Since trait_row is not None, clinical data is available, so we extract features\n", "if trait_row is not None:\n", " # We need clinical_data from previous steps to run this part\n", " # Assuming clinical_data is available from previous steps\n", " try:\n", " # Create output directory if it doesn't exist\n", " os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)\n", " \n", " # Extract selected 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 dataframe\n", " preview_result = preview_df(selected_clinical_df)\n", " print(\"Preview of selected clinical features:\")\n", " print(preview_result)\n", " \n", " # Save to CSV\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 NameError:\n", " print(\"Cannot extract clinical features: clinical_data is not available.\")\n", "else:\n", " print(\"Clinical data is not available for this cohort.\")" ] } ], "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 }