{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "4ec1a4b4", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:01:36.509921Z", "iopub.status.busy": "2025-03-25T06:01:36.509506Z", "iopub.status.idle": "2025-03-25T06:01:36.680472Z", "shell.execute_reply": "2025-03-25T06:01:36.680081Z" } }, "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 = \"Osteoporosis\"\n", "cohort = \"GSE56814\"\n", "\n", "# Input paths\n", "in_trait_dir = \"../../input/GEO/Osteoporosis\"\n", "in_cohort_dir = \"../../input/GEO/Osteoporosis/GSE56814\"\n", "\n", "# Output paths\n", "out_data_file = \"../../output/preprocess/Osteoporosis/GSE56814.csv\"\n", "out_gene_data_file = \"../../output/preprocess/Osteoporosis/gene_data/GSE56814.csv\"\n", "out_clinical_data_file = \"../../output/preprocess/Osteoporosis/clinical_data/GSE56814.csv\"\n", "json_path = \"../../output/preprocess/Osteoporosis/cohort_info.json\"\n" ] }, { "cell_type": "markdown", "id": "9d969c8b", "metadata": {}, "source": [ "### Step 1: Initial Data Loading" ] }, { "cell_type": "code", "execution_count": 2, "id": "ee294987", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:01:36.681738Z", "iopub.status.busy": "2025-03-25T06:01:36.681583Z", "iopub.status.idle": "2025-03-25T06:01:36.810832Z", "shell.execute_reply": "2025-03-25T06:01:36.810430Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Background Information:\n", "!Series_title\t\"Gene expression study of blood monocytes in pre- and postmenopausal females with low or high bone mineral density (HuEx-1_0-st-v2)\"\n", "!Series_summary\t\"Comparison of circulating monocytes from pre- and postmenopausal females with low or high bone mineral density (BMD). Circulating monocytes are progenitors of osteoclasts, and produce factors important to bone metabolism. Results provide insight into the role of monocytes in osteoporosis.\"\n", "!Series_summary\t\"We identify osteoporosis genes by microarray analyses of monocytes in high vs. low hip BMD (bone mineral density) subjects.\"\n", "!Series_overall_design\t\"Microarray analyses of monocytes were performed using Affymetrix 1.0 ST arrays in 73 Caucasian females (age: 47-56) with extremely high (mean ZBMD =1.38, n=42, 16 pre- and 26 postmenopausal subjects) or low hip BMD (mean ZBMD=-1.05, n=31, 15 pre- and 16 postmenopausal subjects). Differential gene expression analysis in high vs. low BMD subjects was conducted in the total cohort as well as pre- and post-menopausal subjects.\"\n", "Sample Characteristics Dictionary:\n", "{0: ['gender: Female'], 1: ['bone mineral density: high BMD', 'bone mineral density: low BMD'], 2: ['state: postmenopausal', 'state: premenopausal'], 3: ['cell type: monocytes']}\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": "9506e29d", "metadata": {}, "source": [ "### Step 2: Dataset Analysis and Clinical Feature Extraction" ] }, { "cell_type": "code", "execution_count": 3, "id": "e27ce3a1", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:01:36.811856Z", "iopub.status.busy": "2025-03-25T06:01:36.811743Z", "iopub.status.idle": "2025-03-25T06:01:36.823803Z", "shell.execute_reply": "2025-03-25T06:01:36.823412Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Preview of selected clinical features:\n", "{'GSM1369683': [nan, 0.0], 'GSM1369684': [nan, 0.0], 'GSM1369685': [nan, 0.0], 'GSM1369686': [nan, 0.0], 'GSM1369687': [nan, 0.0], 'GSM1369688': [nan, 0.0], 'GSM1369689': [nan, 0.0], 'GSM1369690': [nan, 0.0], 'GSM1369691': [nan, 0.0], 'GSM1369692': [nan, 0.0], 'GSM1369693': [nan, 0.0], 'GSM1369694': [nan, 0.0], 'GSM1369695': [nan, 0.0], 'GSM1369696': [nan, 0.0], 'GSM1369697': [nan, 0.0], 'GSM1369698': [nan, 0.0], 'GSM1369699': [nan, 0.0], 'GSM1369700': [nan, 0.0], 'GSM1369701': [nan, 0.0], 'GSM1369702': [nan, 0.0], 'GSM1369703': [nan, 0.0], 'GSM1369704': [nan, 0.0], 'GSM1369705': [nan, 0.0], 'GSM1369706': [nan, 0.0], 'GSM1369707': [nan, 0.0], 'GSM1369708': [nan, 0.0], 'GSM1369709': [nan, 0.0], 'GSM1369710': [nan, 0.0], 'GSM1369711': [nan, 0.0], 'GSM1369712': [nan, 0.0], 'GSM1369713': [nan, 0.0], 'GSM1369714': [nan, 0.0], 'GSM1369715': [nan, 0.0], 'GSM1369716': [nan, 0.0], 'GSM1369717': [nan, 0.0], 'GSM1369718': [nan, 0.0], 'GSM1369719': [nan, 0.0], 'GSM1369720': [nan, 0.0], 'GSM1369721': [nan, 0.0], 'GSM1369722': [nan, 0.0], 'GSM1369723': [nan, 0.0], 'GSM1369724': [nan, 0.0], 'GSM1369725': [nan, 0.0], 'GSM1369726': [nan, 0.0], 'GSM1369727': [nan, 0.0], 'GSM1369728': [nan, 0.0], 'GSM1369729': [nan, 0.0], 'GSM1369730': [nan, 0.0], 'GSM1369731': [nan, 0.0], 'GSM1369732': [nan, 0.0], 'GSM1369733': [nan, 0.0], 'GSM1369734': [nan, 0.0], 'GSM1369735': [nan, 0.0], 'GSM1369736': [nan, 0.0], 'GSM1369737': [nan, 0.0], 'GSM1369738': [nan, 0.0], 'GSM1369739': [nan, 0.0], 'GSM1369740': [nan, 0.0], 'GSM1369741': [nan, 0.0], 'GSM1369742': [nan, 0.0], 'GSM1369743': [nan, 0.0], 'GSM1369744': [nan, 0.0], 'GSM1369745': [nan, 0.0], 'GSM1369746': [nan, 0.0], 'GSM1369747': [nan, 0.0], 'GSM1369748': [nan, 0.0], 'GSM1369749': [nan, 0.0], 'GSM1369750': [nan, 0.0], 'GSM1369751': [nan, 0.0], 'GSM1369752': [nan, 0.0], 'GSM1369753': [nan, 0.0], 'GSM1369754': [nan, 0.0], 'GSM1369755': [nan, 0.0]}\n", "Clinical data saved to ../../output/preprocess/Osteoporosis/clinical_data/GSE56814.csv\n" ] } ], "source": [ "# 1. Gene Expression Data Availability\n", "# Based on the background information, this is a microarray gene expression study using Affymetrix arrays\n", "is_gene_available = True\n", "\n", "# 2. Variable Availability and Data Type Conversion\n", "# 2.1 Identify rows for trait, age, and gender data\n", "trait_row = 1 # bone mineral density (BMD) is related to osteoporosis\n", "age_row = None # No age information is explicitly provided in the sample characteristics\n", "gender_row = 0 # Gender information is available\n", "\n", "# 2.2 Data Type Conversion functions\n", "def convert_trait(value):\n", " \"\"\"Convert trait (BMD) value to binary format.\"\"\"\n", " if value is None:\n", " return None\n", " \n", " # Extract value after colon if present\n", " if ':' in value:\n", " value = value.split(':', 1)[1].strip()\n", " \n", " # Convert to binary (1 for high BMD, 0 for low BMD)\n", " if 'high BMD' in value.lower():\n", " return 1 # High BMD (not osteoporotic)\n", " elif 'low BMD' in value.lower():\n", " return 0 # Low BMD (osteoporotic)\n", " else:\n", " return None\n", "\n", "def convert_age(value):\n", " \"\"\"Convert age value to continuous format.\"\"\"\n", " # This function is defined but won't be used since age data is not available\n", " if value is None:\n", " return None\n", " \n", " if ':' in value:\n", " value = value.split(':', 1)[1].strip()\n", " \n", " try:\n", " # Extract numeric value and convert to float\n", " age_value = ''.join(c for c in value if c.isdigit() or c == '.')\n", " return float(age_value)\n", " except:\n", " return None\n", "\n", "def convert_gender(value):\n", " \"\"\"Convert gender value to binary format.\"\"\"\n", " if value is None:\n", " return None\n", " \n", " if ':' in value:\n", " value = value.split(':', 1)[1].strip()\n", " \n", " if 'female' in value.lower():\n", " return 0\n", " elif 'male' in value.lower():\n", " return 1\n", " else:\n", " return None\n", "\n", "# 3. Save Metadata\n", "# Check if trait data is available\n", "is_trait_available = trait_row is not None\n", "# Initial validation 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", "if trait_row is not None:\n", " # Use the clinical_data variable that should be available from previous steps\n", " # Extract 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 extracted clinical features\n", " preview = preview_df(selected_clinical_df)\n", " print(\"Preview of selected clinical features:\")\n", " print(preview)\n", " \n", " # Make sure the directory exists before saving\n", " os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)\n", " # Save the clinical data 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" ] }, { "cell_type": "markdown", "id": "2692100a", "metadata": {}, "source": [ "### Step 3: Gene Data Extraction" ] }, { "cell_type": "code", "execution_count": 4, "id": "5d0ffec9", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:01:36.824728Z", "iopub.status.busy": "2025-03-25T06:01:36.824620Z", "iopub.status.idle": "2025-03-25T06:01:37.004918Z", "shell.execute_reply": "2025-03-25T06:01:37.004289Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Index(['2315554', '2315633', '2315674', '2315739', '2315894', '2315918',\n", " '2315951', '2316218', '2316245', '2316379', '2316558', '2316605',\n", " '2316746', '2316905', '2316953', '2317246', '2317317', '2317434',\n", " '2317472', '2317512'],\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": "88af9ec4", "metadata": {}, "source": [ "### Step 4: Gene Identifier Review" ] }, { "cell_type": "code", "execution_count": 5, "id": "80bb9a20", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:01:37.006735Z", "iopub.status.busy": "2025-03-25T06:01:37.006618Z", "iopub.status.idle": "2025-03-25T06:01:37.008913Z", "shell.execute_reply": "2025-03-25T06:01:37.008482Z" } }, "outputs": [], "source": [ "# These identifiers (2315554, 2315633, etc.) appear to be probe IDs rather than standard human gene symbols.\n", "# Standard gene symbols would typically be alphanumeric like BRCA1, TP53, etc.\n", "# These numeric identifiers suggest they're likely platform-specific probe IDs that need mapping to gene symbols.\n", "\n", "requires_gene_mapping = True\n" ] }, { "cell_type": "markdown", "id": "c39913cc", "metadata": {}, "source": [ "### Step 5: Gene Annotation" ] }, { "cell_type": "code", "execution_count": 6, "id": "c214c642", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:01:37.010795Z", "iopub.status.busy": "2025-03-25T06:01:37.010486Z", "iopub.status.idle": "2025-03-25T06:01:40.664533Z", "shell.execute_reply": "2025-03-25T06:01:40.663884Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Gene annotation preview:\n", "{'ID': ['2315100', '2315106', '2315109', '2315111', '2315113'], 'GB_LIST': ['NR_024005,NR_034090,NR_024004,AK093685', 'DQ786314', nan, nan, 'DQ786265'], 'SPOT_ID': ['chr1:11884-14409', 'chr1:14760-15198', 'chr1:19408-19712', 'chr1:25142-25532', 'chr1:27563-27813'], 'seqname': ['chr1', 'chr1', 'chr1', 'chr1', 'chr1'], 'RANGE_GB': ['NC_000001.10', 'NC_000001.10', 'NC_000001.10', 'NC_000001.10', 'NC_000001.10'], 'RANGE_STRAND': ['+', '+', '+', '+', '+'], 'RANGE_START': ['11884', '14760', '19408', '25142', '27563'], 'RANGE_STOP': ['14409', '15198', '19712', '25532', '27813'], 'total_probes': ['20', '8', '4', '4', '4'], 'gene_assignment': ['NR_024005 // DDX11L2 // DEAD/H (Asp-Glu-Ala-Asp/His) box polypeptide 11 like 2 // 2q13 // 84771 /// NR_034090 // DDX11L9 // DEAD/H (Asp-Glu-Ala-Asp/His) box polypeptide 11 like 9 // 15q26.3 // 100288486 /// NR_024004 // DDX11L2 // DEAD/H (Asp-Glu-Ala-Asp/His) box polypeptide 11 like 2 // 2q13 // 84771 /// AK093685 // DDX11L2 // DEAD/H (Asp-Glu-Ala-Asp/His) box polypeptide 11 like 2 // 2q13 // 84771', '---', '---', '---', '---'], 'mrna_assignment': ['NR_024005 // RefSeq // Homo sapiens DEAD/H (Asp-Glu-Ala-Asp/His) box polypeptide 11 like 2 (DDX11L2), transcript variant 2, non-coding RNA. // chr1 // 100 // 80 // 16 // 16 // 0 /// NR_034090 // RefSeq // Homo sapiens DEAD/H (Asp-Glu-Ala-Asp/His) box polypeptide 11 like 9 (DDX11L9), non-coding RNA. // chr1 // 100 // 80 // 16 // 16 // 0 /// NR_024004 // RefSeq // Homo sapiens DEAD/H (Asp-Glu-Ala-Asp/His) box polypeptide 11 like 2 (DDX11L2), transcript variant 1, non-coding RNA. // chr1 // 100 // 75 // 15 // 15 // 0 /// AK093685 // GenBank // Homo sapiens cDNA FLJ36366 fis, clone THYMU2007824. // chr1 // 94 // 80 // 15 // 16 // 0 /// ENST00000513886 // ENSEMBL // cdna:known chromosome:GRCh37:16:61555:64090:1 gene:ENSG00000233614 // chr1 // 100 // 80 // 16 // 16 // 0 /// ENST00000456328 // ENSEMBL // cdna:known chromosome:GRCh37:1:11869:14409:1 gene:ENSG00000223972 // chr1 // 100 // 80 // 16 // 16 // 0 /// ENST00000518655 // ENSEMBL // cdna:known chromosome:GRCh37:1:11869:14409:1 gene:ENSG00000253101 // chr1 // 100 // 80 // 16 // 16 // 0', 'DQ786314 // GenBank // Homo sapiens clone HLS_IMAGE_811138 mRNA sequence. // chr1 // 100 // 38 // 3 // 3 // 0', '---', '---', 'DQ786265 // GenBank // Homo sapiens clone HLS_IMAGE_298685 mRNA sequence. // chr1 // 100 // 100 // 4 // 4 // 0'], 'category': ['main', 'main', '---', '---', 'main']}\n" ] } ], "source": [ "# 1. 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", "# 2. 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": "481e19a3", "metadata": {}, "source": [ "### Step 6: Gene Identifier Mapping" ] }, { "cell_type": "code", "execution_count": 7, "id": "001c8db6", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:01:40.666474Z", "iopub.status.busy": "2025-03-25T06:01:40.666318Z", "iopub.status.idle": "2025-03-25T06:01:41.157514Z", "shell.execute_reply": "2025-03-25T06:01:41.156932Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Mapped gene data preview (first 10 genes):\n", "Index(['A-', 'A-2', 'A-52', 'A-E', 'A-I', 'A-II', 'A-IV', 'A-V', 'A0', 'A1'], dtype='object', name='Gene')\n" ] } ], "source": [ "# 1. Identify the relevant columns for gene mapping\n", "# The gene expression data uses numeric IDs (e.g., 2315554) which match the 'ID' column in the gene annotation\n", "# The gene symbols are in the 'gene_assignment' column of the gene annotation\n", "\n", "# 2. Create gene mapping dataframe using these columns\n", "gene_mapping = get_gene_mapping(gene_annotation, 'ID', 'gene_assignment')\n", "\n", "# 3. Apply gene mapping to convert probe-level measurements to gene-level expression data\n", "gene_data = apply_gene_mapping(gene_data, gene_mapping)\n", "\n", "# Preview the mapped gene data to verify the transformation\n", "print(\"Mapped gene data preview (first 10 genes):\")\n", "print(gene_data.index[:10])\n" ] }, { "cell_type": "markdown", "id": "bb17a9b2", "metadata": {}, "source": [ "### Step 7: Data Normalization and Linking" ] }, { "cell_type": "code", "execution_count": 8, "id": "a20c3943", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T06:01:41.159184Z", "iopub.status.busy": "2025-03-25T06:01:41.158916Z", "iopub.status.idle": "2025-03-25T06:01:42.216324Z", "shell.execute_reply": "2025-03-25T06:01:42.215671Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Normalized gene data saved to ../../output/preprocess/Osteoporosis/gene_data/GSE56814.csv\n", "Clinical data shape: (4, 74)\n", "Clinical data columns (first 5): ['!Sample_geo_accession', 'GSM1369683', 'GSM1369684', 'GSM1369685', 'GSM1369686']\n", "Clinical data index: [0, 1, 2, 3]\n", "Sample trait value from clinical_data: 'bone mineral density: high BMD'\n", "Transposed clinical data shape: (74, 4)\n", "\n", "Fixed clinical features (first 5 samples):\n", " Osteoporosis Gender\n", "GSM1369683 None 0\n", "GSM1369684 None 0\n", "GSM1369685 None 0\n", "GSM1369686 None 0\n", "GSM1369687 None 0\n", "Trait values: [None]\n", "Gender values: [0]\n", "Clinical data saved to ../../output/preprocess/Osteoporosis/clinical_data/GSE56814.csv\n", "Linked data shape: (73, 18420)\n", "Quartiles for 'Osteoporosis':\n", " 25%: nan\n", " 50% (Median): nan\n", " 75%: nan\n", "Min: nan\n", "Max: nan\n", "The distribution of the feature 'Osteoporosis' in this dataset is fine.\n", "\n", "Abnormality detected in the cohort: GSE56814. Preprocessing failed.\n", "Data was determined to be unusable and was not saved\n" ] } ], "source": [ "# 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", "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", "# Debug the clinical data structure to better understand it\n", "print(\"Clinical data shape:\", clinical_data.shape)\n", "print(\"Clinical data columns (first 5):\", list(clinical_data.columns)[:5])\n", "print(\"Clinical data index:\", clinical_data.index.tolist())\n", "\n", "# Let's examine one cell value completely to understand the format\n", "sample_value = clinical_data.iloc[trait_row, 1] # Get the second column value from the trait row\n", "print(f\"Sample trait value from clinical_data: '{sample_value}'\")\n", "\n", "# This is a transposed view of the clinical data - let's fix our approach\n", "# The data is organized with rows as characteristics and columns as samples\n", "# First, transpose so samples are rows\n", "transposed_clinical_data = clinical_data.transpose()\n", "print(\"Transposed clinical data shape:\", transposed_clinical_data.shape)\n", "\n", "# Get column names from the first row (the identifiers)\n", "column_names = transposed_clinical_data.iloc[0].tolist()\n", "# Remove the first row which was just used for headers\n", "transposed_clinical_data = transposed_clinical_data.iloc[1:]\n", "# Set column names\n", "transposed_clinical_data.columns = column_names\n", "\n", "# Now extract trait and gender data properly\n", "trait_data = transposed_clinical_data.iloc[:, trait_row].apply(convert_trait)\n", "gender_data = transposed_clinical_data.iloc[:, gender_row].apply(convert_gender)\n", "\n", "# Create a proper DataFrame with the extracted features\n", "clinical_features_df = pd.DataFrame({\n", " trait: trait_data,\n", " 'Gender': gender_data\n", "})\n", "\n", "# Print out the fixed clinical features to verify\n", "print(\"\\nFixed clinical features (first 5 samples):\")\n", "print(clinical_features_df.head())\n", "print(f\"Trait values: {clinical_features_df[trait].unique()}\")\n", "print(f\"Gender values: {clinical_features_df['Gender'].unique()}\")\n", "\n", "# Save the clinical data\n", "os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)\n", "clinical_features_df.to_csv(out_clinical_data_file)\n", "print(f\"Clinical data saved to {out_clinical_data_file}\")\n", "\n", "# Now link the clinical and genetic data\n", "linked_data = pd.merge(clinical_features_df, \n", " normalized_gene_data.transpose(), \n", " left_index=True, \n", " right_index=True)\n", "print(\"Linked data shape:\", linked_data.shape)\n", "\n", "# Handle missing values in the linked data\n", "linked_data = handle_missing_values(linked_data, trait)\n", "\n", "# 4. Determine whether the trait and some demographic features are severely biased\n", "is_trait_biased, unbiased_linked_data = judge_and_remove_biased_features(linked_data, trait)\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=\"This dataset compares monocytes from females with high versus low bone mineral density to identify genes relevant to osteoporosis.\"\n", ")\n", "\n", "# 6. If the linked data is usable, save it as a CSV 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(\"Data was determined to be unusable and was 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 }