RCSB Python I/O Utility Classes
Introduction
This module contains a collection of utility classes for performing I/O operations on common file formats encountered in the PDB data repository.
Installation
Download the library source software from the project repository:
git clone --recurse-submodules https://github.com/rcsb/py-rcsb_utils_io.git
Optionally, run test suite (Python versions 3.9) using tox:
tox
Installation is via the program pip.
pip install rcsb.utils.io
or from the local repository:
pip install .
Usage
The MarshalUtil offers an easy way for reading in and writing out files in various formats, including CSV, JSON, pickle, mmCIF, bcif (BinaryCIF), fasta , and "list" files (plain text file in which each row is a list item).
Reading files
Let's say you have a JSON file, "data.json". You can read this in by:
from rcsb.utils.io.MarshalUtil import MarshalUtil
mU = MarshalUtil(workDir=".")
dataD = mU.doImport("data.json", fmt="json")
The same method works even if the file is compressed (e.g., "data.json.gz"):
dataD = mU.doImport("data.json.gz", fmt="json")
Note that this automatic handling of compressed gzip files applies to any type of input format.
You can also import remote files directly from the command line, e.g.:
dataD = mU.doImport("https://files.rcsb.org/pub/pdb/holdings/current_file_holdings.json.gz", fmt="json")
To read in a pickle file, "data.pic":
from rcsb.utils.io.MarshalUtil import MarshalUtil
mU = MarshalUtil()
dataD = mU.doImport("data.pic", fmt="pickle")
To read in and parse an mmCIF file, "4hhb.cif.gz":
from rcsb.utils.io.MarshalUtil import MarshalUtil
mU = MarshalUtil()
# Read all data containers from the mmCIF file into `dataContainerList`
dataContainerList = mU.doImport("https://files.rcsb.org/pub/pdb/data/structures/divided/mmCIF/hh/4hhb.cif.gz", fmt="mmcif")
# Get the first dataContainer (in most cases, there will only be one container in the file)
dataContainer = dataContainerList[0]
# Print the name of the container
eName = dataContainer.getName()
print(eName)
# Get the list of categories
catNameList = dataContainer.getObjNameList()
print(catNameList)
# Iterate over all the categories and attributes and store them in a new dictionary
cifDataD = {}
for catName in catNameList:
if not dataContainer.exists(catName):
continue
dObj = dataContainer.getObj(catName)
for ii in range(dObj.getRowCount()):
dD = dObj.getRowAttributeDict(ii)
cifDataD.setdefault(eName, {}).setdefault(catName, []).append(dD)
To read in and parse an BCIF file, "4hhb.bcif.gz":
from rcsb.utils.io.MarshalUtil import MarshalUtil
mU = MarshalUtil()
# Read all data containers from the mmCIF file into `dataContainerList`
dataContainerList = mU.doImport("https://files.rcsb.org/pub/pdb/data/structures/divided/mmCIF/hh/4hhb.bcif.gz", fmt="bcif")
**_For more examples, see [testMarshallUtil.py](https://github.com/rcsb/py-rcsb_utils_io/blob/master/rcsb/utils/tests-io/testMarshallUtil.py)._**
#### Writing files
You can use the `MarshalUtil` to write out the following data structures into the corresponding file formats:
Object | Output fmt
list | list dict | json or pickle DataContainerList | mmcif or bcif
For example, if you have a dictionary object, `dataD`, you can export it via:
```python
from rcsb.utils.io.MarshalUtil import MarshalUtil
mU = MarshalUtil()
dataD = {"name": "John Doe", "age": "33"}
mU.doExport("data.json", dataD, fmt="json", indent=2)
# Or, to export and compress as gzip:
mU.doExport("data.json.gz", dataD, fmt="json", indent=2)
Additionally, if you want to export a BCIF file, you can export in via:
from rcsb.utils.io.MarshalUtil import MarshalUtil
mU = MarshalUtil()
# Use dataContainerList (created for reading a file) as reference
mU.doExport("data.bcif", dataContainerList, fmt="bcif")
# Or, to export and compress as gzip:
mU.doExport("data.bcif.gz", dataContainerList, fmt="bcif")
Metadata
Release files for rcsb.utils.io 1.56
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| rcsb_utils_io-1.56.tar.gz | 62.5 kB | Without provenance | Details for rcsb_utils_io-1.56.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| rcsb_utils_io-1.56-py3-none-any.whl | Python 3 | none | any | Details for rcsb_utils_io-1.56-py3-none-any.whl |
Total release size: 115.9 kB
Release files / rcsb_utils_io-1.56.tar.gz
| Download URL | rcsb_utils_io-1.56.tar.gz |
|---|---|
| Size | 62.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7ac27f18641c2e927c0b7f4a959c48449566c6e7b015fe362a8e2cfbfa4d44c7
|
|
BLAKE2b-256 checksum How to use checksums |
2a803b3bc4dc1e6a226bb3ce9b9707789ef7399543680fe6b99b15a2cf553950
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Release files / rcsb_utils_io-1.56-py3-none-any.whl
| Download URL | rcsb_utils_io-1.56-py3-none-any.whl |
|---|---|
| Size | 53.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c5a7882fcd9d26f6ebd373401032d2fb2c021700b4a61c861b34c8dfb9ff0a4c
|
|
BLAKE2b-256 checksum How to use checksums |
4f0c2a18f8500ce6af4bc858dfbb7ef0279aece162105b313f4b3b576c6ce607
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Security for rcsb.utils.io 1.56
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.