-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparsing.cpp
630 lines (541 loc) · 18.5 KB
/
parsing.cpp
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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
/** Program written by Luis Ardila following the double Love slow cooking
method, a first cooking step follow by a long fermentation and a post-cooking
period. All this delivers an extraordinary taste of double cooked and fermented
Love portion :*
email: [email protected]
*/
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <iomanip>
using namespace std;
#include <cstring>
#include <stdlib.h>
struct TrtSt
{
string grouping;
string mean; //float
string sampleN; //int
string trtName;
};
TrtSt *pointerTrtSt = new TrtSt[20];
const int BiomarkersNum1 = 20, StatTestsNum = 4, TrtNum1 = 20;
const string StatGrouping[4] = {"t", "Duncan", "Tukey", "Scheffe"};
const string StatTest[4] = { "t Tests (LSD) for ",
"Duncan's Multiple Range Test for ",
"Tukey's Studentized Range (HSD) Test for ",
"Scheffe's Test for "};
TrtSt DataStructure [BiomarkersNum1][StatTestsNum][TrtNum1];
string LSmeansSt [BiomarkersNum1][TrtNum1][TrtNum1];
string reduce( const string& str,
const string& fill,
const string& whitespace);
string trim( const string& str,
const string& whitespace);
void line2array(string& ReadReduced);
void words2structure(TrtSt** pointerTrtSt);
void printingScreen();
void printingFile();
int getTrtNumber();
ifstream Reading;
string ReadingName, Read, ReadReduced;
const int linelenght = 200;
string delimiter = " ";
char Buffer[linelenght];
string token;
string Trtnames[15];
string Biomarkers[15];
string farray[15];
string test[15];
size_t pos = 0;
int num, innerNum, i, TreatmentsReadNum, LSnum;
int o = 0, Bio = 0, Stat = 0, Trt = 0;
int ii = 0;
int BiomarkersNum;
int TrtNum;
int main(int argc, char** argv) {
ReadingName = argv[1];
Reading.open(ReadingName.c_str());
num = 0;
innerNum = 0;
//Reading one line at a time of the input file
while (!Reading.eof()) {
//reading each line until the character /n apears: meaning end of line
Reading.getline(Buffer,linelenght,'\n');
//from char to string
Read=Buffer;
//runing each line through the "reduce" function to
//get rid of extra espaces
ReadReduced = reduce(Read, " ", " \t");
//num is an indicator of the step we are in, first gather
//the number of Biomarkers, then the number of treatments and finally
//fill in the info for each
switch (num) {
//gathering the info for the biomarkers
case 0 :
//finding line with this words as begining
if (ReadReduced.find("Obs Trt Rep") == 0) {
line2array(ReadReduced);
//Biomarkers num minus the first three words
BiomarkersNum = ii - 3;
//cout << BiomarkersNum << endl;
for (i = 0; i < BiomarkersNum; i++) {
//printing names with tabs
Biomarkers[i] = farray [ii-BiomarkersNum+i];
//cout << Biomarkers [i] << "\t";
}
cout << endl;
num++;
}
break;
//gathering info about the treatments, number and names
case 1 :
//finding line with this words as begining
if (ReadReduced.find("Trt") == 0) {
line2array(ReadReduced);
istringstream ( farray[1] ) >> TrtNum;
//cout << TrtNum << endl;
for (i = 0; i < TrtNum; i++) {
//printing names with tabs
Trtnames[i] = farray [ii-TrtNum+i];
//cout << Trtnames [i] << "\t";
}
//cout << endl;
num++;
//Initializing the index for the DataStructure
Bio = 0;
Stat = 0;
Trt = 0;
/** printing
for (int a = 0; a < 4; a++)
cout << StatGrouping[a] << endl;
*/
}
break;
//###########################################################################
//finding info for each biomarker, each probablility test and
//organizing the data inside each treatment name
//case 2 is for t Tests (LSD)
case 2 :
{
switch (innerNum) {
case 0 :
//finding line with this words as begining
if (ReadReduced.find(StatTest[Stat] + Biomarkers[Bio])
== 0) {
//cout << StatTest[Stat] << Biomarkers[Bio] << endl;
innerNum++;
}
break;
case 1 :
if (ReadReduced.find(StatGrouping[Stat] + " Grouping")
== 0) {
//cout << StatGrouping[Stat] << " Grouping" << endl;
innerNum++;
}
break;
case 2 :
innerNum++; //empty space line
break;
case 3 :
{
line2array(ReadReduced);
// Function to save the words in a line into the
//structure of:
// Grouping Mean N Trt
words2structure(&pointerTrtSt);
//increasing number of treatments already read
TreatmentsReadNum++;
//if iqual to treatment num then go to next, else go
//to next line
if (TreatmentsReadNum == TrtNum) {
//cout << endl;
for (int a = 0; a < TrtNum; a++) {
DataStructure [Bio][Stat][a] = pointerTrtSt[a];
/** printing
cout << pointerTrtSt[a].grouping << "\t"
<< pointerTrtSt[a].mean << "\t"
<< pointerTrtSt[a].sampleN << "\t"
<< pointerTrtSt[a].trtName << endl;
*/
}
/** Printing
for (int a = 0; a < TrtNum; a++) {
DataStructure [Bio][Stat][a] = pointerTrtSt[a];
cout << (DataStructure[Bio][Stat][a]).grouping
<< "\t" << (DataStructure[Bio][Stat][a]).mean
<< "\t" << (DataStructure[Bio][Stat][a]).sampleN
<< "\t" << (DataStructure[Bio][Stat][a]).trtName
<< endl;
}
*/
Stat++;
TreatmentsReadNum = 0;
innerNum = 0;
if (Stat == 4) {
Bio++;
Stat = 0;
if (Bio == BiomarkersNum) {
num++;
}
}
}
else {
innerNum = 2;
}
}
break;
default :
cout << "ERROR!!!!!!" << endl;
break;
}
}
break;
default:
break;
}
//cout << num << endl;
//cout << ReadReduced << endl;
}//end while
Reading.close();
Reading.open(ReadingName.c_str());
LSnum = 0;
Bio = 0;
TreatmentsReadNum = 0;
//Reading one line at a time of the input file
while (!Reading.eof()) {
//reading each line until the character /n apears: meaning end of line
Reading.getline(Buffer,linelenght,'\n');
//from char to string
Read=Buffer;
//runing each line through the "reduce" function to
//get rid of extra espaces
ReadReduced = reduce(Read, " ", " \t");
//num is an indicator of the step we are in, first gather
//the number of Biomarkers, then the number of treatments and finally
//fill in the info for each
switch (LSnum) {
case 0 :
//finding line with this words as begining
if (ReadReduced.find("Dependent Variable: " + Biomarkers[Bio])
== 0) {
//cout << ReadReduced << endl;
LSnum++;
}
break;
case 1 :
//blank line
LSnum++;
//cout << ReadReduced << " " << LSnum << endl;
break;
case 2 :
//number of words is numTrt + 1?
//cout << ReadReduced << endl;
line2array(ReadReduced);
if (ii == TrtNum + 1) {
LSnum++;
}
else
LSnum = 0;
break;
case 3 :
//blank line
LSnum++;
break;
case 4 :
{
//Trt info
//cout << ReadReduced << endl;
line2array(ReadReduced);
int b = 1;
for (int a = 0; a < TrtNum; a++) {
if (TreatmentsReadNum == a) {
LSmeansSt [Bio][TreatmentsReadNum][a] = " ";
}
else {
LSmeansSt [Bio][TreatmentsReadNum][a] = farray [b];
b++;
}
}
TreatmentsReadNum++;
if (TreatmentsReadNum == TrtNum) {
Bio++;
TreatmentsReadNum = 0;
if (Bio == BiomarkersNum) {
LSnum++;
}
else {
LSnum = 0;
}
}
}
break;
default :
break;
}//end switch LSnum
}//end while
Reading.close();
printingScreen();
printingFile();
cout << "END" << endl;
delete [] pointerTrtSt;
}
//-----------------------------------------------------------------------------
//#############################################################################
//FUNCTIONS
void line2array(string& ReadReduced) {
ii = 0;
//while position is different to the end of string
while ((pos = ReadReduced.find(delimiter)) != string::npos) {
//save substring in token
token = ReadReduced.substr(0, pos);
//erase from string the analyzed part saved in token
ReadReduced.erase(0, pos + delimiter.length());
farray[ii] = token;
//incrementing i
ii++;
}//end of while
farray[ii] = ReadReduced;
ii++;
}
void words2structure(TrtSt** pointerTrtSt) {
//defining temporary variable
int switchLetter = 0;
//Asigning a number depending on the initial letter of the line
if (farray[0] == "A")
switchLetter = 1;
else if (farray[0] == "B")
switchLetter = 2;
else if (farray[0] == "C")
switchLetter = 3;
int n1 = getTrtNumber();
if (ii == 4){
switch (switchLetter) {
case 1 :
(*(pointerTrtSt[0]+n1)).grouping = " " + farray [0] + " | | ";
break;
case 2 :
(*(pointerTrtSt[0]+n1)).grouping = " | " + farray [0] + " | ";
break;
case 3 :
(*(pointerTrtSt[0]+n1)).grouping = " | | " + farray [0] + " ";
break;
default :
break;
}
}
else if (ii == 5) {
switch (switchLetter) {
case 1 :
if (farray[1] == "B") {
// B is second after A
(*(pointerTrtSt[0]+n1)).grouping = " " + farray [0] + " | "
+ farray [1] + " | ";
}
else {
// C is second after A
(*(pointerTrtSt[0]+n1)).grouping = " " + farray [0] + " | | "
+ farray [1] + " ";
}
break;
break;
case 2 :
if (farray[1] == "A") {
// A is second after B
(*(pointerTrtSt[0]+n1)).grouping = " " + farray [1] + " | "
+ farray [0] + " | ";
}
else {
// C is second after B
(*(pointerTrtSt[0]+n1)).grouping = " | " + farray [0] + " | "
+ farray [1] + " ";
}
break;
case 3 :
if (farray[1] == "A") {
// A is second after C
(*(pointerTrtSt[0]+n1)).grouping = " " + farray [1] + " | | "
+ farray [0] + " ";
}
else {
// B is second after C
(*(pointerTrtSt[0]+n1)).grouping = " | " + farray [1] + " | "
+ farray [0] + " ";
}
break;
default :
break;
}
}
else if (ii == 6) {
switch (switchLetter) {
case 1 :
if (farray[1] == "B") {
// B is second after A
(*(pointerTrtSt[0]+n1)).grouping = " " + farray [0] + " | "
+ farray [1] + " | " + farray [2] + " ";
}
else {
// C is second after A
(*(pointerTrtSt[0]+n1)).grouping = " " + farray [0] + " | "
+ farray [2] + " | " + farray [1] + " ";
}
break;
case 2 :
if (farray[1] == "A") {
// A is second after B
(*(pointerTrtSt[0]+n1)).grouping = " " + farray [1] + " | "
+ farray [0] + " | " + farray [2] + " ";
}
else {
// C is second after B
(*(pointerTrtSt[0]+n1)).grouping = " " + farray [2] + " | "
+ farray [0] + " | " + farray [1] + " ";
}
break;
case 3 :
if (farray[1] == "A") {
// A is second after C
(*(pointerTrtSt[0]+n1)).grouping = " " + farray [1] + " | "
+ farray [2] + " | " + farray [0] + " ";
}
else {
// B is second after C
(*(pointerTrtSt[0]+n1)).grouping = " " + farray [2] + " | "
+ farray [1] + " | " + farray [0] + " ";
}
break;
default :
break;
}
}
(*(pointerTrtSt[0]+n1)).mean = farray [ii-3];
(*(pointerTrtSt[0]+n1)).sampleN = farray [ii-2];
(*(pointerTrtSt[0]+n1)).trtName = farray [ii-1];
/** printing
for (int a = 0; a < TrtNum; a++) {
cout << "Pre2 " << a << endl;
cout << (*pointerTrtSt[a]).grouping << "\t"
<< (*pointerTrtSt[a]).mean << "\t"
<< (*pointerTrtSt[a]).sampleN << "\t"
<< (*pointerTrtSt[a]).trtName << endl;
getchar();
}
*/
}
int getTrtNumber() {
int n;
for (int a = 0; a < TrtNum; a++) {
if (Trtnames[a].compare(farray[ii-1]) == 0) {
n = a;
break;
}
}
return n;
}
string trim(const string& str,
const string& whitespace = " \t") {
const int strBegin = str.find_first_not_of(whitespace);
if (strBegin == string::npos)
return ""; // no content
const int strEnd = str.find_last_not_of(whitespace);
const int strRange = strEnd - strBegin + 1; //+1 for WINDOWS, 0 for LINUX
return str.substr(strBegin, strRange);
}
string reduce(const string& str,
const string& fill = " ",
const string& whitespace = " \t") {
// trim first
string result = trim(str, whitespace);
// replace sub ranges
int beginSpace = result.find_first_of(whitespace);
while (beginSpace != string::npos)
{
const int endSpace = result.find_first_not_of(whitespace, beginSpace);
const int range = endSpace - beginSpace;
result.replace(beginSpace, range, fill);
const int newStart = beginSpace + fill.length();
beginSpace = result.find_first_of(whitespace, newStart);
}
return result;
}
void printingScreen (){
for (int a = 0; a < BiomarkersNum; a++) {
cout << endl;
cout << setw(7) << Biomarkers[a] << "|"
<< setw(14) << "LSD " << "|"
<< setw(14) << "Duncan " << "|"
<< setw(14) << "Tukey " << "|"
<< setw(14) << "Scheffe " << "|"
<< endl;
cout << setfill('-') << setw(68) << "-"
<< endl << setfill(' ');
for (int b = 0; b < TrtNum; b++) {
cout << setw(7) << Trtnames[b] << "|"
<< setw(14) << (DataStructure[a][0][b]).grouping << "|"
<< setw(14) << (DataStructure[a][1][b]).grouping << "|"
<< setw(14) << (DataStructure[a][2][b]).grouping << "|"
<< setw(14) << (DataStructure[a][3][b]).grouping << "|"
<< endl;
}
cout << endl;
}
//LSmeans
for ( int z = 0; z < BiomarkersNum; z++) {
cout << setw(7) << Biomarkers[z] << "|";
for (int a = 0; a < TrtNum; a++) {
cout << setw(7) << Trtnames[a] << "|";
}
cout << endl;
for (int a = 0; a < TrtNum; a++) {
cout << setw(7) << Trtnames [a] << "|";
for (int c = 0; c < TrtNum; c++) {
cout << setw(7) << (LSmeansSt [z][a][c]) << "|";
}
cout << endl;
}
cout << endl;
}
}
void printingFile (){
ofstream Output;
string OutputName;
OutputName = ReadingName;
//putting .csv for the output name
OutputName.replace(OutputName.length() - 4, OutputName.length(), ".csv");
//putting Out- at the begining of the output file name
OutputName.insert(OutputName.length() - 4, "-Out");
Output.open(OutputName.c_str());
for (int a = 0; a < BiomarkersNum; a++) {
Output << endl;
Output << Biomarkers[a]
<< ",LSD"
<< ",Duncan"
<< ",Tukey"
<< ",Scheffe"
<< ",," << Biomarkers[a];
for (int aa = 0; aa < TrtNum; aa++) {
Output << "," << Trtnames[aa];
}
Output << endl;
for (int b = 0; b < TrtNum; b++) {
Output << Trtnames[b]
<< "," << (DataStructure[a][0][b]).grouping
<< "," << (DataStructure[a][1][b]).grouping
<< "," << (DataStructure[a][2][b]).grouping
<< "," << (DataStructure[a][3][b]).grouping
<< ",,"<< Trtnames [b];
for (int c = 0; c < TrtNum; c++) {
Output << "," << (LSmeansSt [a][b][c]);
}
Output << endl;
}
}
Output.close();
}
//Interesting function for printing each char of a c++ string
// for (std::string::iterator it = farray[ii-1].begin();
// it != farray[ii-1].end(); ++it)
// cout << "ITfarray " << *it << endl;