File: charmm2ini

package info (click to toggle)
ball 1.4.3~beta1-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 318,984 kB
  • sloc: cpp: 346,579; ansic: 4,097; python: 2,664; yacc: 1,778; lex: 1,099; xml: 964; sh: 688; sql: 316; awk: 118; makefile: 108
file content (1206 lines) | stat: -rwxr-xr-x 29,912 bytes parent folder | download | duplicates (7)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
#
#  CHARMM parameter and topology file converter
#  produces output for BALL force field INI files
#

#
#  functions to translate an atom name

# replace all '%' (CHARMM wildcard for single character)
# with '?' (BALL wildcard for single character)
# and 'X' (CHARMM wildcard for any type)
# with '*' (BALL wildcard)
#
function wildcardSubst(name)
{
	sub(/\%/, "?", name)	
	sub(/^X$/, "*", name)
	return name
}

function SkipRest()
{
	# skip remaining part of the setion and restart parsing at the first empty line
	while (NF != 0)
	{
		getline
	}
}

function NextSection(expected)
{
	found = 0
	
	do
	{
		# read new line until a non-empty line is encountered
		while (NF == 0)
		{
			getline
		}
		
		if (match(expected, substr($1, 1, 4)))
		{
			found = 1
		}	else {
			# this section does not match - skip it and retry
			print "found section " $1 " while expecting " expected "!" >> "/dev/stderr"
			exit
		}
	} while (found == 0)
	print "reading section " $1  > "/dev/stderr"
}

function PDBAtomName(atom, res) 
{
	query_name = res ":" atom
	gsub("-[A-Z]*:", ":", query_name)
	if (translation[query_name] != "")
	{
		atom = translation[query_name]
		gsub("^.*:", "", atom)
	} else {
		if (translation["*:"atom] != "")
		{
			atom = translation["*:"atom]
			gsub("^.*:", "", atom)
		}
	}
	return atom
}

function completePDBName(atom_name)
{
	split(atom_name, fields, ":")
	res = fields[1]
	atom = fields[2]
	query_name = res ":" atom
	gsub("-[A-Z]*:", ":", query_name)
	if (translation[query_name] != "")
	{
		atom = translation[query_name]
		gsub("^.*:", "", atom)
	} else {
		if (translation["*:"atom] != "")
		{
			atom = translation["*:"atom]
			gsub("^.*:", "", atom)
		}
	}
	return res":"atom
}

