1 /*
2  HyPhy - Hypothesis Testing Using Phylogenies.
3 
4  Copyright (C) 1997-now
5  Core Developers:
6  Sergei L Kosakovsky Pond (sergeilkp@icloud.com)
7  Art FY Poon    (apoon42@uwo.ca)
8  Steven Weaver (sweaver@temple.edu)
9 
10  Module Developers:
11  Lance Hepler (nlhepler@gmail.com)
12  Martin Smith (martin.audacis@gmail.com)
13 
14  Significant contributions from:
15  Spencer V Muse (muse@stat.ncsu.edu)
16  Simon DW Frost (sdf22@cam.ac.uk)
17 
18  Permission is hereby granted, free of charge, to any person obtaining a
19  copy of this software and associated documentation files (the
20  "Software"), to deal in the Software without restriction, including
21  without limitation the rights to use, copy, modify, merge, publish,
22  distribute, sublicense, and/or sell copies of the Software, and to
23  permit persons to whom the Software is furnished to do so, subject to
24  the following conditions:
25 
26  The above copyright notice and this permission notice shall be included
27  in all copies or substantial portions of the Software.
28 
29  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
30  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
32  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
33  CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
34  TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
35  SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36  */
37 
38 #include      <ctype.h>
39 
40 #include      "alignment.h"
41 #include      "hy_string_buffer.h"
42 #include      "global_things.h"
43 #include      "trie.h"
44 #include      "likefunc.h"
45 #include      "scfg.h"
46 #include      "global_object_lists.h"
47 
48 #include      "bayesgraph.h"
49 
50 
51 
52 using namespace hyphy_global_objects;
53 using namespace hy_global;
54 
55 //____________________________________________________________________________________
56 // global variables
57 
58 _String     isDynamicGraph          ("BGM_DYNAMIC"),
59             treeNodeNameMapping     ("TREE_NODE_NAME_MAPPING");
60 
61 
62 extern      _String  blHBLProfile;
63 
64 _SimpleList _HY_HBLCommandHelperAux;
65 
66 _List        scfgList,
67              scfgNamesList,
68              bgmList,
69              bgmNamesList,
70              _HY_GetStringGlobalTypesAux;
71 
72 _Trie        _HY_ValidHBLExpressions;
73 
74 _AVLListX    _HY_GetStringGlobalTypes (&_HY_GetStringGlobalTypesAux),
75              _HY_HBLCommandHelper     (&_HY_HBLCommandHelperAux);
76 
77 
78 //____________________________________________________________________________________
79 
80 int          _HYSQLCallBack                     (void* data,int callCount);
81 //int        _HYSQLBusyCallBack                 (void* exList,int cc,char** rd,char** cn);
82 
83 //____________________________________________________________________________________
84 
_hyInitCommandExtras(const long cut,const long conditions,_String const & commandInvocation,const char sep,const bool doTrim,const bool isAssignment,const bool needsVerb,_SimpleList * conditionList)85 _HBLCommandExtras* _hyInitCommandExtras (const long cut, const long conditions, _String const& commandInvocation, const char sep, const bool doTrim, const bool isAssignment, const bool needsVerb, _SimpleList * conditionList) {
86 
87     struct _HBLCommandExtras * commandInfo           = new _HBLCommandExtras();
88     commandInfo->cut_string                          = cut;
89     if (conditions < 0 && conditionList)
90         commandInfo->extract_conditions              << *conditionList;
91     else
92         commandInfo->extract_conditions              << conditions;
93     commandInfo->extract_condition_separator         = sep;
94     commandInfo->do_trim                             = doTrim;
95     commandInfo->is_assignment                       = isAssignment;
96     commandInfo->needs_verb                          = needsVerb;
97     commandInfo->command_invocation                  < new _String (commandInvocation);
98 
99     return                                             commandInfo;
100 
101 }
102 
103 //____________________________________________________________________________________
104 
ExtractValidateAddHBLCommand(_StringBuffer & current_stream,const long command_code,_List * pieces,_HBLCommandExtras * command_spec,_ExecutionList & command_list)105 bool      _ElementaryCommand::ExtractValidateAddHBLCommand (_StringBuffer& current_stream,const long command_code,  _List* pieces, _HBLCommandExtras* command_spec, _ExecutionList& command_list)
106 
107 {
108     if (command_spec->is_assignment) {
109         // TBA
110     } else {
111         // by default push all of the 'pieces' arguments to the "argument" list
112         (new _ElementaryCommand (command_code))->addAndClean  (command_list, pieces, 0);
113     }
114 
115     return true;
116 }
117 
118 
119 //____________________________________________________________________________________
120 
121 
_HBL_Init_Const_Arrays(void)122 void        _HBL_Init_Const_Arrays  (void)
123 {
124     // init GetString lookups
125     _HY_GetStringGlobalTypes.Insert(new _String("LikelihoodFunction"), HY_BL_LIKELIHOOD_FUNCTION);
126     _HY_GetStringGlobalTypes.Insert(new _String("DataSet"), HY_BL_DATASET);
127     _HY_GetStringGlobalTypes.Insert(new _String("DataSetFilter"), HY_BL_DATASET_FILTER);
128     _HY_GetStringGlobalTypes.Insert(new _String("UserFunction"), HY_BL_HBL_FUNCTION);
129     _HY_GetStringGlobalTypes.Insert(new _String("Tree"), HY_BL_TREE);
130     _HY_GetStringGlobalTypes.Insert(new _String("SCFG"), HY_BL_SCFG);
131     _HY_GetStringGlobalTypes.Insert(new _String("Variable"), HY_BL_VARIABLE);
132     _HY_GetStringGlobalTypes.Insert(new _String("BayesianGraphicalModel"), HY_BL_BGM);
133 
134 
135     _HY_ValidHBLExpressions.Insert ("function ",                            HY_HBL_COMMAND_FUNCTION);
136     _HY_ValidHBLExpressions.Insert ("ffunction ",                           HY_HBL_COMMAND_FFUNCTION);
137     _HY_ValidHBLExpressions.Insert ("return ",                              HY_HBL_COMMAND_RETURNSPACE);
138     _HY_ValidHBLExpressions.Insert ("return(",                              HY_HBL_COMMAND_RETURNPAREN);
139     _HY_ValidHBLExpressions.Insert ("if(",                                  HY_HBL_COMMAND_IF);
140     _HY_ValidHBLExpressions.Insert ("else",                                 HY_HBL_COMMAND_ELSE);
141     _HY_ValidHBLExpressions.Insert ("do{",                                  HY_HBL_COMMAND_DO);
142     _HY_ValidHBLExpressions.Insert ("break;",                               HY_HBL_COMMAND_BREAK);
143     _HY_ValidHBLExpressions.Insert ("continue;",                            HY_HBL_COMMAND_CONTINUE);
144     _HY_ValidHBLExpressions.Insert ("#include",                             HY_HBL_COMMAND_INCLUDE);
145     _HY_ValidHBLExpressions.Insert ("DataSet ",                             HY_HBL_COMMAND_DATA_SET);
146     _HY_ValidHBLExpressions.Insert ("DataSetFilter ",                       HY_HBL_COMMAND_DATA_SET_FILTER);
147     _HY_ValidHBLExpressions.Insert ("ConstructCategoryMatrix(",				HY_HBL_COMMAND_CONSTRUCT_CATEGORY_MATRIX);
148     _HY_ValidHBLExpressions.Insert ("Tree ",                                HY_HBL_COMMAND_TREE);
149     _HY_ValidHBLExpressions.Insert ("LikelihoodFunction ",					HY_HBL_COMMAND_LIKELIHOOD_FUNCTION);
150     _HY_ValidHBLExpressions.Insert ("LikelihoodFunction3 ",					HY_HBL_COMMAND_LIKELIHOOD_FUNCTION_3);
151     _HY_ValidHBLExpressions.Insert ("MolecularClock(",                      HY_HBL_COMMAND_MOLECULAR_CLOCK);
152     _HY_ValidHBLExpressions.Insert ("fscanf(",                              HY_HBL_COMMAND_FSCANF);
153     _HY_ValidHBLExpressions.Insert ("sscanf(",                              HY_HBL_COMMAND_SSCANF);
154     _HY_ValidHBLExpressions.Insert ("ReplicateConstraint(",					HY_HBL_COMMAND_REPLICATE_CONSTRAINT);
155     //_HY_ValidHBLExpressions.Insert ("Import(",                              HY_HBL_COMMAND_IMPORT);
156     _HY_ValidHBLExpressions.Insert ("category ",                            HY_HBL_COMMAND_CATEGORY);
157     _HY_ValidHBLExpressions.Insert ("Model ",                               HY_HBL_COMMAND_MODEL);
158     _HY_ValidHBLExpressions.Insert ("ChoiceList(",                          HY_HBL_COMMAND_CHOICE_LIST);
159     _HY_ValidHBLExpressions.Insert ("GetInformation(",                      HY_HBL_COMMAND_GET_INFORMATION);
160     _HY_ValidHBLExpressions.Insert ("ExecuteCommands(",                     HY_HBL_COMMAND_EXECUTE_COMMANDS);
161     _HY_ValidHBLExpressions.Insert ("ExecuteAFile(",                        HY_HBL_COMMAND_EXECUTE_A_FILE);
162     _HY_ValidHBLExpressions.Insert ("LoadFunctionLibrary(",					HY_HBL_COMMAND_LOAD_FUNCTION_LIBRARY);
163     _HY_ValidHBLExpressions.Insert ("FindRoot(",                            HY_HBL_COMMAND_FIND_ROOT);
164     _HY_ValidHBLExpressions.Insert ("MPIReceive(",                          HY_HBL_COMMAND_MPI_RECEIVE);
165     _HY_ValidHBLExpressions.Insert ("MPISend(",                             HY_HBL_COMMAND_MPI_SEND);
166     _HY_ValidHBLExpressions.Insert ("GetDataInfo(",                         HY_HBL_COMMAND_GET_DATA_INFO);
167     _HY_ValidHBLExpressions.Insert ("StateCounter(",                        HY_HBL_COMMAND_STATE_COUNTER);
168     _HY_ValidHBLExpressions.Insert ("Integrate(",                           HY_HBL_COMMAND_INTEGRATE);
169     _HY_ValidHBLExpressions.Insert ("DoSQL(",                               HY_HBL_COMMAND_DO_SQL);
170     _HY_ValidHBLExpressions.Insert ("Topology ",                            HY_HBL_COMMAND_TOPOLOGY);
171     _HY_ValidHBLExpressions.Insert ("AlignSequences(",                      HY_HBL_COMMAND_ALIGN_SEQUENCES);
172     _HY_ValidHBLExpressions.Insert ("#profile",                             HY_HBL_COMMAND_PROFILE);
173     _HY_ValidHBLExpressions.Insert ("SCFG ",                                HY_HBL_COMMAND_SCFG);
174     _HY_ValidHBLExpressions.Insert ("NeuralNet ",                           HY_HBL_COMMAND_NEURAL_NET);
175     _HY_ValidHBLExpressions.Insert ("BGM ",                                 HY_HBL_COMMAND_BGM);
176     _HY_ValidHBLExpressions.Insert ("SimulateDataSet",                      HY_HBL_COMMAND_SIMULATE_DATA_SET);
177     _HY_ValidHBLExpressions.Insert ("KeywordArgument",                      HY_HBL_COMMAND_KEYWORD_ARGUMENT);
178 /*
179 const long cut, const long conditions, const char sep, const bool doTrim, const bool isAssignment, const bool needsVerb, length options
180 */
181 
182     _SimpleList lengthOptions;
183 
184     _HY_HBLCommandHelper.Insert    ((BaseRef)HY_HBL_COMMAND_FOR,
185                                     (long)_hyInitCommandExtras (_HY_ValidHBLExpressions.Insert ("for(", HY_HBL_COMMAND_FOR,false),3, "for (<initialization>;<condition>;<increment>) {loop body}"));
186 
187     _HY_HBLCommandHelper.Insert    ((BaseRef)HY_HBL_COMMAND_WHILE,
188                                     (long)_hyInitCommandExtras (_HY_ValidHBLExpressions.Insert ("while(", HY_HBL_COMMAND_WHILE,false),1, "while (<condition>) {loop body}"));
189 
190 
191     _HY_HBLCommandHelper.Insert    ((BaseRef)HY_HBL_COMMAND_SET_DIALOG_PROMPT,
192                                     (long)_hyInitCommandExtras (_HY_ValidHBLExpressions.Insert ("SetDialogPrompt(", HY_HBL_COMMAND_SET_DIALOG_PROMPT,false),
193                                     1,
194                                     "SetDialogPrompt(<prompt string>);"));
195 
196     _HY_HBLCommandHelper.Insert    ((BaseRef)HY_HBL_COMMAND_REPLICATE_CONSTRAINT,
197                                     (long)_hyInitCommandExtras (_HY_ValidHBLExpressions.Insert ("ReplicateConstraint(", HY_HBL_COMMAND_REPLICATE_CONSTRAINT,false),
198                                                                 -2,
199                                                                 "ReplicateConstraint(<constraint pattern in terms of 'this1', 'this2',...>, <an argument to replace 'this*', for each 'thisN' in the pattern);",','));
200 
201 
202     lengthOptions.Clear();lengthOptions.Populate (3,1,1);
203     _HY_HBLCommandHelper.Insert    ((BaseRef)HY_HBL_COMMAND_EXECUTE_COMMANDS,
204                                     (long)_hyInitCommandExtras (_HY_ValidHBLExpressions.Insert ("ExecuteCommands(", HY_HBL_COMMAND_EXECUTE_COMMANDS,false),
205                                                                 -1,
206                                                                 "ExecuteCommands(<source code>, [optional <'compiled' | (input redirect , [optional <namespace>]) ])",
207                                                                 ',',
208                                                                 true,
209                                                                 false,
210                                                                 false,
211                                                                 &lengthOptions));
212 
213     _HY_HBLCommandHelper.Insert    ((BaseRef)HY_HBL_COMMAND_EXECUTE_A_FILE,
214                                     (long)_hyInitCommandExtras (_HY_ValidHBLExpressions.Insert ("ExecuteAFile(", HY_HBL_COMMAND_EXECUTE_A_FILE,false),
215                                                                 -1,
216                                                                 "ExecuteAFile(<file path>, [optional <'compiled' | (input redirect , [optional <namespace>]) ])",
217                                                                 ',',
218                                                                 true,
219                                                                 false,
220                                                                 false,
221                                                                 &lengthOptions));
222 
223     _HY_HBLCommandHelper.Insert    ((BaseRef)HY_HBL_COMMAND_LOAD_FUNCTION_LIBRARY,
224                                     (long)_hyInitCommandExtras (_HY_ValidHBLExpressions.Insert ("LoadFunctionLibrary(", HY_HBL_COMMAND_EXECUTE_A_FILE,false),
225                                                                 -1,
226                                                                 "LoadFunctionLibrary(<file path | library name>, [optional <'compiled' | (input redirect , [optional <namespace>]) ])",
227                                                                 ',',
228                                                                 true,
229                                                                 false,
230                                                                 false,
231                                                                 &lengthOptions));
232 
233     lengthOptions.Clear();lengthOptions.Populate (3,5,1);
234     _HY_HBLCommandHelper.Insert    ((BaseRef)HY_HBL_COMMAND_HARVEST_FREQUENCIES,
235                                     (long)_hyInitCommandExtras (_HY_ValidHBLExpressions.Insert ("HarvestFrequencies(", HY_HBL_COMMAND_HARVEST_FREQUENCIES,false),
236                                     -1,
237                                     "HarvestFrequencies(<receptacle>, <DataSet or DataSetFilter>, <atom INTEGER>, <unit INTEGER <= atom>, <position aware 0 or 1>, [optional site partition], [optional sequence partition] (only for DataSetArguments)",
238                                         ',',
239                                         true,
240                                         false,
241                                         false,
242                                         &lengthOptions));
243 
244 
245     _HY_HBLCommandHelper.Insert    ((BaseRef)HY_HBL_COMMAND_GET_INFORMATION,
246                                     (long)_hyInitCommandExtras (_HY_ValidHBLExpressions.Insert ("GetInformation(", HY_HBL_COMMAND_GET_INFORMATION,false),
247                                                                 2,
248                                                                 "GetInformation(<receptacle>, <DataSet or DataSetFilter or LikelihoodFunction or Model or Variable or Regexp or String>",
249                                                                 ','));
250 
251     lengthOptions.Clear();lengthOptions.Populate (4,2,1); // 2, 3, 4, 5
252     _HY_HBLCommandHelper.Insert    ((BaseRef)HY_HBL_COMMAND_GET_DATA_INFO,
253                                     (long)_hyInitCommandExtras (_HY_ValidHBLExpressions.Insert ("GetDataInfo(", HY_HBL_COMMAND_GET_DATA_INFO,false),
254                                                                 -1,
255                                                                 "GetDataInfo(<receptacle>, <DataSet or DataSetFilter>, [optional <sequence ref, site ref | sequence 1 , sequence 2, DISTANCES>])",
256                                                                 ',',
257                                                                 true,
258                                                                 false,
259                                                                 false,
260                                                                 &lengthOptions));
261 
262     lengthOptions.Clear();lengthOptions.Populate (2,2,1); // 2, 3
263     _HY_HBLCommandHelper.Insert    ((BaseRef)HY_HBL_COMMAND_MPI_SEND,
264                                     (long)_hyInitCommandExtras (_HY_ValidHBLExpressions.Insert ("MPISend(", HY_HBL_COMMAND_MPI_SEND,false),
265                                                                 -1,
266                                                                 "MPISend(<node id>, <string | likelihood function ID | filename [in conjunction with argument 3]>, [if specified, treat the second argument as a script path, and use the dict supplied here as input options to the script])",
267                                                                 ',',
268                                                                 true,
269                                                                 false,
270                                                                 false,
271                                                                 &lengthOptions));
272 
273   _HY_HBLCommandHelper.Insert    ((BaseRef)HY_HBL_COMMAND_CONSTRUCT_CATEGORY_MATRIX,
274                                     (long)_hyInitCommandExtras (_HY_ValidHBLExpressions.Insert ("ConstructCategoryMatrix(", HY_HBL_COMMAND_CONSTRUCT_CATEGORY_MATRIX,false),
275                                                                 -1,
276                                                                 "ConstructCategoryMatrix(<receptacle>, <Likelihood Function|Tree>, [optional <COMPLETE|SHORT|WEIGHTS|CLASSES (default = COMPLETE)> , matrix argument with partitions to include (default = all)>])",
277                                                                 ',',
278                                                                 true,
279                                                                 false,
280                                                                 false,
281                                                                 &lengthOptions));
282 
283     _HY_HBLCommandHelper.Insert    ((BaseRef)HY_HBL_COMMAND_OPTIMIZE,
284                                     (long)_hyInitCommandExtras (_HY_ValidHBLExpressions.Insert ("Optimize(", HY_HBL_COMMAND_OPTIMIZE,false),
285                                                                 -1,
286                                                                 "Optimize (<receptacle>, <likelihood function/scfg/bgm>, [optional dictionary of arguments]",',',
287                                                                 true,
288                                                                 false,
289                                                                 false,
290                                                                 &lengthOptions));
291 
292     _HY_HBLCommandHelper.Insert    ((BaseRef)HY_HBL_COMMAND_FIND_ROOT,
293                                     (long)_hyInitCommandExtras (_HY_ValidHBLExpressions.Insert ("FindRoot(", HY_HBL_COMMAND_FIND_ROOT,false),
294                                                                 5,
295                                                                 "FindRoot (<receptacle>, <expression>, <variable to solve for>,<left bound>,<right bound>)",','));
296 
297 
298     _HY_HBLCommandHelper.Insert    ((BaseRef)HY_HBL_COMMAND_INTEGRATE,
299                                     (long)_hyInitCommandExtras (_HY_ValidHBLExpressions.Insert ("Integrate(", HY_HBL_COMMAND_INTEGRATE,false),
300                                                                 5,
301                                                                 "Integrate (<receptacle>, <expression>, <variable to integrate over for>,<left bound>,<right bound>)",','));
302 
303 
304     _HY_HBLCommandHelper.Insert    ((BaseRef)HY_HBL_COMMAND_MPI_RECEIVE,
305                                     (long)_hyInitCommandExtras (_HY_ValidHBLExpressions.Insert ("MPIReceive(", HY_HBL_COMMAND_MPI_RECEIVE,false),
306                                                                 3,
307                                                                 "MPIReceive (<from node; or -1 to receive from any>, <message storage>, <sender index storage>)",','));
308 
309     _HY_HBLCommandHelper.Insert    ((BaseRef)HY_HBL_COMMAND_LFCOMPUTE,
310                                       (long)_hyInitCommandExtras (_HY_ValidHBLExpressions.Insert ("LFCompute(", HY_HBL_COMMAND_LFCOMPUTE,false),
311                                                                   2,
312                                                                   "LFCompute (<likelihood function/scfg/bgm>,<LF_START_COMPUTE|LF_DONE_COMPUTE|receptacle>)",','));
313 
314 
315     _HY_HBLCommandHelper.Insert    ((BaseRef)HY_HBL_COMMAND_COVARIANCE_MATRIX,
316                                     (long)_hyInitCommandExtras (_HY_ValidHBLExpressions.Insert ("CovarianceMatrix(", HY_HBL_COMMAND_COVARIANCE_MATRIX,false),
317                                                                 2,
318                                                                 "CovarianceMatrix (<receptacle>, <likelihood function/scfg/bgm>)",','));
319 
320     _HY_HBLCommandHelper.Insert    ((BaseRef)HY_HBL_COMMAND_DO_SQL,
321                                     (long)_hyInitCommandExtras (_HY_ValidHBLExpressions.Insert ("DoSQL(", HY_HBL_COMMAND_DO_SQL,false),
322                                                                 3,
323                                                                 "DoSQL (<dbID | SQL_OPEN | SQL_CLOSE>, <transaction string | file name>, <ID here | result here>)",','));
324 
325 
326     _HY_HBLCommandHelper.Insert    ((BaseRef)HY_HBL_COMMAND_SELECT_TEMPLATE_MODEL,
327                                     (long)_hyInitCommandExtras (_HY_ValidHBLExpressions.Insert ("SelectTemplateModel(", HY_HBL_COMMAND_SELECT_TEMPLATE_MODEL,false),
328                                     1,
329                                     "SelectTemplateModel(<DataSetFilter>);"));
330 
331 
332     _HY_HBLCommandHelper.Insert    ((BaseRef)HY_HBL_COMMAND_USE_MODEL,
333                                     (long)_hyInitCommandExtras (_HY_ValidHBLExpressions.Insert ("UseModel(", HY_HBL_COMMAND_USE_MODEL,false),
334                                                                 1,
335                                                                 "UseModel (<model ID>)",','));
336 
337 
338     _HY_HBLCommandHelper.Insert    ((BaseRef)HY_HBL_COMMAND_ALIGN_SEQUENCES,
339                                     (long)_hyInitCommandExtras (_HY_ValidHBLExpressions.Insert ("AlignSequences(", HY_HBL_COMMAND_ALIGN_SEQUENCES,false),
340                                                                 3,
341                                                                 "AlignSequences (result, sequences, options)",','));
342 
343     _HY_HBLCommandHelper.Insert    ((BaseRef)HY_HBL_COMMAND_SET_PARAMETER,
344                                     (long)_hyInitCommandExtras (_HY_ValidHBLExpressions.Insert ("SetParameter(", HY_HBL_COMMAND_SET_PARAMETER,false),
345                                                                 3,
346                                                                 "SetParameter(<object>, <parameter index>, <value>)",','));
347 
348 
349     lengthOptions.Clear();lengthOptions.Populate (2,1,1);
350     _HY_HBLCommandHelper.Insert    ((BaseRef)HY_HBL_COMMAND_ASSERT,
351                                     (long)_hyInitCommandExtras (_HY_ValidHBLExpressions.Insert ("assert(", HY_HBL_COMMAND_ASSERT,false),
352                                     -1,
353                                     "assert (<statement>,[optional message on failure]>",
354                                         ',',
355                                         true,
356                                         false,
357                                         false,
358                                         &lengthOptions));
359 
360 
361     _HY_HBLCommandHelper.Insert    ((BaseRef)HY_HBL_COMMAND_REQUIRE_VERSION,
362                                     (long)_hyInitCommandExtras (_HY_ValidHBLExpressions.Insert ("RequireVersion(", HY_HBL_COMMAND_REQUIRE_VERSION,false),
363                                                                 1,
364                                                                 "RequireVersion (<version string>)",','));
365 
366     _HY_HBLCommandHelper.Insert    ((BaseRef)HY_HBL_COMMAND_DELETE_OBJECT,
367                                     (long)_hyInitCommandExtras (_HY_ValidHBLExpressions.Insert ("DeleteObject(", HY_HBL_COMMAND_DELETE_OBJECT,false),
368                                                                 -1,
369                                                                 "DeleteObject(<object 1> [optional ,<object 2>, <object 3>, ..., <object N>])",','));
370 
371     _HY_HBLCommandHelper.Insert    ((BaseRef)HY_HBL_COMMAND_CLEAR_CONSTRAINTS,
372                                     (long)_hyInitCommandExtras (_HY_ValidHBLExpressions.Insert ("ClearConstraints(", HY_HBL_COMMAND_CLEAR_CONSTRAINTS,false),
373                                                                 -1,
374                                                                 "ClearConstraints(<object 1> [optional ,<object 2>, <object 3>, ..., <object N>])",','));
375 
376     _HY_HBLCommandHelper.Insert    ((BaseRef)HY_HBL_COMMAND_MOLECULAR_CLOCK,
377                                     (long)_hyInitCommandExtras (_HY_ValidHBLExpressions.Insert ("MolecularClock(", HY_HBL_COMMAND_MOLECULAR_CLOCK,false),
378                                                                 -2,
379                                                                 "MolecularClock(tree or tree node, local variable 1 [optional ,<local variable 2>, ..., <local variable N>])",','));
380 
381     _HY_HBLCommandHelper.Insert    ((BaseRef)HY_HBL_COMMAND_FSCANF,
382                                   (long)_hyInitCommandExtras (_HY_ValidHBLExpressions.Insert ("fscanf(", HY_HBL_COMMAND_FSCANF,false),
383                                                               -3,
384                                                               _String ("fscanf(file path (string),<optional 'REWIND'>,'type 1 (") & _String ((_String*)_ElementaryCommand::fscanf_allowed_formats.Join('|')) & ")[optional , <type 2>, ... <type N>]', var1 [optional , <var 2>, ... <var N>])",','));
385 
386     _HY_HBLCommandHelper.Insert    ((BaseRef)HY_HBL_COMMAND_SSCANF,
387                                   (long)_hyInitCommandExtras (_HY_ValidHBLExpressions.Insert ("sscanf(", HY_HBL_COMMAND_SSCANF,false),
388                                                               -3,
389                                                               _String ("sscanf(string,<optional 'REWIND'>,'type 1 (") & _String ((_String*)_ElementaryCommand::fscanf_allowed_formats.Join('|')) & ")[optional , <type 2>, ... <type N>]', var1 [optional , <var 2>, ... <var N>])",','));
390 
391 
392     _HY_HBLCommandHelper.Insert    ((BaseRef)HY_HBL_COMMAND_CHOICE_LIST,
393                                   (long)_hyInitCommandExtras (_HY_ValidHBLExpressions.Insert ("ChoiceList(", HY_HBL_COMMAND_CHOICE_LIST,false),
394                                                               -5,
395                                                               _String ("ChoiceList(store_here {ID}, title {String}, how many choices (0 for any number >= 1) {Integer}, [NO_SKIP or SKIP_NONE | list of indices not to show as options], [source object | comma separated list of 'key', 'description' pairs]"),
396                                                               ','));
397 
398     _HY_HBLCommandHelper.Insert    ((BaseRef)HY_HBL_COMMAND_FPRINTF,
399                                     (long)_hyInitCommandExtras (_HY_ValidHBLExpressions.Insert ("fprintf(", HY_HBL_COMMAND_FPRINTF,false),
400                                                                 -2,
401                                                                 "fprintf(stdout|MESSAGE_LOG|TEMP_FILE_NAME|PROMPT_FOR_FILE|file path, object 1 [optional ,<object 2>, ..., <object N>])",','));
402 
403 
404     lengthOptions.Clear();lengthOptions.Populate (1,2,1); // 2
405     _HY_HBLCommandHelper.Insert    ((BaseRef)HY_HBL_COMMAND_EXPORT,
406                                     (long)_hyInitCommandExtras (_HY_ValidHBLExpressions.Insert ("Export(", HY_HBL_COMMAND_EXPORT,false),
407                                                                 -1,
408                                                                 "Export (<string variable ID>, <object ID>)",
409                                                                 ',',
410                                                                 true,
411                                                                 false,
412                                                                 false,
413                                                                 &lengthOptions));
414 
415 
416     lengthOptions.Clear();lengthOptions.Populate (2,2,1);
417     _HY_HBLCommandHelper.Insert    ((BaseRef)HY_HBL_COMMAND_GET_URL,
418                                     (long)_hyInitCommandExtras (_HY_ValidHBLExpressions.Insert ("GetURL(", HY_HBL_COMMAND_GET_URL,false),
419                                     -1,
420                                     "GetURL (<receptacle>,<URL>[, SAVE_TO_FILE])",
421                                         ',',
422                                         true,
423                                         false,
424                                         false,
425                                         &lengthOptions));
426 
427     lengthOptions.Clear();lengthOptions.Populate (3,2,1);
428     _HY_HBLCommandHelper.Insert    ((BaseRef)HY_HBL_COMMAND_KEYWORD_ARGUMENT,
429                                     (long)_hyInitCommandExtras (_HY_ValidHBLExpressions.Insert ("KeywordArgument(", HY_HBL_COMMAND_KEYWORD_ARGUMENT,false),
430                                                                 -1,
431                                                                 "KeywordArgument (keyword, description, [default value, [dialog reference]])",
432                                                                 ',',
433                                                                 true,
434                                                                 false,
435                                                                 false,
436                                                                 &lengthOptions));
437 
438     lengthOptions.Clear();lengthOptions.Populate (2,3,1); // 3 or 4
439     _HY_HBLCommandHelper.Insert    ((BaseRef)HY_HBL_COMMAND_GET_STRING,
440                                     (long)_hyInitCommandExtras (_HY_ValidHBLExpressions.Insert ("GetString(", HY_HBL_COMMAND_GET_STRING,false),
441                                     -1,
442                                     "GetString(<receptacle>,<object>,<index>,[optional <second index>])",
443                                         ',',
444                                         true,
445                                         false,
446                                         false,
447                                         &lengthOptions));
448 
449 
450     lengthOptions.Clear();lengthOptions.Populate (2,3,1); // 3 or 4
451     _HY_HBLCommandHelper.Insert    ((BaseRef)HY_HBL_COMMAND_DIFFERENTIATE,
452                                     (long)_hyInitCommandExtras (_HY_ValidHBLExpressions.Insert ("Differentiate(", HY_HBL_COMMAND_DIFFERENTIATE,false),
453                                     -1,
454                                     "Differentiate(<receptacle>, <the expression to differentiate>, <variable to differentiate>[, number of times, default = 1])",
455                                     ',',
456                                     true,
457                                     false,
458                                     false,
459                                     &lengthOptions));
460 
461 // matrix global arrays
462 
463 
464     _HY_MatrixRandomValidPDFs.Insert ("Dirichlet", _HY_MATRIX_RANDOM_DIRICHLET,
465                                       "Gaussian", _HY_MATRIX_RANDOM_GAUSSIAN,
466                                       "Wishart", _HY_MATRIX_RANDOM_WISHART,
467                                       "InverseWishart", _HY_MATRIX_RANDOM_INVERSE_WISHART,
468                                       "Multinomial", _HY_MATRIX_RANDOM_MULTINOMIAL);
469 
470 
471   _List keywords;
472   keywords << &blReturn << &blDataSet << &blDataSetFilter << &blTree << &blTopology << &blLF << &blLF3 << &blSCFG;
473 
474 
475   for (long key = 0; key < keywords.lLength; key++) {
476     _String* key_string = (_String*)keywords.GetItem (key);
477     _HY_HBL_KeywordsPreserveSpaces.Insert (key_string->Reverse().Cut (1,kStringEnd), key_string->length()-1);
478   }
479 
480 }
481 
482 //____________________________________________________________________________________
InsertVarIDsInList(_AssociativeList * theList,_String const & theKey,_SimpleList const & varIDs)483 void         InsertVarIDsInList     (_AssociativeList* theList , _String const& theKey, _SimpleList const& varIDs) {
484     _FString arrayKey (theKey, false);
485     _Matrix *mxEntry = nil;
486 
487     if (varIDs.lLength) {
488         _List     varNames;
489         for (unsigned long i=0; i < varIDs.lLength; i++) {
490             _Variable* v = LocateVar (varIDs.list_data[i]);
491             if (v) {
492                 varNames << v->GetName();
493             }
494         }
495         mxEntry = new _Matrix (varNames);
496     } else {
497         mxEntry = new _Matrix;
498     }
499 
500     theList->MStore (&arrayKey,mxEntry,false);
501 }
502 
503 //____________________________________________________________________________________
InsertStringListIntoAVL(_AssociativeList * theList,_String const & theKey,_SimpleList const & stringsToPick,_List const & theStrings)504 void         InsertStringListIntoAVL    (_AssociativeList* theList , _String const& theKey, _SimpleList const& stringsToPick, _List const& theStrings) {
505     _FString arrayKey (theKey, false);
506     _Matrix *mxEntry = nil;
507 
508     if (stringsToPick.lLength) {
509         _List     theNames;
510         for (unsigned long i=0; i < stringsToPick.lLength; i++) {
511             _String * v = (_String*)theStrings.GetItem(stringsToPick.list_data[i]);
512             if (v) {
513                 theNames << v;
514             }
515         }
516         mxEntry = new _Matrix (theNames);
517     } else {
518         mxEntry = new _Matrix;
519     }
520 
521     theList->MStore (&arrayKey,mxEntry,false);
522 }
523 
524 //____________________________________________________________________________________
525 
ConstructProfileStatement(_StringBuffer & source,_ExecutionList & target)526 bool    _ElementaryCommand::ConstructProfileStatement (_StringBuffer&source, _ExecutionList&target)
527 // syntax: #profile START|PAUSE|RESUME|indetifier to dump in
528 {
529 
530     _List pieces;
531     ExtractConditions (source,blHBLProfile.length()+1,pieces,';');
532     if (pieces.lLength!=2) {
533         HandleApplicationError (_String ("Expected syntax:")& blHBLProfile &" START|PAUSE|RESUME|where to store)");
534         return false;
535     }
536 
537     _ElementaryCommand *sp = new _ElementaryCommand (58);
538     sp->addAndClean(target,&pieces,0);
539     return true;
540 }
541 
542 
543 
544 //____________________________________________________________________________________
545 /*
546 int  _HYSQLBusyCallBack (void* data, int callCount)
547 {
548     if (callCount > 100)
549         return 0;
550 
551 #ifdef __WINDOZE__
552     Sleep  (1 + genrand_real2()*100.);
553 #else
554     usleep (100 + genrand_real2()*100000.);
555 #endif
556     return 1;
557 }*/
558 
559 
560 //____________________________________________________________________________________
561 
ExecuteDataFilterCases(_ExecutionList & chain)562 void      _ElementaryCommand::ExecuteDataFilterCases (_ExecutionList& chain) {
563 
564     using namespace hyphy_global_objects;
565 
566     chain.currentCommand++;
567 
568     _String dataObjectID = chain.AddNameSpaceToID(*(_String*)parameters(1));
569 
570     /*printf ("%s raw : '%s' processed : '%s'\n", __PRETTY_FUNCTION__,  (const char*)*(_String*)parameters(1), (const char*) dataObjectID);
571     */
572 
573     long dsID           = (parameters.lLength>2)?FindDataSetName (dataObjectID):-1;
574     bool isFilter       = false;
575 
576     if (dsID == -1) {
577         dsID = (parameters.lLength>2)? FindDataFilter (dataObjectID):-1;
578         if (dsID == -1) {
579             _AssociativeList * numericFilter = (_AssociativeList*)FetchObjectFromVariableByType(&dataObjectID, ASSOCIATIVE_LIST);
580             if (numericFilter) {
581                 _String errCode;
582 
583                 long    categoryCount = 1;
584 
585                 if (parameters.lLength > 2) {
586                     // have multiple categories
587                     categoryCount = (long) ProcessNumericArgument((_String*)parameters(2),nil);
588                 }
589 
590                 _String const namesKey ("FILTER_NAMES"),
591                               dataKey  ("FILTER_ARRAYS"),
592                               freqKey  ("FILTER_FREQS");
593 
594                 _Matrix* sequenceNames = (_Matrix*)numericFilter->GetByKey (namesKey,MATRIX);
595                 _List seqNames;
596                 if (sequenceNames) {
597                     sequenceNames->FillInList (seqNames);
598                 }
599                 if (!sequenceNames || seqNames.lLength == 0) {
600                     errCode = _String("Expected a non-empty string matrix as the ") & namesKey & " argument in call to CreateFilter";
601                 } else {
602                     _AssociativeList * dataList = (_AssociativeList*)numericFilter->GetByKey (dataKey,ASSOCIATIVE_LIST);
603                     _Matrix          * freqList = (_Matrix*)numericFilter->GetByKey (freqKey,MATRIX);
604 
605                     if (dataList && freqList) {
606                         _List       goodSeqs;
607                         long        sitePatterns    = freqList->GetVDim(),
608                                     categDim      = -1;
609 
610                         if (freqList->GetHDim() != 1 || sitePatterns < 1 || freqList->MatrixType() != 1 ) {
611                             errCode = _String("Expected a non-empty numeric ROW matrix as the ") & freqKey & " argument in call to CreateFilter";
612                         } else {
613                             for (long k=0; k<seqNames.lLength; k=k+1) {
614                                 _Matrix * dataMx = (_Matrix*)dataList->GetByKey (k,MATRIX);
615                                 if (dataMx && dataMx->MatrixType() == 1 ) {
616                                     if (categDim < 0) {
617                                         categDim = dataMx->GetVDim();
618                                         if (categDim < 1) {
619                                             break;
620                                         }
621                                     } else if (dataMx->GetVDim() != categDim) {
622                                         break;
623                                     }
624                                     if (dataMx->GetHDim () != sitePatterns*categoryCount) {
625                                         break;
626                                     }
627 
628                                     goodSeqs << dataMx;
629                                     continue;
630                                 }
631                                 break;
632                             }
633 
634                             if (goodSeqs.lLength == seqNames.lLength) {
635                                 _String             dataFilterID (chain.AddNameSpaceToID(*(_String*)parameters(0))),
636                                                     dataSetName  = dataFilterID & "_internal_ds";
637 
638                                 _DataSet * dummyDS = new _DataSet;
639 
640                                 dummyDS->SetNoSpecies (seqNames.lLength);
641                                 dummyDS->SetNames     (seqNames);
642                                 dummyDS->GetTheMap().Populate (sitePatterns,0,1);
643 
644                                 AddDataSetToList  (dataSetName, dummyDS);
645 
646                                 _DataSetFilterNumeric * dsn = new _DataSetFilterNumeric (freqList,goodSeqs,dummyDS,categoryCount);
647 
648                                 if (StoreDataFilter (dataFilterID, dsn) >= 0) {
649                                   return;
650                                 }
651 
652                                 errCode = _String ("Failed to store _DataSetFilterNumeric '") & dataFilterID & "'";
653 
654                             } else {
655                                 errCode = _String ("Site frequency patterns/numeric vectors did not pass dimension checks in call to CreateFilter");
656                             }
657                         }
658                     }
659 
660                 }
661                 if (errCode.nonempty()) {
662                     HandleApplicationError(errCode);
663                     return;
664                 }
665 
666             }
667             HandleApplicationError (((_String)("DataSet(Filter)/Associative Array ")&dataObjectID&_String(" has not been properly initialized")));
668             return;
669         }
670         isFilter = true;
671     }
672 
673     // build the formula from the 2nd parameter (unit size)
674 
675     unsigned char                unit = ProcessNumericArgument((_String*)parameters(2),chain.nameSpacePrefix);
676     // here's our unit
677 
678     _String             dataFilterID (chain.AddNameSpaceToID(*(_String*)parameters(0))),
679                         site_partition,
680                         sequence_partition;
681 
682 
683 
684     if (parameters.countitems()>3) {
685         sequence_partition = *(_String*)parameters(3);
686     }
687 
688     if (parameters.countitems()>4) {
689         site_partition = *(_String*)parameters(4);
690     }
691 
692     _DataSet            *dataset;
693 
694     _SimpleList         site_list,
695                         sequence_list;
696 
697     site_list.RequestSpace (1024UL);
698     sequence_list.RequestSpace (1024UL);
699 
700     auto ensure_site_partition = [] (_String& site_part, const long code, const long max_site) -> void {
701         if (code != 6 && site_part.empty () ) {
702             site_part = _String ("\"0-") & _String (max_site - 1L) & ("\"");
703         }
704     };
705 
706     if (!isFilter) {
707         dataset = (_DataSet*)dataSetList(dsID);
708         dataset -> ProcessPartition (site_partition,site_list,false, unit, nil, nil, chain.GetNameSpace());
709         ensure_site_partition (sequence_partition, code, dataset->NoOfColumns());
710         dataset->ProcessPartition (sequence_partition,sequence_list,true, unit, nil, nil, chain.GetNameSpace());
711 
712     } else {
713         const _DataSetFilter * dataset1 = GetDataFilter (dsID);
714         dataset1->GetData()->ProcessPartition (site_partition,site_list,false, unit, &dataset1->theNodeMap, &dataset1->theOriginalOrder, chain.GetNameSpace());
715         ensure_site_partition (sequence_partition, code, dataset1->GetSiteCount());
716         dataset1->GetData()->ProcessPartition (sequence_partition,sequence_list ,true,  unit, &dataset1->theOriginalOrder, &dataset1->theNodeMap, chain.GetNameSpace());
717         dataset = (_DataSet*)dataset1;
718     }
719 
720     if (code!=6) {
721         if (sequence_list.countitems() % unit) {
722             ReportWarning ((_String)"Unit size of "& _String((long)unit) & " doesn't divide the length of specified partition. The partition has been trimmed at the end.");
723             for (long chop = sequence_list.countitems(); chop>0; chop--) {
724                 sequence_list.Pop();
725             }
726         }
727         if (code == 27) {
728             sequence_list.Permute (unit);
729         } else {
730             sequence_list.PermuteWithReplacement(unit);
731         }
732 
733     }
734 
735     _DataSetFilter * thedf = new _DataSetFilter;
736     thedf->SetFilter (dataset, unit, site_list, sequence_list, isFilter);
737 
738     if (parameters.lLength>5) {
739         thedf->SetExclusions (GetStringFromFormula((_String*)parameters(5),chain.nameSpacePrefix));
740     } else if ( code!=6 && isFilter ) {
741         const _DataSetFilter * df1 = GetDataFilter (dsID);
742         if (df1->theExclusions.nonempty()) {
743             thedf->theExclusions << df1->theExclusions;
744             thedf->SetDimensions();
745         }
746     }
747 
748     thedf->SetDimensions();
749     thedf->SetupConversion();
750 
751     StoreDataFilter(dataFilterID, thedf, true);
752 }
753 
754 
755 
756 //____________________________________________________________________________________
757 
ExecuteCase54(_ExecutionList & chain)758 void      _ElementaryCommand::ExecuteCase54 (_ExecutionList& chain) {
759     chain.currentCommand++;
760 
761     SetStatusLine (_String("Constructing Topology ")&*(_String*)parameters(0));
762 
763     _String  *treeSpec = ((_String*)parameters(1));
764     _TreeTopology * tr = nil;
765 
766     _AssociativeList* mapping = (_AssociativeList*)FetchObjectFromVariableByType(&treeNodeNameMapping, ASSOCIATIVE_LIST);
767 
768     if (treeSpec->nonempty()) {
769         if (treeSpec->char_at (0)!='(') {
770             _Variable* testTree = FetchVar(LocateVarByName (AppendContainerName(*treeSpec,chain.nameSpacePrefix)));
771             if (testTree && testTree->ObjectClass () == TREE) {
772                 tr = new _TreeTopology ((_TheTree*)testTree);
773             } else {
774                 _String   flaData (*treeSpec);
775                 _Formula  nameForm (flaData,chain.nameSpacePrefix);
776                 HBLObjectRef formRes = nameForm.Compute();
777                 if (formRes&&formRes->ObjectClass () == STRING)
778                     tr = new _TreeTopology (AppendContainerName(*(_String*)parameters(0),chain.nameSpacePrefix),
779                                             ((_FString*)formRes)->get_str(),
780                                             false, mapping);
781             }
782         } else
783             tr = new _TreeTopology (AppendContainerName(*(_String*)parameters(0),chain.nameSpacePrefix),
784                                     *(_String*)parameters(1),false);
785     }
786 
787     if (!tr) {
788         HandleApplicationError ("Illegal right hand side in call to Topology id = ...; it must be a string, a Newick tree spec or a topology");
789     }
790 }
791 
792 //____________________________________________________________________________________
793 
ExecuteCase58(_ExecutionList & chain)794 void      _ElementaryCommand::ExecuteCase58 (_ExecutionList& chain)
795 {
796     chain.currentCommand++;
797 
798    static  const _String kStart ("START"),
799                   kPause ("PAUSE"),
800                   kResume ("RESUME");
801 
802     _String *   profileCode   = (_String*)parameters(0);
803 
804     if (*profileCode == kStart) {
805         chain.StartProfile();
806     } else if (*profileCode == kPause) {
807         chain.doProfile = 2;
808     } else if (*profileCode == kResume) {
809         chain.doProfile = 1;
810     } else {
811         _Variable * outVar = CheckReceptacle (&AppendContainerName(*profileCode,chain.nameSpacePrefix), blHBLProfile, true);
812         if (outVar) {
813             if (chain.profileCounter) {
814                 outVar->SetValue (chain.CollectProfile(),false,true,NULL);
815             } else {
816                 HandleApplicationError("Profiler dump invoked before #profile START;");
817             }
818         }
819     }
820 
821 }
822 
823 
824 //____________________________________________________________________________________
825 
ExecuteCase61(_ExecutionList & chain)826 void      _ElementaryCommand::ExecuteCase61 (_ExecutionList& chain)
827 {
828     chain.currentCommand++;
829 
830     HBLObjectRef           avl1    = FetchObjectFromVariableByType (&AppendContainerName(*(_String*)parameters(1),chain.nameSpacePrefix),ASSOCIATIVE_LIST),
831                         avl2  = FetchObjectFromVariableByType (&AppendContainerName(*(_String*)parameters(2),chain.nameSpacePrefix),ASSOCIATIVE_LIST),
832                         start   = parameters.lLength>3?FetchObjectFromVariableByType (&AppendContainerName(*(_String*)parameters(3),chain.nameSpacePrefix),NUMBER):nil;
833 
834     if (! (avl1 && avl2)) {
835         HandleApplicationError (_String ("Both arguments (") & *(_String*)parameters(1) & " and " & *(_String*)parameters(2) & " in a call to SCFG = ... must be evaluate to associative arrays");
836     } else {
837         Scfg    * scfg      = new Scfg ((_AssociativeList*)avl1,(_AssociativeList*)avl2,start?start->Value():0);
838         _String scfgName    = AppendContainerName(*(_String*)parameters(0),chain.nameSpacePrefix);
839         long    f           = FindSCFGName (scfgName);
840 
841         if (f==-1) {
842             for (f=0; f<scfgNamesList.lLength; f++)
843                 if (((_String*)scfgNamesList(f))->empty()) {
844                     break;
845                 }
846 
847             if (f==scfgNamesList.lLength) {
848                 scfgList << scfg;
849                 scfgNamesList&&(&scfgName);
850                 DeleteObject (scfg);
851             } else {
852                 scfgNamesList.Replace(f,&scfgName,true);
853                 scfgList.list_data[f] = (long)scfg;
854             }
855         } else {
856             scfgNamesList.Replace(f,&scfgName,true);
857             scfgList.Replace(f,scfg,false);
858         }
859     }
860 }
861 
862 //____________________________________________________________________________________
863 
ExecuteCase63(_ExecutionList & chain)864 void      _ElementaryCommand::ExecuteCase63 (_ExecutionList& chain)
865 {
866     chain.currentCommand++;
867 
868     /*_PMathObj         avl1    = FetchObjectFromVariableByType ((_String*)parameters(1),ASSOCIATIVE_LIST),
869                         avl2    = FetchObjectFromVariableByType ((_String*)parameters(2),ASSOCIATIVE_LIST),
870                         start   = parameters.lLength>3?FetchObjectFromVariableByType ((_String*)parameters(3),NUMBER):nil;
871 
872     if (! (avl1 && avl2))
873     {
874         _String errMsg = _String ("Both arguments (") & *(_String*)parameters(1) & " and " & *(_String*)parameters(2) & " in a call to SCFG = ... must be evaluate to associative arrays";
875         WarnError (errMsg);
876     }
877     else
878     {
879         Scfg    * scfg   = new Scfg ((_AssociativeList*)avl1,(_AssociativeList*)avl2,start?start->Value():0);
880         _String * str    = (_String*)parameters(0);
881         long    f        = FindSCFGName (*str);
882 
883         if (f==-1)
884         {
885             for (f=0; f<scfgNamesList.lLength; f++)
886                 if (((_String*)scfgNamesList(f))->sLength==0)
887                     break;
888 
889             if (f==scfgNamesList.lLength)
890             {
891                 scfgList << scfg;
892                 scfgNamesList&&(str);
893                 DeleteObject (scfg);
894             }
895             else
896             {
897                 scfgNamesList.Replace(f,str,true);
898                 scfgList.list_data[f] = (long)scfg;
899             }
900         }
901         else
902         {
903             scfgNamesList.Replace(f,str,true);
904             scfgList.Replace(f,scfg,false);
905         }
906     }   */
907 }
908 
909 
910 //____________________________________________________________________________________
911 
ExecuteCase64(_ExecutionList & chain)912 void    _ElementaryCommand::ExecuteCase64 (_ExecutionList& chain)
913 {
914 	ReportWarning (_String("ExecuteCase64()"));
915     chain.currentCommand++;
916 
917     HBLObjectRef   avl1    = FetchObjectFromVariableByType (&AppendContainerName(*(_String*)parameters(1),chain.nameSpacePrefix), ASSOCIATIVE_LIST);
918 
919     if (! (avl1)) {
920         HandleApplicationError (_String ("Argument (") & *(_String*)parameters(1) & " in call to BGM = ... must evaluate to associative array");
921     } else {
922         _BayesianGraphicalModel * bgm   = new _BayesianGraphicalModel ((_AssociativeList *) avl1);
923 
924         _String bgmName     = AppendContainerName (*(_String *) parameters(0), chain.nameSpacePrefix);
925         long    bgmIndex    = FindBgmName (bgmName);
926 
927         if (bgmIndex == -1) {   // not found
928             for (bgmIndex = 0; bgmIndex < bgmNamesList.lLength; bgmIndex++) {
929                 // locate empty strings in name list
930                 if (((_String *)bgmNamesList(bgmIndex))->empty() == 0) {
931                     break;
932                 }
933             }
934 
935             if (bgmIndex == bgmNamesList.lLength) {
936                 // reached end of list without finding empty string, append new string
937                 bgmList.AppendNewInstance(bgm);
938                 bgmNamesList && (&bgmName);
939             } else {
940                 // replace empty string in list
941                 bgmNamesList.Replace (bgmIndex, &bgmName, true);
942                 bgmList.Replace (bgmIndex, bgm, false);
943             }
944         } else { // 20070626: SLKP edit to deal with already existing BGMs
945             bgmNamesList.Replace(bgmIndex,&bgmName,true);
946             bgmList.Replace(bgmIndex,bgm,false);
947         }
948 
949 		ReportWarning(_String("Created BGM ") & bgmName & " at index " & bgmIndex);
950     }
951 }
952 
953 //____________________________________________________________________________________
954 
955 
appendCompiledFormulae(_Formula * f,_Formula * f2)956 void   _ElementaryCommand::appendCompiledFormulae(_Formula* f, _Formula *f2) {
957   if (f || f2) {
958     _SimpleList*        varList = new _SimpleList;
959     _AVLList            varListA (varList);
960     if (f)
961       f->ScanFForVariables (varListA, true, true, true, true);
962     if (f2)
963       f2->ScanFForVariables(varListA, true, true);
964     varListA.ReorderList();
965     listOfCompiledFormulae<<(long)this;
966     compiledFormulaeParameters.AppendNewInstance(varList);
967   }
968 }
969 
970 //____________________________________________________________________________________
DecompileFormulae(void)971 bool    _ElementaryCommand::DecompileFormulae (void) {
972   switch (code) {
973     case 0:
974       if (simpleParameters.nonempty()) {
975         _Formula* f = (_Formula*)simpleParameters.list_data[1],
976                 *f2 = (_Formula*)simpleParameters.list_data[2] ;
977         if (f) {
978           delete f;
979         }
980         if (f2) {
981           delete f2;
982         }
983         simpleParameters.Clear();
984 
985         return true;
986       }
987       break;
988     case 4: {
989       if (parameters.lLength && simpleParameters.lLength == 3) {
990         _Formula* f = (_Formula*)simpleParameters.list_data[2];
991         if (f) {
992           delete f;
993         }
994         simpleParameters.Delete (2);
995         return true;
996       }
997       break;
998     }
999     case 14: {
1000       if (parameters.lLength && simpleParameters.lLength == 2) {
1001         _Formula* f = (_Formula*)simpleParameters.list_data[1];
1002         if (f) {
1003           delete f;
1004         }
1005         simpleParameters.Delete (1);
1006         return true;
1007       }
1008       break;
1009     }
1010 
1011 
1012   }
1013   return false;
1014 }
1015 
1016 //____________________________________________________________________________________
ConstructSCFG(_StringBuffer & source,_ExecutionList & target)1017 bool    _ElementaryCommand::ConstructSCFG (_StringBuffer&source, _ExecutionList&target) {
1018 // syntax: SCFG ident = (Rules1, Rules2 <,start>)
1019 
1020     long    mark1 = source.FirstNonSpaceFollowingSpace (),
1021             mark2 = mark1 > 0 ? source.FindTerminator (mark1 + 1, "=") : 0;
1022 
1023 
1024     if ( mark1==-1 || mark2==-1 || mark1+1 > mark2  ) {
1025         HandleApplicationError ("SCFG declaration missing a valid identifier");
1026         return false;
1027     }
1028 
1029     _String scfgID (source,mark1,mark2-1);
1030 
1031     _List pieces;
1032     mark2 ++;
1033     mark1 = source.ExtractEnclosedExpression(mark2, '(', ')', fExtractRespectQuote | fExtractRespectEscape);
1034 
1035     ExtractConditions (source,mark2+1,pieces,',');
1036 
1037     if ( mark1==-1 || mark2==-1 || mark1<mark2 || (pieces.lLength != 2 && pieces.lLength != 3)) {
1038         HandleApplicationError ("Expected: SCFG ident = (Rules1, Rules2 <,start>)");
1039         return false;
1040     }
1041 
1042     _ElementaryCommand * scfg = new _ElementaryCommand (61);
1043 
1044     scfg->parameters    &&(&scfgID);
1045     scfg->addAndClean(target,&pieces,0);
1046     return true;
1047 }
1048 
1049 //____________________________________________________________________________________
ConstructBGM(_StringBuffer & source,_ExecutionList & target)1050 bool    _ElementaryCommand::ConstructBGM (_StringBuffer&source, _ExecutionList&target)
1051 // syntax: BGM ident = (<nodes>)
1052 {
1053     //ReportWarning(_String("ConstructBGM()"));
1054     // locate ident in HBL string
1055     long    mark1 = source.FirstNonSpaceFollowingSpace (),
1056             mark2 = mark1 > 0 ? source.FindTerminator (mark1 + 1, "=") : 0;
1057 
1058     // assign ident to _String variable
1059 
1060     if ( mark1==-1 || mark2==-1 || mark1+1 > mark2  ) {
1061         HandleApplicationError ("BGM declaration missing a valid identifier");
1062         return false;
1063     }
1064 
1065     _String bgmID (source,mark1,mark2-1);
1066 
1067     _List pieces;
1068     mark2 ++;
1069     mark1 = source.ExtractEnclosedExpression(mark2, '(', ')', fExtractRespectQuote | fExtractRespectEscape);
1070 
1071     ExtractConditions (source,mark2+1,pieces,',');
1072 
1073     if ( mark1==-1 || mark2==-1 || mark1<mark2 || (pieces.lLength != 1)) {
1074         HandleApplicationError ("Expected: BGM ident = (<nodes>)");
1075         return false;
1076     }
1077 
1078     _ElementaryCommand * bgm = new _ElementaryCommand (64);
1079     bgm->parameters && (&bgmID);
1080     bgm->addAndClean(target,&pieces,0);
1081 
1082     return true;
1083 }
1084 
1085 
1086 //____________________________________________________________________________________
1087 
RetrieveModelComponents(long mid,_Matrix * & mm,_Matrix * & fv,bool & mbf)1088 void    RetrieveModelComponents (long mid, _Matrix*& mm, _Matrix*& fv, bool & mbf)
1089 {
1090     if (mid >=0 && mid < modelTypeList.lLength) {
1091         if (modelTypeList.list_data[mid] == 0) {
1092             mm = (_Matrix*)FetchObjectFromVariableByTypeIndex(modelMatrixIndices.list_data[mid],MATRIX);
1093         } else {
1094             mm = nil;
1095         }
1096 
1097         long fvi = modelFrequenciesIndices.list_data[mid];
1098         fv = (_Matrix*)FetchObjectFromVariableByTypeIndex(fvi>=0?fvi:(-fvi-1),MATRIX);
1099         mbf = (fvi>=0);
1100     } else {
1101         mm = fv = nil;
1102         mbf = false;
1103     }
1104 }
1105 
1106 //____________________________________________________________________________________
1107 
RetrieveModelComponents(long mid,_Variable * & mm,_Variable * & fv,bool & mbf)1108 void    RetrieveModelComponents (long mid, _Variable*& mm, _Variable*& fv, bool & mbf)
1109 {
1110     if (mid >= 0 && modelTypeList.list_data[mid] == 0) {
1111         mm = LocateVar(modelMatrixIndices.list_data[mid]);
1112     } else {
1113         mm = nil;
1114     }
1115 
1116     long fvi = modelFrequenciesIndices.list_data[mid];
1117     fv = LocateVar (fvi>=0?fvi:(-fvi-1));
1118     mbf = (fvi>=0);
1119 }
1120 
1121 
1122 
1123 //____________________________________________________________________________________
1124 
ScanModelForVariables(long modelID,_AVLList & theReceptacle,bool inclG,long modelID2,bool inclCat)1125 void    ScanModelForVariables        (long modelID, _AVLList& theReceptacle, bool inclG, long modelID2, bool inclCat)
1126 {
1127     if (modelID != HY_NO_MODEL) {
1128         // standard rate matrix
1129         if (modelTypeList.list_data[modelID] == 0) {
1130             ((_Matrix*) (LocateVar(modelMatrixIndices.list_data[modelID])->GetValue()))->ScanForVariables2(theReceptacle,inclG,modelID2,inclCat);
1131         } else {
1132         // formula based
1133             // inclG was replaced with false in a previous commit. This caused problems in the optimizer and in
1134             // likelihood reporting (it was consistently worse than optimizer results)
1135             ((_Formula*)modelMatrixIndices.list_data[modelID])->ScanFForVariables(theReceptacle, inclG, false, inclCat);
1136         }
1137     }
1138 }
1139 
1140 //____________________________________________________________________________________
1141 
_HYHBLTypeToText(long type)1142 _String const _HYHBLTypeToText (long type) {
1143     _StringBuffer result (128L);
1144     if (type & HY_BL_DATASET) {
1145         result << "DataSet|";
1146     }
1147 
1148     if (type & HY_BL_DATASET_FILTER) {
1149         result << "DataSetFilter|";
1150     }
1151 
1152     if (type & HY_BL_LIKELIHOOD_FUNCTION) {
1153         result << "LikelihoodFunction|";
1154     }
1155 
1156     if (type & HY_BL_SCFG) {
1157         result << "SCFG|";
1158     }
1159 
1160     if (type & HY_BL_BGM) {
1161         result << "BGM|";
1162     }
1163 
1164     if (type & HY_BL_MODEL) {
1165         result << "Model|";
1166     }
1167 
1168     if (type & HY_BL_HBL_FUNCTION) {
1169         result << "function|";
1170     }
1171 
1172     result.Trim (0,result.length()-2L);
1173     return result;
1174 }
1175 
1176 
1177 
1178 
1179 //____________________________________________________________________________________
1180 
1181 
ScanStringExpressionForHBLFunctions(_String * expression,_ExecutionList const & chain,bool recursive,_AVLListX & collection,bool help_mode)1182 void _ElementaryCommand::ScanStringExpressionForHBLFunctions (_String* expression, _ExecutionList const& chain, bool recursive, _AVLListX& collection, bool help_mode) {
1183 
1184   _Formula f, f2;
1185 
1186   _String err_msg;
1187   _FormulaParsingContext fpc (&err_msg, chain.nameSpacePrefix);
1188   fpc.buildComplexObjects() = false;
1189 
1190   long     parseCode = Parse(&f,*expression,fpc,&f2);
1191 
1192   if (parseCode != HY_FORMULA_FAILED ) {
1193     f.ScanFormulaForHBLFunctions (collection, recursive, !help_mode);
1194     f2.ScanFormulaForHBLFunctions(collection, recursive, !help_mode);
1195   }
1196 
1197 
1198 }
1199 
1200 //____________________________________________________________________________________
1201 
BuildListOfDependancies(_AVLListX & collection,bool recursive,_ExecutionList const & chain,bool help_mode)1202 void      _ElementaryCommand::BuildListOfDependancies    (_AVLListX & collection, bool recursive, _ExecutionList const & chain, bool help_mode) {
1203 
1204   switch (code) {
1205 
1206     case 0:
1207     case 4:
1208     case 14:
1209     {
1210       if (parameters.lLength) {
1211         ScanStringExpressionForHBLFunctions((_String*)parameters (0), chain, recursive, collection, help_mode);
1212       }
1213       break;
1214     }
1215 
1216 
1217 
1218   }
1219 }
1220 
1221 
1222