File: ULInteractionsBuilder.m

package info (click to toggle)
adun.app 0.81-13
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 16,600 kB
  • sloc: objc: 70,912; ansic: 6,662; yacc: 394; python: 75; cpp: 36; makefile: 33; xml: 15; awk: 3
file content (1742 lines) | stat: -rwxr-xr-x 57,614 bytes parent folder | download | duplicates (2)
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
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
/*
   Project: UL

   Copyright (C) 2005 Michael Johnston & Jordi Villa-Freixa

   Author: Michael Johnston

   Created: 2005-07-14 13:34:47 +0200 by michael johnston

   This application is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This application is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU General Public
   License along with this library; if not, write to the Free
   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA.
*/

#include <math.h>
#include <AdunKernel/AdunDefinitions.h>
#include "ULFramework/ULInteractionsBuilder.h"
#include "ULFramework/ULFrameworkFunctions.h"

static NSComparisonResult sortIndexesByNames(id indexOne, id indexTwo, void* atomNames);

static NSComparisonResult sortIndexesByNames(id indexOne, id indexTwo, void* atomNames)
{
	NSString* atomOneName, *atomTwoName;

	atomOneName = [(NSArray*)atomNames objectAtIndex: [indexOne intValue]];
	atomTwoName = [(NSArray*)atomNames objectAtIndex: [indexTwo intValue]];

	return [atomOneName compare: atomTwoName 
		options: NSCaseInsensitiveSearch];
}

/**
Protocol adopted by objects that supply information
on the interactions and parameters of a force field
\note This is a temporary protocol. It and all objects
that use it will be deprecated when  force fields are 
written in FFML 1.0.
*/
@protocol ULForceFieldInformation
- (NSArray*) interactions;
- (NSArray*) unitsForParametersOfInteraction: (NSString*) interaction;
- (NSArray*) namesOfParametersOfInteraction: (NSString*) interaction;
@end

/*
 * Theses objects provide information on a given force field.
 * In the future this informatio will be present in the force field
 * file.
 */

static NSDictionary* classMap;

@interface ULForceFieldInformation: NSObject
{
	NSMutableDictionary* unitsForInteraction;
	NSMutableDictionary* parametersForInteraction;
}
+ (id) objectForForceField: (NSString*) forceField;
@end

@interface ULEnzymixForceFieldInformation: ULForceFieldInformation <ULForceFieldInformation>
@end

@interface ULAmberForceFieldInformation: ULForceFieldInformation <ULForceFieldInformation>
@end

@interface ULCharmm27ForceFieldInformation: ULForceFieldInformation <ULForceFieldInformation>
@end

@implementation ULForceFieldInformation

+ (void) initialize
{
	classMap = [NSDictionary dictionaryWithObjectsAndKeys:
			[ULEnzymixForceFieldInformation class], @"Enzymix",
			[ULAmberForceFieldInformation class], @"Amber",
			[ULCharmm27ForceFieldInformation class], @"Charmm27",
			nil];
	[classMap retain];		
}

+ (id) objectForForceField: (NSString*) forceField
{
	Class class;

	class = [classMap objectForKey: forceField];
	if(class == nil)
		return nil;

	return [[class new] autorelease];	
}

@end

@implementation ULEnzymixForceFieldInformation

- (id) init
{
	NSArray* array;

	if((self = [super init]))
	{
		unitsForInteraction = [NSMutableDictionary new];
		array = [NSArray arrayWithObjects: @"KCalMol", @"Angstrom", nil];
		[unitsForInteraction setObject: array forKey: @"HarmonicBond"];
		array = [NSArray arrayWithObjects: @"KCalMol", @"Degree", nil];
		[unitsForInteraction setObject: array forKey: @"HarmonicAngle"];
		array = [NSArray arrayWithObjects: @"KCalMol", @"None", @"Degree", nil];
		[unitsForInteraction setObject: array forKey: @"FourierTorsion"];
		array = [NSArray arrayWithObjects: @"KCalMol", @"Degree", nil];
		[unitsForInteraction setObject: array forKey: @"HarmonicImproperTorsion"];
		array = [NSArray arrayWithObjects: @"KCalMol-2", @"KCalMol-2", nil];
		[unitsForInteraction setObject: array forKey: @"TypeOneVDWInteraction"];
		array = [NSArray arrayWithObjects: @"AMU", @"None", nil];
		[unitsForInteraction setObject: array forKey: @"Mass"];

		parametersForInteraction = [NSMutableDictionary new];
		array = [NSArray arrayWithObjects: @"Constant", @"Separation", nil];
		[parametersForInteraction setObject: array forKey: @"HarmonicBond"];
		array = [NSArray arrayWithObjects: @"Constant", @"Angle", nil];
		[parametersForInteraction setObject: array forKey: @"HarmonicAngle"];
		array = [NSArray arrayWithObjects: @"Constant", @"Periodicity", @"Phase", nil];
		[parametersForInteraction setObject: array forKey: @"FourierTorsion"];
		array = [NSArray arrayWithObjects: @"Constant", @"Angle", nil];
		[parametersForInteraction setObject: array forKey: @"HarmonicImproperTorsion"];
		array = [NSArray arrayWithObjects: @"VDW A", @"VDW B", nil];
		[parametersForInteraction setObject: array forKey: @"TypeOneVDWInteraction"];
		array = [NSArray arrayWithObjects: @"Mass", @"ElementNumber", nil];
		[parametersForInteraction setObject: array forKey: @"Mass"];
	}

	return self;
}

- (void) dealloc
{
	[unitsForInteraction release];
	[parametersForInteraction release];
	[super dealloc];
}

- (NSArray*) interactions
{
	return [[[unitsForInteraction allKeys]
		retain] autorelease];
}

- (NSArray*) unitsForParametersOfInteraction: (NSString*) interaction
{
	return [[[unitsForInteraction objectForKey: interaction]
		retain] autorelease];
}		

- (NSArray*) namesOfParametersOfInteraction: (NSString*) interaction
{
	return [[[parametersForInteraction objectForKey: interaction]
		retain] autorelease];
}

- (NSString*) vdwType
{
	return @"TypeOneVDWInteraction";
}

@end

@implementation ULAmberForceFieldInformation

- (id) init
{
	NSArray* array;

	if((self = [super init]))
	{
		unitsForInteraction = [NSMutableDictionary new];
		array = [NSArray arrayWithObjects: @"KCalMol", @"Angstrom", nil];
		[unitsForInteraction setObject: array forKey: @"HarmonicBond"];
		array = [NSArray arrayWithObjects: @"KCalMol", @"Degree", nil];
		[unitsForInteraction setObject: array forKey: @"HarmonicAngle"];
		array = [NSArray arrayWithObjects: @"KCalMol", @"None", @"Degree", nil];
		[unitsForInteraction setObject: array forKey: @"FourierTorsion"];
		array = [NSArray arrayWithObjects: @"KCalMol", @"Degree", nil];
		[unitsForInteraction setObject: array forKey: @"HarmonicImproperTorsion"];
		array = [NSArray arrayWithObjects: @"KCalMol", @"Angstrom", nil];
		[unitsForInteraction setObject: array forKey: @"TypeTwoVDWInteraction"];
		array = [NSArray arrayWithObjects: @"AMU", @"None", nil];
		[unitsForInteraction setObject: array forKey: @"Mass"];

		parametersForInteraction = [NSMutableDictionary new];
		array = [NSArray arrayWithObjects: @"Constant", @"Separation", nil];
		[parametersForInteraction setObject: array forKey: @"HarmonicBond"];
		array = [NSArray arrayWithObjects: @"Constant", @"Angle", nil];
		[parametersForInteraction setObject: array forKey: @"HarmonicAngle"];
		array = [NSArray arrayWithObjects: @"Constant", @"Periodicity", @"Phase", nil];
		[parametersForInteraction setObject: array forKey: @"FourierTorsion"];
		array = [NSArray arrayWithObjects: @"Constant", @"Angle", nil];
		[parametersForInteraction setObject: array forKey: @"HarmonicImproperTorsion"];
		array = [NSArray arrayWithObjects: @"VDW WellDepth", @"VDW Separation", nil];
		[parametersForInteraction setObject: array forKey: @"TypeTwoVDWInteraction"];
		array = [NSArray arrayWithObjects: @"Mass", @"ElementNumber", nil];
		[parametersForInteraction setObject: array forKey: @"Mass"];
	}

	return self;
}

- (void) dealloc
{
	[unitsForInteraction release];
	[parametersForInteraction release];
	[super dealloc];
}

