Skip to content

Commit 5c702c4

Browse files
committed
implement append option to write_pdb
1 parent e9d4a87 commit 5c702c4

File tree

1 file changed

+60
-24
lines changed

1 file changed

+60
-24
lines changed

src/read_write/write_pdb.jl

+60-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,15 @@ end
4673
f1 = readdlm(PDBTools.SMALLPDB, '\n', header=true)
4774
f2 = readdlm(tmpfile, '\n', header=true)
4875
@test f1[1] == f2[1]
76+
append_pdb = tempname() * ".pdb"
77+
write_pdb(append_pdb, pdb; append=true)
78+
pdb1 = read_pdb(append_pdb)
79+
@test length(eachmodel(pdb1)) == 1
80+
@test length(pdb1) == 35
81+
write_pdb(append_pdb, pdb; append=true)
82+
pdb2 = read_pdb(append_pdb)
83+
@test length(eachmodel(pdb2)) == 2
84+
@test length(pdb2) == 70
4985
end
5086

5187
#

0 commit comments

Comments
 (0)