Skip to content

Commit

Permalink
Merge pull request #45 from molssi-seamm/dev
Browse files Browse the repository at this point in the history
Support for .gz and .bz2 files, and multi-structure .xyz files
  • Loading branch information
seamm authored Jul 27, 2023
2 parents 27a7b4e + 62d3bac commit 6a5d258
Show file tree
Hide file tree
Showing 9 changed files with 365 additions and 152 deletions.
4 changes: 4 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
History
=======

2023.7.27 -- Support for .gz and .bz2 files, and multi-structure .xyz files
* Handle .gz and .bz2 files for .sdf and .xyz extensions.
* Handle multi-structure XYZ files with a blank line between records.

2023.7.6 -- Bugfixes
* Fixed output of number of structures written to SDF files, which was off by 1.
* Cleaned up the output for the write-structure step
Expand Down
18 changes: 15 additions & 3 deletions read_structure_step/formats/sdf/sdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Implementation of the reader for SDF files using OpenBabel
"""

import bz2
import gzip
from pathlib import Path
import shutil
Expand Down Expand Up @@ -133,10 +134,15 @@ def load_sdf(
path.expanduser().resolve()

# Get the information for progress output, if requested.
compress = path.suffix == ".gz"
if printer is not None:
n_structures = 0
with gzip.open(path, mode="rt") if compress else open(path, "r") as fd:
with (
gzip.open(path, mode="rt")
if path.suffix == ".gz"
else bz2.open(path, mode="rt")
if path.suffix == ".bz2"
else open(path, "r")
) as fd:
for line in fd:
if line[0:4] == "$$$$":
n_structures += 1
Expand All @@ -154,7 +160,13 @@ def load_sdf(
n_errors = 0
obMol = openbabel.OBMol()
text = ""
with gzip.open(path, mode="rt") if compress else open(path, "r") as fd:
with (
gzip.open(path, mode="rt")
if path.suffix == ".gz"
else bz2.open(path, mode="rt")
if path.suffix == ".bz2"
else open(path, "r")
) as fd:
for line in fd:
text += line

Expand Down
Loading

0 comments on commit 6a5d258

Please sign in to comment.