<p>Converting a PDB file to some version of an mmCIF file is always possible provided the PDB file doesn't have any "problems". However grouping identical molecules into the same entity is non-trivial so the Biopython and BioJulia mmCIF writers (which I wrote) do not try and do this, simply assigning new entites for each molecule. These files will also be missing other things like the metadata parts of the mmCIF dictionary not involving the atoms.</p>

<p>In Biopython:</p>

<pre class="lang-py prettyprint-override"><code>from Bio.PDB import PDBParser, MMCIFIO
p = PDBParser()
struc = p.get_structure("", "file.pdb")
io = MMCIFIO()
io.set_structure(struc)
io.save("file.cif")
</code></pre>

<p>In BioJulia:</p>

<pre><code>using BioStructures
struc = read("file.pdb", PDB)
writemmcif("file.cif", struc)
</code></pre>

<p>Going the other way is, however, not always possible. You can run out of atom serials or chain IDs to use in the output PDB file (this is one reason for the switch to mmCIF files in general). The gist you have linked is a way to get round this.</p>

<p>If this is not a problem for your structure you can invert the above snippets to get a decent PDB file - see the relevant docs. Note that in both cases it will use the <code>auth_seq_id</code> and <code>auth_asym_id</code> fields to get the residue number and chain ID rather than the relabelled <code>label</code> versions. The test suites of Biopython and BioJulia do test these conversions pretty thoroughly.</p>

<p>There is also <a href="https://github.com/haddocking/pdb-tools" rel="noreferrer">pdb-tools</a> if you do not want to install Biopython, though both involve a simple <code>pip install</code> so it's not much difference.</p>

<p>For some speed benchmarks of various software see <a href="https://github.com/jgreener64/pdb-benchmarks" rel="noreferrer">here</a> and <a href="https://github.com/project-gemmi/mmcif-benchmark" rel="noreferrer">here</a>.</p>

CC BY-SA 4.0
2021-07-16T12:42:00.630Z
hey @jgreener! Thanks for the clarification! 
I'm wondering how come MMCIFIO drops metadata and such? Isn't that pretty inconvenient? let's say i need to rename (change the ids of ) certain chains in a structure and nothing else... I'll be losing all the authors' annotation, seq data etc. just by doing `io.set_structure(struct)`, right? Are there any alternatives that you know of?
rtviii
CC BY-SA 4.0
2021-07-17T15:27:40.040Z
Yes, you would lose that info. The readers and writers focus on parsing the atomic structural data into a unified representation that can be modified and written back out to any file type. Developer focus has gone there rather than on writing a unified representation for the metadata. My recommendation would either be to concatenate the newly generated file onto the old metadata (beware some may be wrong now) or to use the `MMCIF2Dict` constructor and modify list items (writing that out preserves metadata, again some metadata may now be wrong).
jgreener
CC BY-SA 4.0
2021-07-17T23:09:59.147Z
that's super useful. I have played with MMCIF2Dict before and that'd actually be ideal to preserve and edit the metadata. Any tips for how to write that representation (the dict resultant from MMCIF2Dict) to a .cif file if not through MMCIFIO?
rtviii
CC BY-SA 4.0
2021-07-18T14:17:26.127Z
You can write a `MMCIF2Dict` with `MMCIFIO`, just use `io.set_dict(d)` rather than `set_structure`.
jgreener
CC BY-SA 4.0
2019-11-30T00:45:37.400Z
2019-11-30T00:45:37.400Z
jgreener
<p>There is already <a href="https://bioinformatics.stackexchange.com/questions/694/converter-between-pdb-or-mmcif-and-mmtf">a question on PDB/CIF to MMTF</a>, however what is a robust way to programmatically go between PDB and CIF files?</p>

<p>For example I can use a python script from this <a href="https://gist.github.com/sbliven/b7cc2c5305aa3652a75a580ae7c6ce33" rel="nofollow noreferrer">gist</a> that relies on Biopython.</p>

<pre><code>id="4ckh"
wget http://www.ebi.ac.uk/pdbe/static/entry/download/<span class="math-container">$id-assembly-1.cif.gz
gunzip $</span>id-assembly-1.cif.gz
python cif2pdb.py <span class="math-container">$id-assembly-1.cif $</span>id.pdb
</code></pre>

<p>This process yields a pdb structure as shown below.</p>

