1
/* This program loads those brain region names from mariana.csv to brain region name database */
5
#include "utils/br_util.h"
13
#define MAXLENGTH 5000
15
int addRegionName(int, string);
16
int addSynRec(int, string, string, string);
17
void addRelationship(int, string, string);
18
vector<string> parseRelStr(string inputStr, string separator);
20
const char * file_in = "marianna.csv";
21
string name_space("Marianna");
23
string rDbName("region_name.db");
24
string sDbName("synonym.db");
25
string rrDbName("region_relation.db");
26
string aDbName("admin.db");
27
string admin_sField("Next Synonym ID");
28
string username("admin");
33
FILE * ifp = fopen(file_in, "r");
35
printf("Fails to read the file: %s\n", file_in);
41
while (fgets(line, MAXLENGTH, ifp)) {
43
// The first 4 lines are skipped
47
// Remove the trailing "return" key
48
stripchars(line, "\n");
50
vector<string> myRow = parseCSV(tmpStr);
52
// Print out error message if the line is not blank but the number of field is not 3
53
if (myRow.size() != 3) {
54
printf("Line #%d: the number of fields is %d\n", lineNo, myRow.size());
55
for (uint i = 0; i < myRow.size(); i++)
56
cout << myRow[i] << endl;
59
// add region name into region db
60
string newName = myRow[0];
61
if (newName.length() == 0) {
62
printf("Line #%d: blank region name, line skipped\n", lineNo);
65
// Note that region names in this namespace are not converted to lower case
66
int foo = addRegionName(lineNo, newName);
68
printf("Line #%d: fails to add region name into region db\n", lineNo);
71
// synonym is not converted to lowercase either
72
string x_synonym = myRow[1];
73
if (x_synonym == newName)
74
printf("Line #%d: synonym is identical to region name", lineNo);
75
else if (x_synonym.length()) {
76
foo = addSynRec(lineNo, x_synonym, newName, "extra synonym from Marianna");
78
printf("Line #%d: fails to add extra synonym into synonym db\n", lineNo);
81
string relStr = myRow[2];
82
addRelationship(lineNo, newName, relStr);
88
/* This function adds a new region name into region name database */
89
int addRegionName(int lineNo, string newName)
91
int foo = chkRegionName(dbHome, rDbName, newName, name_space);
93
printf("Line #%d: fails to check region name availability in region db\n", lineNo);
96
// If region name already exists, skip it instead of terminating the program
98
printf("Line #%d: region \"%s\" exists in region name db\n", lineNo, newName.c_str());
102
foo = chkSynonymStr(dbHome, sDbName, newName, name_space);
104
printf("Line #%d: fails to check region name availability in synonym db\n", lineNo);
107
// If region name already exists, skip it instead of terminating the program
109
printf("Line #%d: region name \"%s\" exists in synonym name db\n", lineNo, newName.c_str());
113
regionRec regionData;
114
regionData.setNameSpace(name_space);
115
regionData.setName(newName);
116
regionData.setSource("Marianna");
117
regionData.setCreator(username);
118
regionData.setAddDate(time(NULL));
119
foo = addRegion_db(dbHome, rDbName, aDbName, regionData);
121
printf("Line #%d: failed to add %s into region name db\n", lineNo, newName.c_str());
127
/* Add a synonym record into synonym db */
128
int addSynRec(int lineNo, string name, string primary, string comments)
130
int foo = chkRegionName(dbHome, rDbName, name, name_space);
132
printf("Line #%d: fails to check synonym's uniqueness in region db\n", lineNo);
135
// If region name already exists, skip it instead of terminating the program
137
printf("Line #%d: synonym \"%s\" exists in region name db\n", lineNo, name.c_str());
141
foo = chkSynonymStr(dbHome, sDbName, name, name_space);
143
printf("Line #%d: fails to check availability of %s in synonym db\n", lineNo, name.c_str());
146
// If synonym already exists, skip it instead of terminating the program
148
printf("Line #%d: synonym \"%s\" exists in synonym db\n", lineNo, name.c_str());
152
long sID = getAdmin_db(dbHome, aDbName, admin_sField);
154
printf("Line #%d: fails to get next available synonym ID from admin db\n", lineNo);
161
sData.setPrimary(primary);
162
sData.setNameSpace(name_space);
163
sData.setAddDate(time(NULL));
164
sData.setCreator(username);
165
sData.setComments(comments);
167
Dbt key(&sID, sizeof(long));
168
void *buff = sData.getBuffer();
169
int size = sData.getBufferSize();
170
Dbt data(buff, size);
172
myDB sDB(dbHome, sDbName);
174
foo = sDB.getDb().put(NULL, &key, &data, DB_NOOVERWRITE);
175
if (foo == DB_KEYEXIST) {
176
printf("Line %d: key %ld exists\n", lineNo, sID);
180
catch(DbException &e) {
181
sDB.getDb().err(e.get_errno(), "Error in addSynRec()");
184
catch(exception &e) {
185
sDB.getDb().errx("Error in addSynRec(): %s", e.what());
190
foo = setAdmin_db(dbHome, aDbName, admin_sField, sID);
192
printf("Line #%d: fails to set next available synonym ID (%ld) from admin db\n", lineNo, sID);
199
/* Parse relationship field and add it into relationship db */
200
void addRelationship(int lineNo, string regionName, string relStr)
202
// "NONE" means no relationship
203
if (relStr == "NONE")
205
// "equiv" means it is equivalent to the same region in NN2002
206
if (relStr == "equiv") {
207
string r2Name = toLowerCase(regionName);
208
int foo = addRelation_db(dbHome, rDbName, rrDbName, aDbName, username,
209
regionName, name_space, r2Name, "NN2002", "equiv");
211
printf("Error code %d: fails to add relationship in line #%d: %s\n", foo, lineNo, relStr.c_str());
214
// "equiv <string>" means it is equivalent to a region in NN2002 whose name is the string
215
if (relStr.length() > 6 && relStr.substr(0, 6) == "equiv ") {
216
int strLen = relStr.length();
217
string r2Name = relStr.substr(6, strLen - 6);
218
int foo = addRelation_db(dbHome, rDbName, rrDbName, aDbName, username,
219
regionName, name_space, r2Name, "NN2002", "equiv");
221
printf("Error code %d: fails to add relationship in line #%d: %s\n", foo, lineNo, relStr.c_str());
224
// deal with "includes <>; includes <> ..."
225
vector<string> relList = parseRelStr(relStr, "; ");
226
for (uint i = 0; i < relList.size(); i++) {
227
if (relList[i].length() > 9 && relList[i].substr(0, 9) == "includes ") {
228
int strLen = relList[i].length();
229
string r2Name = relList[i].substr(9, strLen - 9);
230
int foo = addRelation_db(dbHome, rDbName, rrDbName, aDbName, username,
231
r2Name, "NN2002", regionName, name_space, "part-of");
233
printf("Error code %d: fails to add relationship for %s in line #%d: %s\n",
234
foo, regionName.c_str(), lineNo, relList[i].c_str());
237
printf("Unknown relationship in line #%d: %s\n", lineNo, relList[i].c_str());
241
/* THis function parses an input string and separates it into individual sections.
242
* The first argument is the input string, the second is the separator string. */
243
vector<string> parseRelStr(string inputStr, string sepStr)
245
string foo = inputStr;
246
vector <string> sections;
247
int strLen = foo.length();
248
int sepLen = sepStr.length();
249
if (strLen == 0 || sepLen == 0) {
250
sections.push_back(inputStr);
255
while (foo.find(sepStr) != string::npos) {
256
int sepPost = foo.find(sepStr);
260
bar = foo.substr(0, sepPost);
261
sections.push_back(bar);
262
foo = foo.substr(sepPost + sepLen, foo.length() - sepPost - sepLen);
264
sections.push_back(foo);