diff --git a/NEWS.md b/NEWS.md index 19447b6..ff4273e 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,9 @@ # BioStructures.jl release notes +## v4.3.0 - Nov 2024 + +* Selection strings are made much faster. + ## v4.2.1 - Oct 2024 * Change compatibility bounds for new MetaGraphs.jl release. diff --git a/Project.toml b/Project.toml index 788028a..4bd32ed 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "BioStructures" uuid = "de9282ab-8554-53be-b2d6-f6c222edabfc" authors = ["Joe G Greener "] -version = "4.2.1" +version = "4.3.0" [deps] BioGenerics = "47718e42-2ac5-11e9-14af-e5595289c2ea" diff --git a/src/select.jl b/src/select.jl index 9b3a7e8..2cadd75 100644 --- a/src/select.jl +++ b/src/select.jl @@ -436,10 +436,19 @@ allselector(el) = true # Acts as a function when used within typical julia filtering functions # by converting a string selection into a query call -struct Select <: Function - sel::String +struct Select{Q} <: Function + query_string::String + query::Q end -(s::Select)(at) = apply_query(parse_query(s.sel), at) + +function Select(query_string::AbstractString) + query = parse_query(query_string) + return Select(query_string, query) +end + +(s::Select)(at) = apply_query(s.query, at) + +Base.show(io::IO, ::MIME"text/plain", s::Select) = print(io, """Select("$(s.query_string)")""") # # Parse selection string allowing interpolation in sel macro: @@ -467,7 +476,6 @@ macro sel_str(s) return esc(ex) end - const operators = ( "=" => (x, y) -> isequal(x, y), "<" => (x, y) -> isless(x, y), diff --git a/test/runtests.jl b/test/runtests.jl index 86603b4..4bc7e8f 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -942,9 +942,14 @@ end @test length(collectmodels(struc, sel"model 1")) == 1 @test length(collectmodels(struc, sel"model 2")) == 0 - @test_throws ArgumentError collectatoms(struc, sel"abc") # Invalid selection syntax - @test_throws ArgumentError collectatoms(struc, sel"index = A") # Invalid value type - @test_throws ArgumentError collectatoms(struc, sel"resnum C") + @test_throws ArgumentError collectatoms(struc, BioStructures.Select("abc")) # Invalid selection syntax + @test_throws ArgumentError collectatoms(struc, BioStructures.Select("index = A")) # Invalid value type + @test_throws ArgumentError collectatoms(struc, BioStructures.Select("resnum C")) + + # Test show method for @sel_str + buff = IOBuffer() + show(buff, MIME"text/plain"(), sel"name CA and resnum 1") + @test String(take!(buff)) == """Select("name CA and resnum 1")""" end @testset "PDB reading" begin