File size: 5,444 Bytes
7623c74
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Path Configuration
from tools.preprocess import *

# Processing context
trait = "Liver_Cancer"
cohort = "GSE218438"

# Input paths
in_trait_dir = "../DATA/GEO/Liver_Cancer"
in_cohort_dir = "../DATA/GEO/Liver_Cancer/GSE218438"

# Output paths
out_data_file = "./output/preprocess/3/Liver_Cancer/GSE218438.csv"
out_gene_data_file = "./output/preprocess/3/Liver_Cancer/gene_data/GSE218438.csv"
out_clinical_data_file = "./output/preprocess/3/Liver_Cancer/clinical_data/GSE218438.csv"
json_path = "./output/preprocess/3/Liver_Cancer/cohort_info.json"

# Get file paths for soft and matrix files
soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)

# Get background info and clinical data from matrix file
background_info, clinical_data = get_background_and_clinical_data(matrix_file)

# Get unique values for each clinical feature row 
clinical_features = get_unique_values_by_row(clinical_data)

# Print background info
print("Background Information:")
print(background_info)
print("\nClinical Features and Sample Values:")
print(json.dumps(clinical_features, indent=2))
# 1. Gene Expression Data Availability
# This dataset is about transcriptional profiling using L1000 platform across multiple cell lines
# So it contains gene expression data
is_gene_available = True

# 2. Variable Availability and Data Type Conversion
# 2.1 Data Availability
# Looking at the sample characteristics, we can't find trait (disease status), age or gender information
# The data is from cell lines, not human patients
trait_row = None 
age_row = None
gender_row = None

# 2.2 Data Type Conversion
# Although not used since data is unavailable, defining conversion functions as required
def convert_trait(x):
    return None

def convert_age(x):
    return None

def convert_gender(x):
    return None

# 3. Save Metadata 
# Initial validation - trait data is not available
validate_and_save_cohort_info(
    is_final=False,
    cohort=cohort,
    info_path=json_path,
    is_gene_available=is_gene_available,
    is_trait_available=False  # trait_row is None
)

# 4. Clinical Feature Extraction
# Skip this step since trait_row is None
# Extract gene expression data from matrix file
genetic_data = get_genetic_data(matrix_file)

# Print DataFrame info and dimensions to verify data structure
print("DataFrame info:")
print(genetic_data.info())
print("\nDataFrame dimensions:", genetic_data.shape)

# Print an excerpt of the data to inspect row/column structure
print("\nFirst few rows and columns of data:")
print(genetic_data.head().iloc[:, :5])

# Print first 20 row IDs
print("\nFirst 20 gene/probe IDs:")
print(genetic_data.index[:20].tolist())
# These IDs appear to be Affymetrix probe IDs (e.g. "1007_s_at", "AFFX-TrpnX-M_at")
# which need to be mapped to human gene symbols for proper analysis
requires_gene_mapping = True
# Extract gene annotation data with different prefix filtering
# Platform sections in SOFT files often start with "!Platform_table_begin" until "!Platform_table_end"
gene_annotation = get_gene_annotation(soft_file, prefixes=['!Platform_table_begin', '!Platform_table_end'])

# Preview the annotation data structure to identify relevant columns
print("Gene Annotation Data Preview:") 
preview = preview_df(gene_annotation)
print(json.dumps(preview, indent=2))

# Print column names
print("\nAvailable columns:")
print(gene_annotation.columns.tolist())
# Extract gene annotation data using platform table markers
gene_annotation = get_gene_annotation(soft_file, prefixes=['!Platform_table_begin', '!Platform_table_end'])

# Preview the data structure and columns
print("Gene Annotation Data Structure:")
print("DataFrame dimensions:", gene_annotation.shape)
print("\nFirst few rows and columns:")
print(gene_annotation.head())

# Print column names to help identify probe ID and gene symbol columns
print("\nAvailable columns:")
print(gene_annotation.columns.tolist())
# Load the gene annotation data from SOFT file - specifically targeting platform annotation table
with gzip.open(soft_file, 'rt') as f:
    platform_section = False
    table_data = []
    header = None
    for line in f:
        if 'Gene_Symbol' in line or 'Gene Symbol' in line:
            # Found the header line
            header = line.strip().split('\t')
            platform_section = True
            continue
        elif platform_section and line.strip():
            if '!Platform_table_end' in line:
                platform_section = False
            else:
                table_data.append(line.strip().split('\t'))
            
# Create dataframe if we found the header            
if header:
    gene_annotation = pd.DataFrame(table_data, columns=header)
    print("Updated Gene Annotation Data:")
    print(gene_annotation.head())
    print("\nColumns:", gene_annotation.columns.tolist())

    # Extract mapping between probe IDs and gene symbols 
    mapping_data = get_gene_mapping(gene_annotation, prob_col='ID', gene_col='Gene_Symbol')
    print("\nGene Mapping Preview:")
    print(mapping_data.head())

    # Apply the mapping to convert probe level data to gene level data
    gene_data = apply_gene_mapping(genetic_data, mapping_data)

    # Preview the resulting gene expression data
    print("\nGene Expression Data:")
    print(f"Shape: {gene_data.shape}")
    print("\nFirst few rows and columns:")
    print(gene_data.head().iloc[:, :5])
else:
    print("Could not find gene symbol column in platform annotation")