-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathProteinDomains.cpp
More file actions
580 lines (541 loc) · 14.6 KB
/
ProteinDomains.cpp
File metadata and controls
580 lines (541 loc) · 14.6 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
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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
//////////////////////////////////////////////////////////////////////////////////
//
// STAMP version 1.3
//
// Written By: Shaun Mahony
//
// ProteinsDomains.cpp
//
// Started: 4th Feb 2006
//
// Copyright 2007-2015 Shaun Mahony
//
// This file is part of STAMP.
//
// STAMP is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// STAMP is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with STAMP; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
////////////////////////////////////////////////////////////////////////////////////
#include "ProteinDomains.h"
//ProteinMotif constructor
ProteinMotif::ProteinMotif(int l)
{
int i, j;
len=l;
f = new double*[len];
for(i=0; i<l; i++)
{ f[i] = new double[AA];
for(j=0; j<AA; j++)
f[i][j]=0;
}
}
//Print the motif in TRANSFAC format
void ProteinMotif::PrintMotif()
{
int i, j;
printf("DE\t%s\n", name);
for(i=0; i<len; i++){
printf("%d\t", i);
for(j=0; j<AA; j++){
printf("%.4lf\t", f[i][j]);
}printf("\t\t%lf\n", Info(i));
}
printf("XX\n");
}
//the information content of a column
double ProteinMotif::Info(int col)
{
int x;
double sum=0;
for(x=0;x<AA;x++) {
if(f[col][x]>0) {
sum+=f[col][x]*log_2(f[col][x]);
}
}
if(sum!=0)
sum=sum*(-1);
else
sum=log_2(20);
return(log_2(20)-sum);
}
//Protein Motif Destructor
ProteinMotif::~ProteinMotif()
{
for(int i=0; i<len; i++)
{
delete [] f[i];
}
delete [] f;
}
//Read the protein alignment file (must be Pfam format!)
void ProteinDomains::ReadDomains(char* inFileName, Motif** inputMotifs, int numMotifs)
{
int i,j,a;
int lineCount=0;
double currCount=0;
char line[LONG_STR];
int protAlignLen=0;
char name[STR_LEN];
char currMotifName[STR_LEN];
char seq[LONG_STR];
strcpy(inputFN, inFileName);
numDomains = numMotifs;
//Open the file
FILE* in = fopen(inputFN, "r");
if(in==NULL){perror("Cannot open protein domain file");exit(1);}
//Count the number of motifs
while(fgets(line, LONG_STR, in)){
if(strlen(line)>2){//if it's not a newline
lineCount++;
sscanf(line, "%s %s ", name, seq);
protAlignLen = strlen(seq);
}
}
if(protAlignLen==0){
perror("No motif found");
exit(1);
}
domainMotif = new ProteinMotif(protAlignLen);
individualMotifs = new ProteinMotif*[numDomains];
for(i=0; i<numDomains; i++){
individualMotifs[i]=new ProteinMotif(protAlignLen);
}
//Read through again, this time extracting a domain motif for every PSSM in our binding motif set
for(i=0; i<numMotifs; i++){
strcpy(currMotifName, inputMotifs[i]->GetName());
strcpy(individualMotifs[i]->name, currMotifName);
currCount=0;
while(fgets(line, LONG_STR, in)) {
if(strlen(line)>2){//if it's not a newline
sscanf(line, "%s %s ", name, seq);
if(strstr(name, currMotifName)!=NULL){
currCount++;
//Add to the individual motif
protAlignLen = strlen(seq);
for(j=0; j<protAlignLen; j++){
if(char2num(seq[j])!=-1)
individualMotifs[i]->f[j][char2num(seq[j])]++;
}
}
}
}
if(currCount==0){
printf("Error: %s not found in protein motif file... exiting!\n\n", currMotifName);
exit(1);
}else{
for(j=0; j<protAlignLen; j++){
for(a=0; a<AA; a++){
individualMotifs[i]->f[j][a] = individualMotifs[i]->f[j][a]/currCount;
}
}
}
}
//Read through once more, this time just constructing the overall alignment
strcpy(domainMotif->name, "OverallDomain");
fseek(in, 0, SEEK_SET);
while(fgets(line, LONG_STR, in)){
sscanf(line, "%s %s ", name, seq);
if(strlen(line)>2){//if it's not a newline
//Add to the domain motif
protAlignLen = strlen(seq);
for(j=0; j<protAlignLen; j++){
if(char2num(seq[j])!=-1)
domainMotif->f[j][char2num(seq[j])]++;
}
}
}
for(j=0; j<protAlignLen; j++){
for(a=0; a<AA; a++){
domainMotif->f[j][a] = domainMotif->f[j][a]/lineCount;
}
}
}
//Analyse the mutual information between alignments
void ProteinDomains::MutualInformation(MultiAlignRec* pssmAlignment, Motif* alignmentMotif, Motif** inputMotifs, int numMotifs)
{
int i, j, a, b, p, n;
int x = pssmAlignment->GetAlignL();
int y = domainMotif->GetLen();
double currTtl=0;
double ttl=0;
char posPrefName[STR_LEN];
double mTtl;
double RandM;
int *rand_acids;
double *ttl_acids= new double[AA];
double *obs_acids= new double[AA];
double **MIConf;
double **M;
double **Minfo;
double **pairwise_ij;
double log_2_20 = log_2(20);
Motif* posPref = new Motif(20);
//Set up pairwise_ij
pairwise_ij=new double*[B];
for(b=0; b<B; b++){
pairwise_ij[b]=new double[AA];
for(a=0; a<AA; a++)
pairwise_ij[b][a]=0;
}
//set up M, Minfo, & MIConf
M = new double*[x];
Minfo = new double*[x];
MIConf = new double*[x];
for(i=0; i<x; i++){
M[i] = new double [y];
Minfo[i] = new double [y];
MIConf[i] = new double [y];
for(j=0; j<y; j++){
M[i][j]=0;
Minfo[i][j]=0;
MIConf[i][j]=0;
}
}
//Fill in the M values
for(i=0; i<x; i++){
for(j=0; j<y; j++){
//Reset the pairwise frequencies
for(b=0; b<B; b++){
for(a=0; a<AA; a++)
pairwise_ij[b][a]=0;
}
//Calculate pairwise frequencies for these values of i & j
currTtl=0;
for(b=0; b<B; b++){
for(a=0; a<AA; a++){
//go through each pair of PSSM - domain
for(p=0; p<pssmAlignment->GetNumAligned(); p++){
if(pssmAlignment->profileAlignment[p]->f[i][b]>0){
pairwise_ij[b][a] += pssmAlignment->profileAlignment[p]->f[i][b] * individualMotifs[pssmAlignment->alignedIDs[p]]->f[j][a];//printf("%s\t%s\n", pssmAlignment->profileAlignment[p]->GetName(), individualMotifs[pssmAlignment->alignedIDs[p]]->name);//
}else{
pairwise_ij[b][a] +=0.25* individualMotifs[pssmAlignment->alignedIDs[p]]->f[j][a];
}
}
currTtl +=pairwise_ij[b][a];
}
}
//normalise
for(a=0; a<AA; a++){
ttl=0;
for(b=0; b<B; b++){
pairwise_ij[b][a] = pairwise_ij[b][a]/((double)pssmAlignment->GetNumAligned());//printf("%.4lf\t",pairwise_ij[b][a]);
ttl+=pairwise_ij[b][a];
}
for(b=0; b<B; b++){
if(ttl==0)
posPref->f[a][b] =0.25;
else
posPref->f[a][b] = pairwise_ij[b][a]/ttl;
sprintf(posPrefName, "DNA_%d_Protein_%d", i,j);
strcpy(posPref->name, posPrefName);
}
}
//Calculate the actual M values
mTtl=0;
for(b=0; b<B; b++){
for(a=0; a<AA; a++){
if(pairwise_ij[b][a]>0 && alignmentMotif->f[i][b]>0 && domainMotif->f[j][a]>0)
{ mTtl += pairwise_ij[b][a] * (log_2(pairwise_ij[b][a]/(alignmentMotif->f[i][b] * domainMotif->f[j][a])));
}
}
}
Minfo[i][j]=mTtl * (domainMotif->Info(j)/log_2_20);
M[i][j]=mTtl;
}
}
//print out the values of M
printf("MI\t");
for(j=0; j<y; j++)
printf("%d\t", j);
printf("\n");
for(i=0; i<x; i++){
printf("%c\t", alignmentMotif->ColConsensus(i));
for(j=0; j<y; j++){
printf("%.4lf\t", M[i][j]);
}
printf("\n");
}
//print out the alignment motif
alignmentMotif->PrintMotif();
//print out the values of Minfo
/* printf("\n\nMI/Info(p)\t");
for(j=0; j<y; j++)
printf("%d\t", j);
printf("\n");
for(i=0; i<x; i++){
printf("%c\t", alignmentMotif->ColConsensus(i));
for(j=0; j<y; j++){
printf("%.4lf\t", Minfo[i][j]);
}
printf("\n");
}
*/
/*
//////////////////////////////////////////////////////////////////
//This area is not completely verified!!!!!
//Complete Randomization area
rand_acids = new int[pssmAlignment->GetNumAligned()];
RandM = 0;
double totalObs=0;
double GTObs=0;
for(i=0; i<x; i++){
for(j=0; j<y; j++){
GTObs=0;
for(n=0; n<RAND_MUTI_N; n++){
//Reset
for(a=0; a<AA; a++)
{ ttl_acids[a]=0; obs_acids[a]=0;}
for(p=0; p<pssmAlignment->GetNumAligned(); p++)
rand_acids[p]=0;
//Count the number of distinct amino acids in the corresponding amino acid column
int aaDistinct=0;
for(a=0; a<AA; a++)
if(domainMotif->f[j][a]>0)
aaDistinct++;
totalObs=0;
for(p=0; p<pssmAlignment->GetNumAligned(); p++){
//populate observations
for(a=0; a<AA; a++){
obs_acids[a]+=individualMotifs[pssmAlignment->alignedIDs[p]]->f[j][a];
totalObs+=individualMotifs[pssmAlignment->alignedIDs[p]]->f[j][a];
}
}
//normalize
for(a=0; a<AA; a++){
obs_acids[a]=obs_acids[a]/totalObs;
}//make probability distribution
for(a=1; a<AA; a++){
obs_acids[a]=obs_acids[a]+obs_acids[a-1];
}
for(p=0; p<pssmAlignment->GetNumAligned(); p++){
//int dice = (int)floor((double)rand()/RAND_MAX*aaDistinct);
double dice = ((double)rand()/RAND_MAX);
int g=-1;
for(a=0; a<AA; a++){
if(dice<=obs_acids[a]){
g=a; a=AA;
}
}
//printf("%c\t%d\n", pssmAlignment->profileAlignment[p]->ColConsensus(i), g);
ttl_acids[g]++;
rand_acids[p]=g;
}
//normalise
for(a=0; a<AA; a++)
ttl_acids[a]=ttl_acids[a]/(double)pssmAlignment->GetNumAligned();
//Reset the pairwise frequencies
for(b=0; b<B; b++){
for(a=0; a<AA; a++)
pairwise_ij[b][a]=0;
}//Calculate pairwise frequencies for the random pairs
for(p=0; p<pssmAlignment->GetNumAligned(); p++){
for(b=0; b<B; b++)
pairwise_ij[b][rand_acids[p]] += pssmAlignment->profileAlignment[p]->f[i][b];
}//normalise
double ttl=0;
for(a=0; a<AA; a++){
for(b=0; b<B; b++){
pairwise_ij[b][a] = pairwise_ij[b][a]/(double)pssmAlignment->GetNumAligned();
ttl+=pairwise_ij[b][a];
}
}//Calculate the actual M values
RandM=0;
for(b=0; b<B; b++){
for(a=0; a<AA; a++){
if(pairwise_ij[b][a]>0 && alignmentMotif->f[i][b]>0 && ttl_acids[a]>0)
RandM += pairwise_ij[b][a] * (log_2(pairwise_ij[b][a]/(alignmentMotif->f[i][b] * ttl_acids[a])));
}
}
//totalRandM+=RandM;
if(RandM>=M[i][j])
GTObs++;
}
MIConf[i][j] = GTObs/(double)RAND_MUTI_N;
}
}
//////////////////////////////////////////////////////////////////////////
*/
/*
//////////////////////////////////////////////////////////////////
//Random shuffling area
rand_acids = new int[pssmAlignment->GetNumAligned()];
double** residueShuffle = new double*[pssmAlignment->GetNumAligned()];
for(p=0; p<pssmAlignment->GetNumAligned(); p++){
residueShuffle[p] = new double[AA];
for(a=0; a<AA; a++)
residueShuffle[p][a] = 0;
}
int numRS = pssmAlignment->GetNumAligned();
RandM = 0;
double totalObs=0;
double GTObs=0;
for(i=0; i<x; i++){
for(j=0; j<y; j++){
GTObs=0;
for(n=0; n<RAND_MUTI_N; n++){
//Reset
for(a=0; a<AA; a++)
{ ttl_acids[a]=0; obs_acids[a]=0;}
for(p=0; p<pssmAlignment->GetNumAligned(); p++)
rand_acids[p]=0;
//Count the number of distinct amino acids in the corresponding amino acid column
int aaDistinct=0;
for(a=0; a<AA; a++)
if(domainMotif->f[j][a]>0)
aaDistinct++;
//Set up the shuffle
for(p=0; p<pssmAlignment->GetNumAligned(); p++){
for(a=0; a<AA; a++)
{ residueShuffle[p][a] = individualMotifs[pssmAlignment->alignedIDs[p]]->f[j][a];
ttl_acids[a]+=individualMotifs[pssmAlignment->alignedIDs[p]]->f[j][a];
}
}numRS = pssmAlignment->GetNumAligned();
//normalise
for(a=0; a<AA; a++)
ttl_acids[a]=ttl_acids[a]/(double)pssmAlignment->GetNumAligned();
//Reset the pairwise frequencies
for(b=0; b<B; b++){
for(a=0; a<AA; a++)
pairwise_ij[b][a]=0;
}
//Calculate pairwise frequencies
//Randomly pick one of the residue positions, and take the DNA motif in position p as its pair
for(p=0; p<pssmAlignment->GetNumAligned(); p++){
double dice = ((double)rand()/RAND_MAX);
int i_dice = (int)floor(dice*numRS);
for(a=0; a<AA; a++)
for(b=0; b<B; b++)
pairwise_ij[b][a] += pssmAlignment->profileAlignment[p]->f[i][b]* residueShuffle[i_dice][a];
//printf("%d\t%d\t%d\tProtein %d paired with DNA %d\n", i,j,n,i_dice, p);
//Delete the chosen residue from the shuffle
for(int g=i_dice; g<numRS-1; g++)
for(a=0; a<AA; a++)
residueShuffle[g][a] =residueShuffle[g+1][a];
numRS--;
}//normalise
double ttl=0;
for(a=0; a<AA; a++){
for(b=0; b<B; b++){
pairwise_ij[b][a] = pairwise_ij[b][a]/(double)pssmAlignment->GetNumAligned();
ttl+=pairwise_ij[b][a];
}
}//Calculate the actual M values
RandM=0;
for(b=0; b<B; b++){
for(a=0; a<AA; a++){
if(pairwise_ij[b][a]>0 && alignmentMotif->f[i][b]>0 && ttl_acids[a]>0)
RandM += pairwise_ij[b][a] * (log_2(pairwise_ij[b][a]/(alignmentMotif->f[i][b] * ttl_acids[a])));
}
}
//totalRandM+=RandM;
if(RandM>=M[i][j])
GTObs++;
}
MIConf[i][j] = GTObs/(double)RAND_MUTI_N;
}
}
///////////////////////////////////////////////////////////////////
//print out the probability scores
printf("Probability of observing the above MI values\n\t");
for(j=0; j<y; j++)
printf("%d\t", j);
printf("\n");
for(i=0; i<x; i++){
printf("%c\t", alignmentMotif->ColConsensus(i));
for(j=0; j<y; j++){
printf("%.8lf\t", MIConf[i][j]);
}
printf("\n");
}
for(p=0; p<pssmAlignment->GetNumAligned(); p++)
delete [] residueShuffle[p];
delete residueShuffle;
delete rand_acids;
//////////////////////////////////////////////////////////////////
*/
//Memory cleanup
for(b=0; b<B; b++)
delete pairwise_ij[b];
delete pairwise_ij;
for(i=0; i<x; i++)
delete M[i];
delete M;
for(i=0; i<x; i++)
delete Minfo[i];
for(i=0; i<x; i++)
delete MIConf[i];
delete Minfo;
delete MIConf;
delete [] ttl_acids;
delete [] obs_acids;
delete posPref;
}
//Converts a single amino acid into it's integer (0 to 20)
int ProteinDomains::char2num(char x) {
int b;
b=tolower(x);
if(b=='a')
return(0);
else if(b=='r')
return(1);
else if(b=='n')
return(2);
else if(b=='d')
return(3);
else if(b=='c')
return(4);
else if(b=='e')
return(5);
else if(b=='q')
return(6);
else if(b=='g')
return(7);
else if(b=='h')
return(8);
else if(b=='i')
return(9);
else if(b=='l')
return(10);
else if(b=='k')
return(11);
else if(b=='m')
return(12);
else if(b=='f')
return(13);
else if(b=='p')
return(14);
else if(b=='s')
return(15);
else if(b=='t')
return(16);
else if(b=='w')
return(17);
else if(b=='y')
return(18);
else if(b=='v')
return(19);
return -1;
}
//Destructor
ProteinDomains::~ProteinDomains()
{
if(domainMotif!=NULL){
delete domainMotif;
}
if(individualMotifs!=NULL){
for(int i=0; i<numDomains; i++){
delete individualMotifs[i];
}delete individualMotifs;
}
}