Skip to content

Commit 6792ca9

Browse files
authored
Merge pull request #66 from m3g/append_to_pdb
implement append option to write_pdb
2 parents e9d4a87 + 3d1af10 commit 6792ca9

File tree

2 files changed

+71
-25
lines changed

2 files changed

+71
-25
lines changed

Project.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "PDBTools"
22
uuid = "e29189f1-7114-4dbd-93d0-c5673a921a58"
33
authors = ["Leandro Martinez <[email protected]>"]
4-
version = "2.6.1-DEV"
4+
version = "2.7.0-DEV"
55

66
[deps]
77
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"

src/read_write/write_pdb.jl

+70-24
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,66 @@
11
"""
2-
write_pdb(filename::String, atoms::AbstractVector{<:Atom}, selection; header=:auto, footer=:auto)
2+
write_pdb(filename::String, atoms::AbstractVector{<:Atom}, [selection]; header=:auto, footer=:auto, append=false)
33
44
Write a PDB file with the atoms in `atoms` to `filename`. The `selection` argument is a string
55
that can be used to select a subset of the atoms in `atoms`. For example, `write_pdb("test.pdb", atoms, "name CA")`.
66
7-
The `header` and `footer` arguments can be used to add a header and footer to the PDB file. If `header` is `:auto`,
8-
then a header will be added with the number of atoms in `atoms`. If `footer` is `:auto`, then a footer will be added
9-
with the "END" keyword. Either can be set to `nothing` if no header or footer is desired.
7+
# Arguments
8+
9+
- `filename::String`: The name of the file to write.
10+
- `atoms::AbstractVector{<:Atom}`: The atoms to write to the file.
11+
12+
# Optional positional argument
13+
14+
- `selection::String`: A selection string to select a subset of the atoms in `atoms`.
15+
16+
# Keyword arguments
17+
18+
- `header::Union{String, Nothing}=:auto`: The header to add to the PDB file. If `:auto`, a header will be added with the number of atoms in `atoms`.
19+
- `footer::Union{String, Nothing}=:auto`: The footer to add to the PDB file. If `:auto`, a footer will be added with the "END" keyword.
20+
- `append::Bool=false`: If `true`, the atoms will be appended to the file instead of overwriting it.
21+
22+
!!! compat
23+
The `append` keyword argument is available in PDBTools.jl v2.7.0 and later.
1024
1125
"""
12-
function write_pdb(filename::String, atoms::AbstractVector{<:Atom}, selection::String; header=:auto, footer=:auto)
26+
function write_pdb(
27+
filename::String,
28+
atoms::AbstractVector{<:Atom},
29+
selection::String;
30+
header=:auto,
31+
footer=:auto,
32+
append=false,
33+
)
1334
query = parse_query(selection)
14-
write_pdb(filename, atoms, only=atom -> apply_query(query, atom); header, footer)
35+
write_pdb(filename, atoms, only=atom -> apply_query(query, atom); header, footer, append)
1536
end
1637

17-
function write_pdb(filename::String, atoms::AbstractVector{<:Atom}; only::Function=all, header=:auto, footer=:auto)
18-
file = open(expanduser(filename), "w")
19-
if header == :auto
20-
curr_date = Dates.format(Dates.today(), "dd-u-yy")
21-
header = "PDBTools.jl - $(length(atoms)) atoms"
22-
println(file, @sprintf "%-10s%-40s%9s" "HEADER" header curr_date)
23-
elseif header !== nothing
24-
println(file, header)
25-
end
26-
for atom in atoms
27-
if only(atom)
28-
println(file, write_pdb_atom(atom))
38+
function write_pdb(
39+
filename::String, atoms::AbstractVector{<:Atom};
40+
only::Function=all,
41+
append=false,
42+
header=:auto,
43+
footer=:auto,
44+
)
45+
open(expanduser(filename), append ? "a" : "w") do io
46+
if header == :auto
47+
curr_date = Dates.format(Dates.today(), "dd-u-yy")
48+
header = "PDBTools.jl - $(length(atoms)) atoms"
49+
println(io, @sprintf "%-10s%-40s%9s" "HEADER" header curr_date)
50+
elseif header !== nothing
51+
println(io, header)
52+
end
53+
for atom in atoms
54+
if only(atom)
55+
println(io, write_pdb_atom(atom))
56+
end
57+
end
58+
if footer == :auto
59+
println(io, "END")
60+
elseif footer !== nothing
61+
println(io, footer)
2962
end
3063
end
31-
if footer == :auto
32-
println(file, "END")
33-
elseif footer !== nothing
34-
println(file, footer)
35-
end
36-
close(file)
3764
end
3865

3966
@testitem "write_pdb" begin
@@ -46,6 +73,25 @@ end
4673
f1 = readdlm(PDBTools.SMALLPDB, '\n', header=true)
4774
f2 = readdlm(tmpfile, '\n', header=true)
4875
@test f1[1] == f2[1]
76+
# test selection
77+
write_pdb(tmpfile, pdb, "name CA")
78+
f3 = read_pdb(tmpfile)
79+
@test length(f3) == 3
80+
# test header and footer
81+
write_pdb(tmpfile, pdb, "name CA"; header="HEADER test", footer="END test")
82+
s = split(String(read(tmpfile)))
83+
@test (s[begin],s[begin+1]) == ("HEADER", "test")
84+
@test (s[end-1],s[end]) == ("END", "test")
85+
# test append
86+
append_pdb = tempname() * ".pdb"
87+
write_pdb(append_pdb, pdb; append=true)
88+
pdb1 = read_pdb(append_pdb)
89+
@test length(eachmodel(pdb1)) == 1
90+
@test length(pdb1) == 35
91+
write_pdb(append_pdb, pdb; append=true)
92+
pdb2 = read_pdb(append_pdb)
93+
@test length(eachmodel(pdb2)) == 2
94+
@test length(pdb2) == 70
4995
end
5096

5197
#

0 commit comments

Comments
 (0)