-
Notifications
You must be signed in to change notification settings - Fork 0
/
TRAP.cpp
411 lines (336 loc) · 11.5 KB
/
TRAP.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
//AUTHOR: Helge Roider
//INSTITUTION: Max Planck Institute for Molecular Genetics - Berlin
//DATE: 16/07/2006
// $Id: TRAP.cpp 339 2012-09-25 09:16:14Z jbao $
//INPUT: 1. TRANSFAC MATRIX, 2. FASTA FILE; optional: 3. LAMBDA and 4.ln(R0)
//OUTPUT: returns, for a given sequence, <N> combined from forward and reverse strand and sequence length.
#include<stdio.h>
#include<cmath>
#include<stdlib.h>
#include<fstream>
#include<iostream>
#include<string>
#include<iomanip>
#include<vector>
//#include <boost/multi_array.hpp>
using namespace std;
const int A = 0;
const int C = 1;
const int G = 2;
const int T = 3;
//typedef boost::multi_array<double, 2> array_type;
//typedef array_type::index array_index;
//typedef vector<vector<double> > My2DArray;
int main(int argc, char *argv[]){
if(argc < 3){
cout << "W A R N I N G ! Input parameter missing!\n";
cout << "1. TRANSFAC MATRIX, 2. FASTA FILE; optional: 3. LAMBDA, 4.ln(R0)\n";
exit(1);
}
//parameters
double lambda;//lambda
double Rmax;//R0 parameter
if(argc < 5){ //if R0 and/or lambda are not defined use DEFAULT
lambda = 0.7;
}
else{
lambda = strtod(argv[3],NULL);
}
const int Pseudocount = 1;
//GLOBAL YEAST BACKGROUND
//const double gc_content = 0.35;
//const double at_content = 0.65;
const double gc_content = 0.5;
const double at_content = 0.5;
//READ FASTA FILE
//----------------------------------------------------------------
ifstream fasta(argv[2]);
if(!fasta){
cout << "FASTA file not opened\n";
}
string newID, seqID;
int seqlength = 0;
int sequenceheader = 0;
int firstrun = 1; //indicates first header
string bases; //complete sequence
string newbases; //sequence in each row of fasta file
while(!fasta.eof()){ //GO THROUGH FASTA FILE
getline(fasta,newbases);
if(newbases.substr(0,1) == ">"){ //NEW SEQUENCE HEADER
sequenceheader = 1;
newID = newbases.substr(1);
}
if(sequenceheader == 0){ //SEQUENCE LINE
bases = bases + newbases;
seqlength = bases.length();
continue;
}
if(firstrun == 1){ //SKIP ANNOTATION FOR FIRST HEADER
firstrun = 0;
sequenceheader = 0;
seqID = newID;
continue;
}
}//loop over fasta file
fasta.close();
//RESET SEQUENCE VARIABLES
//sequenceheader = 0;
//seqID = newID;
//bases = "";
//READ TRANSFAC FILE
//----------------------------------------------------------------
ifstream transfac(argv[1]);
if(!transfac){
cout << "TRANSFAC file not opened\n";
}
double pwm[35][4], complement[35][4]; //matrices for forward and reverse strand
//My2DArray pwm, complement; //matrices for forward and reverse strand
string word[5]; //elements in row
double value[4]; //count + pseudocount
double max = 0; //consensus base count
string row; //transfac file rows
string delimiters = " \t"; //word seperators in each line
int motiflength, reading, n;
int start, end;
string matrix_id;
double info, sum, p;
//array_type A(boost::extents[35][4]);
//vector<My2DArray> all_pwm, all_complement;
while(!transfac.eof()){
getline(transfac,row);
start = row.find_first_not_of(delimiters);
int i = 0;
while((start != string::npos)&&(i<5)){ //split row into tokens - word[]
end = row.find_first_of(delimiters,start+1);
if(end == string::npos){
end = row.length();
}
word[i] = row.substr(start,end-start);
i++;
start = row.find_first_not_of(delimiters,end+1);
}
if((reading == 1)&&(word[0] == "XX")){ //end of matrix is reached
reading = 0;
motiflength++;
for(int m = 0; m < motiflength; m++){ //create complement to matrix
complement[motiflength-m-1][A] = pwm[m][T];
complement[motiflength-m-1][C] = pwm[m][G];
complement[motiflength-m-1][G] = pwm[m][C];
complement[motiflength-m-1][T] = pwm[m][A];
//vector<double> tmp;
//tmp.push_back(pwm[m][T]);
//tmp.push_back(pwm[m][G]);
//tmp.push_back(pwm[m][C]);
//tmp.push_back(pwm[m][A]);
//complement.push_back(tmp);
}
//all_pwm.push_back(pwm);
//all_complement.push_back(complement);
//SET PARAMETERS Lambda and R0
//----------------------------------------------------------------
if(argc < 5){ //if R0 and/or lambda are not defined use DEFAULT
Rmax = exp(0.584 * motiflength - 5.66);
}
else{
Rmax = strtod(argv[4],NULL);
Rmax = exp(Rmax);
}
cerr << "Lambda: " << lambda << "\t" << "R0: " << Rmax << "\t" << "MotifLength: " << motiflength << "\n";
// match current motif to seq
double Pstored[seqlength - motiflength + 1];
double P_combined = 0; //only palindrome correction
double P_uncorrected = 0; //uncorrected expected count
//LOOP OVER SEQUENCE
int illegalBase;
cout << matrix_id << "\t";
for(int n = 0; n < seqlength - motiflength + 1; n++){ //LOOP OVER SEQUENCE
double dE_forward = 0;
double dE_compl = 0;
//LOOP OVER MOTIF
for(int m = 0; m < motiflength; m++){ //LOOP OVER MOTIF
int BASE;
illegalBase = 0;
switch(bases[n+m])
{
case 'A':
BASE = 0;break;
case 'a':
BASE = 0;break;
case 'C':
BASE = 1;break;
case 'c':
BASE = 1;break;
case 'G':
BASE = 2;break;
case 'g':
BASE = 2;break;
case 'T':
BASE = 3;break;
case 't':
BASE = 3;break;
case 'N':
illegalBase = 1;break;
case 'n':
illegalBase = 1;break;
default:
cerr << "ILLEGAL CHARACTER " << bases[n+m] << " FOUND!\n Only A/a, T/t, G/g, C/c, N/n are allowed!\n"; illegalBase = 1;
}
dE_forward = dE_forward + pwm[m][BASE];
dE_compl = dE_compl + complement[m][BASE];
}//loop over motif
//CALCULATE P(BOUND) FOR CURRENT SITE
if(illegalBase == 0){
double product = Rmax * exp(-1*dE_forward);
double P_bound_F = product/(1 + product);
product = Rmax * exp(-1*dE_compl);
double P_bound_C = product/(1 + product);
Pstored[n] = P_bound_F + (1 - P_bound_F) * P_bound_C; //correction for forward and reverse strand
P_combined = P_combined + Pstored[n];
//P_uncorrected = P_uncorrected + P_bound_F + P_bound_C;
//cout<<P_bound_F<<"\t"<<P_bound_C<<"\t"<<Pstored[n]<<"\t"<<P_combined<<"\t"<<P_uncorrected<<"\n";
cout << Pstored[n] << ",";
}
else{
Pstored[n] = 0;
}
}//loop over sequence
cout << "\t" << P_combined << "\t" << motiflength << "\t" <<
info << "\n";
}
if(reading == 1){ //generate matrix
n++;
motiflength = n;
//add pseudocounts
value[0] = strtod( word[1].c_str(),NULL ) + Pseudocount;
value[1] = strtod( word[2].c_str(),NULL ) + Pseudocount;
value[2] = strtod( word[3].c_str(),NULL ) + Pseudocount;
value[3] = strtod( word[4].c_str(),NULL ) + Pseudocount;
// prob
sum = 0;
for (int i = 0; i < 4; ++i)
sum += value[i];
for (int i = 0; i < 4; ++i) {
p = value[i] / sum;
info += -p * log2(p);
}
double maxAT, maxCG;
if(value[0] > value[3]){
maxAT = value[0];
}
else{
maxAT = value[3];
}
if(value[1] > value[2]){
maxCG = value[1];
}
else{
maxCG = value[2];
}
if(maxAT > maxCG){
pwm[n][A] = log(maxAT/value[0]) / lambda;
pwm[n][C] = log((maxAT/at_content)*(gc_content/value[1])) / lambda;
pwm[n][G] = log((maxAT/at_content)*(gc_content/value[2])) / lambda;
pwm[n][T] = log(maxAT/value[3]) / lambda;
//vector<double> tmp;
//tmp.push_back(log(maxAT/value[0]) / lambda);
//tmp.push_back(log((maxAT/at_content)*(gc_content/value[1])) / lambda);
//tmp.push_back(log((maxAT/at_content)*(gc_content/value[2])) / lambda);
//tmp.push_back(log(maxAT/value[3]) / lambda);
//pwm.push_back(tmp);
}
else{
pwm[n][A] = log((maxCG/gc_content)*(at_content/value[0])) / lambda;
pwm[n][C] = log(maxCG/value[1]) / lambda;
pwm[n][G] = log(maxCG/value[2]) / lambda;
pwm[n][T] = log((maxCG/gc_content)*(at_content/value[3])) / lambda;
//vector<double> tmp;
//tmp.push_back(log((maxCG/gc_content)*(at_content/value[0])) / lambda);
//tmp.push_back(log(maxCG/value[1]) / lambda);
//tmp.push_back(log(maxCG/value[2]) / lambda);
//tmp.push_back(log((maxCG/gc_content)*(at_content/value[3])) / lambda);
//pwm.push_back(tmp);
}
if(maxAT == maxCG){
pwm[n][A] = log(maxAT/value[0]) / lambda;
pwm[n][C] = log(maxAT/value[1]) / lambda;
pwm[n][G] = log(maxAT/value[2]) / lambda;
pwm[n][T] = log(maxAT/value[3]) / lambda;
//vector<double> tmp;
//tmp.push_back(log(maxAT/value[0]) / lambda);
//tmp.push_back(log(maxAT/value[1]) / lambda);
//tmp.push_back(log(maxAT/value[2]) / lambda);
//tmp.push_back(log(maxAT/value[3]) / lambda);
//pwm.push_back(tmp);
}
}
if(word[0] == "P0"){ //start of matrix
n = -1;
reading = 1;
info = 0;
}
if(word[0] == "ID"){ //matrix ID
matrix_id = word[1];
}
}
transfac.close();
/*
//LAST SEQUENCE
double Pstored[seqlength - motiflength + 1];
double P_combined = 0; //only palindrome correction
double P_uncorrected = 0; //uncorrected expected count
//LOOP OVER SEQUENCE
int illegalBase;
for(int n = 0; n < seqlength - motiflength + 1; n++){ //LOOP OVER SEQUENCE
double dE_forward = 0;
double dE_compl = 0;
//LOOP OVER MOTIF
for(int m = 0; m < motiflength; m++){ //LOOP OVER MOTIF
int BASE;
illegalBase = 0;
switch(bases[n+m])
{
case 'A':
BASE = 0;break;
case 'a':
BASE = 0;break;
case 'C':
BASE = 1;break;
case 'c':
BASE = 1;break;
case 'G':
BASE = 2;break;
case 'g':
BASE = 2;break;
case 'T':
BASE = 3;break;
case 't':
BASE = 3;break;
case 'N':
illegalBase = 1;break;
case 'n':
illegalBase = 1;break;
default:
cerr << "ILLEGAL CHARACTER FOUND!\n Only A/a, T/t, G/g, C/c, N/n are allowed!\n"; illegalBase = 1;
}
dE_forward = dE_forward + pwm[m][BASE];
dE_compl = dE_compl + complement[m][BASE];
}//loop over motif
//CALCULATE P(BOUND) FOR CURRENT SITE
if(illegalBase == 0){
double product = Rmax * exp(-1*dE_forward);
double P_bound_F = product/(1 + product);
product = Rmax * exp(-1*dE_compl);
double P_bound_C = product/(1 + product);
Pstored[n] = P_bound_F + (1 - P_bound_F) * P_bound_C; //correction for forward and reverse strand
P_combined = P_combined + Pstored[n];
//P_uncorrected = P_uncorrected + P_bound_F + P_bound_C;
//cout<<P_bound_F<<"\t"<<P_bound_C<<"\t"<<Pstored[n]<<"\t"<<P_combined<<"\t"<<P_uncorrected<<"\n";
}
else{
Pstored[n] = 0;
}
}//loop over sequence
cout << matrix_id[0] << "\t" << P_combined << "\t" << seqlength << "\n";
*/
return 0;
}