Repository navigation
Expand file tree
/
Copy pathmd_mod.f90
More file actions
executable file
·1383 lines (1052 loc) · 48.5 KB
/
Copy pathmd_mod.f90
File metadata and controls
executable file
·1383 lines (1052 loc) · 48.5 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
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
module md_mod
implicit none
! Global Variables
integer, parameter :: ERROR_UNIT = 0
! Constants/Defaults
double precision, parameter :: PI = 3.14159265
contains
!-------------------------------------------------------------------------------
! Functions !!
!-------------------------------------------------------------------------------
function cross(a, b)
! Calculates the cross product of two vectors
real, dimension(3) :: cross
real, dimension(3), intent(in) :: a, b
cross(1) = a(2)*b(3) - a(3)*b(2)
cross(2) = a(3)*b(1) - a(1)*b(3)
cross(3) = a(1)*b(2) - a(2)*b(1)
end function cross
!-------------------------------------------------------------------------------
function mag(a)
! Calculates the length (magnitude) of a vector
real :: mag
real, dimension(3), intent(in) :: a
mag = sqrt(a(1)**2 + a(2)**2 + a(3)**2)
end function mag
!-------------------------------------------------------------------------------
function unit(a)
! Calculates the unit vector of a vector
real, dimension(3) :: unit
real, dimension(3), intent(in) :: a
unit = a / mag(a)
end function unit
!-------------------------------------------------------------------------------
function distance(a,b)
! Calculates the distance between two points
real :: distance
real, dimension(3), intent(in) :: a, b
distance = sqrt((a(1)-b(1))**2 + (a(2)-b(2))**2 + (a(3)-b(3))**2)
end function distance
!-------------------------------------------------------------------------------
function wrap_distance(p,q,a,b,c)
! Calculates distance between two points including wrapping
! p and q are coordinates
! a, b, c are unit vectors
real :: wrap_distance
real, dimension(3), intent(in) :: p, q
real*8, intent(in) :: a, b, c
real, dimension(27) :: dist
!a, b, c are cell side lengths in x, y, z respectively
dist(1) = sqrt((p(1)-q(1))**2 + (p(2)-q(2))**2 + (p(3)-q(3))**2) !
dist(2) = sqrt((p(1)-q(1)+a)**2 + (p(2)-q(2))**2 + (p(3)-q(3))**2) ! +x
dist(3) = sqrt((p(1)-q(1)-a)**2 + (p(2)-q(2))**2 + (p(3)-q(3))**2) ! -x
dist(4) = sqrt((p(1)-q(1))**2 + (p(2)-q(2)+b)**2 + (p(3)-q(3))**2) ! +y
dist(5) = sqrt((p(1)-q(1))**2 + (p(2)-q(2)-b)**2 + (p(3)-q(3))**2) ! -y
dist(6) = sqrt((p(1)-q(1))**2 + (p(2)-q(2))**2 + (p(3)-q(3)+c)**2) ! +z
dist(7) = sqrt((p(1)-q(1))**2 + (p(2)-q(2))**2 + (p(3)-q(3)-c)**2) ! -z
dist(8) = sqrt((p(1)-q(1)+a)**2 + (p(2)-q(2)+b)**2 + (p(3)-q(3))**2) ! +x, +y
dist(9) = sqrt((p(1)-q(1)+a)**2 + (p(2)-q(2)-b)**2 + (p(3)-q(3))**2) ! +x, -y
dist(10) = sqrt((p(1)-q(1)-a)**2 + (p(2)-q(2)+b)**2 + (p(3)-q(3))**2) ! -x, +y
dist(11) = sqrt((p(1)-q(1)-a)**2 + (p(2)-q(2)-b)**2 + (p(3)-q(3))**2) ! -x, -y
dist(12) = sqrt((p(1)-q(1))**2 + (p(2)-q(2)+b)**2 + (p(3)-q(3)+c)**2) ! +y, +z
dist(13) = sqrt((p(1)-q(1))**2 + (p(2)-q(2)+b)**2 + (p(3)-q(3)-c)**2) ! +y, -z
dist(14) = sqrt((p(1)-q(1))**2 + (p(2)-q(2)-b)**2 + (p(3)-q(3)+c)**2) ! -y, +z
dist(15) = sqrt((p(1)-q(1))**2 + (p(2)-q(2)-b)**2 + (p(3)-q(3)-c)**2) ! -y, -z
dist(16) = sqrt((p(1)-q(1)+a)**2 + (p(2)-q(2))**2 + (p(3)-q(3)+c)**2) ! +x, +z
dist(17) = sqrt((p(1)-q(1)+a)**2 + (p(2)-q(2))**2 + (p(3)-q(3)-c)**2) ! +x, -z
dist(18) = sqrt((p(1)-q(1)-a)**2 + (p(2)-q(2))**2 + (p(3)-q(3)+c)**2) ! -x, +z
dist(19) = sqrt((p(1)-q(1)-a)**2 + (p(2)-q(2))**2 + (p(3)-q(3)-c)**2) ! -x, -z
dist(20) = sqrt((p(1)-q(1)+a)**2 + (p(2)-q(2)+b)**2 + (p(3)-q(3)+c)**2) ! +x, +y, +z
dist(21) = sqrt((p(1)-q(1)+a)**2 + (p(2)-q(2)+b)**2 + (p(3)-q(3)-c)**2) ! +x, +y, -z
dist(22) = sqrt((p(1)-q(1)+a)**2 + (p(2)-q(2)-b)**2 + (p(3)-q(3)+c)**2) ! +x, -y, +z
dist(23) = sqrt((p(1)-q(1)+a)**2 + (p(2)-q(2)-b)**2 + (p(3)-q(3)-c)**2) ! +x, -y, -z
dist(24) = sqrt((p(1)-q(1)-a)**2 + (p(2)-q(2)+b)**2 + (p(3)-q(3)+c)**2) ! -x, +y, +z
dist(25) = sqrt((p(1)-q(1)-a)**2 + (p(2)-q(2)+b)**2 + (p(3)-q(3)-c)**2) ! -x, +y, -z
dist(26) = sqrt((p(1)-q(1)-a)**2 + (p(2)-q(2)-b)**2 + (p(3)-q(3)+c)**2) ! -x, -y, +z
dist(27) = sqrt((p(1)-q(1)-a)**2 + (p(2)-q(2)-b)**2 + (p(3)-q(3)-c)**2) ! -x, -y, -z
wrap_distance = minval(dist)
end function wrap_distance
!-------------------------------------------------------------------------------
function vector(p,q)
real, dimension(3) :: vector
real, dimension(3), intent(in) :: p, q
vector(1) = p(1) - q(1)
vector(2) = p(2) - q(2)
vector(3) = p(3) - q(3)
end function vector
!-------------------------------------------------------------------------------
function wrap_vector(p,q,a_in,b_in,c_in)
! Calculates shortest distance vector between two points including wrapping
! p and q are coordinates
! a, b, c are unit vectors
real, dimension(3) :: wrap_vector
real, dimension(3), intent(in) :: p, q
real*8, intent(in) :: a_in, b_in, c_in
real :: a, b, c
real, dimension(27,3) :: dist
real :: smallest
integer :: i, smallest_i
!a, b, c are cell side lengths in x, y, z respectively
a = real(a_in,4)
b = real(b_in,4)
c = real(c_in,4)
dist(1,:) = [p(1)-q(1), p(2)-q(2), p(3)-q(3)] !
dist(2,:) = [p(1)-q(1)+a, p(2)-q(2), p(3)-q(3)] ! +x
dist(3,:) = [p(1)-q(1)-a, p(2)-q(2), p(3)-q(3)] ! -x
dist(4,:) = [p(1)-q(1), p(2)-q(2)+b, p(3)-q(3)] ! +y
dist(5,:) = [p(1)-q(1), p(2)-q(2)-b, p(3)-q(3)] ! -y
dist(6,:) = [p(1)-q(1), p(2)-q(2), p(3)-q(3)+c] ! +z
dist(7,:) = [p(1)-q(1), p(2)-q(2), p(3)-q(3)-c] ! -z
dist(8,:) = [p(1)-q(1)+a, p(2)-q(2)+b, p(3)-q(3)] ! +x, +y
dist(9,:) = [p(1)-q(1)+a, p(2)-q(2)-b, p(3)-q(3)] ! +x, -y
dist(10,:) = [p(1)-q(1)-a, p(2)-q(2)+b, p(3)-q(3)] ! -x, +y
dist(11,:) = [p(1)-q(1)-a, p(2)-q(2)-b, p(3)-q(3)] ! -x, -y
dist(12,:) = [p(1)-q(1), p(2)-q(2)+b, p(3)-q(3)+c] ! +y, +z
dist(13,:) = [p(1)-q(1), p(2)-q(2)+b, p(3)-q(3)-c] ! +y, -z
dist(14,:) = [p(1)-q(1), p(2)-q(2)-b, p(3)-q(3)+c] ! -y, +z
dist(15,:) = [p(1)-q(1), p(2)-q(2)-b, p(3)-q(3)-c] ! -y, -z
dist(16,:) = [p(1)-q(1)+a, p(2)-q(2), p(3)-q(3)+c] ! +x, +z
dist(17,:) = [p(1)-q(1)+a, p(2)-q(2), p(3)-q(3)-c] ! +x, -z
dist(18,:) = [p(1)-q(1)-a, p(2)-q(2), p(3)-q(3)+c] ! -x, +z
dist(19,:) = [p(1)-q(1)-a, p(2)-q(2), p(3)-q(3)-c] ! -x, -z
dist(20,:) = [p(1)-q(1)+a, p(2)-q(2)+b, p(3)-q(3)+c] ! +x, +y, +z
dist(21,:) = [p(1)-q(1)+a, p(2)-q(2)+b, p(3)-q(3)-c] ! +x, +y, -z
dist(22,:) = [p(1)-q(1)+a, p(2)-q(2)-b, p(3)-q(3)+c] ! +x, -y, +z
dist(23,:) = [p(1)-q(1)+a, p(2)-q(2)-b, p(3)-q(3)-c] ! +x, -y, -z
dist(24,:) = [p(1)-q(1)-a, p(2)-q(2)+b, p(3)-q(3)+c] ! -x, +y, +z
dist(25,:) = [p(1)-q(1)-a, p(2)-q(2)+b, p(3)-q(3)-c] ! -x, +y, -z
dist(26,:) = [p(1)-q(1)-a, p(2)-q(2)-b, p(3)-q(3)+c] ! -x, -y, +z
dist(27,:) = [p(1)-q(1)-a, p(2)-q(2)-b, p(3)-q(3)-c] ! -x, -y, -z
smallest=mag(dist(1,:))
smallest_i=1
do i=1, 27
if (mag(dist(i,:)) .lt. smallest) then
smallest = mag(dist(i,:))
smallest_i = i
end if
end do
wrap_vector = dist(smallest_i,:)
end function wrap_vector
!-------------------------------------------------------------------------------
function dihedral(a,b,c,d)
! DO NOT USE - NOT FINISHED
! Calculates the dihedral angle between 4 points
! a--b
! \
! c--d
real :: dihedral
real, dimension(3), intent(in) :: a, b, c, d
real, dimension(3) :: vec_ab, vec_bc, vec_cd
real, dimension(3) :: norm_abc, norm_bcd
vec_ab = vector(a,b)
vec_bc = vector(b,c)
vec_cd = vector(c,d)
norm_abc = cross(vec_ab, vec_bc)
norm_bcd = cross(vec_bc, vec_cd)
dihedral = acos(-1*(dot_product(norm_abc, norm_bcd) / &
(mag(norm_abc)*mag(norm_bcd))))
end function dihedral
!-------------------------------------------------------------------------------
!-------------------------------------------------------------------------------
! Subroutines
!-------------------------------------------------------------------------------
!-------------------------------------------------------------------------------
!-------------------------------------------------------------------------------
! Open input file, stop if the input file is not found
!-------------------------------------------------------------------------------
subroutine open_input_file_stop_if_not_found(input_file_unit, input_filename)
integer, intent(in) :: input_file_unit
character(len=80), intent(in) :: input_filename
logical :: file_exists
inquire(file=input_filename, exist=file_exists)
if (file_exists) then
open(input_file_unit,file=input_filename,status='old',form='formatted')
else
write(ERROR_UNIT,*) "Unable to open file: ", input_filename
stop
end if
end subroutine open_input_file_stop_if_not_found
!-------------------------------------------------------------------------------
! Open output file
!-------------------------------------------------------------------------------
subroutine open_output_file(output_file_unit, output_filename)
integer, intent(in) :: output_file_unit
character*80, intent(in) :: output_filename
open(output_file_unit, file=output_filename)
end subroutine open_output_file
!-------------------------------------------------------------------------------
! Get filename suffix - everything before the last full stop
! # CHECK THIS ONE - I DONT THINK ITS USED ANYWHERE AND MAY NOT WORK
!-------------------------------------------------------------------------------
subroutine get_filename_suffix(filename,suffix)
character*80, intent(in) :: filename
character*80, intent(out) :: suffix
integer :: pos
! Determine position of full stop
pos = scan(trim(filename),".", BACK= .true.)
! Define prefix of filename as everything after full stop
if ( pos > 0 ) suffix = filename(1:pos-1)
end subroutine get_filename_suffix
!-------------------------------------------------------------------------------
! Get filename extension - everything after the last full stop
!-------------------------------------------------------------------------------
subroutine get_filename_extension(filename, ext)
character*80, intent(in) :: filename
character*80, intent(out) :: ext
integer :: pos
! Determine position of full stop
pos = scan(trim(filename),".", BACK= .true.)
! Define prefix of filename as everything after full stop
if ( pos > 0 ) ext = filename(pos+1:len(filename))
end subroutine get_filename_extension
!-------------------------------------------------------------------------------
! Open trajectory file
! Currently only supports dcd and xyz files. Might add xtc if I'm having
! a good day.
!-------------------------------------------------------------------------------
subroutine open_traj_file(traj_file_unit, traj_filename, traj_type)
integer, intent(in) :: traj_file_unit
character*80, intent(in) :: traj_filename
character*80, intent(out) :: traj_type
character*80 :: ext
logical :: file_exists
! See if trajectory file exists. If doesn't, stop
inquire(file=traj_filename, exist=file_exists)
if (file_exists) then
! Get trajectory type
call get_filename_extension(traj_filename, traj_type)
traj_type = trim(adjustl(traj_type))
if (traj_type == "dcd") then
open(traj_file_unit,file=traj_filename,status='old',form='unformatted')
elseif (traj_type == "xyz") then
open(traj_file_unit,file=traj_filename,status='old',form='formatted')
end if
else
write(ERROR_UNIT,*) "Unable to open file: ", traj_filename
stop
end if
end subroutine open_traj_file
!-------------------------------------------------------------------------------
! Read traj header
! Only supports dcd and xyz files. Use catdcd to convert others to dcd
!-------------------------------------------------------------------------------
subroutine read_traj_header(traj_file_unit, traj_type, n_frames, n_atoms)
integer, intent(in) :: traj_file_unit
character*80, intent(in) :: traj_type
integer, intent(out) :: n_frames, n_atoms
character*4 :: dum
if (traj_type == "dcd") then
read(traj_file_unit) dum, n_frames
read(traj_file_unit)
read(traj_file_unit) n_atoms
elseif (traj_type == "xyz") then
! Get n_atoms out
read(traj_file_unit,*) n_atoms
rewind(traj_file_unit)
elseif ((traj_type == "xtc") .or. (traj_type == "trr")) then
write(ERROR_UNIT,*) "Gromacs trajectories currently unsupported."
write(ERROR_UNIT,*) "Just use a good MD engine FFS."
stop
else
write(ERROR_UNIT,*) "You done gone fucked up."
end if
end subroutine read_traj_header
!-------------------------------------------------------------------------------
! Read frame of trajectory
! Only supports dcd and xyz files. Use catdcd to convert others to dcd
!-------------------------------------------------------------------------------
subroutine read_traj_frame(traj_file_unit, traj_type, n_atoms, coords, cell_basis_vectors)
integer, intent(in) :: traj_file_unit
integer, intent(inout) :: n_atoms
character*80, intent(in) :: traj_type
real, dimension(:,:), allocatable, intent(out) :: coords
real*8, dimension(2,3), intent(out) :: cell_basis_vectors
! cell_basis_vectors is an array of form:
! -- --
! | a_min, b_min, c_min |
! | a_max, b_max, c_max |
! -- --
real*8 :: a_min, a_max, b_min, b_max, c_min, c_max
integer :: i, timestep
real :: dum
character :: name
! define coords array
allocate(coords(n_atoms,3))
if (traj_type == "dcd") then
! ### NEXT READ LINE NEEDS TESTING
read(traj_file_unit) a_min, a_max, b_min, b_max, c_min, c_max
read(traj_file_unit) (coords(i,1), i=1, n_atoms)
read(traj_file_unit) (coords(i,2), i=1, n_atoms)
read(traj_file_unit) (coords(i,3), i=1, n_atoms)
! Fill cell_basis_vectors array
cell_basis_vectors(1,:) = [a_min, b_min, c_min]
cell_basis_vectors(2,:) = [a_max, b_max, c_max]
elseif (traj_type == "xyz") then
read(traj_file_unit,*) n_atoms!, dum, timestep
read(traj_file_unit,*) a_max, b_max, c_max
do i=1, n_atoms
read(traj_file_unit,*) name, coords(i,1), coords(i,2), coords(i,3)
end do
cell_basis_vectors(1,:) = [0, 0, 0]
cell_basis_vectors(2,:) = [a_max, b_max, c_max]
elseif ((traj_type == "xtc") .or. (traj_type == "trr")) then
write(ERROR_UNIT,*) "Gromacs trajectories currently unsupported."
write(ERROR_UNIT,*) "Just use a good MD engine FFS."
stop
else
write(ERROR_UNIT,*) "You done gone fucked up."
end if
end subroutine read_traj_frame
!-------------------------------------------------------------------------------
! Close file
!-------------------------------------------------------------------------------
subroutine close_file(output_file_unit)
integer, intent(in) :: output_file_unit
close(output_file_unit)
end subroutine close_file
!-------------------------------------------------------------------------------
! Read the config input file. Input file should have the following format:
! - pdb file name
! - trajectory file name
! - cutoff distance (A)
! - resname of interest
! - number of residues for each resname
! - number of atoms in each residue, for each resname
! - Number of atoms in each of the below categories: eg 6 2 1 6 2 1
! - Atom names making base of vector on 1st molecule. Eg:
! C2 C3 C4 C5 C6 C7
! - Atom names making tip of lateral vector of 1st mol
! - Atom names making tip of orthogonal vector of 1st mol.
! - Atom names making base of vector on 2nd mol
! - Atom names making tip of lateral vector of 2nd mol
! - Atom names making tip of orthogonal vector of 2nd mol.
!-------------------------------------------------------------------------------
subroutine read_aof_conf_file(conf_file_unit, pdb_filename, traj_filename, cutoff_dist, &
resnames_of_interest, n_molecules, atoms_per_molecule,&
base_vec_names1, lat_vec_names1, orth_vec_names1,&
base_vec_names2, lat_vec_names2, orth_vec_names2)
integer, intent(in) :: conf_file_unit
character*80, intent(out) :: pdb_filename, traj_filename
real, intent(out) :: cutoff_dist
character*4, dimension(:), allocatable, intent(out) :: resnames_of_interest
character*4, dimension(:), allocatable, intent(out) :: base_vec_names1, lat_vec_names1, orth_vec_names1
character*4, dimension(:), allocatable, intent(out) :: base_vec_names2, lat_vec_names2, orth_vec_names2
integer, dimension(:), allocatable, intent(out) :: n_molecules, atoms_per_molecule
integer, dimension(6) :: n_atoms_defining_vectors
integer :: i
allocate(resnames_of_interest(2))
allocate(n_molecules(2))
allocate(atoms_per_molecule(2))
! initilise n_molecules(2)
n_molecules(2) = 0
! Read config file
read(conf_file_unit,*) pdb_filename
read(conf_file_unit,*) traj_filename
read(conf_file_unit,*) cutoff_dist
read(conf_file_unit,*) n_molecules(1), n_molecules(2)
if (n_molecules(2) == 0) then
read(conf_file_unit,*) resnames_of_interest(1)
resnames_of_interest(2) = resnames_of_interest(1)
read(conf_file_unit,*) atoms_per_molecule(1)
atoms_per_molecule(2) = atoms_per_molecule(1)
read(conf_file_unit,*) (n_atoms_defining_vectors(i), i=1, 3)
n_atoms_defining_vectors(4) = n_atoms_defining_vectors(1)
n_atoms_defining_vectors(5) = n_atoms_defining_vectors(2)
n_atoms_defining_vectors(6) = n_atoms_defining_vectors(3)
else
read(conf_file_unit,*) resnames_of_interest(1), resnames_of_interest(2)
read(conf_file_unit,*) atoms_per_molecule(1), atoms_per_molecule(2)
read(conf_file_unit,*) (n_atoms_defining_vectors(i), i=1, 6)
end if
! NAmes of atoms defining various COGs
allocate(base_vec_names1(n_atoms_defining_vectors(1)))
allocate(lat_vec_names1(n_atoms_defining_vectors(2)))
allocate(orth_vec_names1(n_atoms_defining_vectors(3)))
allocate(base_vec_names2(n_atoms_defining_vectors(4)))
allocate(lat_vec_names2(n_atoms_defining_vectors(5)))
allocate(orth_vec_names2(n_atoms_defining_vectors(6)))
do i=1, size(base_vec_names1)
if(i .lt. size(base_vec_names1)) then
read(conf_file_unit,'(A4)',advance='no') base_vec_names1(i)
else
read(conf_file_unit,'(A4)') base_vec_names1(i)
base_vec_names1(i) = adjustl(base_vec_names1(i))
end if
end do
do i=1, size(lat_vec_names1)
if(i .lt. size(lat_vec_names1)) then
read(conf_file_unit,'(A4)',advance='no') lat_vec_names1(i)
else
read(conf_file_unit,'(A4)') lat_vec_names1(i)
end if
end do
do i=1, size(orth_vec_names1)
if(i .lt. size(orth_vec_names1)) then
read(conf_file_unit,'(A4)',advance='no') orth_vec_names1(i)
else
read(conf_file_unit,'(A4)') orth_vec_names1(i)
end if
end do
if (n_molecules(2)==0) then
base_vec_names2 = base_vec_names1
lat_vec_names2 = lat_vec_names1
orth_vec_names2 = orth_vec_names1
else
do i=1, size(base_vec_names2)
if(i .lt. size(base_vec_names2)) then
read(conf_file_unit,'(A4)',advance='no') base_vec_names2(i)
else
read(conf_file_unit,'(A4)') base_vec_names2(i)
end if
end do
do i=1, size(lat_vec_names2)
if(i .lt. size(lat_vec_names2)) then
read(conf_file_unit,'(A4)',advance='no') lat_vec_names2(i)
else
read(conf_file_unit,'(A4)') lat_vec_names2(i)
end if
end do
do i=1, size(orth_vec_names2)
if(i .lt. size(orth_vec_names2)) then
read(conf_file_unit,'(A4)',advance='no') orth_vec_names2(i)
else
read(conf_file_unit,'(A4)') orth_vec_names2(i)
end if
end do
end if
end subroutine read_aof_conf_file
!-------------------------------------------------------------------------------
! Read pqr file
!-------------------------------------------------------------------------------
subroutine read_pqr_file(pqr_file_unit, atomtype, coords, atom, restype, resid, chain, n_atoms, o, b)
integer, intent(in) :: pqr_file_unit
integer, dimension(:), allocatable, intent(out) :: resid
real, dimension(:,:), allocatable, intent(out) :: coords
real, dimension(:), allocatable, intent(out) :: o, b
character(4), dimension(:), allocatable, intent(out) :: atomtype, atom, restype, chain
integer, intent(out) :: n_atoms
character(4) :: temp
integer :: i, itemp, n_lines, j
n_atoms = 0
n_lines = 0
! Calculate number of atoms
do while (1 .gt. 0)
read(pqr_file_unit,*,END=5000) temp
if (temp == "ATOM") then
n_atoms = n_atoms + 1
end if
n_lines = n_lines + 1
end do
5000 continue
rewind(pqr_file_unit)
! Allocate atomic properties arrays
allocate(coords(n_atoms,3))
allocate(atomtype(n_atoms))
allocate(atom(n_atoms))
allocate(restype(n_atoms))
allocate(resid(n_atoms))
allocate(chain(n_atoms))
allocate(o(n_atoms))
allocate(b(n_atoms))
! If line starts with "ATOM" read line, add properties to arrays.
i=1
do j=1, n_lines
read(pqr_file_unit,*) temp
if (temp == "ATOM" .or. temp == "HETA") then
backspace(pqr_file_unit)
read(pqr_file_unit,210) temp, itemp, atomtype(i), restype(i), &
chain(i), resid(i), coords(i,1), coords(i,2), coords(i,3), &
o(i), b(i)
i=i+1
end if
end do
210 format(A4,2x,I5,2x,A4,A4,A1,I4,4x,3F8.3,2F6.2)
end subroutine read_pqr_file
!-------------------------------------------------------------------------------
! Read pdb file - same as read_pqr_file just with pqr replaced with pdb
!-------------------------------------------------------------------------------
subroutine read_pdb_file(pdb_file_unit, atomtype, coords, atom, restype, resid, chain, n_atoms, o, b)
integer, intent(in) :: pdb_file_unit
integer, dimension(:), allocatable, intent(out) :: resid
real, dimension(:,:), allocatable, intent(out) :: coords
real, dimension(:), allocatable, intent(out) :: o, b
character(4), dimension(:), allocatable, intent(out) :: atomtype, atom, restype, chain
integer, intent(out) :: n_atoms
character(4) :: temp
integer :: i, itemp, n_lines, j
n_atoms = 0
n_lines = 0
! Calculate number of atoms
do while (1 .gt. 0)
read(pdb_file_unit,*,END=5000) temp
if (temp == "ATOM" .or. temp == "HETA") then
n_atoms = n_atoms + 1
end if
n_lines = n_lines + 1
end do
5000 continue
rewind(pdb_file_unit)
! Allocate atomic properties arrays
allocate(coords(n_atoms,3))
allocate(atomtype(n_atoms))
allocate(atom(n_atoms))
allocate(restype(n_atoms))
allocate(resid(n_atoms))
allocate(chain(n_atoms))
allocate(o(n_atoms))
allocate(b(n_atoms))
! If line starts with "ATOM" read line, add properties to arrays.
i=1
do j=1, n_lines
read(pdb_file_unit,*) temp
if (temp == "ATOM" .or. temp == "HETA") then
backspace(pdb_file_unit)
read(pdb_file_unit,210) temp, itemp, atomtype(i), restype(i), &
chain(i), resid(i), coords(i,1), coords(i,2), coords(i,3), &
o(i), b(i)
i=i+1
end if
end do
210 format(A4,2x,I5,1x,A4,1x,A4,A1,I4,4x,3F8.3,2F6.2)
end subroutine read_pdb_file
!-------------------------------------------------------------------------------
! Write to pqr file
!-------------------------------------------------------------------------------
subroutine write_to_output_pqr(pqr_file_unit, start_index, atomtype, coords, atom, restype, resid, chain, n_atoms, o, b)
integer, intent(in) :: pqr_file_unit, start_index
integer, dimension(:), intent(in) :: resid
real, dimension(:,:), intent(in) :: coords
character(4), dimension(:), intent(in) :: atomtype, atom, restype, chain
integer, intent(in) :: n_atoms
real, dimension(:), allocatable, intent(in) :: o, b
integer :: i
do i=1, n_atoms
write(pqr_file_unit,211) "ATOM", i+start_index-1, atomtype(i),&
trim(adjustl(restype(i))), chain(i), resid(i),&
coords(i,1), coords(i,2), coords(i,3), o(i), b(i)
end do
211 format(A4,2x,I5,2x,A3,A4,1x,A1,I4,4x,3F8.3,2F6.2)
end subroutine write_to_output_pqr
!-------------------------------------------------------------------------------
! Write to pdb file
!-------------------------------------------------------------------------------
subroutine write_to_output_pdb(pdb_file_unit, start_index, atomtype, coords, atom, restype, resid, chain, n_atoms, o, b)
integer, intent(in) :: pdb_file_unit, start_index
integer, dimension(:), intent(in) :: resid
real, dimension(:,:), intent(in) :: coords
character(4), dimension(:), intent(in) :: atomtype, atom, restype, chain
integer, intent(in) :: n_atoms
real, dimension(:), allocatable, intent(in) :: o, b
integer :: i
do i=1, n_atoms
write(pdb_file_unit,211) "ATOM", i+start_index-1, atomtype(i),&
trim(adjustl(restype(i))), chain(i), resid(i),&
coords(i,1), coords(i,2), coords(i,3), o(i), b(i)
end do
211 format(A4,2x,I5,2x,A3,A4,1x,A1,I4,4x,3F8.3,2F6.2)
end subroutine write_to_output_pdb
!-------------------------------------------------------------------------------
! Translate atomic coordinates
!-------------------------------------------------------------------------------
subroutine translate_coords(coords_in, trans_vector, coords_out)
real, dimension(:,:), intent(in) :: coords_in
real, dimension(:,:), allocatable, intent(out) :: coords_out
real, dimension(3), intent(in) :: trans_vector
integer :: i
allocate(coords_out(size(coords_in,1), size(coords_in,2)))
! print*, "Trans_vector:", trans_vector
! print*, coords_in(1,1), coords_in(1,2), coords_in(1,3)
do i=1, size(coords_in,1)
coords_out(i,1) = coords_in(i,1) + trans_vector(1)
coords_out(i,2) = coords_in(i,2) + trans_vector(2)
coords_out(i,3) = coords_in(i,3) + trans_vector(3)
end do
! print*, coords_out(1,1), coords_out(1,2), coords_out(1,3)
end subroutine translate_coords
!-------------------------------------------------------------------------------
! ROTATE ( Generate a rotation matrix )
! from ftp://naif.jpl.nasa.gov/pub/naif/toolkit/FORTRAN/MacIntel_OSX_gfortran_64bit/packages/
! and in
! /Users/pickle/bin/cspice/toolkit/src/spicelib
!-------------------------------------------------------------------------------
SUBROUTINE ROTATE ( ANGLE, IAXIS, MOUT )
!$ Abstract
!
! Calculate the 3x3 rotation matrix generated by a rotation
! of a specified angle about a specified axis. This rotation
! is thought of as rotating the coordinate system.
DOUBLE PRECISION ANGLE
INTEGER IAXIS
DOUBLE PRECISION MOUT ( 3,3 )
!$ Brief_I/O
!
! VARIABLE I/O DESCRIPTION
! -------- --- --------------------------------------------------
! ANGLE I Angle of rotation (radians).
! IAXIS I Axis of rotation (X=1, Y=2, Z=3).
! MOUT O Resulting rotation matrix [ANGLE]
! IAXIS
!$ Detailed_Input
!
! ANGLE The angle given in radians, through which the rotation
! is performed.
!
! IAXIS The index of the axis of rotation. The X, Y, and Z
! axes have indices 1, 2 and 3 respectively.
!
!$ Detailed_Output
!
! MOUT Rotation matrix which describes the rotation of the
! COORDINATE system through ANGLE radians about the
! axis whose index is IAXIS.
!
!$ Parameters
!
! None.
!
!$ Particulars
!
! A rotation about the first, i.e. x-axis, is described by
!
! | 1 0 0 |
! | 0 cos(theta) sin(theta) |
! | 0 -sin(theta) cos(theta) |
!
! A rotation about the second, i.e. y-axis, is described by
!
! | cos(theta) 0 -sin(theta) |
! | 0 1 0 |
! | sin(theta) 0 cos(theta) |
!
! A rotation about the third, i.e. z-axis, is described by
!
! | cos(theta) sin(theta) 0 |
! | -sin(theta) cos(theta) 0 |
! | 0 0 1 |
!
! ROTATE decides which form is appropriate according to the value
! of IAXIS.
!
!$ Examples
!
! If ROTATE is called from a FORTRAN program as follows:
!
! CALL ROTATE (PI/4, 3, MOUT)
!
! then MOUT will be given by
!
! | SQRT(2)/2 SQRT(2)/2 0 |
! MOUT = |-SQRT(2)/2 SQRT(2)/2 0 |
! | 0 0 1 |
!
DOUBLE PRECISION S
DOUBLE PRECISION C
INTEGER TEMP
INTEGER I1
INTEGER I2
INTEGER I3
INTEGER INDEXS ( 5 )
SAVE INDEXS
DATA INDEXS / 3, 1, 2, 3, 1 /
!
! Get the sine and cosine of ANGLE
!
S = DSIN(ANGLE)
C = DCOS(ANGLE)
!
! Get indices for axes. The first index is for the axis of rotation.
! The next two axes follow in right hand order (XYZ). First get the
! non-negative value of IAXIS mod 3 .
!
TEMP = MOD ( MOD(IAXIS,3) + 3, 3 )
I1 = INDEXS( TEMP + 1 )
I2 = INDEXS( TEMP + 2 )
I3 = INDEXS( TEMP + 3 )
!
! Construct the rotation matrix
!
MOUT(I1,I1) = 1.D0
MOUT(I2,I1) = 0.D0
MOUT(I3,I1) = 0.D0
MOUT(I1,I2) = 0.D0
MOUT(I2,I2) = C
MOUT(I3,I2) = -S
MOUT(I1,I3) = 0.D0
MOUT(I2,I3) = S
MOUT(I3,I3) = C
RETURN
END SUBROUTINE ROTATE
!-------------------------------------------------------------------------------
! get centre of geometry of set of coordinates
!-------------------------------------------------------------------------------
subroutine get_coords_from_index(coords, index, coords_out)
real, dimension(:,:), intent(in) :: coords
integer, intent(in) :: index
real, dimension(3), intent(out) :: coords_out
coords_out = coords(index,:)
end subroutine get_coords_from_index
!-------------------------------------------------------------------------------
! get centre of geometry of set of coordinates
!-------------------------------------------------------------------------------
subroutine get_cog(coords, cog)
real, dimension(:,:), intent(in) :: coords
real, dimension(3), intent(out) :: cog
cog(1) = sum(coords(:,1))/size(coords,1)
cog(2) = sum(coords(:,2))/size(coords,1)
cog(3) = sum(coords(:,3))/size(coords,1)
end subroutine get_cog
!-------------------------------------------------------------------------------
! get centre of geometry of NTER coordinates
!-------------------------------------------------------------------------------
subroutine get_NTER_cog(coords, resid, cog)
real, dimension(:,:), intent(in) :: coords
integer, dimension(:), intent(in) :: resid
real, dimension(3), intent(out) :: cog
integer :: i, nter_resid, n_nter_atoms
! Assume that NTER is first residue in the pdb file.
n_nter_atoms = 0
nter_resid = minval(resid)
! Get number of atoms in NTER residue
do i=1, size(resid,1)
if (resid(i) == nter_resid) then
n_nter_atoms = n_nter_atoms + 1
end if
end do
cog(1) = sum(coords(1:n_nter_atoms,1))/n_nter_atoms
cog(2) = sum(coords(1:n_nter_atoms,2))/n_nter_atoms
cog(3) = sum(coords(1:n_nter_atoms,3))/n_nter_atoms
end subroutine get_NTER_cog
!-------------------------------------------------------------------------------
! get centre of geometry of CTER coordinates
!-------------------------------------------------------------------------------
subroutine get_CTER_cog(coords, resid, cog)
real, dimension(:,:), intent(in) :: coords
integer, dimension(:), intent(in) :: resid
real, dimension(3), intent(out) :: cog
integer :: i, cter_resid, n_cter_atoms
! Assume that CTER is last residue in the pqr file.
n_cter_atoms = 0
cter_resid = maxval(resid)
! Get number of atoms in NTER residue
do i=1, size(resid,1)
if (resid(i) == cter_resid) then
n_cter_atoms = n_cter_atoms + 1
end if
end do
cog(1) = sum(coords(size(resid,1)-n_cter_atoms+1:size(resid,1),1))/n_cter_atoms
cog(2) = sum(coords(size(resid,1)-n_cter_atoms+1:size(resid,1),2))/n_cter_atoms
cog(3) = sum(coords(size(resid,1)-n_cter_atoms+1:size(resid,1),3))/n_cter_atoms
end subroutine get_CTER_cog
!-------------------------------------------------------------------------------
! Get NTER nitrogen index
!-------------------------------------------------------------------------------
subroutine get_NTER_N_index(resid, atomtype, index_out)
integer, dimension(:), intent(in) :: resid
character(4), dimension(:), intent(in) :: atomtype
integer, intent(out) :: index_out
integer :: i, nter_resid
nter_resid = minval(resid)
! Search through all atoms looking for last resid and atomtype N
do i=1, size(resid,1)
if (resid(i) == nter_resid .and. trim(adjustl(atomtype(i))) == "N") then
index_out = i
exit
end if