File: NCLInterface.h

package info (click to toggle)
libncl 2.1.18%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 8,344 kB
  • ctags: 3,526
  • sloc: xml: 43,716; cpp: 30,400; sh: 10,658; python: 231; makefile: 190
file content (304 lines) | stat: -rw-r--r-- 11,751 bytes parent folder | download | duplicates (5)
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
//  Copyright (C) 2007-2008 Brian O'Meara & Derrick Zwickl
//  A modification of the BasicCommandLine file of the NCL (see below)
//  to use for loading trees and data from Nexus into R. Licensing as below.

//	Copyright (C) 1999-2002 Paul O. Lewis
//
//	This file is part of NCL (Nexus Class Library).
//
//	NCL is free software; you can redistribute it and/or modify
//	it under the terms of the GNU General Public License as published by
//	the Free Software Foundation; either version 2 of the License, or
//	(at your option) any later version.
//
//	NCL is distributed in the hope that it will be useful,
//	but WITHOUT ANY WARRANTY; without even the implied warranty of
//	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//	GNU General Public License for more details.
//
//	You should have received a copy of the GNU General Public License
//	along with NCL; if not, write to the Free Software Foundation, Inc.,
//	59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//

#ifndef NCL_INTERFACE_H
#define NCL_INTERFACE_H


#define NEW_NCL_INTERFACE

std::string GetRStringLiteral(const std::string & inp);

#if defined(NEW_NCL_INTERFACE)

//  Copyright (C) 2007-2008 Brian O'Meara & Derrick Zwickl
//	Copyright (C) 2007-2008 Mark T. Holder
//
//	This file is part of NCL (Nexus Class Library).
//
//	NCL is free software; you can redistribute it and/or modify
//	it under the terms of the GNU General Public License as published by
//	the Free Software Foundation; either version 2 of the License, or
//	(at your option) any later version.
//
//	NCL is distributed in the hope that it will be useful,
//	but WITHOUT ANY WARRANTY; without even the implied warranty of
//	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//	GNU General Public License for more details.
//
//	You should have received a copy of the GNU General Public License
//	along with NCL; if not, write to the Free Software Foundation, Inc.,
//	59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//

/*******************************************************************************
 * This file merges content NEXUSnormalizer and phylobase NCLInterface.{h,cpp}
 by  Brian O'Meara & Derrick Zwickl


 */
#include "ncl/ncl.h"
#include "ncl/nxsblock.h"
#include "ncl/nxspublicblocks.h"
#include "ncl/nxsmultiformat.h"
#include <cassert>

using namespace std;
class BASICCMDLINE {
		enum MessageLevel {
			DEBUG_MESSAGE_LEVEL = 0,
			STATUS_MESSAGE_LEVEL = 1,
			REPORT_MESSAGE_LEVEL = 2,
			WARNING_MESSAGE_LEVEL = 3,
			ERROR_MESSAGE_LEVEL = 4
		};
	public:
		BASICCMDLINE()
			: nexusReader(0L),
			verboseLevel(REPORT_MESSAGE_LEVEL),
			fmt(MultiFormatReader::NEXUS_FORMAT),
			gStrictLevel(2),
			warningLevel(NxsReader::SUPPRESS_WARNINGS_LEVEL)
			{}
		~BASICCMDLINE(){
			Clear();
		}

		// verbosity level here refers to facets of the NxsReader::NxsWarnLevel enum (not the MessageLevel above)
		void Initialize(char *infile_name, int verbosityLevel=8) {
			if (verbosityLevel >= NxsReader::SUPPRESS_WARNINGS_LEVEL) {
				this->warningLevel = NxsReader::SUPPRESS_WARNINGS_LEVEL;
				this->verboseLevel = WARNING_MESSAGE_LEVEL;
			}
			else if (verbosityLevel >= 0) {
				this->warningLevel = NxsReader::NxsWarnLevel(verboseLevel);
			}
			processFilepath(infile_name);
		}
		void Clear();

		void RReturnCharacters(NxsString & nexuscharacters, bool allchar, bool polymorphictomissing, bool levelsall);
		void RReturnTrees(NxsString & nexustrees);
		void RReturnDistances(NxsString & nexusdistances);