- (NSArray*) interactions
{
	return [[[unitsForInteraction allKeys]
		retain] autorelease];
}

- (NSArray*) unitsForParametersOfInteraction: (NSString*) interaction
{
	return [[[unitsForInteraction objectForKey: interaction]
		retain] autorelease];
}

- (NSArray*) namesOfParametersOfInteraction: (NSString*) interaction
{
	return [[[parametersForInteraction objectForKey: interaction]
		retain] autorelease];
}

- (NSString*) vdwType
{
	return @"TypeTwoVDWInteraction";
}

@end

//Not ready yet
@implementation ULCharmm27ForceFieldInformation

- (id) init
{
	NSArray* array;

	if((self = [super init]))
	{
		unitsForInteraction = [NSMutableDictionary new];
		array = [NSArray arrayWithObjects: @"KCalMol", @"Angstrom", nil];
		[unitsForInteraction setObject: array forKey: @"HarmonicBond"];
		array = [NSArray arrayWithObjects: @"KCalMol", @"Degree", nil];
		[unitsForInteraction setObject: array forKey: @"HarmonicAngle"];
		array = [NSArray arrayWithObjects: @"KCalMol", @"None", @"Degree", nil];
		[unitsForInteraction setObject: array forKey: @"FourierTorsion"];
		array = [NSArray arrayWithObjects: @"KCalMol", @"Degree", nil];
		[unitsForInteraction setObject: array forKey: @"HarmonicImproperTorsion"];
		array = [NSArray arrayWithObjects: @"KCalMol", @"Angstrom", nil];
		[unitsForInteraction setObject: array forKey: @"TypeTwoVDWInteraction"];
		array = [NSArray arrayWithObjects: @"KCalMol", @"Angstrom", nil];
		[unitsForInteraction setObject: array forKey: @"1-4Interaction"];
		array = [NSArray arrayWithObjects: @"KCalMol", @"Angstrom", nil];
		[unitsForInteraction setObject: array forKey: @"UreyBradley"];
		array = [NSArray arrayWithObjects: @"AMU", nil];
		[unitsForInteraction setObject: array forKey: @"Mass"];

		parametersForInteraction = [NSMutableDictionary new];
		array = [NSArray arrayWithObjects: @"Constant", @"Separation", nil];
		[parametersForInteraction setObject: array forKey: @"HarmonicBond"];
		array = [NSArray arrayWithObjects: @"Constant", @"Angle", nil];
		[parametersForInteraction setObject: array forKey: @"HarmonicAngle"];
		array = [NSArray arrayWithObjects: @"Constant", @"Periodicity", @"Phase", nil];
		[parametersForInteraction setObject: array forKey: @"FourierTorsion"];
		array = [NSArray arrayWithObjects: @"Constant", @"Angle", nil];
		[parametersForInteraction setObject: array forKey: @"HarmonicImproperTorsion"];
		array = [NSArray arrayWithObjects: @"VDW WellDepth", @"VDW Separation", nil];
		[parametersForInteraction setObject: array forKey: @"TypeTwoVDWInteraction"];
		array = [NSArray arrayWithObjects: @"1-4 VDW WellDepth", @"1-4 VDW Separation", nil];
		[parametersForInteraction setObject: array forKey: @"1-4Interaction"];
		array = [NSArray arrayWithObjects: @"Constant", @"Separation", nil];
		[parametersForInteraction setObject: array forKey: @"UreyBradley"];
		array = [NSArray arrayWithObjects: @"Mass", nil];
		[parametersForInteraction setObject: array forKey: @"Mass"];
	}

	return self;
}

- (void) dealloc
{
	[unitsForInteraction release];
	[parametersForInteraction release];
	[super dealloc];
}

- (NSArray*) interactions
{
	return [[[unitsForInteraction allKeys]
		retain] autorelease];
}

- (NSArray*) unitsForParametersOfInteraction: (NSString*) interaction
{
	return [[[unitsForInteraction objectForKey: interaction]
		retain] autorelease];
}

- (NSArray*) namesOfParametersOfInteraction: (NSString*) interaction
{
	return [[[parametersForInteraction objectForKey: interaction]
		retain] autorelease];
}

- (NSString*) vdwType
{
	return @"TypeTwoVDWInteraction";
}

@end

/*
 * ULInterfaceBuilder
 */

@interface ULInteractionsBuilder (TemporaryDataSourceConversions)
- (id) createDataSourceFromConfiguration: (id) configuration topology: (id) topology;
@end

@implementation ULInteractionsBuilder

/***
Converting Parameters
**/

- (NSArray*) convertParameters: (NSArray*) parameters withUnits: (NSArray*) units
{
	int i;
	NSMutableArray* convertedArray = [NSMutableArray array];
	id unit;
	double parameter, conversionConstant;

	if([parameters count] != [units count])
	{
		NSWarnLog(@"Parameters %@. Units %@", parameters, units);
		[NSException raise: NSInvalidArgumentException
			format: @"Number of parameters %d does not match number of units %d",
			[parameters count],
			[units count]];
	}

	//We want to convert energies to simulation units and degrees to radians
	//We cycle through the units and convert whichever ones we need
	
	for(i=0; i<(int)[units count]; i++)
	{
		unit = [units objectAtIndex: i];
		parameter = [[parameters objectAtIndex: i] doubleValue];
		if([unitsToConvert containsObject: unit])
		{
			conversionConstant = [[constantForUnit objectForKey:unit] 
						doubleValue];
			parameter *= conversionConstant;
		}

		[convertedArray addObject: 
			[NSNumber numberWithDouble: parameter]];
	}

	return convertedArray;
}

/************************

Finding parameters

************************/

//Necessary for improper torsion assignment
//Check that the trigonal atom matches the begining or end of the tag
//If trigonal atom matches, look for all possible combinations of atoms in the tag to match
//return the idices of the matching atoms in the "correct" order (parameter order)
//if no match retuns NULL
- ( NSArray * ) _matchImproperTorsion: (NSArray*) idStrings 
			 toParamLabel: (NSString*) idString fromRow: (id) row
{
	int i, number, index,trigonal;
	NSNumber *trigonalIndex;
	NSMutableArray *atomsTopo, *atomsParam;
	NSMutableDictionary *atomsTopoDic, *atomsParamDic;
	NSMutableArray *rowOut, *rowIn;
	id iObject;
	
	atomsParam = [NSMutableArray arrayWithCapacity:1]; 
	atomsTopo  = [NSMutableArray arrayWithCapacity:1];
	
	[atomsTopo addObjectsFromArray: 
         [idString componentsSeparatedByString: @" "]];
	
	[atomsParam addObjectsFromArray: 
         [[idStrings objectAtIndex: 0] componentsSeparatedByString: @" "]];
	
	//Dont do anything if parameters use only one atom
	if ( [atomsParam count ] < 4 )
		return NULL;
	
	//Check to see if the trigonal atom matches and remove it from the list
	//Charmm parameters have the trigonal atom at the begining or end
	
	if (![[atomsParam objectAtIndex: 0] isEqual: [atomsTopo objectAtIndex: 2]])
		if (![[atomsParam objectAtIndex: 3] isEqual: [atomsTopo objectAtIndex: 2]])
			return NULL;
	
	if ([[atomsParam objectAtIndex: 0] isEqual: [atomsTopo objectAtIndex: 2]] )
		trigonal = 0;
	else
		trigonal = 3;
		
	[atomsTopo removeObjectAtIndex: 2];
	[atomsParam removeObjectAtIndex: trigonal];
	
	rowIn =  [NSMutableArray arrayWithCapacity:1];
	[rowIn addObjectsFromArray: row];
	trigonalIndex = [ rowIn objectAtIndex: 2 ];
	[rowIn removeObjectAtIndex: 2 ];
	
	//check all permutations of the remaining 3 atom names
	atomsTopoDic = [NSMutableDictionary dictionaryWithCapacity:1];
	atomsParamDic = [NSMutableDictionary dictionaryWithCapacity:1];
	for( i=0;i<3;i++)
	{
		[atomsTopoDic setObject: [NSNumber numberWithInt: 0] 
				 forKey: [atomsTopo objectAtIndex: i]];
		[atomsParamDic setObject:[NSNumber numberWithInt: 0 ] 
				  forKey: [atomsParam objectAtIndex: i]];
	}
	
	for( i=0;i<3;i++)
	{  
		number =  [[atomsTopoDic objectForKey: [atomsTopo objectAtIndex: i]]
			   intValue ];
		number++;
		[atomsTopoDic setObject: [NSNumber numberWithInt: number] 
				 forKey: [atomsTopo objectAtIndex: i]];
		number =  [[atomsParamDic objectForKey: [atomsParam objectAtIndex: i]]
			   intValue];
		number++;
		[atomsParamDic setObject:[NSNumber numberWithInt: number] 
				  forKey: [atomsParam objectAtIndex: i]];
	}
	
	// if its a match place the atoms in the correct order (parameter order)
	if ( [ atomsTopoDic isEqualToDictionary: atomsParamDic] )
	{
		rowOut = [NSMutableArray arrayWithCapacity:4 ];
		for(i=0;i<3;i++)
		{
			iObject = [atomsParam objectAtIndex: i ];
			index = [atomsTopo indexOfObject: iObject ];
			[atomsTopo removeObjectAtIndex: index ];
			[rowOut addObject: [rowIn objectAtIndex: index ]];
			[rowIn removeObjectAtIndex: index ];
		}
		[rowOut insertObject: trigonalIndex atIndex: trigonal];
		return rowOut;
	}
	else
	{
		return NULL;
	}
}

