<p>You can use <a href="https://gemmi.readthedocs.io/" rel="nofollow noreferrer">Gemmi</a>.</p>

<pre class="lang-py prettyprint-override"><code>import gemmi
ccp4_map = gemmi.read_ccp4_map('my.map')
ccp4_map.setup()  # optional
np_array = numpy.array(ccp4_map.grid, copy=False)
# ...
ccp4_map.write_ccp4_map('out.ccp4')
</code></pre>

<p>The setup() call expands data to the whole unit cell.<br>
The numpy array above accesses the data through Python's <em>Buffer Protocol</em> (it does not copy the data). So changing values in numpy array changes gemmi.Ccp4Map.</p>

<p>You can also use gemmi to read mtz file and transform map coefficients to a real-space map, see <a href="https://gemmi.readthedocs.io/en/latest/hkl.html" rel="nofollow noreferrer">https://gemmi.readthedocs.io/en/latest/hkl.html</a>.</p>

CC BY-SA 4.0
2020-04-25T12:17:56.463Z
Two birds in one stone! Thanks ever so much!
Matteo Ferla
CC BY-SA 4.0
2020-04-25T11:44:23.370Z
2020-04-25T11:44:23.370Z
marcin
<p>I would like to merge together several ccp4 formatted density maps (and do a few minor things).</p>

<p>So ideally I would like to open the ccp4/mrc files as numpy arrays in Python 3 and save the array as a ccp4 file. CCTBX can open ccp4 maps as numpy arrays, but I really would like to avoid using Python 2.7 for various reasons.</p>

<p>This is to be used for constraining energy minimisation in PyRosetta using the <code>pyrosetta.rosetta.protocols.electron_density.SetupForDensityScoringMover</code> mover/sampler. But its <code>pyrosetta.rosetta.core.scoring.electron_density.getDensityMap</code> data loader only likes ccp4/mrc files (density maps) and not mtz files (structure factor maps).</p>

<p>From what I can tell there is no way to fudge a ccp4 map from a mtz map, correct? The fact that only ePDB provides ccp4 maps makes me want to use mtz files.</p>

<p>CCP4's Blend can merge datasets but uses only mtz files...</p>

<proteins><protein-structure>
CCP4 file to a Python 3 numpy array or similar workaround
# CCP4 file to a Python 3 numpy array or similar workaround

(Asked by: username_0 on 2020-04-25)

I would like to merge together several ccp4 formatted density maps (and do a few minor things).

So ideally I would like to open the ccp4/mrc files as numpy arrays in Python 3 and save the array as a ccp4 file. CCTBX can open ccp4 maps as numpy arrays, but I really would like to avoid using Python 2.7 for various reasons.

This is to be used for constraining energy minimisation in PyRosetta using the `pyrosetta.rosetta.protocols.electron_density.SetupForDensityScoringMover` mover/sampler. But its `pyrosetta.rosetta.core.scoring.electron_density.getDensityMap` data loader only likes ccp4/mrc files (density maps) and not mtz files (structure factor maps).

From what I can tell there is no way to fudge a ccp4 map from a mtz map, correct? The fact that only ePDB provides ccp4 maps makes me want to use mtz files.

CCP4's Blend can merge datasets but uses only mtz files...

**Tags:** proteins, protein-structure

---

## 1 answers

### Answer by username_1 on 2020-04-25. Score: 4 | Accepted answer

You can use Gemmi.
import gemmi
ccp4_map = gemmi.read_ccp4_map('my.map')
ccp4_map.setup()  # optional
np_array = numpy.array(ccp4_map.grid, copy=False)
# ...
ccp4_map.write_ccp4_map('out.ccp4')

The setup() call expands data to the whole unit cell.
The numpy array above accesses the data through Python's Buffer Protocol (it does not copy the data). So changing values in numpy array changes gemmi.Ccp4Map.
You can also use gemmi to read mtz file and transform map coefficients to a real-space map, see https://gemmi.readthedocs.io/en/latest/hkl.html.

> **Comments:**
> **username_0**: Two birds in one stone! Thanks ever so much!  

---