BEGIN\
{
	if (ARGC != 3 && ARGC != 4)
	{
		print "charmm2ini <parm file> <top file> [<EEF1_file>]" > "/dev/stderr"
		exit
	}	

	# read the atom name translation table
	# from AmberNames.dat
	# the first column contains the AMBER name, the second column the PDB name
	result = getline < "CHARMMNames.dat"
	if (result == 0)
	{
		print "Could not open name conversion file CHARMMNames.dat" >"/dev/stderr"
		exit
	}
	while (result == 1)
	{
		translation[$1] = $2
		result = getline < "CHARMMNames.dat"
	}
	close("CHARMMNames.dat")


	# number of different names (res + atom) 
	# for the ChargesAndTypeNames section
	number_of_names=0


	#	read all CHARMM atom types from an external
	# file since the param files do not contain an explicit listing
	# of the types
	number_of_atom_types = 0;
	getline < "CHARMMTypes.dat"
	while (RT != 0)
	{
		if (substr($1, 1, 1) != "#")
		{
			atom_types[number_of_atom_types] = $1
			type_map[$1] = number_of_atom_types
			$1 = ""
			sub(/^ */, "", $0)
			atom_type_comment[number_of_atom_types] = $0		
			number_of_atom_types++
		}
		getline < "CHARMMTypes.dat"
	}
	close("CHARMMTypes.dat")

	# add the wildcard type to the type_map
	type_map["*"] = -1
	}
	{
	if (ARGIND == 1)
	{
		# write the parameter file title as a comment to the first line
		print "; " $0 
		print "; converted from " FILENAME " using charmm2ini"
		print ";"

		#	next section: bond stretch parameters
		#
		print "[QuadraticBondStretch]"
		print "ver:version key:I key:J value:k value:r0 value:comment"
		print "@unit_k=kcal/mol"
		print "@unit_r0=Angstrom"
		print ";"
		print ";"
		print ";  Rev   I     J       k           r0      comment"
		print ";  --- ----- ----- ---------- ------------ -----------------------"

		while (substr($1, 1, 4) != "BOND")
		{
			getline
		}

		
		print "reading parameter file " FILENAME > "/dev/stderr"
		print "reading section " $1 > "/dev/stderr"

		getline
		while (NF > 0)
		{
			# avoid comment lines
			if ((substr($1, 1, 1) != "!") && (NF > 3))
			{
				rev = 1.0
				I = $1
				J = $2
				k = $3
				sub(/!/, "", $4) # remove the '!' (indicating comments) from field $4
				r0 = $4

				# check whether the types are known
				if (type_map[I] == "")
				{
					print "Unknown first atom type " I " in line " FNR " of " FILENAME> "/dev/stderr"
					exit
				}
				if (type_map[J] == "")
				{
					print "Unknown second atom type " J " in line " FNR " of " FILENAME > "/dev/stderr"
					exit
				}
				
				# extract the comment
				$1 = ""
				$2 = ""
				$3 = ""
				$4 = ""
				comment = $0

				sub(/^[! ]*/, "", comment)
			
				printf "   %3.1f  %-4s  %-4s %9.3f  %11.7f   %c%s%c\n", rev, I, J, k, r0, 34, comment, 34
			}
			
			getline
		}
		print ""
		print ""

		
		# next section: angle bend parameters
		#
		print "[QuadraticAngleBend]"
		print "ver:version key:I key:J key:K value:k value:theta0 value:comment"
		print "@unit_k=kcal/mol"
		print "@unit_theta0=degree"
		print ";"
		print ";"
		print ";  Rev   I     J     K       k         r0      comment"
		print ";  --- ----- ----- ----- ---------- ---------- -----------------------"
		

		NextSection("THETA ANGLES ")
		getline
		while (NF > 0)
		{
			# avoid comment lines
			if ((substr($1, 1, 1) != "!") && (NF > 4))
			{
				rev = 1.0
				I = $1
				J = $2
				K = $3
				k = $4
				theta0 = $5
				sub(/!/, "", theta0)
				
				# check whether the types are known
				if (type_map[I] == "")
				{
					print "Unknown first atom type " I " in line " FNR " of " FILENAME > "/dev/stderr"
					exit
				}
				if (type_map[J] == "")
				{
					print "Unknown second atom type " J " in line " FNR " of " FILENAME > "/dev/stderr"
					exit
				}
				if (type_map[K] == "")
				{
					print "Unknown third atom type " K " in line " FNR " of " FILENAME > "/dev/stderr"
					exit
				}

				# extract the comment
				$1 = $2 = $3 = $4 = $5 = ""
				comment = $0
				sub(/^[! ]*/, "", comment)
			
				printf "   %3.1f  %-4s  %-4s  %-4s %10.5f %10.5f %c%s%c\n", rev, I, J, K, k, theta0, 34, comment, 34
			}
			
			getline
		}
		print ""
		print ""
		
		# next section: proper torsions
		#
		print "[Torsions]"
		print "ver:version key:I key:J key:K key:L key:n value:div value:V value:phi0 value:f value:comment"
		print "@unit_V=kcal/mol"
		print "@unit_phi0=degree"
		print "@unit_div=1"
		print "@unit_f=1"
		print ";"
		print "; energy is calculated as follows:"
		print ";"
		print ";   E = (V / div) * (1 + cos(f * phi + phi0))"
		print ";"
		print ";"
		print ";  Rev   I     J     K     L    n    div     V         phi0     f    comment"
		print ";  --- ----- ----- ----- ----- ---   --- ---------- ---------- --- -----------------------"
		
		# clear some variables we need later
		old_I = old_J = old_K = old_L = ""
		first = 0
		number = 0

		NextSection("TORSIONS DIHEDRALS")
		getline
		while (NF > 0)
		{
			if ((substr($1, 1, 1) != "!") && (NF > 6))
			{
				rev = 1.0
				I = wildcardSubst($1)
				J = wildcardSubst($2)
				K = wildcardSubst($3)
				L = wildcardSubst($4)
				div = 1
				V = $5
				phi0 = $7
				sub(/!/, "", phi0)
				f = $6
				
				# check whether the types are known
				if (type_map[I] == "")
				{
					print "Unknown first atom type " I " in line " FNR " of " FILENAME > "/dev/stderr"
					exit
				}
				if (type_map[J] == "")
				{
					print "Unknown second atom type " J " in line " FNR " of " FILENAME > "/dev/stderr"
					exit
				}
				if (type_map[K] == "")
				{
					print "Unknown third atom type " K " in line " FNR " of " FILENAME > "/dev/stderr"
					exit
				}
				if (type_map[L] == "")
				{
					print "Unknown fourth atom type " L " in line " FNR " of " FILENAME > "/dev/stderr"
					exit
				}

				$1 = $2 = $3 = $4 = $5 = $6 = $7 = ""
				comment = $0
				sub(/^[! ]*/, "", comment)
			
				# check for a repeated line
				if (I == old_I && J == old_J \
						&& K == old_K && L == old_L)
				{
					# buffer all lines
					div_arr[number] = div
					V_arr[number] = V
					phi0_arr[number] = phi0
					f_arr[number] = f
					comment_arr[number] = comment
					number++
				} else {
					# print the buffered lines
					if (number > 0)
					{
						printf "   %3.1f  %-4s  %-4s  %-4s  %-4s  %-4s  %1d  %10.5f %10.5f  %1d  %c%s%c\n",\
										 rev, old_I, old_J, old_K, old_L, "N", number, 0.0, 0.0, 0, 34, "", 34
						for (i = 0; i < number; i++)
						{
							printf "   %3.1f  %-4s  %-4s  %-4s  %-4s  %-4s  %1d  %10.5f %10.5f  %1d  %c%s%c\n",\
										 rev, old_I, old_J, old_K, old_L, i + 1, div_arr[i], V_arr[i], phi0_arr[i], f_arr[i], 34, comment_arr[i], 34
						}
					}
					
					# and buffer the new line
					div_arr[0] = div
					V_arr[0] = V
					phi0_arr[0] = phi0
					f_arr[0] = f
					comment_arr[0] = comment
					number = 1
				}

				# remember the atom types of this line
				old_I = I
				old_J = J
				old_K = K
				old_L = L				
			}

			getline
		}
		
		if (number > 0)
		{
			# print the buffered lines
			printf "   %3.1f  %-4s  %-4s  %-4s  %-4s  %-4s  %1d  %10.5f %10.5f  %1d  %c%s%c\n",\
						 rev, old_I, old_J, old_K, old_L, "N", number, 0.0, 0.0, 0, 34, "", 34
			for (i = 0; i < number; i++)
			{
				printf "   %3.1f  %-4s  %-4s  %-4s  %-4s  %-4s  %1d  %10.5f %10.5f  %1d  %c%s%c\n",\
							 rev, old_I, old_J, old_K, old_L, i + 1, div_arr[i], V_arr[i], phi0_arr[i], f_arr[i], 34, comment_arr[i], 34
			}
		}
					
		print ""
		print ""
		
		# next section: improper torsions
		#
		print "[ImproperTorsions]"
		print "ver:version key:I key:J key:K key:L value:k value:phase value:comment"
		print "@unit_k=kcal/mol"
		print "@unit_phase=deg"
		print ";"
		print "; energy is calculated as follows:"
		print ";"
		print ";   E = k * (psi - psi0)^2"
		print ";"
		print ";"
		print ";  Rev   I     J     K     L        k          phase           comment"
		print ";  --- ----- ----- ----- ----- ---------- ------------- -----------------------"
		
		NextSection("IMPROPER IMPHI")
		getline
		while (NF > 0)
		{
			if ((substr($1, 1, 1) != "!") && (NF > 6))
			{
				rev = 1.0
				I = wildcardSubst($1)
				J = wildcardSubst($2)
				K = wildcardSubst($3)
				L = wildcardSubst($4)
				div = 1
				k = $5
				psi0 = $7
				sub(/!/, "", psi0)
				f = $6
				
				# check whether the types are known
				if (type_map[I] == "")
				{
					print "Unknown first atom type " I " in line " FNR " of " FILENAME > "/dev/stderr"
					exit
				}
				if (type_map[J] == "")
				{
					print "Unknown second atom type " J " in line " FNR " of " FILENAME > "/dev/stderr"
					exit
				}
				if (type_map[K] == "")
				{
					print "Unknown third atom type " K " in line " FNR " of " FILENAME > "/dev/stderr"
					exit
				}
				if (type_map[L] == "")
				{
					print "Unknown fourth atom type " L " in line " FNR " of " FILENAME > "/dev/stderr"
					exit
				}

				$1 = $2 = $3 = $4 = $5 = $6 = $7 = ""
				comment = $0
				sub(/^[! ]*/, "", comment)
			
				printf "   %3.1f  %-4s  %-4s  %-4s  %-4s %10.5f %10.5f %c%s%c\n",\
									 rev,    I,    J,    K,     L,   k,    psi0, 34, comment, 34
			}

			getline
		}
					
		print ""
		print ""

		# next section: vdW parameters
		#
		print "[LennardJones]"
		print "ver:version key:I value:R value:epsilon"
		print "@unit_R=Angstrom"
		print "@unit_epsilon=kcal/mol"
		
		# print the nonbonded options
		$1 = ""
		options_string = $0
		getline 
		options_string = options_string " " $0
		number_of_options = split(options_string, options)
		for (i = 1; i <= number_of_options; i++)
		{
			if (((i + 1) <= number_of_options) && (match(options[i + 1], "^[0-9\.eE\+\-]*$") != 0))
			{
				print "@" options[i] "=" options[i+1]
				i++
			} else {
				print "@" options[i] "=true"
			}
		}
			
		
		print ";"
		print ";"
		print ";  Rev type   Rmin/2      Emin    comment"
		print ";  --- ---- ---------- ---------- -------------------------"
		
		number_of_14_params = 0

		NextSection("NONBONDED NBONDED")		
		getline
		while (NF > 0)
		{
			if ((substr($1, 1, 1) != "!") && (NF >= 4))
			{
				rev = 1.0
				I = $1
				sub(/ /, "", I)
				sub(/!/, "", $4)
				I = wildcardSubst(I)
				Rmin = $4
				Emin = $3
				r_min[I] = Rmin
				if ((match($5, /[0-9.\-\+eE]/) > 0)\
						&& (match($6, /[0-9.\-\+eE]/) > 0)\
						&& (match($7, /[0-9.\-\+eE]/) > 0))
				{
					sub(/!/, "", $7)
					Emin14[number_of_14_params] = $6
					Rmin14[number_of_14_params] = $7
					Type14[number_of_14_params] = I
					number_of_14_params++
					$5 = $6 = $7 = ""
				}
					
				$1 = $2 = $3 = $4 = ""
				comment = $0;
				sub(/^[! ]*/, "", comment)

				printf "   %3.1f %-4s %10.6f %10.6f %c%s%c\n", rev, I, Rmin, Emin, 34, comment, 34
			}

			getline
		}
		print "" 
		print ""


		print "[LennardJones14]"
		print "ver:version key:I value:R value:epsilon"
		print "@unit_R=Angstrom"
		print "@unit_epsilon=kcal/mol"
		print ";"
		print ";"
		print ";  Rev type   Rmin/2      Emin    comment"
		print ";  --- ---- ---------- ---------- -------------------------"

		for (i = 0; i < number_of_14_params; i++)
		{
			printf "   %3.1f %-4s %10.6f %10.6f\n", rev, Type14[i], Rmin14[i], Emin14[i]
		}
		print "" 
		print ""
		
		# next section: NBFIX (optional!)
		#
		NextSection("NBFIX HBOND")

		if (substr($1, 1, 4) == "NBFI")
		{
			getline

			number_of_14_params = 0
			print "[SpecificNonBonded]"
			print "ver:version key:I key:J value:R value:epsilon"
			print "@unit_R=Angstrom"
			print "@unit_epsilon=kcal/mol"
			print ";"
			print ";"
			print ";  Rev  I    J      Rmin      epsilon"
			print ";  --- ---- ---- ---------- ----------"
			

			getline
			while (NF > 0)
			{
				if ((substr($1, 1, 1) != "!") && (NF > 3))
				{
					rev = 1.0
					I = wildcardSubst($1)
					J = wildcardSubst($2)
					Rmin = $4
					Emin = $3

					if (Emin != 0.0)
					{
						printf "   %3.1f  %-4s %-4s %10.4f %10.4f\n", rev, I, J, Rmin, Emin
					}


					if ((match($5, /[0-9.\-\+eE]/) > 0) && (match($6, /[0-9.\-\+eE]/) > 0))	
					{
						Rmin14[number_of_14_params] = $6
						Emin14[number_of_14_params] = $5
						TypeI14[number_of_14_params] = I
						TypeJ14[number_of_14_params] = J
			
						number_of_14_params++
					}
				}
				
				getline
			}
			print ""
			print ""
			number_of_14_params = 0
			print "[SpecificNonBonded14]"
			print "ver:version key:I key:J value:R value:epsilon"
			print "@unit_R=Angstrom"
			print "@unit_epsilon=kcal/mol"
			print ";"
			print ";"
			print ";  Rev  I    J      Rmin      epsilon"
			print ";  --- ---- ---- ---------- ----------"
			

			for (i = 0; i < number_of_14_params; i++)
			{
				if (Emin14[i] != 0.0)
				{
					printf "   %3.1f  %-4s %-4s %10.4f %10.4f\n", rev, 
								TypeI14[i], TypeJ14[i], Rmin14[i], Emin14[i]
				}
			}

			print ""
			print ""
						
		} else {
			NextSection("HBOND")
		}

		# read the remainder of the file
		while (ARGIND == 1)
		{
			getline
		}
	}

	if (ARGIND == 2)
	{
		print "reading topology file " FILENAME  > "/dev/stderr"

		# read the masses for the different atom types
		# and the default patches
		while ($1 != "RESI" )
		{
			#  read mass
			if ($1 == "MASS")
			{
				mass[$3] = $4
			}
			#   read default patch names
			if (substr($1, 1, 4) == "DEFA")
			{
				default_patch_first = $3
				default_patch_last = $5
			}
			
			#   read autogenerate tag
			if (substr($1, 1, 4) == "AUTO")
			{
				if ((substr($2, 1, 4) == "DIHE") || (substr($3, 1, 4) == "DIHE"))
				{	
					create_residue_dihedrals = true
				} else {
					create_residue_dihedrals = false
				}
				if ((substr($2, 1, 4) != "ANGL") && (substr($3, 1, 4) != "ANGL"))
				{	
					print "Cannot read parameter files without autogenerated angles!" >"/dev/stderr"
				}
			}
			
			getline
		}
		
		# write a section for the atom types
		print "[AtomTypes]"
		print "value:ver key:type value:mass value:extended value:comment"
		print "@unit_mass=g/mol"
		print ";"
		print ";"
		print ";   Rev Type    mass     extended   comment"
		print ";   --- ---- ---------- ---------- ---------------------------------------------"
		rev = 1.0
		for (i = 0; i < number_of_atom_types; i++)
		{
			if (mass[atom_types[i]] != "")
			{
				if (match(atom_types[i],/^..[1-4][Ee]/) != 0)
				{
					extended="true"
				} else {
					extended="false"
				}
				printf "    %3.1f %-4s %10.6f   %-5s    %c%s%c\n", rev, atom_types[i], mass[atom_types[i]], extended, 34, atom_type_comment[i], 34
			}
		}
		print ""
		print ""


		number_of_residue_atoms = 0
		number_of_residues = 0
		number_of_res_torsions = 0

		# next section: improper torsions 
		#
		print "[ResidueImproperTorsions]"
		print "key:name value:B value:C value:D"
		print ";"
		print ";"
		print ";    res:atom    atom B atom C atom D"
		print ";  ------------  ------ ------ ------" 	

		
		while ($1 != "END")
		{
			while (substr($1, 1, 1) == "!")
			{
				getline
			} 
			line_read = "false"

			if ($1 == "RESI")
			{
				name = $2
				res_names[number_of_residues] = name
				number_of_residues++
				
				#	set the default patches for this residue
				#	(overwritten by a PATC card)
				first_patch[name] = default_patch_first
				last_patch[name] = default_patch_last
				
				getline
				line_read = "true"
				while ($1 != "END" && $1 != "PRES" && $1 != "" && $1 != "RESI")
				{
					# extract the types and charges
					# 
					if ($1 == "ATOM")
					{
						atom = name":"$2
						atom_charge[atom] = $4
						atom_type[atom] = $3
						residue_atom_list[name] = residue_atom_list[name] " " $2
						residue_atoms[number_of_residue_atoms] = atom
						number_of_residue_atoms++
					}

					# check for required patches
					# (overwriting the default patches)
					if (substr($1, 1, 4) == "PATC")
					{	
						if ((NF != 5) && (NF != 3))
						{
							print "illegal PATCh card in line " FNR " of " FILENAME >"/dev/stderr"
							exit
						}
						first_patch[name] = $3
						if (NF > 3)
						{
							last_patch[name] = $5
						}
					}

					# extract the improper torsions
					#
					if ($1 == "IMPH")
					{
						# check whether we have a complete line
						if ((NF != 5) && (NF != 9) && (NF != 13))
						{
							print "illegal IMPH line in line " FNR " of " FILENAME
						} else {
							printf "    %-8s      %-4s   %-4s   %-4s\n", 
								name ":" PDBAtomName($2, name), PDBAtomName($3, name), PDBAtomName($4, name), PDBAtomName($5, name)
							if (NF > 5)	
							{
								printf "    %-8s      %-4s   %-4s   %-4s\n", 
									name ":" PDBAtomName($6, name), PDBAtomName($7, name), PDBAtomName($8, name), PDBAtomName($9, name)
							}
							if (NF > 9)	
							{
								printf "    %-8s      %-4s   %-4s   %-4s\n", 
									name ":" PDBAtomName($10, name), PDBAtomName($11, name), PDBAtomName($12, name), PDBAtomName($13, name)
							}
						}
					}

					# extract the proper torsions
					#
					if ($1 == "DIHE")
					{
						# check whether we have a complete line
						if ((NF != 5) && (NF != 9) && (NF != 13))
						{
							print "illegal DIHE line in line " FNR " of " FILENAME
						} else {
							res_torsions_res[number_of_res_torsions] = name
							res_torsions_A[number_of_res_torsions] = PDBAtomName($2, name)
							res_torsions_B[number_of_res_torsions] = PDBAtomName($3, name)
							res_torsions_C[number_of_res_torsions] = PDBAtomName($4, name)
							res_torsions_D[number_of_res_torsions] = PDBAtomName($5, name)
							number_of_res_torsions++

							if (NF > 5)	
							{
								res_torsions_res[number_of_res_torsions] = name
								res_torsions_A[number_of_res_torsions] = PDBAtomName($6, name)
								res_torsions_B[number_of_res_torsions] = PDBAtomName($7, name)
								res_torsions_C[number_of_res_torsions] = PDBAtomName($8, name)
								res_torsions_D[number_of_res_torsions] = PDBAtomName($9, name)
								number_of_res_torsions++

								if (NF > 9)	
								{
									res_torsions_res[number_of_res_torsions] = name
									res_torsions_A[number_of_res_torsions] = PDBAtomName($10, name)
									res_torsions_B[number_of_res_torsions] = PDBAtomName($11, name)
									res_torsions_C[number_of_res_torsions] = PDBAtomName($12, name)
									res_torsions_D[number_of_res_torsions] = PDBAtomName($13, name)
									number_of_res_torsions++
								}
							}
						}
					}
					getline 
				} 
			}
			#	read residue patches
			#
			if ($1 == "PRES")
			{
				name = $2
				# just to find out whether we have this patch
				patch_names[name] = name

				getline 
				line_read = "true"
				while ($1 != "" && $1 != "END" && $1 != "RESI" && $1 != "PRES")
				{
					#	read DELEte cards
					# we only interpret the deleted atoms - the rest is irrelevant
					if (substr($1, 1, 4) == "DELE" && $2 == "ATOM")
					{
						for (i = 3; i <= NF; i++)
						{
							patch_deletes[name] = patch_deletes[name] " " $i
						}
					}

					#	read ATOM cards
					# remember the new charge and atom type
					if (substr($1, 1, 4) == "ATOM")
					{
						patch_charge[name":"$2] = $4
						patch_type[name":"$2] = $3
						patch_atoms[name] = patch_atoms[name] " " $2
					}

					# read improper torsions
					#
					if ($1 == "IMPH")
					{
						# check whether we have a complete line
						if ((NF != 5) && (NF != 9) && (NF != 13))
						{
							print "illegal IMPH line for residue patch " name " in line " FNR " of " FILENAME
						} else {
							for (i = 2; i <= NF; i++)
							{
								patch_impropers[name] = patch_impropers[name]" "$i
							}
						}
					}

					# read proper torsions
					#
					if ($1 == "DIHE")
					{
						# check whether we have a complete line
						if ((NF != 5) && (NF != 9) && (NF != 13))
						{
							print "illegal DIHE line for residue patch " name " in line " FNR " of " FILENAME
						} else {
							for (i = 2; i <= NF; i++)
							{
								patch_torsions[name] = patch_torsions[name]" "$i
							}
						}
					}

					getline
				}
			}
			#	read the next line if neither "RESI" nor "PRES"
			# were found
			#
			if (line_read = "false")
			{
				getline
			}
		}

		#
		# write the patched impropers
		#
		for (j = 0; j < number_of_residues; j++)
		{
			name = res_names[j]
			# check whether we need a first patch
			if (first_patch[res_names[j]] != "NONE")
			{
				patch_name = first_patch[res_names[i]]
				if (patch_names[patch_name] != patch_name)
				{
					print "undefined first patch " patch_name " for residue " res_names[j] >"/dev/stderr"
				}
				# if impropers were defined for this patch,
				# append them to the current section
				if (patch_impropers[patch_name] != "")
				{
					number_of_atoms = split(patch_impropers[patch_name], imp_atoms)
					for (i = 1; i < number_of_atoms; i = i + 4)
					{
						name = name"-N"
						printf "    %-8s      %-4s   %-4s   %-4s\n", 
								name ":" PDBAtomName(imp_atoms[i], name), PDBAtomName(imp_atoms[i+1], name), 
								PDBAtomName(imp_atoms[i+2], name), PDBAtomName(imp_atoms[i+3], name)
					}
				}
			}

			# check whether we need a last patch
			if (last_patch[res_names[j]] != "NONE")
			{
				patch_name = last_patch[res_names[j]]
				if (patch_names[patch_name] != patch_name)
				{
					print "undefined last patch " patch_name " for residue " res_names[j] >"/dev/stderr"
				}
				# if impropers were defined for this patch,
				# append them to the current section
				if (patch_impropers[patch_name] != "")
				{
					number_of_atoms = split(patch_impropers[patch_name], imp_atoms, " ")
					for (i = 1; i < number_of_atoms; i = i + 4)
					{
						name = name"-C"
						printf "    %-8s      %-4s   %-4s   %-4s\n", 
								name ":" PDBAtomName(imp_atoms[i], name), PDBAtomName(imp_atoms[i+1], name), 
								PDBAtomName(imp_atoms[i+2], name), PDBAtomName(imp_atoms[i+3], name)
					}
				}
			}
		}
		
		# done with the residue improper section
		print ""
		print ""

		#
		# next section: improper torsions 
		#
		print "[ResidueTorsions]"
		print "value:ver key:name key:A key:B key:C key:D"
		print ";"
		print "; + indicates: atom in the next residue"
		print "; - indicates: atom in the previous residue"
		print ";"
		print ";"
		print ";  rev residue  atom A  atom B  atom C  atom D"
		print ";  --- -------  ------  ------  ------  ------" 	
		for (i = 0; i < number_of_res_torsions; i++)
		{
			printf "   %3.1f  %-8s  %-4s    %-4s    %-4s    %-4s\n", 
							rev, res_torsions_res[i], res_torsions_A[i], res_torsions_B[i], res_torsions_C[i], res_torsions_D[i]
		}
		print ""
		print ""


		# write the ChargesAndTypes section
		# write the section header
		print ""
		print "[ChargesAndTypeNames]"
		print "ver:version key:name value:q value:type"
		print "@unit_q=e0"
		
		for (i = 0; i < number_of_residue_atoms; i++)
		{
			atom = completePDBName(residue_atoms[i])
			printf "   1.0 %-10s %8.5f %-5s\n", 
				atom, atom_charge[residue_atoms[i]], atom_type[residue_atoms[i]]
		}

		#  now append the charges and types for the patched
		#	 residues
		for (i = 0; i < number_of_residues;i++)
		{
			name = res_names[i]
			if (first_patch[name] != "NONE")
			{
				# which patch is to be applied?
				patch_name = first_patch[name]
				all_atoms = residue_atom_list[name]
				
				number_of_atoms = split(all_atoms, atoms, " ")
				# delete atoms if necessary
				if (patch_deletes[patch_name] != "")
				{
					# create arrays with the atoms and the atoms to delete
					number_of_atoms_to_delete = split(patch_deletes[patch_name], del_atoms, " ")

					# remove the atoms to delete from the atoms
					for (j = 1; j <= number_of_atoms; j++)
					{
						for (k = 1; k <= number_of_atoms_to_delete; k++)
						{
							if (del_atoms[k] == atoms[j])
							{
								atoms[j] = ""
							}
						}
					}
					
					# atoms now contains all remaining atoms
					#	now check for added atoms
					if (patch_atoms[patch_name] != "")
					{
						number_of_changed_atoms =	split(patch_atoms[patch_name], changed_atoms, " ")
						
						# ignore changed atoms
						for (j = 1; j <= number_of_changed_atoms; j++)
						{
							for (k = 1; k <= number_of_atoms; k++)
							{
								if (changed_atoms[j] == atoms[k])
								{
									break;
								}
							}
							# is this a new atom?
							if (k > number_of_atoms)
							{
								# yes, add it to the atoms_array
								atoms[changed_atoms[j]] = changed_atoms[j]
							}
						}
					}
					
					# write the types and charges for the patched residue
					# 
					for (idx in atoms)
					{
						atom = atoms[idx]
						if (atom != "")
						{
							atom_name = name "-N:" PDBAtomName(atom, name)
							if (patch_charge[patch_name":"atom] != "")
							{
								printf "   1.0 %-10s %8.5f %-5s\n", atom_name, patch_charge[patch_name":"atom], patch_type[patch_name":"atom]	
							} else {
								printf "   1.0 %-10s %8.5f %-5s\n", atom_name, atom_charge[name":"atom], atom_type[name":"atom]	
							}
						}
					}
				}
			}
			# add C-terminal patches
			if (last_patch[name] != "NONE")
			{
				# which patch is to be applied?
				patch_name = last_patch[name]
				all_atoms = residue_atom_list[name]
				
				number_of_atoms = split(all_atoms, atoms, " ")
				# delete atoms if necessary
				if (patch_deletes[patch_name] != "")
				{
					# create arrays with the atoms and the atoms to delete
					number_of_atoms_to_delete = split(patch_deletes[patch_name], del_atoms, " ")

					# remove the atoms to delete from the atoms
					for (j = 1; j <= number_of_atoms; j++)
					{
						for (k = 1; k <= number_of_atoms_to_delete; k++)
						{
							if (del_atoms[k] == atoms[j])
							{
								atoms[j] = ""
							}
						}
					}
					
					# atoms now contains all remaining atoms
					#	now check for added atoms
					if (patch_atoms[patch_name] != "")
					{
						number_of_changed_atoms =	split(patch_atoms[patch_name], changed_atoms, " ")
						
						# ignore changed atoms
						for (j = 1; j <= number_of_changed_atoms; j++)
						{
							for (k = 1; k <= number_of_atoms; k++)
							{
								if (changed_atoms[j] == atoms[k])
								{
									break;
								}
							}
							# is this a new atom?
							if (k > number_of_atoms)
							{
								# yes, add it to the atoms_array
								atoms[changed_atoms[j]] = changed_atoms[j]
							}
						}
					}
					
					# write the types and charges for the patched residue
					# 
					for (idx in atoms)
					{
						atom = atoms[idx]
						if (atom != "")
						{
							atom_name = name "-C:" PDBAtomName(atom, name)
							if (patch_charge[patch_name":"atom] != "")
							{
								printf "   1.0 %-10s %8.5f %-5s\n", atom_name, patch_charge[patch_name":"atom], patch_type[patch_name":"atom]	
							} else {
								printf "   1.0 %-10s %8.5f %-5s\n", atom_name, atom_charge[name":"atom], atom_type[name":"atom]	
							}
						}
					}
				}
			}
		}
	
		# skip the remaineder of the topology file
		while ((ARGIND == 2) && (getline > 0));

	}
	
	
	print "" 
	print "" 

	#  read the EEF1 parameters
	#  
	if (ARGIND == 3)
	{
		# write a section for the atom types
		print "[EEF1Solvation]"
		print "value:ver key:type value:V value:dG_ref value:dG_free value:dH_ref value:Cp_ref value:sig_w value:R_min"
		print "@unit_V=Angstrom^3"
		print "@unit_R_min=Angstrom"
		print "@unit_dG_free=kcal/mol"
		print "@unit_dG_ref=kcal/mol"
		print "@unit_dH_ref=kcal/mol"
		print "@unit_Cp_ref=cal/(molK)"
		print "@unit_sig_w=Angstrom"
		print ";"
		print ";"
		print ";   Rev Type        V       dG_ref     dG_free    dH_Ref     Cp_ref     sig_w       R_min"
		print ";   --- ----    --------- ---------- ---------- ---------- ---------- ---------- ----------"
		rev = 1.0

		# read the EEF solvation parameters
		lines_read = 1
		while ($1 != "" && lines_read > 0)
		{
			# ignore comment lines
			if (substr($1, 1,1) != "!")
			{
				type = $1
				volume = $2
				dGref = $3
				dGfree = $4
				dHref = $5
				Cpref = $6
				sigw = $7 
				R_min = r_min[$1]
				if (R_min == "")
				{
					R_min = r_min[substr($1,1,1)"?"]	
					if (R_min == "")
					{
						R_min = r_min[substr($1,1,1)"*"]
						if (R_min == "")
						{
							print "could not find vdw parameter for type " $1 " while reading line " FNR " of " FILENAME >"/dev/stderr"
							exit
						}
					}
				}
				printf "    %3.1f %-4s %10.3f %10.3f %10.3f %10.3f %10.3f %10.3f %10.3f\n", rev, type, volume, dGref, dGfree, dHref, Cpref, sigw, R_min
			}
			
			lines_read = getline
		}
		print ""
		print ""
	}		
}