/**
This is a workaround until FFML is fully developed.
We know we are working with Enzymix so we can make
certain assumptions here e.g. the diehedral interactions
note: Improper Torsions that have no parameters are removed
*/

- (void) _findParametersForInteractions: (NSDictionary*) topology ofAtoms: (NSMutableArray*) atomList
{
	int rowCount;
	id class, interaction, row, trow, atom;
	NSEnumerator *interactionEnum, *rowEnum, *atomEnum;	
	NSDictionary* topologyClassNodes;
	NSMutableArray* atomArray, *newRow;
	NSString* interactionType, *idString;
	NSMutableString* failString, *missingParams;
	NSMutableIndexSet* unknownInteractions;
	AdMutableDataMatrix* newMatrix;	//The matrix with parameters
	NSArray* convertedParameters, *interactionUnits, *idStrings;
	
	//get the correct topology information
	
	topologyClassNodes = [parameterLibrary topologiesForClass: @"generic"];
	
	NSDebugLLog(@"ULInteractionsBuilder", 
		    @"Finding parameters for %@", 
		    [topology valueForKey:@"InteractionType"]);
	
	rowEnum = [[topology valueForKey:@"Matrix"] rowEnumerator];
	class = [topologyClassNodes valueForKey:[topology valueForKey:@"InteractionType"]];
	atomArray  = [NSMutableArray arrayWithCapacity:1];
	interactionType = [topology valueForKey: @"InteractionType"];
	unknownInteractions = [NSMutableIndexSet indexSet];
	
	failString = [NSMutableString stringWithCapacity: 1];
	missingParams = [NSMutableString stringWithCapacity: 1];
	newMatrix = [[AdMutableDataMatrix new] autorelease];
	newRow = [NSMutableArray array];
	rowCount = 0;	
	interactionUnits = [forceFieldInfo unitsForParametersOfInteraction: interactionType];
	
	while((row = [rowEnum nextObject]))
	{
		//Hackity hack hack hack!
		//We also have to set an array explaining the units of 
		//the parameters to be returned.
		
		if([interactionType isEqual:@"FourierTorsion"] && 
		   [forceField isEqual:@"Enzymix"] )
		{
			[atomArray addObject: 
			 [atomList objectAtIndex: [[row objectAtIndex:1] intValue]]];
			[atomArray addObject: 
			 [atomList objectAtIndex: [[row objectAtIndex:2] intValue]]];
		}
		else if([interactionType isEqual:@"HarmonicImproperTorsion"] && 
			[forceField isEqual:@"Enzymix"])
		{
			[atomArray addObject: 
			 [atomList objectAtIndex: [[row objectAtIndex:2] intValue]]];
		}
		else
		{
			atomEnum = [row objectEnumerator];
			while((atom = [atomEnum nextObject]))
				[atomArray addObject:
				 [atomList objectAtIndex: [atom intValue]]];
		}
		
		idString = [atomArray componentsJoinedByString:@" "];
		interactionEnum = [[class children] objectEnumerator];
		while((interaction = [interactionEnum nextObject]))
		{
			idStrings = [interaction idStringsForInteraction];
			if ( [interactionType isEqual:@"HarmonicImproperTorsion"] )
			{ 
				// find if a improper torsion matches
				// Enzymix will just return NULL
				trow = [self _matchImproperTorsion: idStrings
						      toParamLabel: idString
							   fromRow: row ];
				// if trow is not NULL it means we found parameters therefore
				// place atoms in the right order and force a parameter match
				if ( trow != NULL)
				{
					row = trow;
					idString = [ idStrings objectAtIndex: 0 ];
				}
			}
			
			if([idStrings containsObject: idString])
			{
				[newRow removeAllObjects];
				[newRow addObjectsFromArray: [interaction parameters]];
				[newRow addObjectsFromArray: [interaction constraints]];
				
				//FFML 1.0 should return an array of paramters
				//Plus their names and their units. For now we have
				//to hack this part. We know were dealing with
				//Enyzmix so we could set the conversions above.
				
				convertedParameters = [self convertParameters: newRow 
								    withUnits: interactionUnits];
				[newRow removeAllObjects];			
				[newRow addObjectsFromArray: row];
				[newRow addObjectsFromArray: convertedParameters];
				[newMatrix extendMatrixWithRow: newRow];
				//Search for multiple matching lines for torsions only
				if (! ( [interactionType isEqual:@"FourierTorsion"] ||
				       [interactionType isEqual:@"HarmonicImproperTorsion"] ) )
					break;
			}
		}
		
		if([newRow count] == 0)
		{
			[missingParams appendString: @"("];
			[missingParams appendString: [atomArray componentsJoinedByString: @", "]];
			[missingParams appendFormat: @")\n"];	
			[unknownInteractions addIndex: rowCount];
		} 
		else
			[newRow removeAllObjects];
		
		[atomArray removeAllObjects];
		rowCount++;
	}
	
	// hack: 1-4 interactions and UB terms cannot have missing parameters
	if([missingParams length] > 0 && (![interactionType isEqual:  @"1-4Interaction"]  &&
					  ![interactionType isEqual:  @"UreyBradley"] ))
	{
		[failString appendFormat:
			@"\nUnable to find parameters for the following %@ interactions.\n",
			interactionType];
		[failString appendString: missingParams];
		
		if([interactionType isEqual: @"HarmonicBond"] || [interactionType isEqual: @"HarmonicAngle"])
		{
			GSPrintf(buildOutput, @"%@\n", failString);
			GSPrintf(buildOutput, @"Aborting build\n");
			[[NSException exceptionWithName: @"ULBuildException"
						 reason: [NSString stringWithFormat: @"Missing %@ parameters.", interactionType]
					       userInfo: [NSDictionary dictionaryWithObject: failString
								forKey: @"ULBuildExceptionDetailedDescriptionKey"]] 
			 raise];
		}
		else if ([interactionType isEqual: @"HarmonicImproperTorsion"]) 
		{
			GSPrintf(buildOutput, @"\nIgnore missing improper torsions for aromatic groups\n");
			GSPrintf(buildOutput, @"%@",failString);
		}
		else	
		{
			[errorString appendString: failString];
			[buildString appendFormat: @"\t\tRemoved %d %@ interactions.\nSee errors.\n", 
			 [unknownInteractions count],
			 interactionType];
		}
	}
	
	//set the new matrix
	[topology setValue: newMatrix forKey: @"Matrix"]; 
}

/*********************

Interaction Building

**********************/

