-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNexusUserInterface.cpp
1161 lines (1032 loc) · 35.2 KB
/
NexusUserInterface.cpp
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
#include <sys/stat.h>
#include "NexusUserInterface.h"
/*
* Use the preprocessor to define a derived class for each command,
* the derived class implements what some people would call a functionoid.
* A functionoid is basically a pure virtual function in a base class
* that is overloaded a bunch of times in derived classes, with each
* derived class implementing different functionality. In this case
* the different functionality is all of the commands... The overloaded
* functioniod MenuFunction simply calls a function in the CNexusUserInterface
* class, that function is different for each derived class and is always takes
* the name of the "type" parameter (Ex: Spacer, OpenNexusFile, SaveFile, etc...)
*
* The behavior of the MenuFunction is defined to return true if the
* program is to continue to operate, and false if the program should
* terminate. Possibly due to errors, or if the user selects the quit
* command.
*
* Note: The preprocessor ## concatenation operator is used to create
* the class name based on the value of the "type" macro parameter
*/
#ifdef _WINDOWS
#define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
#endif
#define NEW_COMMAND_DEFINE_CAST(type, cast) \
class type : public CNexusMenuBase \
{ \
public:\
type(const char * strCommand, const char * strHelpText) : CNexusMenuBase(strCommand, strHelpText){}\
type(const char * strCommand, const char * strHelpText, map<const char*, int, ltstr> assignments) : CNexusMenuBase(strCommand, strHelpText, assignments){}\
type(const char * strCommand, const char * strHelpText, vector<int> assignments) : CNexusMenuBase(strCommand, strHelpText, assignments){}\
bool MenuFunction(CNexusUserInterface *pNexusUserInterface, string *value, int nMappedVal)\
{\
return pNexusUserInterface->f##type(value, (cast)nMappedVal);\
}\
};
#define NEW_COMMAND_DEFINE(type) NEW_COMMAND_DEFINE_CAST(type, int)
#define MAKE_STR_VECTOR(slist) vector<string>(slist, slist + (sizeof(slist) / sizeof(slist[0])))
#define MAKE_INT_VECTOR(slist) vector<int>(slist, slist + (sizeof(slist) / sizeof(slist[0])))
/*
* The followind actually defines the derived class for each command on the main menu
*/
NEW_COMMAND_DEFINE(CNexusMenuSpacer )
NEW_COMMAND_DEFINE(CNexusMenuOpenNexusFile )
NEW_COMMAND_DEFINE(CNexusMenuOpenTreeFile )
NEW_COMMAND_DEFINE(CNexusMenuSaveFile )
NEW_COMMAND_DEFINE(CNexusMenuCloseNexusFile )
NEW_COMMAND_DEFINE(CNexusMenuHelp )
NEW_COMMAND_DEFINE(CNexusMenuQuit )
NEW_COMMAND_DEFINE(CNexusMenuAbout )
NEW_COMMAND_DEFINE(CNexusMenuCommandLog )
NEW_COMMAND_DEFINE(CNexusMenuStatus )
NEW_COMMAND_DEFINE(CNexusMenuChdir )
NEW_COMMAND_DEFINE(CNexusMenuExclude )
NEW_COMMAND_DEFINE(CNexusMenuInclude )
NEW_COMMAND_DEFINE(CNexusMenuOutgroup )
NEW_COMMAND_DEFINE(CNexusMenuIngroup )
//NEW_COMMAND_DEFINE(CNexusMenuChar )
NEW_COMMAND_DEFINE(CNexusMenuOrdered )
NEW_COMMAND_DEFINE(CNexusMenuUnordered )
NEW_COMMAND_DEFINE(CNexusMenuHeuristicSearch)
NEW_COMMAND_DEFINE(CNexusMenuScoreTree )
NEW_COMMAND_DEFINE(CNexusMenuExhaust )
NEW_COMMAND_DEFINE(CNexusMenuBNB )
NEW_COMMAND_DEFINE(CNexusMenuStepwise )
NEW_COMMAND_DEFINE(CNexusMenuBootstrap )
NEW_COMMAND_DEFINE(CNexusMenuJackknife )
NEW_COMMAND_DEFINE(CNexusMenuSTR )
NEW_COMMAND_DEFINE(CNexusMenuConsens )
NEW_COMMAND_DEFINE(CNexusMenuCollapse )
NEW_COMMAND_DEFINE(CNexusMenuReport )
/*
* The following actually defines the derived class for each command on the set menu
*/
NEW_COMMAND_DEFINE_CAST(CNexusMenuBranchSwapType, mpl_bbreak_t)
NEW_COMMAND_DEFINE_CAST(CNexusMenuAddSeqType , mpl_addseq_t)
NEW_COMMAND_DEFINE_CAST(CNexusMenuRatchetSearch , bool)
//NEW_COMMAND_DEFINE_CAST(CNexusMenuCollapseAt , mfl_set_collapse_at_t)
//NEW_COMMAND_DEFINE_CAST(CNexusMenuCollapse , bool)
NEW_COMMAND_DEFINE(CNexusMenuNumReplicates)
NEW_COMMAND_DEFINE(CNexusMenuTreeLimit)
//NEW_COMMAND_DEFINE_CAST(CNexusMenuRatchetSearch , bool)
NEW_COMMAND_DEFINE_CAST(CNexusMenuGap, mpl_gap_t)
NEW_COMMAND_DEFINE_CAST(CNexusMenuHold, int)
NEW_COMMAND_DEFINE(CNexusMenuMainMenu )
/*
* The UI constructor puts the menu together, it stores each
* menu option in a stl vector.
*/
CNexusUserInterface::CNexusUserInterface()
{
m_mflHandle = NULL;
m_pNexusParse = NULL;
m_pNexusInputTrees = NULL;
m_strCwd = "./";
m_pMainMenu = new CNexusMenuData("morphy>");
if (!m_pMainMenu)
{
throw "Unable to allocate memory for main menu";
}
m_pMainMenu->AddMenuItem(new CNexusMenuSpacer (NULL, "File"));
m_pMainMenu->AddMenuItem(new CNexusMenuOpenNexusFile ("open" , "Open a nexus file"));
m_pMainMenu->AddMenuItem(new CNexusMenuOpenTreeFile ("gettrees" , "Open a nexus file"));
m_pMainMenu->AddMenuItem(new CNexusMenuCloseNexusFile ("close" , "Close a nexus file"));
m_pMainMenu->AddMenuItem(new CNexusMenuSaveFile ("save" , "Save according to the options"));
m_pMainMenu->AddMenuItem(new CNexusMenuSpacer (NULL, "Program"));
m_pMainMenu->AddMenuItem(new CNexusMenuCommandLog ("log" ,"Toggles record of commands, variable states, etc"));
m_pMainMenu->AddMenuItem(new CNexusMenuStatus ("status" ,"Prints status of all current settings, eg. logmode on/off"));
m_pMainMenu->AddMenuItem(new CNexusMenuChdir ("cd" ,"change working directory"));
m_pMainMenu->AddMenuItem(new CNexusMenuHelp ("help" , "Help"));
m_pMainMenu->AddMenuItem(new CNexusMenuAbout ("about" , "About"));
m_pMainMenu->AddMenuItem(new CNexusMenuQuit ("quit" , "Quit"));
m_pMainMenu->AddMenuItem(new CNexusMenuSpacer (NULL, "Data"));
m_pMainMenu->AddMenuItem(new CNexusMenuExclude ("exclude" , "Exclude taxa or characters"));
m_pMainMenu->AddMenuItem(new CNexusMenuInclude ("include" , "Include excluded taxa or characters"));
m_pMainMenu->AddMenuItem(new CNexusMenuOutgroup ("outGroup" , "Assign taxa to outgroup"));
m_pMainMenu->AddMenuItem(new CNexusMenuIngroup ("inGroup" , "Return taxa from outgroup to ingroup"));
m_pMainMenu->AddMenuItem(new CNexusMenuOrdered ("ordered" , "Define characters as ordered type"));
m_pMainMenu->AddMenuItem(new CNexusMenuUnordered ("Unordered" , "Define characters as unordered type"));
m_pMainMenu->AddMenuItem(new CNexusMenuSpacer (NULL, "Analysis"));
ConfigMenuRatchetSearch();
m_pMainMenu->AddMenuItem(new CNexusMenuHeuristicSearch ("heuristic" , "Begin a heuristic search"));
m_pMainMenu->AddMenuItem(new CNexusMenuScoreTree ("scoretrees" , "Report the score of trees in memory"));
// m_pMainMenu->AddMenuItem(new CNexusMenuRatchetSearch ("ratchet" , "Toggle ratchet searching"));
// m_pMainMenu->AddMenuItem(new CNexusMenuExhaust ("exhaustive" , "Begin an exhaustive search"));
// m_pMainMenu->AddMenuItem(new CNexusMenuBNB ("branchbound" , "Begin a branch-and-bound search"));
// m_pMainMenu->AddMenuItem(new CNexusMenuStepwise ("stepwise" , "Begin a stepwise search"));
// m_pMainMenu->AddMenuItem(new CNexusMenuBootstrap ("bootStrap" , "Begin a bootstrap analysis"));
// m_pMainMenu->AddMenuItem(new CNexusMenuJackknife ("jackKnife" , "Begin a jackknife analysis"));
// m_pMainMenu->AddMenuItem(new CNexusMenuSTR ("reduction" , "Perform a safe taxonomic reduction"));
m_pMainMenu->AddMenuItem(new CNexusMenuSpacer (NULL, "Results"));
m_pMainMenu->AddMenuItem(new CNexusMenuConsens ("consensus" , "Compute consensus tree for trees in memory"));
//m_pMainMenu->AddMenuItem(new CNexusMenuCollapse ("collapse" , "Collapse zero-length branches, condense the tree set"));
m_pMainMenu->AddMenuItem(new CNexusMenuReport ("report" , "Print a report about the current open nexus file"));
const int NumReps [] = {0, 10000000};
const int TreeLimit [] = {0, 10000000};
const int holdLimit [] = {0, 10000000};
m_pMainMenu->AddMenuItem(new CNexusMenuSpacer (NULL, "Parameters"));
ConfigMenuBranchSwapType();
ConfigMenuAddSeqType();
//CJD FIXME: ConfigMenuCollapseAt();
//CJD FIXME: ConfigMenuCollapseZero();
m_pMainMenu->AddMenuItem(new CNexusMenuNumReplicates ("nreps" , "Set the number of replications for a heuristic search", MAKE_INT_VECTOR(NumReps)));
m_pMainMenu->AddMenuItem(new CNexusMenuTreeLimit ("treeLimit" , "Set the maximum number of trees allowed to be stored in memory", MAKE_INT_VECTOR(TreeLimit)));
m_pMainMenu->AddMenuItem(new CNexusMenuHold ("hold" , "Set the number of trees to hold during stepwise addition", MAKE_INT_VECTOR(holdLimit)));
ConfigMenuGap();
m_ioCommands = new CEditLineHist("nui1234567890", &m_fCommandLog);
m_ioInputFiles = new CEditLineHist("nui12345678901234567890", &m_fCommandLog);
m_ioLogFiles = new CEditLineHist("nui1234567890", &m_fCommandLog);
m_ioWorkingDir = new CEditLineHist("nui1234567890", &m_fCommandLog);
m_ioNumericSubCommands = new CEditLineHist("nui1234567890", &m_fCommandLog);
}
void CNexusUserInterface::ConfigMenuBranchSwapType()
{
map<const char*, int, ltstr> selections;
selections["TreeBisection"] = MPL_TBR_T;
selections["TBR"] = MPL_TBR_T;
selections["SubtreePruning"] = MPL_SPR_T;
selections["SPR"] = MPL_SPR_T;
m_pMainMenu->AddMenuItem(new CNexusMenuBranchSwapType ("branchSwap" , "Set branch swap type for heuristic searches", selections));
}
void CNexusUserInterface::ConfigMenuAddSeqType()
{
map<const char*, int, ltstr> selections;
// selections["Simple"] = PAWM_AST_SIMPLE;
selections["Random"] = MPL_AST_RANDOM;
selections["AsIs"] = MPL_AST_ASIS;
selections["None"] = MPL_AST_INMEM;
// selections["Closest"] = PAWM_AST_CLOSEST;
m_pMainMenu->AddMenuItem(new CNexusMenuAddSeqType ("addSeq" , "Selects the manner in which branches are added during the generation of starting trees", selections));
}
/* CJD FIXME:
void CNexusUserInterface::ConfigMenuCollapseAt()
{
map<const char*, int, ltstr> selections;
selections["MaxIs0"] = MFL_SC_MAX_LEN;
selections["MinIs0"] = MFL_SC_MIN_LEN;
selections["Equal"] = MFL_SC_EQUAL_RECONSTRUCTION_SETS;
m_pMainMenu->AddMenuItem(new CNexusMenuCollapseAt ("collapseAt" , "Configure when to collapse nodes", selections));
}
*/
void CNexusUserInterface::ConfigMenuCollapseZero()
{
// TODO: Remove this when implemented
cout << "Not implemented." << endl;
return;
map<const char*, int, ltstr> selections;
// TODO: COnfigure these settings
selections["no"] = false;
selections["min"] = true;
selections["max"] = true; // TODO: These need MPL codes
m_pMainMenu->AddMenuItem(new CNexusMenuCollapse ("collapseZero" , "Enable collapsing of zero length branches during search", selections));
}
void CNexusUserInterface::ConfigMenuRatchetSearch()
{
map<const char*, int, ltstr> selections;
selections["Yes"] = true;
selections["No"] = false;
m_pMainMenu->AddMenuItem(new CNexusMenuRatchetSearch ("ratchet" , "Toggle the ratchet", selections));
}
void CNexusUserInterface::ConfigMenuGap()
{
map<const char*, int, ltstr> selections;
selections["Inapplicable"] = GAP_INAPPLIC;
selections["Missing"] = GAP_MISSING;
selections["State"] = GAP_NEWSTATE;
m_pMainMenu->AddMenuItem(new CNexusMenuGap ("gap" , "Set whether gap symbol ('-') will be treated as inapplicability or as missing data", selections));
}
void CNexusUserInterface::ChangeMenu(CNexusMenuData *pMenu)
{
m_pMenu = pMenu;
// fCNexusMenuHelp(NULL, 0, false);
}
void CNexusUserInterface::Delete(CEditLineHist *pMem)
{
if (pMem)
{
delete (pMem);
}
}
/*
* The UI destructor deletes all memory.
*/
CNexusUserInterface::~CNexusUserInterface()
{
delete m_pMainMenu;
fCNexusMenuCloseNexusFile(NULL, 0, false);
if (m_fCommandLog)
{
m_fCommandLog.close();
}
DestroyHandle();
Delete(m_ioCommands);
Delete(m_ioInputFiles);
Delete(m_ioLogFiles);
Delete(m_ioWorkingDir);
Delete(m_ioNumericSubCommands);
}
/*
* Actually read input from the user, and issue the selected commands
*/
void CNexusUserInterface::DoMenu()
{
bool ret = true;
string strInput;
fCNexusMenuAbout(NULL, 0, false);
ChangeMenu(m_pMainMenu);
do
{
try
{
strInput.clear();
m_ioCommands->GetUserInput(m_pMenu->GetPrompt(), &strInput, false);
ret = m_pMenu->RunSelections(strInput, this);
}
catch (const char *e)
{
cout<<endl<<"Error: "<<e<<endl;
}
catch (ifstream::failure &e)
{
cout<<endl<<"Error: "<<e.what()<<endl;
}
catch (NxsException &e)
{
cout<<endl<<"Error: "<<e.nxs_what()<<endl;
}
catch (const std::exception &e)
{
cout<<endl<<"Error: "<<e.what()<<endl;
}
catch (...)
{
cout<<endl<<"Error: Unknown exception"<<endl;
}
} while (ret);
}
/*
* Run the open file command, just prompt the user for input
* and attempt to read the nexus file
*/
bool CNexusUserInterface::fCNexusMenuOpenNexusFile(string *value, int nMappedVal)
{
string strFilename = *value;
// struct stat st;
if (!m_pNexusParse)
{
if (strFilename.length() == 0)
{
m_ioInputFiles->GetUserInput(" Enter filename: " + m_strCwd, &strFilename, true);
}
// Check if the input isn't a path by checking for slashes
if (strFilename.find_first_of("\\/") == string::npos)
{
strFilename = m_strCwd + strFilename;
}
m_pNexusParse = new CNexusParse();
if (m_pNexusParse)
{
if (m_pNexusParse->ReadNexusFile(&strFilename, NULL))
{
CreateHandle();
cout<<" "<<strFilename<<" opened successfully"<<endl;
}
else
{
fCNexusMenuCloseNexusFile(NULL, false);
cout<<" Error: Unable to read "<<strFilename<<endl;
}
}
else
{
cout<<" Error: Unable to open "<<strFilename<<endl;
}
}
else
{
cout<<" Error: Nexus file "<<m_pNexusParse->m_cNexusReader->GetInFileName()<<" is already open"<<endl;
}
return true;
}
bool CNexusUserInterface::fCNexusMenuOpenTreeFile(string *value, int nMappedVal)
{
int i = 0;
string strFilename = *value;
if (m_pNexusParse)
{
if (strFilename.length() == 0)
{
m_ioInputFiles->GetUserInput(" Enter filename: " + m_strCwd, &strFilename, true);
}
if (strFilename.find_first_of("\\/") == string::npos)
{
strFilename = m_strCwd + strFilename;
}
m_pNexusInputTrees = new CNexusParse();
if (m_pNexusInputTrees)
{
m_pNexusInputTrees->m_cTaxa = m_pNexusParse->GetNexusTaxaBlock()->Clone();
if (m_pNexusInputTrees->ReadNexusFile(&strFilename, NULL))
{
cout<<" "<<strFilename<<" opened successfully"<<endl<<endl;
m_pNexusInputTrees->m_cTaxa = NULL;
long ntrees = mpl_get_num_trees(m_mflHandle);
if (ntrees) {
cout << "There are " << ntrees << " currently in memory." << endl;
cout << "Would you like to append? ";
if (!UserConfirmation()) {
cout << endl;
cout << "To overwrite trees clear the buffer (cleartrees) and retry import" << endl;
return true;
}
}
for (i = 0; i < m_pNexusInputTrees->m_cTrees->GetNumTrees(); ++i)
{
const NxsFullTreeDescription &t = m_pNexusInputTrees->m_cTrees->GetFullTreeDescription(i);
string newick = t.GetNewick();
newick.append(";");
mpl_add_newick(newick.c_str(), m_mflHandle);
}
cout << i << " trees successfully read from file" << endl;
}
else
{
// TODO: Replace call to fCNexusMenuCloseNexusFile(NULL, false);
cout<<" Error: Unable to read "<<strFilename<<endl;
}
delete m_pNexusInputTrees;
m_pNexusInputTrees = NULL;
}
else
{
cout<<" Error: Unable to open "<<strFilename<<endl;
}
}
else
{
cout<<"No file open"<<endl;
}
return true;
}
void CNexusUserInterface::DestroyHandle()
{
if (m_mflHandle)
{
// delete m_mflHandle;
// m_mflHandle = NULL;
mpl_handle_delete(&m_mflHandle);
}
}
void CNexusUserInterface::CreateHandle()
{
DestroyHandle();
int err = 0;
stringstream ss;
int nTax = m_pNexusParse->m_cTaxa->GetNTax();
int nChar = m_pNexusParse->m_cChars->GetNCharTotal();
int i;
std::string tmp;
const char* cstr;
m_mflHandle = mpl_handle_new();//new PAWM(nTax, nChar);
if (m_mflHandle != NULL)
{
err = 0;
err = mpl_set_dimensions(nTax, nChar, m_mflHandle);
if (err < 0)
{
cout << "Unable to set dimensions. Likely causes: " << endl;
if (nTax < 3 && nTax > 0)
{
cout << "- Input specifies fewer than 3 taxa." << endl;
}
else if (nChar < 1 || nTax < 0)
{
cout << "- Number of characters less than 1" << endl;
cout << "- Illogical (i.e. negative) matrix dimensions." << endl;
}
else
{
cout << "- Insufficient memory." << endl;
}
}
}
else
{
cout << "Error: unable to initialise Morphy. " << endl <<
"Likely insufficient memory for operation." << endl;
return;
}
for (i = 0; i < nTax; i++)
{
m_pNexusParse->m_cChars->WriteStatesForTaxonAsNexus(ss, i, 0, nChar);
}
ss << ";";
tmp = ss.str();
cstr = tmp.c_str();
err = 0;
err = mpl_attach_rawdata(MPL_DISCR_T, cstr, m_mflHandle);
if (err < 0)
{
cout << "Unable to read data. Check your file for errors." << endl;
}
}
bool CNexusUserInterface::SaveTranslateTable(myofstream &fSave)
{
int it;
int n_taxa = m_pNexusParse->m_cTaxa->GetNumTaxonLabels();
for (it = 0; it < n_taxa; it++)
{
fSave<<"\t\t"<<it + 1<<" ";
if (m_pNexusParse->m_cTaxa->NeedsQuotes(it))
{
fSave<<"'"<<m_pNexusParse->m_cTaxa->GetTaxonLabel(it)<<"'";
}
else
{
fSave<<m_pNexusParse->m_cTaxa->GetTaxonLabel(it);
}
if (it < n_taxa-1)
{
fSave<<",";
}
fSave<<endl;
}
return true;
}
bool CNexusUserInterface::SaveNewickStrings(myofstream &fSave)
{
// TODO: Re-implement
if (m_mflHandle)
{
std::vector<std::string> newicks;
if (mpl_get_num_trees(m_mflHandle) > 0)
{
int i = 0;
for (i = 0; i < mpl_get_num_trees(m_mflHandle); i++)
{
char* newick = mpl_get_newick(i, m_mflHandle);
fSave<<"\t\tTREE Morphy_"<< i+1 <<" = [&U] " << newick <<endl;
// TODO: Free the string
}
}
else
{
cout<<"Unable to get newick trees."<<endl;
}
}
else
{
cout<<"No file open."<<endl;
}
return true;
}
bool CNexusUserInterface::UserConfirmation()
{
string usrResponse;
do {
m_ioLogFiles->GetUserInput(" (y,n): ", &usrResponse, false);
if (usrResponse.compare("y") == 0 || usrResponse.compare("Y") == 0) {
return true;
} else if (usrResponse.compare("n") == 0 || usrResponse.compare("N") == 0) {
cout << "Aborting." << endl;
return false;
}
} while (1);
}
bool CNexusUserInterface::fCNexusMenuSaveFile(string *value, int nMappedVal)
{
string strFilename = *value;
string inFilename;
string usrResponse;
myofstream fSave;
struct stat st;
if (strFilename.length() == 0)
{
m_ioLogFiles->GetUserInput(" Enter save filename: " + m_strCwd, &strFilename, true);
}
inFilename = strFilename; // Store a copy of the input file name w/o path for reporting back to user
strFilename = m_strCwd + strFilename;
// Check if the file does not already exist & warn the user if it does.
if ((stat(strFilename.c_str(), &st) == 0) && (S_ISREG(st.st_mode)))
{
cout <<"'"<< inFilename <<"'"<< " is already a file, would you like to overwrite?" << endl;
if (!UserConfirmation()) {
return true;
}
}
fSave.open(strFilename.c_str());
if (fSave)
{
fSave<<"#NEXUS"<<endl;
fSave<<"BEGIN TREES;"<<endl<<"\tTRANSLATE"<<endl;
SaveTranslateTable(fSave);
fSave<<"\t\t;"<<endl<<endl;
SaveNewickStrings(fSave);
fSave<<"END;";
fSave.close();
cout<<" Successfully opened '"<<strFilename<<"'"<<endl;
}
else
{
cout<<" Error: Unable to open '"<<strFilename<<"'"<<endl;
}
return true;
}
/*
* Close a file opened with the OpenNexusFile command
* bVerbose is defaulted to = true
*/
bool CNexusUserInterface::fCNexusMenuCloseNexusFile(string *value, int nMappedVal, bool bVerbose)
{
if (m_pNexusParse)
{
delete m_pNexusParse;
m_pNexusParse = NULL;
delete m_pNexusInputTrees;
m_pNexusInputTrees = NULL;
if (bVerbose)
{
cout<<" Successfully closed file..."<<endl;
}
}
else if (bVerbose)
{
cout<<" Error: No file is currently open"<<endl;
}
DestroyHandle();
return true;
}
/*
* Print the menu
*/
bool CNexusUserInterface::fCNexusMenuHelp (string *value, int nMappedVal, bool bForceShowMenu)
{
m_pMenu->Help(bForceShowMenu);
return true;
}
bool CNexusUserInterface::fCNexusMenuQuit (string *value, int nMappedVal)
{
cout<<"Goodbye!"<<endl;
return false;
}
bool CNexusUserInterface::fCNexusMenuAbout (string *value, int nMappedVal, bool bShowBuildTime)
{
cout<<endl;
cout<<"Morphy NUI Version: "<<morphy_nui_v_MAJOR_VERSION <<"."<<morphy_nui_v_MINOR_VERSION;
#ifdef BETA
cout << " beta";
#endif
cout<<endl;
cout<<"Copyright 2020 (C) Martin Brazeau and Chris Desjardins. All rights reserved."<<endl;
cout<<"This program uses the NCL by Paul O. Lewis."<<endl<<endl;
cout<<"For a list of commands enter the command \"help\""<<endl<<endl;
if (bShowBuildTime)
{
cout<<"Build time: "<<__DATE__<<" "<<__TIME__<<endl;
}
#ifdef DEBUG
cout<<endl;
cout<<endl;
cout<<"\t********************************************************"<<endl;
cout<<"\t* *"<<endl;
cout<<"\t* DEBUGGING BUILD *"<<endl;
cout<<"\t* *"<<endl;
cout<<"\t********************************************************"<<endl;
cout<<endl;
cout<<endl;
#endif
return true;
}
bool CNexusUserInterface::fCNexusMenuCommandLog (string *value, int nMappedVal)
{
string strFilename = *value;
if (!m_fCommandLog)
{
if (strFilename.length() == 0)
{
m_ioLogFiles->GetUserInput(" Enter log filename: " + m_strCwd, &strFilename, true);
}
strFilename = m_strCwd + strFilename;
m_fCommandLog.open(strFilename.c_str());
if (m_fCommandLog)
{
cout<<" Successfully opened '"<<strFilename<<"'"<<endl;
}
else
{
cout<<" Error: Unable to open '"<<strFilename<<"'"<<endl;
}
}
else
{
cout<<" Error: Log file "<<m_fCommandLog.GetFileName()<<" is already open"<<endl;
}
return true;
}
bool CNexusUserInterface::fCNexusMenuStatus (string *value, int nMappedVal)
{
string strNexusFile = "File not open";
if ((m_pNexusParse) && (m_pNexusParse->m_cNexusReader))
{
strNexusFile = m_pNexusParse->m_cNexusReader->GetInFileName();
}
cout<<endl;
cout<<"Nexus file: "<<strNexusFile<<endl;
cout<<"Command log file: "<<m_fCommandLog.GetFileName()<<endl;
cout<<"Working directory: "<<m_strCwd<<endl;
cout<<endl;
return true;
}
bool CNexusUserInterface::fCNexusMenuChdir (string *value, int nMappedVal)
{
string strCwd = *value;
struct stat st;
if (strCwd.length() == 0)
{
m_ioWorkingDir->GetUserInput(" Enter new working directory: ", &strCwd);
}
if (strCwd[strCwd.length() - 1] != '/')
{
strCwd += "/";
}
if ((stat(strCwd.c_str(), &st) == 0) && (S_ISDIR(st.st_mode)))
{
cout<<" Setting working directory to: "<<strCwd<<endl;
m_strCwd = strCwd;
}
else
{
cout<<" Error: Invalid directory '"<<strCwd<<"'"<<endl;
}
return true;
}
bool CNexusUserInterface::fCNexusMenuExclude (string *value, int nMappedVal)
{
cout<<"Not implemented"<<endl;
return true;
}
bool CNexusUserInterface::fCNexusMenuInclude (string *value, int nMappedVal)
{
cout<<"Not implemented"<<endl;
return true;
}
bool CNexusUserInterface::fCNexusMenuOutgroup (string *value, int nMappedVal)
{
cout<<"Not implemented"<<endl;
return true;
}
bool CNexusUserInterface::fCNexusMenuIngroup (string *value, int nMappedVal)
{
cout<<"Not implemented"<<endl;
return true;
}
/*
This is for when a user supplies a list, such as a list of taxa or indices
of characters or taxa.
*/
void CNexusUserInterface::ParseUserList (vector<string> &tokens, string *value)
{
stringstream input(*value);
string s;
while(getline(input, s, ' '))
{
tokens.push_back(s);
}
}
void CNexusUserInterface::SetCharType (const mpl_parsim_t ptype, string *value, int nMappedVal)
{
int i = 0;
vector<string> tokens;
char *p;
long index = 0;
ParseUserList(tokens, value);
for (i = 0; i < tokens.size(); ++i)
{
if (!all_of(tokens[i].begin(), tokens[i].end(), ::isdigit))
{
cout << "Unrecognised index: " << tokens[i] << endl;
continue;
}
index = strtol(tokens[i].c_str(), &p, 10);
if (mpl_set_parsim_t(index-1, ptype, m_mflHandle) < 0)
{
cout << "Index out of bounds: " << index << endl;
}
}
}
bool CNexusUserInterface::fCNexusMenuOrdered (string *value, int nMappedVal)
{
// cout << "Not implemented" << endl;
// return true;
SetCharType(MPL_WAGNER_T, value, nMappedVal);
return true;
}
bool CNexusUserInterface::fCNexusMenuUnordered (string *value, int nMappedVal)
{
SetCharType(MPL_FITCH_T, value, nMappedVal);
return true;
}
void CNexusUserInterface::PrintIslandData()
{
/* CJD FIXME:
mfl_add_sequence_t addseq_type = (mfl_add_sequence_t)(long int)mfl_get_parameter(m_mflHandle, MFL_PT_ADD_SEQUENCE_TYPE);
if (addseq_type == MFL_AST_RANDOM)
{
int count = mfl_get_resultant_data(m_mflHandle, MFL_RT_ISLAND_COUNT, 0);
int size;
int len;
cout<<left<<setw(10)<<"Island"<<setw(10)<<"Size"<<setw(10)<<"Length"<<endl;
for (int i = 0; i < count; i++)
{
size = mfl_get_resultant_data(m_mflHandle, MFL_RT_ISLAND_SIZE , i);
len = mfl_get_resultant_data(m_mflHandle, MFL_RT_ISLAND_LENGTH, i);
cout<<setw(10)<<i<<setw(10)<<size<<setw(10)<<len<<endl;
}
}
*/
}
void CNexusUserInterface::PrintHsearchData()
{
#if 0
CJD FIXME:
cout <<endl<<endl<<"Heuristic search completed" << endl;
//int rearr = mfl_get_resultant_data(m_mflHandle, MFL_RT_NUM_REARRANGMENTS, 0);
int savedtr = mfl_get_resultant_data(m_mflHandle, MFL_RT_NUM_SAVED_TREES, 0);
int bestlen = mfl_get_resultant_data(m_mflHandle, MFL_RT_SHORTEST_TREE_LEN, 0);
/* We don't want to report rearrangements until we can set user-defined
* seeds. Otherwise, the number may vary between machines.*/
//cout << " Number of rearrangements tried: "<<rearr<<endl;
cout << " Number of trees saved: "<<savedtr<<endl;
cout << " Length of shortest tree: "<<bestlen<<endl<<endl;
#endif
}
bool CNexusUserInterface::fCNexusMenuHeuristicSearch(string *value, int nMappedVal)
{
int err = 0;
if (m_mflHandle)
{
// TODO: Make sure this is straightened out
err = mpl_do_search(m_mflHandle);
if (err == 0)
{
PrintHsearchData();
PrintIslandData();
}
else
{
cout << "Unable to initiate search." << endl;
}
}
else
{
cout<<"No file open"<<endl;
}
return true;
}
bool CNexusUserInterface::fCNexusMenuScoreTree (string *value, int nMappedVal)
{
long i = 0;
long ntrees = 0;
double score = 0.0;
vector<string> tokens;
char *p;
long index = 0;
ParseUserList(tokens, value);
ntrees = mpl_get_num_trees(m_mflHandle);
if (ntrees == 0) {
cout << "No trees in memory." << endl;
return true;
}
if (value->length() == 0)
{
int res = 0;
for (i = 0; i < ntrees; ++i)
{
if ((res = mpl_score_tree(&score, i, m_mflHandle)))
{
cout << "Unable to report tree score. Error: " << res << endl;
}
else
{
cout << "Tree " << i+1 << ": "<< endl << score << endl << endl;
}
}
return true;
}
for (i = 0; i < tokens.size(); ++i)
{
if (!all_of(tokens[i].begin(), tokens[i].end(), ::isdigit))
{
cout << "Unrecognised index: " << tokens[i] << endl << endl;
continue;
}
index = strtol(tokens[i].c_str(), &p, 10);
if (mpl_score_tree(&score, index-1, m_mflHandle)< 0)
{
cout << "Index out of bounds: " << index << endl << endl;
}
else
{
cout << "Tree " << index << ": "<< endl << score << endl << endl;
}
}
return true;
}
bool CNexusUserInterface::fCNexusMenuExhaust (string *value, int nMappedVal)
{
// TODO: Exhaustive probably will never be implemented
cout<<"Not implemented"<<endl;
return true;
// if (m_mflHandle)
// {
// m_mflHandle->searchType(PAWM_SEARCH_EXHAUSTIVE);
// m_mflHandle->doSearch();
// }
// else
// {
// cout<<"No file open"<<endl;
// }
return true;
}
// TODO: Implement this one day
bool CNexusUserInterface::fCNexusMenuBNB (string *value, int nMappedVal)
{
cout<<"Not implemented"<<endl;
return true;
// if (m_mflHandle)
// {
// m_mflHandle->searchType(PAWM_SEARCH_EXHAUSTIVE);
// m_mflHandle->doSearch();
// }
// else
// {
// cout<<"No file open"<<endl;
// }
return true;
}
bool CNexusUserInterface::fCNexusMenuStepwise (string *value, int nMappedVal)
{
cout<<"Not implemented"<<endl;
return true;
// if (m_mflHandle)
// {
// m_mflHandle->searchType(PAWM_SEARCH_STEPWISE);