File size: 10,694 Bytes
53eb596
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "ea33696f",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T07:56:07.184862Z",
     "iopub.status.busy": "2025-03-25T07:56:07.184459Z",
     "iopub.status.idle": "2025-03-25T07:56:07.349548Z",
     "shell.execute_reply": "2025-03-25T07:56:07.349121Z"
    }
   },
   "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 = \"Lupus_(Systemic_Lupus_Erythematosus)\"\n",
    "cohort = \"GSE193442\"\n",
    "\n",
    "# Input paths\n",
    "in_trait_dir = \"../../input/GEO/Lupus_(Systemic_Lupus_Erythematosus)\"\n",
    "in_cohort_dir = \"../../input/GEO/Lupus_(Systemic_Lupus_Erythematosus)/GSE193442\"\n",
    "\n",
    "# Output paths\n",
    "out_data_file = \"../../output/preprocess/Lupus_(Systemic_Lupus_Erythematosus)/GSE193442.csv\"\n",
    "out_gene_data_file = \"../../output/preprocess/Lupus_(Systemic_Lupus_Erythematosus)/gene_data/GSE193442.csv\"\n",
    "out_clinical_data_file = \"../../output/preprocess/Lupus_(Systemic_Lupus_Erythematosus)/clinical_data/GSE193442.csv\"\n",
    "json_path = \"../../output/preprocess/Lupus_(Systemic_Lupus_Erythematosus)/cohort_info.json\"\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e5a48629",
   "metadata": {},
   "source": [
    "### Step 1: Initial Data Loading"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "cf0623c8",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T07:56:07.351220Z",
     "iopub.status.busy": "2025-03-25T07:56:07.351084Z",
     "iopub.status.idle": "2025-03-25T07:56:07.439909Z",
     "shell.execute_reply": "2025-03-25T07:56:07.439436Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Background Information:\n",
      "!Series_title\t\"Transcriptional profiling of human KIR+ CD8 T cells\"\n",
      "!Series_summary\t\"This SuperSeries is composed of the SubSeries listed below.\"\n",
      "!Series_overall_design\t\"Refer to individual Series\"\n",
      "Sample Characteristics Dictionary:\n",
      "{0: ['tissue: PBMC'], 1: ['cell type: KIR+ CD8 T']}\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": "01f09e30",
   "metadata": {},
   "source": [
    "### Step 2: Dataset Analysis and Clinical Feature Extraction"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "9527f20a",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T07:56:07.441198Z",
     "iopub.status.busy": "2025-03-25T07:56:07.441086Z",
     "iopub.status.idle": "2025-03-25T07:56:07.447669Z",
     "shell.execute_reply": "2025-03-25T07:56:07.447324Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Analyze the dataset based on the background information and sample characteristics\n",
    "\n",
    "# 1. Gene Expression Data Availability\n",
    "# Based on the series title and sample characteristics, this dataset seems to focus on transcriptional profiling\n",
    "# of human KIR+ CD8 T cells, which suggests it contains gene expression data.\n",
    "is_gene_available = True\n",
    "\n",
    "# 2. Variable Availability and Data Type Conversion\n",
    "# 2.1 Data Availability\n",
    "# From the sample characteristics, we don't see explicit fields for lupus/SLE status, age, or gender\n",
    "# The dataset appears to be focusing only on cell types rather than patient characteristics\n",
    "\n",
    "# For trait (SLE)\n",
    "# No explicit SLE status is provided in the sample characteristics\n",
    "trait_row = None  # No explicit trait information available\n",
    "\n",
    "# For age\n",
    "# No age information is provided in the sample characteristics\n",
    "age_row = None  # No age information available\n",
    "\n",
    "# For gender\n",
    "# No gender information is provided in the sample characteristics\n",
    "gender_row = None  # No gender information available\n",
    "\n",
    "# 2.2 Data Type Conversion\n",
    "# Since we don't have access to these variables, we'll define placeholder conversion functions\n",
    "\n",
    "def convert_trait(value):\n",
    "    # Placeholder function since trait data is not available\n",
    "    if value is None:\n",
    "        return None\n",
    "    \n",
    "    value = value.split(': ')[-1].strip().lower()\n",
    "    if 'lupus' in value or 'sle' in value:\n",
    "        return 1\n",
    "    elif 'control' in value or 'healthy' in value or 'normal' in value:\n",
    "        return 0\n",
    "    else:\n",
    "        return None\n",
    "\n",
    "def convert_age(value):\n",
    "    # Placeholder function since age data is not available\n",
    "    if value is None:\n",
    "        return None\n",
    "    \n",
    "    try:\n",
    "        # Extract value after colon and convert to float\n",
    "        age_str = value.split(': ')[-1].strip()\n",
    "        return float(age_str)\n",
    "    except (ValueError, AttributeError):\n",
    "        return None\n",
    "\n",
    "def convert_gender(value):\n",
    "    # Placeholder function since gender data is not available\n",
    "    if value is None:\n",
    "        return None\n",
    "    \n",
    "    value = value.split(': ')[-1].strip().lower()\n",
    "    if 'female' in value or 'f' == value:\n",
    "        return 0\n",
    "    elif 'male' in value or 'm' == value:\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",
    "\n",
    "# Save the initial filtering result\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. Since trait_row is None, we skip the clinical feature extraction step\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4f35d3f2",
   "metadata": {},
   "source": [
    "### Step 3: Gene Data Extraction"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "d51a12ed",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-25T07:56:07.448860Z",
     "iopub.status.busy": "2025-03-25T07:56:07.448757Z",
     "iopub.status.idle": "2025-03-25T07:56:07.992086Z",
     "shell.execute_reply": "2025-03-25T07:56:07.991423Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "This appears to be a SuperSeries. Looking at the SOFT file to find potential subseries:\n",
      "No subseries references found in the first 1000 lines of the SOFT file.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Gene data extraction result:\n",
      "Number of rows: 0\n",
      "First 20 gene/probe identifiers:\n",
      "Index([], dtype='object', name='ID')\n"
     ]
    }
   ],
   "source": [
    "# 1. First get the path to the soft and matrix files\n",
    "soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)\n",
    "\n",
    "# 2. Looking more carefully at the background information\n",
    "# This is a SuperSeries which doesn't contain direct gene expression data\n",
    "# Need to investigate the soft file to find the subseries\n",
    "print(\"This appears to be a SuperSeries. Looking at the SOFT file to find potential subseries:\")\n",
    "\n",
    "# Open the SOFT file to try to identify subseries\n",
    "with gzip.open(soft_file, 'rt') as f:\n",
    "    subseries_lines = []\n",
    "    for i, line in enumerate(f):\n",
    "        if 'Series_relation' in line and 'SuperSeries of' in line:\n",
    "            subseries_lines.append(line.strip())\n",
    "        if i > 1000:  # Limit search to first 1000 lines\n",
    "            break\n",
    "\n",
    "# Display the subseries found\n",
    "if subseries_lines:\n",
    "    print(\"Found potential subseries references:\")\n",
    "    for line in subseries_lines:\n",
    "        print(line)\n",
    "else:\n",
    "    print(\"No subseries references found in the first 1000 lines of the SOFT file.\")\n",
    "\n",
    "# Despite trying to extract gene data, we expect it might fail because this is a SuperSeries\n",
    "try:\n",
    "    gene_data = get_genetic_data(matrix_file)\n",
    "    print(\"\\nGene data extraction result:\")\n",
    "    print(\"Number of rows:\", len(gene_data))\n",
    "    print(\"First 20 gene/probe identifiers:\")\n",
    "    print(gene_data.index[:20])\n",
    "except Exception as e:\n",
    "    print(f\"Error extracting gene data: {e}\")\n",
    "    print(\"This confirms the dataset is a SuperSeries without direct gene expression data.\")"
   ]
  }
 ],
 "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
}