@@ -75,16 +75,20 @@ Call `MMCIFDict` with a filepath or stream to read the dictionary from that
75
75
source.
76
76
The keyword argument `gzip` (default `false`) determines if the input is gzipped.
77
77
"""
78
- struct MMCIFDict <: AbstractDict{String , Vector{String }}
79
- dict:: Dict{String , Vector{String }}
78
+ struct MMCIFDict{K <: AbstractString } <: AbstractDict{K , Vector{K }}
79
+ dict:: Dict{K , Vector{K }}
80
80
end
81
81
82
- MMCIFDict () = MMCIFDict (Dict ())
82
+ MMCIFDict {K} () where K<: AbstractString = MMCIFDict {K} (Dict {K,Vector{K}} ())
83
+ MMCIFDict () = MMCIFDict {String} ()
84
+
85
+ MMCIFDict (d:: AbstractDict{K, Vector{K}} ) where K<: AbstractString = MMCIFDict {K} (d)
86
+ MMCIFDict (d:: AbstractDict ) = MMCIFDict {String} (Dict (d))
83
87
84
88
Base. getindex (mmcif_dict:: MMCIFDict , field:: AbstractString ) = mmcif_dict. dict[field]
85
89
86
90
function Base. setindex! (mmcif_dict:: MMCIFDict ,
87
- val:: AbstractVector{<:String } ,
91
+ val:: AbstractVector{<:AbstractString } ,
88
92
field:: AbstractString )
89
93
mmcif_dict. dict[field] = val
90
94
return mmcif_dict
@@ -147,7 +151,7 @@ splitline(s::AbstractString) = splitline!(String[], s) # mostly for testing
147
151
148
152
# Get tokens from a mmCIF file
149
153
function tokenizecif (f:: IO )
150
- tokens = String[]
154
+ tokens = SubString{ String} []
151
155
for line in eachline (f)
152
156
if startswith (line, " #" )
153
157
continue
172
176
# This will fail if there is only a single atom record in the file
173
177
# and it is not in the loop format
174
178
function tokenizecifstructure (f:: IO )
175
- tokens = String[]
179
+ tokens = SubString{ String} []
176
180
reading = false
177
181
in_keys = true
178
182
category_groups = [" _atom_site." , " _struct_conf." ]
@@ -218,14 +222,14 @@ end
218
222
219
223
# Read a mmCIF file into a MMCIFDict
220
224
function MMCIFDict (f:: IO ; gzip:: Bool = false )
221
- mmcif_dict = MMCIFDict ()
222
225
if gzip
223
226
gz = GzipDecompressorStream (f)
224
227
tokens = tokenizecif (gz)
225
228
close (gz)
226
229
else
227
230
tokens = tokenizecif (f)
228
231
end
232
+ mmcif_dict = MMCIFDict {eltype(tokens)} ()
229
233
# Data label token is read first
230
234
if length (tokens) == 0
231
235
return mmcif_dict
@@ -236,16 +240,16 @@ function MMCIFDict(f::IO; gzip::Bool=false)
236
240
end
237
241
238
242
# Add tokens to a mmCIF dictionary
239
- function populatedict! (mmcif_dict:: MMCIFDict , tokens:: AbstractVector{<:AbstractString} )
243
+ function populatedict! (mmcif_dict:: MMCIFDict{K} , tokens:: AbstractVector{<:AbstractString} ) where K <: AbstractString
240
244
key = " "
241
- keys = String []
245
+ keys = K []
242
246
loop_flag = false
243
247
i = 0 # Value counter
244
248
n = 0 # Key counter
245
249
for token in tokens
246
250
if token == " loop_" || token == " LOOP_"
247
251
loop_flag = true
248
- keys = String []
252
+ keys = K []
249
253
i = 0
250
254
n = 0
251
255
continue
@@ -258,7 +262,7 @@ function populatedict!(mmcif_dict::MMCIFDict, tokens::AbstractVector{<:AbstractS
258
262
if i > 0
259
263
loop_flag = false
260
264
else
261
- mmcif_dict[token] = String []
265
+ mmcif_dict[token] = K []
262
266
push! (keys, token)
263
267
n += 1
264
268
continue
@@ -290,14 +294,14 @@ function Base.read(input::IO,
290
294
run_dssp:: Bool = false ,
291
295
run_stride:: Bool = false ,
292
296
gzip:: Bool = false )
293
- mmcif_dict = MMCIFDict ()
294
297
if gzip
295
298
gz = GzipDecompressorStream (input)
296
299
tokens = tokenizecifstructure (gz)
297
300
close (gz)
298
301
else
299
302
tokens = tokenizecifstructure (input)
300
303
end
304
+ mmcif_dict = MMCIFDict {eltype(tokens)} ()
301
305
populatedict! (mmcif_dict, tokens)
302
306
return MolecularStructure (
303
307
mmcif_dict;
@@ -673,13 +677,13 @@ end
673
677
Write multiple `MMCIFDict`s as a `Dict{String, MMCIFDict}` to a filepath or stream.
674
678
The keyword argument `gzip` (default `false`) determines if the output is gzipped.
675
679
"""
676
- function writemultimmcif (filepath:: AbstractString , cifs:: Dict{String, MMCIFDict} ; gzip:: Bool = false )
680
+ function writemultimmcif (filepath:: AbstractString , cifs:: Dict{String, <: MMCIFDict} ; gzip:: Bool = false )
677
681
open (filepath, " w" ) do f
678
682
writemultimmcif (f, cifs; gzip= gzip)
679
683
end
680
684
end
681
685
682
- function writemultimmcif (io:: IO , cifs:: Dict{String, MMCIFDict} ; gzip:: Bool = false )
686
+ function writemultimmcif (io:: IO , cifs:: Dict{String, <: MMCIFDict} ; gzip:: Bool = false )
683
687
if gzip
684
688
io = GzipCompressorStream (io)
685
689
end
0 commit comments