{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "f83dca11", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:12:06.777331Z", "iopub.status.busy": "2025-03-25T07:12:06.776791Z", "iopub.status.idle": "2025-03-25T07:12:06.944136Z", "shell.execute_reply": "2025-03-25T07:12:06.943773Z" } }, "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 = \"Irritable_bowel_syndrome_(IBS)\"\n", "cohort = \"GSE20881\"\n", "\n", "# Input paths\n", "in_trait_dir = \"../../input/GEO/Irritable_bowel_syndrome_(IBS)\"\n", "in_cohort_dir = \"../../input/GEO/Irritable_bowel_syndrome_(IBS)/GSE20881\"\n", "\n", "# Output paths\n", "out_data_file = \"../../output/preprocess/Irritable_bowel_syndrome_(IBS)/GSE20881.csv\"\n", "out_gene_data_file = \"../../output/preprocess/Irritable_bowel_syndrome_(IBS)/gene_data/GSE20881.csv\"\n", "out_clinical_data_file = \"../../output/preprocess/Irritable_bowel_syndrome_(IBS)/clinical_data/GSE20881.csv\"\n", "json_path = \"../../output/preprocess/Irritable_bowel_syndrome_(IBS)/cohort_info.json\"\n" ] }, { "cell_type": "markdown", "id": "a9474626", "metadata": {}, "source": [ "### Step 1: Initial Data Loading" ] }, { "cell_type": "code", "execution_count": 2, "id": "d4e7f3b9", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:12:06.945575Z", "iopub.status.busy": "2025-03-25T07:12:06.945427Z", "iopub.status.idle": "2025-03-25T07:12:07.213757Z", "shell.execute_reply": "2025-03-25T07:12:07.213404Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Background Information:\n", "!Series_title\t\"Colon biopsies from Crohns patients and healthy controls\"\n", "!Series_summary\t\"The aim of this study was to investigate differential intestinal gene expression in patients with Crohn's disease (CD) and controls. 172 biopsies from CD and control subjects were studied. Endoscopic biopsies were taken at ileocolonoscopy from five specific anatomical locations including the terminal ileum for RNA extraction.\"\n", "!Series_overall_design\t\"Biopsies from one of four anatomic locations, from healthy controls and treated (with non-biologic standard of care) or untreated CD patients.\"\n", "Sample Characteristics Dictionary:\n", "{0: ['patient: 101', 'patient: 102', 'patient: 103', 'patient: 105', 'patient: 106', 'patient: 108', 'patient: 104', 'patient: 107', 'patient: 109', 'patient: 110', 'patient: 111', 'patient: 112', 'patient: 113', 'patient: 114', 'patient: 115', 'patient: 116', 'patient: 117', 'patient: 119', 'patient: 120', 'patient: 121', 'patient: 122', 'patient: 124', 'patient: 125', 'patient: 126', 'patient: 127', 'patient: 128', 'patient: 134', 'patient: 135', 'patient: 136', 'patient: 133'], 1: ['current medication: Paracetamol', 'current medication: Movicol 1 sachet daily', 'current medication: None', 'current medication: Asacol 400mg BD', 'current medication: OCP', 'current medication: Frusemide, Codine Phosphate', 'current medication: Clomiprimine', 'current medication: Loperamide', 'current medication: Salubtamol, Bricanyl', 'current medication: Minicycline', 'current medication: Cp-tenedone, Enalapril, Loperamide.', 'current medication: Venlafaxine', 'current medication: Prednisolone 10mg Heparin Questran Gabapentin FA', 'current medication: Pentaza', 'current medication: none', 'current medication: metranidazole 400mg TDS Prednisolone', 'current medication: Omeprazole 20mg', 'current medication: Pentaza 1g TDS', 'current medication: Asacol 400mg TDS, Adcal D3, HRT.', 'current medication: Pentaza 1g BD, Folic acid 5mg, Codine P, Atenolol.', 'current medication: Adcal D3', 'current medication: Asacol 800mg TDS, Cocodamol', 'current medication: Azathioprine 150mg. Salazopyrine', 'current medication: Aza 150mg Prednisolone 20mg Adcal D3', 'current medication: Asprin Mesalazine', 'current medication: Pentaza 4g daily', 'current medication: AZA 100mg Budesonide 3mg Omeprazole 20mg Asacol 6d', 'current medication: Pentaza Omeprazole Calcium Vit D', 'current medication: Sulphasalazine Natalizumab losartan omeprazole Fe', 'current medication: Pentaza, Adcal D3'], 2: ['birth date: 10/5/63', 'birth date: 9/5/62', 'birth date: 6/30/65', 'birth date: 7/29/84', 'birth date: 5/29/87', 'birth date: 8/30/33', 'birth date: 1/2/72', 'birth date: 07/28/1060', 'birth date: 10/10/57', 'birth date: 3/13/54', 'birth date: 9/5/82', 'birth date: 7/5/81', 'birth date: 8/17/81', 'birth date: 6/27/36', 'birth date: 9/14/28', 'birth date: 1/19/56', 'birth date: 3/31/67', 'birth date: 9/30/70', 'birth date: 5/6/54', 'birth date: 4/8/59', 'birth date: 7/10/61', 'birth date: 6/25/77', 'birth date: 3/17/78', 'birth date: 4/4/87', 'birth date: 9/9/36', 'birth date: 3/19/81', 'birth date: 12/21/50', 'birth date: 8/14/59', 'birth date: 5/21/51', 'birth date: 4/23/65'], 3: ['ethnicity: CAUCASIAN', 'ethnicity: JEWISH', 'ethnicity: unknown'], 4: ['symptoms onset date: ', 'symptoms onset date: 11/1/94', 'symptoms onset date: 1/1/01', 'symptoms onset date: 5/27/97', 'symptoms onset date: 5/10/90', 'symptoms onset date: 3/1/80', 'symptoms onset date: 7/1/94', 'symptoms onset date: 1/20/05', 'symptoms onset date: 1/1/94', 'symptoms onset date: 8/24/93', 'symptoms onset date: 1/1/99', 'symptoms onset date: 11/1/01', 'symptoms onset date: 1/6/00', 'symptoms onset date: 1/1/93', 'symptoms onset date: 6/1/03', 'symptoms onset date: 6/1/83', 'symptoms onset date: 9/1/04', 'symptoms onset date: 3/16/89', 'symptoms onset date: 10/2/73', 'symptoms onset date: 1/1/90', 'symptoms onset date: 1/1/87', 'symptoms onset date: 1/1/92', 'symptoms onset date: 4/28/00', 'symptoms onset date: 5/1/05', 'symptoms onset date: 5/4/92', 'symptoms onset date: 1/1/81', 'symptoms onset date: 1/1/03', 'symptoms onset date: 10/18/04', 'symptoms onset date: 4/5/04', 'symptoms onset date: 5/1/71'], 5: ['diagnosis date: ', 'diagnosis date: 11/1/94', 'diagnosis date: 1/1/01', 'diagnosis date: 8/20/97', 'diagnosis date: 6/19/97', 'diagnosis date: 3/1/80', 'diagnosis date: 6/2/95', 'diagnosis date: 7/20/05', 'diagnosis date: 1/1/94', 'diagnosis date: 9/1/93', 'diagnosis date: 1/1/99', 'diagnosis date: 12/22/01', 'diagnosis date: 8/24/01', 'diagnosis date: 1/1/93', 'diagnosis date: 12/1/03', 'diagnosis date: 6/1/83', 'diagnosis date: 10/6/04', 'diagnosis date: 3/16/89', 'diagnosis date: 10/6/73', 'diagnosis date: 6/10/99', 'diagnosis date: 1/1/88', 'diagnosis date: 4/3/92', 'diagnosis date: 8/24/00', 'diagnosis date: 8/20/05', 'diagnosis date: 7/2/92', 'diagnosis date: 1/1/81', 'diagnosis date: 1/1/03', 'diagnosis date: 12/30/04', 'diagnosis date: 10/28/04', 'diagnosis date: 1/18/79'], 6: ['ocp: ', 'ocp: unknown', 'ocp: NEVER', 'ocp: EX', 'ocp: CURRENT'], 7: ['joint problems: FALSE', 'joint problems: TRUE'], 8: ['family history: FALSE', 'family history: TRUE'], 9: ['aza treated: ', 'aza treated: NO', 'aza treated: YES', 'aza treated: unknown'], 10: ['aza tolerant: FALSE', 'aza tolerant: TRUE'], 11: ['procedure date: 11/2/04', 'procedure date: 11/9/04', 'procedure date: 11/15/04', 'procedure date: 11/18/04', 'procedure date: 11/25/04', 'procedure date: 12/2/04', 'procedure date: 12/3/04', 'procedure date: 12/7/04', 'procedure date: 12/14/04', 'procedure date: 12/21/04', 'procedure date: 1/27/05', 'procedure date: 1/18/05', 'procedure date: 2/1/05', 'procedure date: 2/10/05', 'procedure date: 3/1/05', 'procedure date: 3/29/05', 'procedure date: 4/1/05', 'procedure date: 7/12/05', 'procedure date: 7/28/05', 'procedure date: 7/20/05', 'procedure date: 5/13/05', 'procedure date: 5/5/05', 'procedure date: 4/6/05', 'procedure date: 10/27/04', 'procedure date: 10/28/04', 'procedure date: 10/8/04', 'procedure date: 1/6/05', 'procedure date: 4/29/05', 'procedure date: 7/15/05', 'procedure date: 8/10/05'], 12: ['indictation for procedure: Weight loss, altered bowel habbit.', 'indictation for procedure: RIF pain, persistent anaemia.', 'indictation for procedure: Diarrhoea, now settled, Mum has UC', 'indictation for procedure: IBS type symptoms', 'indictation for procedure: Abdominal pain, upper GI symptoms.', 'indictation for procedure: Altered bowel habbit, ? UC 1974, never on Bx.', 'indictation for procedure: FH Colon Cancer', 'indictation for procedure: FH of Colon Cancer', 'indictation for procedure: Diarrhoea', 'indictation for procedure: FH of CD, Path Increased lymphiod in TI Bx.', 'indictation for procedure: Altered bowel habit', 'indictation for procedure: Recurrant abcess, ?IBD', 'indictation for procedure: Altered bowel habit, Alcohol excess', 'indictation for procedure: Diarrhoea, Also has Prostate Ca', 'indictation for procedure: FH Colon cancer', 'indictation for procedure: FH Colon Ca', 'indictation for procedure: Diarrhoea, Blastocytitis on stool cultures.', 'indictation for procedure: IBS Type symptoms', 'indictation for procedure: Diarrhoea predominant IBS', \"indictation for procedure: FH of Crohn's disease, Symtom free at procedure\", 'indictation for procedure: Pseudomembranous Colitis', 'indictation for procedure: Abdominal pain', 'indictation for procedure: bloody diahorrea', 'indictation for procedure: Diarrhoea PR bleeding', 'indictation for procedure: '], 13: ['cdai: ', 'cdai: 0', 'cdai: 6', 'cdai: 506'], 14: ['harvey bradshaw: ', 'harvey bradshaw: 4', 'harvey bradshaw: 2', 'harvey bradshaw: 5', 'harvey bradshaw: 6', 'harvey bradshaw: 0', 'harvey bradshaw: 8', 'harvey bradshaw: 3', 'harvey bradshaw: 7', 'harvey bradshaw: 9', 'harvey bradshaw: 1', 'harvey bradshaw: 17', 'harvey bradshaw: 12', 'harvey bradshaw: 10', 'harvey bradshaw: 11'], 15: ['esr: ', 'esr: 0', 'esr: 52', 'esr: 33'], 16: ['crp: ', 'crp: 0', 'crp: 217', 'crp: 18', 'crp: 4'], 17: ['hb: ', 'hb: 0', 'hb: 111', 'hb: 116', 'hb: 149'], 18: ['wcc: ', 'wcc: 0', 'wcc: 12', 'wcc: 7', 'wcc: 8'], 19: ['neutrophils: ', 'neutrophils: 0', 'neutrophils: 10', 'neutrophils: 5'], 20: ['albumin: ', 'albumin: 0', 'albumin: 31', 'albumin: 40', 'albumin: 38'], 21: ['blood obtained: FALSE', 'blood obtained: TRUE'], 22: ['ibd affected relatives: ', 'ibd affected relatives: 0', 'ibd affected relatives: 1', 'ibd affected relatives: 2'], 23: ['igr2096: ', 'igr2096: 12', 'igr2096: 22', 'igr2096: 0'], 24: ['igr2198: ', 'igr2198: 12', 'igr2198: 22', 'igr2198: 0'], 25: ['igr2230: ', 'igr2230: 12', 'igr2230: 22', 'igr2230: 0'], 26: ['octn1: ', 'octn1: 12', 'octn1: 22', 'octn1: 0'], 27: ['octn2: ', 'octn2: 12', 'octn2: 22', 'octn2: 0'], 28: ['nod 702: ', 'nod 702: 11', 'nod 702: 12'], 29: ['nod 908: ', 'nod 908: 0', 'nod 908: 11'], 30: ['dlg5 133a: ', 'dlg5 133a: 11', 'dlg5 133a: 0'], 31: ['nod 1007 fs: ', 'nod 1007 fs: 11', 'nod 1007 fs: 12', 'nod 1007 fs: 0'], 32: ['further operations: ', 'further operations: 16/05/1995 R hemicolectomy and iliectomy', 'further operations: 01/06/1985 colectomy and ileostomy -01/07/1986 Excision of rectum', 'further operations: 07/03/2003 Laying open of fistula in ano'], 33: ['stoma ileal: FALSE', 'stoma ileal: TRUE'], 34: ['diagnosis oral: FALSE', 'diagnosis oral: TRUE'], 35: ['diagnosis ileal: FALSE', 'diagnosis ileal: TRUE'], 36: ['diagnosis colonic: FALSE', 'diagnosis colonic: TRUE'], 37: ['diagnosis rectal: FALSE', 'diagnosis rectal: TRUE'], 38: ['diagnosis anal perianal: FALSE', 'diagnosis anal perianal: TRUE'], 39: ['diagnosis vienna classifi: ', 'diagnosis vienna classifi: 2', 'diagnosis vienna classifi: 3', 'diagnosis vienna classifi: 1', 'diagnosis vienna classifi: 0'], 40: ['last followup date: ', 'last followup date: 5/5/05', 'last followup date: 3/29/05', 'last followup date: 4/6/05', 'last followup date: 10/27/04', 'last followup date: 10/29/04', 'last followup date: 10/8/04', 'last followup date: 7/20/05', 'last followup date: 1/6/05', 'last followup date: 5/10/05', 'last followup date: 7/15/05', 'last followup date: 9/5/05', 'last followup date: 9/2/05', 'last followup date: 11/5/05', 'last followup date: 1/17/05', 'last followup date: 9/9/05', 'last followup date: 10/5/04', 'last followup date: 10/12/04', 'last followup date: 9/12/05', 'last followup date: 5/13/05', 'last followup date: 4/5/05', 'last followup date: 5/23/05', 'last followup date: 8/20/05', 'last followup date: 1/27/05', 'last followup date: 4/12/05', 'last followup date: 4/27/05', 'last followup date: 12/21/04', 'last followup date: 9/30/04', 'last followup date: 10/14/04', 'last followup date: 12/6/04'], 41: ['followup oral: FALSE', 'followup oral: TRUE'], 42: ['followup ileal: FALSE', 'followup ileal: TRUE'], 43: ['followup colonic: FALSE', 'followup colonic: TRUE'], 44: ['followup rectal: FALSE', 'followup rectal: TRUE'], 45: ['followup anal perianal: FALSE', 'followup anal perianal: TRUE'], 46: ['followup vienna classific: ', 'followup vienna classific: 2', 'followup vienna classific: 3', 'followup vienna classific: 1', 'followup vienna classific: 0'], 47: ['casenote review date: ', 'casenote review date: 7/7/05', 'casenote review date: 6/1/05', 'casenote review date: 6/2/05', 'casenote review date: 1/27/05', 'casenote review date: 9/30/05', 'casenote review date: 5/30/05', 'casenote review date: 8/17/05', 'casenote review date: 9/28/05', 'casenote review date: 9/14/05', 'casenote review date: 1/25/05', 'casenote review date: 8/16/05', 'casenote review date: 9/21/05', 'casenote review date: 3/4/05', 'casenote review date: 8/15/05'], 48: ['behavior at diagnosis: ', 'behavior at diagnosis: 1', 'behavior at diagnosis: 3', 'behavior at diagnosis: 2'], 49: ['diagnosis stricture fistu: ', 'diagnosis stricture fistu: 0', 'diagnosis stricture fistu: 2', 'diagnosis stricture fistu: 1'], 50: ['diagnosis details: ', 'diagnosis details: Peri-anal abcess at diagnosis', 'diagnosis details: Actively draining fistula at diagnosis'], 51: ['behavior at followup: ', 'behavior at followup: 1', 'behavior at followup: 3', 'behavior at followup: 2'], 52: ['disease group: ', 'disease group: REMISSION', 'disease group: FAILURE OF TREATMENT', 'disease group: NEW'], 53: ['smoking status: ', 'smoking status: never', 'smoking status: current', 'smoking status: ex'], 54: ['smoking start date: ', 'smoking start date: 1/1/70', 'smoking start date: 1/1/53', 'smoking start date: 1/1/91', 'smoking start date: 1/1/80'], 55: ['smoking stop date: ', 'smoking stop date: 1/1/80', 'smoking stop date: 1/1/99', 'smoking stop date: 1/1/90', 'smoking stop date: 1/1/98', 'smoking stop date: 1/1/03', 'smoking stop date: 1/8/03', 'smoking stop date: 1/1/04', 'smoking stop date: 7/1/04', 'smoking stop date: 1/1/01', 'smoking stop date: 1/1/00', 'smoking stop date: 1/10/02', 'smoking stop date: 1/1/75'], 56: ['smoking amount: ', 'smoking amount: unknown', 'smoking amount: 5-14', 'smoking amount: 25+', 'smoking amount: 15-24', 'smoking amount: 0-4'], 57: ['other illnesses: ', 'other illnesses: none', 'other illnesses: None', 'other illnesses: Hypercholesterolemia', 'other illnesses: Reiters syndrome 14/05/1997', 'other illnesses: Mild asthma', 'other illnesses: Osteoporosis', 'other illnesses: COPD-Angina', 'other illnesses: Asthma', 'other illnesses: Hypertension -Peripheral vascular disease', 'other illnesses: Primary infertility', 'other illnesses: Chronic active hepatitis', 'other illnesses: MI 1986-Hypertension', 'other illnesses: Previous bleeding DU', 'other illnesses: Depression, Irritable bowel syndrome', 'other illnesses: Epilepsy, L parietal bleed 06/2004, Hypothyrodism', 'other illnesses: Hayfever', 'other illnesses: Myobacterium identified in biopsies from resection 2001. Given 6 months of anti TB therapy.', 'other illnesses: TB 1979', 'other illnesses: Hypertension-Hypothyrodism-RA', 'other illnesses: Ankylosing spondilitis', 'other illnesses: Peripheral neuropathy -IHD'], 58: ['disease: healthy', 'disease: crohns disease'], 59: ['treatment: control', 'anatomic location: sigmoid colon', 'anatomic location: terminal ileum', 'anatomic location: ascending colon', 'anatomic location: descending colon'], 60: ['anatomic location: sigmoid colon', 'anatomic location: terminal ileum', 'anatomic location: ascending colon', 'anatomic location: descending colon', 'treatment: SoC treated', 'treatment: SoC untreated'], 61: ['treatment response: NA', 'treatment response: responder', 'treatment response: nonresponder'], 62: [nan, 'inflammation status: uninflamed', 'inflammation status: inflamed']}\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": "911cdea7", "metadata": {}, "source": [ "### Step 2: Dataset Analysis and Clinical Feature Extraction" ] }, { "cell_type": "code", "execution_count": 3, "id": "1af4e722", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:12:07.214981Z", "iopub.status.busy": "2025-03-25T07:12:07.214859Z", "iopub.status.idle": "2025-03-25T07:12:07.231438Z", "shell.execute_reply": "2025-03-25T07:12:07.231140Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Preview of extracted clinical features:\n", "{'GSM522094': [0.0, 42.0], 'GSM522095': [0.0, 43.0], 'GSM522096': [0.0, 40.0], 'GSM522097': [0.0, 21.0], 'GSM522098': [0.0, 21.0], 'GSM522099': [0.0, 18.0], 'GSM522100': [0.0, 72.0], 'GSM522101': [0.0, 33.0], 'GSM522102': [0.0, 945.0], 'GSM522103': [0.0, 48.0], 'GSM522104': [0.0, 51.0], 'GSM522105': [0.0, 23.0], 'GSM522106': [0.0, 24.0], 'GSM522107': [0.0, 24.0], 'GSM522108': [0.0, 69.0], 'GSM522109': [0.0, 77.0], 'GSM522110': [0.0, 49.0], 'GSM522111': [0.0, 38.0], 'GSM522112': [0.0, 42.0], 'GSM522113': [0.0, 33.0], 'GSM522114': [0.0, 51.0], 'GSM522115': [0.0, 42.0], 'GSM522116': [0.0, 40.0], 'GSM522117': [0.0, 33.0], 'GSM522118': [0.0, 21.0], 'GSM522119': [0.0, 18.0], 'GSM522120': [0.0, 945.0], 'GSM522121': [0.0, 43.0], 'GSM522122': [0.0, 48.0], 'GSM522123': [0.0, 51.0], 'GSM522124': [0.0, 23.0], 'GSM522125': [0.0, 24.0], 'GSM522126': [0.0, 69.0], 'GSM522127': [0.0, 77.0], 'GSM522128': [0.0, 49.0], 'GSM522129': [0.0, 38.0], 'GSM522130': [0.0, 40.0], 'GSM522131': [0.0, 33.0], 'GSM522132': [0.0, 21.0], 'GSM522133': [0.0, 945.0], 'GSM522134': [0.0, 48.0], 'GSM522135': [0.0, 51.0], 'GSM522136': [0.0, 23.0], 'GSM522137': [0.0, 24.0], 'GSM522138': [0.0, 69.0], 'GSM522139': [0.0, 77.0], 'GSM522140': [0.0, 38.0], 'GSM522141': [0.0, 35.0], 'GSM522142': [0.0, 51.0], 'GSM522143': [0.0, 51.0], 'GSM522144': [0.0, 46.0], 'GSM522145': [0.0, 46.0], 'GSM522146': [0.0, 46.0], 'GSM522147': [0.0, 44.0], 'GSM522148': [0.0, 44.0], 'GSM522149': [0.0, 44.0], 'GSM522150': [0.0, 28.0], 'GSM522151': [0.0, 28.0], 'GSM522152': [0.0, 28.0], 'GSM522153': [0.0, 27.0], 'GSM522154': [0.0, 27.0], 'GSM522155': [0.0, 18.0], 'GSM522156': [0.0, 69.0], 'GSM522157': [0.0, 24.0], 'GSM522158': [0.0, 24.0], 'GSM522159': [0.0, 18.0], 'GSM522160': [0.0, 24.0], 'GSM522161': [0.0, 55.0], 'GSM522162': [0.0, 46.0], 'GSM522163': [0.0, 46.0], 'GSM522164': [0.0, 54.0], 'GSM522165': [0.0, 54.0], 'GSM522166': [0.0, 40.0], 'GSM522167': [1.0, 35.0], 'GSM522168': [1.0, 25.0], 'GSM522169': [1.0, 36.0], 'GSM522170': [1.0, 48.0], 'GSM522171': [1.0, 56.0], 'GSM522172': [1.0, 36.0], 'GSM522173': [1.0, 48.0], 'GSM522174': [1.0, 40.0], 'GSM522175': [1.0, 28.0], 'GSM522176': [1.0, 43.0], 'GSM522177': [1.0, 27.0], 'GSM522178': [1.0, 82.0], 'GSM522179': [1.0, 43.0], 'GSM522180': [1.0, 39.0], 'GSM522181': [1.0, 34.0], 'GSM522182': [1.0, 35.0], 'GSM522183': [1.0, 29.0], 'GSM522184': [1.0, 46.0], 'GSM522185': [1.0, 47.0], 'GSM522186': [1.0, 63.0], 'GSM522187': [1.0, 76.0], 'GSM522188': [1.0, 43.0], 'GSM522189': [1.0, 23.0], 'GSM522190': [1.0, 16.0], 'GSM522191': [1.0, 28.0], 'GSM522192': [1.0, 51.0], 'GSM522193': [1.0, 26.0], 'GSM522194': [1.0, 21.0], 'GSM522195': [1.0, 41.0], 'GSM522196': [1.0, 54.0], 'GSM522197': [1.0, 27.0], 'GSM522198': [1.0, 47.0], 'GSM522199': [1.0, 29.0], 'GSM522200': [1.0, 36.0], 'GSM522201': [1.0, 43.0], 'GSM522202': [1.0, 63.0], 'GSM522203': [1.0, 40.0], 'GSM522204': [1.0, 27.0], 'GSM522205': [1.0, 56.0], 'GSM522206': [1.0, 36.0], 'GSM522207': [1.0, 41.0], 'GSM522208': [1.0, 21.0], 'GSM522209': [1.0, 46.0], 'GSM522210': [1.0, 38.0], 'GSM522211': [1.0, 54.0], 'GSM522212': [1.0, 46.0], 'GSM522213': [1.0, 51.0], 'GSM522214': [1.0, 52.0], 'GSM522215': [1.0, 54.0], 'GSM522216': [1.0, 40.0], 'GSM522217': [1.0, 43.0], 'GSM522218': [1.0, 27.0], 'GSM522219': [1.0, 41.0], 'GSM522220': [1.0, 29.0], 'GSM522221': [1.0, 47.0], 'GSM522222': [1.0, 46.0], 'GSM522223': [1.0, 21.0], 'GSM522224': [1.0, 40.0], 'GSM522225': [1.0, 28.0], 'GSM522226': [1.0, 27.0], 'GSM522227': [1.0, 43.0], 'GSM522228': [1.0, 23.0], 'GSM522229': [1.0, 48.0], 'GSM522230': [1.0, 74.0], 'GSM522231': [1.0, 47.0], 'GSM522232': [1.0, 41.0], 'GSM522233': [1.0, 36.0], 'GSM522234': [1.0, 43.0], 'GSM522235': [1.0, 63.0], 'GSM522236': [1.0, 39.0], 'GSM522237': [1.0, 26.0], 'GSM522238': [1.0, 27.0], 'GSM522239': [1.0, 40.0], 'GSM522240': [1.0, 44.0], 'GSM522241': [1.0, 33.0], 'GSM522242': [1.0, 47.0], 'GSM522243': [1.0, 35.0], 'GSM522244': [1.0, 21.0], 'GSM522245': [1.0, 48.0], 'GSM522246': [1.0, 77.0], 'GSM522247': [1.0, 43.0], 'GSM522248': [1.0, 34.0], 'GSM522249': [1.0, 43.0], 'GSM522250': [1.0, 29.0], 'GSM522251': [1.0, 41.0], 'GSM522252': [1.0, 51.0], 'GSM522253': [1.0, 41.0], 'GSM522254': [1.0, 48.0], 'GSM522255': [1.0, 34.0], 'GSM522256': [1.0, 29.0], 'GSM522257': [1.0, 47.0], 'GSM522258': [1.0, 28.0], 'GSM522259': [1.0, 37.0], 'GSM522260': [1.0, 47.0], 'GSM522261': [1.0, 46.0], 'GSM522262': [1.0, 38.0], 'GSM522263': [1.0, 16.0], 'GSM522264': [1.0, 56.0], 'GSM522265': [1.0, 39.0]}\n", "Clinical features saved to ../../output/preprocess/Irritable_bowel_syndrome_(IBS)/clinical_data/GSE20881.csv\n" ] } ], "source": [ "# 1. Gene Expression Data Availability\n", "is_gene_available = True # Looking at background title and summary, it's a gene expression study comparing Crohn's patients and healthy controls\n", "\n", "# 2.1 Data Availability \n", "# For trait: Looking at row 58, contains disease status (healthy or Crohn's)\n", "trait_row = 58\n", "# For age: Can be derived from birth date in row 2\n", "age_row = 2\n", "# For gender: Gender information is not available in the sample characteristics\n", "gender_row = None\n", "\n", "# 2.2 Data Type Conversion\n", "def convert_trait(value):\n", " \"\"\"Convert trait information to binary (0 for healthy, 1 for Crohn's disease)\"\"\"\n", " if isinstance(value, str) and \":\" in value:\n", " value = value.split(\":\", 1)[1].strip().lower()\n", " if \"healthy\" in value:\n", " return 0\n", " elif \"crohns disease\" in value:\n", " return 1\n", " return None\n", "\n", "def convert_age(value):\n", " \"\"\"Convert birth date to age (as of the study date which appears to be 2004-2005)\"\"\"\n", " if isinstance(value, str) and \":\" in value:\n", " birth_date_str = value.split(\":\", 1)[1].strip()\n", " try:\n", " # Handle various date formats\n", " if \"/\" in birth_date_str:\n", " parts = birth_date_str.split(\"/\")\n", " if len(parts) == 3:\n", " # Format MM/DD/YY or M/D/YY\n", " month, day, year = parts\n", " if len(year) == 2:\n", " year = \"19\" + year # Assuming 19xx\n", " elif len(year) == 4:\n", " pass # Year is already in YYYY format\n", " \n", " # Use 2005 as reference year for age calculation (from procedure dates)\n", " reference_year = 2005\n", " age = reference_year - int(year)\n", " return age\n", " except:\n", " pass\n", " return None\n", "\n", "def convert_gender(value):\n", " \"\"\"This function is included but not used as gender data is not available\"\"\"\n", " return None\n", "\n", "# 3. Save Metadata\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", "if trait_row is not None:\n", " # File containing the clinical data must be previously loaded as clinical_data\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 extracted clinical features\n", " preview = preview_df(clinical_features)\n", " print(\"Preview of extracted clinical features:\")\n", " print(preview)\n", " \n", " # Save the clinical features as CSV\n", " os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)\n", " clinical_features.to_csv(out_clinical_data_file, index=False)\n", " print(f\"Clinical features saved to {out_clinical_data_file}\")\n" ] }, { "cell_type": "markdown", "id": "7390dce9", "metadata": {}, "source": [ "### Step 3: Gene Data Extraction" ] }, { "cell_type": "code", "execution_count": 4, "id": "e6300c02", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:12:07.232541Z", "iopub.status.busy": "2025-03-25T07:12:07.232431Z", "iopub.status.idle": "2025-03-25T07:12:07.758561Z", "shell.execute_reply": "2025-03-25T07:12:07.758185Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Matrix table marker not found in first 100 lines\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Index(['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13',\n", " '14', '15', '16', '17', '18', '19', '20'],\n", " dtype='object', name='ID')\n" ] } ], "source": [ "# 1. Get the file paths for the SOFT file and matrix file\n", "soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)\n", "\n", "# 2. First, let's examine the structure of the matrix file to understand its format\n", "import gzip\n", "\n", "# Peek at the first few lines of the file to understand its structure\n", "with gzip.open(matrix_file, 'rt') as file:\n", " # Read first 100 lines to find the header structure\n", " for i, line in enumerate(file):\n", " if '!series_matrix_table_begin' in line:\n", " print(f\"Found data marker at line {i}\")\n", " # Read the next line which should be the header\n", " header_line = next(file)\n", " print(f\"Header line: {header_line.strip()}\")\n", " # And the first data line\n", " first_data_line = next(file)\n", " print(f\"First data line: {first_data_line.strip()}\")\n", " break\n", " if i > 100: # Limit search to first 100 lines\n", " print(\"Matrix table marker not found in first 100 lines\")\n", " break\n", "\n", "# 3. Now try to get the genetic data with better error handling\n", "try:\n", " gene_data = get_genetic_data(matrix_file)\n", " print(gene_data.index[:20])\n", "except KeyError as e:\n", " print(f\"KeyError: {e}\")\n", " \n", " # Alternative approach: manually extract the data\n", " print(\"\\nTrying alternative approach to read the gene data:\")\n", " with gzip.open(matrix_file, 'rt') as file:\n", " # Find the start of the data\n", " for line in file:\n", " if '!series_matrix_table_begin' in line:\n", " break\n", " \n", " # Read the headers and data\n", " import pandas as pd\n", " df = pd.read_csv(file, sep='\\t', index_col=0)\n", " print(f\"Column names: {df.columns[:5]}\")\n", " print(f\"First 20 row IDs: {df.index[:20]}\")\n", " gene_data = df\n" ] }, { "cell_type": "markdown", "id": "8ffa9e2c", "metadata": {}, "source": [ "### Step 4: Gene Identifier Review" ] }, { "cell_type": "code", "execution_count": 5, "id": "fbbb8540", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:12:07.759920Z", "iopub.status.busy": "2025-03-25T07:12:07.759797Z", "iopub.status.idle": "2025-03-25T07:12:07.765316Z", "shell.execute_reply": "2025-03-25T07:12:07.765019Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Files in directory: ['GSE20881_family.soft.gz', 'GSE20881_series_matrix.txt.gz']\n", "Using file: ../../input/GEO/Irritable_bowel_syndrome_(IBS)/GSE20881/GSE20881_series_matrix.txt.gz\n", "Sample gene identifiers: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']\n", "requires_gene_mapping = True\n" ] } ], "source": [ "import gzip\n", "import pandas as pd\n", "import os\n", "\n", "# List files in the cohort directory to find the gene expression data file\n", "files_in_dir = os.listdir(in_cohort_dir)\n", "print(f\"Files in directory: {files_in_dir}\")\n", "\n", "# Try to identify the gene expression file\n", "gene_expr_file = None\n", "for file in files_in_dir:\n", " if 'series_matrix' in file.lower():\n", " gene_expr_file = os.path.join(in_cohort_dir, file)\n", " break\n", "\n", "if gene_expr_file:\n", " print(f\"Using file: {gene_expr_file}\")\n", " try:\n", " # Handle gzipped file correctly\n", " if gene_expr_file.endswith('.gz'):\n", " with gzip.open(gene_expr_file, 'rt') as f:\n", " # Find the start of the data matrix\n", " for line in f:\n", " if line.startswith('!series_matrix_table_begin'):\n", " break\n", " # Next line should be column headers\n", " header = next(f).strip()\n", " # Look at first few data rows to examine identifiers\n", " sample_ids = [next(f).split('\\t')[0] for _ in range(10)]\n", " \n", " print(\"Sample gene identifiers:\", sample_ids)\n", " \n", " # Determine if these look like gene symbols or need mapping\n", " are_probe_ids = any('_at' in id for id in sample_ids) or any(id.startswith('ILMN_') for id in sample_ids)\n", " are_numeric = all(id.isdigit() for id in sample_ids if id)\n", " \n", " requires_gene_mapping = are_probe_ids or are_numeric or not all(id[0].isalpha() for id in sample_ids if id)\n", " else:\n", " print(\"File is not gzipped, but expected a .gz file\")\n", " requires_gene_mapping = True\n", " except Exception as e:\n", " print(f\"Error examining gene identifiers: {e}\")\n", " requires_gene_mapping = True\n", "else:\n", " print(\"No gene expression file found\")\n", " requires_gene_mapping = True\n", "\n", "print(f\"requires_gene_mapping = {requires_gene_mapping}\")\n" ] }, { "cell_type": "markdown", "id": "57ad0440", "metadata": {}, "source": [ "### Step 5: Gene Annotation" ] }, { "cell_type": "code", "execution_count": 6, "id": "e8283c38", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:12:07.766407Z", "iopub.status.busy": "2025-03-25T07:12:07.766298Z", "iopub.status.idle": "2025-03-25T07:12:17.664829Z", "shell.execute_reply": "2025-03-25T07:12:17.664221Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Gene annotation preview:\n", "{'ID': ['1', '2', '3', '4', '5'], 'COL': ['103', '103', '103', '103', '103'], 'ROW': [430.0, 428.0, 426.0, 424.0, 422.0], 'NAME': ['BrightCorner', 'NegativeControl', 'NM_001003689', 'NM_005503', 'NM_004672'], 'SPOT_ID': ['BrightCorner', 'NegativeControl', 'A_23_P80353', 'A_23_P158231', 'A_32_P223017'], 'CONTROL_TYPE': ['pos', 'neg', 'FALSE', 'FALSE', 'FALSE'], 'REFSEQ': [nan, nan, 'NM_001003689', 'NM_005503', 'NM_004672'], 'GB_ACC': [nan, nan, 'NM_001003689', 'NM_005503', 'NM_004672'], 'GENE': [nan, nan, 83746.0, 321.0, 9064.0], 'GENE_SYMBOL': [nan, nan, 'L3MBTL2', 'APBA2', 'MAP3K6'], 'GENE_NAME': [nan, nan, 'l(3)mbt-like 2 (Drosophila)', 'amyloid beta (A4) precursor protein-binding, family A, member 2 (X11-like)', 'mitogen-activated protein kinase kinase kinase 6'], 'UNIGENE_ID': [nan, nan, 'Hs.517641', 'Hs.525718', 'Hs.194694'], 'ENSEMBL_ID': [nan, nan, 'ENST00000216237', 'ENST00000219865', 'ENST00000357582'], 'TIGR_ID': [nan, nan, 'THC2264916', 'THC2241506', 'THC2236536'], 'ACCESSION_STRING': [nan, nan, 'ref|NM_001003689|ref|NM_031488|gb|AL136564|ens|ENST00000216237', 'ref|NM_005503|gb|BC082986|gb|AF029108|ens|ENST00000219865', 'ref|NM_004672|gb|AB208805|ens|ENST00000357582|ens|ENST00000374036'], 'CHROMOSOMAL_LOCATION': [nan, nan, 'chr22:39950687-39950746', 'chr15:27193462-27196566', 'chr1:27366686-27366347'], 'CYTOBAND': [nan, nan, 'hs|22q13.2', 'hs|15q13.1', 'hs|1p36.11'], 'DESCRIPTION': [nan, nan, 'Homo sapiens l(3)mbt-like 2 (Drosophila) (L3MBTL2), transcript variant 2, mRNA [NM_001003689]', 'Homo sapiens amyloid beta (A4) precursor protein-binding, family A, member 2 (X11-like) (APBA2), mRNA [NM_005503]', 'Homo sapiens mitogen-activated protein kinase kinase kinase 6 (MAP3K6), mRNA [NM_004672]'], 'GO_ID': [nan, nan, 'GO:0003714(transcription corepressor activity)|GO:0005634(nucleus)|GO:0006355(regulation of transcription, DNA-dependent)|GO:0008270(zinc ion binding)|GO:0016568(chromatin modification)|GO:0046872(metal ion binding)', 'GO:0005515(protein binding)|GO:0007399(nervous system development)|GO:0015031(protein transport)', 'GO:0000166(nucleotide binding)|GO:0004674(protein serine/threonine kinase activity)|GO:0005524(ATP binding)|GO:0006468(protein amino acid phosphorylation)|GO:0016301(kinase activity)|GO:0016740(transferase activity)'], 'SEQUENCE': [nan, nan, 'CCCGACAAGGCTTCAAGTCCAGAGCTGCCTGTCTCCGTCGAGAACATCAAGCAGGAAACA', 'CCACAGCCCACGAGAAGATAGTCCAAGCTCTGTCCAACTCGGTCGGAGAGATCCACATGA', 'GAATGTGGATTCAGGCACCATCCAAATGCTGTTGAACCATAGCTTCACCCTCCACACTCT']}\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": "8c4cf643", "metadata": {}, "source": [ "### Step 6: Gene Identifier Mapping" ] }, { "cell_type": "code", "execution_count": 7, "id": "4e5eef90", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:12:17.666816Z", "iopub.status.busy": "2025-03-25T07:12:17.666669Z", "iopub.status.idle": "2025-03-25T07:12:19.758113Z", "shell.execute_reply": "2025-03-25T07:12:19.757415Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Gene data preview after mapping:\n", "{'GSM522094': [-0.18819999999999998, 0.7342, -0.5223, 0.6615, 1.3971], 'GSM522095': [-0.2644, 0.23610000000000003, 0.1128, 0.1063, 0.2396], 'GSM522096': [-0.32, 0.2738, 0.0625, 0.1618, 0.3476], 'GSM522097': [-0.2627, 0.2891, 0.1878, 0.139, 0.1318], 'GSM522098': [-0.25780000000000003, 0.3607, 0.4293, 0.0722, 0.1184], 'GSM522099': [-0.24170000000000003, 0.1795, -0.1666, 0.1234, 0.1841], 'GSM522100': [-0.1961, 0.2155, 0.2197, 0.0703, 0.0984], 'GSM522101': [-0.23249999999999998, 0.4037, 0.0456, 0.1742, 0.2205], 'GSM522102': [-0.1814, 0.2915, -0.0154, 0.0737, 0.0977], 'GSM522103': [-0.0937, 0.4138, 0.0673, 0.278, 0.5211], 'GSM522104': [-0.2962, 0.1915, 0.2145, 0.145, 0.0456], 'GSM522105': [-0.242, 0.0736, 0.1543, 0.0761, -0.0113], 'GSM522106': [-0.2652, 0.4219, 0.2232, 0.2397, 0.2875], 'GSM522107': [-0.3797, 0.0758, -0.1404, 0.1503, 0.2481], 'GSM522108': [-0.3331, 0.2781, 0.5383, 0.2188, 0.4684], 'GSM522109': [-0.3078, 0.3024, -0.1255, 0.0939, 0.0913], 'GSM522110': [-0.332, 0.0897, 0.1557, 0.1461, 0.0502], 'GSM522111': [-0.2661, 0.3275, -0.1678, 0.1766, 0.3235], 'GSM522112': [-0.28440000000000004, 0.5649, -0.2904, 0.3228, 0.4666], 'GSM522113': [-0.3477, -0.34009999999999996, -0.3456, 0.1913, 0.1553], 'GSM522114': [-0.24860000000000002, 0.1546, -0.1621, 0.1837, 0.2544], 'GSM522115': [-0.3389, 0.2921, -0.0828, 0.1191, 0.1083], 'GSM522116': [-0.3011, 0.11860000000000001, 0.3393, 0.0899, 0.1112], 'GSM522117': [-0.2823, 0.41409999999999997, 0.4492, 0.1111, 0.3419], 'GSM522118': [-0.20690000000000003, 0.2278, 0.1409, 0.1795, 0.4434], 'GSM522119': [-0.37829999999999997, 0.45489999999999997, -0.1595, 0.1817, 0.1524], 'GSM522120': [-0.30870000000000003, 0.3184, 0.1873, 0.1485, 0.4918], 'GSM522121': [-0.23759999999999998, -0.08300000000000002, 0.1053, 0.1937, 0.1009], 'GSM522122': [-0.11510000000000001, 0.06230000000000001, 0.0221, 0.2303, 0.2706], 'GSM522123': [-0.3045, 0.2842, -0.4603, 0.3911, 0.6673], 'GSM522124': [-0.2664, 0.007699999999999985, -0.7831, 0.3699, 0.3827], 'GSM522125': [-0.28909999999999997, 0.094, -0.5557, 0.2725, 0.3275], 'GSM522126': [-0.1027, -0.018799999999999997, 0.2428, 0.3299, 0.3869], 'GSM522127': [-0.26839999999999997, 0.48530000000000006, -0.6744, 0.4079, 0.581], 'GSM522128': [-0.2944, 0.10819999999999999, -0.4546, 0.1979, 0.2268], 'GSM522129': [-0.256, 0.2951, -0.0858, 0.2541, 0.3385], 'GSM522130': [-0.14, 0.095, -0.3546, 0.2843, 0.2202], 'GSM522131': [-0.2113, 0.3155, -0.1475, 0.3034, 0.2562], 'GSM522132': [-0.1064, 0.2873, 0.3775, 0.2327, 0.451], 'GSM522133': [-0.2766, 0.19740000000000002, -0.264, 0.2282, 0.0537], 'GSM522134': [-0.15689999999999998, 0.4586, -0.5753, 0.5585, 0.8799], 'GSM522135': [-0.2704, 0.08249999999999999, -0.1643, 0.1781, 0.2923], 'GSM522136': [-0.2891, 0.32630000000000003, 0.0062, 0.1845, 0.1248], 'GSM522137': [-0.1619, 0.2986, 0.0078, 0.1777, 0.2604], 'GSM522138': [-0.2427, 0.3458, 0.4133, 0.3139, 0.3768], 'GSM522139': [-0.30460000000000004, 0.24830000000000002, -0.2318, 0.2358, 0.1738], 'GSM522140': [-0.2606, 0.5045, 0.0749, 0.7197, 0.4944], 'GSM522141': [-0.19829999999999998, 0.2286, -0.2834, 0.2237, 0.4078], 'GSM522142': [-0.2851, 0.2314, -0.2699, 0.2464, 0.2959], 'GSM522143': [-0.3198, 0.1827, -0.2133, 0.2284, 0.2338], 'GSM522144': [-0.24680000000000002, 0.2263, -0.2402, 0.3146, 0.6731], 'GSM522145': [-0.23869999999999997, 0.17070000000000002, 0.0398, 0.1198, 0.2887], 'GSM522146': [-0.3419, 0.03230000000000002, -0.195, 0.3038, 0.6025], 'GSM522147': [-0.2632, 0.2954, -0.2241, 0.2403, 0.7273], 'GSM522148': [-0.329, 0.07050000000000001, 0.1068, 0.1123, 0.1701], 'GSM522149': [-0.17270000000000002, 0.3684, 0.3578, 0.1311, 0.0386], 'GSM522150': [-0.30410000000000004, 0.3593, 0.0874, 0.2567, 0.1596], 'GSM522151': [-0.2549, 0.1853, -0.1123, 0.2592, 0.3374], 'GSM522152': [-0.2309, 0.2115, 0.3686, 0.2732, 0.5163], 'GSM522153': [-0.26699999999999996, 0.0436, -0.1245, 0.1487, 0.4], 'GSM522154': [-0.23220000000000002, 0.3317, 0.428, 0.1629, 0.2726], 'GSM522155': [-0.2595, 0.0121, -0.1682, 0.222, 0.4929], 'GSM522156': [-0.3969, 0.1577, -0.9737, 0.4154, 0.8313], 'GSM522157': [-0.19119999999999998, 0.19519999999999998, -0.0978, 0.2028, 0.1282], 'GSM522158': [-0.24870000000000003, 0.3901, 0.1351, 0.1856, 0.0953], 'GSM522159': [-0.33830000000000005, 0.1272, 0.1757, 0.4388, 0.99], 'GSM522160': [-0.318, 0.1768, -0.015, 0.1517, 0.2173], 'GSM522161': [-0.2758, 0.2162, -0.3221, 0.3845, 1.3051], 'GSM522162': [-0.1929, 0.1345, 0.01, 0.1654, -0.0311], 'GSM522163': [-0.2816, 0.38130000000000003, -0.0123, 0.3192, 0.578], 'GSM522164': [-0.21109999999999998, 0.2783, 0.4607, 0.1361, 0.3845], 'GSM522165': [-0.2816, 0.2088, -0.7935, 0.4501, 1.2288], 'GSM522166': [-0.29000000000000004, 0.1784, -0.1502, 0.2155, 0.0668], 'GSM522167': [-0.25789999999999996, 0.2964, -0.5217, 0.2725, 0.2463], 'GSM522168': [-0.216, 0.4504, -0.2169, 0.4411, 0.7318], 'GSM522169': [-0.2939, 0.323, 0.4899, 0.0581, 0.0712], 'GSM522170': [-0.2711, 0.31989999999999996, 0.2761, 0.0746, 0.3738], 'GSM522171': [-0.3019, 0.2419, 0.0096, 0.1252, 0.1404], 'GSM522172': [-0.27159999999999995, 0.1522, -0.3079, 0.1765, 0.9522], 'GSM522173': [-0.29319999999999996, 0.5075000000000001, 0.0505, 0.2089, 0.064], 'GSM522174': [-0.2655, 0.0512, -0.1083, 0.1506, 0.3104], 'GSM522175': [-0.1376, 0.7358, 0.1365, 0.4123, 1.5683], 'GSM522176': [-0.274, 0.403, 0.2906, 0.0758, -0.0238], 'GSM522177': [-0.31220000000000003, 0.5299, 0.1136, 0.1298, 0.0069], 'GSM522178': [-0.3912, -0.0391, -0.0398, 0.2091, 0.4603], 'GSM522179': [-0.28059999999999996, 0.23620000000000002, 0.3356, 0.0998, 0.2594], 'GSM522180': [-0.2034, 0.0392, 0.5297, 0.0913, 0.291], 'GSM522181': [-0.3176, 0.2688, -0.2109, 0.3339, 0.7611], 'GSM522182': [-0.2958, 0.3038, 0.3729, 0.1191, 0.2042], 'GSM522183': [-0.15990000000000001, 0.0987, 0.4006, 0.1262, 0.3556], 'GSM522184': [-0.3029, 0.29259999999999997, 0.0757, 0.0878, 0.1047], 'GSM522185': [-0.37760000000000005, 0.23319999999999996, -0.4679, 0.4663, 1.6664], 'GSM522186': [-0.2439, 0.2461, -0.5523, 0.1357, -0.0013], 'GSM522187': [-0.3472, 0.5281, -0.8241, 0.3631, 1.2638], 'GSM522188': [-0.2865, 0.36329999999999996, -0.9867, 0.3102, 0.8612], 'GSM522189': [-0.348, 0.1452, -0.232, 0.1814, 0.1268], 'GSM522190': [-0.2454, 0.29390000000000005, 0.3159, 0.1601, -0.0072], 'GSM522191': [-0.428, 0.137, 0.3052, 0.0222, 0.1179], 'GSM522192': [-0.26380000000000003, 0.16060000000000002, 0.2283, 0.0787, 0.0073], 'GSM522193': [-0.4122, 0.3232, -0.322, 0.1759, 0.1225], 'GSM522194': [-0.3515, 0.3553, 0.2229, 0.2036, 0.2981], 'GSM522195': [-0.3348, 0.1759, -0.0696, 0.1037, 0.19], 'GSM522196': [-0.3337, 0.24810000000000001, 0.0445, 0.0928, 0.1796], 'GSM522197': [-0.3076, 0.0945, -0.2117, 0.1127, 0.1993], 'GSM522198': [-0.19419999999999998, 0.0945, 0.1727, 0.1055, 0.3777], 'GSM522199': [-0.2487, 0.010399999999999993, -0.0239, 0.2183, 0.3655], 'GSM522200': [-0.1592, 0.3358, 0.6159, 0.2815, 0.6721], 'GSM522201': [-0.2686, 0.20600000000000002, -0.2083, 0.299, 0.6624], 'GSM522202': [-0.21080000000000002, 0.3265, -0.5759, 0.1951, 0.147], 'GSM522203': [-0.32130000000000003, 0.1382, -0.0241, 0.2042, 0.4408], 'GSM522204': [-0.2572, 0.4163, 0.0964, 0.1369, -0.0322], 'GSM522205': [-0.2996, -0.04849999999999999, 0.0343, 0.3887, 0.2961], 'GSM522206': [-0.4216, -0.04239999999999999, 0.209, 0.1448, 0.2285], 'GSM522207': [-0.26089999999999997, -0.06939999999999999, -0.2217, 0.1384, 0.239], 'GSM522208': [-0.2412, -0.1053, 0.1013, 0.093, 0.2689], 'GSM522209': [-0.2527, 0.2748, 0.4481, 0.1326, 0.3775], 'GSM522210': [-0.3097, 0.0968, 0.3615, 0.2368, 0.7664], 'GSM522211': [-0.12689999999999999, -0.004299999999999998, 0.3047, 0.121, 0.1927], 'GSM522212': [-0.29910000000000003, 0.03589999999999999, -0.2222, 0.3676, 0.5808], 'GSM522213': [-0.25680000000000003, -0.14529999999999998, 0.2078, 0.1395, 0.0228], 'GSM522214': [-0.30079999999999996, 0.2375, 0.1231, 0.3364, 0.6159], 'GSM522215': [-0.3782, -0.1177, -0.1781, 0.2326, 0.4023], 'GSM522216': [-0.3286, -0.0824, 0.3268, 0.0889, 0.1984], 'GSM522217': [-0.2262, 0.3134, 0.3218, 0.1059, 0.0127], 'GSM522218': [-0.28600000000000003, 0.3855, 0.1546, 0.1421, 0.0318], 'GSM522219': [-0.1506, 0.27099999999999996, -0.1985, 0.6496, 1.7745], 'GSM522220': [-0.2984, -0.0915, 0.0565, 0.1379, 0.1712], 'GSM522221': [-0.2631, 0.1308, -0.2313, 0.2012, 0.7563], 'GSM522222': [-0.1891, 0.2678, -0.2629, 0.2614, 0.591], 'GSM522223': [-0.13640000000000002, 0.20029999999999998, 0.4438, 0.302, 0.6127], 'GSM522224': [-0.2925, 0.4739, -0.8758, 0.6226, 1.2991], 'GSM522225': [-0.2152, 0.113, 0.3945, 0.087, 0.1072], 'GSM522226': [-0.23940000000000003, 0.1684, 0.0747, 0.1534, 0.1334], 'GSM522227': [-0.2935, 0.39339999999999997, -0.1785, 0.1526, 0.0793], 'GSM522228': [-0.348, 0.2761, -0.6485, 0.2774, 0.3614], 'GSM522229': [-0.3243, 0.1645, 0.0875, 0.1619, 0.7062], 'GSM522230': [-0.2781, 0.08910000000000001, 0.0938, 0.1412, 0.2816], 'GSM522231': [-0.2556, 0.0917, 0.0973, 0.1748, 0.2648], 'GSM522232': [-0.3141, 0.2886, 0.0436, 0.1862, 0.2206], 'GSM522233': [-0.2346, 0.1487, -0.1066, 0.168, 0.0907], 'GSM522234': [-0.2741, 0.1163, -0.5383, 0.1511, 0.2115], 'GSM522235': [-0.2953, 0.3002, -0.5261, 0.174, 0.1565], 'GSM522236': [-0.39999999999999997, -0.013600000000000001, 0.1709, 0.1946, 0.1583], 'GSM522237': [-0.3619, 0.3248, -0.0332, 0.1958, 0.2384], 'GSM522238': [-0.26489999999999997, 0.2026, -0.2722, 0.2487, 0.109], 'GSM522239': [-0.2152, 0.11220000000000001, -0.3856, 0.2924, 0.3794], 'GSM522240': [-0.3411, 0.0938, 0.0071, 0.1968, 0.392], 'GSM522241': [-0.2307, 0.25279999999999997, -0.0669, 0.6549, 1.1905], 'GSM522242': [-0.3316, -0.022900000000000004, -0.1036, 0.1381, 0.1639], 'GSM522243': [-0.2814, 0.20249999999999999, -0.6517, 0.2671, 0.1827], 'GSM522244': [-0.2082, 0.4155, 0.5448, 0.5887, 1.1187], 'GSM522245': [-0.3282, 0.1607, 0.1423, 0.1712, 0.0283], 'GSM522246': [-0.178, 0.5981, -0.8245, 0.486, 1.0908], 'GSM522247': [-0.2014, 0.3951, 0.1691, 0.1691, 0.1677], 'GSM522248': [-0.1588, 0.047200000000000006, -0.2296, 0.2299, 0.3429], 'GSM522249': [-0.3246, 0.3735, -0.3135, 0.2358, 0.179], 'GSM522250': [-0.26089999999999997, -0.05639999999999999, 0.1599, 0.1283, 0.1092], 'GSM522251': [-0.2105, 0.42289999999999994, -0.3948, 0.3954, 1.0921], 'GSM522252': [-0.2712, 0.1958, 0.1742, 0.1841, 0.03], 'GSM522253': [-0.3699, 0.24080000000000001, -0.0773, 0.3837, 0.4231], 'GSM522254': [-0.24669999999999997, 0.29979999999999996, 0.116, 0.0947, 0.0263], 'GSM522255': [-0.2919, 0.31589999999999996, 0.0362, 0.1257, 0.2032], 'GSM522256': [-0.2576, 0.1914, 0.507, 0.167, 0.0167], 'GSM522257': [-0.3059, 0.0707, 0.2001, 0.2658, 0.3928], 'GSM522258': [-0.3154, 0.04769999999999999, 0.3594, 0.1387, 0.0031], 'GSM522259': [-0.23609999999999998, 0.3476, -0.3085, 0.3905, 1.0455], 'GSM522260': [0.0664, -0.036500000000000005, 0.5117, 0.1513, 0.3497], 'GSM522261': [-0.2832, 0.2762, -0.5798, 0.3178, 0.1558], 'GSM522262': [-0.2698, 0.1992, -0.0677, 0.1174, 0.011], 'GSM522263': [-0.3599, 0.31789999999999996, 0.1226, 0.1945, -0.0336], 'GSM522264': [-0.3588, 0.0194, -0.3066, 0.3082, 0.3625], 'GSM522265': [-0.17429999999999998, -0.122, 0.6406, 0.1117, 0.1855]}\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Gene expression data saved to ../../output/preprocess/Irritable_bowel_syndrome_(IBS)/gene_data/GSE20881.csv\n" ] } ], "source": [ "# 1. After examining the previews from previous steps:\n", "# - Gene expression data has numerical IDs (1, 2, 3, etc.) in its index\n", "# - Gene annotation data has 'ID' column matching these numerical IDs\n", "# - 'GENE_SYMBOL' column contains the gene symbols we need to map to\n", "\n", "# 2. Extract the mapping between probe IDs and gene symbols\n", "mapping_data = get_gene_mapping(gene_annotation, prob_col='ID', gene_col='GENE_SYMBOL')\n", "\n", "# 3. Convert probe-level measurements to gene expression data\n", "gene_data = apply_gene_mapping(gene_data, mapping_data)\n", "\n", "# 4. Preview the gene data after mapping\n", "print(\"Gene data preview after mapping:\")\n", "print(preview_df(gene_data))\n", "\n", "# 5. Ensure the output directory exists\n", "os.makedirs(os.path.dirname(out_gene_data_file), exist_ok=True)\n", "\n", "# 6. Save the gene expression data\n", "gene_data.to_csv(out_gene_data_file)\n", "print(f\"Gene expression data saved to {out_gene_data_file}\")\n" ] }, { "cell_type": "markdown", "id": "50baf01a", "metadata": {}, "source": [ "### Step 7: Data Normalization and Linking" ] }, { "cell_type": "code", "execution_count": 8, "id": "714e3f8e", "metadata": { "execution": { "iopub.execute_input": "2025-03-25T07:12:19.759948Z", "iopub.status.busy": "2025-03-25T07:12:19.759795Z", "iopub.status.idle": "2025-03-25T07:12:35.053984Z", "shell.execute_reply": "2025-03-25T07:12:35.053190Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Shape of gene data after normalization: (16774, 172)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Normalized gene expression data saved to ../../output/preprocess/Irritable_bowel_syndrome_(IBS)/gene_data/GSE20881.csv\n", "Clinical data preview (after transposing):\n", "{'Irritable_bowel_syndrome_(IBS)': [0.0, 0.0, 0.0, 0.0, 0.0], 'Age': [43.0, 40.0, 21.0, 21.0, 18.0]}\n", "Clinical data shape: (171, 2)\n", "Linked data shape: (171, 16776)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Data shape after handling missing values: (171, 16776)\n", "For the feature 'Irritable_bowel_syndrome_(IBS)', the least common label is '0.0' with 72 occurrences. This represents 42.11% of the dataset.\n", "The distribution of the feature 'Irritable_bowel_syndrome_(IBS)' in this dataset is fine.\n", "\n", "Quartiles for 'Age':\n", " 25%: 28.0\n", " 50% (Median): 41.0\n", " 75%: 48.0\n", "Min: 16.0\n", "Max: 945.0\n", "The distribution of the feature 'Age' in this dataset is fine.\n", "\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Linked data saved to ../../output/preprocess/Irritable_bowel_syndrome_(IBS)/GSE20881.csv\n" ] } ], "source": [ "# 1. Normalize gene symbols in the gene expression data\n", "gene_data = pd.read_csv(out_gene_data_file, index_col=0)\n", "normalized_gene_data = normalize_gene_symbols_in_index(gene_data)\n", "print(f\"Shape of gene data after normalization: {normalized_gene_data.shape}\")\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 expression data saved to {out_gene_data_file}\")\n", "\n", "# 2. Load clinical data that was extracted in Step 2\n", "# The clinical data is transposed (samples as columns, traits as rows)\n", "# We need to transpose it to have samples as rows and traits as columns\n", "clinical_data = pd.read_csv(out_clinical_data_file, index_col=0).T\n", "clinical_data.columns = [trait, 'Age'] # Rename columns to match expected format\n", "\n", "# Check the structure and contents of clinical data\n", "print(\"Clinical data preview (after transposing):\")\n", "print(preview_df(clinical_data))\n", "print(f\"Clinical data shape: {clinical_data.shape}\")\n", "\n", "# 3. Link the clinical and genetic data\n", "# Transpose gene data to have samples as rows, genes as columns\n", "gene_data_t = normalized_gene_data.T\n", "# Join the two dataframes on their index (sample IDs)\n", "linked_data = clinical_data.join(gene_data_t)\n", "print(f\"Linked data shape: {linked_data.shape}\")\n", "\n", "# 4. Handle missing values systematically\n", "linked_data = handle_missing_values(linked_data, trait)\n", "print(f\"Data shape after handling missing values: {linked_data.shape}\")\n", "\n", "# Debug: Check if we still have data after missing value handling\n", "if linked_data.shape[0] == 0:\n", " print(\"WARNING: All samples were removed during missing value handling!\")\n", " print(\"This may indicate a problem with data alignment or excessive missing values.\")\n", "\n", "# 5. Determine if trait and demographic features are biased\n", "is_biased, unbiased_linked_data = judge_and_remove_biased_features(linked_data, trait)\n", "\n", "# 6. Conduct final validation\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_biased,\n", " df=unbiased_linked_data,\n", " note=\"Dataset contains gene expression data comparing Crohn's disease patients and healthy controls.\"\n", ")\n", "\n", "# 7. Save the linked data if usable\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 }