1 | #include "muscle.h" |
---|
2 | #include <stdio.h> |
---|
3 | #include <time.h> |
---|
4 | |
---|
5 | // Functions that provide visible feedback to the user |
---|
6 | // that progress is being made. |
---|
7 | |
---|
8 | static unsigned g_uIter = 0; // Main MUSCLE iteration 1, 2.. |
---|
9 | static unsigned g_uLocalMaxIters = 0; // Max iters |
---|
10 | static FILE *g_fProgress = stderr; // Default to standard error |
---|
11 | static char g_strFileName[32]; // File name |
---|
12 | static time_t g_tLocalStart; // Start time |
---|
13 | static char g_strDesc[32]; // Description |
---|
14 | static bool g_bWipeDesc = false; |
---|
15 | static int g_nPrevDescLength; |
---|
16 | static unsigned g_uTotalSteps; |
---|
17 | |
---|
18 | const char *ElapsedTimeAsStr() |
---|
19 | { |
---|
20 | time_t Now = time(0); |
---|
21 | unsigned long ElapsedSecs = (unsigned long) (Now - g_tLocalStart); |
---|
22 | return SecsToStr(ElapsedSecs); |
---|
23 | } |
---|
24 | |
---|
25 | const char *MemToStr(double MB) |
---|
26 | { |
---|
27 | if (MB < 0) |
---|
28 | return ""; |
---|
29 | |
---|
30 | static char Str[16]; |
---|
31 | static double MaxMB = 0; |
---|
32 | static double RAMMB = 0; |
---|
33 | |
---|
34 | if (RAMMB == 0) |
---|
35 | RAMMB = GetRAMSizeMB(); |
---|
36 | |
---|
37 | if (MB > MaxMB) |
---|
38 | MaxMB = MB; |
---|
39 | double Pct = (MaxMB*100.0)/RAMMB; |
---|
40 | if (Pct > 100) |
---|
41 | Pct = 100; |
---|
42 | sprintf(Str, "%.0f MB(%.0f%%)", MaxMB, Pct); |
---|
43 | return Str; |
---|
44 | } |
---|
45 | |
---|
46 | void SetInputFileName(const char *pstrFileName) |
---|
47 | { |
---|
48 | NameFromPath(pstrFileName, g_strFileName, sizeof(g_strFileName)); |
---|
49 | } |
---|
50 | |
---|
51 | void SetSeqStats(unsigned uSeqCount, unsigned uMaxL, unsigned uAvgL) |
---|
52 | { |
---|
53 | if (g_bQuiet) |
---|
54 | return; |
---|
55 | |
---|
56 | fprintf(g_fProgress, "%s %u seqs, max length %u, avg length %u\n", |
---|
57 | g_strFileName, uSeqCount, uMaxL, uAvgL); |
---|
58 | if (g_bVerbose) |
---|
59 | Log("%u seqs, max length %u, avg length %u\n", |
---|
60 | uSeqCount, uMaxL, uAvgL); |
---|
61 | } |
---|
62 | |
---|
63 | void SetStartTime() |
---|
64 | { |
---|
65 | time(&g_tLocalStart); |
---|
66 | } |
---|
67 | |
---|
68 | unsigned long GetStartTime() |
---|
69 | { |
---|
70 | return (unsigned long) g_tLocalStart; |
---|
71 | } |
---|
72 | |
---|
73 | void SetIter(unsigned uIter) |
---|
74 | { |
---|
75 | g_uIter = uIter; |
---|
76 | } |
---|
77 | |
---|
78 | void IncIter() |
---|
79 | { |
---|
80 | ++g_uIter; |
---|
81 | } |
---|
82 | |
---|
83 | void SetMaxIters(unsigned uMaxIters) |
---|
84 | { |
---|
85 | g_uLocalMaxIters = uMaxIters; |
---|
86 | } |
---|
87 | |
---|
88 | void SetProgressDesc(const char szDesc[]) |
---|
89 | { |
---|
90 | strncpy(g_strDesc, szDesc, sizeof(g_strDesc)); |
---|
91 | g_strDesc[sizeof(g_strDesc) - 1] = 0; |
---|
92 | } |
---|
93 | |
---|
94 | static void Wipe(int n) |
---|
95 | { |
---|
96 | for (int i = 0; i < n; ++i) |
---|
97 | fprintf(g_fProgress, " "); |
---|
98 | } |
---|
99 | |
---|
100 | void Progress(const char *szFormat, ...) |
---|
101 | { |
---|
102 | CheckMaxTime(); |
---|
103 | |
---|
104 | if (g_bQuiet) |
---|
105 | return; |
---|
106 | |
---|
107 | double MB = GetMemUseMB(); |
---|
108 | |
---|
109 | char szStr[4096]; |
---|
110 | va_list ArgList; |
---|
111 | va_start(ArgList, szFormat); |
---|
112 | vsprintf(szStr, szFormat, ArgList); |
---|
113 | |
---|
114 | fprintf(g_fProgress, "%8.8s %12s %s", |
---|
115 | ElapsedTimeAsStr(), |
---|
116 | MemToStr(MB), |
---|
117 | szStr); |
---|
118 | |
---|
119 | fprintf(g_fProgress, "\n"); |
---|
120 | fflush(g_fProgress); |
---|
121 | } |
---|
122 | |
---|
123 | void Progress(unsigned uStep, unsigned uTotalSteps) |
---|
124 | { |
---|
125 | CheckMaxTime(); |
---|
126 | |
---|
127 | if (g_bQuiet) |
---|
128 | return; |
---|
129 | |
---|
130 | double dPct = ((uStep + 1)*100.0)/uTotalSteps; |
---|
131 | double MB = GetMemUseMB(); |
---|
132 | fprintf(g_fProgress, "%8.8s %12s Iter %3u %6.2f%% %s", |
---|
133 | ElapsedTimeAsStr(), |
---|
134 | MemToStr(MB), |
---|
135 | g_uIter, |
---|
136 | dPct, |
---|
137 | g_strDesc); |
---|
138 | |
---|
139 | if (g_bWipeDesc) |
---|
140 | { |
---|
141 | int n = g_nPrevDescLength - (int) strlen(g_strDesc); |
---|
142 | Wipe(n); |
---|
143 | g_bWipeDesc = false; |
---|
144 | } |
---|
145 | |
---|
146 | fprintf(g_fProgress, "\r"); |
---|
147 | |
---|
148 | g_uTotalSteps = uTotalSteps; |
---|
149 | } |
---|
150 | |
---|
151 | void ProgressStepsDone() |
---|
152 | { |
---|
153 | CheckMaxTime(); |
---|
154 | |
---|
155 | if (g_bVerbose) |
---|
156 | { |
---|
157 | double MB = GetMemUseMB(); |
---|
158 | Log("Elapsed time %8.8s Peak memory use %12s Iteration %3u %s\n", |
---|
159 | ElapsedTimeAsStr(), |
---|
160 | MemToStr(MB), |
---|
161 | g_uIter, |
---|
162 | g_strDesc); |
---|
163 | } |
---|
164 | |
---|
165 | if (g_bQuiet) |
---|
166 | return; |
---|
167 | |
---|
168 | Progress(g_uTotalSteps - 1, g_uTotalSteps); |
---|
169 | fprintf(g_fProgress, "\n"); |
---|
170 | g_bWipeDesc = true; |
---|
171 | g_nPrevDescLength = (int) strlen(g_strDesc); |
---|
172 | } |
---|