File size: 2,476 Bytes
8649d5a
 
 
 
7f14dfe
f6a67e4
 
 
8649d5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4a67952
8649d5a
 
 
 
4a67952
8649d5a
 
4a67952
8649d5a
 
4a67952
7f14dfe
 
 
 
 
 
 
 
f6a67e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gemmi
import os
import shutil
import gradio as gr
import subprocess
import json
import yaml
from pathlib import Path

def mcif_gz_to_pdb(file_path: str) -> str:
    """
    Converts a .mcif.gz file to pdb and saves it to the same directory. Returns the path to the pdb file.
    
    Parameters:
    ----------
    file_path: str,
        Path to the .mcif.gz file.
    
    Returns
    -------
        str: path to the generated pdb file.
    """
    st = gemmi.read_structure(file_path)
    st.setup_entities()  # Recommended for consistent entity handling [web:18]
    pdb_path = file_path.replace(".cif.gz", ".pdb")
    st.write_minimal_pdb(pdb_path)
    return pdb_path


def download_results_as_zip(directory):
    """
    Check that an output directory is specified, then creates a zip file of the directory for download.
    
    Parameters:
    ----------
    directory: gr.State or str
        Path to the directory containing generated results. None if generation has not been run yet.
    
    Returns
    -------
        str or None: Path to the created zip file if directory is valid, else None.
    """
    if directory is None:
        return None
    zip_path = f"{directory}.zip"
    shutil.make_archive(directory, 'zip', directory)
    return zip_path


def collect_outputs(gen_directory, num_batches, num_designs_per_batch):
    try:
        cmd = f"ls -R {gen_directory}"
        file_list = subprocess.check_output(cmd, shell=True).decode()
        return file_list
    except Exception as e:
        return f"Error: {str(e)}"
    

def load_config(file_path: str | Path) -> dict | list:
    """
    Load YAML or JSON file into a Python object.
    
    Args:
        file_path: Path to the YAML or JSON file.
    
    Returns:
        Parsed Python object (dict, list, etc.).
    
    Raises:
        ValueError: If extension is not .yaml, .yml, or .json.
        Exception: On parse errors.
    """
    path = Path(file_path)
    if not path.exists():
        raise FileNotFoundError(f"File not found: {file_path}")
    
    ext = path.suffix.lower()
    if ext in {'.yaml', '.yml'}:
        with open(path, 'r', encoding='utf-8') as f:
            return yaml.safe_load(f)  # Secure loader [web:1][web:4]
    elif ext == '.json':
        with open(path, 'r', encoding='utf-8') as f:
            return json.load(f)  # Built-in JSON loader [web:12]
    else:
        raise ValueError(f"Unsupported extension: {ext}. Use .yaml, .yml, or .json.")