- (id) _buildBondsForAtoms: (NSMutableArray*) atomNames withBondedAtoms: (NSMutableArray*) bondedAtomsList
{
	int i;
	NSEnumerator* bondedAtomsListEnum, *bondedAtomsEnum;
	NSMutableDictionary* interaction;
	NSMutableArray* row;
	id bondedAtoms, atomIndex;
	AdMutableDataMatrix* bondMatrix;

	bondMatrix = [[AdMutableDataMatrix alloc] 
			initWithNumberOfColumns: 0
			columnHeaders: nil
			columnDataTypes: nil];
	[bondMatrix autorelease];		
	bondedAtomsListEnum = [bondedAtomsList objectEnumerator];

	i = 0;
	while((bondedAtoms = [bondedAtomsListEnum nextObject]))
	{
		bondedAtomsEnum = [bondedAtoms objectEnumerator];
		while((atomIndex = [bondedAtomsEnum nextObject]))
			if([atomIndex intValue] > i)
			{
				row = [NSMutableArray arrayWithCapacity:2];
				[row addObject: [NSNumber numberWithInt: i]];
				[row addObject: atomIndex];
				[bondMatrix extendMatrixWithRow: row];
			}
		i++;
	}

	interaction = [NSMutableDictionary dictionaryWithCapacity: 1];
	[interaction setValue: @"HarmonicBond" forKey: @"InteractionType"];
	[interaction setValue: [NSNumber numberWithInt: 2] forKey: @"ElementsPerInteraction"];
	[interaction setValue: bondMatrix forKey: @"Matrix"];
	[self _findParametersForInteractions: interaction ofAtoms: atomNames];

	GSPrintf(buildOutput, @"There are %d bonded interactions\n", [bondMatrix numberOfRows]);
	[buildString appendFormat: @"\t\tThere are %d bonds\n", [bondMatrix numberOfRows]];
	return interaction;
}

- (id) _buildAnglesForAtoms: (NSMutableArray*) atomNames withBondedAtoms: (NSMutableArray*) bondedAtomsList
{
	int i, j, atom;
	NSEnumerator *bondedAtomsListEnum;
	NSMutableArray *angle;
	NSMutableDictionary* interaction;
	AdMutableDataMatrix *angleMatrix;
	id bondedAtoms; 

	angleMatrix = [[AdMutableDataMatrix alloc] 
			initWithNumberOfColumns:0 
			columnHeaders: nil
			columnDataTypes: nil];
	[angleMatrix autorelease];		
	bondedAtomsListEnum = [bondedAtomsList objectEnumerator];

	atom = 0;	
	while((bondedAtoms = [bondedAtomsListEnum nextObject]))
	{
		if([bondedAtoms count] > 1)
			for(i=0; i<[bondedAtoms count] - 1; i++)
				for(j=i+1; j < [bondedAtoms count]; j++)	
				{
					angle = [NSMutableArray arrayWithCapacity: 3];
					[angle addObject: [bondedAtoms objectAtIndex: i]];
					[angle addObject: [NSNumber numberWithInt: atom]];
					[angle addObject: [bondedAtoms objectAtIndex: j]];
					[angleMatrix extendMatrixWithRow: angle];
				}
		atom++;
	}
	
	interaction = [NSMutableDictionary dictionaryWithCapacity: 1];
	[interaction setValue: @"HarmonicAngle" forKey: @"InteractionType"];
	[interaction setValue: [NSNumber numberWithInt: 3] forKey: @"ElementsPerInteraction"];
	[interaction setValue: angleMatrix forKey: @"Matrix"];
	[self _findParametersForInteractions: interaction ofAtoms: atomNames];

	GSPrintf(buildOutput,@"There are %d angle interactions\n", [angleMatrix numberOfRows]);
	[buildString appendFormat: @"\t\tThere are %d angle interactions\n", [angleMatrix numberOfRows]];
	return interaction;
}


- (id) _buildTorsionsForAtoms: (NSMutableArray*) atomNames 
		withBondedAtoms: (NSMutableArray*) bondedAtomsList 
		bonds: (AdDataMatrix*) bondMatrix
{
	int atomOneIndex, atomTwoIndex, connections1, connections2;
	NSEnumerator* bondEnum, *arrayTwoEnum, *arrayOneEnum;
	AdMutableDataMatrix* torsionMatrix;
	NSMutableDictionary* interaction;
	NSMutableArray* torsion;
	id firstAtomIndex, lastAtomIndex, bond;

	bondEnum = [bondMatrix rowEnumerator];
	torsionMatrix = [[AdMutableDataMatrix alloc] 
				initWithNumberOfColumns: 0 
				columnHeaders: nil
				columnDataTypes: nil];	
	[torsionMatrix autorelease];			

	//now use the bond list to build the torsions
	//each bond is a possible torsion center

	while((bond = [bondEnum nextObject]))
	{
		atomOneIndex = [[bond objectAtIndex:0] intValue];
		atomTwoIndex = [[bond objectAtIndex:1] intValue];
		connections1 = [[bondedAtomsList objectAtIndex: atomOneIndex] count];
		connections2 = [[bondedAtomsList objectAtIndex: atomTwoIndex] count];

		if(connections1 > 1 && connections2 > 1)
		{
			arrayOneEnum = [[bondedAtomsList objectAtIndex: atomOneIndex] objectEnumerator];
			while((firstAtomIndex = [arrayOneEnum nextObject]))
				if([firstAtomIndex intValue] != atomTwoIndex)
				{ 
					arrayTwoEnum = [[bondedAtomsList objectAtIndex: atomTwoIndex] 
										objectEnumerator];
					while((lastAtomIndex = [arrayTwoEnum nextObject]))
						if([lastAtomIndex intValue] != atomOneIndex)
						{
							torsion = [NSMutableArray arrayWithCapacity: 4];
							[torsion addObject: firstAtomIndex];
							[torsion addObject: [bond objectAtIndex: 0]];
							[torsion addObject: [bond objectAtIndex: 1]];
							[torsion addObject: lastAtomIndex];	
							[torsionMatrix extendMatrixWithRow: torsion];
						}
				}
		}
	}
	interaction = [NSMutableDictionary dictionaryWithCapacity: 1];
	[interaction setValue: @"FourierTorsion" forKey: @"InteractionType"];
	[interaction setValue: [NSNumber numberWithInt: 4] forKey: @"ElementsPerInteraction"];
	[interaction setValue: torsionMatrix forKey: @"Matrix"];
	[self _findParametersForInteractions: interaction ofAtoms: atomNames];

	GSPrintf(buildOutput, @"There are %d torsion interactions\n", [torsionMatrix numberOfRows]);
	[buildString appendFormat: @"\t\tThere are %d proper torsion interactions\n", [torsionMatrix numberOfRows]];
	return interaction;
}

/*
Improper Torsions Note:

The itor interactions have the center as the third atom.
The other atoms are in alphabetical order (AMBER convention applied to Enzymix).
Charmm puts the trigonal atom first or last
If we dont find parameters for the itor then it is removed
*/

- (id) _buildImproperTorsionsForAtoms: (NSMutableArray*) atomNames 
		withBondedAtoms: (NSMutableArray*) bondedAtomsList 
		bonds: (AdDataMatrix*) bondMatrix
{
	int i;
	id bondedAtoms;
	AdMutableDataMatrix* improperMatrix;
	NSMutableDictionary* interaction;
	NSMutableArray* itor;

	improperMatrix = [[AdMutableDataMatrix alloc]
				initWithNumberOfColumns: 0
				columnHeaders: nil
				columnDataTypes: nil];
	[improperMatrix autorelease];			

	//The possible improper torsions are atoms that have three bonds
	//So we simply search for each of these in bonded atoms list

	for(i=0; i<[bondedAtomsList count]; i++)
	{
		bondedAtoms = [bondedAtomsList objectAtIndex: i];
		if([bondedAtoms count] == 3)
		{
			itor = [NSMutableArray array];

			//do we need to define a particular order!?

			[itor addObject: [bondedAtoms objectAtIndex: 0]];
			[itor addObject: [bondedAtoms objectAtIndex: 1]];
			[itor addObject: [bondedAtoms objectAtIndex: 2]];
			[itor sortUsingFunction: sortIndexesByNames 
				context: (void*)atomNames];
			[itor insertObject: [NSNumber numberWithInt: i]
				   atIndex: 2];
			[improperMatrix extendMatrixWithRow: itor]; 
		}
	}
		
	interaction = [NSMutableDictionary dictionaryWithCapacity: 1];
	[interaction setValue: @"HarmonicImproperTorsion"
		forKey: @"InteractionType"];
	[interaction setValue: [NSNumber numberWithInt: 4] 
		forKey: @"ElementsPerInteraction"];
	[interaction setValue: improperMatrix
		forKey: @"Matrix"];
	[self _findParametersForInteractions: interaction 
		ofAtoms: atomNames];

	GSPrintf(buildOutput, @"There are %d improper torsion interactions\n", [improperMatrix numberOfRows]);
	[buildString appendFormat: @"\t\tThere are %d improper torsion interactions\n",
		 [improperMatrix numberOfRows]];

	return interaction;
}

