forked from ccsb-scripps/AutoDock-GPU
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetparameters.cpp
More file actions
2629 lines (2481 loc) · 98 KB
/
getparameters.cpp
File metadata and controls
2629 lines (2481 loc) · 98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
AutoDock-GPU, an OpenCL implementation of AutoDock 4.2 running a Lamarckian Genetic Algorithm
Copyright (C) 2017 TU Darmstadt, Embedded Systems and Applications Group, Germany. All rights reserved.
For some of the code, Copyright (C) 2019 Computational Structural Biology Center, the Scripps Research Institute.
AutoDock is a Trade Mark of the Scripps Research Institute.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <cstdint>
#include <fstream>
#include <filesystem>
#include <algorithm>
#include <cctype>
#include <locale>
#include <sys/stat.h>
#include "getparameters.h"
// trim from start (in place)
static inline void ltrim(std::string &s) {
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int ch) {
return !std::isspace(ch);
}));
}
// trim from end (in place)
static inline void rtrim(std::string &s) {
s.erase(std::find_if(s.rbegin(), s.rend(), [](int ch) {
return !std::isspace(ch);
}).base(), s.end());
}
// trim from both ends (in place)
static inline void trim(std::string &s) {
ltrim(s);
rtrim(s);
}
int dpf_token(const char* token)
{
const struct {
const char* string;
const int token;
bool evaluate; // need to evaluate parameters (and either use it or make sure it's a certain value)
} supported_dpf_tokens [] = {
{"ligand", DPF_MOVE, true}, /* movable ligand file name */
{"move", DPF_MOVE, true}, /* movable ligand file name */
{"fld", DPF_FLD, true}, /* grid data file name */
{"map", DPF_MAP, true}, /* grid map specifier */
{"about", DPF_ABOUT, false}, /* rotate about */
{"tran0", DPF_TRAN0, true}, /* translate (needs to be "random") */
{"axisangle0", DPF_AXISANGLE0, true}, /* rotation axisangle (needs to be "random") */
{"quaternion0", DPF_QUATERNION0, true}, /* quaternion (of rotation, needs to be "random") */
{"quat0", DPF_QUAT0, true}, /* quaternion (of rotation, needs to be "random") */
{"dihe0", DPF_DIHE0, true}, /* number of dihedrals (needs to be "random") */
{"ndihe", DPF_NDIHE, false}, /* number of dihedrals (is in pdbqt) */
{"torsdof", DPF_TORSDOF, false}, /* torsional degrees of freedom (is in pdbqt) */
{"intnbp_coeffs", DPF_INTNBP_COEFFS, true}, /* internal pair energy coefficients */
{"intnbp_r_eps", DPF_INTNBP_REQM_EPS, true}, /* internal pair energy coefficients */
{"runs", DPF_RUNS, true}, /* number of runs */
{"ga_run", DPF_GALS, true}, /* run a number of runs */
{"gals_run", DPF_GALS, true}, /* run a number of runs */
{"outlev", DPF_OUTLEV, false}, /* output level */
{"rmstol", DPF_RMSTOL, true}, /* RMSD cluster tolerance */
{"extnrg", DPF_EXTNRG, false}, /* external grid energy */
{"intelec", DPF_INTELEC, true}, /* calculate ES energy (needs not be "off") */
{"smooth", DPF_SMOOTH, true}, /* smoothing range */
{"seed", DPF_SEED, true}, /* random number seed */
{"e0max", DPF_E0MAX, false}, /* simanneal max inital energy (ignored) */
{"set_ga", DPF_SET_GA, false}, /* use genetic algorithm (yes, that's us) */
{"set_sw1", DPF_SET_SW1, false}, /* use Solis-Wets (we are by default)*/
{"set_psw1", DPF_SET_PSW1, false}, /* use pseudo Solis-Wets (nope, SW) */
{"analysis", DPF_ANALYSIS, false}, /* analysis data (we're doing it) */
{"ga_pop_size", GA_pop_size, true}, /* population size */
{"ga_num_generations", GA_num_generations, true}, /* number of generations */
{"ga_num_evals", GA_num_evals, true}, /* number of evals */
{"ga_window_size", GA_window_size, false}, /* genetic algorithm window size */
{"ga_elitism", GA_elitism, false}, /* GA parameters: */
{"ga_mutation_rate", GA_mutation_rate, true}, /* The ones set to true */
{"ga_crossover_rate", GA_crossover_rate, true}, /* have a corresponding */
{"ga_cauchy_alpha", GA_Cauchy_alpha, false}, /* parameter in AD-GPU */
{"ga_cauchy_beta", GA_Cauchy_beta, false}, /* the others ignored */
{"sw_max_its", SW_max_its, true}, /* local search iterations */
{"sw_max_succ", SW_max_succ, true}, /* cons. success limit */
{"sw_max_fail", SW_max_fail, true}, /* cons. failure limit */
{"sw_rho", SW_rho, false}, /* rho - is 1.0 here */
{"sw_lb_rho", SW_lb_rho, true}, /* lower bound of rho */
{"ls_search_freq", LS_search_freq, false}, /* ignored as likely wrong for algorithm here */
{"parameter_file", DPF_PARAMETER_LIBRARY, false}, /* parameter file (use internal currently) */
{"ligand_types", DPF_LIGAND_TYPES, true}, /* ligand types used */
{"output_pop_file", DPF_POPFILE, false}, /* output population to file */
{"flexible_residues", DPF_FLEXRES, true}, /* flexibe residue file name */
{"flexres", DPF_FLEXRES, true}, /* flexibe residue file name */
{"elecmap", DPF_ELECMAP, false}, /* electrostatic grid map (we use fld file basename) */
{"desolvmap", DPF_DESOLVMAP, false}, /* desolvation grid map (we use fld file basename) */
{"dsolvmap", DPF_DESOLVMAP, false}, /* desolvation grid map (we use fld file basename) */
{"unbound_model", DPF_UNBOUND_MODEL, true} /* unbound model (bound|extended|compact) */
};
if (token[0]=='\0')
return DPF_BLANK_LINE;
if (token[0]=='#')
return DPF_COMMENT;
for (int i=0; i<(int)(sizeof(supported_dpf_tokens)/sizeof(*supported_dpf_tokens)); i++){
if(stricmp(supported_dpf_tokens[i].string,token) == 0){
if(supported_dpf_tokens[i].evaluate)
return supported_dpf_tokens[i].token;
else
return DPF_NULL;
break; // found
}
}
return DPF_UNKNOWN;
}
int parse_dpf(
Dockpars* mypars,
Gridinfo* mygrid,
FileList& filelist,
bool get_grid_info = true
)
{
if (mypars->dpffile)
{
std::ifstream file(mypars->dpffile);
if(file.fail()){
printf("\nError: Could not open dpf file %s. Check path and permissions.\n",mypars->dpffile);
return 1;
}
std::string dpf_path = get_filepath(mypars->dpffile);
if(dpf_path==".") dpf_path="";
bool check_path = false;
if(dpf_path.size()>0){
dpf_path += "/";
check_path = true;
}
mypars->elec_min_distance = 0.5; // default for AD4
std::string line;
char tempstr[256], argstr[256];
char* args[2];
int tempint, i, len;
float tempfloat;
int line_count = 0;
int ltype_nr = 0;
int mtype_nr = 0;
// use a 4-times longer runway for atom types definable in one dpf
// - this is to allow more reactive types and to enable the strategy
// to define all possible atom types for a set of ligands once for
// performance reasons (as they can be read once)
// - each ligand is still going to be limited to MAX_NUM_OF_ATYPES
char ltypes[4*MAX_NUM_OF_ATYPES][4];
memset(ltypes,0,16*MAX_NUM_OF_ATYPES*sizeof(char));
std::string map_fn;
int idx;
pair_mod* curr_pair;
float paramA, paramB;
int m, n;
char typeA[4], typeB[4];
bool new_device = false; // indicate if current mypars has a new device requested
unsigned int run_cnt=0;
while(std::getline(file, line)) {
line_count++;
trim(line); // Remove leading and trailing whitespace
tempstr[0]='\0';
sscanf(line.c_str(),"%255s",tempstr);
int token_id = dpf_token(tempstr);
if (token_id >= DPF_MOVE ){ // take care of end-comments for regular tokens
int comment_loc = line.find("#");
if(comment_loc>0) line.erase(comment_loc,line.size()-comment_loc);
}
argstr[0] = '\0';
tempint = 0;
tempfloat = -1;
switch(token_id){
case DPF_MOVE: // movable ligand file name
if(!mypars->xml2dlg){
sscanf(line.c_str(),"%*s %255s",argstr);
if(mypars->ligandfile) free(mypars->ligandfile);
if(strincmp(argstr,"empty",5) != 0){
if(check_path && !has_absolute_path(argstr)){
mypars->ligandfile = (char*)malloc((dpf_path.size()+strlen(argstr)+1)*sizeof(char));
strcat(strcpy(mypars->ligandfile, dpf_path.c_str()), argstr);
} else mypars->ligandfile = strdup(argstr);
}
}
break;
case DPF_FLEXRES: // flexibe residue file name
if(!mypars->xml2dlg){
sscanf(line.c_str(),"%*s %255s",argstr);
if(mypars->flexresfile) free(mypars->flexresfile);
if(check_path && !has_absolute_path(argstr)){
mypars->flexresfile = (char*)malloc((dpf_path.size()+strlen(argstr)+1)*sizeof(char));
strcat(strcpy(mypars->flexresfile, dpf_path.c_str()), argstr);
} else mypars->flexresfile = strdup(argstr);
}
break;
case DPF_FLD: // grid data file name
if(get_grid_info){
sscanf(line.c_str(),"%*s %255s",argstr);
// Add the .fld file
if(mypars->fldfile) free(mypars->fldfile);
if(check_path && !has_absolute_path(argstr)){
mypars->fldfile = (char*)malloc((dpf_path.size()+strlen(argstr)+1)*sizeof(char));
strcat(strcpy(mypars->fldfile, dpf_path.c_str()), argstr);
} else mypars->fldfile = strdup(argstr); // this allows using the dpf to set up all parameters but the ligand
// Filling mygrid according to the specified fld file
if (get_gridinfo(mypars->fldfile, mygrid) != 0)
{
printf("\nError: get_gridinfo failed with fld file specified with <%s> parameter at %s:%u.\n",tempstr,mypars->dpffile,line_count);
return 1;
}
}
break;
case DPF_LIGAND_TYPES: // ligand types used
len=-1;
for(i=strlen(tempstr); i<(int)line.size(); i++){
if(isspace(line[i])){ // whitespace
len=-1;
} else{ // not whitespace aka an atom type
if(len<0){ // new type starts
len=i;
ltype_nr++;
if(ltype_nr>4*MAX_NUM_OF_ATYPES){
printf("\nError: Too many atoms types (>%d)defined in the <%s> parameter at %s:%u.\n",4*MAX_NUM_OF_ATYPES,tempstr,mypars->dpffile,line_count);
return 1;
}
}
if(i-len<3){
ltypes[ltype_nr-1][i-len] = line[i];
ltypes[ltype_nr-1][i-len+1] = '\0'; // make extra sure we terminate the string
} else{
printf("\nError: Atom types are limited to 3 characters in <%s> parameter at %s:%u.\n",tempstr,mypars->dpffile,line_count);
return 1;
}
}
}
mtype_nr=0;
break;
case DPF_MAP: // grid map specifier
if(mtype_nr>=ltype_nr){
printf("\nError: More map files specified than atom types at %s:%u (ligand types need to be specified before maps).\n",mypars->dpffile,line_count);
return 1;
}
sscanf(line.c_str(),"%*s %255s",argstr);
map_fn=argstr;
if(mygrid->grid_mapping.size()<2){
printf("Error: fld keyword needs to be placed before <%s> parameter at %s:%u.\n",tempstr,mypars->dpffile,line_count);
return 1;
}
n=-1;
for(m=mygrid->grid_mapping.size()/2; m<mygrid->grid_mapping.size(); m++)
if(map_fn.find(mygrid->grid_mapping[m])!=std::string::npos){
n=m-mygrid->grid_mapping.size()/2;
break;
}
if(n<0){
printf("Error: No matching map file <%s> specified in fld file at %s:%u.\n",argstr,mypars->dpffile,line_count);
return 1;
}
strcpy(argstr,mygrid->grid_mapping[n].c_str());
if(strcmp(argstr,ltypes[mtype_nr])){ // derived type
if(mypars->nr_deriv_atypes==0){ // get the derived atom types started
mypars->deriv_atypes=(deriv_atype*)malloc(sizeof(deriv_atype));
if(mypars->deriv_atypes==NULL){
printf("Error: Cannot allocate memory for derivative type.\n");
return 1;
}
}
idx = add_deriv_atype(mypars,ltypes[mtype_nr],strlen(ltypes[mtype_nr]),true);
if(idx == 0){
printf("Error: Derivative (ligand type %s) names can only be upto 3 characters long.\n",ltypes[mtype_nr]);
return 1;
}
if(idx>0){
idx = mypars->nr_deriv_atypes-1;
strcpy(mypars->deriv_atypes[idx].base_name,argstr);
#ifdef DERIVTYPE_INFO
printf("%i: %s=%s\n",mypars->deriv_atypes[idx].nr,mypars->deriv_atypes[idx].deriv_name,mypars->deriv_atypes[idx].base_name);
#endif
} else{ // same derived type name - make sure base type is the same
idx++; // idx is -type idx-1
if(strcmp(mypars->deriv_atypes[-idx].base_name,argstr)){
printf("Error: Redefinition of ligand type %s with different map types.\n",ltypes[mtype_nr]);
return 1;
}
}
}
mtype_nr++;
break;
case DPF_INTNBP_COEFFS: // internal pair energy coefficients
case DPF_INTNBP_REQM_EPS: // internal pair energy coefficients
if(sscanf(line.c_str(), "%*s %f %f %d %d %3s %3s", ¶mA, ¶mB, &m, &n, typeA, typeB)<6){
printf("Error: Syntax error for <%s>, 6 values are required at %s:%u.\n",tempstr,mypars->dpffile,line_count);
return 1;
}
if(m==n){
printf("Error: Syntax error for <%s>, exponents need to be different at %s:%u.\n",tempstr,mypars->dpffile,line_count);
return 1;
}
if(token_id==DPF_INTNBP_COEFFS){
tempfloat = pow(paramB/paramA*n/m,m-n); // reqm
paramA = paramB*float(m-n)/(pow(tempfloat,n)*m); // epsAB
paramB = tempfloat; // rAB
}
// parameters are sorted out, now add to modpairs
mypars->nr_mod_atype_pairs++;
if(mypars->nr_mod_atype_pairs==1)
mypars->mod_atype_pairs=(pair_mod*)malloc(sizeof(pair_mod));
else
mypars->mod_atype_pairs=(pair_mod*)realloc(mypars->mod_atype_pairs, mypars->nr_mod_atype_pairs*sizeof(pair_mod));
if(mypars->mod_atype_pairs==NULL){
printf("Error: Cannot allocate memory for <%s> pair energy modification.\n at %s:%u.\n",tempstr,mypars->dpffile,line_count);
return 1;
}
curr_pair=&mypars->mod_atype_pairs[mypars->nr_mod_atype_pairs-1];
strcpy(curr_pair->A,typeA);
strcpy(curr_pair->B,typeB);
curr_pair->nr_parameters=4;
curr_pair->parameters=(float*)malloc(curr_pair->nr_parameters*sizeof(float));
if(curr_pair->parameters==NULL){
printf("Error: Cannot allocate memory for <%s> pair energy modification.\n at %s:%u.\n",tempstr,mypars->dpffile,line_count);
return 1;
}
curr_pair->parameters[0]=paramA;
curr_pair->parameters[1]=paramB;
curr_pair->parameters[2]=m;
curr_pair->parameters[3]=n;
#ifdef MODPAIR_INFO
printf("%i: %s:%s",mypars->nr_mod_atype_pairs,curr_pair->A,curr_pair->B);
for(idx=0; idx<curr_pair->nr_parameters; idx++)
printf(",%f",curr_pair->parameters[idx]);
printf("\n");
#endif
break;
case DPF_TRAN0: // translate (needs to be "random")
case DPF_AXISANGLE0: // rotation axisangle (needs to be "random")
case DPF_QUATERNION0: // quaternion (of rotation, needs to be "random")
case DPF_QUAT0: // quaternion (of rotation, needs to be "random")
case DPF_DIHE0: // number of dihedrals (needs to be "random")
sscanf(line.c_str(),"%*s %255s",argstr);
if(stricmp(argstr,"random")){
printf("\nError: Currently only \"random\" is supported as <%s> parameter at %s:%u.\n",tempstr,mypars->dpffile,line_count);
return 1;
}
break;
case DPF_RUNS: // set number of runs
case DPF_GALS: // actually run a search (only if xml2dlg isn't specified)
if(!mypars->xml2dlg){
sscanf(line.c_str(),"%*s %d",&tempint);
if ((tempint >= 1) && (tempint <= MAX_NUM_OF_RUNS)){
mypars->num_of_runs = (int) tempint;
} else{
printf("Error: Value of <%s> at %s:%u must be an integer between 1 and %d.\n",tempstr,mypars->dpffile,line_count,MAX_NUM_OF_RUNS);
return 1;
}
if(token_id!=DPF_RUNS){
// Add the fld file to use
if (!mypars->fldfile){
printf("\nError: No map file on record yet. Please specify a map file before the first ligand.\n");
return 1;
}
if(filelist.fld_files.size()>0){
// only add to fld_files if different from previous one
if(strcmp(mypars->fldfile,filelist.fld_files.back().name.c_str())!=0){
filelist.fld_files.push_back({mypars->fldfile,filelist.mygrids.size()});
// Also add the grid
filelist.mygrids.push_back(*mygrid);
}
} else{
filelist.fld_files.push_back({mypars->fldfile,filelist.mygrids.size()});
filelist.mygrids.push_back(*mygrid);
}
mypars->list_nr++;
mypars->filelist_grid_idx=filelist.fld_files.back().grid_idx;
// If more than one unique protein, cant do map preloading yet
if (filelist.fld_files.size()>1){
filelist.preload_maps=false;
}
// Add the ligand to filelist
if(mypars->ligandfile != NULL){ // free ligand file specified (test for covalent below)
filelist.ligand_files.push_back(mypars->ligandfile);
mypars->free_roaming_ligand = true;
} else{
if(mypars->flexresfile != NULL){
filelist.ligand_files.push_back(mypars->flexresfile);
mypars->free_roaming_ligand = false;
} else{ // error: no ligand specified
printf("\nError: No ligand (either non-covalent (with move) or covalent (with flexres)) specified for run at %s:%u.\n",tempstr,line_count);
return 1;
}
}
// Default resname is filelist basename
if(mypars->resname) free(mypars->resname);
len=filelist.ligand_files.back().size()-6; // .pdbqt = 6 chars
if(len>0){
mypars->resname = (char*)malloc((len+1)*sizeof(char));
strncpy(mypars->resname,filelist.ligand_files.back().c_str(),len); // Default is ligand file basename
mypars->resname[len]='\0';
} else mypars->resname = strdup("docking"); // Fallback to old default
filelist.resnames.push_back(mypars->resname);
if(new_device){
mypars->dev_pool_nr=mypars->dev_pool.size();
mypars->dev_pool.push_back(mypars->devnum);
}
// Before pushing parameters and grids back make sure
// the filename pointers are unique
if(filelist.mypars.size()>0){ // mypars and mygrids have same size
if((filelist.mypars.back().flexresfile) &&
(filelist.mypars.back().flexresfile==mypars->flexresfile))
mypars->flexresfile=strdup(mypars->flexresfile);
if((filelist.mypars.back().xrayligandfile) &&
(filelist.mypars.back().xrayligandfile==mypars->xrayligandfile))
mypars->xrayligandfile=strdup(mypars->xrayligandfile);
}
// Add the parameter block now that resname is set
filelist.mypars.push_back(*mypars);
}
} else{
if(token_id!=DPF_RUNS) run_cnt++;
if((mypars->list_nr>0) && (run_cnt>=mypars->list_nr)) return 0; // finished
}
break;
case DPF_INTELEC: // calculate ES energy (needs not be "off")
sscanf(line.c_str(),"%*s %255s",argstr);
if(stricmp(argstr,"off")==0){
printf("\nError: \"Off\" is not supported as <%s> parameter at %s:%u.\n",tempstr,mypars->dpffile,line_count);
return 1;
}
break;
case DPF_SMOOTH: // smoothing range
sscanf(line.c_str(),"%*s %f",&tempfloat);
// smooth is measured in Angstrom
if ((tempfloat >= 0.0f) && (tempfloat <= 0.5f)){
mypars->smooth = tempfloat;
} else{
printf("Error: Value of <%s> at %s:%u must be a float between 0 and 0.5.\n",tempstr,mypars->dpffile,line_count);
return 1;
}
break;
case DPF_SEED: // random number seed
m=0; n=0; i=0;
if(sscanf(line.c_str(),"%*s %d %d %d",&m, &n, &i)>0){ // one or more numbers
mypars->seed[0]=m; mypars->seed[1]=n; mypars->seed[2]=i;
} else // only warn here to not crash on unsupported values (we have a different RNG so if they'd be used they'd be useless to us anyway)
printf("Warning: Only numerical values currently supported for <%s> at %s:%u.\n",tempstr,mypars->dpffile,line_count);
break;
case DPF_RMSTOL: // RMSD clustering tolerance
sscanf(line.c_str(),"%*s %f",&tempfloat);
if (tempfloat > 0.0){
mypars->rmsd_tolerance = tempfloat;
} else{
printf("Error: Value of <%s> at %s:%u must be greater than 0.\n",tempstr,mypars->dpffile,line_count);
return 1;
}
break;
case GA_pop_size: // population size
sscanf(line.c_str(),"%*s %d",&tempint);
if ((tempint >= 2) && (tempint <= MAX_POPSIZE)){
mypars->pop_size = (unsigned long) (tempint);
} else{
printf("Error: Value of <%s> at %s:%u must be an integer between 2 and %d.\n",tempstr,mypars->dpffile,line_count,MAX_POPSIZE);
return 1;
}
break;
case GA_num_generations: // number of generations
tempint = 0;
sscanf(line.c_str(),"%*s %d",&tempint);
if ((tempint > 0) && (tempint < 16250000)){
mypars->num_of_generations = (unsigned long) tempint;
} else{
printf("Error: Value of <%s> at %s:%u must be between 0 and 16250000.\n",tempstr,mypars->dpffile,line_count);
return 1;
}
break;
case GA_num_evals: // number of evals
sscanf(line.c_str(),"%*s %d",&tempint);
if ((tempint > 0) && (tempint < 0x7FFFFFFF)){
mypars->num_of_energy_evals = (unsigned long) tempint;
mypars->nev_provided = true;
} else{
printf("Error: Value of <%s> at %s:%u must be between 0 and 2^31-1.\n",tempstr,mypars->dpffile,line_count);
return 1;
}
break;
case GA_mutation_rate: // mutation rate
sscanf(line.c_str(),"%*s %f",&tempfloat);
tempfloat*=100.0;
if ((tempfloat >= 0.0) && (tempfloat < 100.0)){
mypars->mutation_rate = tempfloat;
} else{
printf("Error: Value of <%s> at %s:%u must be a float between 0 and 1.\n",tempstr,mypars->dpffile,line_count);
return 1;
}
break;
case GA_crossover_rate: // crossover rate
sscanf(line.c_str(),"%*s %f",&tempfloat);
tempfloat*=100.0;
if ((tempfloat >= 0.0) && (tempfloat <= 100.0)){
mypars->crossover_rate = tempfloat;
} else{
printf("Error: Value of <%s> at %s:%u must be a float between 0 and 1.\n",tempstr,mypars->dpffile,line_count);
return 1;
}
break;
case SW_max_its: // local search iterations
sscanf(line.c_str(),"%*s %d",&tempint);
if ((tempint > 0) && (tempint < 262144)){
mypars->max_num_of_iters = (unsigned long) tempint;
} else{
printf("Error: Value of <%s> at %s:%u must be an integer between 1 and 262143.\n",tempstr,mypars->dpffile,line_count);
return 1;
}
break;
case SW_max_succ: // cons. success limit
case SW_max_fail: // cons. failure limit
sscanf(line.c_str(),"%*s %d",&tempint);
if ((tempint > 0) && (tempint < 256)){
mypars->cons_limit = (unsigned long) (tempint);
} else{
printf("Error: Value of <%s> at %s:%u must be an integer between 1 and 255.\n",tempstr,mypars->dpffile,line_count);
return 1;
}
break;
case SW_lb_rho: // lower bound of rho
sscanf(line.c_str(),"%*s %f",&tempfloat);
if ((tempfloat >= 0.0) && (tempfloat < 1.0)){
mypars->rho_lower_bound = tempfloat;
} else{
printf("Error: Value of <%s> at %s:%u must be a float between 0 and 1.\n",tempstr,mypars->dpffile,line_count);
return 1;
}
break;
case DPF_UNBOUND_MODEL: // unbound model (bound|extended|compact)
sscanf(line.c_str(),"%*s %255s",argstr);
if(stricmp(argstr,"bound")==0){
mypars->unbound_model = 0;
mypars->coeffs = unbound_models[mypars->unbound_model];
} else if(stricmp(argstr,"extended")==0){
mypars->unbound_model = 1;
mypars->coeffs = unbound_models[mypars->unbound_model];
} else if(stricmp(argstr,"compact")==0){
mypars->unbound_model = 2;
mypars->coeffs = unbound_models[mypars->unbound_model];
} else{
printf("Error: Unsupported value for <%s> at %s:%u. Value must be one of (bound|extend|compact).\n",tempstr,mypars->dpffile,line_count);
return 1;
}
break;
case DPF_COMMENT: // we use comments to allow specifying AD-GPU command lines
tempstr[0] = '\0';
sscanf(line.c_str(),"%*s %255s %255s",tempstr,argstr);
if(tempstr[0]=='-'){ // potential command line argument
i=2; // one command line argument to be parsed
args[0]=tempstr;
args[1]=argstr;
if(get_commandpars(&i,args,&(mygrid->spacing),mypars,false)<2){
printf("Warning: Command line option '%s' at %s:%u is not supported inside a dpf file.\n",tempstr,mypars->dpffile,line_count);
}
// count GPUs in case we set a different one
if(argcmp("devnum",tempstr,'D')){
new_device=true;
for(i=0; i<mypars->dev_pool.size(); i++){
if(mypars->devnum==mypars->dev_pool[i]){
new_device=false;
mypars->dev_pool_nr=i;
break;
}
}
}
}
break;
case DPF_UNKNOWN: // error condition
printf("\nError: Unknown or unsupported dpf token <%s> at %s:%u.\n",tempstr,mypars->dpffile,line_count);
return 1;
default: // means there's a keyword detected that's not yet implemented here
printf("<%s> has not yet been implemented.\n",tempstr);
case DPF_BLANK_LINE: // nothing to do here
case DPF_NULL:
break;
}
}
}
return 0;
}
int initial_commandpars(
const int* argc,
char** argv,
Dockpars* mypars,
Gridinfo* mygrid,
FileList& filelist
)
// This function checks if a dpf file is used and, if runs are specified, map and ligand information
// is stored in the filelist; flexres information and which location in the dpf parameters are in each
// run is stored separately to allow logical parsing with the correct parameters initialized per run
{
bool output_multiple_warning = true;
std::vector<std::string> xml_files;
bool read_more_xml_files = false;
#ifdef TOOLMODE
mypars->xml2dlg = true;
read_more_xml_files = true;
#endif
int error, number;
for (int i=1; i<(*argc)-1+(read_more_xml_files); i++)
{
if (argcmp("help", argv[i], 'h')){
print_options(argv[0]);
}
// wildcards for -xml2dlg are allowed (or multiple file names)
// - if more than one xml file is specified this way, they will end up in xml_files
// the test below is to stop reading arguments as filenames when another argument starts with "-"
if (read_more_xml_files && (argv[i][0]=='-')){
read_more_xml_files = false;
if(i>=(*argc)-1) break; // ignore last argument if there is no parameter specified
} else if (read_more_xml_files) xml_files.push_back(argv[i]); // copy argument into xml_files when read_more_xml_files is true
// Argument: dpf file name.
if (argcmp("import_dpf", argv[i], 'I')){
if(mypars->dpffile){
free(mypars->dpffile);
if(output_multiple_warning){
printf("Warning: Multiple --import_dpf (-I) arguments, only the last one will be used.");
output_multiple_warning = false;
}
}
mypars->dpffile = strdup(argv[i+1]);
i++;
}
// Argument: load initial data from xml file and reconstruct dlg, then finish
if (argcmp("xml2dlg", argv [i], 'X'))
{
if(mypars->xml_files>0){
printf("Error: Only one --xml2dlg (-X) argument is allowed.\n");
return 1;
}
mypars->load_xml = strdup(argv[i+1]);
read_more_xml_files = true;
mypars->xml2dlg = true;
mypars->xml_files = 1;
}
if (argcmp("contact_analysis", argv[i], 'C'))
{
float temp;
error = sscanf(argv[i+1], "%f,%f,%f", &temp, &temp, &temp);
if(error==1){
sscanf(argv [i+1], "%d", &error);
if (error == 0)
mypars->contact_analysis = false;
else
mypars->contact_analysis = true;
} else{
if(error!=3){
printf("\nError: Argument --contact_analysis (-C) expects either one parameter to enable/disable (i.e. --contact_analysis 1)\n"
" or exactly three parameters to specify cutoffs (default: --contact_analysis %.1f,%.1f,%.1f)\n", mypars->R_cutoff, mypars->H_cutoff, mypars->V_cutoff);
return 1;
}
sscanf(argv[i+1], "%f,%f,%f", &(mypars->R_cutoff), &(mypars->H_cutoff), &(mypars->V_cutoff));
mypars->contact_analysis = true;
}
if(mypars->contact_analysis) mypars->output_contact_analysis = true;
i++;
}
// Argument: print dlg output to stdout instead of to a file
if (argcmp("dlg2stdout", argv [i], '2'))
{
sscanf(argv [i+1], "%d", &number);
if (number == 0)
mypars->dlg2stdout = false;
else
mypars->dlg2stdout = true;
i++;
}
// Argument: output up to a certain number of poses per cluster
// -1 ... auto based on contact analysis
// 0 ... all poses per cluster are output (default)
// >0 ... up to this many per cluster
if (argcmp("output-cluster-poses", argv [i]))
{
error = sscanf(argv [i+1], "%d", &number);
if(error == 0){ // no number found
error = -1;
if(stricmp(argv [i+1], "all")==0){
error = 0;
number = 0;
} else{
if(stricmp(argv [i+1], "auto")==0){
error = 0;
number = -1;
mypars->contact_analysis = true; // auto needs contact analysis
mypars->calc_clustering = true; // and clustering
}
}
} else{
if(number < 0) error = -1;
if(number > 0) mypars->calc_clustering = true; // need clustering when >0
}
if(error < 0){
printf("Error: Value of --output-cluster-poses argument must be 'all', 'auto', or an integer greater than or equal to zero .\n");
return -1;
}
mypars->nr_cluster_poses = number;
i++;
}
}
bool specified_dpf = (mypars->dpffile!=NULL);
if(specified_dpf){
if((error=parse_dpf(mypars,mygrid,filelist))) return error;
}
mypars->xml_files = xml_files.size();
if(xml_files.size() == 1){
if(is_dirname(xml_files[0].c_str())){
struct stat dir_stat;
int dir_int = stat(xml_files[0].c_str(), &dir_stat);
if ((dir_int != 0) || !(dir_stat.st_mode & S_IFDIR)){
printf("\nError: Specified directory \"%s\" for xml conversion does not exist.\n", xml_files[0].c_str());
exit(12);
}
std::string dir(xml_files[0]);
std::string ext(".xml");
xml_files.clear();
for(const std::filesystem::directory_entry& l : std::filesystem::directory_iterator(dir))
if(l.path().extension() == ext)
xml_files.push_back(l.path().string());
}
}
if(xml_files.size()>0){ // use filelist parameter list in case multiple xml files are converted
if(xml_files.size()>100){ // output progress bar
printf("Preparing ");
if(mypars->output_contact_analysis)
printf("analysis\n");
else
printf("conversion\n");
printf("0%% 20%% 40%% 60%% 80%% 100%%\n");
printf("---------+---------+---------+---------+---------+\n");
}
// Need to setup file names from command line in case they weren't set with a dpf
if (get_filenames_and_ADcoeffs(argc, argv, mypars, filelist.used, false) != 0){
return 1;
}
char* orig_fld = mypars->fldfile;
Dockpars orig_pars;
if(!specified_dpf) orig_pars = *mypars;
for(unsigned int i=0; i<xml_files.size(); i++){
if(xml_files.size()>100){
if((50*(i+1)) % xml_files.size() < 50){
printf("*"); fflush(stdout);
}
}
// make sure to NOT free the previous ones as otherwise the strings of other mypars will be gone too ...
if(!specified_dpf){
*mypars = orig_pars;
mypars->dpffile=NULL;
}
if(orig_fld) mypars->fldfile = strdup(orig_fld);
mypars->ligandfile = NULL;
mypars->flexresfile = NULL;
mypars->free_roaming_ligand = false;
// load_xml is the xml file from which the other parameters will be set
mypars->load_xml = strdup(xml_files[i].c_str());
read_xml_filenames(mypars->load_xml,
mypars->dpffile,
mypars->fldfile,
mypars->ligandfile,
mypars->flexresfile,
mypars->list_nr,
mypars->seed);
mypars->free_roaming_ligand = (mypars->ligandfile != NULL);
// Filling mygrid according to the specified fld file
if (get_gridinfo(mypars->fldfile, mygrid) != 0)
{
printf("\nError: get_gridinfo failed with fld file (%s) specified in %s.\n",mypars->fldfile,mypars->load_xml);
return 1;
}
if(!specified_dpf){ // parse dpf file in XML file unless user specified one
if((error=parse_dpf(mypars,mygrid,filelist,false))) return error;
}
mypars->pop_size=1;
if(filelist.fld_files.size()>0){
// only add to fld_files if different from previous one
if(strcmp(mypars->fldfile,filelist.fld_files.back().name.c_str())!=0){
filelist.fld_files.push_back({mypars->fldfile,filelist.mygrids.size()});
filelist.mygrids.push_back(*mygrid);
}
} else{
filelist.fld_files.push_back({mypars->fldfile,filelist.mygrids.size()});
filelist.mygrids.push_back(*mygrid);
}
mypars->filelist_grid_idx=filelist.fld_files.back().grid_idx;
// If more than one unique protein, cant do map preloading yet
if (filelist.fld_files.size()>1)
filelist.preload_maps=false;
// Add the ligand filename in the xml to the filelist
if(mypars->free_roaming_ligand) filelist.ligand_files.push_back(mypars->ligandfile);
filelist.mypars.push_back(*mypars);
}
if(xml_files.size()>100) printf("\n\n");
filelist.nfiles = xml_files.size();
} else{
#ifdef TOOLMODE
printf("Error: No xml files specified.\n\n");
print_options(argv[0]);
return 1;
#endif
filelist.nfiles = filelist.ligand_files.size();
}
if(filelist.nfiles>0){
filelist.used = true;
if(mypars->contact_analysis && filelist.preload_maps){
std::string receptor_name=mygrid->grid_file_path;
if(mygrid->grid_file_path.size()>0) receptor_name+="/";
receptor_name += mygrid->receptor_name + ".pdbqt";
mypars->receptor_atoms = read_receptor(receptor_name.c_str(),mygrid,mypars->receptor_map,mypars->receptor_map_list);
mypars->nr_receptor_atoms = mypars->receptor_atoms.size();
}
}
return 0;
}
int filelist_add(
Dockpars* mypars,
Gridinfo* mygrid,
FileList& filelist
)
{
mypars->list_nr++;
// Before pushing parameters and grids back make sure
// the filename pointers are unique in the filelist
if(filelist.mypars.size()>0){
if((filelist.mypars.back().fldfile) &&
(filelist.mypars.back().fldfile==mypars->fldfile))
mypars->fldfile=strdup(mypars->fldfile);
if((filelist.mypars.back().flexresfile) &&
(filelist.mypars.back().flexresfile==mypars->flexresfile))
mypars->flexresfile=strdup(mypars->flexresfile);
if((filelist.mypars.back().xrayligandfile) &&
(filelist.mypars.back().xrayligandfile==mypars->xrayligandfile))
mypars->xrayligandfile=strdup(mypars->xrayligandfile);
}
// Keep track of fld files
if (filelist.fld_files.size()==0){
if(mygrid->fld_name.length()>0){ // already read a map file in with dpf import
printf("Using map file from dpf import.\n\n");
} else{
printf("Error: No map file on record yet. Please specify a .fld file before the first ligand (%s).\n",filelist.ligand_files.back().c_str());
return 1;
}
}
// Add the parameter block
filelist.mypars.push_back(*mypars);
return 0;
}
int get_filelist(
const int* argc,
char** argv,
Dockpars* mypars,
Gridinfo* mygrid,
FileList& filelist
)
// The function checks if a filelist has been provided according to the proper command line arguments.
// If it is, it loads the .fld, .pdbqt, and resname files into vectors
{
if(mypars->xml2dlg){ // no file list for -xml2dlg (wildcards are allowed in argument)
filelist.preload_maps&=filelist.used;
return 0;
}
bool read_ligands = false;
std::vector<std::string> ligands;
for (int i=1; i<(*argc)-1+(read_ligands); i+=1+(!read_ligands))
{
// wildcards for -filelist are allowed (or multiple file names)
// - one file specified is the filelist containing file
// - more than one file will be multiple ligands
// the test below is to stop reading arguments as filenames when another argument starts with "-"
if (read_ligands && (argv[i][0]=='-')){
read_ligands = false;
if(i>=(*argc)-1) break; // ignore last argument if there is no parameter specified
} else if (read_ligands) ligands.push_back(argv[i]); // copy argument into xml_files when read_more_xml_files is true
if (argcmp("xml2dlg", argv[i], 'X'))
i+=mypars->xml_files-1; // skip ahead in case there are multiple entries here
// Argument: file name that contains list of files.
if (argcmp("filelist", argv[i], 'B'))
{
if(ligands.size()>0){
printf("Error: Only one --filelist (-B) argument is allowed.\n");
return 1;
}
if(filelist.filename){
free(filelist.filename);
filelist.filename = NULL;
}
read_ligands=true;
}
}
mypars->filelist_files = ligands.size();
bool add_pdbqts = (ligands.size() > 1);
if(ligands.size() == 1){
if(is_dirname(ligands[0].c_str())){
struct stat dir_stat;
int dir_int = stat(ligands[0].c_str(), &dir_stat);
if ((dir_int != 0) || !(dir_stat.st_mode & S_IFDIR)){
printf("\nError: Specified directory \"%s\" for file list with `--filelist` does not exist.\n", ligands[0].c_str());
exit(12);
}
std::string dir(ligands[0]);
std::string ext(".pdbqt");
ligands.clear();
for(const std::filesystem::directory_entry& l : std::filesystem::directory_iterator(dir))
if(l.path().extension() == ext)
ligands.push_back(l.path().string());
// ligands may also contain receptor and/or flexres pdbqt - gets filtered out below
add_pdbqts = true;
} else filelist.filename = strdup(ligands[0].c_str());
}
if(add_pdbqts){
// Need to setup file names from command line in case they weren't set with a dpf
if (get_filenames_and_ADcoeffs(argc, argv, mypars, filelist.used, false) != 0){
return 1;
}
// use current (aka last specified) fld file for this file list
if(mypars->fldfile==NULL){
printf("Error: Argument --filelist (-B) with ligand files needs a grid file. Please specify through --ffile (-M) or --import_dpf (-I).\n");
return 1;
}
filelist.fld_files.push_back({mypars->fldfile,filelist.mygrids.size()});
mypars->filelist_grid_idx=filelist.fld_files.back().grid_idx;
// Filling mygrid according to the specified fld file
if (get_gridinfo(mypars->fldfile, mygrid) != 0)
{
printf("Error: get_gridinfo failed with fld file specified in file list.\n");
return 1;
}
std::string receptor_name=mygrid->grid_file_path;
if(mygrid->grid_file_path.size()>0) receptor_name+="/";
receptor_name += mygrid->receptor_name + ".pdbqt";
// Add the grid info
filelist.mygrids.push_back(*mygrid);
for(unsigned int i=0; i<ligands.size(); i++){
bool skip = (receptor_name.compare(ligands[i]) == 0);
if(mypars->flexresfile != NULL){
skip |= (ligands[i].compare(mypars->flexresfile) == 0);
}
if(skip) continue;
// Need new mypars->fldfile char* block to preserve previous one
if(filelist.mypars.size()>0){
if((filelist.mypars.back().fldfile) &&
(filelist.mypars.back().fldfile==mypars->fldfile))
mypars->fldfile=strdup(mypars->fldfile);
if((filelist.mypars.back().flexresfile) &&
(filelist.mypars.back().flexresfile==mypars->flexresfile))
mypars->flexresfile=strdup(mypars->flexresfile);
if((filelist.mypars.back().xrayligandfile) &&
(filelist.mypars.back().xrayligandfile==mypars->xrayligandfile))
mypars->xrayligandfile=strdup(mypars->xrayligandfile);
}
mypars->ligandfile = strdup(ligands[i].c_str());
filelist.ligand_files.push_back(ligands[i]);
mypars->list_nr++;
long long len = strrchr(mypars->ligandfile,'.')-mypars->ligandfile;
if(len<1) len=strlen(mypars->ligandfile);
filelist.resnames.push_back(filelist.ligand_files[i].substr(0,len));
mypars->resname=strdup(filelist.resnames[i].c_str());
mypars->free_roaming_ligand=true;
// Add the parameter block
filelist.mypars.push_back(*mypars);
}