<p><img src="https://i.sstatic.net/wWiJG.png" alt="A large protein structure"></p>

<p>The issues are:</p>

<ul>
<li>Requires python/Biopython to be installed.</li>
<li>The output isn't perfectly clean.</li>
<li>The gist is relatively undeveloped and hasn't got much evidence of testing.</li>
</ul>

CC BY-SA 4.0
2019-11-29T10:44:09.083Z
I can't really address your issues certainly so I thought I would comment rather than answer, but I've had good results using Maxit (https://sw-tools.rcsb.org/) which is produced by the RCSB. At least for submitting to the PDB Maxit is endorsed so I would hope it's robust.
TW93
<file-formats><protein-structure>
How to quickly and robustly convert between mmCIF and PDB?
# How to quickly and robustly convert between mmCIF and PDB?

(Asked by: username_0 on 2019-11-27)

There is already a question on PDB/CIF to MMTF, however what is a robust way to programmatically go between PDB and CIF files?
For example I can use a python script from this gist that relies on Biopython.
id="4ckh"
wget http://www.ebi.ac.uk/pdbe/static/entry/download/$id-assembly-1.cif.gz
gunzip $id-assembly-1.cif.gz
python cif2pdb.py $id-assembly-1.cif $id.pdb

This process yields a pdb structure as shown below.

The issues are:

Requires python/Biopython to be installed.
The output isn't perfectly clean.
The gist is relatively undeveloped and hasn't got much evidence of testing.

**Tags:** file-formats, protein-structure
> **Comments:**
> **username_1**: I can't really address your issues certainly so I thought I would comment rather than answer, but I've had good results using Maxit (https://sw-tools.rcsb.org/) which is produced by the RCSB. At least for submitting to the PDB Maxit is endorsed so I would hope it's robust.  

---

## 1 answers

### Answer by username_2 on 2019-11-30. Score: 5

Converting a PDB file to some version of an mmCIF file is always possible provided the PDB file doesn't have any "problems". However grouping identical molecules into the same entity is non-trivial so the Biopython and BioJulia mmCIF writers (which I wrote) do not try and do this, simply assigning new entites for each molecule. These files will also be missing other things like the metadata parts of the mmCIF dictionary not involving the atoms.
In Biopython:
from Bio.PDB import PDBParser, MMCIFIO
p = PDBParser()
struc = p.get_structure("", "file.pdb")
io = MMCIFIO()
io.set_structure(struc)
io.save("file.cif")

In BioJulia:
using BioStructures
struc = read("file.pdb", PDB)
writemmcif("file.cif", struc)

Going the other way is, however, not always possible. You can run out of atom serials or chain IDs to use in the output PDB file (this is one reason for the switch to mmCIF files in general). The gist you have linked is a way to get round this.
If this is not a problem for your structure you can invert the above snippets to get a decent PDB file - see the relevant docs. Note that in both cases it will use the auth_seq_id and auth_asym_id fields to get the residue number and chain ID rather than the relabelled label versions. The test suites of Biopython and BioJulia do test these conversions pretty thoroughly.
There is also pdb-tools if you do not want to install Biopython, though both involve a simple pip install so it's not much difference.
For some speed benchmarks of various software see here and here.

> **Comments:**
> **username_3**: hey @jgreener! Thanks for the clarification!  I'm wondering how come MMCIFIO drops metadata and such? Isn't that pretty inconvenient? let's say i need to rename (change the ids of ) certain chains in a structure and nothing else... I'll be losing all the authors' annotation, seq data etc. just by doing `io.set_structure(struct)`, right? Are there any alternatives that you know of?  
> **username_2**: Yes, you would lose that info. The readers and writers focus on parsing the atomic structural data into a unified representation that can be modified and written back out to any file type. Developer focus has gone there rather than on writing a unified representation for the metadata. My recommendation would either be to concatenate the newly generated file onto the old metadata (beware some may be wrong now) or to use the `MMCIF2Dict` constructor and modify list items (writing that out preserves metadata, again some metadata may now be wrong).  
> **username_3**: that's super useful. I have played with MMCIF2Dict before and that'd actually be ideal to preserve and edit the metadata. Any tips for how to write that representation (the dict resultant from MMCIF2Dict) to a .cif file if not through MMCIFIO?  
> **username_2**: You can write a `MMCIF2Dict` with `MMCIFIO`, just use `io.set_dict(d)` rather than `set_structure`.  

---

