Liu-Hy commited on
Commit
83ac729
·
verified ·
1 Parent(s): ba264e7

Add files using upload-large-folder tool

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. .gitattributes +29 -0
  2. p1/preprocess/Esophageal_Cancer/TCGA.csv +3 -0
  3. p1/preprocess/Esophageal_Cancer/gene_data/TCGA.csv +3 -0
  4. p1/preprocess/Essential_Thrombocythemia/gene_data/GSE159514.csv +3 -0
  5. p1/preprocess/Gastroesophageal_reflux_disease_(GERD)/TCGA.csv +3 -0
  6. p1/preprocess/Gastroesophageal_reflux_disease_(GERD)/gene_data/GSE43580.csv +3 -0
  7. p1/preprocess/Gastroesophageal_reflux_disease_(GERD)/gene_data/GSE77563.csv +3 -0
  8. p1/preprocess/Gastroesophageal_reflux_disease_(GERD)/gene_data/TCGA.csv +3 -0
  9. p1/preprocess/Gaucher_Disease/GSE124283.csv +3 -0
  10. p1/preprocess/Gaucher_Disease/gene_data/GSE124283.csv +3 -0
  11. p1/preprocess/Generalized_Anxiety_Disorder/gene_data/GSE61672.csv +3 -0
  12. p1/preprocess/Glioblastoma/code/GSE279426.py +147 -0
  13. p1/preprocess/Glioblastoma/code/GSE39144.py +135 -0
  14. p1/preprocess/Glioblastoma/code/TCGA.py +119 -0
  15. p1/preprocess/Glioblastoma/gene_data/GSE134470.csv +3 -0
  16. p1/preprocess/Glioblastoma/gene_data/GSE148949.csv +3 -0
  17. p1/preprocess/Glioblastoma/gene_data/GSE159000.csv +0 -0
  18. p1/preprocess/Glioblastoma/gene_data/GSE175700.csv +3 -0
  19. p1/preprocess/Glioblastoma/gene_data/GSE178236.csv +3 -0
  20. p1/preprocess/Glioblastoma/gene_data/GSE226976.csv +0 -0
  21. p1/preprocess/Glioblastoma/gene_data/GSE249289.csv +0 -0
  22. p1/preprocess/Glioblastoma/gene_data/GSE279426.csv +0 -0
  23. p1/preprocess/Glioblastoma/gene_data/GSE39144.csv +3 -0
  24. p1/preprocess/Glucocorticoid_Sensitivity/GSE32962.csv +3 -0
  25. p1/preprocess/Glucocorticoid_Sensitivity/GSE33649.csv +3 -0
  26. p1/preprocess/Glucocorticoid_Sensitivity/GSE50012.csv +3 -0
  27. p1/preprocess/Glucocorticoid_Sensitivity/GSE57795.csv +3 -0
  28. p1/preprocess/Glucocorticoid_Sensitivity/GSE66705.csv +3 -0
  29. p1/preprocess/Glucocorticoid_Sensitivity/clinical_data/GSE32962.csv +2 -0
  30. p1/preprocess/Glucocorticoid_Sensitivity/clinical_data/GSE33649.csv +4 -0
  31. p1/preprocess/Glucocorticoid_Sensitivity/clinical_data/GSE48801.csv +2 -0
  32. p1/preprocess/Glucocorticoid_Sensitivity/clinical_data/GSE50012.csv +4 -0
  33. p1/preprocess/Glucocorticoid_Sensitivity/clinical_data/GSE57795.csv +2 -0
  34. p1/preprocess/Glucocorticoid_Sensitivity/clinical_data/GSE66705.csv +2 -0
  35. p1/preprocess/Glucocorticoid_Sensitivity/code/GSE15820.py +221 -0
  36. p1/preprocess/Glucocorticoid_Sensitivity/code/GSE32962.py +227 -0
  37. p1/preprocess/Glucocorticoid_Sensitivity/code/GSE33649.py +252 -0
  38. p1/preprocess/Glucocorticoid_Sensitivity/code/GSE42002.py +75 -0
  39. p1/preprocess/Glucocorticoid_Sensitivity/code/GSE48801.py +229 -0
  40. p1/preprocess/Glucocorticoid_Sensitivity/code/GSE50012.py +266 -0
  41. p1/preprocess/Glucocorticoid_Sensitivity/code/GSE57795.py +226 -0
  42. p1/preprocess/Glucocorticoid_Sensitivity/code/GSE58715.py +208 -0
  43. p1/preprocess/Glucocorticoid_Sensitivity/code/GSE65645.py +217 -0
  44. p1/preprocess/Glucocorticoid_Sensitivity/code/GSE66705.py +228 -0
  45. p1/preprocess/Glucocorticoid_Sensitivity/code/TCGA.py +56 -0
  46. p1/preprocess/Glucocorticoid_Sensitivity/cohort_info.json +1 -0
  47. p1/preprocess/Glucocorticoid_Sensitivity/gene_data/GSE15820.csv +3 -0
  48. p1/preprocess/Glucocorticoid_Sensitivity/gene_data/GSE32962.csv +3 -0
  49. p1/preprocess/Glucocorticoid_Sensitivity/gene_data/GSE33649.csv +3 -0
  50. p1/preprocess/Glucocorticoid_Sensitivity/gene_data/GSE50012.csv +3 -0
.gitattributes CHANGED
@@ -1104,3 +1104,32 @@ p1/preprocess/Essential_Thrombocythemia/gene_data/GSE174060.csv filter=lfs diff=
1104
  p1/preprocess/Essential_Thrombocythemia/gene_data/GSE61629.csv filter=lfs diff=lfs merge=lfs -text
1105
  p1/preprocess/Gastroesophageal_reflux_disease_(GERD)/GSE77563.csv filter=lfs diff=lfs merge=lfs -text
1106
  p1/preprocess/Essential_Thrombocythemia/gene_data/GSE57793.csv filter=lfs diff=lfs merge=lfs -text
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1104
  p1/preprocess/Essential_Thrombocythemia/gene_data/GSE61629.csv filter=lfs diff=lfs merge=lfs -text
1105
  p1/preprocess/Gastroesophageal_reflux_disease_(GERD)/GSE77563.csv filter=lfs diff=lfs merge=lfs -text
1106
  p1/preprocess/Essential_Thrombocythemia/gene_data/GSE57793.csv filter=lfs diff=lfs merge=lfs -text
