gabboud commited on
Commit
c49e7b8
·
1 Parent(s): 8705e46

mcif to pdb conversion with gemmi

Browse files
Files changed (1) hide show
  1. utils/pipelines.py +16 -15
utils/pipelines.py CHANGED
@@ -7,6 +7,7 @@ import spaces
7
  import subprocess
8
  from Bio.PDB import MMCIFParser, PDBIO
9
  import gzip
 
10
 
11
 
12
 
@@ -72,7 +73,7 @@ def unconditional_generation(num_batches, num_designs_per_batch, length):
72
  for batch in range(num_batches):
73
  for design in range(num_designs_per_batch):
74
  file_name = os.path.join(directory, f"_{batch}_model_{design}.cif.gz")
75
- results.append({"batch": batch, "design": design, "file": file_name, "pdb": cif_gz_to_pdb(file_name)})
76
 
77
  print(results)
78
  return directory, results
@@ -89,18 +90,18 @@ def collect_outputs(gen_directory, num_batches, num_designs_per_batch):
89
  return f"Error: {str(e)}"
90
 
91
 
92
- def cif_gz_to_pdb(cif_gz_path):
93
- """Convert .cif.gz to PDB string for viewer."""
94
- # Decompress & parse
95
- parser = MMCIFParser(QUIET=True)
96
- with gzip.open(cif_gz_path, 'rt') as f:
97
- struct = parser.get_structure('model', f)
98
 
99
- # Write to string
100
- io = PDBIO()
101
- io.set_structure(struct)
102
- pdb_lines = []
103
- class StringIO:
104
- def write(self, s): pdb_lines.append(s)
105
- io.save(StringIO())
106
- return ''.join(pdb_lines)
 
 
 
 
7
  import subprocess
8
  from Bio.PDB import MMCIFParser, PDBIO
9
  import gzip
10
+ import gemmi
11
 
12
 
13
 
 
73
  for batch in range(num_batches):
74
  for design in range(num_designs_per_batch):
75
  file_name = os.path.join(directory, f"_{batch}_model_{design}.cif.gz")
76
+ results.append({"batch": batch, "design": design, "file": file_name, "pdb": mcif_gz_to_pdb_string_gemmi(file_name)})
77
 
78
  print(results)
79
  return directory, results
 
90
  return f"Error: {str(e)}"
91
 
92
 
93
+ def mcif_gz_to_pdb_string_gemmi(file_path: str) -> str:
94
+ """
95
+ Converts a .mcif.gz file to a PDB-formatted string.
 
 
 
96
 
97
+ Args:
98
+ file_path (str): Path to the .mcif.gz file.
99
+
100
+ Returns:
101
+ str: PDB content as string.
102
+
103
+ Requires: pip install gemmi
104
+ """
105
+ st = gemmi.read_structure(file_path)
106
+ st.setup_entities() # Recommended for consistent entity handling [web:18]
107
+ return st.make_pdb_string(gemmi.PdbWriteOptions(minimal=True))