Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update package to use package extensions #32

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,23 @@ uuid = "1692102d-eeb4-4df9-807b-c9517f998d44"
authors = ["Michael F. Herbst <[email protected]> and contributors"]
version = "0.2.3"

[deps]
AtomsBase = "a963bdd2-2df7-4f54-a1ee-49d51e6be12a"
[weakdeps]
Chemfiles = "46823bd8-5fb3-5f92-9aa0-96921f3dd015"
ExtXYZ = "352459e4-ddd7-4360-8937-99dcb397b478"
XCrySDenStructureFormat = "02310045-4665-40c9-a658-98c497d2eca9"

[extensions]
AtomsIOChemfilesExt = "Chemfiles"
AtomsIOExtXYZExt = "ExtXYZ"
AtomsIOXCrySDenStructureFormatExt = "XCrySDenStructureFormat"

[deps]
AtomsBase = "a963bdd2-2df7-4f54-a1ee-49d51e6be12a"
Logging = "56ddb016-857b-54e1-b83d-db4d58db5568"
PeriodicTable = "7b2266bf-644c-5ea3-82d8-af4bbd25a884"
Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"
UnitfulAtomic = "a7773ee8-282e-5fa2-be4e-bd808c38a91a"
XCrySDenStructureFormat = "02310045-4665-40c9-a658-98c497d2eca9"

[compat]
AtomsBase = "0.3"
Expand All @@ -33,4 +40,4 @@ Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["AtomsBaseTesting", "LinearAlgebra", "Pkg", "Test"]
test = ["AtomsBaseTesting", "LinearAlgebra", "Pkg", "Test", "Chemfiles", "ExtXYZ", "XCrySDenStructureFormat"]
26 changes: 10 additions & 16 deletions src/chemfiles.jl → ext/AtomsIOChemfilesExt.jl
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
module AtomsIOChemfilesExt

import Chemfiles
using Logging
using Unitful
using UnitfulAtomic

"""
Parse or write file using [Chemfiles](https://github.com/chemfiles/Chemfiles.jl).

Supported formats:
- [CIF](https://www.iucr.org/resources/cif) files
- [Gromacs](http://manual.gromacs.org/archive/5.0.7/online/trj.html) / [LAMMPS](https://lammps.sandia.gov/doc/dump.html) / [Amber](http://ambermd.org/netcdf/nctraj.xhtml) trajectory files
"""
struct ChemfilesParser <: AbstractParser end
using AtomsIO, Unitful, UnitfulAtomic

function supports_parsing(::ChemfilesParser, file; save, trajectory)
function AtomsIO.supports_parsing(::ChemfilesParser, file; save, trajectory)
format = Logging.with_logger(NullLogger()) do
Chemfiles.guess_format(file)
end
Expand All @@ -29,32 +21,34 @@ function supports_parsing(::ChemfilesParser, file; save, trajectory)
end


function load_system(::ChemfilesParser, file::AbstractString, index=nothing)
function AtomsIO.load_system(::ChemfilesParser, file::AbstractString, index=nothing)
Chemfiles.Trajectory(file, 'r') do trajectory
cfindex = something(index, length(trajectory)) - 1 # chemfiles is 0-based
convert(AbstractSystem, Chemfiles.read_step(trajectory, cfindex))
end
end

function save_system(::ChemfilesParser, file::AbstractString, system::AbstractSystem)
function AtomsIO.save_system(::ChemfilesParser, file::AbstractString, system::AbstractSystem)
Chemfiles.Trajectory(file, 'w') do trajectory
write(trajectory, convert(Chemfiles.Frame, system))
end
end

function load_trajectory(::ChemfilesParser, file::AbstractString)
function AtomsIO.load_trajectory(::ChemfilesParser, file::AbstractString)
Chemfiles.Trajectory(file, 'r') do trajectory
map(0:length(trajectory)-1) do ci # Chemfiles is 0-based
convert(AbstractSystem, Chemfiles.read_step(trajectory, ci))
end
end
end

