1 /* Quadruple Statistics on an arbitrary alphabet
3 Vienna RNA Package --- Peter F Stadler 1993
14 #include "distance_matrix.h"
17 #define PRIVATE static
19 #define MIN2(A, B) ((A) < (B) ? (A) : (B))
20 #define MIN4(A, B, C, D) MIN2( (MIN2((A),(B))), (MIN2((C),(D))) )
21 #define MAX2(A, B) ((A) > (B) ? (A) : (B))
22 #define MAX4(A, B, C, D) MAX2( (MAX2((A),(B))), (MAX2((C),(D))) )
27 PUBLIC float *statgeom(char **seqs, int n_of_seqs);
28 PUBLIC float *statgeom4(char **ss[4], int nn[4]);
29 PUBLIC void printf_stg(float *B);
31 PRIVATE void SingleBox(char *x1, char *x2, char *x3, char *x4);
32 PRIVATE void SortSingleBox(void);
34 /* ----------------------------------------------------------------------- */
36 PUBLIC float *statgeom(char **seqs, int n_of_seqs)
45 fprintf(stderr,"Less than 4 sequences for statistical geometry.\n");
49 B = (float *) space(16*sizeof(float));
51 for(i=3; i<n_of_seqs; i++) {
55 SingleBox(seqs[i], seqs[j], seqs[k], seqs[l]);
57 B[0] = (float) IBox[0]; /* transfer length */
58 for(i1=1;i1<=15;i1++) B[i1] += ( (float) IBox[i1] );
64 temp = (float) n_of_seqs;
65 temp = temp*(temp-1.)*(temp-2.)*(temp-3.)/24.;
66 for(i1=1;i1<=15;i1++) B[i1] /= (temp*B[0]);
71 /* ----------------------------------------------------------------------- */
73 PUBLIC float *statgeom4(char **ss[4], int nn[4])
81 B = (float *) space(16*sizeof(float));
83 for(i=0; i<nn[0]; i++) {
84 for(j=0; j<nn[1]; j++) {
85 for(k=0; k<nn[2]; k++) {
86 for(l=0; l<nn[3]; l++) {
87 SingleBox(ss[0][i], ss[1][j], ss[2][k], ss[3][l]);
88 B[0] = (float) IBox[0]; /* transfer length */
89 for(i1=1;i1<=15;i1++) B[i1] += ( (float) IBox[i1] );
95 for (temp = 1, i=0;i<4;i++) temp *= ((float) nn[i]) ;
96 for(i1=1;i1<=15;i1++) B[i1] /= (temp*B[0]);
100 /* ------------------------------------------------------------------------- */
102 PUBLIC void printf_stg(float *B)
104 printf("> Statistical Geometry.\n");
105 printf("> %d (sequence length)\n", (int) B[0]);
107 printf(" %7.5f\n", B[1]);
108 printf("> BAAA ABAA AABA AAAB\n");
109 printf(" %7.5f %7.5f %7.5f %7.5f\n", B[2],B[3],B[4],B[5]);
110 printf("> AABB ABAB ABBA\n");
111 printf(" %7.5f %7.5f %7.5f\n", B[6],B[7],B[8]);
112 printf("> AABC ABAC ABCA BAAC BACA BCAA\n");
113 printf(" %7.5f %7.5f %7.5f %7.5f %7.5f %7.5f\n"
114 ,B[9],B[10],B[11],B[12],B[13],B[14]);
116 printf(" %7.5f\n",B[15]);
119 /* ------------------------------------------------------------------------- */
122 PUBLIC void SimplifiedBox(float *B, char *filename)
127 float t1, t2, t3, t4;
128 float p1, p2, p3, p4;
129 float x1, x2, y1, y2,z1,z2;
131 float view[3] = {0.3, 1.5, 0.1};
132 float axis[3] = {1.0, 0.0, 0.0};
134 t1 = B[9]+B[10]+B[11]+B[15];
135 t2 = B[9]+B[12]+B[13]+B[15];
136 t3 = B[10]+B[12]+B[14]+B[15];
137 t4 = B[11]+B[13]+B[14]+B[15];
139 p1 = B[2]; p2 = B[3]; p3 = B[4]; p4 = B[5];
148 X = (x1+x2)/2.; Y = (y1+y2)/2.; Z = (z1+z2)/2;
149 T = (t1+t2+t3+t4)/4.;
150 P = (p1+p2+p3+p4)/4.;
152 printf("> X = %7.5f \n",X);
153 printf("> Y = %7.5f \n",Y);
154 printf("> Z = %7.5f \n",Z);
155 printf("> T = %7.5f \n",T);
156 printf("> P = %7.5f \n",P);
158 tmp = get_taxon_label(-1); /* retrieve the dataset identifier */
161 if(tmp) { strcat(temp,tmp);strcat(temp,"_"); free(tmp); }
162 strcat(temp,filename);
164 fp = fopen(temp,"w");
166 ps3d_Preambel(fp, view, axis, "N");
167 PS_DrawSimplifiedBox(X,Y,Z,T,P, fp);
169 } else fprintf(stderr,"couldn't open %s -- not drawing box\n", temp);
174 /* ------------------------------------------------------------------------- */
176 PRIVATE void SingleBox(char *x1, char *x2, char *x3, char *x4)
178 int len,i1,j1,k1,i,M,m;
183 if(strlen(x2)!=len) nrerror("Sequences of unequal length in 'SingleBox'");
184 if(strlen(x3)!=len) nrerror("Sequences of unequal length in 'SingleBox'");
185 if(strlen(x4)!=len) nrerror("Sequences of unequal length in 'SingleBox'");
188 for(i=1; i<=15; i++) IBox[i] = 0;
190 for(i1=0;i1<len;i1++) {
195 for(j1=0;j1<4;j1++) {
197 for(k1=0;k1<4;k1++) d[j1] -=(t[j1]!=t[k1]);
199 M=MAX4(d[0],d[1],d[2],d[3]);
201 case 4 : /* Four of a kind */
202 IBox[1]++; /* A A A A */
204 case 3 : /* Three of a kind */
205 if(d[0] == 1) IBox[2]++; /* B A A A */
206 else if(d[1] == 1) IBox[3]++; /* A B A A */
207 else if(d[2] == 1) IBox[4]++; /* A A B A */
208 else IBox[5]++; /* A A A B */
211 m=MIN4(d[0],d[1],d[2],d[3]);
212 if(m==2){ /* Two Pairs */
213 if(t[1]==t[0]) IBox[6]++; /* A A B B */
214 else if(t[2]==t[0]) IBox[7]++; /* A B A B */
215 else IBox[8]++; /* A B B A */
217 else { /* One Pair */
218 if(d[0]==2){ /* 0 is in the pair */
219 if(t[1]==t[0]) IBox[9]++; /* A A B C */
220 else if(t[2]==t[0]) IBox[10]++; /* A B A C */
221 else IBox[11]++ ;/* A B C A */
223 else if(d[1]==2){ /* 1 is in the pair */
224 if(t[2]==t[1]) IBox[12]++; /* B A A C */
225 else IBox[13]++; /* B A C A */
227 else IBox[14]++; /* B C A A */
230 case 1 : /* No Pair */
231 IBox[15]++; /* A B C D */
234 nrerror("This can't happen.");
239 /* ----------------------------------------------------------------------- */
241 PRIVATE void SortSingleBox(void)
248 M = MAX2(MAX2( IBox[6], IBox[7] ), IBox[8] );
250 if( M== IBox[6] ) { /* 12|34 */
252 if( IBox[9] >= IBox[14] ) { /* 1,2 > 3,4 */
255 if( IBox[7] >= IBox[8] ) { /* 13|24 */
258 if( IBox[10] >= IBox[13] ) { /* 1>2>3>4 */
263 s[0]=0; s[1]=1; s[2]=2; s[3]=3; /* 1 2 3 4 */
270 s[0]=1; s[1]=0; s[2]=3; s[3]=2; /* 2 1 4 3 */
276 if( IBox[11] >= IBox[12]) { /* 1>2>4>3 */
281 s[0]=0; s[1]=1; s[2]=3; s[3]=2; /* 1 2 4 3 */
288 s[0]=1; s[1]=0; s[2]=2; s[3]=3; /* 2 1 3 4 */
292 else { /* 3,4 > 1,2 */
295 if( IBox[7] >= IBox[8] ) { /* 31|42 */
298 if(IBox[10] >= IBox[13]) { /* 3>4>1>2 */
303 s[0]=2; s[1]=3; s[2]=0; s[3]=1; /* 3 4 1 2 */
310 s[0]=3; s[1]=2; s[2]=1; s[3]=0; /* 4 3 2 1 */
316 if( IBox[11] >= IBox[12]) { /* 3>4>2>1 */
321 s[0]=2; s[1]=3; s[2]=1; s[3]=0; /* 3 4 2 1 */
328 s[0]=2; s[1]=3; s[2]=0; s[3]=1; /* 3 4 1 2 */
333 else if (M == IBox[7] ) { /* 13|24 */
335 if( IBox[10] >= IBox[13] ) { /* 1,3 > 2,4 */
338 if( IBox[6] >= IBox[8]) { /* 12|34 */
341 if( IBox[9] >= IBox[14] ) { /* 1,2>3,4 */
346 s[0]=0; s[1]=2; s[2]=1; s[3]=3; /* 1 3 2 4 */
353 s[0]=2; s[1]=0; s[2]=3; s[3]=1; /* 3 1 4 2 */
359 if( IBox[11] >= IBox[12] ){ /* 1,4 > 2,3 */
364 s[0]=0; s[1]=2; s[2]=3; s[3]=1; /* 1 3 4 2 */
371 s[0]=2; s[1]=0; s[2]=1; s[3]=3; /* 3 1 2 4 */
375 else { /* 2,4 > 1,3 */
378 if( IBox[6] >= IBox[8]) { /* 21|43 */
381 if( IBox[9] >= IBox[14] ) { /* 2,1>4,3 */
386 s[0]=1; s[1]=3; s[2]=0; s[3]=2; /* 2 4 1 3 */
393 s[0]=3; s[1]=1; s[2]=2; s[3]=0; /* 4 2 3 1 */
399 if( IBox[11] >= IBox[12] ){ /* 1,4 > 2,3 */
404 s[0]=3; s[1]=1; s[2]=0; s[3]=2; /* 4 2 1 3 */
411 s[0]=1; s[1]=3; s[2]=2; s[3]=0; /* 2 4 3 1 */
418 if( IBox[11] >= IBox[12] ) { /* 1,4 > 2,3 */
421 if(IBox[6] >= IBox[7]) { /* 12|34 */
424 if(IBox[9] >= IBox[14]) { /* 1,2>3,4 */
429 s[0]=0; s[1]=3; s[2]=1; s[3]=2; /* 1 4 2 3 */
436 s[0]=3; s[1]=0; s[2]=2; s[3]=1; /* 4 1 3 2 */
442 if( IBox[10] >= IBox[13]) { /* 1,3 > 2,4 */
447 s[0]=0; s[1]=3; s[2]=2; s[3]=1; /* 1 4 3 2 */
454 s[0]=3; s[1]=0; s[2]=1; s[3]=2; /* 4 1 2 3 */
458 else { /* 2,3 > 1,4 */
461 if(IBox[6] >= IBox[7]) { /* 34|12 */
464 if(IBox[9] >= IBox[14]) { /* 2,1>4,3 */
469 s[0]=1; s[1]=2; s[2]=0; s[3]=3; /* 2 3 1 4 */
476 s[0]=2; s[1]=1; s[2]=3; s[3]=0; /* 3 2 4 1 */
482 if( IBox[10] >= IBox[13]) { /* 1,3 > 2,4 */
487 s[0]=2; s[1]=1; s[2]=0; s[3]=4; /* 3 2 1 4 */
494 s[0]=1; s[1]=2; s[2]=3; s[3]=0; /* 2 3 4 1 */
502 IBB[2] = IBox[2+s[0]];
503 IBB[3] = IBox[2+s[1]];
504 IBB[4] = IBox[2+s[2]];
505 IBB[5] = IBox[2+s[3]];
507 IBB[6...14] see above :-)
511 for(i=0;i<=15;i++) IBox[i] = IBB[i];
514 /* ----------------------------------------------------------------------- */