- (id) _buildVDWForAtoms: (NSMutableArray*) atomNames withBondedAtoms: (NSMutableArray*) bondedAtomsList
{
	int i;
	AdMutableDataMatrix* vdwMatrix;
	NSMutableDictionary* interaction;
	NSMutableArray* row;

	vdwMatrix = [[AdMutableDataMatrix alloc] 
			initWithNumberOfColumns: 0
			columnHeaders: nil
			columnDataTypes: nil];
	[vdwMatrix autorelease];		

	for(i=0; i<[atomNames count]; i++)
	{
		row = [NSMutableArray arrayWithCapacity: 1];
		[row addObject: [NSNumber numberWithInt: i]];
		[vdwMatrix extendMatrixWithRow: row];
	}

	interaction = [NSMutableDictionary dictionaryWithCapacity: 1];
	[interaction setValue: [forceFieldInfo vdwType]
		forKey: @"InteractionType"];
	[interaction setValue: vdwMatrix forKey: @"Matrix"];
	[interaction setValue: [NSNumber numberWithInt: 1] forKey: @"ElementsPerInteraction"];
	[self _findParametersForInteractions: interaction ofAtoms: atomNames];

	return interaction;
}

- (id) _build14ForAtoms: (NSMutableArray*) atomNames 
		withBondedInteractions: (NSMutableArray*) bondedInteractions 
		withVDWAtoms: (NSMutableArray*) vdwAtomsList
{
	int i,index;
	int noAtoms,noTorsions,noList14;
	int firstatm,lastatm, noInteraction14;
	double eps1,eps2,rmin1,rmin2;
	NSMutableArray* list14; 
	AdMutableDataMatrix* vdw14Matrix, *matrix14;
	NSMutableArray* row;
	NSArray* matrixRow;
	NSMutableDictionary* interaction14, *params14, *params14Scaled;
	NSMutableIndexSet* indexes;
	NSNumber* atom1,* atom2;
	NSMutableDictionary* interaction;
	id torsions, itors,torMatrix;
	
	noAtoms = [atomNames count];
	vdw14Matrix = [AdMutableDataMatrix new];
	matrix14 = [AdMutableDataMatrix new];
	[matrix14 autorelease];
	
	row = [NSMutableArray array];
	for(i=0; i<noAtoms; i++)
	{
		[row addObject: [NSNumber numberWithInt: i]];
		[vdw14Matrix extendMatrixWithRow: row];
		[row removeAllObjects];
	}
	
	interaction14 = [NSMutableDictionary dictionaryWithCapacity: 1];
	[interaction14 setValue: @"1-4Interaction" forKey: @"InteractionType"];
	[interaction14 setValue: vdw14Matrix forKey: @"Matrix"];
	[interaction14 setValue: [NSNumber numberWithInt: 1] forKey: @"ElementsPerInteraction"];
	[self _findParametersForInteractions: interaction14 ofAtoms: atomNames];   
	noInteraction14 =[ [interaction14 valueForKey:@"Matrix"] numberOfRows ];
	params14Scaled = [NSMutableDictionary dictionaryWithCapacity: noInteraction14  ];
	
	//create a dictionary with the scaled 1-4 VDW parameters
	for(i=0; i<noInteraction14; i++)
	{
		matrixRow = [ [interaction14 valueForKey:@"Matrix"] row: i];
		[ params14Scaled setObject: matrixRow forKey: [ matrixRow objectAtIndex: 0 ] ];
	}
	
	//create a dictionary with the std. VDW parameters
	noInteraction14 =[ [vdwAtomsList valueForKey:@"Matrix"] numberOfRows ];
	params14 = [NSMutableDictionary dictionaryWithCapacity: noInteraction14  ];
	for(i=0; i<noInteraction14; i++)
	{
		matrixRow = [ [vdwAtomsList valueForKey:@"Matrix"] row: i];
		[ params14 setObject: matrixRow forKey: [ matrixRow objectAtIndex: 0 ] ];
	}
	
	//create a list of 1-4 interactions using the torsion list
	list14 = [NSMutableArray arrayWithCapacity: 1];
	for(i=0; i<noAtoms-1; i++)
	{
		indexes = [NSMutableIndexSet indexSet ];
		[list14 addObject: indexes];
	}
	torsions = [ bondedInteractions valueForKey: @"FourierTorsion" ];
	torMatrix = [ torsions valueForKey:@"Matrix" ];
	noTorsions = [ torMatrix numberOfRows ];
	for(i=0; i<noTorsions; i++)
	{
		itors = [torMatrix row: i];
		firstatm = [[ itors objectAtIndex: 0 ] intValue];
		lastatm = [[ itors objectAtIndex: 3 ] intValue];
		if ( firstatm < lastatm )
			[ [ list14 objectAtIndex: firstatm ] addIndex: lastatm ];
		else
			[ [ list14 objectAtIndex: lastatm ] addIndex: firstatm ];
	}
	
	noList14 = 0;
	for(i=0; i<[list14 count]; i++)
		noList14 += [[list14 objectAtIndex:i] count];
	GSPrintf(buildOutput, @"There are %d 1-4 interactions\n", noList14);
	[buildString appendFormat: @"\t\tThere are %d 1-4 interactions\n", noList14];
	
	//build a matrix for the interaction
	row = [NSMutableArray arrayWithCapacity: 4];
	for(i=0; i<noAtoms-1; i++)
	{
		indexes = [ list14 objectAtIndex: i ];
		index = [indexes firstIndex];
		atom1 = [NSNumber numberWithInt: i ];
		while(index != NSNotFound)
		{
			atom2 = [NSNumber numberWithInt: index];
			matrixRow = [ params14 objectForKey: atom1];
			eps1  = [ [ matrixRow objectAtIndex: 1 ] doubleValue ];
			rmin1 = [ [ matrixRow objectAtIndex: 2 ] doubleValue ];
			matrixRow = [ params14 objectForKey: atom2];
			eps2  = [ [ matrixRow objectAtIndex: 1 ] doubleValue ];
			rmin2 = [ [ matrixRow objectAtIndex: 2 ] doubleValue ];
			
			if (([ params14Scaled objectForKey: atom1] != nil ) ) 
			{
				matrixRow =  [ params14Scaled objectForKey: atom1];
				eps1  = [ [ matrixRow objectAtIndex: 1 ] doubleValue ];
				rmin1 = [ [ matrixRow objectAtIndex: 2 ] doubleValue ];
			}
			if (([ params14Scaled objectForKey: atom2] != nil ) ) 
			{
				matrixRow =  [ params14Scaled objectForKey: atom2] ;
				eps2  = [ [ matrixRow objectAtIndex: 1 ] doubleValue ];
				rmin2 = [ [ matrixRow objectAtIndex: 2 ] doubleValue ];
			}         
			
			[row addObject: atom1 ];
			[row addObject: atom2 ];
			[row addObject: [NSNumber numberWithDouble: (sqrt(eps1*eps2)) ]];
			[row addObject: [NSNumber numberWithDouble: (rmin1+rmin2) ]];
			[matrix14 extendMatrixWithRow: row];
			[row removeAllObjects];
			index = [indexes indexGreaterThanIndex: index];
		}
	}
	interaction = [NSMutableDictionary dictionaryWithCapacity: 1];
	[interaction setValue: @"1-4Interaction" forKey: @"InteractionType"];
	[interaction setValue: [NSNumber numberWithInt: 2] forKey: @"ElementsPerInteraction"];
	[interaction setValue: matrix14 forKey: @"Matrix"];
	
	[vdw14Matrix release];
	return interaction;
}


- (id) _buildUreyBradleyForAtoms: (NSMutableArray*) atomNames 
                 withBondedAtoms: (NSMutableArray*) bondedAtomsList
{
	int i, j, atom;
	NSEnumerator *bondedAtomsListEnum;
	NSMutableArray *angle;
	NSMutableDictionary* interaction;
	AdMutableDataMatrix *ubMatrix;
	id bondedAtoms; 

	ubMatrix = [[AdMutableDataMatrix alloc] 
			initWithNumberOfColumns:0 
			columnHeaders: nil
			columnDataTypes: nil];
	[ubMatrix autorelease];		
	bondedAtomsListEnum = [bondedAtomsList objectEnumerator];

	atom = 0;	
	while((bondedAtoms = [bondedAtomsListEnum nextObject]))
	{
		if([bondedAtoms count] > 1)
			for(i=0; i<[bondedAtoms count] - 1; i++)
				for(j=i+1; j < [bondedAtoms count]; j++)	
				{
					angle = [NSMutableArray arrayWithCapacity: 3];
					[angle addObject: [bondedAtoms objectAtIndex: i]];
					[angle addObject: [NSNumber numberWithInt: atom]];
					[angle addObject: [bondedAtoms objectAtIndex: j]];
					[ubMatrix extendMatrixWithRow: angle];
				}
		atom++;
	}
	interaction = [NSMutableDictionary dictionaryWithCapacity: 1];
	[interaction setValue: @"UreyBradley" forKey: @"InteractionType"];
	[interaction setValue: [NSNumber numberWithInt: 3] forKey: @"ElementsPerInteraction"];
	[interaction setValue: ubMatrix forKey: @"Matrix"];
	[self _findParametersForInteractions: interaction ofAtoms: atomNames];
   
   ubMatrix = [ interaction objectForKey: @"Matrix"];
	GSPrintf(buildOutput,@"There are %d UreyBradley interactions\n", [ubMatrix numberOfRows]);
	[buildString appendFormat: @"\t\tThere are %d UreyBradley interactions\n", [ubMatrix numberOfRows]];
	return interaction;
}