function save_trajectory(::ChemfilesParser, file::AbstractString,
function AtomsIO.save_trajectory(::ChemfilesParser, file::AbstractString,
systems::AbstractVector{<:AbstractSystem})
Chemfiles.Trajectory(file, 'w') do trajectory
for system in systems
write(trajectory, convert(Chemfiles.Frame, system))
end
end
end

end
23 changes: 10 additions & 13 deletions src/extxyz.jl → ext/AtomsIOExtXYZExt.jl
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import ExtXYZ

"""
Parse or write file using [ExtXYZ](https://github.com/libAtoms/ExtXYZ.jl)
module AtomsIOExtXYZExt

Supported formats:
- [XYZ](https://openbabel.org/wiki/XYZ) and [extxyz](https://github.com/libAtoms/extxyz#extended-xyz-specification-and-parsing-tools) files
"""
struct ExtxyzParser <: AbstractParser end
import ExtXYZ
using AtomsIO

function supports_parsing(::ExtxyzParser, file; save, trajectory)
function AtomsIO.supports_parsing(::ExtxyzParser, file; save, trajectory)
_, ext = splitext(file)
ext in (".xyz", ".extxyz")
end
Expand All @@ -34,7 +29,7 @@ function _extxyz_read_frames(args...; kwargs...)
frames
end

function load_system(::ExtxyzParser, file::AbstractString, index=nothing)
function AtomsIO.load_system(::ExtxyzParser, file::AbstractString, index=nothing)
if isnothing(index)
frames = _extxyz_read_frames(file)
return ExtXYZ.Atoms(last(frames))
Expand All @@ -44,15 +39,17 @@ function load_system(::ExtxyzParser, file::AbstractString, index=nothing)
end
end

function save_system(::ExtxyzParser, file::AbstractString, system::AbstractSystem)
function AtomsIO.save_system(::ExtxyzParser, file::AbstractString, system::AbstractSystem)
ExtXYZ.write_frame(file, ExtXYZ.write_dict(system))
end

function load_trajectory(::ExtxyzParser, file::AbstractString)
function AtomsIO.load_trajectory(::ExtxyzParser, file::AbstractString)
ExtXYZ.Atoms.(_extxyz_read_frames(file))
end

function save_trajectory(::ExtxyzParser, file::AbstractString,
function AtomsIO.save_trajectory(::ExtxyzParser, file::AbstractString,
systems::AbstractVector{<:AbstractSystem})
ExtXYZ.write_frames(file, ExtXYZ.write_dict.(systems))
end

end
37 changes: 37 additions & 0 deletions ext/AtomsIOXCrySDenStructureFormatExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
module AtomsIOXCrySDenStructureFormatExt

import XCrySDenStructureFormat as XSF
using AtomsIO

function AtomsIO.supports_parsing(::XcrysdenstructureformatParser, file; save, trajectory)
_, ext = splitext(file)
ext in (".xsf", ".axsf")
end

function AtomsIO.load_system(::XcrysdenstructureformatParser, file::AbstractString, index=nothing)
frames = XSF.load_xsf(file)
isempty(frames) && error(
"XSF returned no frames. Check the passed file is a valid (a)xsf file."
)
if isnothing(index)
return last(frames)
else
return frames[index]
end
end

function AtomsIO.save_system(::XcrysdenstructureformatParser,
file::AbstractString, system::AbstractSystem)
XSF.save_xsf(file, system)
end

function AtomsIO.load_trajectory(::XcrysdenstructureformatParser, file::AbstractString)
XSF.load_xsf(file)
end

function AtomsIO.save_trajectory(::XcrysdenstructureformatParser, file::AbstractString,
systems::AbstractVector{<:AbstractSystem})
XSF.save_xsf(file, systems)
end

end
9 changes: 3 additions & 6 deletions src/AtomsIO.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@ using Reexport
import PeriodicTable

include("parser.jl")
include("chemfiles.jl")
include("extxyz.jl")
include("xsf.jl")
include("saveload.jl")
export AbstractParser, ChemfilesParser, ExtxyzParser, XcrysdenstructureformatParser

include("saveload.jl")
export load_system, save_system, load_trajectory, save_trajectory
export AbstractParser
export ChemfilesParser, ExtxyzParser, XcrysdenstructureformatParser

end
30 changes: 30 additions & 0 deletions src/parser.jl
Original file line number Diff line number Diff line change
@@ -1,2 +1,32 @@
abstract type AbstractParser end


"""
Parse or write file using [Chemfiles](https://github.com/chemfiles/Chemfiles.jl).

Supported formats:
- [CIF](https://www.iucr.org/resources/cif) files
- [Gromacs](http://manual.gromacs.org/archive/5.0.7/online/trj.html) / [LAMMPS](https://lammps.sandia.gov/doc/dump.html) / [Amber](http://ambermd.org/netcdf/nctraj.xhtml) trajectory files
"""
struct ChemfilesParser <: AbstractParser end

"""
Parse or write file using [ExtXYZ](https://github.com/libAtoms/ExtXYZ.jl)

Supported formats:
- [XYZ](https://openbabel.org/wiki/XYZ) and [extxyz](https://github.com/libAtoms/extxyz#extended-xyz-specification-and-parsing-tools) files
"""
struct ExtxyzParser <: AbstractParser end


"""
Parse or write file using [XCrySDenStructureFormat](https://github.com/azadoks/XCrySDenStructureFormat.jl).

Supported formats:
- [XSF](http://www.xcrysden.org/doc/XSF.html) and [AXSF](http://www.xcrysden.org/doc/XSF.html)
atomic structure files. These are the files typically used by the
[XCrySDen](http://www.xcrysden.org/) visualisation program.
"""
struct XcrysdenstructureformatParser <: AbstractParser end

supports_parsing(::AbstractParser, file; save, trajectory) = false
42 changes: 0 additions & 42 deletions src/xsf.jl

This file was deleted.

2 changes: 1 addition & 1 deletion test/chemfiles.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using AtomsIO
using Test
using AtomsIO
using AtomsBaseTesting
using Unitful
using UnitfulAtomic
Expand Down
4 changes: 0 additions & 4 deletions test/extxyz.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
using AtomsIO
using Test
using AtomsBaseTesting

@testset "ExtXYZ system write / read" begin
system = make_test_system().system
mktempdir() do d
Expand Down
5 changes: 1 addition & 4 deletions test/failed_files.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
using Test
using AtomsIO
using Unitful
using LinearAlgebra

# Tests parsing files, where previously users reported problems
Expand All @@ -19,7 +16,7 @@ end
elseif VERSION < v"1.8"
@test_throws ErrorException load_system("files/zero_atoms.xyz")
else
@test_throws "ExtXYZ frame contains zero atoms." load_system("files/zero_atoms.xyz")
@test_throws "ExtXYZ returned no frames. Check the passed file is a valid (ext)xyz file." load_system("files/zero_atoms.xyz")
end
end

Expand Down
4 changes: 4 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
using Test
using Pkg
using AtomsIO, AtomsBaseTesting
using Unitful, UnitfulAtomic

using Chemfiles, ExtXYZ, XCrySDenStructureFormat

const GROUP = get(ENV, "GROUP", "Core")
const GROUP_COVERAGE = !isempty(get(ENV, "GROUP_COVERAGE", ""))
Expand Down
4 changes: 0 additions & 4 deletions test/safe_load.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
using Test
using AtomsIO
using AtomsBaseTesting

@testset "save / load / determine_parser" begin
system = make_test_system().system

Expand Down
7 changes: 0 additions & 7 deletions test/xsf.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
using AtomsIO
using Test
using AtomsBaseTesting
using Unitful
using UnitfulAtomic


function make_xsf_system(D=3; drop_atprop=Symbol[], drop_sysprop=Symbol[], kwargs...)
@assert D == 3 # Only 3D systems are supported
n_atoms = 5 # Copied from AtomsBaseTesting.make_test_system, used for making forces
Expand Down
Loading