		std::string TestRunning();


	private:
		NxsString			RemoveUnderscoresAndSpaces(NxsString input) const;

		void AppendRContent(
				  NxsString & nexuscharacters,
				  NxsCharactersBlock &characters,
				  bool allchar,
				  bool polymorphictomissing,
				  bool levelsall) const;
		void AppendRContent(NxsString & nexuscharacters,  NxsTreesBlock &treesB) const;

		void AppendRContent(NxsString & nexuscharacters, NxsDistancesBlock &distancesB) const;

		void processFilepath(const char * filename);
		void statusMessage(const std::string & m)  const{
			this->writeMessage(STATUS_MESSAGE_LEVEL, m);
		}
		void errorMessage(const std::string & m)  const{
			this->writeMessage(ERROR_MESSAGE_LEVEL, m);
		}
		void writeMessage(MessageLevel level, const std::string & m) const;

		MultiFormatReader * nexusReader;
		int verboseLevel;
		MultiFormatReader::DataFormatType fmt;
		long gStrictLevel;
		NxsReader::NxsWarnLevel warningLevel; // passed to NxsReader to discriminate between different levels of NexusWarning
};

#else // NEW_NCL_INTERFACE





#define COMMAND_MAXLEN  255

/*!
	BASICCMDLINE provides a template for creating a program that reads NEXUS data files and provides a basic command
	line. After compiling BASICCMDLINE, you will already have a program that understands the following commands, either
	typed in at the console or provided in a BASICCMDLINE block in a NEXUS data file (exception is the execute command,
	which can only be entered at the console). Keywords in the descriptions below are given in uppercase, however the
	commands themselves are case-insensitive. Lower-case indicates a parameter supplied by the user (e.g., "filename"
	would be replaced by the actual name of the file). Square brackets indicate optional keywords or subcommands.
>
	EXECUTE filename;

	LOG [options];

	  Option         Action
	  ------------------------------------------------------
	  FILE=filename  specifies name of log file to start
	  START          indicates logging is to be started
	  STOP           indicates logging is to be stopped
	  APPEND         append to log file if it already exists
	  REPLACE        replace log file without asking

	QUIT;
>
	See the Read function for details and to add other commands.

	To change the name of the program (which is also the prompt name and the name of the program's private NEXUS
	block), replace all occurrences of BASICCMDLINE with the name of your program (also search for the string
	"basiccmdline" and replace with an appropriate string at each occurrence).

	This class handles reading and storage for the NxsReader block BASICCMDLINE. It also serves as the main class for
	the program BASICCMDLINE, acting as both a NxsReader object (in order to be capable of parsing data files) as well
	as a NxsBlock object (in order to be able to process commands in a BASICCMDLINE block).

	Acting as a NxsBlock, it overrides the member functions Read and Reset, which are virtual functions in the base
	class NxsBlock. Acting as a NxsReader object, it overrides the member functions EnteringBlock, SkippingBlock, and
	NexusError.

	Adding a new data member? Don't forget to:
~
	o Describe it in the class header comment at the top of "basiccmdline.h"
	o Initialize it (unless it is self-initializing) in the constructor and reinitialize it in the Reset function
	o Describe the initial state in the constructor documentation
	o Delete memory allocated to it in both the destructor and Reset function
	o Report it in some way in the Report function
~
*/
class BASICCMDLINE
  : public NxsBlock,
  public NxsReader
	{
	public:

		enum UserQueryEnum		/* enumeration used with UserQuery member function to specify which choices to provide the user */
			{
			uq_cancel = 0x01,	/* provide opportunity to cancel */
			uq_ok	  = 0x02,	/* provide opportunity to answer ok */
			uq_yes	  = 0x04,	/* provide opportunity to answer yes */
			uq_no 	  = 0x08	/* provide opportunity to answer no */
			};

							BASICCMDLINE();
		virtual				~BASICCMDLINE();

		bool				EnteringBlock(NxsString blockName);
		void				ExitingBlock(NxsString blockName);
		void				ExecuteStarting();
		void				ExecuteStopping();
		void				OutputComment(const NxsString &msg);
		void				HandleNextCommand();
		void				NexusError(NxsString msg, file_pos pos, long line, long col);
		void				PreprocessNextCommand();
		void				PrintMessage(bool linefeed = true);
		virtual void		Report(ostream &out);
		void				Run(char *infile_name);
		void				Initialize(char *infile_name); //@@should be const char * !!! it is not being modified, but changing will break phylobase's ReadWithNCL
		void				SkippingBlock(NxsString blockName);
		void				SkippingCommand(NxsString commandName);
		void				SkippingDisabledBlock(NxsString blockName);
		virtual bool		UserQuery(NxsString mb_message, NxsString mb_title, BASICCMDLINE::UserQueryEnum mb_choices = BASICCMDLINE::uq_ok);
		void				HandleReturnData(NxsToken& token);
		NxsString			ReturnDataForR(bool allchar, bool polymorphictomissing, bool levelsall);
		NxsString			RemoveUnderscoresAndSpaces(NxsString input);
		void				RReturnCharacters(NxsString & nexuscharacters, bool allchar, bool polymorphictomissing, bool levelsall);
		void				RReturnTrees(NxsString & nexustrees);
		void				RReturnDistances(NxsString & nexusdistances);
		string				TestRunning();

		void 				ReadFilePath(const char * fn);
	protected:

		bool				inf_open;			/* true if `inf' is currently open */
		bool				logf_open;			/* true if `logf' is currently open */
		bool				quit_now;			/* set to false at beginning of Run and turns true only when QUIT command processed */
		ofstream			logf;				/* the log file output stream */
		NxsString			message;			/* workspace for composing output strings */
		NxsTreesBlock		*trees;				/* pointer to NxsTreesBlock object */
		NxsTaxaBlock		*taxa;				/* pointer to NxsTaxaBlock object */
		NxsAssumptionsBlock	*assumptions;		/* pointer to NxsAssumptionsBlock object */
		NxsDistancesBlock	*distances;			/* pointer to NxsDistancesBlock object */
		NxsCharactersBlock	*characters;		/* pointer to NxsCharactersBlock object */
		NxsDataBlock		*data;				/* pointer to NxsDataBlock object */
		char				*next_command;		/* workspace for processing next command entered interactively by user */
		unsigned			CharLabelToNumber(NxsString s);
		bool				FileExists(const char* fn);
		NxsString			GetFileName(NxsToken& token);
		void				FactoryDefaults();
		void				HandleEndblock(NxsToken& token);
		void				HandleShow(NxsToken& token);
		void				HandleHelp(NxsToken& token);
		void				HandleLog(NxsToken& token);
		void				HandleExecute(NxsToken& token);
		void				PurgeBlocks();
		virtual void		Read(NxsToken& token);
		virtual void		Reset();
		unsigned			TaxonLabelToNumber(NxsString s);
	};