- (id) _findMassesForAtoms: (NSMutableArray*) atomNames
{
	int i;
	AdMutableDataMatrix* massMatrix;
	NSMutableDictionary* interaction;
	NSMutableArray* row;

	massMatrix = [[AdMutableDataMatrix alloc]
			initWithNumberOfColumns: 0
			columnHeaders: 0
			columnDataTypes: 0];
	[massMatrix autorelease];		

	for(i=0; i<[atomNames count]; i++)
	{
		row = [NSMutableArray arrayWithCapacity: 1];
		[row addObject: [NSNumber numberWithInt: i]];
		[massMatrix extendMatrixWithRow: row];
	}

	interaction = [NSMutableDictionary dictionaryWithCapacity: 1];
	[interaction setValue: @"Mass" forKey: @"InteractionType"];
	[interaction setValue: massMatrix forKey: @"Matrix"];
	[interaction setValue: [NSNumber numberWithInt: 1] forKey: @"ElementsPerInteraction"];
	[self _findParametersForInteractions: interaction ofAtoms: atomNames];

	return interaction;
}

- (id) _buildNonBondedForAtoms: (NSMutableArray*) atomNames 
		bondedInteractions: (NSMutableDictionary*) bondedInteractions
		atomsPerResidue: (NSMutableArray*) atomsPerResidue
{
	int i, j, elementsPerInteraction;
	int noAtoms, noResidues, index, residueStart, residueEnd;
	NSMutableArray* nonbonded; 
	NSMutableIndexSet* indexes, *topIndexes, *interactionIndexes;
	NSEnumerator *interactionEnum;
	NSRange indexRange;
	id topology, matrix, interaction;

	noAtoms = [atomNames count];
	noResidues = [atomsPerResidue count];
	nonbonded = [NSMutableArray arrayWithCapacity: 1];
	
	for(i=0; i<noAtoms-1; i++)
	{
		indexRange.location = i+1;
		indexRange.length = noAtoms - indexRange.location;
		indexes = [NSMutableIndexSet indexSetWithIndexesInRange: indexRange];
		[nonbonded addObject: indexes];
	}

	residueEnd = 0;
	interactionIndexes = [NSMutableIndexSet indexSet];

	index = 0;
	for(i=0; i<[nonbonded count]; i++)
		index += [[nonbonded objectAtIndex:i] count];

	NSDebugLLog(@"ULInteractionsBuilder", 
		@"There are %d nonbonded interactions before removal", 
		index);

	for(i=0; i<noResidues; i++)
	{	

		residueStart = residueEnd;
		residueEnd += [[atomsPerResidue objectAtIndex: i] intValue];

		//the last atom has no nonbonded interactions (newtons third law)
		//so we shouldnt iterate on it

		if(residueEnd == noAtoms)
			residueEnd = residueEnd - 1;

		interactionEnum = [[bondedInteractions allValues] objectEnumerator];
		
		while((topology = [interactionEnum nextObject]))
		{
			if([[topology valueForKey:@"InteractionType"] isEqual: @"VDW"])
				continue;			

			topIndexes =  [[topology valueForKey:@"ResidueInteractions"] 
						objectAtIndex: i];
			elementsPerInteraction = [[topology valueForKey:@"ElementsPerInteraction"] intValue];
			matrix = [topology valueForKey:@"Matrix"];
					
			//for each of the interactions in this residue go through
			//all the atoms in the residue. If they are invovled in the interaction
			//remove them from the nonbonded index set
			
			index = [topIndexes firstIndex];
			while(index != NSNotFound)
			{
				interaction = [matrix row: index];
				for(j=0; j<elementsPerInteraction; j++)
					[interactionIndexes addIndex: [[interaction objectAtIndex: j] intValue]];

				for(j=residueStart; j<residueEnd; j++)
					if([interactionIndexes containsIndex: j])
						[[nonbonded objectAtIndex: j] removeIndexes: interactionIndexes];
				
				index = [topIndexes indexGreaterThanIndex: index];
				[interactionIndexes removeAllIndexes];
			}
		}
	}

	index = 0;
	for(i=0; i<[nonbonded count]; i++)
		index += [[nonbonded objectAtIndex:i] count];

	GSPrintf(buildOutput, @"There are %d nonbonded interactions\n", index);
	[buildString appendFormat: @"\t\tThere are %d nonbonded interactions\n", index];

	return nonbonded;
}

/*************************

Interactions Per Residue

**************************/

- (id) _residueIndexes: (NSMutableArray*) atomsPerResidue
{
	NSIndexSet *indexSet;
	NSRange indexRange;
	NSEnumerator* residueEnum;
	NSMutableArray* residueIndexes;
	id number;

	residueIndexes = [NSMutableArray arrayWithCapacity:1];
	residueEnum = [atomsPerResidue objectEnumerator];
	indexRange.location = 0;
	while((number = [residueEnum nextObject]))
	{
		indexRange.length = [number intValue];	
		indexSet = [NSIndexSet indexSetWithIndexesInRange: indexRange];
		[residueIndexes addObject: indexSet];
		indexRange.location += indexRange.length;
	}	

	return residueIndexes;
}


- (id) _subsetOfInteractions: (NSMutableDictionary*) interaction
	 withIndexesInRange: (NSRange) indexRange 
	 startAt: (int) startIndex
	 endAt: (int) endIndex
{
	int i, j, elementsPerInteraction;
	AdDataMatrix* interactionMatrix;
	NSMutableIndexSet* searchSet, *resultSet;
	id row, index, end;

	interactionMatrix = [interaction valueForKey:@"Matrix"];
	elementsPerInteraction = [[interaction valueForKey:@"ElementsPerInteraction"] intValue];
	searchSet = [NSIndexSet indexSetWithIndexesInRange: indexRange];
	resultSet = [NSMutableIndexSet indexSet];
	end = [NSNumber numberWithInt: endIndex +1];	

	for(j=startIndex; j<[interactionMatrix numberOfRows]; j++)
	{
		row = [interactionMatrix row: j];
		if([row containsObject: end])
			break;

		for(i=0; i<elementsPerInteraction; i++)
		{
			index = [row objectAtIndex: i];
			if([searchSet containsIndex: [index intValue]])
			{
				[resultSet addIndex: j];
				break;
			}
		}
	}

	return resultSet;
}

- (void) _interactionsPerResidue: (NSMutableDictionary*) interaction residueIndexes: (NSArray*) residueIndexes
{
	int startIndex, endIndex, i, noResidues;
	NSRange indexRange;
	NSMutableArray* subsetIndexes;	//array of IndexSets - one for each residue
	NSMutableArray* interactionsPerResidue;
	id indexSet;

	subsetIndexes = [NSMutableArray arrayWithCapacity: 1];
	interactionsPerResidue = [NSMutableArray arrayWithCapacity: 1];
	startIndex = endIndex = 0;
	noResidues = [residueIndexes count];

	for(i=0; i< noResidues; i++)
	{
		indexRange.location = [[residueIndexes objectAtIndex: i] firstIndex];
		indexRange.length = [[residueIndexes objectAtIndex: i] count];

		if(i!= noResidues - 1)
			endIndex = [[residueIndexes objectAtIndex: i +1] lastIndex];
		else
			endIndex = [[residueIndexes lastObject] lastIndex];
		
		indexSet = [self _subsetOfInteractions: interaction 
					withIndexesInRange: indexRange 
					startAt: startIndex
					endAt: endIndex];

		[subsetIndexes addObject: indexSet];
		[interactionsPerResidue addObject: 
			[NSNumber numberWithInt: [indexSet count]]];
		
		if([indexSet count] != 0)
			startIndex = [indexSet firstIndex];
	}

	[interaction setValue: subsetIndexes forKey: @"ResidueInteractions"];
	[interaction setValue: interactionsPerResidue forKey: @"InteractionsPerResidue"];
	NSDebugLLog(@"ULInteractionsBuilder",
		@"%@\n%@", 
		[interaction objectForKey: @"InteractionType"], 
		interactionsPerResidue);
}

