bioinf595_formed / scripts /st2_dataset_curation.py
haneulpark's picture
Upload 11 files
d927342 verified
# Curation of dataset
import pandas as pd
import os
from tqdm import tqdm
# Path to the input CSV and the folder containing XYZ files
csv_path = 'formed.csv'
xyz_folder = 'XYZ_FORMED'
# Load the CSV file
df = pd.read_csv(csv_path)
# Select necessary columns in 'formed.csv'
df = df[['name', 'gap']]
# Function to clean and load XYZ file data
def load_xyz(mol_name):
file_path = os.path.join(xyz_folder, mol_name + '.xyz')
if not os.path.exists(file_path):
print(f"File not found: {file_path}") # Debugging line
return None
try:
with open(file_path, 'r') as f:
xyz_data = f.read()
return clean_xyz(xyz_data)
except Exception as e:
print(f"Error reading {file_path}: {e}") # Debugging line
return None
# Function to clean XYZ data (removes unwanted meta information and formats correctly)
def clean_xyz(xyz_str):
# Remove the quotes if there are any around the whole string
xyz_str = xyz_str.replace('"', '').strip() # Remove all quotes and strip whitespace
# Split the string into lines
xyz_lines = xyz_str.splitlines()
# Remove the first two lines (e.g., "10", "./CONFAM_opt.xyz")
if len(xyz_lines) >= 2:
xyz_lines = xyz_lines[2:] # Remove the first two lines
# Prepare cleaned data by keeping atom names and coordinates only
cleaned_data = []
for line in xyz_lines:
parts = line.split()
if len(parts) == 4: # Ensure that there are exactly 4 parts (atom and coordinates)
cleaned_data.append(" ".join(parts[0:4])) # Join atom symbol with coordinates
# Join the cleaned data into a single string
return "\n".join(cleaned_data)
# Apply the cleaning function to the 'name' column and load the corresponding XYZ data
tqdm.pandas()
df['xyz'] = df['name'].progress_apply(load_xyz)
# Check if xyz column has any data missing or empty
print(f"Number of missing XYZ values: {df['xyz'].isnull().sum()}")
# Save the cleaned dataset to a new CSV file
df.to_csv('formed_xyz_combined.csv', index=False)