CoolFace
Apppublic

hugging-science/RFdiffusion3

sourceHugging Faceupdated 7mo agoView on Hugging Face
4likes
handle_files.py86 linesDownload Raw Back to utils
1import gemmi2import os3import shutil4import gradio as gr5import subprocess6import json7import yaml8from pathlib import Path9 10def mcif_gz_to_pdb(file_path: str) -> str:11    """12    Converts a .mcif.gz file to pdb and saves it to the same directory. Returns the path to the pdb file.13    14    Parameters:15    ----------16    file_path: str,17        Path to the .mcif.gz file.18    19    Returns20    -------21        str: path to the generated pdb file.22    """23    st = gemmi.read_structure(file_path)24    st.setup_entities()  # Recommended for consistent entity handling [web:18]25    pdb_path = file_path.replace(".cif.gz", ".pdb")26    st.write_minimal_pdb(pdb_path)27    return pdb_path28 29 30def download_results_as_zip(directory):31    """32    Check that an output directory is specified, then creates a zip file of the directory for download.33    34    Parameters:35    ----------36    directory: gr.State or str37        Path to the directory containing generated results. None if generation has not been run yet.38    39    Returns40    -------41        str or None: Path to the created zip file if directory is valid, else None.42    """43    if directory is None:44        return None45    zip_path = f"{directory}.zip"46    shutil.make_archive(directory, 'zip', directory)47    return zip_path48 49 50def collect_outputs(gen_directory, num_batches, num_designs_per_batch):51    try:52        cmd = f"ls -R {gen_directory}"53        file_list = subprocess.check_output(cmd, shell=True).decode()54        return file_list55    except Exception as e:56        return f"Error: {str(e)}"57    58 59def load_config(file_path: str | Path) -> dict | list:60    """61    Load YAML or JSON file into a Python object.62    63    Args:64        file_path: Path to the YAML or JSON file.65    66    Returns:67        Parsed Python object (dict, list, etc.).68    69    Raises:70        ValueError: If extension is not .yaml, .yml, or .json.71        Exception: On parse errors.72    """73    path = Path(file_path)74    if not path.exists():75        raise FileNotFoundError(f"File not found: {file_path}")76    77    ext = path.suffix.lower()78    if ext in {'.yaml', '.yml'}:79        with open(path, 'r', encoding='utf-8') as f:80            return yaml.safe_load(f)  # Secure loader [web:1][web:4]81    elif ext == '.json':82        with open(path, 'r', encoding='utf-8') as f:83            return json.load(f)  # Built-in JSON loader [web:12]84    else:85        raise ValueError(f"Unsupported extension: {ext}. Use .yaml, .yml, or .json.")86