Skip to content

Commit

Permalink
parse query on Select object creation (#61)
Browse files Browse the repository at this point in the history
* parse query on Select object creation

* fix Select test: the macro sel throws a `LoadError` before the `ArgumentError`.

* remove unnecessary object creation in test
  • Loading branch information
lmiq authored Nov 23, 2024
1 parent a98a59d commit 29d70e2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
13 changes: 9 additions & 4 deletions src/select.jl
Original file line number Diff line number Diff line change
Expand Up @@ -436,11 +436,16 @@ 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)")""")

"String selection syntax."
macro sel_str(str)
Expand Down
11 changes: 8 additions & 3 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -937,9 +937,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
Expand Down

0 comments on commit 29d70e2

Please sign in to comment.