Spaces:
Build error
Build error
Commit ·
993440c
1
Parent(s): a599a1a
Convert XYZ to PDB for compatibility with gradio_molecule3d viewer
Browse files
app.py
CHANGED
|
@@ -175,12 +175,41 @@ MOL_REPS = [
|
|
| 175 |
{"model": 0, "style": "sphere", "color": "whiteCarbon", "opacity": 0.3},
|
| 176 |
]
|
| 177 |
|
| 178 |
-
def
|
| 179 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 180 |
tmp = tempfile.NamedTemporaryFile(
|
| 181 |
-
suffix=".
|
| 182 |
)
|
| 183 |
-
tmp.write(
|
| 184 |
tmp.close()
|
| 185 |
return tmp.name
|
| 186 |
|
|
@@ -242,10 +271,10 @@ def generate(num_molecules, size_mode, fixed_size, diffusion_steps, seed):
|
|
| 242 |
xyz_strings.append(xyz_str)
|
| 243 |
summary_rows.append(parse_composition(xyz_str))
|
| 244 |
|
| 245 |
-
# 5. Output generation – save
|
| 246 |
file_paths = []
|
| 247 |
for i, xyz_str in enumerate(xyz_strings):
|
| 248 |
-
file_paths.append(
|
| 249 |
|
| 250 |
zip_path = create_xyz_zip(xyz_strings)
|
| 251 |
df_out = pd.DataFrame(summary_rows)
|
|
|
|
| 175 |
{"model": 0, "style": "sphere", "color": "whiteCarbon", "opacity": 0.3},
|
| 176 |
]
|
| 177 |
|
| 178 |
+
def xyz_to_pdb(xyz_str):
|
| 179 |
+
"""Convert XYZ string to PDB format for the viewer."""
|
| 180 |
+
lines = xyz_str.strip().splitlines()
|
| 181 |
+
atoms = lines[2:] # Skip header
|
| 182 |
+
pdb_lines = []
|
| 183 |
+
|
| 184 |
+
for i, line in enumerate(atoms):
|
| 185 |
+
parts = line.split()
|
| 186 |
+
if len(parts) >= 4:
|
| 187 |
+
elem = parts[0]
|
| 188 |
+
x, y, z = float(parts[1]), float(parts[2]), float(parts[3])
|
| 189 |
+
# Simple HETATM record
|
| 190 |
+
# Col 1-6: Record name "HETATM"
|
| 191 |
+
# Col 7-11: Serial number
|
| 192 |
+
# Col 13-16: Atom name
|
| 193 |
+
# Col 18-20: Residue name "UNK"
|
| 194 |
+
# Col 23-26: Residue sequence number "1"
|
| 195 |
+
# Col 31-38: X coord
|
| 196 |
+
# Col 39-46: Y coord
|
| 197 |
+
# Col 47-54: Z coord
|
| 198 |
+
# Col 55-60: Occupancy (1.00)
|
| 199 |
+
# Col 61-66: Temp factor (0.00)
|
| 200 |
+
# Col 77-78: Element symbol
|
| 201 |
+
pdb_lines.append(
|
| 202 |
+
f"HETATM{i+1:>5} {elem:<3} UNK 1 {x:8.3f}{y:8.3f}{z:8.3f} 1.00 0.00 {elem:>2}"
|
| 203 |
+
)
|
| 204 |
+
return "\n".join(pdb_lines)
|
| 205 |
+
|
| 206 |
+
def save_pdb_to_file(xyz_str, idx):
|
| 207 |
+
"""Save converted PDB string to a temp file and return the path."""
|
| 208 |
+
pdb_str = xyz_to_pdb(xyz_str)
|
| 209 |
tmp = tempfile.NamedTemporaryFile(
|
| 210 |
+
suffix=".pdb", prefix=f"mol_{idx:03d}_", delete=False, mode="w"
|
| 211 |
)
|
| 212 |
+
tmp.write(pdb_str)
|
| 213 |
tmp.close()
|
| 214 |
return tmp.name
|
| 215 |
|
|
|
|
| 271 |
xyz_strings.append(xyz_str)
|
| 272 |
summary_rows.append(parse_composition(xyz_str))
|
| 273 |
|
| 274 |
+
# 5. Output generation – save PDB files for the viewer (XYZ for download)
|
| 275 |
file_paths = []
|
| 276 |
for i, xyz_str in enumerate(xyz_strings):
|
| 277 |
+
file_paths.append(save_pdb_to_file(xyz_str, i))
|
| 278 |
|
| 279 |
zip_path = create_xyz_zip(xyz_strings)
|
| 280 |
df_out = pd.DataFrame(summary_rows)
|