~ubuntu-branches/ubuntu/saucy/libbpp-core/saucy

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
//
// File: ApplicationTools.cpp
// Created by: Julien Dutheil
// Created on: Fri Oct 21 16:19 2005
// From old file created on: Sun Dec 14 09:36:26 2003
//

/*
Copyright or © or Copr. CNRS, (November 16, 2004)

This software is a computer program whose purpose is to provide basal and 
utilitary classes. This file belongs to the Bio++ Project.

This software is governed by the CeCILL  license under French law and
abiding by the rules of distribution of free software.  You can  use, 
modify and/ or redistribute the software under the terms of the CeCILL
license as circulated by CEA, CNRS and INRIA at the following URL
"http://www.cecill.info". 

As a counterpart to the access to the source code and  rights to copy,
modify and redistribute granted by the license, users are provided only
with a limited warranty  and the software's author,  the holder of the
economic rights,  and the successive licensors  have only  limited
liability. 

In this respect, the user's attention is drawn to the risks associated
with loading,  using,  modifying and/or developing or reproducing the
software by the user in light of its specific status of free software,
that may mean  that it is complicated to manipulate,  and  that  also
therefore means  that it is reserved for developers  and  experienced
professionals having in-depth computer knowledge. Users are therefore
encouraged to load and test the software's suitability as regards their
requirements in conditions enabling the security of their systems and/or 
data to be ensured and,  more generally, to use and operate it in the 
same conditions as regards security. 

The fact that you are presently reading this means that you have had
knowledge of the CeCILL license and that you accept its terms.
*/

#include "ApplicationTools.h"

using namespace bpp;

// From the STL:
#include <cmath>

using namespace std;

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

OutputStream* ApplicationTools::error   = new StdErr();
OutputStream* ApplicationTools::message = new StdOut();
OutputStream* ApplicationTools::warning = new StdOut();
time_t ApplicationTools::startTime;
unsigned int ApplicationTools::terminalWidth = 80;
float ApplicationTools::terminalSplit = 0.5;
bool ApplicationTools::interactive = true;

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

bool ApplicationTools::parameterExists(
  const std::string & parameterName,
  std::map<std::string, std::string>& params)
{
  return params.find(parameterName) != params.end() && !TextTools::isEmpty(params[parameterName]);
}
  
/******************************************************************************/

std::string ApplicationTools::getAFilePath(
  const std::string& parameter,
  std::map<std::string, std::string>& params,
  bool isRequired,
  bool mustExist,
  const std::string& suffix,
  bool suffixIsOptional) throw (Exception)
{
  string filePath = getStringParameter(parameter, params, "none", suffix, suffixIsOptional, false);
  if (filePath == "") filePath = "none";
  if (filePath == "none" && isRequired)
  {
    throw Exception("You must specify a file for this parameter: " + parameter + (suffixIsOptional ? "" : suffix));
  }
  if(filePath == "none") return filePath;
  if(mustExist && !FileTools::fileExists(filePath))
  {
    throw Exception("File does not exists: " + filePath);
  }
  return filePath;
}

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

double ApplicationTools::getDoubleParameter(
  const std::string & parameterName,
  std::map<std::string, std::string> & params,
  double defaultValue,
  const std::string & suffix,
  bool suffixIsOptional,
  bool warn)
{
  double dParam = defaultValue;
  if (parameterExists(parameterName + suffix, params))
  {
    dParam = TextTools::toDouble(params[parameterName + suffix]);
  }
  else if (suffixIsOptional && parameterExists(parameterName, params))
  {
    dParam = TextTools::toDouble(params[parameterName]);
  }
  else if(warn)
  {
    displayWarning("Parameter " + parameterName + suffix + " not specified. Default used instead: " + TextTools::toString(defaultValue));
  }
  return dParam;
}

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

int ApplicationTools::getIntParameter(
  const std::string & parameterName,
  std::map<std::string, std::string> & params,
  int defaultValue,
  const std::string & suffix,
  bool suffixIsOptional,
  bool warn)
{
  int iParam = defaultValue;
  if (parameterExists(parameterName + suffix, params)) {
    iParam = TextTools::toInt(params[parameterName + suffix]);
  } else if(suffixIsOptional && parameterExists(parameterName, params)) {
    iParam = TextTools::toInt(params[parameterName]);
  } else if (warn) {
    displayWarning("Parameter " + parameterName + suffix + " not specified. Default used instead: " + TextTools::toString(defaultValue));
  }
  return iParam;
}

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

std::string ApplicationTools::getStringParameter(
  const std::string& parameterName,
  std::map<std::string, std::string>& params,
  const std::string& defaultValue,
  const std::string& suffix,
  bool suffixIsOptional,
  bool warn)
{
  string sParam = defaultValue;
  if (parameterExists(parameterName + suffix, params)) {
    sParam = params[parameterName + suffix];
  } else if (suffixIsOptional && parameterExists(parameterName, params)) {
    sParam = params[parameterName];
  } else if (warn) {
    displayWarning("Parameter " + parameterName + " not specified. Default used instead: " + defaultValue);
  }
  return sParam;
}

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