- (void) _setBuildError: (NSError**) buildError
{
	NSMutableDictionary* userInfo;

	[errorString insertString: @" and have been omitted.\n" atIndex: 0];
	[errorString insertString: @"The following interactions were missing parameters\n"
		atIndex: 0];

	userInfo = [NSMutableDictionary dictionary];
	[userInfo setObject: @"Errors while retrieving parameters for system.\n"
		forKey: NSLocalizedDescriptionKey];
	[userInfo setObject: errorString
		forKey: @"ULBuildErrorDetailedDesciptionKey"];
	[userInfo setObject: @"Depending on the force field these omissions may or may not be critical.\n"
		forKey: @"ULBuildErrorRecoverySuggestionKey"];
	
	*buildError = [NSError errorWithDomain: @"ULBuildErrorDomain"
				code: 4
				userInfo: userInfo];
}

/********************

Public Methods

*********************/

- (id) initForForceField: (NSString*)  aString
{
	NSString* libraryPath, *parameterFileName;

	if((self = [super init]))
	{
		if(aString == nil)
		{
			forceField = [[NSUserDefaults standardUserDefaults]
					objectForKey: @"DefaultForceField"];
			if(forceField == nil)
				forceField = @"Enzymix";
		}
		else
			forceField = aString;

		[forceField retain];
		
		parameterFileName = [NSString stringWithFormat: @"%@Parameters.ffml",
				forceField];

		NSDebugLLog(@"ULInteractionsBuilder", 
			@"Using parameter library %@", parameterFileName);
		libraryPath = [[[NSBundle bundleForClass: [self class]] resourcePath] 	
				stringByAppendingPathComponent: @"ForceFields"];
		libraryPath = [libraryPath stringByAppendingPathComponent: forceField];
		libraryPath = [libraryPath stringByAppendingPathComponent: parameterFileName];
		NSDebugLLog(@"ULInteractionsBuilder", 
			@"Parameter library path %@", libraryPath);

		NSDebugLLog(@"ULInteractionsBuilder", @"Creating document tree for parmLib");
		parameterLibrary = [[ULParameterTree alloc] 
					documentTreeForXMLFile: libraryPath];

		if(parameterLibrary == nil)
		{
			[self release];
			[NSException raise: NSInternalInconsistencyException
				format: @"Unable to locate parameter library for force field %@",
				forceField];
		}		

		NSDebugLLog(@"ULInteractionsBuilder", @"Complete");

		//Temporary
		unitsToConvert = [NSArray arrayWithObjects: 
					@"KCalMol", 
					@"Degree",
					@"KCalMol-2",
					nil];
		[unitsToConvert retain];			

		constantForUnit = [NSDictionary dictionaryWithObjectsAndKeys:
					[NSNumber numberWithDouble: BOND_FACTOR], @"KCalMol",
					[NSNumber numberWithDouble: DEG_2_RAD], @"Degree", 
					[NSNumber numberWithDouble: sqrt(BOND_FACTOR)],
					@"KCalMol-2", nil];
		[constantForUnit retain];		

		forceFieldInfo = [ULForceFieldInformation objectForForceField: forceField];
		[forceFieldInfo retain];
	}
	
	return self;
}

- (void) dealloc
{
	[forceField release];
	[parameterLibrary release];
	[unitsToConvert release];
	[constantForUnit release];
	[forceFieldInfo release];
	[super dealloc];
}	

- (NSString*) forceField
{
	return [[forceField retain] autorelease];
}

- (id) buildInteractionsForConfiguration: (id) configuration 
		error: (NSError**) buildError
		userInfo: (NSString**) buildInfo
{
	NSError *error = nil;
	NSMutableDictionary* interactions, *topology, *nonbonded;
	NSMutableArray* libraryNameList;
	NSMutableArray* bondedAtoms;
	NSMutableArray* residueIndexes;
	id interaction, nonbondedInteractions, masses, path;
	
	path = [[NSUserDefaults standardUserDefaults] stringForKey: @"BuildOutput"];
	buildOutput = fopen([path cString], "a");

	[buildString release];
	buildString = [[NSMutableString string] retain];
	if(buildInfo != NULL)
		*buildInfo = buildString;
	errorString = [NSMutableString string];

	interactions = [NSMutableDictionary dictionary];
	nonbonded = [NSMutableDictionary dictionary];
	libraryNameList = [configuration valueForKey: @"LibraryNames"];
	bondedAtoms = [configuration valueForKey:@"BondedAtoms"];
	topology = [NSMutableDictionary dictionary];

	NSDebugLLog(@"ULInteractionsBuilder", @"%@", 
		[configuration valueForKey: @"AtomsPerResidue"]);

	residueIndexes = [self _residueIndexes: [configuration valueForKey: @"AtomsPerResidue"]];

	[buildString appendString: @"\nBuilding interaction lists\n"];

	/*
   	 * Build the bonded interactions first
	 */
	
	[buildString appendString: @"\tHarmonicBonds:\n"];
	interaction = [self _buildBondsForAtoms: libraryNameList withBondedAtoms: bondedAtoms];
	[interactions setObject: interaction forKey: [interaction valueForKey: @"InteractionType"]];
	[self _interactionsPerResidue: interaction residueIndexes: residueIndexes];
	
	[buildString appendString: @"\tHarmonicAngles:\n"];
	interaction = [self _buildAnglesForAtoms: libraryNameList withBondedAtoms: bondedAtoms];
	[interactions setObject: interaction forKey: [interaction valueForKey: @"InteractionType"]];
	[self _interactionsPerResidue: interaction residueIndexes: residueIndexes];
	
	[buildString appendString: @"\tFourierTorsions:\n"];
	   interaction = [self _buildTorsionsForAtoms: libraryNameList 	
			withBondedAtoms: bondedAtoms 
			bonds: [interactions valueForKeyPath: @"HarmonicBond.Matrix"]];
	[interactions setObject: interaction forKey: [interaction valueForKey: @"InteractionType"]];
	[self _interactionsPerResidue: interaction residueIndexes: residueIndexes];
	
	//testing improper torsions

	[buildString appendString: @"\tHarmonicImproperTorsions:\n"];
	interaction = [self _buildImproperTorsionsForAtoms: libraryNameList 	
			withBondedAtoms: bondedAtoms 
			bonds: [interactions valueForKeyPath: @"HarmonicBond.Matrix"]];
	[interactions setObject: interaction forKey: [interaction valueForKey: @"InteractionType"]];
	[self _interactionsPerResidue: interaction residueIndexes: residueIndexes];

	[topology setValue: interactions forKey: @"Bonded"];
	
	/*
	 * Now the nonbonded interactions
	 */

	[buildString appendString: @"\tNonbonded Interactions\n"];
	nonbondedInteractions = [self _buildNonBondedForAtoms: [configuration valueForKey:@"AtomNames"]
					bondedInteractions: [topology valueForKey:@"Bonded"]
					atomsPerResidue: [configuration valueForKey:@"AtomsPerResidue"]];	
	[nonbonded setValue: nonbondedInteractions forKey: @"Interactions"];
	
	interaction = [self _buildVDWForAtoms: libraryNameList withBondedAtoms: bondedAtoms];
	[self _interactionsPerResidue: interaction residueIndexes: residueIndexes];
	[nonbonded setValue: interaction forKey: @"VDWParameters"];

	[topology setValue: nonbonded forKey:@"Nonbonded"];
   //build the 1-4 VDW and electrostatic if the ff has them:
   if([ [forceFieldInfo interactions] containsObject: @"1-4Interaction"])
   {
      interaction = [self _build14ForAtoms: libraryNameList 
                     withBondedInteractions: [topology valueForKey:@"Bonded"]
                     withVDWAtoms: [nonbonded objectForKey: @"VDWParameters"] ];
      [self _interactionsPerResidue: interaction residueIndexes: residueIndexes];
      [ [topology objectForKey: @"Bonded" ] setObject: interaction 
         forKey: @"1-4Interaction" ];
   }
   //build the UreyBradley Interactions
   if([ [forceFieldInfo interactions] containsObject: @"UreyBradley"])
   {
      interaction = [self _buildUreyBradleyForAtoms: libraryNameList
                                    withBondedAtoms: bondedAtoms];
      [self _interactionsPerResidue: interaction residueIndexes: residueIndexes];
      [ [topology objectForKey: @"Bonded" ] setObject: interaction 
         forKey: @"UreyBradley" ];
   }


	//we have to retrieve the masses as if they were an interaction
	
	interaction = [self _findMassesForAtoms: libraryNameList];

	//now we will extract the masses as an array

	masses = [[interaction valueForKey:@"Matrix"] column: 1];
 	[configuration setValue: masses forKey: @"Masses"];
	/*
	 * Create an AdDataSource from all the information
	 * First we have to convert the information into the correct
	 * format then use it to initialise the data source
	 */
	
	fclose(buildOutput);
	[buildString appendString: @"\nCompleted interactions build\n"];

	if([errorString length] != 0)
	{
		[self _setBuildError: &error];
		AdLogError(error);
		if(buildError != NULL)
			*buildError = error;
	}		

	return [self createDataSourceFromConfiguration: configuration
		topology: topology];
}


