-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers-for-main.c
164 lines (155 loc) · 3.72 KB
/
helpers-for-main.c
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
#include "helpers-for-main.h"
#include "nj_param.h"
#include "na.h"
#include "ctools/dary.h"
#include <string.h>
#include <stdio.h>
#include <math.h>
/* TODO: hack for now, do this nicer */
double **
helper_make_K_nj_alloc(size_t ndim)
{
uint i, j;
double **K_nj;
xmalloc2d_one_chunk(K_nj, ndim, ndim);
for (i = 0; i < ndim; i++)
for (j = 0; j < ndim; j++)
K_nj[i][j] = K_NJ[i][j];
return K_nj;
}
bool
system_is_exploded(double **p, size_t n1, size_t n2)
{
size_t i, j;
if (! dary2d_is_finite(p, n1, n2))
return true;
for (i = 0; i < n1; i++)
for (j = 0; j < n2; j++)
if (p[i][j] < -1.0 || p[i][j] > 2.0)
return true;
return false;
}
/* calculate an entropy-like measure for the sequence
s = - 1/N * \sum_{i,j} p_ij * log(p_ij)
the base of the logarithm should be the alphabet size (n2) */
double
calc_entropy(double **p, size_t n1, size_t n2)
{
size_t i, j;
double s = 0;
for (i = 0; i < n1; i++)
for (j = 0; j < n2; j++)
s += plogp_sane(p[i][j]);
s = - s / n1 / log(n2);
return s;
}
bool
is_bad_bp(char *seq, uint i, uint j)
{
char bp[3];
bp[2] = '\0';
bp[0] = seq[i];
bp[1] = seq[j];
if (strcmp(bp, "CG") && strcmp(bp, "GC") &&
strcmp(bp, "GU") && strcmp(bp, "UG") &&
strcmp(bp, "AU") && strcmp(bp, "UA")) {
return true;
}
return false;
}
/* TODO: list of permissible pairs is hardcoded */
void
show_bad_bp(char *seq, uint *pairs, size_t n, bool verbose)
{
size_t i;
for (i = 0; i < n; i++) {
if (pairs[i] == NA_UNPAIRED || i > pairs[i])
continue;
if (verbose && is_bad_bp(seq, i, pairs[i])) {
printf("bad bp: %zu %u: %c%c\n", i, pairs[i],
seq[i], seq[pairs[i]]);
}
}
}
size_t
fix_bad_bp(char *seq, uint *pairs, size_t n)
{
size_t i, nfix = 0;
for (i = 0; i < n; i++) {
if (pairs[i] == NA_UNPAIRED || i > pairs[i])
continue;
if (is_bad_bp(seq, i, pairs[i])) {
/* TODO: really only a hack */
seq[i] = 'G';
seq[pairs[i]] = 'C';
nfix++;
}
}
return nfix;
}
static double
calc_dmin(double **p, size_t ndim, size_t i)
{
size_t j, k;
double d, di_min = 0;
/* calculate the minimum distance to each 'pure' corner
(0, ..., 0, 1, 0, ..., 0) of the hypercube */
for (j = 0; j < ndim; j++) {
d = 0;
for (k = 0; k < ndim; k++) {
if (k != j)
d += p[i][k] * p[i][k];
else
d += (p[i][k] - 1) * (p[i][k] - 1);
}
d = sqrt(d);
if (j == 0 || d < di_min)
di_min = d;
}
return di_min;
}
void
show_bad_prob(double **p, size_t n, size_t ndim, bool verbose)
{
size_t i;
double di_min, d_max = 0, d_avg = 0;
for (i = 0; i < n; i++) {
di_min = calc_dmin(p, ndim, i);
if (di_min > d_max)
d_max = di_min;
d_avg += di_min;
}
d_avg /= n;
if (verbose) {
printf("p: d_max = %f\n", d_max);
printf("p: d_avg = %f\n", d_avg);
printf("\n");
}
#if 0
size_t j;
for (i = 0; i < n; i++) {
for (j = 0; j < ndim; j++) {
printf(" % f", p[i][j]);
}
di_min = calc_dmin(p, ndim, i);
if (di_min < 0.01)
printf(" (%4s)", "-");
else
printf(" (% .4f)", di_min);
printf("\n");
}
printf("\n");
/*
printf("p:\n");
dary2d_print(p, n, ndim);
printf("\n");
*/
#endif
}
void
print_for_movie(double **p, size_t n, size_t ndim, char *seq)
{
pseq_to_str(p, n, ndim, seq);
printf("%s\n", seq);
dary2d_print(p, n, ndim);
}