1107
+ p1/preprocess/Essential_Thrombocythemia/gene_data/GSE159514.csv filter=lfs diff=lfs merge=lfs -text
1108
+ p1/preprocess/Gastroesophageal_reflux_disease_(GERD)/gene_data/GSE77563.csv filter=lfs diff=lfs merge=lfs -text
1109
+ p1/preprocess/Esophageal_Cancer/TCGA.csv filter=lfs diff=lfs merge=lfs -text
1110
+ p1/preprocess/Gaucher_Disease/GSE124283.csv filter=lfs diff=lfs merge=lfs -text
1111
+ p1/preprocess/Esophageal_Cancer/gene_data/TCGA.csv filter=lfs diff=lfs merge=lfs -text
1112
+ p1/preprocess/Gastroesophageal_reflux_disease_(GERD)/gene_data/GSE43580.csv filter=lfs diff=lfs merge=lfs -text
1113
+ p1/preprocess/Glioblastoma/gene_data/GSE148949.csv filter=lfs diff=lfs merge=lfs -text
1114
+ p1/preprocess/Glioblastoma/gene_data/GSE175700.csv filter=lfs diff=lfs merge=lfs -text
1115
+ p1/preprocess/Gaucher_Disease/gene_data/GSE124283.csv filter=lfs diff=lfs merge=lfs -text
1116
+ p1/preprocess/Glioblastoma/gene_data/GSE178236.csv filter=lfs diff=lfs merge=lfs -text
1117
+ p1/preprocess/Glioblastoma/gene_data/GSE134470.csv filter=lfs diff=lfs merge=lfs -text
1118
+ p1/preprocess/Generalized_Anxiety_Disorder/gene_data/GSE61672.csv filter=lfs diff=lfs merge=lfs -text
1119
+ p1/preprocess/Glioblastoma/gene_data/GSE39144.csv filter=lfs diff=lfs merge=lfs -text
1120
+ p1/preprocess/Glucocorticoid_Sensitivity/GSE32962.csv filter=lfs diff=lfs merge=lfs -text
1121
+ p1/preprocess/Glucocorticoid_Sensitivity/GSE33649.csv filter=lfs diff=lfs merge=lfs -text
1122
+ p1/preprocess/Glucocorticoid_Sensitivity/GSE50012.csv filter=lfs diff=lfs merge=lfs -text
1123
+ p1/preprocess/Glucocorticoid_Sensitivity/GSE57795.csv filter=lfs diff=lfs merge=lfs -text
1124
+ p1/preprocess/Gastroesophageal_reflux_disease_(GERD)/TCGA.csv filter=lfs diff=lfs merge=lfs -text
1125
+ p1/preprocess/Glucocorticoid_Sensitivity/gene_data/GSE32962.csv filter=lfs diff=lfs merge=lfs -text
1126
+ p1/preprocess/Glucocorticoid_Sensitivity/gene_data/GSE15820.csv filter=lfs diff=lfs merge=lfs -text
1127
+ p1/preprocess/Glucocorticoid_Sensitivity/gene_data/GSE65645.csv filter=lfs diff=lfs merge=lfs -text
1128
+ p1/preprocess/Gastroesophageal_reflux_disease_(GERD)/gene_data/TCGA.csv filter=lfs diff=lfs merge=lfs -text
1129
+ p1/preprocess/Glucocorticoid_Sensitivity/gene_data/GSE33649.csv filter=lfs diff=lfs merge=lfs -text
1130
+ p1/preprocess/Glucocorticoid_Sensitivity/gene_data/GSE57795.csv filter=lfs diff=lfs merge=lfs -text
1131
+ p1/preprocess/Glucocorticoid_Sensitivity/GSE66705.csv filter=lfs diff=lfs merge=lfs -text
1132
+ p1/preprocess/Glucocorticoid_Sensitivity/gene_data/GSE50012.csv filter=lfs diff=lfs merge=lfs -text
1133
+ p1/preprocess/Head_and_Neck_Cancer/GSE151179.csv filter=lfs diff=lfs merge=lfs -text
1134
+ p1/preprocess/Head_and_Neck_Cancer/gene_data/GSE148320.csv filter=lfs diff=lfs merge=lfs -text
1135
+ p1/preprocess/Head_and_Neck_Cancer/gene_data/GSE151179.csv filter=lfs diff=lfs merge=lfs -text
p1/preprocess/Esophageal_Cancer/TCGA.csv ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:9cdc90f2d48d68aef2f8d5317f528520a7cf3ba69702f0c8904edd95cb3702de
3
+ size 58742434
p1/preprocess/Esophageal_Cancer/gene_data/TCGA.csv ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:be8b1dd125bb45c3b527f42bfa3ddd3ac8a86d1f3a92868da89995845df4ef72
3
+ size 58741039
p1/preprocess/Essential_Thrombocythemia/gene_data/GSE159514.csv ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:3a530c1ba1aa68ff3bc2a0f2a3b81ac7058dc724a31670961c8782ec73111f48
3
+ size 21786410
p1/preprocess/Gastroesophageal_reflux_disease_(GERD)/TCGA.csv ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:184342abd4b11025497e5c986c9e6a382af24f6e4468c7821519e15673ec26c8
3
+ size 58742455
p1/preprocess/Gastroesophageal_reflux_disease_(GERD)/gene_data/GSE43580.csv ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:a5af619a5350088586843a2ad5dd0ce92c077ec4cdd74fc0297b02f37b76f097
3
+ size 32761120
p1/preprocess/Gastroesophageal_reflux_disease_(GERD)/gene_data/GSE77563.csv ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:fc197e3d0c62761d28270b799124288713b71400f6e7859a9185fea4d4a6f4b1
3
+ size 13183964
p1/preprocess/Gastroesophageal_reflux_disease_(GERD)/gene_data/TCGA.csv ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:be8b1dd125bb45c3b527f42bfa3ddd3ac8a86d1f3a92868da89995845df4ef72
3
+ size 58741039
p1/preprocess/Gaucher_Disease/GSE124283.csv ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b8d80b259923477046ecccd1b6c66bb681838f1be324b34eef01db62516d1586
3
+ size 24553953
p1/preprocess/Gaucher_Disease/gene_data/GSE124283.csv ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:9f11218efac786969f10d262ee11631a3da24a6cd20c2bf5bc82254a073f926c
3
+ size 24724228
p1/preprocess/Generalized_Anxiety_Disorder/gene_data/GSE61672.csv ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:dc3ec7db3721c2df4aee7f88f4d34dde6e8caea3f63df7c671a696f04352165c
3
+ size 33639737
p1/preprocess/Glioblastoma/code/GSE279426.py ADDED
@@ -0,0 +1,147 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Path Configuration
2
+ from tools.preprocess import *
3
+
4
+ # Processing context
5
+ trait = "Glioblastoma"
6
+ cohort = "GSE279426"
7
+
8
+ # Input paths
9
+ in_trait_dir = "../DATA/GEO/Glioblastoma"
10
+ in_cohort_dir = "../DATA/GEO/Glioblastoma/GSE279426"
11
+
12
+ # Output paths
13
+ out_data_file = "./output/preprocess/1/Glioblastoma/GSE279426.csv"
14
+ out_gene_data_file = "./output/preprocess/1/Glioblastoma/gene_data/GSE279426.csv"
15
+ out_clinical_data_file = "./output/preprocess/1/Glioblastoma/clinical_data/GSE279426.csv"
16
+ json_path = "./output/preprocess/1/Glioblastoma/cohort_info.json"
17
+
18
+ # STEP1
19
+ from tools.preprocess import *
20
+
21
+ # 1. Identify the paths to the SOFT file and the matrix file
22
+ soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)
23
+
24
+ # 2. Read the matrix file to obtain background information and sample characteristics data
25
+ background_prefixes = ['!Series_title', '!Series_summary', '!Series_overall_design']
26
+ clinical_prefixes = ['!Sample_geo_accession', '!Sample_characteristics_ch1']
27
+ background_info, clinical_data = get_background_and_clinical_data(
28
+ matrix_file,
29
+ background_prefixes,
30
+ clinical_prefixes
31
+ )
32
+
33
+ # 3. Obtain the sample characteristics dictionary from the clinical dataframe
34
+ sample_characteristics_dict = get_unique_values_by_row(clinical_data)
35
+
36
+ # 4. Explicitly print out all the background information and the sample characteristics dictionary
37
+ print("Background Information:")
38
+ print(background_info)
39
+ print("Sample Characteristics Dictionary:")
40
+ print(sample_characteristics_dict)
41
+ # Step 1: Gene Expression Data Availability
42
+ # Based on the title ("Expression data... glioblastoma") and the descriptions, it is gene expression data.
43
+ is_gene_available = True
44
+
45
+ # Step 2: Variable Availability and Data Type Conversion
46
+
47
+ # Observing the sample characteristics dictionary:
48
+ # 0 => name_in_pmid_21471286: ... (IDs)
49
+ # 1 => alternative_name: ... (IDs)
50
+ # 2 => treatment_gefitinib: T0/T1/T2
51
+ # 3 => type: human/xenograft
52
+ # 4 => egfr_amplification: A0/A1
53
+ # 5 => disease: GBM
54
+ #
55
+ # There is no row explicitly or implicitly indicating age or gender.
56
+ # For the trait "Glioblastoma", all samples appear to be GBM (no variation), thus it's not useful for association analysis.
57
+ # Hence, we conclude that trait_row = None, age_row = None, gender_row = None.
58
+
59
+ trait_row = None
60
+ age_row = None
61
+ gender_row = None
62
+
63
+ # Even though we have no actual data rows for these variables, we'll define conversion functions as placeholders:
64
+
65
+ def convert_trait(raw_value: str) -> int:
66
+ # Placeholder for trait data (not used since trait_row is None)
67
+ return None
68
+
69
+ def convert_age(raw_value: str) -> float:
70
+ # Placeholder for age data (not used since age_row is None)
71
+ return None
72
+
73
+ def convert_gender(raw_value: str) -> int:
74
+ # Placeholder for gender data (not used since gender_row is None)
75
+ return None
76
+
77
+ # Step 3: Save Metadata
78
+ # Check trait data availability: if trait_row is None => is_trait_available = False
79
+ is_trait_available = (trait_row is not None)
80
+
81
+ is_usable = validate_and_save_cohort_info(
82
+ is_final=False,
83
+ cohort=cohort,
84
+ info_path=json_path,
85
+ is_gene_available=is_gene_available,
86
+ is_trait_available=is_trait_available
87
+ )
88
+
89
+ # Step 4: Clinical Feature Extraction
90
+ # Since trait_row is None, we skip clinical feature extraction.
91
+ # STEP3
92
+ # 1. Use the get_genetic_data function from the library to get the gene_data from the matrix_file previously defined.
93
+ gene_data = get_genetic_data(matrix_file)
94
+
95
+ # 2. Print the first 20 row IDs (gene or probe identifiers) for future observation.
96
+ print(gene_data.index[:20])
97
+ # From the given index values, such as "1007_s_at" and "1053_at", these look like Affymetrix probe set IDs
98
+ # rather than standard human gene symbols. Hence they require mapping.
99
+ print("requires_gene_mapping = True")
100
+ # STEP5
101
+ # 1. Use the 'get_gene_annotation' function from the library to get gene annotation data from the SOFT file.
102
+ gene_annotation = get_gene_annotation(soft_file)
103
+
104
+ # 2. Use the 'preview_df' function from the library to preview the data and print out the results.
105
+ print("Gene annotation preview:")
106
+ print(preview_df(gene_annotation))
107
+ # STEP: Gene Identifier Mapping
108
+
109
+ # 1. Based on the preview, the column "ID" in the gene annotation matches the Affymetrix probe IDs
110
+ # used in the gene_data. The "Gene Symbol" column stores the gene symbols we want.
111
+ probe_col = "ID"
112
+ symbol_col = "Gene Symbol"
113
+
114
+ # 2. Get a dataframe mapping probe IDs to their corresponding gene symbols.
115
+ mapping_df = get_gene_mapping(gene_annotation, prob_col=probe_col, gene_col=symbol_col)
116
+
117
+ # 3. Convert probe-level measurements to gene-level expression values.
118
+ gene_data = apply_gene_mapping(gene_data, mapping_df)
119
+ print("Mapped gene_data shape:", gene_data.shape)
120
+ print("Mapped gene_data index preview:", gene_data.index[:10])
121
+ # STEP 5
122
+ # 1) Normalize the gene expression data
123
+ normalized_gene_data = normalize_gene_symbols_in_index(gene_data)
124
+ normalized_gene_data.to_csv(out_gene_data_file)
125
+
126
+ # Since there is no trait data (trait_row was None), we cannot link clinical features or perform bias checks.
127
+ # We still must do final validation to record that trait data is unavailable.
128
+
129
+ # Provide a placeholder for is_biased; it won't matter because is_trait_available is False.
130
+ is_biased_placeholder = False
131
+
132
+ # 5) Perform final validation and save metadata
133
+ is_usable = validate_and_save_cohort_info(
134
+ is_final=True,
135
+ cohort=cohort,
136
+ info_path=json_path,
137
+ is_gene_available=True, # We do have gene expression data
138
+ is_trait_available=False, # Trait data is unavailable
139
+ is_biased=is_biased_placeholder,
140
+ df=normalized_gene_data, # We pass the gene data but there's no trait column
141
+ note="No trait data; cannot complete linking or bias checks."
142
+ )
143
+
144
+ # 6) If the dataset is deemed usable, save the final linked data
145
+ # In this scenario, is_usable will be False because the trait is not available.
146
+ if is_usable:
147
+ normalized_gene_data.to_csv(out_data_file)
p1/preprocess/Glioblastoma/code/GSE39144.py ADDED
@@ -0,0 +1,135 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Path Configuration
2
+ from tools.preprocess import *
3
+
4
+ # Processing context
5
+ trait = "Glioblastoma"
6
+ cohort = "GSE39144"
7
+
8
+ # Input paths
9
+ in_trait_dir = "../DATA/GEO/Glioblastoma"
10
+ in_cohort_dir = "../DATA/GEO/Glioblastoma/GSE39144"
11
+
12
+ # Output paths
13
+ out_data_file = "./output/preprocess/1/Glioblastoma/GSE39144.csv"
14
+ out_gene_data_file = "./output/preprocess/1/Glioblastoma/gene_data/GSE39144.csv"
15
+ out_clinical_data_file = "./output/preprocess/1/Glioblastoma/clinical_data/GSE39144.csv"
16
+ json_path = "./output/preprocess/1/Glioblastoma/cohort_info.json"
17
+
18
+ # STEP1
19
+ from tools.preprocess import *
20
+
21
+ # 1. Identify the paths to the SOFT file and the matrix file
22
+ soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)
23
+
24
+ # 2. Read the matrix file to obtain background information and sample characteristics data
25
+ background_prefixes = ['!Series_title', '!Series_summary', '!Series_overall_design']
26
+ clinical_prefixes = ['!Sample_geo_accession', '!Sample_characteristics_ch1']
27
+ background_info, clinical_data = get_background_and_clinical_data(
28
+ matrix_file,
29
+ background_prefixes,
30
+ clinical_prefixes
31
+ )
32
+
33
+ # 3. Obtain the sample characteristics dictionary from the clinical dataframe
34
+ sample_characteristics_dict = get_unique_values_by_row(clinical_data)
35
+
36
+ # 4. Explicitly print out all the background information and the sample characteristics dictionary
37
+ print("Background Information:")
38
+ print(background_info)
39
+ print("Sample Characteristics Dictionary:")
40
+ print(sample_characteristics_dict)
41
+
42
+ # 1. Gene Expression Data Availability
43
+ # Assuming this is a gene expression dataset
44
+ is_gene_available = True
45
+
46
+ # 2. Variable Availability and Data Type Conversion
47
+ # No variation or data was found for trait, age, or gender in this dataset,
48
+ # so we set their row identifiers to None
49
+ trait_row = None
50
+ age_row = None
51
+ gender_row = None
52
+
53
+ # Define conversion functions, though we won't actually use them given our row selections
54
+ def convert_trait(x: str):
55
+ # Example stub: parse after colon
56
+ val = x.split(':', 1)[-1].strip() if ':' in x else x.strip()
57
+ # If it were available and binary, we'd convert to 0/1 or continuous accordingly
58
+ return None
59
+
60
+ def convert_age(x: str):
61
+ val = x.split(':', 1)[-1].strip() if ':' in x else x.strip()
62
+ # If it were available, parse as integer or float
63
+ return None
64
+
65
+ def convert_gender(x: str):
66
+ val = x.split(':', 1)[-1].strip() if ':' in x else x.strip()
67
+ # If it were available, convert female -> 0, male -> 1
68
+ return None
69
+
70
+ # 3. Save Metadata (initial filtering)
71
+ is_trait_available = (trait_row is not None)
72
+ _ = validate_and_save_cohort_info(
73
+ is_final=False,
74
+ cohort=cohort,
75
+ info_path=json_path,
76
+ is_gene_available=is_gene_available,
77
+ is_trait_available=is_trait_available
78
+ )
79
+
80
+ # 4. Clinical Feature Extraction
81
+ # Since trait_row is None, we skip this step
82
+ # STEP3
83
+ # 1. Use the get_genetic_data function from the library to get the gene_data from the matrix_file previously defined.
84
+ gene_data = get_genetic_data(matrix_file)
85
+
86
+ # 2. Print the first 20 row IDs (gene or probe identifiers) for future observation.
87
+ print(gene_data.index[:20])
88
+ # These identifiers look like Affymetrix probe set IDs rather than human gene symbols.
89
+ # Therefore, they need to be mapped to gene symbols.
90
+
91
+ print("requires_gene_mapping = True")
92
+ # STEP5
93
+ # 1. Use the 'get_gene_annotation' function from the library to get gene annotation data from the SOFT file.
94
+ gene_annotation = get_gene_annotation(soft_file)
95
+
96
+ # 2. Use the 'preview_df' function from the library to preview the data and print out the results.
97
+ print("Gene annotation preview:")
98
+ print(preview_df(gene_annotation))
99
+ # STEP7: Gene Identifier Mapping
100
+
101
+ # 1) Identify the annotation columns that match the probe IDs in the gene expression data ("ID")
102
+ # and the gene symbol column in the annotation data ("Gene Symbol").
103
+
104
+ # 2) Create a gene mapping dataframe by extracting these two columns from the annotation dataframe.
105
+ mapping_df = get_gene_mapping(gene_annotation, prob_col="ID", gene_col="Gene Symbol")
106
+
107
+ # 3) Apply probe-to-gene mapping on the expression data to get gene-level expression.
108
+ gene_data = apply_gene_mapping(gene_data, mapping_df)
109
+ # STEP 5
110
+ # 1) Normalize the gene expression data
111
+ normalized_gene_data = normalize_gene_symbols_in_index(gene_data)
112
+ normalized_gene_data.to_csv(out_gene_data_file)
113
+
114
+ # Since there is no trait data (trait_row was None), we cannot link clinical features or perform bias checks.
115
+ # We still must do final validation to record that trait data is unavailable.
116
+
117
+ # Provide a placeholder for is_biased; it won't matter because is_trait_available is False.
118
+ is_biased_placeholder = False
119
+
120
+ # 5) Perform final validation and save metadata
121
+ is_usable = validate_and_save_cohort_info(
122
+ is_final=True,
123
+ cohort=cohort,
124
+ info_path=json_path,
125
+ is_gene_available=True, # We do have gene expression data
126
+ is_trait_available=False, # Trait data is unavailable
127
+ is_biased=is_biased_placeholder,
128
+ df=normalized_gene_data, # We pass the gene data but there's no trait column
129
+ note="No trait data; cannot complete linking or bias checks."
130
+ )
131
+
132
+ # 6) If the dataset is deemed usable, save the final linked data
133
+ # In this scenario, is_usable will be False because the trait is not available.
134
+ if is_usable:
135
+ normalized_gene_data.to_csv(out_data_file)
p1/preprocess/Glioblastoma/code/TCGA.py ADDED
@@ -0,0 +1,119 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Path Configuration
2
+ from tools.preprocess import *
3
+
4
+ # Processing context
5
+ trait = "Glioblastoma"
6
+
7
+ # Input paths
8
+ tcga_root_dir = "../DATA/TCGA"
9
+
10
+ # Output paths
11
+ out_data_file = "./output/preprocess/1/Glioblastoma/TCGA.csv"
12
+ out_gene_data_file = "./output/preprocess/1/Glioblastoma/gene_data/TCGA.csv"
13
+ out_clinical_data_file = "./output/preprocess/1/Glioblastoma/clinical_data/TCGA.csv"
14
+ json_path = "./output/preprocess/1/Glioblastoma/cohort_info.json"
15
+
16
+ import os
17
+ import pandas as pd
18
+
19
+ # 1. Identify the relevant subdirectory for "Glioblastoma" (GBM)
20
+ subdirectories = [
21
+ 'CrawlData.ipynb', '.DS_Store', 'TCGA_lower_grade_glioma_and_glioblastoma_(GBMLGG)',
22
+ 'TCGA_Uterine_Carcinosarcoma_(UCS)', 'TCGA_Thyroid_Cancer_(THCA)', 'TCGA_Thymoma_(THYM)',
23
+ 'TCGA_Testicular_Cancer_(TGCT)', 'TCGA_Stomach_Cancer_(STAD)', 'TCGA_Sarcoma_(SARC)',
24
+ 'TCGA_Rectal_Cancer_(READ)', 'TCGA_Prostate_Cancer_(PRAD)', 'TCGA_Pheochromocytoma_Paraganglioma_(PCPG)',
25
+ 'TCGA_Pancreatic_Cancer_(PAAD)', 'TCGA_Ovarian_Cancer_(OV)', 'TCGA_Ocular_melanomas_(UVM)',
26
+ 'TCGA_Mesothelioma_(MESO)', 'TCGA_Melanoma_(SKCM)', 'TCGA_Lung_Squamous_Cell_Carcinoma_(LUSC)',
27
+ 'TCGA_Lung_Cancer_(LUNG)', 'TCGA_Lung_Adenocarcinoma_(LUAD)', 'TCGA_Lower_Grade_Glioma_(LGG)',
28
+ 'TCGA_Liver_Cancer_(LIHC)', 'TCGA_Large_Bcell_Lymphoma_(DLBC)', 'TCGA_Kidney_Papillary_Cell_Carcinoma_(KIRP)',
29
+ 'TCGA_Kidney_Clear_Cell_Carcinoma_(KIRC)', 'TCGA_Kidney_Chromophobe_(KICH)', 'TCGA_Head_and_Neck_Cancer_(HNSC)',
30
+ 'TCGA_Glioblastoma_(GBM)', 'TCGA_Esophageal_Cancer_(ESCA)', 'TCGA_Endometrioid_Cancer_(UCEC)',
31
+ 'TCGA_Colon_and_Rectal_Cancer_(COADREAD)', 'TCGA_Colon_Cancer_(COAD)', 'TCGA_Cervical_Cancer_(CESC)',
32
+ 'TCGA_Breast_Cancer_(BRCA)', 'TCGA_Bladder_Cancer_(BLCA)', 'TCGA_Bile_Duct_Cancer_(CHOL)',
33
+ 'TCGA_Adrenocortical_Cancer_(ACC)', 'TCGA_Acute_Myeloid_Leukemia_(LAML)'
34
+ ]
35
+
36
+ trait_keyword = "Glioblastoma"
37
+ trait_abbreviation = "GBM"
38
+
39
+ target_subdir = None
40
+ for sd in subdirectories:
41
+ if trait_keyword.lower() in sd.lower() or trait_abbreviation.lower() in sd.lower():
42
+ # Pick the first matching directory, presumably the most specific one we find
43
+ target_subdir = sd
44
+ break
45
+
46
+ if target_subdir is None:
47
+ # No suitable data found for this trait; mark as completed
48
+ print("No TCGA subdirectory found for the trait. Skipping.")
49
+ else:
50
+ cohort_dir = os.path.join(tcga_root_dir, target_subdir)
51
+ # 2. Locate clinical and genetic data files
52
+ clinical_file_path, genetic_file_path = tcga_get_relevant_filepaths(cohort_dir)
53
+
54
+ # 3. Load the data
55
+ clinical_df = pd.read_csv(clinical_file_path, index_col=0, sep='\t')
56
+ genetic_df = pd.read_csv(genetic_file_path, index_col=0, sep='\t')
57
+
58
+ # 4. Print column names of clinical data
59
+ print(clinical_df.columns)
60
+ # Step 1: Identify candidate columns for age and gender
61
+ candidate_age_cols = []
62
+ candidate_gender_cols = []
63
+
64
+ # Print the results
65
+ print("candidate_age_cols =", candidate_age_cols)
66
+ print("candidate_gender_cols =", candidate_gender_cols)
67
+
68
+ # Step 2: If there were any candidate columns, preview the data
69
+ if candidate_age_cols or candidate_gender_cols:
70
+ # Assuming 'clinical_df' holds the clinical data
71
+ preview_columns = candidate_age_cols + candidate_gender_cols
72
+ preview_data = clinical_df[preview_columns].head(5).to_dict(orient='list')
73
+ print("Preview of candidate columns:", preview_data)
74
+ age_col = None
75
+ gender_col = None
76
+
77
+ print("Chosen age column:", age_col)
78
+ print("Chosen gender column:", gender_col)
79
+ # 1. Extract and standardize the clinical features
80
+ selected_clinical_df = tcga_select_clinical_features(
81
+ clinical_df=clinical_df,
82
+ trait=trait,
83
+ age_col=age_col,
84
+ gender_col=gender_col
85
+ )
86
+
87
+ # (Optional) Save the selected clinical data
88
+ selected_clinical_df.to_csv(out_clinical_data_file)
89
+
90
+ # 2. Normalize gene symbols in the genetic data
91
+ normalized_gene_df = normalize_gene_symbols_in_index(genetic_df)
92
+ normalized_gene_df.to_csv(out_gene_data_file)
93
+
94
+ # 3. Link the clinical and genetic data on sample IDs
95
+ linked_data = selected_clinical_df.join(normalized_gene_df.T, how="inner")
96
+
97
+ # 4. Handle missing values
98
+ cleaned_df = handle_missing_values(linked_data, trait)
99
+
100
+ # 5. Determine if the trait or demographic features are biased
101
+ is_biased, final_df = judge_and_remove_biased_features(cleaned_df, trait)
102
+
103
+ # 6. Final quality validation
104
+ is_gene_available = not normalized_gene_df.empty
105
+ is_trait_available = trait in final_df.columns
106
+ is_usable = validate_and_save_cohort_info(
107
+ is_final=True,
108
+ cohort="TCGA",
109
+ info_path=json_path,
110
+ is_gene_available=is_gene_available,
111
+ is_trait_available=is_trait_available,
112
+ is_biased=is_biased,
113
+ df=final_df,
114
+ note=""
115
+ )
116
+
117
+ # 7. If the dataset is usable, save the final dataframe
118
+ if is_usable:
119
+ final_df.to_csv(out_data_file)
p1/preprocess/Glioblastoma/gene_data/GSE134470.csv ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:ba24674f901fdcc483785dd18b8239b22bc3a75e85a2fec50cc5b0e1b6fbd16d
3
+ size 21960028
p1/preprocess/Glioblastoma/gene_data/GSE148949.csv ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:2b3b053d323f55d881c67cded230cd46ecbed8115f2109c1f264fd33992ccfe8
3
+ size 12311824
p1/preprocess/Glioblastoma/gene_data/GSE159000.csv ADDED
The diff for this file is too large to render. See raw diff
 
p1/preprocess/Glioblastoma/gene_data/GSE175700.csv ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:6f2dfe914271f10264fd743ea3f6f4c4e442f6a119d03c7a1f15a4b24456c84d
3
+ size 14207713
p1/preprocess/Glioblastoma/gene_data/GSE178236.csv ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:9cb9c85cce7814f435afbf3c3183ab56c3b9c9558796a01af7a341b9b4d74b60
3
+ size 16314430
p1/preprocess/Glioblastoma/gene_data/GSE226976.csv ADDED
The diff for this file is too large to render. See raw diff
 
p1/preprocess/Glioblastoma/gene_data/GSE249289.csv ADDED
The diff for this file is too large to render. See raw diff
 
p1/preprocess/Glioblastoma/gene_data/GSE279426.csv ADDED
The diff for this file is too large to render. See raw diff
 
