-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.cpp
487 lines (423 loc) · 13.1 KB
/
functions.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
/*
* functions.cpp
* viralMC
*
* Created by Andrew Ferguson on 4/15/11.
* Copyright 2011 __MyCompanyName__. All rights reserved.
*
*/
#include "functions.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>
#include <sys/time.h>
double get_wall_time(){
struct timeval time;
if (gettimeofday(&time,NULL)){
// handle error
return 0;
}
return (double)time.tv_sec + (double)time.tv_usec * .000001;
}
double get_cpu_time(){
return (double)clock() / CLOCKS_PER_SEC;
}
int load_seq(std::string fstr, int seq[], std::vector<long>& nRes, std::vector< std::vector<long> >& resIdx, long m) {
std::ifstream fin;
fin.open(fstr.c_str());
if (!fin) {
std::cout << "Cannot open input file " << fstr << "; Using WT." << std::endl;
for(long i=0; i<m; i++){
seq[i] = 0;
}
return 0;
}
std::string readStr;
std::getline(fin,readStr);
std::stringstream readStrStrm(readStr);
std::string word;
for(long i=0; i<m; i++) {
if(readStrStrm.eof()){
std::cerr << "Encountered end of line read from " << fstr << "before fully populationg arry; aborting." << std::endl;
exit(-1);
}
readStrStrm >> word;
seq[i] = atof(word.c_str());
for(long j=0; j<nRes[i]; j++){
if(seq[i] == resIdx[i][j]){
seq[i] = j;
break;
}
}
}
if (!readStrStrm.eof()) {
std::cerr << "Elements remain in " << fstr << " after populating array; aborting." << std::endl;
exit(-1);
}
fin.close();
return 0;
}
int load_epitopes(std::string fstr, std::vector<long>& epitope_start, std::vector<long>& epitope_end, std::vector< std::vector<double> >& chi, long n, std::vector< std::vector<long> >& resIdx, std::vector<long>& nRes, std::vector<state_type>& Tcells, int rep_lim) {
std::ifstream fin;
fin.open(fstr.c_str());
if (!fin) {
std::cerr << "Cannot open input file " << fstr << "; aborting." << std::endl;
exit(-1);
}
std::string epi_file;
for(long i=0; i<n; i++) {
if (fin.eof()) {
std::cerr << "Prematurely encountered eof in " << fstr << "; aborting." << std::endl;
exit(-1);
}
std::ifstream epi_in;
{
std::getline(fin, epi_file);
std::stringstream readStrStrm(epi_file);
std::string word;
if(readStrStrm >> word){
epi_in.open(word.c_str());
if (!epi_in) {
std::cerr << "Cannot open input file " << epi_file << "; aborting." << std::endl;
exit(-1);
}
} else {
std::cerr << "Not enough lines in " << fstr << "; aborting." << std::endl;
exit(-1);
}
Tcells[i][0] = 200.0;//500;
Tcells[i][rep_lim+1] = 0.0;
for(long j=1; j<=rep_lim; j++){
Tcells[i][j] = 0.0;
}
if(readStrStrm >> word){
Tcells[i][0] = atof(word.c_str());
} else {
std::cerr << "Number of cells for epitope " << i << " not given. Using 200.0 Naive, 0.0 Effector, and 0.0 Memory." << std::endl;
}
if(readStrStrm >> word){
Tcells[i][1] = atof(word.c_str());
}
if(readStrStrm >> word){
Tcells[i][rep_lim+1] = atof(word.c_str());
}
if(readStrStrm >> word) {
std::cerr << "Extra entries for epitope " << i << "; aborting." << std::endl;
exit(-1);
}
}
std::string readStr;
std::getline(epi_in,readStr);
std::stringstream readStrStrm(readStr);
std::string word;
double average = 0;
if(readStrStrm >> word){
average = atof(word.c_str());
} else {
std::cerr << "Empty line in " << epi_file << "; aborting." << std::endl;
exit(-1);
}
word = "";
readStrStrm >> word;
if(!word.empty()){
std::cerr << "Extra characters on the first line of " << epi_file << "; aborting." << std::endl;
exit(-1);
}
size_t pos = 0;
pos = epi_file.find_last_of('/');
epi_file = epi_file.substr(pos+1);
std::string token;
pos = epi_file.find('_');
epi_file.erase(0, pos + 1);
pos = epi_file.find('_');
token = epi_file.substr(0, pos);
pos = token.find('-');
epitope_start[i] = atoi(token.substr(0,pos).c_str())-1;
epitope_end[i] = atoi(token.substr(pos+1).c_str());
int place_value[epitope_end[i]-epitope_start[i]];
int size = 1;
int count = 0;
for(int j=epitope_start[i]; j<epitope_end[i]; j++){
place_value[count] = size;
size *= nRes[j];
count ++;
}
(chi[i]).resize(size, average);
int j=0;
while(std::getline(epi_in,readStr)){
int index = 0;
readStrStrm.clear();
readStrStrm.str(readStr);
for(int k=0; k<epitope_end[i]-epitope_start[i]; k++){
if(!(readStrStrm >> word)){
std::cerr << "Too few residues on line " << j+2 << " of " << epi_file << "; aborting." << std::endl;
exit(-1);
}
for(int p=0; p<resIdx[epitope_start[i]+k].size(); p++){
if(atoi(word.c_str()) == resIdx[epitope_start[i]+k][p]){
index += place_value[k]*p;
break;
}
}
}
if(readStrStrm >> word){
chi[i][index] = atof(word.c_str());
} else {
std::cerr << "No chi on line " << j+2 << " of " << epi_file << "; aborting." << std::endl;
exit(-1);
}
word = "";
readStrStrm >> word;
if(!word.empty()){
std::cerr << "Too many residues on line " << j+2 << " of " << epi_file << "; aborting." << std::endl;
exit(-1);
}
j++;
}
std::cout << i+1 << "th epitope starts at " << epitope_start[i] << " and ends at " << epitope_end[i] << std::endl;
while(!epi_in.eof()) {
std::getline(epi_in,readStr);
if(!readStr.empty()){
std::cerr << "Elements remain in " << epi_file << " after populating array; aborting." << std::endl;
exit(-1);
}
}
}
while(!fin.eof()) {
std::getline(fin,epi_file);
if(!epi_file.empty()){
std::cerr << "Elements remain in " << fstr << " after populating array; aborting." << std::endl;
exit(-1);
}
}
fin.close();
return 0;
}
int load_epitopes(std::string fstr, std::vector<long>& epitope_start, std::vector<long>& epitope_end, std::vector< std::vector<int> >& epitopes, long n, std::vector<double>& chi, std::vector< std::vector<long> >& resIdx) {
std::ifstream fin;
fin.open(fstr.c_str());
if (!fin) {
std::cerr << "Cannot open input file " << fstr << "; aborting." << std::endl;
exit(-1);
}
std::string readStr;
for(long i=0; i<n; i++) {
if (fin.eof()) {
std::cerr << "Prematurely encountered eof in " << fstr << "; aborting." << std::endl;
exit(-1);
}
std::getline(fin,readStr);
std::stringstream readStrStrm(readStr);
std::string word;
if(readStrStrm >> word){
chi[i] = atof(word.c_str());
} else {
std::cerr << "Empty line in " << fstr << "; aborting." << std::endl;
}
if(readStrStrm >> word){
epitope_start[i] = atol(word.c_str());
} else {
std::cerr << "Line ended early in " << fstr << "; aborting." << std::endl;
}
if(readStrStrm >> word){
epitope_end[i] = atol(word.c_str());
} else {
std::cerr << "Missing the second position on line " << i+1 << " of " << fstr << "; aborting." << std::endl;
}
int counter = 0;
while (readStrStrm >> word) {
(epitopes[i]).push_back(atoi(word.c_str()));
counter++;
}
if(counter > epitope_end[i]-epitope_start[i]+1){
std::cerr << "Too many residues on line " << i+1 << " of " << fstr << "; aborting." << std::endl;
} else if(counter <epitope_end[i]-epitope_start[i]+1){
std::cerr << "Too few residues on line " << i+1 << " of " << fstr << "; aborting." << std::endl;
}
}
if (!fin.eof()) {
std::cerr << "Elements remain in " << fstr << " after populating array; aborting." << std::endl;
exit(-1);
}
fin.close();
for(long i=0; i<n; i++){
for(long j=0; j<epitope_end[i]-epitope_start[i]+1; j++){
for(long k=0; k<resIdx[epitope_start[i]+j-1].size(); k++){
if(epitopes[i][j] == resIdx[epitope_start[i]+j-1][k]){
epitopes[i][j] = k;
break;
}
}
}
}
return 0;
}
int load_resIdx(std::string fstr, std::vector<long>& nRes, std::vector< std::vector<long> >& resIdx, long m) {
std::ifstream fin;
fin.open(fstr.c_str());
if (!fin) {
std::cerr << "Cannot open input file " << fstr << "; aborting." << std::endl;
exit(-1);
}
std::string readStr;
for(long i=0; i!=m; i++) {
if (fin.eof()) {
std::cerr << "Prematurely encountered eof in " << fstr << "; aborting." << std::endl;
exit(-1);
}
std::getline(fin,readStr);
std::stringstream readStrStrm(readStr);
std::string word;
readStrStrm >> word; // dumping position index
long cntr=0;
while (readStrStrm >> word) {
(resIdx[i]).push_back(atof(word.c_str()));
cntr++;
}
nRes[i]=cntr;
}
if (!fin.eof()) {
std::cerr << "Elements remain in " << fstr << " after populating array; aborting." << std::endl;
exit(-1);
}
fin.close();
return 0;
}
int load_X1(std::string fstr, std::vector<long>& nRes, std::vector< std::vector<double> >& X1, long m) {
std::ifstream fin;
fin.open(fstr.c_str());
if (!fin) {
std::cerr << "Cannot open input file " << fstr << "; aborting." << std::endl;
exit(-1);
}
std::string readStr;
std::getline(fin,readStr);
std::stringstream readStrStrm(readStr);
std::string word;
for(long i=0; i!=m; i++) {
for(long p=0; p!=nRes[i]; p++) {
if (readStrStrm.eof()) {
std::cerr << "Encountered end of line read from " << fstr << " before fully populating array; aborting." << std::endl;
exit(-1);
}
readStrStrm >> word;
(X1[i][p]) = atof(word.c_str());
}
}
if (!readStrStrm.eof()) {
std::cerr << "Elements remain in line read from " << fstr << " after populating array; aborting." << std::endl;
exit(-1);
}
if (!fin.eof()) {
std::cerr << "More than one line detected in " << fstr << "; aborting." << std::endl;
exit(-1);
}
fin.close();
return 0;
}
int load_X2(std::string fstr, std::vector<long>& nRes, std::vector< std::vector< std::vector< std::vector<double> > > >& X2, long m) {
std::ifstream fin;
fin.open(fstr.c_str());
if (!fin) {
std::cerr << "Cannot open input file " << fstr << "; aborting." << std::endl;
exit(-1);
}
std::string readStr;
std::getline(fin,readStr);
std::stringstream readStrStrm(readStr);
std::string word;
for(long i=0; i!=m; i++) {
for(long j=i+1; j!=m; j++) {
for(long q=0; q!=nRes[j]; q++) {
for(long p=0; p!=nRes[i]; p++) {
if (readStrStrm.eof()) {
std::cerr << "Encountered end of line read from " << fstr << " before fully populating array; aborting." << std::endl;
exit(-1);
}
readStrStrm >> word;
X2[i][j][p][q] = atof(word.c_str()); // column major order file load
}
}
}
}
if (!readStrStrm.eof()) {
std::cerr << "Elements remain in line read from " << fstr << " after populating array; aborting." << std::endl;
exit(-1);
}
if (!fin.eof()) {
std::cerr << "More than one line detected in " << fstr << "; aborting." << std::endl;
exit(-1);
}
fin.close();
return 0;
}
double energy(std::vector<int8_t>& sequence, long m, std::vector< std::vector<double> >& h, std::vector< std::vector< std::vector< std::vector<double> > > >& J){
double E=0;
for (long i=0; i<m; i++) {
E = E + h[i][sequence[i]];
for (long j=i+1; j<m; j++){
E = E + J[i][j][sequence[i]][sequence[j]];
}
}
return E;
}
double energy_P(std::vector<int8_t>& sequence, long m, std::vector< std::vector<double> >& h, std::vector< std::vector< std::vector< std::vector<double> > > >& J){
double E=0;
#pragma omp parallel shared(E)
{
int size = omp_get_num_threads();
int rank = omp_get_thread_num();
int len = (m-1)/size + 1;
int begin = rank * len;
int end = begin + len;
if(end > m)
end = m;
double E_P = 0;
for (long i=begin; i<end; i++)
{
E_P = E_P + h[i][sequence[i]];
for (long j=i+1; j<m; j++)
{
E = E + J[i][j][sequence[i]][sequence[j]];
}
}
#pragma omp atomic
E += E_P;
#pragma omp barrier
}
return E;
}
double Tresponse(double affinity, double num_Ecells, double b, long N, double aff_rate){
double k = .01;//3;
double mid = .1*N;
return b*affinity*1.0/(1.0 + exp(-k*(num_Ecells-mid)));
}
void init_energy_index(int m, int size, std::vector<std::vector<int>> & arr_idx){
arr_idx.resize(size);
int i = 0;
bool rev = true;
while(i < m) {
for(int j = 0; j < size; j++) {
if(i < m)
arr_idx[j].push_back(i);
else
break;
i++;
}
}
}
int check_duplicate(int* arr, int size){
std::sort(arr, arr+size);
int prev = arr[0];
for(int i = 1; i < size; i++)
{
if(arr[i] == prev) return i;
else prev = arr[i];
}
return 0;
}