-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathPSCM_to_PSEM.cpp
executable file
·270 lines (214 loc) · 7.83 KB
/
PSCM_to_PSEM.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
//AUTHOR: Helge Roider
//INSTITUTION: Max Planck Institute for Molecular Genetics - Berlin
//DATE: 20/11/2006
//takes a count matrix in TRANSFAC format, adds pseudocounts and removes uninformative positions
//and then converts the modified count matrix into an energy matrix
//Modified by Marcel Schulz and Florian Schmidt
//Cluster of Excellence, Saarland University, 2016
#include<stdio.h>
#include<cmath>
#include<stdlib.h>
#include<fstream>
#include<iostream>
#include<string.h>
#include<iomanip>
using namespace std;
const int A = 0;
const int C = 1;
const int G = 2;
const int T = 3;
int main(int argc, char *argv[]){
//--------------------------------------------------------------
//PARAMETERS
//--------------------------------------------------------------
double lambda = 0.7;
//double slope = 0.66; // slope * effectivelength[f]
//double intercept = -6.6;
double slope = 0.584;
double intercept = -5.66;
double lnR0; //effectifelength * slope + intercept
//GLOBAL GENOMIC BACKGROUND
const double gc_content = 0.43;
const double at_content = 1 - gc_content;
cout << "#/Lambda=" << lambda << "\t/Regression_slope=" << slope << "\t/Regression_intercept=" << intercept << "\t/GC_content=" << gc_content << "\t/Pseudocount=maximal_row_counts/counts_in_row\n";
//---------------------------------------------------------------
//VARIABLES
//---------------------------------------------------------------
const int numoffactors = 16000;
const int numofpositions = 50;
double *** pwm;
double *** complement;
pwm = new double ** [numoffactors];
complement = new double ** [numoffactors];
for(int i = 0; i < numoffactors; i++){
pwm[i] = new double * [numofpositions];
complement[i] = new double * [numofpositions];
for(int j = 0; j < numofpositions; j++){
pwm[i][j] = new double[4];
complement[i][j] = new double[4];
}
}
// double pwm[numoffactors][numofpositions][4]; // energy matrix forward strand
//double complement[numoffactors][numofpositions][4]; // energy matrix reverse strand
int corelength[numoffactors];
int factors = -1;
string factornames[numoffactors];
//----------------------------------------------------------------
//READ TRANSFAC FILE
//----------------------------------------------------------------
ifstream transfac(argv[1]);
if(!transfac){
cout << "TRANSFAC file not opened\n";
exit(1);
}
//matrix variables
double ** entropy;
double *** matrix;
matrix = new double ** [numoffactors];
entropy = new double * [numoffactors];
for(int i = 0; i < numoffactors; i++){
matrix[i] = new double * [numofpositions];
entropy[i] = new double [numofpositions];
for(int j = 0; j < numofpositions; j++){
matrix[i][j] = new double[4];
}
}
// double matrix[numoffactors][numofpositions][4]; // read in matrix
//double entropy[numoffactors][numofpositions]; // entropy of positions
double Pseudocount; //count dependent
int position;
int motiflength[numoffactors];
int effectivelength;
double maxcount[numoffactors];
double rowsum[numoffactors][numofpositions];
//reading file variables
string word[5]; //elements in row
double max = 0; //consensus base count
string row; //transfac file rows
string delimiters = " \t"; //word seperators in each line
int reading;
int start, end;
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;
}
if(reading == 1){ //generate matrix
matrix[factors][position][A] = strtod(word[1].c_str(),NULL);
matrix[factors][position][C] = strtod(word[2].c_str(),NULL);
matrix[factors][position][G] = strtod(word[3].c_str(),NULL);
matrix[factors][position][T] = strtod(word[4].c_str(),NULL);
rowsum[factors][position] = strtod(word[1].c_str(),NULL)+strtod(word[2].c_str(),NULL)+strtod(word[3].c_str(),NULL)+strtod(word[4].c_str(),NULL);
if(rowsum[factors][position] > maxcount[factors]){
maxcount[factors] = rowsum[factors][position];
}
motiflength[factors]++;
position++;
}
if(word[0] == "P0"){ //start of matrix
factors++;
reading = 1;
position = 0;
motiflength[factors] = 0;
maxcount[factors] = 0;
}
if(word[0] == "ID"){
factornames[factors + 1] = word[1]+"\t"+word[2];
}
}
transfac.close();
//------------------------------------------------------------
//MAKE FORWARD AND REVERSE MATRIX
//------------------------------------------------------------
for(int f = 0; f <= factors; f++){
for(int p = 0; p < motiflength[f]; p++){
// Pseudocount = maxcount[f] / rowsum[f][p];
Pseudocount = 1;
matrix[f][p][A] = matrix[f][p][A] + Pseudocount;
matrix[f][p][C] = matrix[f][p][C] + Pseudocount;
matrix[f][p][G] = matrix[f][p][G] + Pseudocount;
matrix[f][p][T] = matrix[f][p][T] + Pseudocount;
entropy[f][p] = 0;
double prob, de;
for(int b = 0; b < 4; b++){
prob = matrix[f][p][b] / (rowsum[f][p] + 4 * Pseudocount);
if(prob > 0){de = prob * log(4 * prob) / log(2.00);}
else{de = 0;}
entropy[f][p] = entropy[f][p] + de;
}
double maxAT, maxCG;
if(matrix[f][p][A] > matrix[f][p][T]){maxAT = matrix[f][p][A];}
else{maxAT = matrix[f][p][T];}
if(matrix[f][p][C] > matrix[f][p][G]){maxCG = matrix[f][p][C];}
else{maxCG = matrix[f][p][G];}
if(maxAT > maxCG){
matrix[f][p][A] = log(maxAT/matrix[f][p][A]) / lambda;
matrix[f][p][T] = log(maxAT/matrix[f][p][T]) / lambda;
matrix[f][p][C] = log((maxAT/at_content)*(gc_content/matrix[f][p][C])) / lambda;
matrix[f][p][G] = log((maxAT/at_content)*(gc_content/matrix[f][p][G])) / lambda;
}
if(maxAT < maxCG){
matrix[f][p][C] = log(maxCG/matrix[f][p][C]) / lambda;
matrix[f][p][G] = log(maxCG/matrix[f][p][G]) / lambda;
matrix[f][p][A] = log((maxCG/gc_content)*(at_content/matrix[f][p][A])) / lambda;
matrix[f][p][T] = log((maxCG/gc_content)*(at_content/matrix[f][p][T])) / lambda;
}
if(maxAT == maxCG){
matrix[f][p][A] = log(maxAT/matrix[f][p][A]) / lambda;
matrix[f][p][C] = log(maxAT/matrix[f][p][C]) / lambda;
matrix[f][p][G] = log(maxAT/matrix[f][p][G]) / lambda;
matrix[f][p][T] = log(maxAT/matrix[f][p][T]) / lambda;
}
}
//DETERMINE INFORMATIVE CORE
int n = 0;
int core = 0;
int pos = 0;
corelength[f] = 0;
for(int p = 0; p < motiflength[f]; p++){
pwm[f][n][A] = matrix[f][p][A];
pwm[f][n][C] = matrix[f][p][C];
pwm[f][n][G] = matrix[f][p][G];
pwm[f][n][T] = matrix[f][p][T];
corelength[f]++;
n++;
}
n = motiflength[f] - 1;
effectivelength=corelength[f];
lnR0 = effectivelength * slope + intercept;
// if(lnR0 < 1e-5){lnR0 = 0;}
//COMPLEMENT MATRIX
if(corelength[f] > 0){
cout << ">" << factornames[f] << "\tlnR0: " << lnR0 << "\n";
for(int p = 0; p < corelength[f]; p++){
cout << pwm[f][p][A] << "\t" << pwm[f][p][C] << "\t" << pwm[f][p][G] << "\t" << pwm[f][p][T] << "\n";
}
}
}//loop over factors
for(int i = 0; i < numoffactors; i++){
for(int j = 0; j < numofpositions; j++){
delete [] matrix[i][j];
delete [] complement[i][j];
delete [] pwm[i][j];
}
delete [] matrix[i];
delete [] entropy[i];
delete [] complement[i];
delete [] pwm[i];
}
delete [] matrix;
delete [] entropy;
delete [] complement;
delete [] pwm;
}