/*!
	The MyNexusToken class provides a NxsToken-derived object that can display output comments as it encounters them.
	The virtual function NxsToken::OutputComment is overridden in this class for this purpose.
*/
class MyNexusToken
  : public NxsToken
	{
	public:
				MyNexusToken(istream &i);

		void	OutputComment(const NxsString &msg);
	};

/*!
	Will be called by NxsReader::Execute after the initial "#NEXUS" keyword is found in a NEXUS file but before other
	tokens are read. Add code here if you need to do any initializations prior to encountering any NEXUS blocks in a
	NEXUS data file.
*/
inline void BASICCMDLINE::ExecuteStarting()
	{
	}

/*!
	Will be called by NxsReader::Execute just before it exits after reading to the end of a NEXUS data file (or until
	encountering a LEAVE command between NEXUS blocks. Add code here if you need to clean up any memory allocated in
	ExecuteStarting.
*/
inline void BASICCMDLINE::ExecuteStopping()
	{
	}

/*!
	Called if an "output comment" is encountered in a NEXUS data file. An output comment is a comment [text enclosed in
	square brackets] that begins with an exclamation point. [!This is an example of a NEXUS output comment]. Output
	comments are supposed to be displayed when encountered. Modify this function's body to display output comments,
	which are made available as they are encountered via the `msg' argument.
*/
inline void	BASICCMDLINE::OutputComment(const NxsString &)
	{
	}

#endif //NEW_NCL_INTERFACE

#endif