@end

//This category is for translating the current information generated by the
//build into something close to the format we would like it to be.
//The exact format will depend on a number of factors (ffml format,
//information extracted from ffml, final core requirements).
//The forms and output here represent an intermediate step.

@implementation ULInteractionsBuilder (TemporaryDataSourceConversions)

- (AdMutableDataSource*) _initDataSourceFromConfiguration: (id) configuration 
			topology: (id) topology
{
	int i;
	AdDataMatrix* matrix;
	AdMutableDataMatrix *properties, *coordinates;
	AdMutableDataSource* dataSource;
	NSArray *headers;
	NSString* vdwType;
	id column;

	headers = [NSArray arrayWithObjects: @"ForceFieldName",
			@"PDBName",
			@"PartialCharge",
			@"Mass",
			nil];

	//Element Information

	coordinates = [configuration valueForKey: @"Coordinates"];
	[coordinates setName: @"Coordinates"];
	
	properties = [AdMutableDataMatrix new];
	[properties autorelease];		
	[properties extendMatrixWithColumn: 
		[configuration objectForKey: @"LibraryNames"]];
	[properties extendMatrixWithColumn:
		[configuration objectForKey: @"AtomNames"]];
	[properties extendMatrixWithColumn:
		[configuration objectForKey: @"PartialCharges"]];
	[properties extendMatrixWithColumn:
		[configuration objectForKey: @"Masses"]];
	[properties setColumnHeaders: headers];
	[properties setName: @"ElementProperties"];
	
	//Add the van der waals parameters to the properties
	//The first column of matrix is just indexes

	matrix = [topology valueForKeyPath: @"Nonbonded.VDWParameters.Matrix"];
	for(i=1; i<[matrix numberOfColumns]; i++)
	{
		column = [matrix column: i];
		[properties extendMatrixWithColumn: column];
	}	
	
	vdwType = [forceFieldInfo vdwType];
	[properties setHeaderOfColumn: 4 
		to: [[forceFieldInfo namesOfParametersOfInteraction: vdwType]
			objectAtIndex: 0]]; 
	[properties setHeaderOfColumn: 5 
		to: [[forceFieldInfo namesOfParametersOfInteraction: vdwType]
			objectAtIndex: 1]]; 
	dataSource = [[AdMutableDataSource alloc] initWithElementProperties: properties
			configuration: coordinates];
	
	return [dataSource autorelease];
}

- (void) _addBondedInteractionsTo: (AdMutableDataSource*) dataSource fromTopology: (id) topology
{
	int i, elementsPerInteraction;
	AdDataMatrix *matrix;
	AdMutableDataMatrix *groups, *parameters;
	NSEnumerator* bondedEnum;
	id  bondedTop;

	NSString* interaction;

	bondedEnum = [[topology valueForKey: @"Bonded"] objectEnumerator];
	while((bondedTop = [bondedEnum nextObject]))
	{
		interaction = [bondedTop objectForKey: @"InteractionType"];
		elementsPerInteraction = [[bondedTop objectForKey: @"ElementsPerInteraction"] intValue];
		groups = [[AdMutableDataMatrix new] autorelease];
		parameters = [[AdMutableDataMatrix new] autorelease];
		matrix = [bondedTop objectForKey: @"Matrix"];
		//FIXME: Interactions are added to topology even if there are none!
		if([matrix numberOfRows] > 0)
		{
			for(i=0; i<elementsPerInteraction; i++)
				[groups extendMatrixWithColumn: [matrix column: i]];

			for(i=elementsPerInteraction; i<[matrix numberOfColumns]; i++)
				[parameters extendMatrixWithColumn: [matrix column: i]];

			[groups setName: [NSString stringWithFormat:
						@"%@Groups", interaction]];
			[parameters setName: [NSString stringWithFormat: 
						@"%@Parameters", interaction]];

			[parameters setColumnHeaders:
				[forceFieldInfo namesOfParametersOfInteraction: interaction]];
			[dataSource addInteraction: interaction
				withGroups: groups
				parameters: parameters
				constraint: nil
				toCategory: @"Bonded"];
		}		
	}
}

//This conversion requires the most hacking ...
//Assuming VDWParameters exist and that the force field
//contains VDW and Electrostatic terms.
- (void) _addNonbondedInteractionsTo: (AdMutableDataSource*) dataSource fromTopology: (id) topology
{
	NSString* vdwType;

	//What type of vdw parameters does this force field use?
	vdwType = [forceFieldInfo vdwType];
	
	//Add interactions
	[dataSource addInteraction: vdwType
		withGroups: nil
		parameters: nil
		constraint: nil
		toCategory: @"Nonbonded"];
	[dataSource addInteraction: @"CoulombElectrostatic"
		withGroups: nil
		parameters: nil
		constraint: nil
		toCategory: @"Nonbonded"];

	[dataSource setNonbondedPairs: [topology valueForKeyPath: @"Nonbonded.Interactions"]];
}

- (void) _addGroupPropertiesTo: (id) dataSource fromConfiguration: (id) configuration
{
	int sequenceNumber, residueNumber;
	NSEnumerator* sequenceEnum, *residueEnum;
	AdMutableDataMatrix* groupProperties;
	NSMutableArray* array;
	id sequence, residue, atomsPerResiude;

	sequenceEnum = [[configuration objectForKey: @"Sequences"] objectEnumerator];
	sequenceNumber = residueNumber = 0;
	array = [NSMutableArray array];
	groupProperties = [AdMutableDataMatrix new];
	[groupProperties autorelease];
	atomsPerResiude = [configuration objectForKey: @"AtomsPerResidue"];
	while((sequence = [sequenceEnum nextObject]))
	{
		residueEnum = [sequence objectEnumerator];
		while((residue = [residueEnum nextObject]))
		{
			[array addObject: residue];
			[array addObject: [NSNumber numberWithInt: sequenceNumber]];
			[array addObject: [atomsPerResiude objectAtIndex: residueNumber]];
			[groupProperties extendMatrixWithRow: array];
			residueNumber++;
			[array removeAllObjects];
		}		
				
		sequenceNumber++;		
	}

	[groupProperties setColumnHeaders: 
		[NSArray arrayWithObjects:
			@"Residue Name",
			@"Chain", 
			@"Atoms",
			nil]];
	[groupProperties setName: @"GroupProperties"];		

	[dataSource setGroupProperties: groupProperties];
}


- (id) createDataSourceFromConfiguration: (id) configuration topology: (id) topology
{
	AdMutableDataSource* dataSource;
	
	NSDebugLLog(@"ULInteractionsBuilder", 
		@"Creating data source from configuration");
	dataSource = [self _initDataSourceFromConfiguration: configuration
			topology: topology];
	NSDebugLLog(@"ULInteractionsBuilder", 
		@"Adding group properties from configuration");
	[self _addGroupPropertiesTo: dataSource 
		fromConfiguration: configuration];
	NSDebugLLog(@"ULInteractionsBuilder", 
		@"Adding bonded interactions");
	[self _addBondedInteractionsTo: dataSource
		fromTopology: topology];
	NSDebugLLog(@"ULInteractionsBuilder", 
		@"Adding nonbonded");
	[self _addNonbondedInteractionsTo: dataSource 
		fromTopology: topology];
	NSDebugLLog(@"ULInteractionsBuilder", 
		@"Complete");
	
	return [[dataSource copy] autorelease];
}

@end