Liu-Hy's picture
Add files using upload-large-folder tool
9e2af38 verified
# Path Configuration
from tools.preprocess import *
# Processing context
trait = "Multiple_sclerosis"
cohort = "GSE193442"
# Input paths
in_trait_dir = "../DATA/GEO/Multiple_sclerosis"
in_cohort_dir = "../DATA/GEO/Multiple_sclerosis/GSE193442"
# Output paths
out_data_file = "./output/preprocess/3/Multiple_sclerosis/GSE193442.csv"
out_gene_data_file = "./output/preprocess/3/Multiple_sclerosis/gene_data/GSE193442.csv"
out_clinical_data_file = "./output/preprocess/3/Multiple_sclerosis/clinical_data/GSE193442.csv"
json_path = "./output/preprocess/3/Multiple_sclerosis/cohort_info.json"
# Get file paths
soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)
# Extract background info and clinical data
background_info, clinical_data = get_background_and_clinical_data(matrix_file)
# Get unique values per clinical feature
sample_characteristics = get_unique_values_by_row(clinical_data)
# Print background info
print("Dataset Background Information:")
print(f"{background_info}\n")
# Print sample characteristics
print("Sample Characteristics:")
for feature, values in sample_characteristics.items():
print(f"Feature: {feature}")
print(f"Values: {values}\n")
# 1. Gene Expression Data Availability
# Based on the Series title and design, this appears to be a transcriptional profiling study of T cells
is_gene_available = True
# 2. Variable Availability and Data Analysis
# Looking at sample characteristics, there is no trait, age or gender data available
trait_row = None
age_row = None
gender_row = None
# 2.2 Define conversion functions (even though not used in this case)
def convert_trait(value):
if not isinstance(value, str):
return None
value = value.split(": ")[-1].strip().lower()
if value == "ms" or value == "multiple sclerosis":
return 1
elif value == "control" or value == "healthy" or value == "hc":
return 0
return None
def convert_age(value):
if not isinstance(value, str):
return None
try:
age = float(value.split(": ")[-1].strip())
return age
except:
return None
def convert_gender(value):
if not isinstance(value, str):
return None
value = value.split(": ")[-1].strip().lower()
if value in ["f", "female"]:
return 0
elif value in ["m", "male"]:
return 1
return None
# 3. Save metadata
# trait_row is None so is_trait_available is False
validate_and_save_cohort_info(is_final=False,
cohort=cohort,
info_path=json_path,
is_gene_available=is_gene_available,
is_trait_available=False)
# 4. Skip clinical feature extraction since trait_row is None
# Get file paths
soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)
# First inspect the file structure
print("First few lines of matrix file:")
with gzip.open(matrix_file, 'rt') as f:
for i, line in enumerate(f):
print(line.strip())
if i >= 10:
break
# Manually find where the data table begins
with gzip.open(matrix_file, 'rt') as f:
for i, line in enumerate(f):
if "series_matrix_table_end" in line:
end_marker = i
if "series_matrix_table_begin" in line:
begin_marker = i
header = next(f).strip() # Get column names
print("\nFound data table at line", i)
print("Header line:", header)
break
# Now extract gene expression data with correct marker positioning
gene_data = get_genetic_data(matrix_file)
# Print shape and content information
print("\nShape of gene expression data:", gene_data.shape)
print("\nFirst few rows of data:")
print(gene_data.head())
# Print first 20 gene/probe identifiers
print("\nFirst 20 gene/probe identifiers:")
print(gene_data.head(20).index)
requires_gene_mapping = True
# Get file paths
soft_file, matrix_file = geo_get_relevant_filepaths(in_cohort_dir)
# First inspect all files in directory
print("Files in directory:")
print(os.listdir(in_cohort_dir))
# First inspect the matrix file content since it may contain platform info
print("\nFirst few lines of matrix file:")
with gzip.open(matrix_file, 'rt') as f:
for i, line in enumerate(f):
if i < 15 or ('!platform_table_begin' in line.lower() and i < 30):
print(line.strip())
if i >= 30:
break
# Try to extract platform annotation from matrix file
platform_lines = []
capturing = False
with gzip.open(matrix_file, 'rt') as f:
for line in f:
if '!platform_table_begin' in line.lower():
capturing = True
continue
if '!platform_table_end' in line.lower():
capturing = False
break
if capturing and line.strip():
platform_lines.append(line)
# Convert platform lines to DataFrame if any exist
if platform_lines:
try:
gene_annotation = pd.read_csv(io.StringIO(''.join(platform_lines)),
delimiter='\t', low_memory=False)
print("\nGene Annotation Preview:")
print("Column names:", gene_annotation.columns.tolist())
print("\nFirst few rows as dictionary:")
print(preview_df(gene_annotation))
except Exception as e:
print(f"\nError reading platform annotation: {str(e)}")
else:
print("\nNo platform annotation data found in matrix file")
# Check output from steps 3 and 5
# Since gene identifiers preview is empty and there is no platform annotation in matrix file
# We will need to create empty dataframe for now and pass it through the pipeline
# The actual gene mapping will be done when proper data is available
gene_data = pd.DataFrame()