-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinwebBinaryNetwork.cpp
More file actions
186 lines (155 loc) · 4.73 KB
/
Copy pathinwebBinaryNetwork.cpp
File metadata and controls
186 lines (155 loc) · 4.73 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
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <map>
#include <iterator>
#include <dirent.h>
#include <stdint.h>
#include <stdlib.h>
using namespace std;
vector<string> getFilesFromDirectory(string path = ".") {
unsigned char isFile = 0x8;
unsigned char isFolder = 0x4;
DIR* dir;
dirent* pdir;
vector<string> files;
dir = opendir(path.c_str());
while (pdir = readdir(dir)) {
//skip hidden files and special directories
if(pdir->d_name[0] == '.'){continue;}
//skip directories
if(pdir->d_type != isFile){continue;}
//take only the _InWeb3_HUGO_sym files
//string filename(pdir->d_name);
//string search("_dataLocAnnot");
//size_t found = filename.find(search);
//if(found != string::npos){continue;}
//search = "sif";
//found = filename.find(search);
//
//if(found == string::npos){continue;}
files.push_back(pdir->d_name);
}
return files;
}
int loadMappingFile(string mapping, vector<string>* int_id_, map<string, unsigned int>* id_int_){
ifstream in_mapping(mapping.c_str());
if(!in_mapping){
cerr << "Mapping file not found: " << endl << mapping.c_str() << endl;
return 0;
}
unsigned i = 0;
string line;
while(getline(in_mapping, line)){
vector<string> tokens;
if(line[0] == '#'){continue;}
istringstream iss(line);
string token;
while(getline(iss, token, '\t')){
tokens.push_back(token);
}
unsigned int newInt = (unsigned int)atoi(tokens[0].c_str());
if(newInt >= int_id_->size()){
int_id_->resize(newInt+1);
}
(*int_id_)[newInt] = tokens[1];
id_int_->insert(pair<string, unsigned int>(tokens[1], newInt));
}
return 1;
}
int main (int argc, char* argv[]){
vector<string> int_id;
map<string, unsigned int> id_int;
if(argc<5){
cout << "Usage: " << endl << "./inwebBinaryNetwork SOURCEDIR TARGETDIR MAPPING_FILE ACTION" << endl <<
"ACTION: 1 = create binary network" << endl <<
" 2 = create plain text network" << endl;
return 0;
}
string NETWORKDIR = argv[1];
string OUTDIR = argv[2];
string MAPPINGFILE = argv[3];
int ACTION = atoi(argv[4]);
vector<string> files;
files = getFilesFromDirectory(NETWORKDIR);
cerr << files.size() << " networks to transform" << endl;
int size = files.size();
static uint16_t prot_prot [23000][23000];
unsigned long OFFSET = 0;
if(!loadMappingFile(MAPPINGFILE, &int_id, &id_int)){
return 0;
}
//set iterator
for(unsigned long i=OFFSET;i<size+OFFSET;i++){
//====================\\
// \\
// create binary file \\
// \\
//====================\\
if(ACTION == 1){
for(unsigned j=0;j<23000;j++){
for(unsigned k=0;k<23000;k++){
prot_prot[j][k]=0;
}
}
ostringstream file;
file << NETWORKDIR << files[i-OFFSET];
ifstream in(file.str().c_str());
if(!in){
cerr << "Networkfile not found: " << endl << file.str() << endl;
return 0;
}
ostringstream filename;
filename << OUTDIR << "/" << files[i-OFFSET] << ".bin";
ofstream o(filename.str().c_str(),ios::binary);
string line;
while(getline(in, line)){
vector<string> tokens;
if(line[0] == '#'){continue;}
istringstream iss(line);
string token;
while(getline(iss, token, '-')){
tokens.push_back(token);
}
uint16_t id1 = (uint16_t)atof(tokens[0].c_str());
uint16_t id2 = (uint16_t)atof(tokens[1].c_str());
if(prot_prot[id1][id2] == 1){ continue; }
prot_prot[id1][id2] = 1;
prot_prot[id2][id1] = 1;
o.write((char*)&id1,sizeof(id1));
o.write((char*)&id2,sizeof(id2));
}
in.close();
o.close();
//========================\\
// \\
// create plain text file \\
// \\
//========================\\
}else{
ostringstream file;
file << NETWORKDIR << files[i-OFFSET];
ifstream in(file.str().c_str());
cerr << file.str().c_str() << endl;
ostringstream filename;
filename << OUTDIR << "/" << files[i-OFFSET] << ".txt";
ofstream o(filename.str().c_str(),ios::binary);
in.seekg (0, in.end);
long length = (long)in.tellg();
in.seekg (0, in.beg);
short int1, int2;
while((long)in.tellg() != length){
in.read((char*)&int1, 2);
in.read((char*)&int2, 2);
//Check for non-existing mappings
//cout << int1 << "\t" << int_id[int1] << "\t" << int2 << "\t" << int_id[int2] << endl;
o << int_id[int1] << "\t" << int_id[int2] << endl;
}
in.close();
o.close();
}
}
return 0;
}