-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdie.cpp
326 lines (308 loc) · 10.8 KB
/
die.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
/*============================================================================
* Daniel J. Greenhoe
* routines for Real Die dieseqs
*============================================================================*/
/*=====================================
* headers
*=====================================*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "main.h"
#include "symseq.h"
#include "r1.h"
#include "r2.h"
#include "r3.h"
#include "r6.h"
#include "c1.h"
#include "die.h"
/*=====================================
* prototypes
*=====================================*/
void phistogram(seqR1 *data, const long start, const long end, FILE *ptr);
/*-------------------------------------------------------------------------
* fill the dieseq with weighted pseudo-random die face values
*-------------------------------------------------------------------------*/
int dieseq::randomize(long start, long end, int wA,int wB,int wC,int wD,int wE,int wF){
int r,u;
long n;
char symbol;
int sum=wA+wB+wC+wD+wE+wF;
if(sum!=100){
fprintf(stderr,"dieseq::randomize error: sum of weight values = %d != 100\n",sum);
return -1;
}
//printf("start=%ld end=%ld weights=(%03d %03d %03d %03d %03d %03d)\n", start,end,wA,wB,wC,wD,wE,wF);
for(n=start; n<=end; n++){
r=rand();
u = r%100;
if (u<wA) symbol='A';
else if(u<wA+wB) symbol='B';
else if(u<wA+wB+wC) symbol='C';
else if(u<wA+wB+wC+wD) symbol='D';
else if(u<wA+wB+wC+wD+wE) symbol='E';
else symbol='F';
put(n,symbol);
}
return 0;
}
/*-------------------------------------------------------------------------
* map die face values to R^1
* A-->1 B-->2 C-->3 D-->4 E-->5 F-->6
* all other values --> 0
*-------------------------------------------------------------------------*/
seqR1 dieseq::dietoR1(void){
const long N=getN();
long n;
char sym;
double xR1;
seqR1 y(N);
for(n=0; n<N; n++){
sym = get(n);
xR1 = die_dietoR1(sym);
y.put(n,xR1);
}
return y;
}
/*-------------------------------------------------------------------------
* map die face values to R^1 using PAM scheme
* A-->-2.5 B-->-1.5 C-->-0.5 D-->0.5 E-->1.5 F-->2.5
* all other values --> 0
*-------------------------------------------------------------------------*/
seqR1 dieseq::dietoR1pam(void){
const long N=getN();
long n;
char sym;
double xR1;
seqR1 y(N);
for(n=0; n<N; n++){
sym = get(n);
xR1 = die_dietoR1pam(sym);
y.put(n,xR1);
}
return y;
}
/*-------------------------------------------------------------------------
* map die face values to C^1
*-------------------------------------------------------------------------*/
seqC1 dieseq::dietoC1(void){
const long N=getN();
long n;
char sym;
complex xC1;
seqC1 y(N);
for(n=0; n<N; n++){
sym = get(n);
xC1 = die_dietoC1c(sym);
y.put(n,xC1);
}
return y;
}
/*-------------------------------------------------------------------------
* map die face values to R^3 sequence
*-------------------------------------------------------------------------*/
seqR3 dieseq::dietoR3(void){
const long N=getN();
long n;
char sym;
vectR3 xR3;
seqR3 y(N);
for(n=0; n<N; n++){
sym = get(n);
xR3 = die_dietoR3(sym);
y.put(n,xR3);
}
return y;
}
/*-------------------------------------------------------------------------
* compute histogram of dna sequence
* return seqR1 y of length 6 where
* y[1]-->number of dna 'A' symbols,
* y[2]-->number of dna 'B' symbols,
* y[3]-->number of dna 'C' symbols,
* y[4]-->number of dna 'D' symbols,
* y[5]-->number of dna 'D' symbols,
* y[6]-->number of dna 'D' symbols,
* y[0]-->number of all other values
* y[7]-->total number of symbols y[1],y[2],...,y[6]
*-------------------------------------------------------------------------*/
seqR1 dieseq::histogram(const long start, const long end, int display, FILE *fptr){
seqR1 data(8);
long n;
long bin;
char symbol;
data.clear();
for(n=start;n<=end;n++){
symbol=get(n);
switch(symbol){
case 'A': bin=1; break;
case 'B': bin=2; break;
case 'C': bin=3; break;
case 'D': bin=4; break;
case 'E': bin=5; break;
case 'F': bin=6; break;
default : bin=0; break;
}
if(bin!=0) data.increment(7);
data.increment(bin);
}
if(display) phistogram(&data,start,end,stdout);
if(fptr!=NULL)phistogram(&data,start,end,fptr );
return data;
}
/*-------------------------------------------------------------------------
* print die sequence histogram with data pointed to by <data>
* to stream pointed to by ptr
*-------------------------------------------------------------------------*/
void phistogram(seqR1 *data, const long start, const long end, FILE *ptr){
const long N=end-start+1;
long bin;
fprintf(ptr,"\n");
fprintf(ptr," -------------------------------------------------------------------------\n");
fprintf(ptr,"| Histogram for sequence [x_n|n=%7ld-%7ld] (length %7ld) |\n|",start,end,N);
for(bin=1;bin<=6;bin++)fprintf(ptr," %c",'A'+(char)bin-1);
fprintf(ptr," extra |\n|");
for(bin=1;bin<=6;bin++)fprintf(ptr,"%10.0lf",data->get(bin));
fprintf(ptr,"%10.0lf |\n|",data->get(0));
for(bin=1;bin<=6;bin++)fprintf(ptr," (%6.2lf%%)",data->get(bin)/(double)N*100.0);
fprintf(ptr," (%6.2lf%%) |\n",data->get(0)/(double)N*100.0);
fprintf(ptr," -------------------------------------------------------------------------\n");
}
/*=====================================
* operators
*=====================================*/
/*-------------------------------------------------------------------------
* operator dieseq x = dieseq y
*-------------------------------------------------------------------------*/
void dieseq::operator=(dieseq y){
const long N= getN();
const long M=y.getN();
long n;
char symbol;
if(N!=M){
fprintf(stderr,"ERROR using dieseq x = dieseq y operation: size of x (%ld) does not equal size of y (%ld)\n",N,M);
exit(EXIT_FAILURE);
}
for(n=0;n<N;n++){
symbol = y.get(n);
put(n,symbol);
}
}
/*=====================================
* external operations
*=====================================*/
/*-------------------------------------------------------------------------
* map die face values to R^1
*-------------------------------------------------------------------------*/
double die_dietoR1(char c){
double rval;
switch(c){
case 'A': rval = 1.0; break;
case 'B': rval = 2.0; break;
case 'C': rval = 3.0; break;
case 'D': rval = 4.0; break;
case 'E': rval = 5.0; break;
case 'F': rval = 6.0; break;
default:
fprintf(stderr,"ERROR using die_dietoR1(c): c=%c(0x%x) is not in the valid domain {A,B,C,D,E,F}\n",c,c);
exit(EXIT_FAILURE);
}
return rval;
}
/*-------------------------------------------------------------------------
* map die face values to R^1 PAM
*-------------------------------------------------------------------------*/
double die_dietoR1pam(char c){
double rval;
switch(c){
case 'A': rval = -2.5; break;
case 'B': rval = -1.5; break;
case 'C': rval = -0.5; break;
case 'D': rval = 0.5; break;
case 'E': rval = 1.5; break;
case 'F': rval = 2.5; break;
default:
fprintf(stderr,"ERROR using die_dietoR1pam(c): c=%c(0x%x) is not in the valid domain {A,B,C,D,E,F}\n",c,c);
exit(EXIT_FAILURE);
}
return rval;
}
/*-------------------------------------------------------------------------
* map die face values to complex plane C^1
*
* imaginary axis
* |
* B=(cos90,sin90)
* |
* |
* (cos150,sin150)=C | A=(cos30,sin30)
* |
* ------------------|----------------- real axis
* |
* (cos210,sin210)=D | F=(cos330,sin330)
* |
* |
* E=(cos270,sin270)
* |
*
*-------------------------------------------------------------------------*/
complex die_dietoC1c(char c){
complex rc;
switch(c){
case 'A': rc = expi( 30.0/180.0*PI); break;
case 'B': rc = expi( 90.0/180.0*PI); break;
case 'C': rc = expi(150.0/180.0*PI); break;
case 'D': rc = expi(210.0/180.0*PI); break;
case 'E': rc = expi(270.0/180.0*PI); break;
case 'F': rc = expi(330.0/180.0*PI); break;
case '0': rc.put(0,0); break;
default: rc.put(0,0);
fprintf(stderr,"ERROR using dietoC1(char c): c=%c(0x%x) is not in the valid domain {0,A,B,C,D,E,F}. Returning (0,0).\n",c,c);
}
return rc;
}
/*-------------------------------------------------------------------------
* map die face values to R^3
* |+1| | 0| | 0| | 0| | 0| |-1|
* A-->| 0| B-->|+1| C-->| 0| D-->| 0| E-->|-1| F-->| 0|
* | 0| | 0| |+1| |-1| | 0| | 0|
*-------------------------------------------------------------------------*/
vectR3 die_dietoR3(char c){
vectR3 xyz;
switch(c){
case 'A': xyz.put(+1, 0, 0); break;
case 'B': xyz.put( 0,+1, 0); break;
case 'C': xyz.put( 0, 0,+1); break;
case 'D': xyz.put( 0, 0,-1); break;
case 'E': xyz.put( 0,-1, 0); break;
case 'F': xyz.put(-1, 0, 0); break;
default:
fprintf(stderr,"ERROR using die_dietoR3(c): c=%c(0x%x) is not in the valid domain {A,B,C,D,E,F}\n",c,c);
exit(EXIT_FAILURE);
}
return xyz;
}
/*-------------------------------------------------------------------------
* map die face values to R^6
* A-->(1,0,0,0,0,0) D-->(0,0,0,1,0,0)
* B-->(0,1,0,0,0,0) E-->(0,0,0,0,1,0)
* C-->(0,0,1,0,0,0) F-->(0,0,0,0,0,1)
* 0-->(0,0,0,0,0,0)
* on ERROR return (0,0,0,0,0,0)
*-------------------------------------------------------------------------*/
vectR6 die_dietoR6(char c){
vectR6 rsix;
switch(c){
case 'A': rsix.put(1,0,0,0,0,0); break;
case 'B': rsix.put(0,1,0,0,0,0); break;
case 'C': rsix.put(0,0,1,0,0,0); break;
case 'D': rsix.put(0,0,0,1,0,0); break;
case 'E': rsix.put(0,0,0,0,1,0); break;
case 'F': rsix.put(0,0,0,0,0,1); break;
default:
fprintf(stderr,"ERROR using dietoR6(char c): c=%c(0x%x) is not in the valid domain {0,A,B,C,D,E,F}.\n",c,c);
exit(EXIT_FAILURE);
}
return rsix;
}