forked from liamnegron3/COP3530_Project3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaxonomyGraph.cpp
More file actions
326 lines (288 loc) · 8.56 KB
/
Copy pathTaxonomyGraph.cpp
File metadata and controls
326 lines (288 loc) · 8.56 KB
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
#include <iomanip>
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <queue>
#include <set>
#include <algorithm>
#include "TaxonomyGraph.h"
using namespace std;
void TaxonomyGraph::ReadTaxonomyIDs(string filename)
{
ifstream file(filename);
string currentData;
string column[4];
//skip first line
getline(file,currentData);
//parse each line with tab delimiter
int i = 0;
while(getline(file,currentData,'\t'))
{
switch(i)
{
case 0:
//taxonID
column[0] = currentData;
break;
case 2:
//parentID
column[1] = currentData;
break;
case 5:
//scientific name
column[2] = currentData;
break;
case 9:
//generalName
column[3] = currentData;
break;
}
//if the end of the line is reached
if(currentData.find('\n') != string::npos)
{
//assign map values
idToName[column[0]] = make_pair(column[3],column[2]);
//assign graph values
ancestorGraph[column[1]].push_back(column[0]);
ancestorGraph[column[0]].push_back(column[1]);
//assign childToParentID
childToParentID[column[0]] = column[1];
//if(ancestorGraph.find(column[0]) == ancestorGraph.end())
//ancestorGraph[column[0]].push_back(column[1]);
//get rid of \n in fron of next taxonomyID
currentData = currentData.substr(currentData.find('\n')+1);
column[0] = currentData;
i = 1;
}
else
i++;
}
file.close();
}
void TaxonomyGraph::ReadCommonNames(string filename)
{
ifstream file(filename);
string currentData;
string column[3];
//skip first line
getline(file,currentData);
//parse each line with tab delimiter
int i = 0;
while(getline(file,currentData,'\t'))
{
switch(i)
{
case 0:
//taxonID
column[0] = currentData;
break;
case 2:
//commonName
column[1] = currentData;
break;
case 4:
//language
column[2] = currentData;
break;
}
//if the end of the line is reached
if(currentData.find('\n') != string::npos)
{
//assign map values taxonID => <commonName, language>
idToNameLang[column[0]].push_back(make_pair(column[1],column[2]));
//get rid of \n in fron of next taxonomyID
currentData = currentData.substr(currentData.find('\n')+1);
column[0] = currentData;
i = 1;
}
else
i++;
}
file.close();
//process idToNameLang into english preferred idToName
for(auto member: idToNameLang)
{
for(unsigned int i = 0; i < member.second.size(); i++)
{
if(member.second[i].second == "eng")
{
string temp = idToName[member.first].second;
idToName[member.first] = make_pair(member.second[i].first,temp);
break;
}
else if(i == member.second.size() - 1)
{
string temp = idToName[member.first].second;
idToName[member.first] = make_pair(member.second[i].first,temp);
}
}
}
//create nameToID
for(auto member: idToName)
{
nameToID[member.second.first] = make_pair(member.second.second,member.first);
}
}
vector<pair<string,string>> TaxonomyGraph::CommonAncestorPath(string commonName1, string commonName2)
{
//do BFS to find ancestral path
vector<pair<string,string>> ancestorPath;
set<string> visited;
queue<string> q;
//insert commonName taxonID
visited.insert(nameToID[commonName1].second);
//push first element to the queue
q.push(nameToID[commonName1].second);
while(!q.empty())
{
string parentID = q.front();
ancestorPath.push_back(make_pair(idToName[parentID].first, idToName[parentID].second));
q.pop();
for(unsigned int i = 0; i < ancestorGraph[parentID].size(); i++)
{
string childID = ancestorGraph[parentID][i];
if(idToName[childID].first == commonName2)
{
ancestorPath.push_back(make_pair(idToName[childID].first, idToName[childID].second));
return ancestorPath;
}
if(visited.find(childID) == visited.end())
{
visited.insert(childID);
q.push(childID);
}
}
}
return ancestorPath;
}
pair<string,string> TaxonomyGraph::CommonAncestor(string commonName1, string commonName2)
{
vector<pair<string,string>> path1 = SpeciesAncestorTree(commonName1);
vector<pair<string,string>> path2 = SpeciesAncestorTree(commonName2);
//find the smaller path
int size = 0;
if(path1.size() < path2.size())
size = path1.size();
else
size = path2.size();
//compare paths up to first difference then the previous element is the
//common ancestor
for(int i = 0; i < size; i++)
{
if(path1[i] != path2[i])
return path1[i-1];
}
//if equivalent up return end of smaller path - 1
if(path1.size() < path2.size())
return path1[path1.size()-2];
else
return path2[path2.size()-2];
}
vector<pair<string,string>> TaxonomyGraph::SpeciesAncestorTree(string speciesName)
{
vector<pair<string,string>> ancestorTree;
string childID = nameToID[speciesName].second;
//check if there is an ID
if (NameExists(speciesName) == false)
return {};
for(unsigned int i = 0; i < childToParentID.size(); i++)
{
ancestorTree.push_back(make_pair(idToName[childID].first, idToName[childID].second));
if(childID == "5T6MX")
{
reverse(ancestorTree.begin(),ancestorTree.end());
return ancestorTree;
}
//traverse up the tree
childID = childToParentID[childID];
}
//path not found
return {};
}
vector<pair<string,string>> TaxonomyGraph::findSiblings(string commonName)
{
string childID = nameToID[commonName].second;
string parentID = childToParentID[childID];
vector<string> siblingsCommon;
//determine siblings
for(unsigned int i = 0; i < ancestorGraph[parentID].size(); i++)
{
if(ancestorGraph[parentID][i] == childID || ancestorGraph[parentID][i] == childToParentID[parentID])
continue;
else
{
string siblingCommonName = idToName[ancestorGraph[parentID][i]].first;
if(siblingCommonName.size() > 0 && count(siblingsCommon.begin(),siblingsCommon.end(),siblingCommonName) == 0)
siblingsCommon.push_back(siblingCommonName);
}
}
//do quick sort here
quickSort<string>(siblingsCommon,0,siblingsCommon.size()-1);
//recombine with scientific names
vector<pair<string,string>> siblings;
for(unsigned int i = 0; i < siblingsCommon.size(); i++)
{
string scientficName = nameToID[siblingsCommon[i]].first;
siblings.push_back(make_pair(siblingsCommon[i],scientficName));
}
return siblings;
}
pair<string,string> TaxonomyGraph::getParentName(string name)
{
string childID = nameToID[name].second;
string parentID = childToParentID[childID];
return idToName[parentID];
}
bool TaxonomyGraph::NameExists(string commonName)
{
if(idToName[nameToID[commonName].second].first.size() > 0)
return true;
else
return false;
}
void TaxonomyGraph::verifyName(string& commonName)
{
if(NameExists(commonName) == false)
{
//check lowercase
//make input all lowercase to match data
string lowercase = commonName;
for(unsigned int i = 0; i < commonName.size(); i++)
lowercase[i] = tolower(commonName[i]);
string uppercase = lowercase;
uppercase[0] = toupper(uppercase[0]); //make first letter uppercase
if(NameExists(lowercase))
commonName = lowercase;
else if(NameExists(uppercase))
commonName = uppercase;
else
{
//check for spaces
int spacePosition = 0;
bool found = false;
for(unsigned int i = 0; i < uppercase.size(); i++)
{
if(found)
{
spacePosition = uppercase.find(' ', spacePosition + 1);
if(uppercase.find(' ') != string::npos)
{
found = true;
// uppercase[spacePosition + 1] = toupper(uppercase[spacePosition + 1]);
}
else
found = false;
}
else
spacePosition = uppercase.find(' ');
if(uppercase.find(' ') != string::npos)
{
found = true;
uppercase[spacePosition + 1] = toupper(uppercase[spacePosition + 1]);
}
}
commonName = uppercase;
}
}
}