-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
606 lines (441 loc) · 13.7 KB
/
main.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
#include <iostream>
#include "ourvector.h"
#include <fstream>
#include <string>
using namespace std;
struct Person
{
string name;
ourvector<int> STR_Counts; // average GPA
bool STR_Match = false;
};
struct STR
{
ourvector<char> STR_seq;
}; // ourvector <STR> STR_Sequences; (Will hold each STR)
struct DNA
{
ourvector<char> DNA_seq;
};
// Function prototypes:
void display(ourvector<STR>& STR_Seqs, ourvector<Person>& people, DNA& DNA_Entry, ourvector<int>& Processed_STR_Counts, bool& isProcessed);
void search(ourvector<int>& Processed_STR_Counts,ourvector<Person>& people, bool dbLoaded, bool dnaLoaded, bool isProcessed);
void STRcount_match(ourvector<int>& Processed_STR_Counts, Person& potentialMatch);
int load_db(string filename, ourvector<STR>& STR_seqs, ourvector<Person>& people, bool& isProcessed, bool& dbLoaded);
int load_dna(string filename, DNA& DNA_Entry, bool& isProcessed, bool& dnaLoaded);
void storeSTRs(string STR_Line, ourvector<STR>& STR_seqs);
void storePeople(string People_Line, ourvector<Person>& people);
void process(ourvector<int>& Processed_STR_Counts, ourvector<STR>& STR_seqs, DNA& DNA_Process, bool& isProcessed, bool dbLoaded, bool dnaLoaded);
int countMatches(ourvector<char>& DNA_To_Process, ourvector<char>& STR_To_Process);
bool confirmMatch(int i, ourvector<char> DNA_To_Process, ourvector<char> STR_To_Process);
/*
void loadData(string filename, ourvector<Student> &students);
void load_dna(string filename, ourvector<DNA>& DNA);
void load_db(string filename, ourvector<Pereeson>& persons);
void parseLine(string line, Student &newStudent);
void loadData(string filename, ourvector<Student> &students);
void print(ourvector<Student> &students);
int searchData(string name, ourvector<Student> &students);
string getName(string &line);
double getGPA(string &line);
int searchData(string name, ourvector<Student> &students);
*/
int main()
{
ourvector<Person> people;
ourvector<STR> STR_Sequences;
ourvector<int> Processed_STR_Counts; //Reset at beginning of process()
DNA DNA_Entry;
bool isProcessed = false, dbLoaded = false, dnaLoaded = false;
// This vector is to store structures composed of STR sequences.
// pop people on after reading the DNA for the last person,
// or this is the first person being added.
//cout << endl << " Welcome to the DNA Profiler NucleoSearch" << endl << endl;
cout << "Welcome to the DNA Profiling Application." << endl;
string filename;
//cout << " Supported Commands:" << endl << " '#': Quit" << endl << " 'load' filename: Load database from file" << endl << " 'display': Display database information" << endl;
//cout << endl;
cout << "Enter command or # to exit: ";
//cout << " Please enter a .txt file containing a DNA sequence" << endl << endl;
//cout << " The DNA Sequence Will Be Searched Among a DNA Database for a Match. " << endl << endl;
//cin >> filename;
// Here: Enter the template for opening the file, or have the file reading done in another function...
//cout << " Enter command: ";
string command;
cin >> command;
//cout << endl;
while (command != "#") {
// "load filename"
// load in the data and store in a useful way
if (command == "load_db") {
cin >> filename;
cout << "Loading database..." << endl;
load_db(filename, STR_Sequences, people, isProcessed, dbLoaded);
//display(STR_Sequences, people, DNA_Entry);
/*
loadData(filename, students);
*/
// "lookup First Last"
// allow user to look up average GPA of a particulary student.
}
else if (command == "load_dna")
{
cin >> filename;
cout << "Loading DNA..." << endl;
load_dna(filename, DNA_Entry,isProcessed, dnaLoaded);
}
else if (command == "display")
{
display(STR_Sequences,people,DNA_Entry,Processed_STR_Counts, isProcessed);
}
else if (command == "process")
{
//ourvector<int>& Processed_STR_Counts, ourvector<STR>& STR_seqs, DNA& DNA_Process
// Check if Database is loaded, and DNA is loaded first.
process(Processed_STR_Counts, STR_Sequences, DNA_Entry, isProcessed, dbLoaded, dnaLoaded);
}
else if (command == "search")
{
search(Processed_STR_Counts,people, dbLoaded, dnaLoaded, isProcessed);
}
else if (command == "lookup") {
string name;
cin.ignore();
getline(cin, name, '\n');
cout << "Lookup command with" << name << endl;
/*
int ind = searchData(name, students);
*/
//cout << name << " found. GPA: " <<
//students[ind].gpa << endl;
}
cout << "Enter command or # to exit: ";
cin >> command;
}
return 0;
}
void display(ourvector<STR>& STR_Seqs, ourvector<Person>& people, DNA& DNA_Entry, ourvector<int>& Processed_STR_Counts, bool& isProcessed)
{
/*
Database loaded:
Alice 2 8 3
Bob 4 1 5
Charlie 3 2 5
No DNA loaded.
No DNA has been processed.
*/
// Output the stored STRs from STR_Sequences
if (STR_Seqs.size() == 0)
{
cout << "No database loaded." << endl;
}
else
{
cout << "Database loaded: " << endl;
/*
for (auto STR : STR_Seqs)
{
for (int i = 0; i < STR.STR_seq.size(); ++i)
{
cout << STR.STR_seq[i];
}
cout << " ";
}
cout << endl;
*/
// Output the stored People from people
// NO DATABASE LOADED!!!!!! ******
//cout << "People Stored In Database: " << endl;
for (auto Person : people)
{
cout << Person.name << " ";
//cout << Person.STR_Counts.size() << endl;
for (int i = 0; i < Person.STR_Counts.size(); ++i)
{
if (i == Person.STR_Counts.size() - 1)
{
cout << Person.STR_Counts[i];
}
else
{
cout << Person.STR_Counts[i] << " ";
}
}
cout << endl;
}
cout << endl;
}
// Output the stored DNA sequence
if (DNA_Entry.DNA_seq.size() == 0)
{
cout << "No DNA loaded." << endl << endl;
}
else
{
cout << "DNA loaded: " << endl;
for (int i = 0; i < DNA_Entry.DNA_seq.size(); ++i)
{
cout << DNA_Entry.DNA_seq[i];
}
cout << endl << endl;
}
// Output the DNA being processed
if (isProcessed)
{
cout << "DNA processed, STR counts: " << endl;
for (int i = 0; i < STR_Seqs.size(); ++i)
{
for (int y = 0; y < STR_Seqs[i].STR_seq.size(); ++y)
{
cout << STR_Seqs[i].STR_seq[y];
}
cout << ": " << Processed_STR_Counts[i] << endl;
}
}
else
{
cout << "No DNA has been processed." << endl;
}
}
void search(ourvector<int>& Processed_STR_Counts,ourvector<Person>& people, bool dbLoaded, bool dnaLoaded, bool isProcessed)
{
if (dbLoaded && dnaLoaded && isProcessed)
{
cout << "Searching database..." << endl;
for (int i = 0; i < people.size(); ++i)
{
STRcount_match(Processed_STR_Counts, people[i]);
}
for (int i = 0; i < people.size(); ++i)
{
if (people[i].STR_Match)
{
cout << "Found in database! DNA matches: " << people[i].name << endl;
return;
}
}
cout << "Not found in database." << endl;
}
else if (!dbLoaded)
{
cout << "No database loaded." << endl;
}
else if (!dnaLoaded)
{
cout << "No DNA loaded." << endl;
}
else if (!isProcessed)
{
cout << "No DNA processed." << endl;
}
return;
}
void STRcount_match(ourvector<int>& Processed_STR_Counts, Person& potentialMatch)
{
for (int i = 0; i < potentialMatch.STR_Counts.size(); ++i)
{
if (potentialMatch.STR_Counts[i] == Processed_STR_Counts[i])
{
}
else
{
return;
}
}
potentialMatch.STR_Match = true;
}
int load_db(string filename, ourvector<STR>& STR_seqs, ourvector<Person>& people, bool& isProcessed, bool& dbLoaded)
{
ifstream infile(filename);
string STR_Line, Person_Line;
if (!infile.is_open())
{
cout << "Error: unable to open '"<< filename << "'" << endl;
//Error: unable to open 'invalid_db_file.txt
return 1;
}
getline(infile, STR_Line, '\n');
if (!infile.fail())
{
storeSTRs(STR_Line, STR_seqs);
}
getline(infile, Person_Line, '\n');
while (!infile.eof())
{
if (!infile.fail())
{
storePeople(Person_Line, people);
}
getline(infile, Person_Line, '\n');
}
dbLoaded = true;
isProcessed = false;
infile.close();
return 0;
}
void storeSTRs(string STR_Line, ourvector<STR>& STR_seqs)
{
for (std::size_t i = 5; i < STR_Line.size(); ++i)
{
STR newSTR;
while (STR_Line[i] != ',' && i < STR_Line.size())
{
newSTR.STR_seq.push_back(STR_Line[i]);
++i;
}
STR_seqs.push_back(newSTR);
}
}
void storePeople(string People_Line, ourvector<Person>& people)
{
// Build each Person structure, then append to the vector of people composing the database
string newName, newInt;
ourvector<int> STR_Counts;
Person newPerson;
int i = 0;
int y = 0; // Tracks the integer length of the STR count
while (People_Line[i] != ',')
{
newName += People_Line[i];
++i;
}
newPerson.name = newName;
for (i; i < People_Line.size(); ++i)
{
newInt = "";
while (People_Line[i] != ',' && i < People_Line.size())
{
newInt += People_Line[i];
i++;
}
if (newInt != "")
{
int newSTR_Count = stoi(newInt);
newPerson.STR_Counts.push_back(newSTR_Count);
}
}
people.push_back(newPerson);
}
int load_dna(string filename, DNA& DNA_Entry, bool& isProcessed, bool& dnaLoaded)
{
ifstream infile2(filename);
string DNA_Lines;
if (!infile2.is_open())
{
cout << "Error: unable to open '" << filename << "'" << endl;
//Error: unable to open 'invalid_dna_file.txt
return 1;
}
//getline(infile, STR_Line, '\n');
infile2 >> DNA_Lines;
if (!infile2.fail())
{
isProcessed = false;
for (int i = 0; i < DNA_Lines.size(); ++i)
{
DNA_Entry.DNA_seq.push_back(DNA_Lines[i]);
}
}
dnaLoaded = true;
return 0;
}
void process(ourvector<int>& Processed_STR_Counts, ourvector<STR>& STR_seqs, DNA& DNA_Process, bool& isProcessed, bool dbLoaded, bool dnaLoaded)
{
int STR_Matches = 0;
if (dbLoaded && dnaLoaded)
{
cout << "Processing DNA..." << endl;
isProcessed = true;
for (int z = 0; z < STR_seqs.size(); ++z)
{
STR_Matches = countMatches(DNA_Process.DNA_seq, STR_seqs[z].STR_seq);
Processed_STR_Counts.push_back(STR_Matches);
}
}
else if (!dbLoaded)
{
cout << "No database loaded." << endl;
}
else if (!dnaLoaded)
{
cout << "No DNA loaded." << endl;
}
}
int countMatches(ourvector<char>& DNA_To_Process, ourvector<char>& STR_To_Process)
{
bool match = false;
int matches = 0, most_matches = 0;
ourvector<int> Matches_Count;
for (int i = 0; i < DNA_To_Process.size() && ((i + STR_To_Process.size()) < DNA_To_Process.size()); ++i)
{
match = false;
matches = 0;
if (DNA_To_Process[i] == STR_To_Process[0])
{
match = confirmMatch(i, DNA_To_Process, STR_To_Process);
}
while (match && ((i + STR_To_Process.size()) < DNA_To_Process.size()))
{
i += STR_To_Process.size();
matches += 1;
match = confirmMatch(i, DNA_To_Process, STR_To_Process);
}
Matches_Count.push_back(matches);
}
/*
if (Matches_Count.size() == 0) {
return 0;
}
*/
most_matches = Matches_Count[0];
for (int y = 0; y < Matches_Count.size(); ++y)
{
if (most_matches < Matches_Count[y])
{
most_matches = Matches_Count[y];
}
}
return most_matches;
}
bool confirmMatch(int i, ourvector<char> DNA_To_Process, ourvector<char> STR_To_Process)
{
for (int x = 0; x < STR_To_Process.size(); ++x)
{
if (STR_To_Process[x] == DNA_To_Process[i])
{
++i;
}
else
{
return false;
}
}
return true;
}
/*
int main()
{
string filename, value;
ourvector<string> DNA_Strands;
// TODO: Write your code here. You should have LOTS of function decomposition.
cout << "TEST" << endl;
cout << "Input file name" << endl;
cin >> filename;
cout << "File Name Was: " << filename << endl;
ifstream infile(filename);
while (!infile.eof())
{
getline(infile,value,'\n');
if (!infile.fail())
{
DNA_Strands.push_back(value);
cout << "Success: " << value << endl;
}
}
for (auto &e : DNA_Strands)
{
cout << e << endl;
}
return 0;
}
*/