bool ApplicationTools::getBooleanParameter(
  const std::string & parameterName,
  std::map<std::string, std::string> & params,
  bool defaultValue,
  const std::string & suffix,
  bool suffixIsOptional,
  bool warn)
{
  bool bParam = defaultValue;
  if(parameterExists(parameterName + suffix, params))
  {
    string sParam = params[parameterName + suffix];
    bParam = (sParam == "true") 
        || (sParam == "TRUE")
        || (sParam == "t")
        || (sParam == "T")
        || (sParam == "yes")
        || (sParam == "YES")
        || (sParam == "y")
        || (sParam == "Y")
        || (sParam == "1");
  }
  else if (suffixIsOptional && parameterExists(parameterName, params)) {
    string sParam = params[parameterName];
    bParam = (sParam == "true") 
        || (sParam == "TRUE")
        || (sParam == "t")
        || (sParam == "T")
        || (sParam == "yes")
        || (sParam == "YES")
        || (sParam == "y")
        || (sParam == "Y")
          || (sParam == "1");
  }
  else if (warn)
  {
    displayWarning("Parameter " + parameterName + " not specified. Default used instead: " + TextTools::toString(defaultValue));
  }
  return bParam;
}

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

void ApplicationTools::displayMessage(const std::string& text) { if(message) (*message << text).endLine(); }
    
void ApplicationTools::displayError(const std::string& text) { if(error) (*error << "ERROR!!! " << text).endLine(); }
    
void ApplicationTools::displayWarning(const std::string& text) { if(warning) (*warning << "WARNING!!! " << text).endLine(); }

void ApplicationTools::displayTask(const std::string& text, bool eof)
{
  if (message)
  {
    *message << TextTools::resizeRight(text, (unsigned int) (terminalWidth * terminalSplit - 1), '.') << ": ";
    if(eof) message->endLine();
    else    message->flush();
  }
}
    
void ApplicationTools::displayTaskDone() { if(message) (*message << "Done.").endLine(); }

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

void ApplicationTools::displayGauge(size_t iter, size_t total, char symbol, const std::string& mes)
{
  if (!message) return;
  if (total == 0) return;//We do not display anything in that case.
  size_t width = static_cast<size_t>(terminalWidth * terminalSplit - 2);
  string gauge = string(static_cast<unsigned int>((1. * iter / total) * width), symbol);
  string info = mes;
  if (interactive)
  {
    string fill = string(width - gauge.length(), ' ');
    gauge = "[" + gauge + fill + "] " + TextTools::resizeLeft(TextTools::toString(100 * iter / total), 3) + "%";
    if (mes.size() > terminalWidth - gauge.size())
      info = TextTools::resizeRight(mes, terminalWidth - gauge.size());
    *message << '\r' + info + gauge;
    message->flush();
  }
  else
  {
    if (iter == 0)
    {
      *message << "[";
      message->flush();
      return;
    }
    size_t step = static_cast<size_t>(ceil(1. * total / width));
    if (iter >= total)
    {
      size_t fill = static_cast<size_t>(terminalWidth * terminalSplit) - (total - 1) / step - 1;
      *message << TextTools::resizeLeft("]", fill, symbol);
      message->flush();
      return;
    }
    size_t x = iter % step;
    if (x == 0) { *message << symbol; message->flush(); }
  }
}

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

void ApplicationTools::displayUnlimitedGauge(size_t iter, const std::string& mes)
{
  if (!message) return;
  string chars = "-/-\\";
  string info = mes;
  if (interactive)
  {
    unsigned int i = iter % 4;
    *message << '\r' << info << chars[i] << " " << TextTools::toString(iter);
    message->flush();
  }
  else
  {
    if (iter == 0)
      *message << info;
    *message << "*";
    message->flush();
    return;
  }
}

/******************************************************************************/
  
void ApplicationTools::displayTime(const std::string& msg)
{
  time_t endTime;
  time(&endTime);
  if (message)
  {
    double nsec = difftime(endTime, startTime);
    double nmin = floor(nsec/60.);
    double nhou = floor(nmin/60.);
    double nday = floor(nhou/24.);
    nhou = nhou - nday * 24;
    nmin = nmin - (nday * 24 + nhou) * 60;
    nsec = nsec - ((nday * 24 + nhou) * 60 + nmin) * 60;
    *message << msg << " ";
    *message << nday << "d, ";
    *message << nhou << "h, ";
    *message << nmin << "m, ";
    *message << nsec << "s.";
    message->endLine();
  }
}

/******************************************************************************/
  
double ApplicationTools::getTime()
{
  time_t endTime;
  time(&endTime);
  return difftime(endTime, startTime);
}

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