p1/preprocess/Glioblastoma/gene_data/GSE39144.csv ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:ca8e547fab39366c1235367f25cf9a1dcc790d5f45a33b7ddf2e206991dc7b75
3
+ size 11323495
p1/preprocess/Glucocorticoid_Sensitivity/GSE32962.csv ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:1c39f2e7b86d5d4bcbff093218c2cd89b39592eb354acbcf2a8dd7e55ae38e53
3
+ size 11282644
p1/preprocess/Glucocorticoid_Sensitivity/GSE33649.csv ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:8ec24c22f36b155d3d763de2a907f48f4cc579f67d061faf09e9dd620245a932
3
+ size 12458722
p1/preprocess/Glucocorticoid_Sensitivity/GSE50012.csv ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:9811f8861a2232d49b53cdcd31457707be9a0332fc2358c0c87c45a1065419c9
3
+ size 12458350
p1/preprocess/Glucocorticoid_Sensitivity/GSE57795.csv ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:d131f713cb4ba47ea1d90397a46ad5d0ee96fdfa8bb1c641f3735690c1c6426e
3
+ size 12922651
p1/preprocess/Glucocorticoid_Sensitivity/GSE66705.csv ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:9f244baedf43be9c9c286cc600df1f4ce5df3f0727083c3e8c2c671dabb870e5
3
+ size 27520522
p1/preprocess/Glucocorticoid_Sensitivity/clinical_data/GSE32962.csv ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ GSM816393,GSM816394,GSM816395,GSM816396,GSM816397,GSM816398,GSM816399,GSM816400,GSM816401,GSM816402,GSM816403,GSM816404,GSM816405,GSM816406,GSM816407,GSM816408,GSM816409,GSM816410,GSM816411,GSM816412,GSM816413,GSM816414,GSM816415,GSM816416,GSM816417,GSM816418,GSM816419,GSM816420,GSM816421,GSM816422,GSM816423,GSM816424,GSM816425,GSM816426,GSM816427,GSM816428,GSM816429,GSM816430,GSM816431,GSM816432,GSM816433,GSM816434,GSM816435
2
+ 1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0
p1/preprocess/Glucocorticoid_Sensitivity/clinical_data/GSE33649.csv ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ GSM832137,GSM832138,GSM832139,GSM832140,GSM832141,GSM832142,GSM832143,GSM832144,GSM832145,GSM832146,GSM832147,GSM832148,GSM832149,GSM832150,GSM832151,GSM832152,GSM832153,GSM832154,GSM832155,GSM832156,GSM832157,GSM832158,GSM832159,GSM832160,GSM832161,GSM832162,GSM832163,GSM832164,GSM832165,GSM832166,GSM832167,GSM832168,GSM832169,GSM832170,GSM832171,GSM832172,GSM832173,GSM832174,GSM832175,GSM832176,GSM832177,GSM832178,GSM832179,GSM832180,GSM832181,GSM832182,GSM832183,GSM832184
2
+ 89.43486,89.43486,89.43486,89.43486,95.88507,95.88507,95.88507,95.88507,95.22036,95.22036,95.22036,95.22036,92.86704,92.86704,92.86704,92.86704,93.71633,93.71633,93.71633,93.71633,96.76962,96.76962,96.76962,96.76962,88.55031,88.55031,88.55031,88.55031,90.09957,90.09957,90.09957,90.09957,94.17097,94.17097,94.17097,94.17097,86.97089,86.97089,86.97089,86.97089,98.34904,98.34904,98.34904,98.34904,91.14896,91.14896,91.14896,91.14896
3
+ 44.15342,44.15342,44.15342,44.15342,24.72329,24.72329,24.72329,24.72329,32.37808,32.37808,32.37808,32.37808,20.38082,20.38082,20.38082,20.38082,21.2411,21.2411,21.2411,21.2411,22.54247,22.54247,22.54247,22.54247,26.13973,26.13973,26.13973,26.13973,21.5616,21.5616,21.5616,21.5616,21.9863,21.9863,21.9863,21.9863,26.76712,26.76712,26.76712,26.76712,23.59452,23.59452,23.59452,23.59452,23.47945,23.47945,23.47945,23.47945
4
+ 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,1.0,1.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,0.0,0.0,0.0,0.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0
p1/preprocess/Glucocorticoid_Sensitivity/clinical_data/GSE48801.csv ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ GSM1184717,GSM1184718,GSM1184719,GSM1184720,GSM1184721,GSM1184722,GSM1184723,GSM1184724,GSM1184725,GSM1184726,GSM1184727,GSM1184728,GSM1184729,GSM1184730,GSM1184731,GSM1184732,GSM1184733,GSM1184734,GSM1184735,GSM1184736,GSM1184737,GSM1184738,GSM1184739,GSM1184740,GSM1184741,GSM1184742,GSM1184743,GSM1184744,GSM1184745,GSM1184746,GSM1184747,GSM1184748,GSM1184749,GSM1184750,GSM1184751,GSM1184752,GSM1184753,GSM1184754,GSM1184755,GSM1184756,GSM1184757,GSM1184758,GSM1184759,GSM1184760,GSM1184761,GSM1184762,GSM1184763,GSM1184764,GSM1184765,GSM1184766,GSM1184767,GSM1184768,GSM1184769,GSM1184770,GSM1184771,GSM1184772,GSM1184773,GSM1184774,GSM1184775,GSM1184776,GSM1184777,GSM1184778,GSM1184779,GSM1184780,GSM1184781,GSM1184782,GSM1184783,GSM1184784,GSM1184785,GSM1184786,GSM1184787,GSM1184788,GSM1184789,GSM1184790,GSM1184791,GSM1184792,GSM1184793,GSM1184794,GSM1184795,GSM1184796,GSM1184797,GSM1184798,GSM1184799,GSM1184800,GSM1184801,GSM1184802,GSM1184803,GSM1184804,GSM1184805,GSM1184806,GSM1184807,GSM1184808,GSM1184809,GSM1184810,GSM1184811,GSM1184812,GSM1184813,GSM1184814,GSM1184815,GSM1184816,GSM1184817,GSM1184818,GSM1184819,GSM1184820,GSM1184821,GSM1184822,GSM1184823,GSM1184824,GSM1184825,GSM1184826,GSM1184827,GSM1184828,GSM1184829,GSM1184830,GSM1184831,GSM1184832,GSM1184833,GSM1184834,GSM1184835,GSM1184836,GSM1184837,GSM1184838,GSM1184839,GSM1184840,GSM1184841,GSM1184842,GSM1184843,GSM1184844,GSM1184845,GSM1184846,GSM1184847,GSM1184848,GSM1184849,GSM1184850,GSM1184851,GSM1184852,GSM1184853,GSM1184854,GSM1184855,GSM1184856,GSM1184857,GSM1184858,GSM1184859,GSM1184860,GSM1184861,GSM1184862,GSM1184863,GSM1184864,GSM1184865,GSM1184866,GSM1184867,GSM1184868,GSM1184869,GSM1184870,GSM1184871,GSM1184872,GSM1184873,GSM1184874,GSM1184875,GSM1184876,GSM1184877,GSM1184878,GSM1184879,GSM1184880,GSM1184881,GSM1184882,GSM1184883,GSM1184884,GSM1184885,GSM1184886,GSM1184887,GSM1184888,GSM1184889,GSM1184890,GSM1184891,GSM1184892,GSM1184893,GSM1184894,GSM1184895
2
+ 90.2096916857165,90.2096916857165,92.0660852718675,92.0660852718675,85.8770390662799,85.8770390662799,87.4945143923344,87.4945143923344,85.1993812425936,85.1993812425936,84.9616236229156,84.9616236229156,83.9341340611542,83.9341340611542,88.7663927292959,88.7663927292959,88.4126127755346,88.4126127755346,90.1302355511097,90.1302355511097,86.3038207243861,86.3038207243861,97.9389927348314,97.9389927348314,85.6565800452145,85.6565800452145,72.080026977723,72.080026977723,95.7902581814721,95.7902581814721,84.7169700775247,84.7169700775247,97.2440363125325,97.2440363125325,98.6965291984436,98.6965291984436,96.3897437049292,96.3897437049292,93.7864779279733,93.7864779279733,88.9409584548941,88.9409584548941,95.2180128029044,95.2180128029044,80.3262384967705,80.3262384967705,98.9664822965928,98.9664822965928,86.7141270837215,86.7141270837215,94.1342236284511,94.1342236284511,76.5646360533747,76.5646360533747,94.4880035822124,94.4880035822124,84.2040871593034,84.2040871593034,81.2524330708547,81.2524330708547,75.0377332194718,75.0377332194718,103.111196853422,103.111196853422,93.7264007046898,93.7264007046898,98.4358920138007,98.4358920138007,91.1219245341963,91.1219245341963,89.7952307882158,89.7952307882158,100.164196369324,100.164196369324,92.2726878044167,92.2726878044167,83.653786832453,83.653786832453,85.4308536742686,85.4308536742686,95.9867474842918,95.9867474842918,97.4697626834784,97.4697626834784,87.1103581762748,87.1103581762748,106.335980304372,106.335980304372,95.0323274416373,95.0323274416373,93.2741255092367,93.2741255092367,88.0517452462257,88.0517452462257,92.7703808066373,92.7703808066373,90.2966860598886,90.2966860598886,90.2966860598886,87.6826035548426,87.6826035548426,87.6826035548426,110.820589380024,110.820589380024,110.820589380024,91.2861567746556,91.2861567746556,90.9575303422268,90.9575303422268,99.844023580098,99.844023580098,92.4380615886291,92.4380615886291,90.6279285533303,90.6279285533303,90.6279285533303,95.4061019654126,95.4061019654126,95.4061019654126,102.574377860977,102.574377860977,102.574377860977,83.3617762565561,83.3617762565561,92.9375020882722,92.9375020882722,83.056592777649,83.056592777649,101.239617979237,101.239617979237,86.5108726528178,86.5108726528178,87.8682889161097,87.8682889161097,89.9631142694748,89.9631142694748,95.5967828443558,95.5967828443558,94.3102962675651,94.3102962675651,97.0235772914671,97.0235772914671,94.6674807225407,94.6674807225407,86.0926581231368,86.0926581231368,107.862883138275,107.862883138275,82.7364199884225,82.7364199884225,88.2331356352063,88.2331356352063,93.614568433444,93.614568433444,103.717527073529,103.717527073529,103.717527073529,91.4503081788735,91.4503081788735,91.4503081788735,89.4567969526773,89.4567969526773,89.4567969526773,91.9430860155202,91.9430860155202,92.1077238348243,92.1077238348243,100.50157318001,100.50157318001,77.6379974012028,77.6379974012028,99.246829525294,99.246829525294,93.4438194050697,93.4438194050697,80.8101665274758,80.8101665274758,93.1053855695312,93.1053855695312
p1/preprocess/Glucocorticoid_Sensitivity/clinical_data/GSE50012.csv ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ ,GSM832137,GSM832138,GSM832139,GSM832140,GSM832141,GSM832142,GSM832143,GSM832144,GSM832145,GSM832146,GSM832147,GSM832148,GSM832149,GSM832150,GSM832151,GSM832152,GSM832153,GSM832154,GSM832155,GSM832156,GSM832157,GSM832158,GSM832159,GSM832160,GSM832161,GSM832162,GSM832163,GSM832164,GSM832165,GSM832166,GSM832167,GSM832168,GSM832169,GSM832170,GSM832171,GSM832172,GSM832173,GSM832174,GSM832175,GSM832176,GSM832177,GSM832178,GSM832179,GSM832180,GSM832181,GSM832182,GSM832183,GSM832184,GSM1212354,GSM1212355,GSM1212356,GSM1212357,GSM1212358,GSM1212359,GSM1212360,GSM1212361,GSM1212362,GSM1212363,GSM1212364,GSM1212365,GSM1212366,GSM1212367,GSM1212368,GSM1212369,GSM1212370,GSM1212371,GSM1212372,GSM1212373,GSM1212374,GSM1212375,GSM1212376,GSM1212377
2
+ Glucocorticoid_Sensitivity,89.43486,89.43486,89.43486,89.43486,95.88507,95.88507,95.88507,95.88507,95.22036,95.22036,95.22036,95.22036,92.86704,92.86704,92.86704,92.86704,93.71633,93.71633,93.71633,93.71633,96.76962,96.76962,96.76962,96.76962,88.55031,88.55031,88.55031,88.55031,90.09957,90.09957,90.09957,90.09957,94.17097,94.17097,94.17097,94.17097,86.97089,86.97089,86.97089,86.97089,98.34904,98.34904,98.34904,98.34904,91.14896,91.14896,91.14896,91.14896,,,,,,,,,,,,,,,,,,,,,,,,
3
+ Age,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,44.15,44.15,24.72,24.72,32.38,32.38,20.38,20.38,21.24,21.24,22.54,22.54,26.14,26.14,21.56,21.56,21.99,21.99,26.77,26.77,23.59,23.59,23.48,23.48
4
+ Gender,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,1.0,1.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,0.0,0.0,0.0,0.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,,,,,,,,,,,,,,,,,,,,,,,,
p1/preprocess/Glucocorticoid_Sensitivity/clinical_data/GSE57795.csv ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ GSM1388640,GSM1388641,GSM1388642,GSM1388643,GSM1388644,GSM1388645,GSM1388646,GSM1388647,GSM1388648,GSM1388649,GSM1388650,GSM1388651,GSM1388652,GSM1388653,GSM1388654,GSM1388655,GSM1388656,GSM1388657,GSM1388658,GSM1388659,GSM1388660,GSM1388661,GSM1388662,GSM1388663,GSM1388664,GSM1388665,GSM1388666,GSM1388667,GSM1388668,GSM1388669,GSM1388670,GSM1388671,GSM1388672,GSM1388673,GSM1388674,GSM1388675,GSM1388676,GSM1388677,GSM1388678,GSM1388679,GSM1388680,GSM1388681,GSM1388682,GSM1388683,GSM1388684,GSM1388685,GSM1388686,GSM1388687,GSM1388688,GSM1388689,GSM1388690,GSM1388691,GSM1388692,GSM1388693,GSM1388694,GSM1388695,GSM1388696,GSM1388697
2
+ 1.0,1.0,1.0,1.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0
p1/preprocess/Glucocorticoid_Sensitivity/clinical_data/GSE66705.csv ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ GSM1629982,GSM1629983,GSM1629984,GSM1629985,GSM1629986,GSM1629987,GSM1629988,GSM1629989,GSM1629990,GSM1629991,GSM1629992,GSM1629993,GSM1629994,GSM1629995,GSM1629996,GSM1629997,GSM1629998,GSM1629999,GSM1630000,GSM1630001,GSM1630002,GSM1630003,GSM1630004,GSM1630005,GSM1630006,GSM1630007,GSM1630008,GSM1630009,GSM1630010,GSM1630011,GSM1630012,GSM1630013,GSM1630014,GSM1630015,GSM1630016,GSM1630017,GSM1630018,GSM1630019,GSM1630020,GSM1630021,GSM1630022,GSM1630023,GSM1630024,GSM1630025,GSM1630026,GSM1630027,GSM1630028,GSM1630029,GSM1630030,GSM1630031,GSM1630032,GSM1630033,GSM1630034,GSM1630035,GSM1630036,GSM1630037,GSM1630038,GSM1630039,GSM1630040,GSM1630041,GSM1630042,GSM1630043,GSM1630044,GSM1630045,GSM1630046,GSM1630047,GSM1630048,GSM1630049,GSM1630050,GSM1630051,GSM1630052,GSM1630053,GSM1630054,GSM1630055,GSM1630056,GSM1630057,GSM1630058,GSM1630059,GSM1630060,GSM1630061,GSM1630062,GSM1630063,GSM1630064,GSM1630065,GSM1630066,GSM1630067,GSM1630068,GSM1630069,GSM1630070,GSM1630071,GSM1630072,GSM1630073,GSM1630074,GSM1630075,GSM1630076,GSM1630077,GSM1630078,GSM1630079,GSM1630080,GSM1630081,GSM1630082,GSM1630083,GSM1630084,GSM1630085,GSM1630086,GSM1630087,GSM1630088,GSM1630089,GSM1630090,GSM1630091,GSM1630092,GSM1630093,GSM1630094,GSM1630095,GSM1630096,GSM1630097,GSM1630098,GSM1630099,GSM1630100,GSM1630101,GSM1630102,GSM1630103,GSM1630104,GSM1630105,GSM1630106,GSM1630107,GSM1630108,GSM1630109,GSM1630110,GSM1630111,GSM1630112,GSM1630113,GSM1630114,GSM1630115,GSM1630116,GSM1630117,GSM1630118,GSM1630119,GSM1630120,GSM1630121,GSM1630122,GSM1630123,GSM1630124,GSM1630125,GSM1630126,GSM1630127,GSM1630128,GSM1630129,GSM1630130,GSM1630131,GSM1630132,GSM1630133,GSM1630135,GSM1630137,GSM1630139,GSM1630142,GSM1630144,GSM1630146,GSM1630149,GSM1630151,GSM1630154,GSM1630156,GSM1630158,GSM1630160,GSM1630162,GSM1630163,GSM1630164,GSM1630165,GSM1630166,GSM1630167,GSM1630168
2
+ ,,,,0.0,,0.0,0.0,,,,,,1.0,,0.0,,,0.0,1.0,0.0,,,,,,,,1.0,1.0,,,0.0,,1.0,,,0.0,,1.0,,,0.0,1.0,0.0,1.0,,1.0,1.0,,,0.0,,,,,1.0,0.0,,,,,,0.0,,1.0,0.0,1.0,,,,0.0,0.0,0.0,,,1.0,,0.0,,1.0,1.0,0.0,,0.0,,1.0,1.0,1.0,0.0,,1.0,0.0,,,1.0,,,,0.0,,1.0,,0.0,0.0,,,,,1.0,,,1.0,1.0,0.0,0.0,0.0,0.0,,1.0,0.0,1.0,1.0,0.0,0.0,1.0,1.0,0.0,1.0,1.0,,1.0,1.0,0.0,0.0,1.0,1.0,1.0,,1.0,1.0,0.0,1.0,0.0,1.0,0.0,0.0,1.0,0.0,1.0,0.0,0.0,1.0,0.0,1.0,0.0,1.0,1.0,0.0,1.0,1.0,1.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,1.0,0.0
p1/preprocess/Glucocorticoid_Sensitivity/code/GSE15820.py ADDED
@@ -0,0 +1,221 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Path Configuration
2
+ from tools.preprocess import *
3
+
4
+ # Processing context
5
+ trait = "Glucocorticoid_Sensitivity"
6
+ cohort = "GSE15820"
7
+
8
+ # Input paths
9
+ in_trait_dir = "../DATA/GEO/Glucocorticoid_Sensitivity"
10
+ in_cohort_dir = "../DATA/GEO/Glucocorticoid_Sensitivity/GSE15820"
11
+
12
+ # Output paths
13
+ out_data_file = "./output/preprocess/1/Glucocorticoid_Sensitivity/GSE15820.csv"
14
+ out_gene_data_file = "./output/preprocess/1/Glucocorticoid_Sensitivity/gene_data/GSE15820.csv"
15
+ out_clinical_data_file = "./output/preprocess/1/Glucocorticoid_Sensitivity/clinical_data/GSE15820.csv"
16
+ json_path = "./output/preprocess/1/Glucocorticoid_Sensitivity/cohort_info.json"
17
+
18
+ # STEP1
19
+ from tools.preprocess import *
20
+ # 1. Identify the paths to the SOFT file and the matrix file
21
+ soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)
22
+
23
+ # 2. Read the matrix file to obtain background information and sample characteristics data
24
+ background_prefixes = ['!Series_title', '!Series_summary', '!Series_overall_design']
25
+ clinical_prefixes = ['!Sample_geo_accession', '!Sample_characteristics_ch1']
26
+ background_info, clinical_data = get_background_and_clinical_data(matrix_file, background_prefixes, clinical_prefixes)
27
+
28
+ # 3. Obtain the sample characteristics dictionary from the clinical dataframe
29
+ sample_characteristics_dict = get_unique_values_by_row(clinical_data)
30
+
31
+ # 4. Explicitly print out all the background information and the sample characteristics dictionary
32
+ print("Background Information:")
33
+ print(background_info)
34
+ print("Sample Characteristics Dictionary:")
35
+ print(sample_characteristics_dict)
36
+ # 1. Gene Expression Data Availability
37
+ # From the background info ("expression profiling on Exon 1.0 microarrays"), we see it's gene expression data.
38
+ is_gene_available = True
39
+
40
+ # 2. Variable Availability and Data Type Conversion
41
+ # Examining the sample characteristics dictionary:
42
+ # {0: [...], 1: [...], 2: [...], 3: [...], 4: [...]}
43
+ # None of the keys provide a measure of "Glucocorticoid_Sensitivity", age, or gender in a way
44
+ # that varies across samples. Therefore, all three are considered unavailable.
45
+
46
+ trait_row = None
47
+ age_row = None
48
+ gender_row = None
49
+
50
+ # The data type conversion functions must still be defined, though they won't be used if their rows are None.
51
+ def convert_trait(value: str):
52
+ # No trait data is available, so return None for all values.
53
+ return None
54
+
55
+ def convert_age(value: str):
56
+ # No age data is available, so return None for all values.
57
+ return None
58
+
59
+ def convert_gender(value: str):
60
+ # No gender data is available, so return None for all values.
61
+ return None
62
+
63
+ # 3. Save Metadata (initial filtering)
64
+ # If trait_row is None, then trait data is not available.
65
+ is_trait_available = (trait_row is not None)
66
+
67
+ validate_and_save_cohort_info(
68
+ is_final=False,
69
+ cohort=cohort,
70
+ info_path=json_path,
71
+ is_gene_available=is_gene_available,
72
+ is_trait_available=is_trait_available,
73
+ note="No trait data found; in vitro cell-line study."
74
+ )
75
+
76
+ # 4. Clinical Feature Extraction
77
+ # Since trait_row is None, we skip this step (no clinical data to extract).
78
+ # STEP3
79
+ # 1. Use the get_genetic_data function from the library to get the gene_data from the matrix_file previously defined.
80
+ gene_data = get_genetic_data(matrix_file)
81
+
82
+ # 2. Print the first 20 row IDs (gene or probe identifiers) for future observation.
83
+ print(gene_data.index[:20])
84
+ # Based on the observed identifiers ('52_36nbg_gcXX'), they do not match standard human gene symbols.
85
+ # They appear to be custom microarray probe IDs and therefore likely require mapping.
86
+ print("These identifiers appear to be probe-level IDs rather than standardized gene symbols.")
87
+ print("requires_gene_mapping = True")
88
+ # STEP5
89
+ import pandas as pd
90
+ import io
91
+
92
+ # 1. Extract the lines that do NOT start with '^', '!', or '#', but do NOT parse them into a DataFrame yet.
93
+ annotation_text, _ = filter_content_by_prefix(
94
+ source=soft_file,
95
+ prefixes_a=['^', '!', '#'],
96
+ unselect=True,
97
+ source_type='file',
98
+ return_df_a=False,
99
+ return_df_b=False
100
+ )
101
+
102
+ # 2. Manually parse the filtered text into a DataFrame, specifying engine="python" to avoid buffer overflow issues.
103
+ gene_annotation = pd.read_csv(
104
+ io.StringIO(annotation_text),
105
+ delimiter='\t',
106
+ on_bad_lines='skip',
107
+ engine='python'
108
+ )
109
+
110
+ print("Gene annotation preview:")
111
+ print(preview_df(gene_annotation))
112
+ # STEP: Gene Identifier Mapping
113
+
114
+ # The reviewer suggests that the annotation column used for probe identifiers should match
115
+ # the row labels in the expression data (e.g., "52_36nbg_gc10"). However, from the annotation
116
+ # preview, none of the columns directly contain the "52_36nbg_gcXX" pattern. The "ID" column
117
+ # has entries like "52_36nENST...", while "probes" and "indices" have numeric references that
118
+ # do not match the expression data index. Therefore, there is no direct column to match
119
+ # "52_36nbg_gcXX". Proceeding with "ID" vs. "symbol" will likely yield an empty or very small
120
+ # mapping, since the annotation file does not appear to align with the expression data.
121
+
122
+ # 1. Decide which columns in the annotation correspond to the expression data "ID" and the gene symbol.
123
+ # We'll use "ID" for the probe/row identifiers and "symbol" for the gene symbols, aware that
124
+ # these do not actually match the expression data index.
125
+
126
+ mapping_df = get_gene_mapping(annotation=gene_annotation, prob_col="ID", gene_col="symbol")
127
+
128
+ # 2. Convert probe-level data to gene-level data by applying the gene mapping.
129
+ # This will likely produce empty or near-empty results due to the mismatch.
130
+ gene_data = apply_gene_mapping(expression_df=gene_data, mapping_df=mapping_df)
131
+ import os
132
+ import pandas as pd
133
+
134
+ # STEP7
135
+
136
+ # 1) Normalize gene symbols and save
137
+ normalized_gene_data = normalize_gene_symbols_in_index(gene_data)
138
+ normalized_gene_data.to_csv(out_gene_data_file)
139
+
140
+ # 2) Try reading the clinical CSV file (trait data). If it's empty or unreadable, treat trait as unavailable.
141
+ if os.path.exists(out_clinical_data_file):
142
+ try:
143
+ tmp_df = pd.read_csv(out_clinical_data_file, header=0)
144
+ row_count = tmp_df.shape[0]
145
+ # Adjust index names based on the row count
146
+ if row_count == 1:
147
+ tmp_df.index = [trait]
148
+ note_msg = "Only trait row found; no age or gender."
149
+ elif row_count == 2:
150
+ tmp_df.index = [trait, "Gender"]
151
+ note_msg = "Trait and gender rows found; no age row."
152
+ elif row_count == 3:
153
+ tmp_df.index = [trait, "Age", "Gender"]
154
+ note_msg = "Trait, age, and gender rows found."
155
+ else:
156
+ # If row_count is unexpected, abort further steps
157
+ validate_and_save_cohort_info(
158
+ is_final=True,
159
+ cohort=cohort,
160
+ info_path=json_path,
161
+ is_gene_available=True,
162
+ is_trait_available=False,
163
+ is_biased=True,
164
+ df=pd.DataFrame(),
165
+ note=f"Unexpected row_count={row_count} in clinical data."
166
+ )
167
+ raise SystemExit("Unexpected row_count in clinical data file. Stopping.")
168
+
169
+ selected_clinical_df = tmp_df
170
+
171
+ # Link the clinical and gene expression data
172
+ linked_data = geo_link_clinical_genetic_data(selected_clinical_df, normalized_gene_data)
173
+
174
+ # 3) Handle missing values
175
+ final_data = handle_missing_values(linked_data, trait_col=trait)
176
+
177
+ # 4) Evaluate bias in the trait (and remove biased demographics if any)
178
+ trait_biased, final_data = judge_and_remove_biased_features(final_data, trait)
179
+
180
+ # 5) Final validation
181
+ is_usable = validate_and_save_cohort_info(
182
+ is_final=True,
183
+ cohort=cohort,
184
+ info_path=json_path,
185
+ is_gene_available=True,
186
+ is_trait_available=True,
187
+ is_biased=trait_biased,
188
+ df=final_data,
189
+ note=note_msg
190
+ )
191
+
192
+ # 6) If the dataset is usable, save
193
+ if is_usable:
194
+ final_data.to_csv(out_data_file)
195
+
196
+ except (pd.errors.EmptyDataError, ValueError):
197
+ # If file is present but empty or invalid, treat trait data as unavailable
198
+ empty_df = pd.DataFrame()
199
+ validate_and_save_cohort_info(
200
+ is_final=True,
201
+ cohort=cohort,
202
+ info_path=json_path,
203
+ is_gene_available=True,
204
+ is_trait_available=False,
205
+ is_biased=True,
206
+ df=empty_df,
207
+ note="Trait file is empty or invalid; final dataset output skipped."
208
+ )
209
+ else:
210
+ # If the clinical file does not exist at all, the trait is unavailable
211
+ empty_df = pd.DataFrame()
212
+ validate_and_save_cohort_info(
213
+ is_final=True,
214
+ cohort=cohort,
215
+ info_path=json_path,
216
+ is_gene_available=True,
217
+ is_trait_available=False,
218
+ is_biased=True,
219
+ df=empty_df,
220
+ note="No trait data file found; final dataset output skipped."
221
+ )
p1/preprocess/Glucocorticoid_Sensitivity/code/GSE32962.py ADDED
@@ -0,0 +1,227 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Path Configuration
2
+ from tools.preprocess import *
3
+
4
+ # Processing context
5
+ trait = "Glucocorticoid_Sensitivity"
6
+ cohort = "GSE32962"
7
+
8
+ # Input paths
9
+ in_trait_dir = "../DATA/GEO/Glucocorticoid_Sensitivity"
10
+ in_cohort_dir = "../DATA/GEO/Glucocorticoid_Sensitivity/GSE32962"
11
+
12
+ # Output paths
13
+ out_data_file = "./output/preprocess/1/Glucocorticoid_Sensitivity/GSE32962.csv"
14
+ out_gene_data_file = "./output/preprocess/1/Glucocorticoid_Sensitivity/gene_data/GSE32962.csv"
15
+ out_clinical_data_file = "./output/preprocess/1/Glucocorticoid_Sensitivity/clinical_data/GSE32962.csv"
16
+ json_path = "./output/preprocess/1/Glucocorticoid_Sensitivity/cohort_info.json"
17
+
18
+ # STEP1
19
+ from tools.preprocess import *
20
+ # 1. Identify the paths to the SOFT file and the matrix file
21
+ soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)
22
+
23
+ # 2. Read the matrix file to obtain background information and sample characteristics data
24
+ background_prefixes = ['!Series_title', '!Series_summary', '!Series_overall_design']
25
+ clinical_prefixes = ['!Sample_geo_accession', '!Sample_characteristics_ch1']
26
+ background_info, clinical_data = get_background_and_clinical_data(matrix_file, background_prefixes, clinical_prefixes)
27
+
28
+ # 3. Obtain the sample characteristics dictionary from the clinical dataframe
29
+ sample_characteristics_dict = get_unique_values_by_row(clinical_data)
30
+
31
+ # 4. Explicitly print out all the background information and the sample characteristics dictionary
32
+ print("Background Information:")
33
+ print(background_info)
34
+ print("Sample Characteristics Dictionary:")
35
+ print(sample_characteristics_dict)
36
+ # 1. Gene Expression Data Availability
37
+ is_gene_available = True # The dataset contains gene expression profiles.
38
+
39
+ # 2. Variable Availability and Data Type Conversion
40
+ # Based on the sample characteristics, the 'prednisolone sensitivity' matches our trait ("Glucocorticoid_Sensitivity").
41
+ # Hence, trait_row is 4. Age has constant value (<1 year), so we set age_row=None. Gender data is not provided, so gender_row=None.
42
+ trait_row = 4
43
+ age_row = None
44
+ gender_row = None
45
+
46
+ # Define conversion functions
47
+ def convert_trait(value: str):
48
+ # Typically "prednisolone sensitivity: resistant" or "prednisolone sensitivity: sensitive"
49
+ # We extract the part after the colon and convert to binary (resistant=1, sensitive=0).
50
+ part = value.split(":", 1)[-1].strip().lower()
51
+ if part == "resistant":
52
+ return 1
53
+ elif part == "sensitive":
54
+ return 0
55
+ else:
56
+ return None
57
+
58
+ # Age and Gender are unavailable
59
+ convert_age = None
60
+ convert_gender = None
61
+
62
+ # 3. Save Metadata (initial filtering)
63
+ is_trait_available = (trait_row is not None)
64
+ is_usable = validate_and_save_cohort_info(
65
+ is_final=False,
66
+ cohort=cohort,
67
+ info_path=json_path,
68
+ is_gene_available=is_gene_available,
69
+ is_trait_available=is_trait_available
70
+ )
71
+
72
+ # 4. Clinical Feature Extraction (only if we have trait data)
73
+ if trait_row is not None:
74
+ clinical_features_df = geo_select_clinical_features(
75
+ clinical_data,
76
+ trait=trait,
77
+ trait_row=trait_row,
78
+ convert_trait=convert_trait,
79
+ age_row=age_row,
80
+ convert_age=convert_age,
81
+ gender_row=gender_row,
82
+ convert_gender=convert_gender
83
+ )
84
+ previewed_clinical = preview_df(clinical_features_df, n=5)
85
+ print("Preview of clinical features:", previewed_clinical)
86
+ clinical_features_df.to_csv(out_clinical_data_file, index=False)
87
+ # STEP3
88
+ # 1. Use the get_genetic_data function from the library to get the gene_data from the matrix_file previously defined.
89
+ gene_data = get_genetic_data(matrix_file)
90
+
91
+ # 2. Print the first 20 row IDs (gene or probe identifiers) for future observation.
92
+ print(gene_data.index[:20])
93
+ # These identifiers (e.g., "1007_s_at") are Affymetrix microarray probe IDs,
94
+ # which are not standard human gene symbols and typically need to be mapped.
95
+
96
+ # Concluding our review:
97
+ print("requires_gene_mapping = True")
98
+ # STEP5
99
+ import pandas as pd
100
+ import io
101
+
102
+ # 1. Extract the lines that do NOT start with '^', '!', or '#', but do NOT parse them into a DataFrame yet.
103
+ annotation_text, _ = filter_content_by_prefix(
104
+ source=soft_file,
105
+ prefixes_a=['^', '!', '#'],
106
+ unselect=True,
107
+ source_type='file',
108
+ return_df_a=False,
109
+ return_df_b=False
110
+ )
111
+
112
+ # 2. Manually parse the filtered text into a DataFrame, specifying engine="python" to avoid buffer overflow issues.
113
+ gene_annotation = pd.read_csv(
114
+ io.StringIO(annotation_text),
115
+ delimiter='\t',
116
+ on_bad_lines='skip',
117
+ engine='python'
118
+ )
119
+
120
+ print("Gene annotation preview:")
121
+ print(preview_df(gene_annotation))
122
+ # STEP: Gene Identifier Mapping
123
+
124
+ # 1) We identify that the "ID" column in the annotation DataFrame
125
+ # matches the probe identifiers (e.g. "1007_s_at") in gene_data.
126
+ # We also identify "Gene Symbol" as the column storing the gene symbols.
127
+
128
+ # 2) Get the mapping between probe IDs and gene symbols.
129
+ mapping_df = get_gene_mapping(gene_annotation, prob_col='ID', gene_col='Gene Symbol')
130
+
131
+ # 3) Convert probe-level measurements to gene-level data.
132
+ gene_data = apply_gene_mapping(gene_data, mapping_df)
133
+
134
+ # Print shape and a quick look at the index to confirm changes.
135
+ print("Gene-level expression data shape:", gene_data.shape)
136
+ print("First 20 genes in the mapped data:", gene_data.index[:20])
137
+ import os
138
+ import pandas as pd
139
+
140
+ # STEP7
141
+
142
+ # 1) Normalize gene symbols and save
143
+ normalized_gene_data = normalize_gene_symbols_in_index(gene_data)
144
+ normalized_gene_data.to_csv(out_gene_data_file)
145
+
146
+ # 2) Try reading the clinical CSV file (trait data). If it's empty or unreadable, treat trait as unavailable.
147
+ if os.path.exists(out_clinical_data_file):
148
+ try:
149
+ tmp_df = pd.read_csv(out_clinical_data_file, header=0)
150
+ row_count = tmp_df.shape[0]
151
+ # Adjust index names based on the row count
152
+ if row_count == 1:
153
+ tmp_df.index = [trait]
154
+ note_msg = "Only trait row found; no age or gender."
155
+ elif row_count == 2:
156
+ tmp_df.index = [trait, "Gender"]
157
+ note_msg = "Trait and gender rows found; no age row."
158
+ elif row_count == 3:
159
+ tmp_df.index = [trait, "Age", "Gender"]
160
+ note_msg = "Trait, age, and gender rows found."
161
+ else:
162
+ # If row_count is unexpected, abort further steps
163
+ validate_and_save_cohort_info(
164
+ is_final=True,
165
+ cohort=cohort,
166
+ info_path=json_path,
167
+ is_gene_available=True,
168
+ is_trait_available=False,
169
+ is_biased=True,
170
+ df=pd.DataFrame(),
171
+ note=f"Unexpected row_count={row_count} in clinical data."
172
+ )
173
+ raise SystemExit("Unexpected row_count in clinical data file. Stopping.")
174
+
175
+ selected_clinical_df = tmp_df
176
+
177
+ # Link the clinical and gene expression data
178
+ linked_data = geo_link_clinical_genetic_data(selected_clinical_df, normalized_gene_data)
179
+
180
+ # 3) Handle missing values
181
+ final_data = handle_missing_values(linked_data, trait_col=trait)
182
+
183
+ # 4) Evaluate bias in the trait (and remove biased demographics if any)
184
+ trait_biased, final_data = judge_and_remove_biased_features(final_data, trait)
185
+
186
+ # 5) Final validation
187
+ is_usable = validate_and_save_cohort_info(
188
+ is_final=True,
189
+ cohort=cohort,
190
+ info_path=json_path,
191
+ is_gene_available=True,
192
+ is_trait_available=True,
193
+ is_biased=trait_biased,
194
+ df=final_data,
195
+ note=note_msg
196
+ )
197
+
198
+ # 6) If the dataset is usable, save
199
+ if is_usable:
200
+ final_data.to_csv(out_data_file)
201
+
202
+ except (pd.errors.EmptyDataError, ValueError):
203
+ # If file is present but empty or invalid, treat trait data as unavailable
204
+ empty_df = pd.DataFrame()
205
+ validate_and_save_cohort_info(
206
+ is_final=True,
207
+ cohort=cohort,
208
+ info_path=json_path,
209
+ is_gene_available=True,
210
+ is_trait_available=False,
211
+ is_biased=True,
212
+ df=empty_df,
213
+ note="Trait file is empty or invalid; final dataset output skipped."
214
+ )
215
+ else:
216
+ # If the clinical file does not exist at all, the trait is unavailable
217
+ empty_df = pd.DataFrame()
218
+ validate_and_save_cohort_info(
219
+ is_final=True,
220
+ cohort=cohort,
221
+ info_path=json_path,
222
+ is_gene_available=True,
223
+ is_trait_available=False,
224
+ is_biased=True,
225
+ df=empty_df,
226
+ note="No trait data file found; final dataset output skipped."
227
+ )
p1/preprocess/Glucocorticoid_Sensitivity/code/GSE33649.py ADDED
@@ -0,0 +1,252 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Path Configuration
2
+ from tools.preprocess import *
3
+
4
+ # Processing context
5
+ trait = "Glucocorticoid_Sensitivity"
6
+ cohort = "GSE33649"
7
+
8
+ # Input paths
9
+ in_trait_dir = "../DATA/GEO/Glucocorticoid_Sensitivity"
10
+ in_cohort_dir = "../DATA/GEO/Glucocorticoid_Sensitivity/GSE33649"
11
+
12
+ # Output paths
13
+ out_data_file = "./output/preprocess/1/Glucocorticoid_Sensitivity/GSE33649.csv"
14
+ out_gene_data_file = "./output/preprocess/1/Glucocorticoid_Sensitivity/gene_data/GSE33649.csv"
15
+ out_clinical_data_file = "./output/preprocess/1/Glucocorticoid_Sensitivity/clinical_data/GSE33649.csv"
16
+ json_path = "./output/preprocess/1/Glucocorticoid_Sensitivity/cohort_info.json"
17
+
18
+ # STEP1
19
+ from tools.preprocess import *
20
+ # 1. Identify the paths to the SOFT file and the matrix file
21
+ soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)
22
+
23
+ # 2. Read the matrix file to obtain background information and sample characteristics data
24
+ background_prefixes = ['!Series_title', '!Series_summary', '!Series_overall_design']
25
+ clinical_prefixes = ['!Sample_geo_accession', '!Sample_characteristics_ch1']
26
+ background_info, clinical_data = get_background_and_clinical_data(matrix_file, background_prefixes, clinical_prefixes)
27
+
28
+ # 3. Obtain the sample characteristics dictionary from the clinical dataframe
29
+ sample_characteristics_dict = get_unique_values_by_row(clinical_data)
30
+
31
+ # 4. Explicitly print out all the background information and the sample characteristics dictionary
32
+ print("Background Information:")
33
+ print(background_info)
34
+ print("Sample Characteristics Dictionary:")
35
+ print(sample_characteristics_dict)
36
+ # 1) Determine whether gene expression data is available
37
+ is_gene_available = True # Based on the series description, it measures transcriptome-wide response
38
+
39
+ # 2) Identify the rows in the sample characteristics dictionary for each variable
40
+ trait_row = 3
41
+ age_row = 6
42
+ gender_row = 5
43
+
44
+ # 2) Define data conversion functions
45
+ def _get_value_after_colon(value: str):
46
+ """Extract the portion of the string after the first colon, stripping extra whitespace."""
47
+ parts = value.split(":", 1)
48
+ if len(parts) < 2:
49
+ return None
50
+ return parts[1].strip()
51
+
52
+ def convert_trait(value: str):
53
+ """Convert the trait (Glucocorticoid_Sensitivity) to a float."""
54
+ val = _get_value_after_colon(value)
55
+ if val is None:
56
+ return None
57
+ try:
58
+ return float(val)
59
+ except ValueError:
60
+ return None
61
+
62
+ def convert_age(value: str):
63
+ """Convert the age to a float."""
64
+ val = _get_value_after_colon(value)
65
+ if val is None:
66
+ return None
67
+ try:
68
+ return float(val)
69
+ except ValueError:
70
+ return None
71
+
72
+ def convert_gender(value: str):
73
+ """
74
+ Convert gender strings to binary values:
75
+ - female -> 0
76
+ - male -> 1
77
+ """
78
+ val = _get_value_after_colon(value)
79
+ if val is None:
80
+ return None
81
+ val_lower = val.lower()
82
+ if 'female' in val_lower:
83
+ return 0
84
+ elif 'male' in val_lower:
85
+ return 1
86
+ return None
87
+
88
+ # 3) Conduct initial filtering and save metadata
89
+ is_trait_available = (trait_row is not None)
90
+ is_usable = validate_and_save_cohort_info(
91
+ is_final=False,
92
+ cohort=cohort,
93
+ info_path=json_path,
94
+ is_gene_available=is_gene_available,
95
+ is_trait_available=is_trait_available
96
+ )
97
+
98
+ # 4) If trait data is available, extract clinical features and save
99
+ if trait_row is not None:
100
+ clinical_selected_df = geo_select_clinical_features(
101
+ clinical_data, # DataFrame from previous steps
102
+ trait=trait,
103
+ trait_row=trait_row,
104
+ convert_trait=convert_trait,
105
+ age_row=age_row,
106
+ convert_age=convert_age,
107
+ gender_row=gender_row,
108
+ convert_gender=convert_gender
109
+ )
110
+
111
+ # Preview the selected clinical features
112
+ preview_info = preview_df(clinical_selected_df)
113
+ print("Preview of extracted clinical features:", preview_info)
114
+
115
+ # Save the clinical data as CSV
116
+ clinical_selected_df.to_csv(out_clinical_data_file, index=False)
117
+ # STEP3
118
+ # 1. Use the get_genetic_data function from the library to get the gene_data from the matrix_file previously defined.
119
+ gene_data = get_genetic_data(matrix_file)
120
+
121
+ # 2. Print the first 20 row IDs (gene or probe identifiers) for future observation.
122
+ print(gene_data.index[:20])
123
+ # These identifiers are Illumina probe IDs, not human gene symbols
124
+ print("These gene identifiers are Illumina probe IDs and require mapping to human gene symbols.")
125
+ print("requires_gene_mapping = True")
126
+ # STEP5
127
+ import pandas as pd
128
+ import io
129
+
130
+ # 1. Extract the lines that do NOT start with '^', '!', or '#', but do NOT parse them into a DataFrame yet.
131
+ annotation_text, _ = filter_content_by_prefix(
132
+ source=soft_file,
133
+ prefixes_a=['^', '!', '#'],
134
+ unselect=True,
135
+ source_type='file',
136
+ return_df_a=False,
137
+ return_df_b=False
138
+ )
139
+
140
+ # 2. Manually parse the filtered text into a DataFrame, specifying engine="python" to avoid buffer overflow issues.
141
+ gene_annotation = pd.read_csv(
142
+ io.StringIO(annotation_text),
143
+ delimiter='\t',
144
+ on_bad_lines='skip',
145
+ engine='python'
146
+ )
147
+
148
+ print("Gene annotation preview:")
149
+ print(preview_df(gene_annotation))
150
+ # 1. Decide which columns correspond to the gene expression ID and the gene symbol
151
+ mapping_df = get_gene_mapping(gene_annotation, prob_col="ID", gene_col="Symbol")
152
+
153
+ # 2. Get a mapping between probe IDs and gene symbols
154
+ # (This is already done by the get_gene_mapping function above.)
155
+
156
+ # 3. Convert probe-level expression data to gene-level data
157
+ gene_data = apply_gene_mapping(gene_data, mapping_df)
158
+
159
+ # For verification, preview the mapped gene_data
160
+ print("Preview of gene expression data after mapping:")
161
+ print(preview_df(gene_data))
162
+ import os
163
+ import pandas as pd
164
+
165
+ # STEP7
166
+
167
+ # 1) Normalize gene symbols and save
168
+ normalized_gene_data = normalize_gene_symbols_in_index(gene_data)
169
+ normalized_gene_data.to_csv(out_gene_data_file)
170
+
171
+ # 2) Try reading the clinical CSV file (trait data). If it's empty or unreadable, treat trait as unavailable.
172
+ if os.path.exists(out_clinical_data_file):
173
+ try:
174
+ tmp_df = pd.read_csv(out_clinical_data_file, header=0)
175
+ row_count = tmp_df.shape[0]
176
+ # Adjust index names based on the row count
177
+ if row_count == 1:
178
+ tmp_df.index = [trait]
179
+ note_msg = "Only trait row found; no age or gender."
180
+ elif row_count == 2:
181
+ tmp_df.index = [trait, "Gender"]
182
+ note_msg = "Trait and gender rows found; no age row."
183
+ elif row_count == 3:
184
+ tmp_df.index = [trait, "Age", "Gender"]
185
+ note_msg = "Trait, age, and gender rows found."
186
+ else:
187
+ # If row_count is unexpected, abort further steps
188
+ validate_and_save_cohort_info(
189
+ is_final=True,
190
+ cohort=cohort,
191
+ info_path=json_path,
192
+ is_gene_available=True,
193
+ is_trait_available=False,
194
+ is_biased=True,
195
+ df=pd.DataFrame(),
196
+ note=f"Unexpected row_count={row_count} in clinical data."
197
+ )
198
+ raise SystemExit("Unexpected row_count in clinical data file. Stopping.")
199
+
200
+ selected_clinical_df = tmp_df
201
+
202
+ # Link the clinical and gene expression data
203
+ linked_data = geo_link_clinical_genetic_data(selected_clinical_df, normalized_gene_data)
204
+
205
+ # 3) Handle missing values
206
+ final_data = handle_missing_values(linked_data, trait_col=trait)
207
+
208
+ # 4) Evaluate bias in the trait (and remove biased demographics if any)
209
+ trait_biased, final_data = judge_and_remove_biased_features(final_data, trait)
210
+
211
+ # 5) Final validation
212
+ is_usable = validate_and_save_cohort_info(
213
+ is_final=True,
214
+ cohort=cohort,
215
+ info_path=json_path,
216
+ is_gene_available=True,
217
+ is_trait_available=True,
218
+ is_biased=trait_biased,
219
+ df=final_data,
220
+ note=note_msg
221
+ )
222
+
223
+ # 6) If the dataset is usable, save
224
+ if is_usable:
225
+ final_data.to_csv(out_data_file)
226
+
227
+ except (pd.errors.EmptyDataError, ValueError):
228
+ # If file is present but empty or invalid, treat trait data as unavailable
229
+ empty_df = pd.DataFrame()
230
+ validate_and_save_cohort_info(
231
+ is_final=True,
232
+ cohort=cohort,
233
+ info_path=json_path,
234
+ is_gene_available=True,
235
+ is_trait_available=False,
236
+ is_biased=True,
237
+ df=empty_df,
238
+ note="Trait file is empty or invalid; final dataset output skipped."
239
+ )
240
+ else:
241
+ # If the clinical file does not exist at all, the trait is unavailable
242
+ empty_df = pd.DataFrame()
243
+ validate_and_save_cohort_info(
244
+ is_final=True,
245
+ cohort=cohort,
246
+ info_path=json_path,
247
+ is_gene_available=True,
248
+ is_trait_available=False,
249
+ is_biased=True,
250
+ df=empty_df,
251
+ note="No trait data file found; final dataset output skipped."
252
+ )
p1/preprocess/Glucocorticoid_Sensitivity/code/GSE42002.py ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Path Configuration
2
+ from tools.preprocess import *
3
+
4
+ # Processing context
5
+ trait = "Glucocorticoid_Sensitivity"
6
+ cohort = "GSE42002"
7
+
8
+ # Input paths
9
+ in_trait_dir = "../DATA/GEO/Glucocorticoid_Sensitivity"
10
+ in_cohort_dir = "../DATA/GEO/Glucocorticoid_Sensitivity/GSE42002"
11
+
12
+ # Output paths
13
+ out_data_file = "./output/preprocess/1/Glucocorticoid_Sensitivity/GSE42002.csv"
14
+ out_gene_data_file = "./output/preprocess/1/Glucocorticoid_Sensitivity/gene_data/GSE42002.csv"
15
+ out_clinical_data_file = "./output/preprocess/1/Glucocorticoid_Sensitivity/clinical_data/GSE42002.csv"
16
+ json_path = "./output/preprocess/1/Glucocorticoid_Sensitivity/cohort_info.json"
17
+
18
+ # STEP1
19
+ from tools.preprocess import *
20
+ # 1. Identify the paths to the SOFT file and the matrix file
21
+ soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)
22
+
23
+ # 2. Read the matrix file to obtain background information and sample characteristics data
24
+ background_prefixes = ['!Series_title', '!Series_summary', '!Series_overall_design']
25
+ clinical_prefixes = ['!Sample_geo_accession', '!Sample_characteristics_ch1']
26
+ background_info, clinical_data = get_background_and_clinical_data(matrix_file, background_prefixes, clinical_prefixes)
27
+
28
+ # 3. Obtain the sample characteristics dictionary from the clinical dataframe
29
+ sample_characteristics_dict = get_unique_values_by_row(clinical_data)
30
+
31
+ # 4. Explicitly print out all the background information and the sample characteristics dictionary
32
+ print("Background Information:")
33
+ print(background_info)
34
+ print("Sample Characteristics Dictionary:")
35
+ print(sample_characteristics_dict)
36
+ # 1. Gene Expression Data Availability
37
+ # From the background info, we see mRNA expression arrays were used, indicating gene expression data is available.
38
+ is_gene_available = True
39
+
40
+ # 2. Variable Availability and Data Type Conversion
41
+ # - Checking the sample characteristics dictionary, we only see genotype, condition (trauma/control), and tissue.
42
+ # - None of these directly map to the trait "Glucocorticoid_Sensitivity", nor do we have age or gender info.
43
+ trait_row = None # No row corresponds to "Glucocorticoid_Sensitivity"
44
+ age_row = None # No age information
45
+ gender_row = None # No gender information
46
+
47
+ # - Although data is not available for trait, age, and gender, we still define the conversion functions as requested.
48
+ # Here they will simply return None because there's no real data to process.
49
+
50
+ def convert_trait(val: str) -> Optional[float]:
51
+ # No actual trait data; for demonstration, parse after the colon if it existed, return None.
52
+ return None
53
+
54
+ def convert_age(val: str) -> Optional[float]:
55
+ # No actual age data in this dataset; return None.
56
+ return None
57
+
58
+ def convert_gender(val: str) -> Optional[int]:
59
+ # No actual gender data in this dataset; return None.
60
+ return None
61
+
62
+ # 3. Save Metadata (initial filtering)
63
+ # Trait data availability is determined by whether trait_row is None. Here it is None, so is_trait_available=False.
64
+ is_trait_available = (trait_row is not None)
65
+
66
+ is_usable = validate_and_save_cohort_info(
67
+ is_final=False,
68
+ cohort=cohort,
69
+ info_path=json_path,
70
+ is_gene_available=is_gene_available,
71
+ is_trait_available=is_trait_available
72
+ )
73
+
74
+ # 4. Clinical Feature Extraction
75
+ # Since trait_row is None, we skip extracting clinical features.
p1/preprocess/Glucocorticoid_Sensitivity/code/GSE48801.py ADDED
@@ -0,0 +1,229 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Path Configuration
2
+ from tools.preprocess import *
3
+
4
+ # Processing context
5
+ trait = "Glucocorticoid_Sensitivity"
6
+ cohort = "GSE48801"
7
+
8
+ # Input paths
9
+ in_trait_dir = "../DATA/GEO/Glucocorticoid_Sensitivity"
10
+ in_cohort_dir = "../DATA/GEO/Glucocorticoid_Sensitivity/GSE48801"
11
+
12
+ # Output paths
13
+ out_data_file = "./output/preprocess/1/Glucocorticoid_Sensitivity/GSE48801.csv"
14
+ out_gene_data_file = "./output/preprocess/1/Glucocorticoid_Sensitivity/gene_data/GSE48801.csv"
15
+ out_clinical_data_file = "./output/preprocess/1/Glucocorticoid_Sensitivity/clinical_data/GSE48801.csv"
16
+ json_path = "./output/preprocess/1/Glucocorticoid_Sensitivity/cohort_info.json"
17
+
18
+ # STEP1
19
+ from tools.preprocess import *
20
+ # 1. Identify the paths to the SOFT file and the matrix file
21
+ soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)
22
+
23
+ # 2. Read the matrix file to obtain background information and sample characteristics data
24
+ background_prefixes = ['!Series_title', '!Series_summary', '!Series_overall_design']
25
+ clinical_prefixes = ['!Sample_geo_accession', '!Sample_characteristics_ch1']
26
+ background_info, clinical_data = get_background_and_clinical_data(matrix_file, background_prefixes, clinical_prefixes)
27
+
28
+ # 3. Obtain the sample characteristics dictionary from the clinical dataframe
29
+ sample_characteristics_dict = get_unique_values_by_row(clinical_data)
30
+
31
+ # 4. Explicitly print out all the background information and the sample characteristics dictionary
32
+ print("Background Information:")
33
+ print(background_info)
34
+ print("Sample Characteristics Dictionary:")
35
+ print(sample_characteristics_dict)
36
+ # 1. Gene Expression Data Availability
37
+ is_gene_available = True # From the background info, this dataset measures transcriptome-wide response.
38
+
39
+ # 2. Variable Availability and Data Type Conversion
40
+ #
41
+ # Observing the sample characteristics dictionary:
42
+ # Key=0: treatment
43
+ # Key=1: in vitro lymphocyte gc sensitivity (measured as %inhibition by dex)
44
+ #
45
+ # There's no mention of age or gender, so those are not available.
46
+ # The "in vitro lymphocyte gc sensitivity" corresponds to our trait "Glucocorticoid_Sensitivity".
47
+ trait_row = 1
48
+ age_row = None
49
+ gender_row = None
50
+
51
+ # Trait is continuous. We'll parse the numeric value after the colon, convert to float, and return None on failure.
52
+ def convert_trait(value: str):
53
+ try:
54
+ val_str = value.split(":")[-1].strip()
55
+ return float(val_str)
56
+ except:
57
+ return None
58
+
59
+ # Age and gender are not available, so we set these converters to None.
60
+ convert_age = None
61
+ convert_gender = None
62
+
63
+ # Determine if trait data is available
64
+ is_trait_available = (trait_row is not None)
65
+
66
+ # 3. Save Metadata (initial filtering)
67
+ validate_and_save_cohort_info(
68
+ is_final=False,
69
+ cohort=cohort,
70
+ info_path=json_path,
71
+ is_gene_available=is_gene_available,
72
+ is_trait_available=is_trait_available
73
+ )
74
+
75
+ # 4. Clinical Feature Extraction (only if trait_row is not None)
76
+ if trait_row is not None:
77
+ selected_clinical_df = geo_select_clinical_features(
78
+ clinical_data,
79
+ trait=trait,
80
+ trait_row=trait_row,
81
+ convert_trait=convert_trait,
82
+ age_row=age_row,
83
+ convert_age=convert_age,
84
+ gender_row=gender_row,
85
+ convert_gender=convert_gender
86
+ )
87
+ # Preview the extracted features
88
+ preview = preview_df(selected_clinical_df)
89
+ print("Preview of clinical features:\n", preview)
90
+ # Save the extracted clinical data
91
+ selected_clinical_df.to_csv(out_clinical_data_file, index=False)
92
+ # STEP3
93
+ # 1. Use the get_genetic_data function from the library to get the gene_data from the matrix_file previously defined.
94
+ gene_data = get_genetic_data(matrix_file)
95
+
96
+ # 2. Print the first 20 row IDs (gene or probe identifiers) for future observation.
97
+ print(gene_data.index[:20])
98
+ # The given identifiers (e.g., ILMN_1343291) appear to be Illumina probe IDs, which are not human gene symbols.
99
+ # Therefore, they require mapping to gene symbols.
100
+ print("requires_gene_mapping = True")
101
+ # STEP5
102
+ import pandas as pd
103
+ import io
104
+
105
+ # 1. Extract the lines that do NOT start with '^', '!', or '#', but do NOT parse them into a DataFrame yet.
106
+ annotation_text, _ = filter_content_by_prefix(
107
+ source=soft_file,
108
+ prefixes_a=['^', '!', '#'],
109
+ unselect=True,
110
+ source_type='file',
111
+ return_df_a=False,
112
+ return_df_b=False
113
+ )
114
+
115
+ # 2. Manually parse the filtered text into a DataFrame, specifying engine="python" to avoid buffer overflow issues.
116
+ gene_annotation = pd.read_csv(
117
+ io.StringIO(annotation_text),
118
+ delimiter='\t',
119
+ on_bad_lines='skip',
120
+ engine='python'
121
+ )
122
+
123
+ print("Gene annotation preview:")
124
+ print(preview_df(gene_annotation))
125
+ # STEP6: Gene Identifier Mapping
126
+
127
+ # 1. We see from the gene annotation preview that the column "ID" matches the probe IDs in our gene_data,
128
+ # and "Symbol" corresponds to human gene symbols.
129
+
130
+ # 2. Create the gene mapping dataframe by selecting the identifier column ("ID") and the symbol column ("Symbol").
131
+ mapping_df = get_gene_mapping(gene_annotation, prob_col="ID", gene_col="Symbol")
132
+
133
+ # 3. Convert probe-level data into gene-level data by applying the mapping.
134
+ gene_data = apply_gene_mapping(gene_data, mapping_df)
135
+
136
+ # Let's preview the resulting gene expression data (first few gene symbols)
137
+ print("Gene expression data after mapping, first few rows:")
138
+ print(gene_data.head())
139
+ import os
140
+ import pandas as pd
141
+
142
+ # STEP7
143
+
144
+ # 1) Normalize gene symbols and save
145
+ normalized_gene_data = normalize_gene_symbols_in_index(gene_data)
146
+ normalized_gene_data.to_csv(out_gene_data_file)
147
+
148
+ # 2) Try reading the clinical CSV file (trait data). If it's empty or unreadable, treat trait as unavailable.
149
+ if os.path.exists(out_clinical_data_file):
150
+ try:
151
+ tmp_df = pd.read_csv(out_clinical_data_file, header=0)
152
+ row_count = tmp_df.shape[0]
153
+ # Adjust index names based on the row count
154
+ if row_count == 1:
155
+ tmp_df.index = [trait]
156
+ note_msg = "Only trait row found; no age or gender."
157
+ elif row_count == 2:
158
+ tmp_df.index = [trait, "Gender"]
159
+ note_msg = "Trait and gender rows found; no age row."
160
+ elif row_count == 3:
161
+ tmp_df.index = [trait, "Age", "Gender"]
162
+ note_msg = "Trait, age, and gender rows found."
163
+ else:
164
+ # If row_count is unexpected, abort further steps
165
+ validate_and_save_cohort_info(
166
+ is_final=True,
167
+ cohort=cohort,
168
+ info_path=json_path,
169
+ is_gene_available=True,
170
+ is_trait_available=False,
171
+ is_biased=True,
172
+ df=pd.DataFrame(),
173
+ note=f"Unexpected row_count={row_count} in clinical data."
174
+ )
175
+ raise SystemExit("Unexpected row_count in clinical data file. Stopping.")
176
+
177
+ selected_clinical_df = tmp_df
178
+
179
+ # Link the clinical and gene expression data
180
+ linked_data = geo_link_clinical_genetic_data(selected_clinical_df, normalized_gene_data)
181
+
182
+ # 3) Handle missing values
183
+ final_data = handle_missing_values(linked_data, trait_col=trait)
184
+
185
+ # 4) Evaluate bias in the trait (and remove biased demographics if any)
186
+ trait_biased, final_data = judge_and_remove_biased_features(final_data, trait)
187
+
188
+ # 5) Final validation
189
+ is_usable = validate_and_save_cohort_info(
190
+ is_final=True,
191
+ cohort=cohort,
192
+ info_path=json_path,
193
+ is_gene_available=True,
194
+ is_trait_available=True,
195
+ is_biased=trait_biased,
196
+ df=final_data,
197
+ note=note_msg
198
+ )
199
+
200
+ # 6) If the dataset is usable, save
201
+ if is_usable:
202
+ final_data.to_csv(out_data_file)
203
+
204
+ except (pd.errors.EmptyDataError, ValueError):
205
+ # If file is present but empty or invalid, treat trait data as unavailable
206
+ empty_df = pd.DataFrame()
207
+ validate_and_save_cohort_info(
208
+ is_final=True,
209
+ cohort=cohort,
210
+ info_path=json_path,
211
+ is_gene_available=True,
212
+ is_trait_available=False,
213
+ is_biased=True,
214
+ df=empty_df,
215
+ note="Trait file is empty or invalid; final dataset output skipped."
216
+ )
217
+ else:
218
+ # If the clinical file does not exist at all, the trait is unavailable
219
+ empty_df = pd.DataFrame()
220
+ validate_and_save_cohort_info(
221
+ is_final=True,
222
+ cohort=cohort,
223
+ info_path=json_path,
224
+ is_gene_available=True,
225
+ is_trait_available=False,
226
+ is_biased=True,
227
+ df=empty_df,
228
+ note="No trait data file found; final dataset output skipped."
229
+ )
p1/preprocess/Glucocorticoid_Sensitivity/code/GSE50012.py ADDED
@@ -0,0 +1,266 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Path Configuration
2
+ from tools.preprocess import *
3
+
4
+ # Processing context
5
+ trait = "Glucocorticoid_Sensitivity"
6
+ cohort = "GSE50012"
7
+
8
+ # Input paths
9
+ in_trait_dir = "../DATA/GEO/Glucocorticoid_Sensitivity"
10
+ in_cohort_dir = "../DATA/GEO/Glucocorticoid_Sensitivity/GSE50012"
11
+
12
+ # Output paths
13
+ out_data_file = "./output/preprocess/1/Glucocorticoid_Sensitivity/GSE50012.csv"
14
+ out_gene_data_file = "./output/preprocess/1/Glucocorticoid_Sensitivity/gene_data/GSE50012.csv"
15
+ out_clinical_data_file = "./output/preprocess/1/Glucocorticoid_Sensitivity/clinical_data/GSE50012.csv"
16
+ json_path = "./output/preprocess/1/Glucocorticoid_Sensitivity/cohort_info.json"
17
+
18
+ # STEP1
19
+ from tools.preprocess import *
20
+ # 1. Identify the paths to the SOFT file and the matrix file
21
+ soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)
22
+
23
+ # 2. Read the matrix file to obtain background information and sample characteristics data
24
+ background_prefixes = ['!Series_title', '!Series_summary', '!Series_overall_design']
25
+ clinical_prefixes = ['!Sample_geo_accession', '!Sample_characteristics_ch1']
26
+ background_info, clinical_data = get_background_and_clinical_data(matrix_file, background_prefixes, clinical_prefixes)
27
+
28
+ # 3. Obtain the sample characteristics dictionary from the clinical dataframe
29
+ sample_characteristics_dict = get_unique_values_by_row(clinical_data)
30
+
31
+ # 4. Explicitly print out all the background information and the sample characteristics dictionary
32
+ print("Background Information:")
33
+ print(background_info)
34
+ print("Sample Characteristics Dictionary:")
35
+ print(sample_characteristics_dict)
36
+ # 1) Gene Expression Data Availability
37
+ # Based on the textual description indicating transcriptional (gene expression) study,
38
+ # we conclude gene expression data is available.
39
+ is_gene_available = True
40
+
41
+ # 2) Variable Availability and Data Type Conversion
42
+ # We identify the following rows in the sample characteristics dictionary:
43
+ # - trait_row = 3 ("in vitro lymphocyte gc sensitivity" => continuous)
44
+ # - age_row = 5 ("age (years): ..." => continuous, also some entries "gender: ...")
45
+ # - gender_row = 5 (the same row 5 includes gender data => binary)
46
+
47
+ trait_row = 3
48
+ age_row = 5
49
+ gender_row = 5
50
+
51
+ def convert_trait(value: str):
52
+ """
53
+ Convert glucocorticoid sensitivity data to float.
54
+ Return None if it doesn't match GC-sensitivity pattern or can't be parsed.
55
+ """
56
+ val_lower = value.lower()
57
+ if "in vitro lymphocyte gc sensitivity" in val_lower:
58
+ parts = value.split(":", 1)
59
+ if len(parts) == 2:
60
+ try:
61
+ return float(parts[1].strip())
62
+ except:
63
+ return None
64
+ return None
65
+
66
+ def convert_age(value: str):
67
+ """
68
+ Convert age data to float.
69
+ Return None if it doesn't match age pattern or can't be parsed.
70
+ """
71
+ val_lower = value.lower()
72
+ if "age (years)" in val_lower:
73
+ parts = value.split(":", 1)
74
+ if len(parts) == 2:
75
+ try:
76
+ return float(parts[1].strip())
77
+ except:
78
+ return None
79
+ return None
80
+
81
+ def convert_gender(value: str):
82
+ """
83
+ Convert gender data to binary: female -> 0, male -> 1.
84
+ Return None if it doesn't match gender pattern or can't be parsed.
85
+ """
86
+ val_lower = value.lower()
87
+ if "gender:" in val_lower:
88
+ parts = value.split(":", 1)
89
+ if len(parts) == 2:
90
+ g = parts[1].strip().lower()
91
+ if g.startswith("female"):
92
+ return 0
93
+ elif g.startswith("male"):
94
+ return 1
95
+ return None
96
+
97
+ # 2.1) Check trait availability
98
+ if trait_row is not None:
99
+ is_trait_available = True
100
+ else:
101
+ is_trait_available = False
102
+
103
+ # 3) Save Metadata (initial filtering)
104
+ is_usable = validate_and_save_cohort_info(
105
+ is_final=False,
106
+ cohort=cohort,
107
+ info_path=json_path,
108
+ is_gene_available=is_gene_available,
109
+ is_trait_available=is_trait_available
110
+ )
111
+
112
+ # 4) Clinical Feature Extraction (only if trait is available)
113
+ if trait_row is not None:
114
+ clinical_features_df = geo_select_clinical_features(
115
+ clinical_df=clinical_data, # assume clinical_data was loaded in a previous step
116
+ trait=trait,
117
+ trait_row=trait_row,
118
+ convert_trait=convert_trait,
119
+ age_row=age_row,
120
+ convert_age=convert_age,
121
+ gender_row=gender_row,
122
+ convert_gender=convert_gender
123
+ )
124
+ # Preview output
125
+ preview_result = preview_df(clinical_features_df)
126
+ print("Preview of selected clinical features:\n", preview_result)
127
+
128
+ # Save extracted clinical data
129
+ clinical_features_df.to_csv(out_clinical_data_file, index=True)
130
+ # STEP3
131
+ # 1. Use the get_genetic_data function from the library to get the gene_data from the matrix_file previously defined.
132
+ gene_data = get_genetic_data(matrix_file)
133
+
134
+ # 2. Print the first 20 row IDs (gene or probe identifiers) for future observation.
135
+ print(gene_data.index[:20])
136
+ # Based on the index, these appear to be Illumina probe IDs rather than human gene symbols.
137
+ # Therefore, mapping to gene symbols is required.
138
+ requires_gene_mapping = True
139
+ # STEP5
140
+ import pandas as pd
141
+ import io
142
+
143
+ # 1. Extract the lines that do NOT start with '^', '!', or '#', but do NOT parse them into a DataFrame yet.
144
+ annotation_text, _ = filter_content_by_prefix(
145
+ source=soft_file,
146
+ prefixes_a=['^', '!', '#'],
147
+ unselect=True,
148
+ source_type='file',
149
+ return_df_a=False,
150
+ return_df_b=False
151
+ )
152
+
153
+ # 2. Manually parse the filtered text into a DataFrame, specifying engine="python" to avoid buffer overflow issues.
154
+ gene_annotation = pd.read_csv(
155
+ io.StringIO(annotation_text),
156
+ delimiter='\t',
157
+ on_bad_lines='skip',
158
+ engine='python'
159
+ )
160
+
161
+ print("Gene annotation preview:")
162
+ print(preview_df(gene_annotation))
163
+ # STEP6: Gene Identifier Mapping
164
+
165
+ # 1) Identify the columns in the gene_annotation DataFrame that match the probe IDs and gene symbols.
166
+ # From the preview, "ID" matches the ILMN_xxx probes and "Symbol" stores the gene symbols.
167
+
168
+ # 2) Obtain the gene mapping dataframe with the two relevant columns: "ID" and "Symbol".
169
+ mapping_df = get_gene_mapping(gene_annotation, prob_col="ID", gene_col="Symbol")
170
+
171
+ # 3) Convert the probe-level data in 'gene_data' to gene-level expression using the mapping dataframe.
172
+ gene_data = apply_gene_mapping(gene_data, mapping_df)
173
+
174
+ # For inspection, let's print the shape of the resulting gene-level DataFrame
175
+ print("Gene-level data shape:", gene_data.shape)
176
+ import os
177
+ import pandas as pd
178
+
179
+ # STEP7
180
+
181
+ # 1) Normalize gene symbols and save
182
+ normalized_gene_data = normalize_gene_symbols_in_index(gene_data)
183
+ normalized_gene_data.to_csv(out_gene_data_file)
184
+
185
+ # 2) Try reading the clinical CSV file (trait data). If it's empty or unreadable, treat trait as unavailable.
186
+ if os.path.exists(out_clinical_data_file):
187
+ try:
188
+ tmp_df = pd.read_csv(out_clinical_data_file, header=0)
189
+ row_count = tmp_df.shape[0]
190
+ # Adjust index names based on the row count
191
+ if row_count == 1:
192
+ tmp_df.index = [trait]
193
+ note_msg = "Only trait row found; no age or gender."
194
+ elif row_count == 2:
195
+ tmp_df.index = [trait, "Gender"]
196
+ note_msg = "Trait and gender rows found; no age row."
197
+ elif row_count == 3:
198
+ tmp_df.index = [trait, "Age", "Gender"]
199
+ note_msg = "Trait, age, and gender rows found."
200
+ else:
201
+ # If row_count is unexpected, abort further steps
202
+ validate_and_save_cohort_info(
203
+ is_final=True,
204
+ cohort=cohort,
205
+ info_path=json_path,
206
+ is_gene_available=True,
207
+ is_trait_available=False,
208
+ is_biased=True,
209
+ df=pd.DataFrame(),
210
+ note=f"Unexpected row_count={row_count} in clinical data."
211
+ )
212
+ raise SystemExit("Unexpected row_count in clinical data file. Stopping.")
213
+
214
+ selected_clinical_df = tmp_df
215
+
216
+ # Link the clinical and gene expression data
217
+ linked_data = geo_link_clinical_genetic_data(selected_clinical_df, normalized_gene_data)
218
+
219
+ # 3) Handle missing values
220
+ final_data = handle_missing_values(linked_data, trait_col=trait)
221
+
222
+ # 4) Evaluate bias in the trait (and remove biased demographics if any)
223
+ trait_biased, final_data = judge_and_remove_biased_features(final_data, trait)
224
+
225
+ # 5) Final validation
226
+ is_usable = validate_and_save_cohort_info(
227
+ is_final=True,
228
+ cohort=cohort,
229
+ info_path=json_path,
230
+ is_gene_available=True,
231
+ is_trait_available=True,
232
+ is_biased=trait_biased,
233
+ df=final_data,
234
+ note=note_msg
235
+ )
236
+
237
+ # 6) If the dataset is usable, save
238
+ if is_usable:
239
+ final_data.to_csv(out_data_file)
240
+
241
+ except (pd.errors.EmptyDataError, ValueError):
242
+ # If file is present but empty or invalid, treat trait data as unavailable
243
+ empty_df = pd.DataFrame()
244
+ validate_and_save_cohort_info(
245
+ is_final=True,
246
+ cohort=cohort,
247
+ info_path=json_path,
248
+ is_gene_available=True,
249
+ is_trait_available=False,
250
+ is_biased=True,
251
+ df=empty_df,
252
+ note="Trait file is empty or invalid; final dataset output skipped."
253
+ )
254
+ else:
255
+ # If the clinical file does not exist at all, the trait is unavailable
256
+ empty_df = pd.DataFrame()
257
+ validate_and_save_cohort_info(
258
+ is_final=True,
259
+ cohort=cohort,
260
+ info_path=json_path,
261
+ is_gene_available=True,
262
+ is_trait_available=False,
263
+ is_biased=True,
264
+ df=empty_df,
265
+ note="No trait data file found; final dataset output skipped."
266
+ )
p1/preprocess/Glucocorticoid_Sensitivity/code/GSE57795.py ADDED
@@ -0,0 +1,226 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Path Configuration
2
+ from tools.preprocess import *
3
+
4
+ # Processing context
5
+ trait = "Glucocorticoid_Sensitivity"
6
+ cohort = "GSE57795"
7
+
8
+ # Input paths
9
+ in_trait_dir = "../DATA/GEO/Glucocorticoid_Sensitivity"
10
+ in_cohort_dir = "../DATA/GEO/Glucocorticoid_Sensitivity/GSE57795"
11
+
12
+ # Output paths
13
+ out_data_file = "./output/preprocess/1/Glucocorticoid_Sensitivity/GSE57795.csv"
14
+ out_gene_data_file = "./output/preprocess/1/Glucocorticoid_Sensitivity/gene_data/GSE57795.csv"
15
+ out_clinical_data_file = "./output/preprocess/1/Glucocorticoid_Sensitivity/clinical_data/GSE57795.csv"
16
+ json_path = "./output/preprocess/1/Glucocorticoid_Sensitivity/cohort_info.json"
17
+
18
+ # STEP1
19
+ from tools.preprocess import *
20
+ # 1. Identify the paths to the SOFT file and the matrix file
21
+ soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)
22
+
23
+ # 2. Read the matrix file to obtain background information and sample characteristics data
24
+ background_prefixes = ['!Series_title', '!Series_summary', '!Series_overall_design']
25
+ clinical_prefixes = ['!Sample_geo_accession', '!Sample_characteristics_ch1']
26
+ background_info, clinical_data = get_background_and_clinical_data(matrix_file, background_prefixes, clinical_prefixes)
27
+
28
+ # 3. Obtain the sample characteristics dictionary from the clinical dataframe
29
+ sample_characteristics_dict = get_unique_values_by_row(clinical_data)
30
+
31
+ # 4. Explicitly print out all the background information and the sample characteristics dictionary
32
+ print("Background Information:")
33
+ print(background_info)
34
+ print("Sample Characteristics Dictionary:")
35
+ print(sample_characteristics_dict)
36
+ # Step 1: Determine gene expression availability
37
+ is_gene_available = True # From the background info, microarray gene expression is performed
38
+
39
+ # Step 2.1: Identify rows for trait, age, and gender
40
+ # Observing the sample characteristics dictionary under key=5 for "dexamethasone response"
41
+ trait_row = 5
42
+ # No human age or gender fields in the dictionary
43
+ age_row = None
44
+ gender_row = None
45
+
46
+ # Step 2.2: Define conversion functions
47
+
48
+ def convert_trait(value: str):
49
+ val = value.split(':')[-1].strip().lower()
50
+ if 'sensitive' in val:
51
+ return 1
52
+ elif 'resistant' in val:
53
+ return 0
54
+ return None
55
+
56
+ def convert_age(value: str):
57
+ # Not used in this dataset
58
+ return None
59
+
60
+ def convert_gender(value: str):
61
+ # Not used in this dataset
62
+ return None
63
+
64
+ # Step 3: Save metadata (initial filtering)
65
+ is_trait_available = (trait_row is not None)
66
+ validate_and_save_cohort_info(
67
+ is_final=False,
68
+ cohort=cohort,
69
+ info_path=json_path,
70
+ is_gene_available=is_gene_available,
71
+ is_trait_available=is_trait_available
72
+ )
73
+
74
+ # Step 4: Extract and save clinical features if trait information is available
75
+ if is_trait_available:
76
+ df_clinical = geo_select_clinical_features(
77
+ clinical_df=clinical_data,
78
+ trait=trait,
79
+ trait_row=trait_row,
80
+ convert_trait=convert_trait,
81
+ age_row=age_row,
82
+ convert_age=convert_age,
83
+ gender_row=gender_row,
84
+ convert_gender=convert_gender
85
+ )
86
+ print(preview_df(df_clinical))
87
+ df_clinical.to_csv(out_clinical_data_file, index=False)
88
+ # STEP3
89
+ # 1. Use the get_genetic_data function from the library to get the gene_data from the matrix_file previously defined.
90
+ gene_data = get_genetic_data(matrix_file)
91
+
92
+ # 2. Print the first 20 row IDs (gene or probe identifiers) for future observation.
93
+ print(gene_data.index[:20])
94
+ # Based on the observed gene identifiers (e.g., ILMN_1343291), these appear to be Illumina probe IDs,
95
+ # not standard human gene symbols, thus mapping is required.
96
+ print("requires_gene_mapping = True")
97
+ # STEP5
98
+ import pandas as pd
99
+ import io
100
+
101
+ # 1. Extract the lines that do NOT start with '^', '!', or '#', but do NOT parse them into a DataFrame yet.
102
+ annotation_text, _ = filter_content_by_prefix(
103
+ source=soft_file,
104
+ prefixes_a=['^', '!', '#'],
105
+ unselect=True,
106
+ source_type='file',
107
+ return_df_a=False,
108
+ return_df_b=False
109
+ )
110
+
111
+ # 2. Manually parse the filtered text into a DataFrame, specifying engine="python" to avoid buffer overflow issues.
112
+ gene_annotation = pd.read_csv(
113
+ io.StringIO(annotation_text),
114
+ delimiter='\t',
115
+ on_bad_lines='skip',
116
+ engine='python'
117
+ )
118
+
119
+ print("Gene annotation preview:")
120
+ print(preview_df(gene_annotation))
121
+ # STEP: Gene Identifier Mapping
122
+
123
+ # 1. Decide which columns in the annotation match the gene expression data index ("ID")
124
+ # and the gene symbols ("Symbol"). From the preview, "ID" corresponds to ILMN_xxx
125
+ # probe IDs, and "Symbol" stores the gene symbols.
126
+
127
+ # 2. Get the gene mapping dataframe
128
+ mapping_df = get_gene_mapping(annotation=gene_annotation, prob_col="ID", gene_col="Symbol")
129
+
130
+ # 3. Convert probe-level measurements to gene expression data
131
+ gene_data = apply_gene_mapping(expression_df=gene_data, mapping_df=mapping_df)
132
+
133
+ # Let's print some info to verify
134
+ print("Mapped gene expression data shape:", gene_data.shape)
135
+ print("First 10 gene symbols in mapped data:", gene_data.index[:10].tolist())
136
+ import os
137
+ import pandas as pd
138
+
139
+ # STEP7
140
+
141
+ # 1) Normalize gene symbols and save
142
+ normalized_gene_data = normalize_gene_symbols_in_index(gene_data)
143
+ normalized_gene_data.to_csv(out_gene_data_file)
144
+
145
+ # 2) Try reading the clinical CSV file (trait data). If it's empty or unreadable, treat trait as unavailable.
146
+ if os.path.exists(out_clinical_data_file):
147
+ try:
148
+ tmp_df = pd.read_csv(out_clinical_data_file, header=0)
149
+ row_count = tmp_df.shape[0]
150
+ # Adjust index names based on the row count
151
+ if row_count == 1:
152
+ tmp_df.index = [trait]
153
+ note_msg = "Only trait row found; no age or gender."
154
+ elif row_count == 2:
155
+ tmp_df.index = [trait, "Gender"]
156
+ note_msg = "Trait and gender rows found; no age row."
157
+ elif row_count == 3:
158
+ tmp_df.index = [trait, "Age", "Gender"]
159
+ note_msg = "Trait, age, and gender rows found."
160
+ else:
161
+ # If row_count is unexpected, abort further steps
162
+ validate_and_save_cohort_info(
163
+ is_final=True,
164
+ cohort=cohort,
165
+ info_path=json_path,
166
+ is_gene_available=True,
167
+ is_trait_available=False,
168
+ is_biased=True,
169
+ df=pd.DataFrame(),
170
+ note=f"Unexpected row_count={row_count} in clinical data."
171
+ )
172
+ raise SystemExit("Unexpected row_count in clinical data file. Stopping.")
173
+
174
+ selected_clinical_df = tmp_df
175
+
176
+ # Link the clinical and gene expression data
177
+ linked_data = geo_link_clinical_genetic_data(selected_clinical_df, normalized_gene_data)
178
+
179
+ # 3) Handle missing values
180
+ final_data = handle_missing_values(linked_data, trait_col=trait)
181
+
182
+ # 4) Evaluate bias in the trait (and remove biased demographics if any)
183
+ trait_biased, final_data = judge_and_remove_biased_features(final_data, trait)
184
+
185
+ # 5) Final validation
186
+ is_usable = validate_and_save_cohort_info(
187
+ is_final=True,
188
+ cohort=cohort,
189
+ info_path=json_path,
190
+ is_gene_available=True,
191
+ is_trait_available=True,
192
+ is_biased=trait_biased,
193
+ df=final_data,
194
+ note=note_msg
195
+ )
196
+
197
+ # 6) If the dataset is usable, save
198
+ if is_usable:
199
+ final_data.to_csv(out_data_file)
200
+
201
+ except (pd.errors.EmptyDataError, ValueError):
202
+ # If file is present but empty or invalid, treat trait data as unavailable
203
+ empty_df = pd.DataFrame()
204
+ validate_and_save_cohort_info(
205
+ is_final=True,
206
+ cohort=cohort,
207
+ info_path=json_path,
208
+ is_gene_available=True,
209
+ is_trait_available=False,
210
+ is_biased=True,
211
+ df=empty_df,
212
+ note="Trait file is empty or invalid; final dataset output skipped."
213
+ )
214
+ else:
215
+ # If the clinical file does not exist at all, the trait is unavailable
216
+ empty_df = pd.DataFrame()
217
+ validate_and_save_cohort_info(
218
+ is_final=True,
219
+ cohort=cohort,
220
+ info_path=json_path,
221
+ is_gene_available=True,
222
+ is_trait_available=False,
223
+ is_biased=True,
224
+ df=empty_df,
225
+ note="No trait data file found; final dataset output skipped."
226
+ )
p1/preprocess/Glucocorticoid_Sensitivity/code/GSE58715.py ADDED
@@ -0,0 +1,208 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Path Configuration
2
+ from tools.preprocess import *
3
+
4
+ # Processing context
5
+ trait = "Glucocorticoid_Sensitivity"
6
+ cohort = "GSE58715"
7
+
8
+ # Input paths
9
+ in_trait_dir = "../DATA/GEO/Glucocorticoid_Sensitivity"
10
+ in_cohort_dir = "../DATA/GEO/Glucocorticoid_Sensitivity/GSE58715"
11
+
12
+ # Output paths
13
+ out_data_file = "./output/preprocess/1/Glucocorticoid_Sensitivity/GSE58715.csv"
14
+ out_gene_data_file = "./output/preprocess/1/Glucocorticoid_Sensitivity/gene_data/GSE58715.csv"
15
+ out_clinical_data_file = "./output/preprocess/1/Glucocorticoid_Sensitivity/clinical_data/GSE58715.csv"
16
+ json_path = "./output/preprocess/1/Glucocorticoid_Sensitivity/cohort_info.json"
17
+
18
+ # STEP1
19
+ from tools.preprocess import *
20
+ # 1. Identify the paths to the SOFT file and the matrix file
21
+ soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)
22
+
23
+ # 2. Read the matrix file to obtain background information and sample characteristics data
24
+ background_prefixes = ['!Series_title', '!Series_summary', '!Series_overall_design']
25
+ clinical_prefixes = ['!Sample_geo_accession', '!Sample_characteristics_ch1']
26
+ background_info, clinical_data = get_background_and_clinical_data(matrix_file, background_prefixes, clinical_prefixes)
27
+
28
+ # 3. Obtain the sample characteristics dictionary from the clinical dataframe
29
+ sample_characteristics_dict = get_unique_values_by_row(clinical_data)
30
+
31
+ # 4. Explicitly print out all the background information and the sample characteristics dictionary
32
+ print("Background Information:")
33
+ print(background_info)
34
+ print("Sample Characteristics Dictionary:")
35
+ print(sample_characteristics_dict)
36
+ # 1. Gene Expression Data Availability
37
+ is_gene_available = True # Based on the study description, this is gene expression data (not miRNA or methylation).
38
+
39
+ # 2. Variable Availability and Data Type Conversion
40
+ # From the provided sample characteristics, there do not appear to be human-subject trait, age, or gender data.
41
+ trait_row = None
42
+ age_row = None
43
+ gender_row = None
44
+
45
+ # Define placeholder conversion functions that return None (since the data is not available).
46
+ def convert_trait(value: str):
47
+ return None
48
+
49
+ def convert_age(value: str):
50
+ return None
51
+
52
+ def convert_gender(value: str):
53
+ return None
54
+
55
+ # 3. Save Metadata
56
+ # Trait data availability can be determined by whether trait_row is None.
57
+ is_trait_available = (trait_row is not None)
58
+
59
+ validate_and_save_cohort_info(
60
+ is_final=False,
61
+ cohort=cohort,
62
+ info_path=json_path,
63
+ is_gene_available=is_gene_available,
64
+ is_trait_available=is_trait_available
65
+ )
66
+
67
+ # 4. Clinical Feature Extraction
68
+ # Since trait_row is None, we skip the clinical feature extraction step.
69
+ # STEP3
70
+ # 1. Use the get_genetic_data function from the library to get the gene_data from the matrix_file previously defined.
71
+ gene_data = get_genetic_data(matrix_file)
72
+
73
+ # 2. Print the first 20 row IDs (gene or probe identifiers) for future observation.
74
+ print(gene_data.index[:20])
75
+ # The gene identifiers have the "ILMN_" prefix, indicating Illumina probe IDs.
76
+ # They are not standard human gene symbols and require mapping to gene symbols.
77
+ requires_gene_mapping = True
78
+ # STEP5
79
+ import pandas as pd
80
+ import io
81
+
82
+ # 1. Extract the lines that do NOT start with '^', '!', or '#', but do NOT parse them into a DataFrame yet.
83
+ annotation_text, _ = filter_content_by_prefix(
84
+ source=soft_file,
85
+ prefixes_a=['^', '!', '#'],
86
+ unselect=True,
87
+ source_type='file',
88
+ return_df_a=False,
89
+ return_df_b=False
90
+ )
91
+
92
+ # 2. Manually parse the filtered text into a DataFrame, specifying engine="python" to avoid buffer overflow issues.
93
+ gene_annotation = pd.read_csv(
94
+ io.StringIO(annotation_text),
95
+ delimiter='\t',
96
+ on_bad_lines='skip',
97
+ engine='python'
98
+ )
99
+
100
+ print("Gene annotation preview:")
101
+ print(preview_df(gene_annotation))
102
+ # STEP: Gene Identifier Mapping
103
+
104
+ # 1. Identify the columns in the gene annotation dataframe that correspond to the probe IDs in the gene expression data
105
+ # and the gene symbol. From the preview, "ID" matches the "ILMN_*" probe identifiers, and "Symbol" houses the gene symbols.
106
+ probe_id_col = 'ID'
107
+ gene_symbol_col = 'Symbol'
108
+
109
+ # 2. Obtain a mapping dataframe by extracting these two columns
110
+ mapping_df = get_gene_mapping(annotation=gene_annotation, prob_col=probe_id_col, gene_col=gene_symbol_col)
111
+
112
+ # 3. Convert probe-level measurements to gene-level expression data
113
+ gene_data = apply_gene_mapping(expression_df=gene_data, mapping_df=mapping_df)
114
+
115
+ # For confirmation/inspection, one might optionally preview a small portion of the resulting mapped dataframe:
116
+ print("Mapped gene expression data (head):")
117
+ print(gene_data.head())
118
+ import os
119
+ import pandas as pd
120
+
121
+ # STEP7
122
+
123
+ # 1) Normalize gene symbols and save
124
+ normalized_gene_data = normalize_gene_symbols_in_index(gene_data)
125
+ normalized_gene_data.to_csv(out_gene_data_file)
126
+
127
+ # 2) Try reading the clinical CSV file (trait data). If it's empty or unreadable, treat trait as unavailable.
128
+ if os.path.exists(out_clinical_data_file):
129
+ try:
130
+ tmp_df = pd.read_csv(out_clinical_data_file, header=0)
131
+ row_count = tmp_df.shape[0]
132
+ # Adjust index names based on the row count
133
+ if row_count == 1:
134
+ tmp_df.index = [trait]
135
+ note_msg = "Only trait row found; no age or gender."
136
+ elif row_count == 2:
137
+ tmp_df.index = [trait, "Gender"]
138
+ note_msg = "Trait and gender rows found; no age row."
139
+ elif row_count == 3:
140
+ tmp_df.index = [trait, "Age", "Gender"]
141
+ note_msg = "Trait, age, and gender rows found."
142
+ else:
143
+ # If row_count is unexpected, abort further steps
144
+ validate_and_save_cohort_info(
145
+ is_final=True,
146
+ cohort=cohort,
147
+ info_path=json_path,
148
+ is_gene_available=True,
149
+ is_trait_available=False,
150
+ is_biased=True,
151
+ df=pd.DataFrame(),
152
+ note=f"Unexpected row_count={row_count} in clinical data."
153
+ )
154
+ raise SystemExit("Unexpected row_count in clinical data file. Stopping.")
155
+
156
+ selected_clinical_df = tmp_df
157
+
158
+ # Link the clinical and gene expression data
159
+ linked_data = geo_link_clinical_genetic_data(selected_clinical_df, normalized_gene_data)
160
+
161
+ # 3) Handle missing values
162
+ final_data = handle_missing_values(linked_data, trait_col=trait)
163
+
164
+ # 4) Evaluate bias in the trait (and remove biased demographics if any)
165
+ trait_biased, final_data = judge_and_remove_biased_features(final_data, trait)
166
+
167
+ # 5) Final validation
168
+ is_usable = validate_and_save_cohort_info(
169
+ is_final=True,
170
+ cohort=cohort,
171
+ info_path=json_path,
172
+ is_gene_available=True,
173
+ is_trait_available=True,
174
+ is_biased=trait_biased,
175
+ df=final_data,
176
+ note=note_msg
177
+ )
178
+
179
+ # 6) If the dataset is usable, save
180
+ if is_usable:
181
+ final_data.to_csv(out_data_file)
182
+
183
+ except (pd.errors.EmptyDataError, ValueError):
184
+ # If file is present but empty or invalid, treat trait data as unavailable
185
+ empty_df = pd.DataFrame()
186
+ validate_and_save_cohort_info(
187
+ is_final=True,
188
+ cohort=cohort,
189
+ info_path=json_path,
190
+ is_gene_available=True,
191
+ is_trait_available=False,
192
+ is_biased=True,
193
+ df=empty_df,
194
+ note="Trait file is empty or invalid; final dataset output skipped."
195
+ )
196
+ else:
197
+ # If the clinical file does not exist at all, the trait is unavailable
198
+ empty_df = pd.DataFrame()
199
+ validate_and_save_cohort_info(
200
+ is_final=True,
201
+ cohort=cohort,
202
+ info_path=json_path,
203
+ is_gene_available=True,
204
+ is_trait_available=False,
205
+ is_biased=True,
206
+ df=empty_df,
207
+ note="No trait data file found; final dataset output skipped."
208
+ )
p1/preprocess/Glucocorticoid_Sensitivity/code/GSE65645.py ADDED
@@ -0,0 +1,217 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Path Configuration
2
+ from tools.preprocess import *
3
+
4
+ # Processing context
5
+ trait = "Glucocorticoid_Sensitivity"
6
+ cohort = "GSE65645"
7
+
8
+ # Input paths
9
+ in_trait_dir = "../DATA/GEO/Glucocorticoid_Sensitivity"
10
+ in_cohort_dir = "../DATA/GEO/Glucocorticoid_Sensitivity/GSE65645"
11
+
12
+ # Output paths
13
+ out_data_file = "./output/preprocess/1/Glucocorticoid_Sensitivity/GSE65645.csv"
14
+ out_gene_data_file = "./output/preprocess/1/Glucocorticoid_Sensitivity/gene_data/GSE65645.csv"
15
+ out_clinical_data_file = "./output/preprocess/1/Glucocorticoid_Sensitivity/clinical_data/GSE65645.csv"
16
+ json_path = "./output/preprocess/1/Glucocorticoid_Sensitivity/cohort_info.json"
17
+
18
+ # STEP1
19
+ from tools.preprocess import *
20
+ # 1. Identify the paths to the SOFT file and the matrix file
21
+ soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)
22
+
23
+ # 2. Read the matrix file to obtain background information and sample characteristics data
24
+ background_prefixes = ['!Series_title', '!Series_summary', '!Series_overall_design']
25
+ clinical_prefixes = ['!Sample_geo_accession', '!Sample_characteristics_ch1']
26
+ background_info, clinical_data = get_background_and_clinical_data(matrix_file, background_prefixes, clinical_prefixes)
27
+
28
+ # 3. Obtain the sample characteristics dictionary from the clinical dataframe
29
+ sample_characteristics_dict = get_unique_values_by_row(clinical_data)
30
+
31
+ # 4. Explicitly print out all the background information and the sample characteristics dictionary
32
+ print("Background Information:")
33
+ print(background_info)
34
+ print("Sample Characteristics Dictionary:")
35
+ print(sample_characteristics_dict)
36
+ # 1. Determine if gene expression data is available
37
+ # Based on the background information (lncRNA expression profiling), we set:
38
+ is_gene_available = True
39
+
40
+ # 2. Identify rows for trait, age, and gender availability
41
+ # From the sample characteristics dictionary:
42
+ # {0: ['sample_type: bone marrow'], 1: ['translocation: TEL_AML1', 'translocation: E2A_PBX1', 'translocation: MLL']}
43
+ # We see no mention of "Glucocorticoid_Sensitivity", age, or gender, so we set them all to None.
44
+ trait_row = None
45
+ age_row = None
46
+ gender_row = None
47
+
48
+ # 2.2 Data Type Conversion Functions
49
+ def convert_trait(value: str):
50
+ # No actual data for trait, return None
51
+ return None
52
+
53
+ def convert_age(value: str):
54
+ # No actual data for age, return None
55
+ return None
56
+
57
+ def convert_gender(value: str):
58
+ # No actual data for gender, return None
59
+ return None
60
+
61
+ # 3. Conduct initial filtering and save metadata
62
+ is_trait_available = (trait_row is not None)
63
+ is_usable = validate_and_save_cohort_info(
64
+ is_final=False,
65
+ cohort=cohort,
66
+ info_path=json_path,
67
+ is_gene_available=is_gene_available,
68
+ is_trait_available=is_trait_available
69
+ )
70
+
71
+ # 4. Since 'trait_row' is None, we skip clinical feature extraction
72
+ if is_trait_available:
73
+ # Would extract clinical features if available
74
+ pass
75
+ # STEP3
76
+ # 1. Use the get_genetic_data function from the library to get the gene_data from the matrix_file previously defined.
77
+ gene_data = get_genetic_data(matrix_file)
78
+
79
+ # 2. Print the first 20 row IDs (gene or probe identifiers) for future observation.
80
+ print(gene_data.index[:20])
81
+ # Based on the displayed identifiers (e.g., A_19_P00315452, (+)E1A_r60_1, etc.),
82
+ # these do not appear to be standard human gene symbols but rather probe or custom IDs.
83
+ # Thus, they likely require mapping to gene symbols.
84
+
85
+ print("requires_gene_mapping = True")
86
+ # STEP5
87
+ import pandas as pd
88
+ import io
89
+
90
+ # 1. Extract the lines that do NOT start with '^', '!', or '#', but do NOT parse them into a DataFrame yet.
91
+ annotation_text, _ = filter_content_by_prefix(
92
+ source=soft_file,
93
+ prefixes_a=['^', '!', '#'],
94
+ unselect=True,
95
+ source_type='file',
96
+ return_df_a=False,
97
+ return_df_b=False
98
+ )
99
+
100
+ # 2. Manually parse the filtered text into a DataFrame, specifying engine="python" to avoid buffer overflow issues.
101
+ gene_annotation = pd.read_csv(
102
+ io.StringIO(annotation_text),
103
+ delimiter='\t',
104
+ on_bad_lines='skip',
105
+ engine='python'
106
+ )
107
+
108
+ print("Gene annotation preview:")
109
+ print(preview_df(gene_annotation))
110
+ # STEP: Gene Identifier Mapping
111
+
112
+ # 1. Decide which columns in the gene_annotation correspond to the probe IDs in gene_data and the gene symbols.
113
+ # From the previous previews, the 'ID' column matches probe identifiers, and 'GENE_SYMBOL' stores gene symbols.
114
+
115
+ # 2. Get a gene mapping DataFrame with two columns: 'ID' (probe IDs) and 'Gene' (gene symbols).
116
+ mapping_df = get_gene_mapping(
117
+ annotation=gene_annotation,
118
+ prob_col="ID",
119
+ gene_col="GENE_SYMBOL"
120
+ )
121
+
122
+ # 3. Convert the probe-level measurements in 'gene_data' to gene-level measurements by applying the mapping.
123
+ gene_data = apply_gene_mapping(gene_data, mapping_df)
124
+
125
+ # If desired, you can preview the resulting gene_data here
126
+ # print(gene_data.head())
127
+ import os
128
+ import pandas as pd
129
+
130
+ # STEP7
131
+
132
+ # 1) Normalize gene symbols and save
133
+ normalized_gene_data = normalize_gene_symbols_in_index(gene_data)
134
+ normalized_gene_data.to_csv(out_gene_data_file)
135
+
136
+ # 2) Try reading the clinical CSV file (trait data). If it's empty or unreadable, treat trait as unavailable.
137
+ if os.path.exists(out_clinical_data_file):
138
+ try:
139
+ tmp_df = pd.read_csv(out_clinical_data_file, header=0)
140
+ row_count = tmp_df.shape[0]
141
+ # Adjust index names based on the row count
142
+ if row_count == 1:
143
+ tmp_df.index = [trait]
144
+ note_msg = "Only trait row found; no age or gender."
145
+ elif row_count == 2:
146
+ tmp_df.index = [trait, "Gender"]
147
+ note_msg = "Trait and gender rows found; no age row."
148
+ elif row_count == 3:
149
+ tmp_df.index = [trait, "Age", "Gender"]
150
+ note_msg = "Trait, age, and gender rows found."
151
+ else:
152
+ # If row_count is unexpected, abort further steps
153
+ validate_and_save_cohort_info(
154
+ is_final=True,
155
+ cohort=cohort,
156
+ info_path=json_path,
157
+ is_gene_available=True,
158
+ is_trait_available=False,
159
+ is_biased=True,
160
+ df=pd.DataFrame(),
161
+ note=f"Unexpected row_count={row_count} in clinical data."
162
+ )
163
+ raise SystemExit("Unexpected row_count in clinical data file. Stopping.")
164
+
165
+ selected_clinical_df = tmp_df
166
+
167
+ # Link the clinical and gene expression data
168
+ linked_data = geo_link_clinical_genetic_data(selected_clinical_df, normalized_gene_data)
169
+
170
+ # 3) Handle missing values
171
+ final_data = handle_missing_values(linked_data, trait_col=trait)
172
+
173
+ # 4) Evaluate bias in the trait (and remove biased demographics if any)
174
+ trait_biased, final_data = judge_and_remove_biased_features(final_data, trait)
175
+
176
+ # 5) Final validation
177
+ is_usable = validate_and_save_cohort_info(
178
+ is_final=True,
179
+ cohort=cohort,
180
+ info_path=json_path,
181
+ is_gene_available=True,
182
+ is_trait_available=True,
183
+ is_biased=trait_biased,
184
+ df=final_data,
185
+ note=note_msg
186
+ )
187
+
188
+ # 6) If the dataset is usable, save
189
+ if is_usable:
190
+ final_data.to_csv(out_data_file)
191
+
192
+ except (pd.errors.EmptyDataError, ValueError):
193
+ # If file is present but empty or invalid, treat trait data as unavailable
194
+ empty_df = pd.DataFrame()
195
+ validate_and_save_cohort_info(
196
+ is_final=True,
197
+ cohort=cohort,
198
+ info_path=json_path,
199
+ is_gene_available=True,
200
+ is_trait_available=False,
201
+ is_biased=True,
202
+ df=empty_df,
203
+ note="Trait file is empty or invalid; final dataset output skipped."
204
+ )
205
+ else:
206
+ # If the clinical file does not exist at all, the trait is unavailable
207
+ empty_df = pd.DataFrame()
208
+ validate_and_save_cohort_info(
209
+ is_final=True,
210
+ cohort=cohort,
211
+ info_path=json_path,
212
+ is_gene_available=True,
213
+ is_trait_available=False,
214
+ is_biased=True,
215
+ df=empty_df,
216
+ note="No trait data file found; final dataset output skipped."
217
+ )
p1/preprocess/Glucocorticoid_Sensitivity/code/GSE66705.py ADDED
@@ -0,0 +1,228 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Path Configuration
2
+ from tools.preprocess import *
3
+
4
+ # Processing context
5
+ trait = "Glucocorticoid_Sensitivity"
6
+ cohort = "GSE66705"
7
+
8
+ # Input paths
9
+ in_trait_dir = "../DATA/GEO/Glucocorticoid_Sensitivity"
10
+ in_cohort_dir = "../DATA/GEO/Glucocorticoid_Sensitivity/GSE66705"
11
+
12
+ # Output paths
13
+ out_data_file = "./output/preprocess/1/Glucocorticoid_Sensitivity/GSE66705.csv"
14
+ out_gene_data_file = "./output/preprocess/1/Glucocorticoid_Sensitivity/gene_data/GSE66705.csv"
15
+ out_clinical_data_file = "./output/preprocess/1/Glucocorticoid_Sensitivity/clinical_data/GSE66705.csv"
16
+ json_path = "./output/preprocess/1/Glucocorticoid_Sensitivity/cohort_info.json"
17
+
18
+ # STEP1
19
+ from tools.preprocess import *
20
+ # 1. Identify the paths to the SOFT file and the matrix file
21
+ soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)
22
+
23
+ # 2. Read the matrix file to obtain background information and sample characteristics data
24
+ background_prefixes = ['!Series_title', '!Series_summary', '!Series_overall_design']
25
+ clinical_prefixes = ['!Sample_geo_accession', '!Sample_characteristics_ch1']
26
+ background_info, clinical_data = get_background_and_clinical_data(matrix_file, background_prefixes, clinical_prefixes)
27
+
28
+ # 3. Obtain the sample characteristics dictionary from the clinical dataframe
29
+ sample_characteristics_dict = get_unique_values_by_row(clinical_data)
30
+
31
+ # 4. Explicitly print out all the background information and the sample characteristics dictionary
32
+ print("Background Information:")
33
+ print(background_info)
34
+ print("Sample Characteristics Dictionary:")
35
+ print(sample_characteristics_dict)
36
+ # 1. Determine gene expression data availability
37
+ is_gene_available = True # Based on "Gene expression profiling" in the series design.
38
+
39
+ # 2. Identify variable availability and define data type conversion functions
40
+ trait_row = 0 # Row 0 has multiple categories ("RES","SEN","INT"), not constant, so it's available.
41
+ age_row = None # No age-related entry found, so not available.
42
+ gender_row = None # No gender-related entry found, so not available.
43
+
44
+ def convert_trait(value: str) -> Optional[int]:
45
+ """
46
+ Convert trait values to binary:
47
+ - SEN => 1
48
+ - INT or RES => 0
49
+ - #N/A or unknown => None
50
+ """
51
+ val = value.split(":", 1)[1].strip()
52
+ if val in ["#N/A", "NA", "n/a", "N/A"]:
53
+ return None
54
+ elif val == "SEN":
55
+ return 1
56
+ elif val in ["RES", "INT"]:
57
+ return 0
58
+ return None
59
+
60
+ def convert_age(value: str) -> Optional[float]:
61
+ return None # Not applicable for this dataset
62
+
63
+ def convert_gender(value: str) -> Optional[int]:
64
+ return None # Not applicable for this dataset
65
+
66
+ # 3. Save metadata with initial filtering
67
+ is_usable = validate_and_save_cohort_info(
68
+ is_final=False,
69
+ cohort=cohort,
70
+ info_path=json_path,
71
+ is_gene_available=is_gene_available,
72
+ is_trait_available=(trait_row is not None)
73
+ )
74
+
75
+ # 4. Extract clinical features if trait data is available
76
+ if trait_row is not None:
77
+ df_clinical = geo_select_clinical_features(
78
+ clinical_data,
79
+ trait=trait,
80
+ trait_row=trait_row,
81
+ convert_trait=convert_trait,
82
+ age_row=age_row,
83
+ convert_age=convert_age,
84
+ gender_row=gender_row,
85
+ convert_gender=convert_gender
86
+ )
87
+ # Preview clinical data
88
+ print(preview_df(df_clinical))
89
+ # Save extracted clinical features
90
+ df_clinical.to_csv(out_clinical_data_file, index=False)
91
+ # STEP3
92
+ # 1. Use the get_genetic_data function from the library to get the gene_data from the matrix_file previously defined.
93
+ gene_data = get_genetic_data(matrix_file)
94
+
95
+ # 2. Print the first 20 row IDs (gene or probe identifiers) for future observation.
96
+ print(gene_data.index[:20])
97
+ # Based on the probe identifiers (e.g., "1007_s_at", "1053_at"), they are not standard human gene symbols.
98
+ # They appear to be Affymetrix microarray probe IDs that require mapping to gene symbols.
99
+ print("requires_gene_mapping = True")
100
+ # STEP5
101
+ import pandas as pd
102
+ import io
103
+
104
+ # 1. Extract the lines that do NOT start with '^', '!', or '#', but do NOT parse them into a DataFrame yet.
105
+ annotation_text, _ = filter_content_by_prefix(
106
+ source=soft_file,
107
+ prefixes_a=['^', '!', '#'],
108
+ unselect=True,
109
+ source_type='file',
110
+ return_df_a=False,
111
+ return_df_b=False
112
+ )
113
+
114
+ # 2. Manually parse the filtered text into a DataFrame, specifying engine="python" to avoid buffer overflow issues.
115
+ gene_annotation = pd.read_csv(
116
+ io.StringIO(annotation_text),
117
+ delimiter='\t',
118
+ on_bad_lines='skip',
119
+ engine='python'
120
+ )
121
+
122
+ print("Gene annotation preview:")
123
+ print(preview_df(gene_annotation))
124
+ # Gene Identifier Mapping
125
+
126
+ # 1. Determine columns corresponding to probe IDs (matching gene_data.index) and gene symbols
127
+ probe_col = "ID"
128
+ gene_symbol_col = "Gene Symbol"
129
+
130
+ # 2. Generate mapping dataframe
131
+ mapping_df = get_gene_mapping(gene_annotation, probe_col, gene_symbol_col)
132
+
133
+ # 3. Convert probe-level data to gene-level data
134
+ gene_data = apply_gene_mapping(gene_data, mapping_df)
135
+
136
+ # Optional: Inspect the resulting gene expression data
137
+ print(gene_data.head(10))
138
+ import os
139
+ import pandas as pd
140
+
141
+ # STEP7
142
+
143
+ # 1) Normalize gene symbols and save
144
+ normalized_gene_data = normalize_gene_symbols_in_index(gene_data)
145
+ normalized_gene_data.to_csv(out_gene_data_file)
146
+
147
+ # 2) Try reading the clinical CSV file (trait data). If it's empty or unreadable, treat trait as unavailable.
148
+ if os.path.exists(out_clinical_data_file):
149
+ try:
150
+ tmp_df = pd.read_csv(out_clinical_data_file, header=0)
151
+ row_count = tmp_df.shape[0]
152
+ # Adjust index names based on the row count
153
+ if row_count == 1:
154
+ tmp_df.index = [trait]
155
+ note_msg = "Only trait row found; no age or gender."
156
+ elif row_count == 2:
157
+ tmp_df.index = [trait, "Gender"]
158
+ note_msg = "Trait and gender rows found; no age row."
159
+ elif row_count == 3:
160
+ tmp_df.index = [trait, "Age", "Gender"]
161
+ note_msg = "Trait, age, and gender rows found."
162
+ else:
163
+ # If row_count is unexpected, abort further steps
164
+ validate_and_save_cohort_info(
165
+ is_final=True,
166
+ cohort=cohort,
167
+ info_path=json_path,
168
+ is_gene_available=True,
169
+ is_trait_available=False,
170
+ is_biased=True,
171
+ df=pd.DataFrame(),
172
+ note=f"Unexpected row_count={row_count} in clinical data."
173
+ )
174
+ raise SystemExit("Unexpected row_count in clinical data file. Stopping.")
175
+
176
+ selected_clinical_df = tmp_df
177
+
178
+ # Link the clinical and gene expression data
179
+ linked_data = geo_link_clinical_genetic_data(selected_clinical_df, normalized_gene_data)
180
+
181
+ # 3) Handle missing values
182
+ final_data = handle_missing_values(linked_data, trait_col=trait)
183
+
184
+ # 4) Evaluate bias in the trait (and remove biased demographics if any)
185
+ trait_biased, final_data = judge_and_remove_biased_features(final_data, trait)
186
+
187
+ # 5) Final validation
188
+ is_usable = validate_and_save_cohort_info(
189
+ is_final=True,
190
+ cohort=cohort,
191
+ info_path=json_path,
192
+ is_gene_available=True,
193
+ is_trait_available=True,
194
+ is_biased=trait_biased,
195
+ df=final_data,
196
+ note=note_msg
197
+ )
198
+
199
+ # 6) If the dataset is usable, save
200
+ if is_usable:
201
+ final_data.to_csv(out_data_file)
202
+
203
+ except (pd.errors.EmptyDataError, ValueError):
204
+ # If file is present but empty or invalid, treat trait data as unavailable
205
+ empty_df = pd.DataFrame()
206
+ validate_and_save_cohort_info(
207
+ is_final=True,
208
+ cohort=cohort,
209
+ info_path=json_path,
210
+ is_gene_available=True,
211
+ is_trait_available=False,
212
+ is_biased=True,
213
+ df=empty_df,
214
+ note="Trait file is empty or invalid; final dataset output skipped."
215
+ )
216
+ else:
217
+ # If the clinical file does not exist at all, the trait is unavailable
218
+ empty_df = pd.DataFrame()
219
+ validate_and_save_cohort_info(
220
+ is_final=True,
221
+ cohort=cohort,
222
+ info_path=json_path,
223
+ is_gene_available=True,
224
+ is_trait_available=False,
225
+ is_biased=True,
226
+ df=empty_df,
227
+ note="No trait data file found; final dataset output skipped."
228
+ )
p1/preprocess/Glucocorticoid_Sensitivity/code/TCGA.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Path Configuration
2
+ from tools.preprocess import *
3
+
4
+ # Processing context
5
+ trait = "Glucocorticoid_Sensitivity"
6
+
7
+ # Input paths
8
+ tcga_root_dir = "../DATA/TCGA"
9
+
10
+ # Output paths
11
+ out_data_file = "./output/preprocess/1/Glucocorticoid_Sensitivity/TCGA.csv"
12
+ out_gene_data_file = "./output/preprocess/1/Glucocorticoid_Sensitivity/gene_data/TCGA.csv"
13
+ out_clinical_data_file = "./output/preprocess/1/Glucocorticoid_Sensitivity/clinical_data/TCGA.csv"
14
+ json_path = "./output/preprocess/1/Glucocorticoid_Sensitivity/cohort_info.json"
15
+
16
+ import os
17
+ import pandas as pd
18
+
19
+ # 1. Identify subdirectories under tcga_root_dir
20
+ subdirectories = os.listdir(tcga_root_dir)
21
+
22
+ # Update search terms to reflect "Glucocorticoid_Sensitivity"
23
+ search_terms = ["glucocorticoid", "corticoid", "adrenal"]
24
+
25
+ trait_subdir = None
26
+ for d in subdirectories:
27
+ d_lower = d.lower()
28
+ if any(term in d_lower for term in search_terms):
29
+ trait_subdir = d
30
+ break
31
+
32
+ # 2. If none found, skip this trait
33
+ if not trait_subdir:
34
+ print(f"No suitable subdirectory found for trait '{trait}'. Skipping...")
35
+ is_gene_available = False
36
+ is_trait_available = False
37
+ validate_and_save_cohort_info(
38
+ is_final=False,
39
+ cohort="TCGA",
40
+ info_path=json_path,
41
+ is_gene_available=is_gene_available,
42
+ is_trait_available=is_trait_available
43
+ )
44
+ else:
45
+ # 2. Identify file paths
46
+ cohort_path = os.path.join(tcga_root_dir, trait_subdir)
47
+ clinical_file_path, genetic_file_path = tcga_get_relevant_filepaths(cohort_path)
48
+
49
+ # 3. Load both files as dataframes
50
+ clinical_df = pd.read_csv(clinical_file_path, index_col=0, sep='\t', low_memory=False)
51
+ genetic_df = pd.read_csv(genetic_file_path, index_col=0, sep='\t', low_memory=False)
52
+
53
+ # 4. Print the column names of the clinical data
54
+ print(f"Selected subdirectory: {trait_subdir}")
55
+ print("Clinical Data Columns:")
56
+ print(clinical_df.columns.tolist())
p1/preprocess/Glucocorticoid_Sensitivity/cohort_info.json ADDED
@@ -0,0 +1 @@
 
 
1
+ {"GSE66705": {"is_usable": true, "is_gene_available": true, "is_trait_available": true, "is_available": true, "is_biased": false, "has_age": false, "has_gender": false, "sample_size": 104, "note": "Only trait row found; no age or gender."}, "GSE65645": {"is_usable": false, "is_gene_available": false, "is_trait_available": false, "is_available": false, "is_biased": null, "has_age": null, "has_gender": null, "sample_size": null, "note": "No trait data file found; final dataset output skipped."}, "GSE58715": {"is_usable": false, "is_gene_available": false, "is_trait_available": false, "is_available": false, "is_biased": null, "has_age": null, "has_gender": null, "sample_size": null, "note": "No trait data file found; final dataset output skipped."}, "GSE57795": {"is_usable": true, "is_gene_available": true, "is_trait_available": true, "is_available": true, "is_biased": false, "has_age": false, "has_gender": false, "sample_size": 58, "note": "Only trait row found; no age or gender."}, "GSE50012": {"is_usable": true, "is_gene_available": true, "is_trait_available": true, "is_available": true, "is_biased": false, "has_age": true, "has_gender": true, "sample_size": 48, "note": "Trait, age, and gender rows found."}, "GSE48801": {"is_usable": true, "is_gene_available": true, "is_trait_available": true, "is_available": true, "is_biased": false, "has_age": false, "has_gender": false, "sample_size": 179, "note": "Only trait row found; no age or gender."}, "GSE42002": {"is_usable": false, "is_gene_available": true, "is_trait_available": false, "is_available": false, "is_biased": null, "has_age": null, "has_gender": null, "sample_size": null, "note": null}, "GSE33649": {"is_usable": true, "is_gene_available": true, "is_trait_available": true, "is_available": true, "is_biased": false, "has_age": true, "has_gender": true, "sample_size": 48, "note": "Trait, age, and gender rows found."}, "GSE32962": {"is_usable": true, "is_gene_available": true, "is_trait_available": true, "is_available": true, "is_biased": false, "has_age": false, "has_gender": false, "sample_size": 43, "note": "Only trait row found; no age or gender."}, "GSE15820": {"is_usable": false, "is_gene_available": false, "is_trait_available": false, "is_available": false, "is_biased": null, "has_age": null, "has_gender": null, "sample_size": null, "note": "No trait data file found; final dataset output skipped."}, "TCGA": {"is_usable": false, "is_gene_available": false, "is_trait_available": false, "is_available": false, "is_biased": null, "has_age": null, "has_gender": null, "sample_size": null, "note": null}}
p1/preprocess/Glucocorticoid_Sensitivity/gene_data/GSE15820.csv ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:77e3f459c3dc04435872e7dff2c55971a33a189c75c7c84ae24dfa7b76e367f2
3
+ size 12945312
p1/preprocess/Glucocorticoid_Sensitivity/gene_data/GSE32962.csv ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:a9cc6e9edb3809b650bd0d8278ae3128a9c012b82c16008068108586a3575729
3
+ size 11282449
p1/preprocess/Glucocorticoid_Sensitivity/gene_data/GSE33649.csv ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:046aa568ff7fb3692737ee31d3c5abe53761761797e2d852bfe16fc75091296e
3
+ size 12457644
p1/preprocess/Glucocorticoid_Sensitivity/gene_data/GSE50012.csv ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b9e95acb316c0586d7303c4972b3039155cbfea37f219f524a946435aaefee08
3
+ size 20849284