-
Notifications
You must be signed in to change notification settings - Fork 2
/
input.jl
1571 lines (1466 loc) · 58.9 KB
/
input.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
## Functions to handle input files.
import Chemfiles
using Statistics: std
include("clustering.jl")
include("guessbonds.jl")
"""
parse_cif(file_path)
Parse a CIF file and return a dictionary where each identifier (without the
starting '_' character) is linked to its value.
Values are either a string or a vector of string (if defined in a loop).
"""
function parse_cif(file)
name = splitext(basename(file))[1]
all_data = Dict{String, Union{String, Vector{String}}}()
inloop = false
loopisspecified = false
loopspec = String[]
loop_n = 0
l = read(file, String)
i, j, x = nextword(l, 0)
lastword = ""
while i != 0
if inloop
if !loopisspecified
if l[i] != '_' # This indicates the start of the values
loop_n = length(loopspec)
iszero(loop_n) && error("invalid _loop: followed by no field (starting with _)")
loopisspecified = true
else # The identifier is part of the loop specification
push!(loopspec, l[i+1:j])
all_data[l[i+1:j]] = String[]
i, j, x = nextword(l, x); continue
end
end
# From this point, the loop has been specified
@toggleassert loopisspecified
if startswith(l[i:j], "data")
inloop = false
@iferror if haskey(all_data, "atom_site_fract_x")
@error lazy"The CIF file $name may contain multiple inputs: only keeping the last one."
end
i, j, x = nextword(l, x)
continue
end
if l[i] != '_' && l[i:j] != "loop_"
for k in 1:loop_n
push!(all_data[loopspec[k]], l[i:j])
i, j, x = nextword(l, x)
end
continue
end
if l[i:j] == "loop_"
loopisspecified = false
loopspec = String[]
i, j, x = nextword(l, x); continue
end
# This point can only be reached if we just quitted a loop
inloop = false
end
@toggleassert !inloop
if l[i] == '_' # Simple identifier definition
next_i, next_j, next_x = nextword(l, x)
@toggleassert next_i != 0
lastword = l[i+1:j]
newword = l[next_i:next_j]
newword != "?" && (all_data[lastword] = newword)
x = next_x
else
if l[i:j] == "loop_"
inloop = true
loopisspecified = false
loopspec = String[]
lastword = ""
elseif j-i ≥ 4 && l[i:i+4] == "data_"
@iferror if haskey(all_data, "data") && haskey(all_data, "atom_site_fract_x")
@error lazy"The CIF file $name may contain multiple inputs: only keeping the last one."
end
all_data["data"] = l[i+5:j]
else
complete_lastword::String = get(all_data, lastword, "")
if complete_lastword == ""
k::Int = something(findprev(isequal('\n'), l, i), 0)
n::Int = count("\n", l[1:k]) + 1
error(lazy"Unknown word \"$(l[i:j])\" at line $n, position $(i-k):$(j-k)")
end
all_data[lastword] = complete_lastword*' '*l[i:j]
end
end
i, j, x = nextword(l, x)
end
all_data["__name"] = name
return all_data
end
function tryparsestrip(T, s, default=nothing)
s = s[end] == ')' ? s[1:prevind(s, findlast('(', s))] : s
x = tryparse(T, s)
if x isa T
return x
end
return default
end
function parsestrip(T, s)
x = tryparsestrip(T, s)
if x isa T
return x
end
throw(ArgumentError(lazy"Invalid CIF file (cannot parse \"$s\" as a $T)."))
end
parsestrip(s) = parsestrip(BigFloat, s)
function popstring!(parsed, name)
if !haskey(parsed, name)
throw(ArgumentError(lazy"Invalid CIF file (cannot find \"_$name\" field)."))
end
x = pop!(parsed, name)
if x isa String
return x
end
return only(x::Vector{String})
end
function popvecstring!(parsed, name)
if !haskey(parsed, name)
throw(ArgumentError(lazy"Invalid CIF file (cannot find \"_$name\" field in loop)."))
end
x = pop!(parsed, name)
if x isa String
return String[x]
end
return x::Vector{String}
end
"""
CIF(file_path::AbstractString)
Make a CIF object out of the parsed file.
"""
CIF(file::AbstractString, label_for_type::Bool=false) = CIF(parse_cif(file), label_for_type)
function CIF(parsed::Dict{String, Union{Vector{String},String}}, label_for_type::Bool=false)
natoms = length(parsed["atom_site_label"]::Vector{String})
equivalentpositions = haskey(parsed, "symmetry_equiv_pos_as_xyz") ?
popvecstring!(parsed, "symmetry_equiv_pos_as_xyz") :
haskey(parsed, "space_group_symop_operation_xyz") ?
popvecstring!(parsed, "space_group_symop_operation_xyz") : String[]
all(contains('?'), equivalentpositions) && empty!(equivalentpositions)
initiallyemptyequivalentpositions = isempty(equivalentpositions)
refid = find_refid(equivalentpositions)
hall = find_hall_number(
haskey(parsed, "space_group_name_Hall") ?
popstring!(parsed, "space_group_name_Hall") :
haskey(parsed, "symmetry_space_group_name_Hall") ?
popstring!(parsed, "symmetry_space_group_name_Hall") : "",
haskey(parsed, "space_group_name_H-M_alt") ?
popstring!(parsed, "space_group_name_H-M_alt") :
haskey(parsed, "symmetry_space_group_name_H-M") ?
popstring!(parsed, "symmetry_space_group_name_H-M") : "",
haskey(parsed, "symmetry_Int_Tables_number") ?
parse(Int, popstring!(parsed, "symmetry_Int_Tables_number")) :
haskey(parsed, "space_group.IT_number") ?
parse(Int, popstring!(parsed, "space_group.IT_number")) : 0)
cell = Cell(hall, (parsestrip(popstring!(parsed, "cell_length_a")),
parsestrip(popstring!(parsed, "cell_length_b")),
parsestrip(popstring!(parsed, "cell_length_c"))),
(parsestrip(popstring!(parsed, "cell_angle_alpha")),
parsestrip(popstring!(parsed, "cell_angle_beta")),
parsestrip(popstring!(parsed, "cell_angle_gamma"))),
parse.(EquivalentPosition, equivalentpositions, Ref(refid)))
haskey(parsed, "symmetry_equiv_pos_site_id") && pop!(parsed, "symmetry_equiv_pos_site_id")
haskey(parsed, "symmetry_cell_setting") && pop!(parsed, "symmetry_cell_setting")
if initiallyemptyequivalentpositions
@ifwarn @warn "Missing _symmetry_equiv_pos_as_xyz and _space_group_symop_operation_xyz fields, will rely on other symmetry indications."
else
removed_identity = false
for i in eachindex(cell.equivalents)
eq = cell.equivalents[i]
if isone(eq.mat) && iszero(eq.ofs)
deleteat!(cell.equivalents, i)
removed_identity = true
break
end
end
if !removed_identity
@ifwarn @warn lazy"""The _symmetry_equiv_pos_as_xyz field did not contain the \"$(join(refid, ", "))\" entry."""
end
end
labels = popvecstring!(parsed, "atom_site_label")
_symbols = haskey(parsed, "atom_site_type_symbol") ?
popvecstring!(parsed, "atom_site_type_symbol") : copy(labels)
symbols = Vector{String}(undef, length(_symbols))
@inbounds for k in 1:length(_symbols)
x = label_for_type ? labels[k] : _symbols[k]
i = findfirst(!isletter, x)
symbols[k] = uppercase(x[1])*lowercase(isnothing(i) ? x[2:end] : x[2:i-1])
end
pos_x = popvecstring!(parsed, "atom_site_fract_x")
pos_y = popvecstring!(parsed, "atom_site_fract_y")
pos_z = popvecstring!(parsed, "atom_site_fract_z")
types = Symbol[]
pos = Matrix{Float64}(undef, 3, natoms)
correspondence = Dict{String, Int}()
alreadywarned = Set{String}()
for i in 1:natoms
if get!(correspondence, labels[i], i) != i
correspondence[labels[i]] = 0 # indicates an atom appearing multiple times
@ifwarn begin
lab = labels[i]
if lab ∉ alreadywarned
@warn lazy"Atom of label \"$lab\" appears multiple time in the input CIF description."
push!(alreadywarned, lab)
end
end
end
push!(types, Symbol(symbols[i]))
pos[:,i] = parsestrip.(Float64, [pos_x[i], pos_y[i], pos_z[i]])
pos[:,i] .-= floor.(Int, pos[:,i])
end
@ifwarn begin
if !isempty(alreadywarned)
if any(x -> length(x) ≥ 1 && !isnumeric(last(x)), alreadywarned)
@info "Atom labels should be unique identifiers, you may want to add a number after the atom symbol to the label."
end
end
end
invids = sortperm(types)
types = types[invids]
ids = invperm(invids)
if haskey(parsed, "geom_bond_atom_site_label_1") &&
haskey(parsed, "geom_bond_atom_site_label_2")
bond_a = popvecstring!(parsed, "geom_bond_atom_site_label_1")
bond_b = popvecstring!(parsed, "geom_bond_atom_site_label_2")
dists = if haskey(parsed, "geom_bond_distance")
tryparsestrip.(Float32, popvecstring!(parsed, "geom_bond_distance"), -Inf32)
else
fill(-Inf32, length(bond_a))
end
bonds = [Tuple{Int,Float32}[] for _ in 1:natoms]
for i in 1:length(bond_a)
x = get(correspondence, bond_a[i], 0)
y = get(correspondence, bond_b[i], 0)
if x == 0 || y == 0
empty!(bonds)
missingatom = x == 0 ? bond_a[i] : bond_b[i]
@iferror @error lazy"""Atom $missingatom, used in a bond, has either zero or multiple placements in CIF file $(parsed["__name"]). This invalidates all bonds from the file, which will thus be discarded."""
break
end
d = 1.004f0*dists[i] # to avoid rounding errors
if isnan(d) || (d != -Inf32 && (d ≤ 0f0 || isinf(d)))
@iferror @error lazy"""Invalid bond distance of $d between atoms $(bond_a[i]) and $(bond_b[i]) in CIF file $(parsed["__name"])."""
continue
end
push!(bonds[x], (y, d))
push!(bonds[y], (x, d))
end
foreach(sortprune_bondlist!, bonds)
else
bonds = Vector{Tuple{Int,Float32}}[]
end
return CIF(parsed, cell, ids, types, pos, bonds::Vector{Vector{Tuple{Int,Float32}}})
end
"""
parse_arc(file)
Parse a .arc Systre archive such as the one used by the RCSR.
Return a pair `(flag, pairs)`.
`flag` is set if the archive corresponds to one generated by a compatible release
of CrystalNets. If unset, the genomes of the archive may not be the same as those
computed by CrystalNets for the same nets.
`pairs` is a `Dict{String,String}` whose entries have the form `genome => id` where `id`
is the name of the net and `genome` is the topological genome corresponding to this net
(given as a string of whitespace-separated values parseable by `PeriodicGraph`).
"""
function parse_arc(file)
pairs = Dict{String,String}()
curr_key = ""
counter = 1
firstline = readline(file)
flag = startswith(firstline, "Made by CrystalNets.jl V")
if flag
flag = CRYSTALNETS_ARCHIVE_VERSION == @view firstline[25:end]
end
for l in eachline(file)
if length(l) > 3 && l[1:3] == "key"
@toggleassert isempty(curr_key)
i = 4
while isspace(l[i])
i += 1
end
curr_key = l[i:end]
@toggleassert !haskey(pairs, curr_key)
elseif length(l) > 2 && l[1:2] == "id"
@toggleassert !isempty(curr_key)
i = 3
while isspace(l[i])
i += 1
end
pairs[curr_key] = l[i:end]
curr_key = ""
end
end
return flag, pairs
end
"""
parse_arcs(file)
Parse a folder containing .arc Systre archives such as the one used by the RCSR.
Return a pair `(flag, pairs)` with the same convention than [`parse_arc`](@ref).
"""
function parse_arcs(path, lesser_priority=["epinet"])
combine(x, y) = x * ", " * y
combineignore(x, _) = x
dict = Dict{String,String}()
flag = true
postprocess = Dict{String,String}[]
for f in readdir(path; sort=true)
arc_name, ext = splitext(f)
ext == ".arc" || continue
_flag, _dict = parse_arc(joinpath(path, f))
flag &= _flag
if arc_name ∈ lesser_priority
push!(postprocess, _dict)
else
mergewith!(combine, dict, _dict)
end
end
for _dict in postprocess
mergewith!(combineignore, dict, _dict)
end
return flag, dict
end
function expand_symmetry_cgd!(nodes::Vector{SVector{N,Float64}}, equivalents::Vector{EquivalentPosition{Float64}}, connectivity::Vector{Int}) where N
n = length(nodes)
for i in 1:n
node = nodes[i]
node3 = N==2 ? SA[node[1], node[2], 0.0] : node
for eq in equivalents
newpos = round.(eq(node3); digits=10) # transform -1e-15 into 0.0
newpos = newpos .- floor.(newpos)
cmppos = N==2 ? SA[newpos[1], newpos[2]] : newpos
minimum(Base.Fix1(dist2, cmppos), nodes) < 1e-8 && continue
push!(nodes, cmppos)
push!(connectivity, connectivity[i])
end
end
nothing
end
struct ClosestSymmetricImage{N,T} <: Function
nodes::Vector{SVector{N,Float64}}
x::PeriodicVertex{N}
eq::EquivalentPosition{T}
end
function (csi::ClosestSymmetricImage{N,T})() where {N,T}
node = csi.nodes[csi.x.v] .+ csi.x.ofs
pos = N==2 ? SA[node[1], node[2], 0.0] : node
newpos = round.(csi.eq(pos); digits=10) # transform -1e-15 into 0.0
ofs = floor.(Int, newpos)
newpos = newpos .- ofs
cmppos = N==2 ? SA[newpos[1], newpos[2]] : newpos
rsrc, newsrc = findmin(Base.Fix1(dist2, cmppos), csi.nodes)
# rsrc > 0.01 && @show csi.nodes csi.x csi.eq node pos newpos ofs cmppos
@assert rsrc < 1e-9
PeriodicVertex{N}(newsrc, N==2 ? SA[ofs[1], ofs[2]] : ofs)
end
function expand_symmetry_cgd!(edgelist::Vector{PeriodicEdge{N}}, nodes::Vector{SVector{N,Float64}}, equivalents::Vector{EquivalentPosition{Float64}}) where N
n = length(edgelist)
memories = [Dict{PeriodicVertex{N},PeriodicVertex{N}}() for _ in equivalents]
for i in 1:n
src, dst = edgelist[i]
for (j, eq) in enumerate(equivalents)
memory = memories[j]
src_v, src_ofs = get!(ClosestSymmetricImage(nodes, PeriodicVertex{N}(src), eq), memory, PeriodicVertex{N}(src))
dst_v, dst_ofs = get!(ClosestSymmetricImage(nodes, dst, eq), memory, dst)
push!(edgelist, PeriodicEdge{N}(src_v, PeriodicVertex{N}(dst_v, dst_ofs.-src_ofs)))
end
end
end
function retrieve_node(nodes::Vector{SVector{N,Float64}}, pos::SVector{N,Float64}) where N
pos = round.(pos; digits=10) # transform -1e-15 into 0.0
ofs = floor.(Int, pos)
find_nearest(nodes, pos .- ofs), ofs
end
function _parse_cgd_crystal!(iterator, ret, current_edgelist2D, current_edgelist3D, path, strict)
current_name = ""
local nodes#::Union{Vector{SVector{2,Float64}}, Vector{SVector{3,Float64}}}
local current_edgelist
local PE
names = Dict{String,Int}()
connectivity = Int[]
hall = 1
hallflag = false
cell = Cell{Float64}()
lastkeyw = ""
dimension = 0
for _l in iterator
l = strip(lowercase(_l))
comment = findfirst('#', l)
if !isnothing(comment)
l = strip(@view l[1:prevind(l, comment)])
end
isempty(l) && continue
splits = split(l; limit=2)
keyw = first(splits)
if keyw == "name" || keyw == "id" || keyw == "key"
current_name = last(splits)
elseif keyw == "group"
hall = PeriodicGraphEmbeddings.find_hall_number("", last(splits), 0, false)
if hall == 1 && last(splits) != "p1"
@info lazy"Unreadable symmetry $(last(splits)): skipping $current_name"
@goto skip
end
if cell != Cell{Float64}()
cell = Cell{Float64}(hall, cell_parameters(cell.mat)...)
end
hallflag = true
elseif keyw == "cell"
_a, _b, unk... = split(last(splits))
a = parse(Float64, _a); b = parse(Float64, _b)
if length(unk) == 1
if hall != 1
@info lazy"2-dimensional nets with symmetries are not currently accepted: skipping $current_name"
@goto skip
end
γ = parse(Float64, only(unk))
c = 10.0
α = β = 90.0
dimension = 2
current_edgelist = current_edgelist2D
PE = PeriodicEdge2D
nodes = SVector{2,Float64}[]
else
c, α, β, γ = parse.(Float64, unk)
dimension = 3
current_edgelist = current_edgelist3D
PE = PeriodicEdge3D
nodes = SVector{3,Float64}[]
end
cell = Cell{Float64}(hall, (a, b, c), (α, β, γ))
elseif keyw == "node" || keyw == "atom"
@label handle_node
if !@isdefined(nodes)
if strict
@error "Error while parsing $path at \"$current_name\": missing cell"
@goto skip
end
dimension = length(split(l)) - 3
if dimension == 3
current_edgelist = current_edgelist3D
PE = PeriodicEdge3D
nodes = SVector{3,Float64}[]
cell = Cell{Float64}(hall, (NaN, NaN, NaN), (NaN, NaN, NaN))
end
end
@assert @isdefined(nodes) && hallflag && (!isempty(nodes) || cell != Cell{Float64}())
if length(splits) == 2
name, conn, _pos... = split(last(splits))
haskey(names, name) && error(lazy"Error while parsing $path at \"$current_name\": Multiple vertices have the same name $name")
push!(connectivity, parse(Int, conn))
newnode = round.(parse.(Float64, dimension == 2 ? SVector{2}(_pos) : SVector{3}(_pos)); digits=10)
push!(nodes, newnode .- floor.(newnode))
end # else, the line is just "node" hence the following are the actual nodes
lastkeyw = "node"
elseif keyw == "edge"
@label handle_edge
@assert @isdefined(nodes) && !isempty(nodes)
if length(splits) == 2
_content = split(last(splits))
isempty(current_edgelist) && expand_symmetry_cgd!(nodes, cell.equivalents, connectivity)
push!(current_edgelist, if length(_content) == 2
PE(parse.(Int, _content)..., dimension==2 ? (0,0) : (0,0,0))
elseif length(_content) == 1+dimension
srcA = parse(Int, _content[1])
dstA_v = parse.(Float64, (dimension==2 ? SVector{2,Float64} : SVector{3,Float64})(@view _content[2:end]))
dstA, ofsA = retrieve_node(nodes, dstA_v)
PE(srcA, dstA, ofsA)
else
@assert length(_content) == 2*dimension
srcB_v = parse.(Float64, (dimension==2 ? SVector{2,SubString{String}} : SVector{3,SubString{String}})(@view _content[1:dimension]))
dstB_v = parse.(Float64, (dimension==2 ? SVector{2,SubString{String}} : SVector{3,SubString{String}})(@view _content[(dimension+1):end]))
srcB, ofsBstart = retrieve_node(nodes, srcB_v)
dstB, ofsBstop = retrieve_node(nodes, dstB_v)
# if srcB == dstB && ofsBstop == ofsBstart
# @show current_name
# display(current_edgelist)
# display(nodes)
# @show srcB, ofsBstart
# println(parse.(Float64, _content[1:dimension]))
# println(parse.(Float64, _content[(dimension+1):end]))
# end
PE(srcB, dstB, ofsBstop .- ofsBstart)
end)
end
lastkeyw = "edge"
elseif keyw == "end"
expand_symmetry_cgd!(current_edgelist, nodes, cell.equivalents, )
g = dimension == 2 ? PeriodicGraph2D(current_edgelist2D) : PeriodicGraph3D(current_edgelist3D)
empty!(current_edgelist)
if strict
if nv(g) != length(nodes)
@error lazy"Error while parsing $path at \"$current_name\": Found a graph with only $(nv(g)) nodes instead of expected $(length(nodes))"
@goto skip
end
for (i, conn) in enumerate(connectivity)
if degree(g, i) != conn
@error lazy"Error while parsing $path at \"$current_name\": Invalid connectivity of $(degree(g, i)) found instead of $conn for node $i"
@goto skip
end
end
end
push!(ret, (current_name, g))
return dimension
else
if lastkeyw == "node"
splits = [l]
@goto handle_node
elseif lastkeyw == "edge"
splits = [l]
@goto handle_edge
end
@error lazy"Error while parsing $path at \"$current_name\": Unknown key: \"$keyw\""
@goto skip
end
end
error(lazy"Error while parsing $path at \"$current_name\": Starting \"CRYSTAL\" missing its corresponding \"END\"")
@label skip
for l2 in iterator
lowercase(strip(l2)) == "end" && break
end
return 0
end
function parse_cgd_lines!(edgelist::Vector{PeriodicEdge{D}}, iterator) where D
for _l in iterator
l = strip(_l)
isempty(l) && continue
lowercase(l) == "end" && break
src, dst, ofs... = parse.(Int, split(l))
push!(edgelist, PeriodicEdge{D}(src, dst, ofs))
end
nothing
end
function _parse_cgd_periodicgraph!(iterator, ret, current_edgelist2D, current_edgelist3D, path)
edgeready = false
current_name = ""
for _l in iterator
l = strip(_l)
comment = findfirst('#', l)
if !isnothing(comment)
l = strip(@view l[1:prevind(l, comment)])
end
isempty(l) && continue
splits = split(l; limit=2)
keyw = lowercase(first(splits))
if keyw == "name" || keyw == "id" || keyw == "key"
current_name = last(splits)
elseif keyw == "edges"
edgeready = true
elseif edgeready
@assert keyw != "edge"
src, dst, ofs... = parse.(Int, split(l))
if length(ofs) == 2
push!(current_edgelist2D, PeriodicEdge2D(src, dst, SVector{2,Int}(ofs)))
parse_cgd_lines!(current_edgelist2D, iterator)
push!(ret, (current_name, PeriodicGraph2D(current_edgelist2D)))
empty!(current_edgelist2D)
return 2
else
@assert length(ofs) == 3
push!(current_edgelist3D, PeriodicEdge3D(src, dst, SVector{3,Int}(ofs)))
parse_cgd_lines!(current_edgelist3D, iterator)
push!(ret, (current_name, PeriodicGraph3D(current_edgelist3D)))
empty!(current_edgelist3D)
return 3
end
else
error(lazy"Error while parsing $path at \"$current_name\": Unknown key: \"$keyw\"")
end
end
error(lazy"Error while parsing $path at \"$current_name\": Starting \"CRYSTAL\" missing its corresponding \"END\"")
end
"""
parse_cgd(path::AbstractString; strict=true)
Parse a .cgd Systre configuration data file such as the one used by the RCSR.
Return a list of `id => g` where `id` is the name of the structure and `g` is its
`PeriodicGraph`.
If `strict` is set, refuse structures that seem to contain errors (inconsistent
connectivity, missing cell, incorrect number of vertices) and print an `@error` instead.
"""
function parse_cgd(path::AbstractString; strict=true)
ret = Tuple{String,Union{PeriodicGraph2D,PeriodicGraph3D}}[]
current_edgelist2D = PeriodicEdge2D[]
current_edgelist3D = PeriodicEdge3D[]
iterator = eachline(path)
for _l in iterator
l = lowercase(strip(_l))
comment = findfirst('#', l)
if !isnothing(comment)
l = strip(@view l[1:prevind(l, comment)])
end
isempty(l) && continue
if l == "periodic_graph"
@assert isempty(current_edgelist2D) && isempty(current_edgelist3D)
_parse_cgd_periodicgraph!(iterator, ret, current_edgelist2D, current_edgelist3D, path)
elseif l == "crystal"
@assert isempty(current_edgelist2D) && isempty(current_edgelist3D)
_parse_cgd_crystal!(iterator, ret, current_edgelist2D, current_edgelist3D, path, strict)
else
error(lazy"Error while parsing $path: Misplaced \"$l\"")
end
end
ret
end
function parse_atom_name(name::AbstractString)
firstsep = findfirst(x -> ispunct(x) || isspace(x) || isnumeric(x), name)
symb::Symbol = if firstsep isa Nothing
Symbol(name)
else
firstsep::Int
if firstsep != 1 && !any(isuppercase, @view name[nextind(name, firstsep):end])
Symbol(name[1:prevind(name, firstsep)])
else
Symbol(name)
end
end
initstr = String(symb)
if get(atomic_numbers, symb, 0) != 0
return initstr
end
if length(initstr) > 2
str = initstr[1:2]
if get(atomic_numbers, Symbol(str), 0) != 0
return str
end
end
if get(atomic_numbers, Symbol(initstr[1]), 0) != 0
return string(initstr[1])
end
return initstr
end
# function parse_atom(name)
# atom = Chemfiles.Atom(name)
# set_type!(atom, parse_atom_name(name))
# return atom
# end
@static if !isdefined(Chemfiles, :atoms) # up to 0.9.3 included
function chem_atoms(residue::Chemfiles.Residue)
count = size(residue)
result = Array{UInt64}(undef, count)
Chemfiles.__check(Chemfiles.lib.chfl_residue_atoms(Chemfiles.__const_ptr(residue), pointer(result), count))
return result
end
else
const chem_atoms = Chemfiles.atoms
end
function attribute_residues(residues, n, assert_use_existing_residues)
m = length(residues)
atoms_in_residues = m == 0 ? 0 : sum(length(chem_atoms(r)) for r in residues)
@toggleassert atoms_in_residues <= n
if atoms_in_residues < n
if assert_use_existing_residues
throw(ArgumentError("""
Cannot use existing residues as vertices because not all atoms have an associated residue.
To fix this, either assign a residue to each atom or provide another way to detect the vertices.
"""))
end
if atoms_in_residues > 0
@ifwarn @warn "Some but not all atoms have an associated residue, so we cannot rely on existing residues"
end
return Int[]
end
attributions = zeros(Int, n)
for i_residue in 1:m
for atom in chem_atoms(residues[i_residue])
attributions[atom+1] = i_residue
end
end
return attributions
end
"""
check_collision(pos, mat)
Given a list of fractional coordinates `pos` and the matrix of the unit cell
`mat`, return a list of atoms that are suspiciously close to another atom
of the list. For each collision site, only one atom is not present in the
returned list.
"""
function check_collision(pos, mat)
n = length(pos)
toremove = Int[]
n == 0 && return toremove
pd2 = PeriodicDistance2(mat)
for i in 1:n
posi = pos[i]
for j in (i+1):n
if pd2(posi, pos[j]) < 0.55^2
push!(toremove, j)
end
end
end
if !isempty(toremove)
@ifwarn @warn "This file contains multiple colliding atoms. Only one atom will be kept per site."
sort!(toremove)
unique!(toremove)
end
return toremove
end
"""
fix_atom_on_a_bond!(graph, pos, mat)
Remove bonds that are intercepted by an atom.
"""
function fix_atom_on_a_bond!(graph, pos, mat)
for i in vertices(graph)
neighs = neighbors(graph, i)
flag = length(neighs) > 1
while flag
flag = false
p = pos[i]
for (j1, u1) in enumerate(neighs)
vec1 = mat * (pos[u1.v] .+ u1.ofs .- p)
for j2 in (j1+1):length(neighs)
u2 = neighs[j2]
vec2 = mat * (pos[u2.v] .+ u2.ofs .- p)
if angle(vec1, vec2) < 15
furthest = norm(vec1) < norm(vec2) ? u2 : u1
rem_edge!(graph, i, furthest)
flag = true
break
end
end
flag && break
end
end
end
end
"""
least_plausible_neighbours(Δs, n)
Find the positions of the n least probable neighbours of an atom, given the list
Δs of the distance between their position and that of the atom.
This function is highly empirical and should not be considered utterly reliable.
"""
function least_plausible_neighbours(Δs::Vector{Float64}, n::Int)
m = length(Δs)
m ≤ n && return collect(1:m) # may happen because H bonds cannot be removed
p::Vector{Int} = sortperm(Δs)
return p[end-n+1:end]
end
macro reduce_valence_to1()
return esc(quote
___neighs = neighbors(graph, i)
___m = length(___neighs)
if ___m > 1
___Δs = Float64[norm(mat * (pos[x.v] .+ x.ofs .- pos[i])) for x in ___neighs]
___toremove = least_plausible_neighbours(___Δs, ___m - 1)
___neighs = copy(___neighs) # otherwise the list is modified by rem_edge!
for v in ___toremove
rem_edge!(graph, PeriodicEdge3D(i, ___neighs[v]))
end
end
end)
end
macro reduce_valence(dofix, n1, n2, nm=0)
comparison = n1 == 0 ? :(___m ≤ $n2) : :($n1 ≤ ___m ≤ $n2)
invalidcond = n1 == 0 ? :(!$dofix) : :(!$dofix || ___m < $n1)
n2update = nm == 0 ? :(___n2 = $n2) : :(___n2 = $n2 + $nm*any(ismetal[atomic_numbers[types[___n.v]]] for ___n in ___neighs))
return esc(quote
___neighs = neighbors(graph, i)
$n2update
___m = length(___neighs)
$comparison && continue
($invalidcond) && push!(invalidatoms, t)
if $dofix && ___m > ___n2
___noHatoms = [x for x in ___neighs if types[x.v] !== :H && types[x.v] !== :D]
___Δs = Float64[norm(mat * (pos[x.v] .+ x.ofs .- pos[i])) for x in ___noHatoms]
___toremove = least_plausible_neighbours(___Δs, ___m - ___n2)
for v in ___toremove
rem_edge!(graph, PeriodicEdge3D(i, ___noHatoms[v]))
end
end
end)
end
"""
check_C_disorder!(graph, pos, types, idx, mat)
Check the presence of carbon cycle disorder (multiple cycles superposed).
If present, send a warning and allow the common carbons and the ends of the cycles to have
an additional bond out of the cycle (even if it overcomes the usual bond limits for a
carbon).
Also remove carbon-metal bonds longer than 2.4 Å.
"""
function check_C_disorder!(graph::PeriodicGraph{N}, pos, types, idx, mat) where {N}
neighs = neighbors(graph, idx)
removeedges = PeriodicVertex{N}[]
for u in neighs
if ismetal[get(atomic_numbers, types[u.v], 1)]
bondlength = Float64(norm(mat * (pos[u.v] .+ u.ofs .- pos[idx])))
if bondlength > 2.4
push!(removeedges, u)
end
end
end
if !isempty(removeedges)
for u in removeedges
rem_edge!(graph, PeriodicEdge(idx, u))
end
neighs = neighbors(graph, idx)
end
length(neighs) ≥ 5 || return false
carbons = PeriodicVertex3D[]
for x in neighs
ty = types[x.v]
if !ismetal[atomic_numbers[ty]]
ty == :C || return false
push!(carbons, x)
end
end
if length(carbons) > 5
Δs = Float64[norm(mat*(pos[x.v] .+ x.ofs .- pos[idx])) for x in neighs]
toremove = neighs[least_plausible_neighbours(Δs, length(carbons) - 5)]
for j in toremove
rem_edge!(graph, PeriodicEdge3D(idx, j))
end
setdiff!(carbons, toremove)
end
length(carbons) == 5 || return false
adjacency = MMatrix{5,5,Bool,25}(undef)
pd2 = PeriodicDistance2(mat)
for i in 1:5
posi = pos[carbons[i].v] .+ carbons[i].ofs
adjacency[i,i] = false
for j in (i+1):5
adjacency[i,j] = adjacency[j,i] = pd2(posi, pos[carbons[j].v], nothing, carbons[j].ofs) < 1.85^2
end
end
odd_one = false
num_neighs = sum(adjacency; dims=1)
for i in 1:5
if num_neighs[i] == 0
odd_one && return false
odd_one = true
end
end
return true
end
"""
fix_valence!(graph::PeriodicGraph3D, pos, types, passH, passO, passCN, mat, ::Val{dofix}, options) where {dofix}
Attempt to ensure that the coordinence of certain atoms are at least plausible
by removing some edges from the graph.
These atoms are H, halogens, O, N and C.
if `dofix` is set, actually modify the graph; otherwise, only emit a warning.
In both cases, return a list of atoms with invalid coordinence.
"""
function fix_valence!(graph::PeriodicGraph3D, pos, types, passH, passO, passCN, mat,
::Val{dofix}, options) where {dofix}
# Small atoms valence check
n = length(types)
invalidatoms = Set{Symbol}()
# First pass over H, since those are likely bonded to their closest neighbor
for i in passH
@reduce_valence_to1
end
for i in passO
t = :O
if options.structure == StructureType.Zeolite
@reduce_valence dofix 0 2
else
@reduce_valence dofix 0 2 2
end
end
anychecked = false
for i in passCN
t = types[i]
checked = t == :C && dofix && check_C_disorder!(graph, pos, types, i, mat)
checked || @reduce_valence dofix 2 4 1
anychecked |= checked
end
@iferror anychecked && @error lazy"Found what looks like a disordered carbon ring for $(options.name): use of disordered input is not advised, please double-check the detected topology or provide a clean input."
if !isempty(invalidatoms)
s = String.(collect(invalidatoms))
@ifwarn begin
endmsg = join(s, ", ", " and ")*" with invalid number of bonds."
if dofix
@warn "After attempted fix, found remaining "*endmsg
else
@info "Initial pass found "*endmsg
end
end
end
return invalidatoms
end
"""
sanitize_removeatoms!(graph::PeriodicGraph3D, pos, types, mat, options)
Special heuristics to remove atoms that seem to arise from an improper cleaning of the file.
Currently implemented:
- C atoms suspiciously close to metallic atoms.
- One of two identical metallic atoms suspiciously close to one another
TODO:
- O atoms with 4 coplanar bonds (warning only).
"""
function sanitize_removeatoms!(graph::PeriodicGraph3D, pos, types, mat, options)
toremove = Set{Int}()
flag = true
for (i, t) in enumerate(types)
if t === :O
@reduce_valence true 0 4
# at this point, the valence is 4 since @reduce_valence would continue otherwise
neighs = neighbors(graph, i)
p = pos[i]
lengths = [norm(mat * (pos[u.v] .+ u.ofs .- p)) for u in neighs]
if flag && any(>(2.6), lengths)
@iferror @error lazy"Very suspicious connectivity found for $(options.name)."
flag = false
end
else
at = get(atomic_numbers, t, nothing)
if at === nothing
if !options.label_for_type
@iferror @error lazy"Unrecognized atom type \"$t\" in $(options.name) will be considered a dummy atom."
push!(toremove, i)
end
elseif at isa Int && ismetal[at]
for u in neighbors(graph, i)
typu = types[u.v]
if typu === :C
u.v ∈ toremove && continue
bondlength = Float64(norm(mat * (pos[u.v] .+ u.ofs .- pos[i])))
bondlength > 1.45 && continue
@ifwarn if isempty(toremove)
@warn lazy"C suspiciously close to a metal (bond length: $bondlength) will be removed."
end
push!(toremove, u.v)
elseif typu == t && u.v > i
u.v ∈ toremove && continue
bondlength = Float64(norm(mat * (pos[u.v] .+ u.ofs .- pos[i])))
bondlength > 0.9 && continue
@ifwarn if isempty(toremove)
@warn lazy"$t atoms suspiciously close to one another(bond length: $bondlength). One will be removed."
end
push!(toremove, u.v)
end
end
end
end
end