-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmoves.c
More file actions
165 lines (150 loc) · 3.56 KB
/
Copy pathmoves.c
File metadata and controls
165 lines (150 loc) · 3.56 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <math.h>
#include "globals.h"
#include "wl_options.h"
#include "moves.h"
#include <ViennaRNA/move_set.h>
#define MINGAP 3
//int get_list(struct_en*, struct_en*);
static int construct_moves_new(const char*, const short*, int , move_str **);
inline int try_insert_seq2(const char*, int, int);
inline int compat(const char, const char);
void mtw_dump_pt(const short*);
/*
compute a random move on a pair table
returns move operations to be applied to pt in order to perform the move
*/
move_str
get_random_move_pt(const char *seq, const short int *pt)
{
move_str r,*mvs=NULL;
int i,count;
count = construct_moves_new((const char *)seq,pt,1,&mvs);
/*
for (i = 0; i<count; i++) {
printf("%d %d\n", mvs[i].left, mvs[i].right);
}
*/
r.left = mvs[0].left;
r.right = mvs[0].right;
free(mvs);
return r;
}
/*
apply move operation on a pair table
*/
void
apply_move_pt(short int *pt,
move_str m)
{
if(m.left < 0){
pt[(int)(fabs(m.left))] = 0;
pt[(int)(fabs(m.right))] = 0;
}
else {
pt[m.left] = m.right;
pt[m.right] = m.left;
}
//print_str(stdout,pt);printf("\n");
}
static int
construct_moves_new(const char *seq,
const short *structure,
int permute,
move_str **array)
{
/* generate all possible moves (less than n^2)*/
int i;
int size = 4;
int count = 0;
move_str *res = (move_str*) malloc(sizeof(move_str)*(size+1));
for (i=1; i<=structure[0]; i++) {
if (structure[i]!=0) {
if (structure[i]<i) continue;
count ++;
// need to reallocate the array?
if (count>size) {
size *= 2;
res = realloc(res, sizeof(move_str)*(size));
}
res[count-1].left = -i;
res[count-1].right = -structure[i];
//fprintf(stderr, "add d(%d, %d)\n", i, structure[i]);
} else {
int j;
for (j=i+1; j<=structure[0]; j++) {
/* fprintf(stderr, "check (%d, %d)\n", i, j); */
if (structure[j]==0) {
if (try_insert_seq2(seq,i,j)) {
count ++;
// need to reallocate the array?
if (count>size) {
size *= 2;
res = realloc(res, sizeof(move_str)*(size));
}
res[count-1].left = i;
res[count-1].right = j;
//fprintf(stderr, "add i(%d, %d)\n", i, j);
continue;
}
} else if (structure[j]>j) { /* '(' */
j = structure[j];
} else break;
}
}
}
res = realloc(res, sizeof(move_str)*(count));
/* permute them */
if (permute) {
for (i=0; i<count; i++) {
int rnd = rand();
rnd = rnd % (count-i) + i;
move_str mv;
mv = res[i];
res[i] = res[rnd];
res[rnd] = mv;
}
}
*array = res;
return count;
}
/* try insert base pair (i,j) */
inline int
try_insert_seq2(const char *seq,
int i,
int j)
{
if (i<=0 || j<=0) return 0;
return (j-i>MINGAP && compat(seq[i-1], seq[j-1]));
}
/* compatible base pair?*/
inline int
compat(const char a,
const char b)
{
if (a=='A' && b=='U') return 1;
if (a=='C' && b=='G') return 1;
if (a=='G' && b=='U') return 1;
if (a=='U' && b=='A') return 1;
if (a=='G' && b=='C') return 1;
if (a=='U' && b=='G') return 1;
/* and with T's*/
if (a=='A' && b=='T') return 1;
if (a=='T' && b=='A') return 1;
if (a=='G' && b=='T') return 1;
if (a=='T' && b=='G') return 1;
return 0;
}
void
mtw_dump_pt(const short *pairtable)
{
int i;
printf("> ");
for (i=0;i<*pairtable;i++){
printf("%i ",*(pairtable+i));
}
printf("\n");
}