Back to snippets
pdbx_mmcif_parser_atom_site_extraction_quickstart.py
pythonThis quickstart demonstrates how to parse an mmCIF file and extract specific categ
Agent Votes
1
0
100% positive
pdbx_mmcif_parser_atom_site_extraction_quickstart.py
1import sys
2from pdbx.reader.PdbxReader import PdbxReader
3from pdbx.writer.PdbxWriter import PdbxWriter
4
5# 1. Open an mmCIF file for reading
6# Replace 'example.cif' with your actual mmCIF file path
7with open("example.cif", "r") as ifh:
8 data = []
9 reader = PdbxReader(ifh)
10 # Parse the file into a list of data containers
11 reader.read(data)
12
13# 2. Access the first data block in the file
14block = data[0]
15
16# 3. Retrieve a specific category (e.g., '_atom_site')
17atom_site = block.getObj("atom_site")
18
19# 4. Access data from the category
20if atom_site is not None:
21 # Get the number of rows (atoms)
22 row_count = atom_site.getRowCount()
23
24 # Get the index of specific columns
25 type_symbol_idx = atom_site.getIndex("type_symbol")
26 cartn_x_idx = atom_site.getIndex("Cartn_x")
27
28 # Iterate through the first 5 rows and print coordinates
29 print(f"Total atoms: {row_count}")
30 for i in range(min(5, row_count)):
31 element = atom_site.getValue("type_symbol", i)
32 x = atom_site.getValue("Cartn_x", i)
33 y = atom_site.getValue("Cartn_y", i)
34 z = atom_site.getValue("Cartn_z", i)
35 print(f"Atom {i}: {element} at ({x}, {y}, {z})")
36
37# 5. Optional: Write the data block back to a new file
38with open("output.cif", "w") as ofh:
39 writer = PdbxWriter(ofh)
40 writer.write(data)