File size: 5,283 Bytes
72233eb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# Path Configuration
from tools.preprocess import *

# Processing context
trait = "Allergies"
cohort = "GSE169149"

# Input paths
in_trait_dir = "../DATA/GEO/Allergies"
in_cohort_dir = "../DATA/GEO/Allergies/GSE169149"

# Output paths
out_data_file = "./output/z1/preprocess/Allergies/GSE169149.csv"
out_gene_data_file = "./output/z1/preprocess/Allergies/gene_data/GSE169149.csv"
out_clinical_data_file = "./output/z1/preprocess/Allergies/clinical_data/GSE169149.csv"
json_path = "./output/z1/preprocess/Allergies/cohort_info.json"


# Step 1: Initial Data Loading
from tools.preprocess import *
# 1. Identify the paths to the SOFT file and the matrix file
soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)

# 2. Read the matrix file to obtain background information and sample characteristics data
background_prefixes = ['!Series_title', '!Series_summary', '!Series_overall_design']
clinical_prefixes = ['!Sample_geo_accession', '!Sample_characteristics_ch1']
background_info, clinical_data = get_background_and_clinical_data(matrix_file, background_prefixes, clinical_prefixes)

# 3. Obtain the sample characteristics dictionary from the clinical dataframe
sample_characteristics_dict = get_unique_values_by_row(clinical_data)

# 4. Explicitly print out all the background information and the sample characteristics dictionary
print("Background Information:")
print(background_info)
print("Sample Characteristics Dictionary:")
print(sample_characteristics_dict)

# Step 2: Dataset Analysis and Clinical Feature Extraction
# 1) Determine gene expression availability
is_gene_available = True  # Blood tissue and treatment context imply gene expression profiling (not miRNA/methylation only)

# 2) Identify availability rows in the sample characteristics
# Sample Characteristics Dictionary given:
# 0: ['subject status: Sarcoidosis patient', 'subject status: healthy control']
# 1: ['treatment: none', 'treatment: tofacitinib']
# 2: ['tissue: Blood']
trait_row = None   # No Allergies-related info present
age_row = None     # No age info present
gender_row = None  # No gender info present

# 2.2) Conversion functions
def _after_colon(x):
    if x is None:
        return None
    if not isinstance(x, str):
        x = str(x)
    parts = x.split(":", 1)
    v = parts[1] if len(parts) > 1 else parts[0]
    return v.strip()

def convert_trait(x):
    # Map allergy-related status to binary: 1 = has allergies/atopy; 0 = no allergies/controls; unknown -> None
    v = _after_colon(x)
    if v is None or v == "":
        return None
    s = v.lower()

    # Strong positive indicators
    pos_terms = [
        "allergy", "allergies", "allergic", "atopy", "atopic", "asthma",
        "hay fever", "rhinitis", "eczema", "urticaria"
    ]
    if any(term in s for term in pos_terms):
        return 1

    # Strong negative indicators
    neg_terms = [
        "non-atopic", "healthy control", "control", "no allergy", "without allergies",
        "none", "negative", "neg", "absent"
    ]
    if any(term in s for term in neg_terms):
        return 0

    # Generic yes/no
    if s in {"yes", "y", "true", "1", "positive", "pos", "present"}:
        return 1
    if s in {"no", "n", "false", "0"}:
        return 0

    return None

def convert_age(x):
    v = _after_colon(x)
    if v is None or v == "":
        return None
    s = v.lower().replace("years", "").replace("year", "").replace("yrs", "").replace("yr", "").strip()
    # Remove common non-numeric placeholders
    if s in {"na", "n/a", "unknown", "none"}:
        return None
    # Extract leading numeric if present
    try:
        return float(s.split()[0].replace(",", ""))
    except Exception:
        # Try to find a number within the string
        import re
        m = re.search(r"[-+]?\d*\.?\d+", s)
        if m:
            try:
                return float(m.group(0))
            except Exception:
                return None
        return None

def convert_gender(x):
    # 0 = female, 1 = male
    v = _after_colon(x)
    if v is None or v == "":
        return None
    s = v.strip().lower()
    # Standard labels
    if s in {"female", "f", "woman", "women"}:
        return 0
    if s in {"male", "m", "man", "men"}:
        return 1
    # Handle common encodings
    if s in {"0", "1"}:
        return 1 if s == "1" else 0
    if s in {"na", "n/a", "unknown", "none"}:
        return None
    return None

# 3) Initial filtering and save metadata
is_trait_available = trait_row is not None
validate_and_save_cohort_info(
    is_final=False,
    cohort=cohort,
    info_path=json_path,
    is_gene_available=is_gene_available,
    is_trait_available=is_trait_available
)

# 4) Clinical feature extraction (skip because trait_row is None)
if trait_row is not None:
    selected_clinical_df = geo_select_clinical_features(
        clinical_df=clinical_data,
        trait=trait,
        trait_row=trait_row,
        convert_trait=convert_trait,
        age_row=age_row,
        convert_age=convert_age,
        gender_row=gender_row,
        convert_gender=convert_gender
    )
    _ = preview_df(selected_clinical_df)
    # Save selected clinical data
    os.makedirs(os.path.dirname(out_clinical_data_file), exist_ok=True)
    selected_clinical_df.to_csv(out_clinical_data_file)