-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathratio_checker_gui.c
498 lines (458 loc) · 17.6 KB
/
ratio_checker_gui.c
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
// gcc -mwindows -static -m32 ratio_checker_gui.c resource.res -o "Ratio Checker.exe" -l gdi32 -l comdlg32
/**
* Ratio Checker is a Windows-based GUI application designed to analyze lipid profile data
* and calculate specific ratios relevant to lipid composition. The program reads a text file
* containing chromatographic data of various fatty acid components and performs calculations
* to derive five distinct ratios:
*
* 1. C16:0 to C12:0 ratio
* 2. C18:0 to C12:0 ratio
* 3. C18:1 to C14:0 ratio
* 4. C18:2 to C14:0 ratio
* 5. Combined C18:1 and C18:2 to the sum of C12:0, C14:0, C16:0, and C18:0 ratio
*
* Functionality:
*
* - Upon opening a data file (text file format), the program extracts relevant chromatographic
* areas for specific fatty acid components such as C12:0, C14:0, C16:0, C18:0, C18:1 trans/cis,
* and C18:2 trans/cis.
*
* - Calculations are performed to derive the aforementioned ratios, offering insights into the
* composition and proportions of these fatty acids within the sample.
*
* - Each ratio is evaluated against predefined ranges, determining whether the derived values
* fall within acceptable bounds or not. The result is displayed as "Pass" or "Not pass" for
* each ratio along with a comment.
*
* Additional Features:
*
* - Drag-and-drop functionality: Accepts data files dropped onto the application window.
*
* - Menu options: Provides the ability to open data files, access an about dialog displaying
* version information, and access local help documentation.
*
* - Keyboard shortcuts: Supports keyboard shortcuts such as Ctrl+O for opening files and F1 for
* accessing local help documentation.
*
* - GUI elements: Displays sample name, individual fatty acid areas, calculated ratios, and
* evaluation results in a user-friendly graphical interface.
*
* The program allows researchers, particularly in lipid analysis, to quickly assess lipid profile
* data and evaluate the proportions of specific fatty acid components within samples based on
* predefined acceptable ratio ranges.
*/
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include <commdlg.h>
#include <shellapi.h>
#include <tchar.h>
#define VERSION "1.2.1"
#define MAX_LINE_LENGTH 512
#define IDI_ICON1 101
#define PRECISION 2
#define IDM_OPEN_BUTTON 101
#define IDM_ONLINE_HELP 102
#define IDM_ABOUT 103
#define IDM_EXIT 999
#define MAX_SAMPLE_NAME_LENGTH 32
#define MAX_NUM_LENGTH 32
HWND sample_name_hwnd;
char sample_name[MAX_SAMPLE_NAME_LENGTH] = "";
double c120 = 0.0, c140 = 0.0, c160 = 0.0, c180 = 0.0, c181 = 0.0, c182 = 0.0;
HWND c120_area, c140_area, c160_area, c180_area, c181_area, c182_area;
double ratio1 = 0.0, ratio2 = 0.0, ratio3 = 0.0, ratio4 = 0.0, ratio5 = 0.0;
HWND ratio1_result, ratio2_result, ratio3_result, ratio4_result, ratio5_result;
char ratio1_pass[16], ratio2_pass[16], ratio3_pass[16], ratio4_pass[16],
ratio5_pass[16];
HWND ratio1_comment, ratio2_comment, ratio3_comment, ratio4_comment,
ratio5_comment;
char buffer[MAX_NUM_LENGTH];
void calculateRatios()
{
ratio1 = (c120 != 0) ? c160 / c120 : 0;
ratio2 = (c120 != 0) ? c180 / c120 : 0;
ratio3 = (c140 != 0) ? c181 / c140 : 0;
ratio4 = (c140 != 0) ? c182 / c140 : 0;
ratio5 = (c120 + c140 + c160 + c180 != 0) ? (c181 + c182) / (
c120 + c140 + c160 + c180) : 0;
}
int findAreaColumn(const char *line)
{
const char *delim = "\t";
char *token = strtok((char *)line, delim);
int column = 0;
while (token != NULL)
{
if (strcmp(token, "Area") == 0)
return column;
token = strtok(NULL, delim);
column++;
}
return -1;
}
double extractArea(const char *line, int areaColumn)
{
const char *delim = "\t";
char *token = strtok((char *)line, delim);
int column = 0;
double area = 0.0;
while (token != NULL)
{
if (column == areaColumn)
{
area = atof(token);
break;
}
token = strtok(NULL, delim);
column++;
}
return area;
}
void extractName(const char *line)
{
const char *delim = "\t";
char *token = strtok((char *)line, delim);
while (token != NULL)
{
if (strcmp(token, "Sample Name") == 0)
{
token = strtok(NULL, delim);
if (token != NULL)
strncpy(sample_name, token, MAX_SAMPLE_NAME_LENGTH - 1);
}
token = strtok(NULL, delim);
}
}
void readFile(char *fileAdress)
{
c120 = c140 = c160 = c180 = c181 = c182 = 0;
FILE *file = fopen(fileAdress, "r");
char line[MAX_LINE_LENGTH];
sample_name[0] = '\0';
int areaColumn = -1;
while (fgets(line, MAX_LINE_LENGTH, file))
{
extractName(line);
if (strlen(sample_name) != 0)
break;
}
while (fgets(line, MAX_LINE_LENGTH, file))
{
areaColumn = findAreaColumn(line);
if (areaColumn != -1)
break;
}
while (fgets(line, MAX_LINE_LENGTH, file))
{
if (strstr(line, "C12:0"))
c120 = extractArea(line, areaColumn);
else if (strstr(line, "C14:0"))
c140 = extractArea(line, areaColumn);
else if (strstr(line, "C16:0"))
c160 = extractArea(line, areaColumn);
else if (strstr(line, "C18:0"))
c180 = extractArea(line, areaColumn);
else if (strstr(line, "C18:1 trans"))
c181 = extractArea(line, areaColumn);
else if (strstr(line, "C18:1 cis"))
c181 += extractArea(line, areaColumn);
else if (strstr(line, "C18:2 trans"))
c182 = extractArea(line, areaColumn);
else if (strstr(line, "C18:2 cis"))
{
c182 += extractArea(line, areaColumn);
break;
}
}
fclose(file);
}
char *doubleToChar(double value)
{
double roundedValue = round(value * pow(10, PRECISION)) / pow(
10, PRECISION);
_snprintf(buffer, sizeof(buffer) / sizeof(buffer[0]), "%.*f",
PRECISION, roundedValue);
return buffer;
}
void setResults()
{
calculateRatios();
SetWindowText(sample_name_hwnd, sample_name);
SetWindowText(c120_area, doubleToChar(c120));
SetWindowText(c140_area, doubleToChar(c140));
SetWindowText(c160_area, doubleToChar(c160));
SetWindowText(c180_area, doubleToChar(c180));
SetWindowText(c181_area, doubleToChar(c181));
SetWindowText(c182_area, doubleToChar(c182));
SetWindowText(ratio1_result, doubleToChar(ratio1));
SetWindowText(ratio2_result, doubleToChar(ratio2));
SetWindowText(ratio3_result, doubleToChar(ratio3));
SetWindowText(ratio4_result, doubleToChar(ratio4));
SetWindowText(ratio5_result, doubleToChar(ratio5));
strcpy(ratio1_pass,
(ratio1 >= 5.8 && ratio1 <= 14.5) ? "Pass" : "Not pass");
strcpy(ratio2_pass, (ratio2 >= 1.9 && ratio2 <= 5.9) ? "Pass" : "Not pass");
strcpy(ratio3_pass, (ratio3 >= 1.6 && ratio3 <= 3.6) ? "Pass" : "Not pass");
strcpy(ratio4_pass, (ratio4 >= 0.1 && ratio4 <= 0.5) ? "Pass" : "Not pass");
strcpy(ratio5_pass, (ratio5 >= 0.4 && ratio5 <= 0.7) ? "Pass" : "Not pass");
SetWindowText(ratio1_comment, ratio1_pass);
SetWindowText(ratio2_comment, ratio2_pass);
SetWindowText(ratio3_comment, ratio3_pass);
SetWindowText(ratio4_comment, ratio4_pass);
SetWindowText(ratio5_comment, ratio5_pass);
}
void DropFile(HDROP hdrop)
{
char fname[MAX_PATH];
DragQueryFile(hdrop, 0, fname, MAX_PATH);
readFile(fname);
setResults();
DragFinish(hdrop);
}
void openFile(HWND hwnd)
{
OPENFILENAME ofn;
char fileName[MAX_PATH] = "";
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hwnd;
ofn.lpstrFilter = "Text files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0";
ofn.lpstrFile = fileName;
ofn.nMaxFile = sizeof(fileName);
ofn.Flags = OFN_FILEMUSTEXIST;
if (GetOpenFileName(&ofn))
{
readFile(fileName);
setResults();
SetFocus(hwnd);
}
}
void showAboutDialog(HWND hwnd)
{
char aboutMessage[MAX_LINE_LENGTH];
sprintf(
aboutMessage, "Ratio Checker v%s\n\n"
"Copyright (c) 2023 Vitaliy Pavlov\n"
"Source code: https://github.com/v-2841/ratio_checker",
VERSION);
MessageBox(hwnd, aboutMessage, "About", MB_OK);
}
void localHelp()
{
ShellExecute(NULL, "open", "help.html", NULL, NULL, SW_SHOWNORMAL);
}
void menu(HWND hwnd)
{
HMENU hMenu = CreateMenu();
HMENU hFileMenu = CreatePopupMenu();
AppendMenu(hFileMenu, MF_STRING, IDM_OPEN_BUTTON,
"Open Data File...\tCtrl+O");
AppendMenu(hFileMenu, MF_SEPARATOR, 0, NULL);
AppendMenu(hFileMenu, MF_STRING, IDM_EXIT, "Exit\tAlt+F4");
HMENU hHelpMenu = CreatePopupMenu();
AppendMenu(hHelpMenu, MF_STRING, IDM_ONLINE_HELP, "User manual\tF1");
AppendMenu(hHelpMenu, MF_SEPARATOR, 0, NULL);
AppendMenu(hHelpMenu, MF_STRING, IDM_ABOUT, "About Ratio Checker...");
AppendMenu(hMenu, MF_POPUP, (UINT_PTR)hFileMenu, "File");
AppendMenu(hMenu, MF_POPUP, (UINT_PTR)hHelpMenu, "Help");
SetMenu(hwnd, hMenu);
}
LRESULT WndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
{
if (message == WM_CREATE)
menu(hwnd);
else if (message == WM_DROPFILES)
DropFile((HDROP)wparam);
else if (message == WM_COMMAND)
{
if (LOWORD(wparam) == IDM_OPEN_BUTTON)
openFile(hwnd);
else if (LOWORD(wparam) == IDM_ABOUT)
showAboutDialog(hwnd);
else if (LOWORD(wparam) == IDM_ONLINE_HELP)
localHelp();
else if (LOWORD(wparam) == IDM_EXIT)
PostQuitMessage(0);
}
else if (message == WM_KEYDOWN)
{
if (LOWORD(wparam) == VK_F1)
localHelp();
else if (GetKeyState(VK_CONTROL) & 0x8000 && wparam == 'O')
openFile(hwnd);
}
else if (message == WM_DESTROY)
PostQuitMessage(0);
else
return DefWindowProcA(hwnd, message, wparam, lparam);
}
int main(int argc, char *argv[])
{
LPRECT rctScr;
rctScr = malloc(sizeof(*rctScr));
GetClientRect(GetDesktopWindow(), rctScr);
WNDCLASSA wlc;
memset(&wlc, 0, sizeof(WNDCLASSA));
wlc.lpszClassName = "window";
wlc.lpfnWndProc = (WNDPROC)WndProc;
wlc.hbrBackground = (HBRUSH)CreateSolidBrush(RGB(255, 252, 231));
wlc.hCursor = LoadCursor(NULL, IDC_ARROW);
wlc.hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(101));
RegisterClassA(&wlc);
HWND hwnd;
hwnd = CreateWindow(
"window", "Ratio checker", WS_OVERLAPPEDWINDOW,
(rctScr[0].right - 606) / 2, (rctScr[0].bottom - 419) / 2,
606, 419, NULL, NULL, NULL, NULL);
ShowWindow(hwnd, SW_SHOWNORMAL);
DragAcceptFiles(hwnd, TRUE);
free(rctScr);
MSG msg;
if (argc != 1)
readFile(argv[1]);
HWND sample_name_name_hwnd = CreateWindow(
"static", "Sample name", WS_VISIBLE | WS_CHILD | WS_BORDER,
20, 20, 100, 20, hwnd, NULL, NULL, NULL);
sample_name_hwnd = CreateWindow(
"static", sample_name, WS_VISIBLE | WS_CHILD | WS_BORDER,
120, 20, 180, 20, hwnd, NULL, NULL, NULL);
HWND acid_name = CreateWindow(
"static", "Acid", WS_VISIBLE | WS_CHILD | WS_BORDER,
20, 60, 80, 20, hwnd, NULL, NULL, NULL);
HWND c120_name = CreateWindow(
"static", "C12:0", WS_VISIBLE | WS_CHILD | WS_BORDER,
20, 80, 80, 20, hwnd, NULL, NULL, NULL);
HWND c140_name = CreateWindow(
"static", "C14:0", WS_VISIBLE | WS_CHILD | WS_BORDER,
20, 100, 80, 20, hwnd, NULL, NULL, NULL);
HWND c160_name = CreateWindow(
"static", "C16:0", WS_VISIBLE | WS_CHILD | WS_BORDER,
20, 120, 80, 20, hwnd, NULL, NULL, NULL);
HWND c180_name = CreateWindow(
"static", "C18:0", WS_VISIBLE | WS_CHILD | WS_BORDER,
20, 140, 80, 20, hwnd, NULL, NULL, NULL);
HWND c181_name = CreateWindow(
"static", "C18:1", WS_VISIBLE | WS_CHILD | WS_BORDER,
20, 160, 80, 20, hwnd, NULL, NULL, NULL);
HWND c182_name = CreateWindow(
"static", "C18:2", WS_VISIBLE | WS_CHILD | WS_BORDER,
20, 180, 80, 20, hwnd, NULL, NULL, NULL);
HWND area_name = CreateWindow(
"static", "Area", WS_VISIBLE | WS_CHILD | WS_BORDER,
100, 60, 100, 20, hwnd, NULL, NULL, NULL);
HWND ratio_name = CreateWindow(
"static", "Ratio", WS_VISIBLE | WS_CHILD | WS_BORDER,
20, 220, 310, 20, hwnd, NULL, NULL, NULL);
HWND ratio1_name = CreateWindow(
"static", "C16:0 : C12:0",
WS_VISIBLE | WS_CHILD | WS_BORDER,
20, 240, 310, 20, hwnd, NULL, NULL, NULL);
HWND ratio2_name = CreateWindow(
"static", "C18:0 : C12:0",
WS_VISIBLE | WS_CHILD | WS_BORDER,
20, 260, 310, 20, hwnd, NULL, NULL, NULL);
HWND ratio3_name = CreateWindow(
"static", "C18:1 : C14:0",
WS_VISIBLE | WS_CHILD | WS_BORDER,
20, 280, 310, 20, hwnd, NULL, NULL, NULL);
HWND ratio4_name = CreateWindow(
"static", "C18:2 : C14:0",
WS_VISIBLE | WS_CHILD | WS_BORDER,
20, 300, 310, 20, hwnd, NULL, NULL, NULL);
HWND ratio5_name = CreateWindow(
"static", "C18:1 + C18:2 : C12:0 + C14:0 + C16:0 + C18:0",
WS_VISIBLE | WS_CHILD | WS_BORDER, 20, 320, 310, 20,
hwnd, NULL, NULL, NULL);
HWND range_name = CreateWindow(
"static", "Range", WS_VISIBLE | WS_CHILD | WS_BORDER | SS_CENTER,
330, 220, 80, 20, hwnd, NULL, NULL, NULL);
HWND range1_name = CreateWindow(
"static", "5.8 - 14.5", WS_VISIBLE | WS_CHILD | WS_BORDER | SS_CENTER,
330, 240, 80, 20, hwnd, NULL, NULL, NULL);
HWND range2_name = CreateWindow(
"static", "1.9 - 5.9", WS_VISIBLE | WS_CHILD | WS_BORDER | SS_CENTER,
330, 260, 80, 20, hwnd, NULL, NULL, NULL);
HWND range3_name = CreateWindow(
"static", "1.6 - 3.6", WS_VISIBLE | WS_CHILD | WS_BORDER | SS_CENTER,
330, 280, 80, 20, hwnd, NULL, NULL, NULL);
HWND range4_name = CreateWindow(
"static", "0.1 - 0.5", WS_VISIBLE | WS_CHILD | WS_BORDER | SS_CENTER,
330, 300, 80, 20, hwnd, NULL, NULL, NULL);
HWND range5_name = CreateWindow(
"static", "0.4 - 0.7", WS_VISIBLE | WS_CHILD | WS_BORDER | SS_CENTER,
330, 320, 80, 20, hwnd, NULL, NULL, NULL);
HWND result_name = CreateWindow(
"static", "Result", WS_VISIBLE | WS_CHILD | WS_BORDER | SS_CENTER,
410, 220, 80, 20, hwnd, NULL, NULL, NULL);
HWND comment_name = CreateWindow(
"static", "Comment", WS_VISIBLE | WS_CHILD | WS_BORDER | SS_CENTER,
490, 220, 80, 20, hwnd, NULL, NULL, NULL);
c120_area = CreateWindow(
"static", doubleToChar(c120), WS_VISIBLE | WS_CHILD | WS_BORDER,
100, 80, 100, 20, hwnd, NULL, NULL, NULL);
c140_area = CreateWindow(
"static", doubleToChar(c140), WS_VISIBLE | WS_CHILD | WS_BORDER,
100, 100, 100, 20, hwnd, NULL, NULL, NULL);
c160_area = CreateWindow(
"static", doubleToChar(c160), WS_VISIBLE | WS_CHILD | WS_BORDER,
100, 120, 100, 20, hwnd, NULL, NULL, NULL);
c180_area = CreateWindow(
"static", doubleToChar(c180), WS_VISIBLE | WS_CHILD | WS_BORDER,
100, 140, 100, 20, hwnd, NULL, NULL, NULL);
c181_area = CreateWindow(
"static", doubleToChar(c181), WS_VISIBLE | WS_CHILD | WS_BORDER,
100, 160, 100, 20, hwnd, NULL, NULL, NULL);
c182_area = CreateWindow(
"static", doubleToChar(c182), WS_VISIBLE | WS_CHILD | WS_BORDER,
100, 180, 100, 20, hwnd, NULL, NULL, NULL);
calculateRatios();
ratio1_result = CreateWindow(
"static", doubleToChar(ratio1),
WS_VISIBLE | WS_CHILD | WS_BORDER | SS_CENTER,
410, 240, 80, 20, hwnd, NULL, NULL, NULL);
ratio2_result = CreateWindow(
"static", doubleToChar(ratio2),
WS_VISIBLE | WS_CHILD | WS_BORDER | SS_CENTER,
410, 260, 80, 20, hwnd, NULL, NULL, NULL);
ratio3_result = CreateWindow(
"static", doubleToChar(ratio3),
WS_VISIBLE | WS_CHILD | WS_BORDER | SS_CENTER,
410, 280, 80, 20, hwnd, NULL, NULL, NULL);
ratio4_result = CreateWindow(
"static", doubleToChar(ratio4),
WS_VISIBLE | WS_CHILD | WS_BORDER | SS_CENTER,
410, 300, 80, 20, hwnd, NULL, NULL, NULL);
ratio5_result = CreateWindow(
"static", doubleToChar(ratio5),
WS_VISIBLE | WS_CHILD | WS_BORDER | SS_CENTER,
410, 320, 80, 20, hwnd, NULL, NULL, NULL);
strcpy(
ratio1_pass, (ratio1 >= 5.8 && ratio1 <= 14.5) ? "Pass" : "Not pass");
strcpy(ratio2_pass, (ratio2 >= 1.9 && ratio2 <= 5.9) ? "Pass" : "Not pass");
strcpy(ratio3_pass, (ratio3 >= 1.6 && ratio3 <= 3.6) ? "Pass" : "Not pass");
strcpy(ratio4_pass, (ratio4 >= 0.1 && ratio4 <= 0.5) ? "Pass" : "Not pass");
strcpy(ratio5_pass, (ratio5 >= 0.4 && ratio5 <= 0.7) ? "Pass" : "Not pass");
ratio1_comment = CreateWindow(
"static", ratio1_pass, WS_VISIBLE | WS_CHILD | WS_BORDER | SS_CENTER,
490, 240, 80, 20, hwnd, NULL, NULL, NULL);
ratio2_comment = CreateWindow(
"static", ratio2_pass, WS_VISIBLE | WS_CHILD | WS_BORDER | SS_CENTER,
490, 260, 80, 20, hwnd, NULL, NULL, NULL);
ratio3_comment = CreateWindow(
"static", ratio3_pass, WS_VISIBLE | WS_CHILD | WS_BORDER | SS_CENTER,
490, 280, 80, 20, hwnd, NULL, NULL, NULL);
ratio4_comment = CreateWindow(
"static", ratio4_pass, WS_VISIBLE | WS_CHILD | WS_BORDER | SS_CENTER,
490, 300, 80, 20, hwnd, NULL, NULL, NULL);
ratio5_comment = CreateWindow(
"static", ratio5_pass, WS_VISIBLE | WS_CHILD | WS_BORDER | SS_CENTER,
490, 320, 80, 20, hwnd, NULL, NULL, NULL);
while (GetMessage(&msg, NULL, 0, 0))
{
DispatchMessage(&msg);
}
return 0;
}