-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMotif.cpp
More file actions
265 lines (245 loc) · 5.91 KB
/
Motif.cpp
File metadata and controls
265 lines (245 loc) · 5.91 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
//////////////////////////////////////////////////////////////////////////////////
//
// STAMP version 1.3
//
// Written By: Shaun Mahony
//
// Motif.cpp
//
// Started: 31st Oct 2005
//
// 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 <string.h>
#include "Motif.h"
#include "globals.h"
#include "ctype.h"
Motif::Motif(int l)
{
int i,j;
len=l;
strcpy(famName, "None");
//Motif
f = new double* [l];
for(i=0; i<l; i++)
{ f[i] = new double[B];
for(j=0; j<B; j++)
f[i][j]=0;
}
//Count matrix
n = new double* [l];
for(i=0; i<l; i++)
{ n[i] = new double[B];
for(j=0; j<B; j++)
n[i][j]=0;
}
//PWM
pwm = new double* [l];
for(i=0; i<l; i++)
{ pwm[i] = new double[B];
for(j=0; j<B; j++)
pwm[i][j]=0;
}
weighting = 1;
//Gaps are only called into use when constructing multiple alignments
gaps = new double[l];
for(i=0; i<l; i++)
gaps[i]=0;
members=1;
}
//Reset the motif
void Motif::Reset()
{ int i, j;
for(i=0; i<len; i++)
for(j=0; j<B; j++)
f[i][j]=0;
for(i=0; i<len; i++)
for(j=0; j<B; j++)
n[i][j]=0;
for(i=0; i<len; i++)
for(j=0; j<B; j++)
pwm[i][j]=0;
for(i=0; i<len; i++)
gaps[i]=0;
members=1;
}
//Reverse complement a motif
void Motif::RevCompMotif(Motif* out)
{
// out->len = len;
if(len == out->len){
strcpy(out->name, name);
strcpy(out->famName, famName);
out->members = members;
for(int i=0; i<len; i++)
{
out->f[(len-i)-1][0] = f[i][3]; out->n[(len-i)-1][0] = n[i][3]; out->pwm[(len-i)-1][0] = pwm[i][3];
out->f[(len-i)-1][3] = f[i][0]; out->n[(len-i)-1][3] = n[i][0]; out->pwm[(len-i)-1][3] = pwm[i][0];
out->f[(len-i)-1][1] = f[i][2]; out->n[(len-i)-1][1] = n[i][2]; out->pwm[(len-i)-1][1] = pwm[i][2];
out->f[(len-i)-1][2] = f[i][1]; out->n[(len-i)-1][2] = n[i][1]; out->pwm[(len-i)-1][2] = pwm[i][1];
out->gaps[(len-i)-1] = gaps[i];
}
}else{
printf("error: lengths of motifs do not match!\n");
exit(1);
}
}
//Reverse complement a single column
void Motif::RevCompColumn(int i)
{
double tmp_f, tmp_n, tmp_pwm;
tmp_f=f[i][0]; tmp_n=n[i][0]; tmp_pwm=pwm[i][0];
f[i][0] = f[i][3]; n[i][0] = n[i][3]; pwm[i][0] = pwm[i][3];
f[i][3]=tmp_f; n[i][3]=tmp_n; pwm[i][3]=tmp_pwm;
tmp_f=f[i][1]; tmp_n=n[i][1]; tmp_pwm=pwm[i][1];
f[i][1] = f[i][2]; n[i][1] = n[i][2]; pwm[i][1] = pwm[i][2];
f[i][2]=tmp_f; n[i][2]=tmp_n; pwm[i][2]=tmp_pwm;
}
//Clone a motif
void Motif::CopyMotif(Motif* nMot)
{
strcpy(nMot->name, name);
strcpy(nMot->famName, famName);
nMot->members=members;
for(int i=0; i<len; i++){
for(int j=0; j<B; j++){
nMot->n[i][j]=n[i][j];
nMot->f[i][j]=f[i][j];
nMot->pwm[i][j]=pwm[i][j];
}
nMot->gaps[i]=gaps[i];
}
}
//Returns the consensus letter for this motif
char Motif::ColConsensus(int i)
{
char val;
char curr;
char two_base_l[6]; //two base consensus
double two_base_c[6];
char three_base_l[4]; //three base consensus
double three_base_c[4];
double sum, p_max;
int j, k;
//Hard-coded consensus alphabet rules
two_base_l[0]='Y'; two_base_l[1]='R';
two_base_l[2]='W'; two_base_l[3]='S';
two_base_l[4]='K'; two_base_l[5]='M';
three_base_l[0]='V'; three_base_l[1]='H';
three_base_l[2]='D'; three_base_l[3]='B';
two_base_c[0]=f[i][1]+f[i][3]; two_base_c[1]=f[i][0]+f[i][2];
two_base_c[2]=f[i][0]+f[i][3]; two_base_c[3]=f[i][1]+f[i][2];
two_base_c[4]=f[i][2]+f[i][3]; two_base_c[5]=f[i][0]+f[i][1];
three_base_c[0]=f[i][0]+f[i][1]+f[i][2];
three_base_c[1]=f[i][0]+f[i][1]+f[i][3];
three_base_c[2]=f[i][0]+f[i][2]+f[i][3];
three_base_c[3]=f[i][1]+f[i][2]+f[i][3];
sum=0;
for(j=0; j<4; j++)
sum+=f[i][j];
if(f[i][0]/sum>=CONS1) {curr='A';}
else if(f[i][1]/sum>=CONS1) {curr='C';}
else if(f[i][2]/sum>=CONS1) {curr='G';}
else if(f[i][3]/sum>=CONS1) {curr='T';}
else {
curr='N';
p_max=CONS2;
for(k=0;k<6;k++) {
if(two_base_c[k]/sum>=p_max) {
p_max=two_base_c[k];
curr=two_base_l[k];
}
}
}
if(gaps[i]!=0)
curr = tolower(curr);
return(curr);
}
//Calculate the information content of a column
double Motif::Info(int i)
{
int x;
double sum=0;
for(x=0;x<B;x++) {
if(f[i][x]>0) {
sum+=f[i][x]*log_2(f[i][x]);
}
}
if(sum!=0)
sum=sum*(-1);
else
sum=2;
return(2-sum);
}
//Print the motif in TRANSFAC format
void Motif::PrintMotif(FILE* out, bool famNames)
{
int i, j;
double w0, w1, w2, w3, wt;
if(out==NULL){
if(famNames)
printf("DE\t%s_%s\n", famName, name);
else
printf("DE\t%s\n", name);
for(i=0; i<len; i++){
printf("%d\t", i);
for(j=0; j<B; j++){
printf("%.4lf\t", f[i][j]);
}
printf("%c\n", ColConsensus(i));
}
printf("XX\n");
}else{
if(famNames)
fprintf(out, "DE\t%s_%s\n", famName, name);
else
fprintf(out, "DE\t%s\n", name);
for(i=0; i<len; i++){
fprintf(out, "%d\t", i);
for(j=0; j<B; j++){
fprintf(out, "%.4lf\t", f[i][j]);
}
fprintf(out, "%c\n", ColConsensus(i));
}
fprintf(out, "XX\n");
}
}
//Print the motif's consensus pattern
void Motif::PrintMotifConsensus()
{
printf("\t%s consensus: ", name);
for(int i=0; i<len; i++){
printf("%c", ColConsensus(i));
}printf("\n");
}
Motif::~Motif()
{
int i;
for(i=0; i<len; i++)
{
delete [] pwm[i];
delete [] n[i];
delete [] f[i];
}
delete [] f;
delete [] n;
delete [] pwm;
delete [] gaps;
}