-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathctfile.f90
More file actions
568 lines (489 loc) · 18.7 KB
/
Copy pathctfile.f90
File metadata and controls
568 lines (489 loc) · 18.7 KB
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
! This file is part of mctc-lib.
!
! Licensed under the Apache License, Version 2.0 (the "License");
! you may not use this file except in compliance with the License.
! You may obtain a copy of the License at
!
! http://www.apache.org/licenses/LICENSE-2.0
!
! Unless required by applicable law or agreed to in writing, software
! distributed under the License is distributed on an "AS IS" BASIS,
! WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
! See the License for the specific language governing permissions and
! limitations under the License.
module mctc_io_read_ctfile
use mctc_env_accuracy, only : wp
use mctc_env_error, only : error_type, fatal_error
use mctc_io_convert, only : aatoau
use mctc_io_structure, only : structure_type, new
use mctc_io_structure_info, only : sdf_data, structure_info
use mctc_io_symbols, only : to_number, symbol_length
use mctc_io_utils, only : next_line, token_type, next_token, io_error, filename, &
read_token, read_next_token, to_string
implicit none
private
public :: read_sdf, read_molfile
contains
subroutine read_sdf(self, unit, error)
!> Instance of the molecular structure data
type(structure_type), intent(out) :: self
!> File handle
integer, intent(in) :: unit
!> Error handling
type(error_type), allocatable, intent(out) :: error
character(len=:), allocatable :: line
integer :: stat, lnum, pos
call read_molfile(self, unit, error)
if (allocated(error)) return
lnum = 0
stat = 0
do while(stat == 0)
call next_line(unit, line, pos, lnum, stat)
if (index(line, "$$$$") == 1) exit
end do
if (stat /= 0) then
call fatal_error(error, "Failed while reading SDF key-value pairs")
return
end if
end subroutine read_sdf
subroutine read_molfile(self, unit, error)
!> Instance of the molecular structure data
type(structure_type), intent(out) :: self
!> File handle
integer, intent(in) :: unit
!> Error handling
type(error_type), allocatable, intent(out) :: error
character(len=:), allocatable :: line
character(len=:), allocatable :: comment
integer :: stat, lnum, pos
integer :: number_of_atoms, number_of_bonds
character(len=2) :: sdf_dim
logical :: two_dim, v3k
type(token_type) :: token
lnum = 0
two_dim = .false.
call next_line(unit, comment, pos, lnum, stat)
call next_line(unit, line, pos, lnum, stat)
read(line, "(20x, a2)", iostat=stat) sdf_dim
if (stat == 0) then
two_dim = sdf_dim == "2D" .or. sdf_dim == "2d"
end if
call next_line(unit, line, pos, lnum, stat)
call next_line(unit, line, pos, lnum, stat)
if (stat == 0) then
token = token_type(1, 3)
call read_token(line, token, number_of_atoms, stat)
end if
if (stat == 0) then
token = token_type(4, 6)
call read_token(line, token, number_of_bonds, stat)
end if
if (stat /= 0) then
call io_error(error, "Cannot read header of molfile", &
& line, token, filename(unit), lnum, "expected integer value")
return
end if
token = token_type(35, 39)
stat = 1
if (len(line) >= 39) then
v3k = line(35:39) == "V3000"
if (line(35:39) == "V2000" .or. v3k) stat = 0
end if
if (stat /= 0) then
call io_error(error, "Format version not supported", &
& line, token, filename(unit), lnum, "invalid format version")
return
end if
if (.not.v3k .and. number_of_atoms < 1) then
call io_error(error, "Invalid number of atoms", &
& line, token_type(1, 3), filename(unit), lnum, "expected positive integer")
return
end if
if (v3k) then
call read_molfile_v3k(self, unit, error)
else
call read_molfile_v2k(self, unit, number_of_atoms, number_of_bonds, error)
end if
if (allocated(error)) return
! Attach additional meta data
self%info%two_dimensional = two_dim
if (len(comment) > 0) self%comment = comment
end subroutine read_molfile
subroutine read_molfile_v2k(self, unit, number_of_atoms, number_of_bonds, error)
!> Instance of the molecular structure data
type(structure_type), intent(out) :: self
!> File handle
integer, intent(in) :: unit
!> Number of atoms from header
integer, intent(in) :: number_of_atoms
!> Number of bonds from header
integer, intent(in) :: number_of_bonds
!> Error handling
type(error_type), allocatable, intent(out) :: error
character(len=:), allocatable :: line
integer :: i, iatom, jatom, ibond, btype, atomtype
integer :: stat, length, charge(2, 15), lnum, pos
integer :: list7(7), list12(12)
real(wp) :: x, y, z
character(len=3) :: symbol
integer, parameter :: ccc_to_charge(0:7) = [0, +3, +2, +1, 0, -1, -2, -3]
type(token_type) :: token
character(len=symbol_length), allocatable :: sym(:)
type(sdf_data), allocatable :: sdf(:)
type(structure_info) :: info
real(wp), allocatable :: xyz(:, :)
integer, allocatable :: bond(:, :)
lnum = 4
allocate(sdf(number_of_atoms))
allocate(xyz(3, number_of_atoms))
allocate(sym(number_of_atoms))
do iatom = 1, number_of_atoms
call next_line(unit, line, pos, lnum, stat)
if (stat == 0) then
token = token_type(1, 10)
call read_token(line, token, x, stat)
end if
if (stat == 0) then
token = token_type(11, 20)
call read_token(line, token, y, stat)
end if
if (stat == 0) then
token = token_type(21, 30)
call read_token(line, token, z, stat)
end if
if (len(line) >= 34) then
symbol = line(32:34)
end if
if (stat == 0) then
token = token_type(35, 36)
call read_token(line, token, list12(1), stat)
end if
list12(:) = 0
do i = 1, 11
if (stat == 0) then
if ((36+i*3) > len_trim(line)) exit
token = token_type(34 + i*3, 36 + i*3)
call read_token(line, token, list12(i+1), stat)
end if
end do
if (stat /= 0) then
call io_error(error, "Cannot read coordinates from connection table", &
& line, token, filename(unit), lnum, "unexpected value")
return
end if
atomtype = to_number(symbol)
if (atomtype == 0) then
call io_error(error, "Cannot map symbol to atomic number", &
& line, token_type(32, 34), filename(unit), lnum, "unknown element")
return
end if
xyz(:, iatom) = [x, y, z] * aatoau
sym(iatom) = symbol
sdf(iatom)%isotope = list12(1)
sdf(iatom)%charge = ccc_to_charge(list12(2)) ! drop doublet radical
sdf(iatom)%hydrogens = list12(4)
sdf(iatom)%valence = list12(6)
end do
allocate(bond(3, number_of_bonds))
do ibond = 1, number_of_bonds
call next_line(unit, line, pos, lnum, stat)
list7(:) = 0
do i = 1, 7
if (stat == 0) then
if ((i*3) > len_trim(line)) exit
token = token_type(i*3 - 2, i*3)
call read_token(line, token, list7(i), stat)
end if
end do
if (stat /= 0) then
call io_error(error, "Cannot read topology from connection table", &
& line, token, filename(unit), lnum, "unexpected value")
return
end if
iatom = list7(1)
jatom = list7(2)
btype = list7(3)
bond(:, ibond) = [iatom, jatom, btype]
end do
do while(stat == 0)
call next_line(unit, line, pos, lnum, stat)
if (index(line, "M END") == 1) exit
if (index(line, "M CHG") == 1) then
token = token_type(7, 9)
read(line(7:9), *) length
call read_token(line, token, length, stat)
if (stat == 0) then
do i = 1, length
if (stat /= 0) exit
token = token_type(3 + i*8, 5 + i*8)
call read_token(line, token, charge(1, i), stat)
if (charge(1, i) > number_of_atoms .or. charge(1, i) < 1) stat = 1
if (stat /= 0) exit
token = token_type(7 + i*8, 9 + i*8)
call read_token(line, token, charge(2, i), stat)
end do
end if
if (stat /= 0) then
call io_error(error, "Cannot read charges", &
& line, token, filename(unit), lnum, "expected integer value")
return
end if
do i = 1, length
sdf(charge(1, i))%charge = charge(2, i)
end do
end if
end do
if (stat /= 0) then
call fatal_error(error, "Cannot read connection table")
return
end if
info = structure_info(missing_hydrogen=any(sdf%hydrogens > 1))
call new(self, sym, xyz, charge=real(sum(sdf%charge), wp), info=info, bond=bond)
call move_alloc(sdf, self%sdf)
end subroutine read_molfile_v2k
subroutine read_molfile_v3k(self, unit, error)
!> Instance of the molecular structure data
type(structure_type), intent(out) :: self
!> File handle
integer, intent(in) :: unit
!> Error handling
type(error_type), allocatable, intent(out) :: error
character(len=:), allocatable :: line, group
integer :: iatom, jatom, ibond, btype, aamap, equal
integer :: stat, lnum, pos, number_of_atoms, number_of_bonds, dummy
real(wp) :: x, y, z
integer, parameter :: ccc_to_charge(0:7) = [0, +3, +2, +1, 0, -1, -2, -3]
type(token_type) :: token, tsym
character(len=symbol_length), allocatable :: sym(:)
type(sdf_data), allocatable :: sdf(:)
type(structure_info) :: info
real(wp), allocatable :: xyz(:, :)
integer, allocatable :: bond(:, :)
lnum = 4
call next_v30(unit, line, pos, lnum, stat)
do while(stat == 0)
call next_token(line, pos, token)
if (slice(line, token%first, token%last) == "BEGIN") then
call next_token(line, pos, token)
if (slice(line, token%first, token%last) == "CTAB") exit
end if
call next_v30(unit, line, pos, lnum, stat)
end do
if (stat /= 0) then
call io_error(error, "Cannot read connection table", &
& line, token_type(0, 0), filename(unit), lnum, "CTAB header not found")
return
end if
call next_v30(unit, line, pos, lnum, stat)
if (stat == 0) then
call next_token(line, pos, token)
if (slice(line, token%first, token%last) /= "COUNTS") then
call io_error(error, "Cannot read connection table", &
& line, token, filename(unit), lnum, "COUNTS header not found")
return
end if
end if
if (stat == 0) then
call read_next_token(line, pos, token, number_of_atoms, stat)
tsym = token
end if
if (stat == 0) then
call read_next_token(line, pos, token, number_of_bonds, stat)
end if
if (stat == 0) then
call read_next_token(line, pos, token, dummy, stat)
end if
if (stat == 0) then
call read_next_token(line, pos, token, dummy, stat)
end if
if (stat == 0) then
call read_next_token(line, pos, token, dummy, stat)
end if
if (stat /= 0) then
call io_error(error, "Cannot read connection table counts", &
& line, token, filename(unit), lnum, "expected integer value")
return
end if
if (number_of_atoms < 1) then
call io_error(error, "Invalid number of atoms", &
& line, tsym, filename(unit), lnum, "expected positive integer")
return
end if
allocate(sdf(number_of_atoms))
allocate(xyz(3, number_of_atoms))
allocate(sym(number_of_atoms))
allocate(bond(3, number_of_bonds))
call next_v30(unit, line, pos, lnum, stat)
do while(stat == 0)
call next_token(line, pos, token)
if (slice(line, token%first, token%last) == "END") exit
if (slice(line, token%first, token%last) == "BEGIN") then
call next_token(line, pos, token)
group = slice(line, token%first, token%last)
select case(group)
case("ATOM")
do iatom = 1, number_of_atoms
call next_v30(unit, line, pos, lnum, stat)
if (stat == 0) then
call read_next_token(line, pos, token, dummy, stat)
end if
if (stat == 0) then
call next_token(line, pos, tsym)
end if
if (stat == 0) then
call read_next_token(line, pos, token, x, stat)
end if
if (stat == 0) then
call read_next_token(line, pos, token, y, stat)
end if
if (stat == 0) then
call read_next_token(line, pos, token, z, stat)
end if
if (stat == 0) then
call read_next_token(line, pos, token, aamap, stat)
end if
if (stat /= 0) then
call io_error(error, "Cannot read coordinates", &
& line, token, filename(unit), lnum, "unexpected value")
return
end if
if (aamap > 0) then
call io_error(error, "Mapping atoms is not supported", &
& line, token, filename(unit), lnum, "unsupported value")
return
end if
tsym%last = min(tsym%last, tsym%first + symbol_length - 1)
sym(iatom) = slice(line, tsym%first, tsym%last)
if (to_number(sym(iatom)) == 0) then
call io_error(error, "Cannot map symbol to atomic number", &
& line, tsym, filename(unit), lnum, "unknown element")
return
end if
xyz(:, iatom) = [x, y, z] * aatoau
sdf(iatom) = sdf_data()
do while(pos < len(line))
call next_token(line, pos, token)
equal = index(slice(line, token%first, token%last), "=") + token%first - 1
if (equal > token%first) then
select case(slice(line, token%first, equal - 1))
case("CHG")
token%first = equal + 1
call read_token(line, token, sdf(iatom)%charge, stat)
case("VAL")
token%first = equal + 1
call read_token(line, token, sdf(iatom)%valence, stat)
case("HCOUNT")
token%first = equal + 1
call read_token(line, token, sdf(iatom)%hydrogens, stat)
case default
continue
end select
end if
if (stat /= 0) then
call io_error(error, "Cannot read atom properties", &
& line, token, filename(unit), lnum, "unexpected value")
return
end if
end do
end do
call next_v30(unit, line, pos, lnum, stat)
call next_token(line, pos, token)
case("BOND")
do ibond = 1, number_of_bonds
call next_v30(unit, line, pos, lnum, stat)
if (stat == 0) then
call read_next_token(line, pos, token, dummy, stat)
end if
if (stat == 0) then
call read_next_token(line, pos, token, btype, stat)
end if
if (stat == 0) then
call read_next_token(line, pos, token, iatom, stat)
end if
if (stat == 0) then
call read_next_token(line, pos, token, jatom, stat)
end if
if (stat /= 0) then
call io_error(error, "Cannot read bond information", &
& line, token, filename(unit), lnum, "expected integer value")
return
end if
bond(:, ibond) = [iatom, jatom, btype]
end do
call next_v30(unit, line, pos, lnum, stat)
call next_token(line, pos, token)
case("COLLECTION", "SGROUP", "OBJ3D")
do while(stat == 0)
call next_v30(unit, line, pos, lnum, stat)
call next_token(line, pos, token)
if (slice(line, token%first, token%last) == "END") exit
end do
case default
call io_error(error, "Cannot read connection table", &
& line, token, filename(unit), lnum, "Unknown entry found")
return
end select
if (slice(line, token%first, token%last) /= "END") then
call io_error(error, group//" block is not terminated", &
& line, token, filename(unit), lnum, "expected END label")
return
end if
call next_token(line, pos, token)
if (slice(line, token%first, token%last) /= group) then
call io_error(error, group//" block is not terminated", &
& line, token, filename(unit), lnum, "expected "//group//" label")
return
end if
end if
call next_v30(unit, line, pos, lnum, stat)
end do
if (slice(line, token%first, token%last) /= "END") then
call io_error(error, "Connection table is not terminated", &
& line, token, filename(unit), lnum, "expected END label")
return
end if
call next_token(line, pos, token)
if (slice(line, token%first, token%last) /= "CTAB") then
call io_error(error, "Connection table is not terminated", &
& line, token, filename(unit), lnum, "expected ATOM label")
return
end if
call next_v30(unit, line, pos, lnum, stat)
do while(stat == 0)
call next_token(line, pos, token)
if (slice(line, token%first, token%last) == "END") exit
end do
if (stat /= 0) then
call io_error(error, "Connection table is not terminated", &
& line, token, filename(unit), lnum, "expected END label")
return
end if
info = structure_info(missing_hydrogen=any(sdf%hydrogens > 1))
call new(self, sym, xyz, charge=real(sum(sdf%charge), wp), info=info, bond=bond)
call move_alloc(sdf, self%sdf)
end subroutine read_molfile_v3k
function slice(string, first, last)
character(len=*), intent(in), target :: string
integer, intent(in) :: first, last
character(len=:), pointer :: slice
slice => string(max(first, 1):min(last, len(string)))
end function slice
subroutine next_v30(unit, line, pos, lnum, iostat, iomsg)
!> Formatted IO unit
integer, intent(in) :: unit
!> Line to read
character(len=:), allocatable, intent(out) :: line
!> Current position in line
integer, intent(out) :: pos
!> Current line number
integer, intent(inout) :: lnum
!> Status of operation
integer, intent(out) :: iostat
!> Error message
character(len=:), allocatable, intent(out), optional :: iomsg
call next_line(unit, line, pos, lnum, iostat, iomsg)
if (iostat /= 0) return
if (index(line, "M END") == 1) pos = 3
if (index(line, "M V30") == 1) pos = 6
end subroutine next_v30
end module mctc_io_read_ctfile