File: progress.cpp

package info (click to toggle)
muscle 3.70%2Bfix1-2
  • links: PTS, VCS
  • area: main
  • in suites: lenny, squeeze
  • size: 1,424 kB
  • ctags: 2,116
  • sloc: cpp: 26,897; xml: 230; makefile: 25
file content (186 lines) | stat: -rw-r--r-- 3,917 bytes parent folder | download
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
#include "muscle.h"
#include <stdio.h>
#include <time.h>

// Functions that provide visible feedback to the user
// that progress is being made.

static unsigned g_uIter = 0;		// Main MUSCLE iteration 1, 2..
static unsigned g_uLocalMaxIters = 0;	// Max iters
static FILE *g_fProgress = stderr;	// Default to standard error
static char g_strFileName[32];		// File name
static time_t g_tLocalStart;				// Start time
static char g_strDesc[32];			// Description
static bool g_bWipeDesc = false;
static int g_nPrevDescLength;
static unsigned g_uTotalSteps;

double GetCheckMemUseMB()
	{
	unsigned MB = (unsigned) GetMemUseMB();
	if (0 == g_uMaxMB || MB <= g_uMaxMB)
		return MB;
	fprintf(stderr, "\n\n*** MAX MEMORY %u MB EXCEEDED***\n", g_uMaxMB);
	fprintf(stderr, "Memory allocated so far %u MB, physical RAM %u MB\n",
	  MB, (unsigned) GetRAMSizeMB());
	fprintf(stderr, "Use -maxmb <n> option to increase limit, where <n> is in MB.\n");
	SaveCurrentAlignment();
	exit(EXIT_FatalError);
	return MB;
	}

const char *ElapsedTimeAsStr()
	{
	time_t Now = time(0);
	unsigned long ElapsedSecs = (unsigned long) (Now - g_tLocalStart);
	return SecsToStr(ElapsedSecs);
	}

const char *MemToStr(double MB)
	{
	if (MB < 0)
		return "";

	static char Str[9];
	static double MaxMB = 0;
	static double RAMMB = 0;

	if (RAMMB == 0)
		RAMMB = GetRAMSizeMB();

	if (MB > MaxMB)
		MaxMB = MB;
	double Pct = (MaxMB*100.0)/RAMMB;
	if (Pct > 100)
		Pct = 100;
	sprintf(Str, "%.0f MB(%.0f%%)", MaxMB, Pct);
	return Str;
	}

void SetInputFileName(const char *pstrFileName)
	{
	NameFromPath(pstrFileName, g_strFileName, sizeof(g_strFileName));
	}

void SetSeqStats(unsigned uSeqCount, unsigned uMaxL, unsigned uAvgL)
	{
	if (g_bQuiet)
		return;

	fprintf(g_fProgress, "%s %u seqs, max length %u, avg  length %u\n",
	  g_strFileName, uSeqCount, uMaxL, uAvgL);
	if (g_bVerbose)
		Log("%u seqs, max length %u, avg  length %u\n",
		  uSeqCount, uMaxL, uAvgL);
	}

void SetStartTime()
	{
	time(&g_tLocalStart);
	}

unsigned long GetStartTime()
	{
	return (unsigned long) g_tLocalStart;
	}

void SetIter(unsigned uIter)
	{
	g_uIter = uIter;
	}

void IncIter()
	{
	++g_uIter;
	}

void SetMaxIters(unsigned uMaxIters)
	{
	g_uLocalMaxIters = uMaxIters;
	}

void SetProgressDesc(const char szDesc[])
	{
	strncpy(g_strDesc, szDesc, sizeof(g_strDesc));
	g_strDesc[sizeof(g_strDesc) - 1] = 0;
	}

static void Wipe(int n)
	{
	for (int i = 0; i < n; ++i)
		fprintf(g_fProgress, " ");
	}

void Progress(const char *szFormat, ...)
	{
	CheckMaxTime();

	if (g_bQuiet)
		return;

	double MB = GetCheckMemUseMB();

	char szStr[4096];
	va_list ArgList;
	va_start(ArgList, szFormat);
	vsprintf(szStr, szFormat, ArgList);

	fprintf(g_fProgress, "%8.8s  %12s  %s",
	  ElapsedTimeAsStr(),
	  MemToStr(MB),
	  szStr);

	fprintf(g_fProgress, "\n");
	fflush(g_fProgress);
	}

void Progress(unsigned uStep, unsigned uTotalSteps)
	{
	CheckMaxTime();

	if (g_bQuiet)
		return;

	double dPct = ((uStep + 1)*100.0)/uTotalSteps;
	double MB = GetCheckMemUseMB();
	fprintf(g_fProgress, "%8.8s  %12s  Iter %3u  %6.2f%%  %s",
	  ElapsedTimeAsStr(),
	  MemToStr(MB),
	  g_uIter,
	  dPct,
	  g_strDesc);

	if (g_bWipeDesc)
		{
		int n = g_nPrevDescLength - (int) strlen(g_strDesc);
		Wipe(n);
		g_bWipeDesc = false;
		}

	fprintf(g_fProgress, "\r");

	g_uTotalSteps = uTotalSteps;
	}

void ProgressStepsDone()
	{
	CheckMaxTime();

	if (g_bVerbose)
		{
		double MB = GetCheckMemUseMB();
		Log("Elapsed time %8.8s  Peak memory use %12s  Iteration %3u %s\n",
		 ElapsedTimeAsStr(),
		 MemToStr(MB),
		 g_uIter,
		 g_strDesc);
		}

	if (g_bQuiet)
		return;

	Progress(g_uTotalSteps - 1, g_uTotalSteps);
	fprintf(g_fProgress, "\n");
	g_bWipeDesc = true;
	g_nPrevDescLength = (int) strlen(g_strDesc);
	}