forked from satoken/centroid-rna-package
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrafold.patch
530 lines (497 loc) · 14.8 KB
/
contrafold.patch
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
diff -urN contrafold_v2.00.orig/src/InferenceEngine.hpp contrafold/src/InferenceEngine.hpp
--- contrafold_v2.00.orig/src/InferenceEngine.hpp 2008-01-09 06:13:58.000000000 +0900
+++ contrafold/src/InferenceEngine.hpp 2008-07-04 10:41:33.735151672 +0900
@@ -281,9 +281,101 @@
void ConvertCacheCounts();
void WriteCounts(std::vector<RealT> &counts);
+#ifdef REUSE_MEMORY
public:
+ struct WS
+ {
+ WS(unsigned int m) : max_len(m)
+ {
+ uint L = m-1;
+ uint SIZE = (L+1)*(L+2)/2;
+ s = new int[L+1];
+ offset = new int[L+1];
+ allow_unpaired_position = new int[L+1];
+ allow_unpaired = new int[SIZE];
+ allow_paired = new int[SIZE];
+ loss_unpaired_position = new RealT[SIZE];
+ loss_unpaired = new RealT[SIZE];
+ loss_paired = new RealT[SIZE];
+
+ F5i = new RealT[L+1];
+ FCi = new RealT[SIZE];
+ FMi = new RealT[SIZE];
+ FM1i = new RealT[SIZE];
+#if PARAMS_HELIX_LENGTH || PARAMS_ISOLATED_BASE_PAIR
+ FEi = new RealT[SIZE];
+ FNi = new RealT[SIZE];
+#endif
+
+ F5o = new RealT[L+1];
+ FCo = new RealT[SIZE];
+ FMo = new RealT[SIZE];
+ FM1o = new RealT[SIZE];
+#if PARAMS_HELIX_LENGTH || PARAMS_ISOLATED_BASE_PAIR
+ FEo = new RealT[SIZE];
+ FNo = new RealT[SIZE];
+#endif
+
+ posterior = new RealT[SIZE];
+ }
+
+ ~WS()
+ {
+ delete [] s;
+ delete [] offset;
+ delete [] allow_unpaired_position;
+ delete [] allow_unpaired;
+ delete [] allow_paired;
+ delete [] loss_unpaired_position;
+ delete [] loss_unpaired;
+ delete [] loss_paired;
+
+ delete [] FCi;
+ delete [] F5i;
+ delete [] FMi;
+ delete [] FM1i;
+ delete [] FCo;
+ delete [] F5o;
+ delete [] FMo;
+ delete [] FM1o;
+
+#if PARAMS_HELIX_LENGTH || PARAMS_ISOLATED_BASE_PAIR
+ delete [] FEi;
+ delete [] FNi;
+ delete [] FEo;
+ delete [] FNo;
+#endif
+ delete [] posterior;
+ }
+
+ unsigned int max_len;
+ int *s, *offset;
+ int *allow_unpaired_position;
+ int *allow_unpaired, *allow_paired;
+ RealT *loss_unpaired_position;
+ RealT *loss_unpaired, *loss_paired;
+ // dynamic programming matrices
+ RealT *FCi, *F5i, *FMi, *FM1i; // inside
+ RealT *FCo, *F5o, *FMo, *FM1o; // outside
+#if PARAMS_HELIX_LENGTH || PARAMS_ISOLATED_BASE_PAIR
+ RealT *FEi, *FNi;
+ RealT *FEo, *FNo;
+#endif
+ RealT *posterior;
+ };
+
+private:
+ WS *ws;
+#endif
+
+public:
+
+#ifdef REUSE_MEMORY
+ InferenceEngine(const std::string &sequence, bool toggle_complementary_only, WS* ws);
+#else
InferenceEngine(const std::string &sequence, bool toggle_complementary_only);
+#endif
~InferenceEngine();
void LoadParameters(const std::vector<RealT> &values);
diff -urN contrafold_v2.00.orig/src/InferenceEngine.ipp contrafold/src/InferenceEngine.ipp
--- contrafold_v2.00.orig/src/InferenceEngine.ipp 2008-01-09 06:13:58.000000000 +0900
+++ contrafold/src/InferenceEngine.ipp 2008-07-04 10:41:33.735151672 +0900
@@ -148,12 +148,62 @@
// Constructor
/////////////////////////////////////////////////////////////////
+#ifdef REUSE_MEMORY
+template<class RealT>
+InferenceEngine<RealT>::InferenceEngine(const std::string &sequence,
+ bool toggle_complementary_only,
+ WS* w) :
+ params(), L(sequence.length()-1), SIZE((L+1)*(L+2)/2),
+ toggle_complementary_only(toggle_complementary_only), ws(w)
+#else
template<class RealT>
InferenceEngine<RealT>::InferenceEngine(const std::string &sequence,
bool toggle_complementary_only) :
params(), L(sequence.length()-1), SIZE((L+1)*(L+2)/2),
toggle_complementary_only(toggle_complementary_only)
+#endif
{
+#ifdef REUSE_MEMORY
+ // allocate memory
+
+ s = ws->s;
+ offset = ws->offset;
+ allow_unpaired_position = ws->allow_unpaired_position;
+ allow_unpaired = ws->allow_unpaired;
+ allow_paired = ws->allow_paired;
+ loss_unpaired_position = ws->loss_unpaired_position;
+ loss_unpaired = ws->loss_unpaired;
+ loss_paired = ws->loss_paired;
+
+#if FAST_HELIX_LENGTHS
+ cache_score_helix_sums = new RealT[(2*L+1)*L];
+ cache_counts_helix_sums = new RealT[(2*L+1)*L];
+#endif
+
+ // initialize dynamic programming matrix pointers
+
+ FCt = F5t = FMt = FM1t = NULL;
+ FCv = F5v = FMv = FM1v = NULL;
+
+ FCi = ws->FCi;
+ F5i = ws->F5i;
+ FMi = ws->FMi;
+ FM1i = ws->FM1i;
+ FCo = ws->FCo;
+ F5o = ws->F5o;
+ FMo = ws->FMo;
+ FM1o = ws->FM1o;
+ posterior = ws->posterior;
+
+#if PARAMS_HELIX_LENGTH || PARAMS_ISOLATED_BASE_PAIR
+ FEt = FNt = NULL;
+ FEv = FNv = NULL;
+ FEi = ws->FEi;
+ FNi = ws->FNi;
+ FEo = ws->FEo;
+ FNo = ws->FNo;
+#endif
+#else
// allocate memory
s = new int[L+1];
@@ -184,6 +234,7 @@
FEi = FNi = NULL;
FEo = FNo = NULL;
#endif
+#endif
// precompute mapping from characters to index representation
@@ -277,6 +328,7 @@
template<class RealT>
InferenceEngine<RealT>::~InferenceEngine()
{
+#ifndef REUSE_MEMORY
delete [] s;
delete [] offset;
delete [] allow_unpaired_position;
@@ -285,6 +337,7 @@
delete [] loss_unpaired_position;
delete [] loss_unpaired;
delete [] loss_paired;
+#endif
#if FAST_HELIX_LENGTHS
delete [] cache_score_helix_sums;
@@ -300,7 +353,8 @@
delete [] F5v;
delete [] FMv;
delete [] FM1v;
-
+
+#ifndef REUSE_MEMORY
delete [] FCi;
delete [] F5i;
delete [] FMi;
@@ -310,19 +364,24 @@
delete [] F5o;
delete [] FMo;
delete [] FM1o;
+#endif
#if PARAMS_HELIX_LENGTH || PARAMS_ISOLATED_BASE_PAIR
delete [] FEt;
delete [] FNt;
delete [] FEv;
delete [] FNv;
+#ifndef REUSE_MEMORY
delete [] FEi;
delete [] FNi;
delete [] FEo;
delete [] FNo;
#endif
+#endif
+#ifndef REUSE_MEMORY
delete [] posterior;
+#endif
}
/////////////////////////////////////////////////////////////////
@@ -1851,7 +1910,7 @@
// Clearly, i and j must refer to actual letters of the sequence,
// and no letter may base-pair to itself.
- Assert(0 < i && i <= L && 0 < j && j <= L && i != j, "Invalid base-pair");
+ Assert(0 < i && i <= L && 0 < j && j <= L /*&& i != j*/, "Invalid base-pair");
return
loss_paired[offset[i]+j]
@@ -3049,11 +3108,12 @@
//double starting_time = GetSystemTime();
// initialization
-
+#ifndef REUSE_MEMORY
delete [] F5i; F5i = new RealT[L+1];
delete [] FCi; FCi = new RealT[SIZE];
delete [] FMi; FMi = new RealT[SIZE];
delete [] FM1i; FM1i = new RealT[SIZE];
+#endif
std::fill(F5i, F5i+L+1, RealT(NEG_INF));
std::fill(FCi, FCi+SIZE, RealT(NEG_INF));
@@ -3061,8 +3121,10 @@
std::fill(FM1i, FM1i+SIZE, RealT(NEG_INF));
#if PARAMS_HELIX_LENGTH || PARAMS_ISOLATED_BASE_PAIR
+#ifndef REUSE_MEMORY
delete [] FEi; FEi = new RealT[SIZE];
delete [] FNi; FNi = new RealT[SIZE];
+#endif
std::fill(FEi, FEi+SIZE, RealT(NEG_INF));
std::fill(FNi, FNi+SIZE, RealT(NEG_INF));
#endif
@@ -3420,11 +3482,12 @@
//double starting_time = GetSystemTime();
// initialization
-
+#ifndef REUSE_MEMORY
delete [] F5o; F5o = new RealT[L+1];
delete [] FCo; FCo = new RealT[SIZE];
delete [] FMo; FMo = new RealT[SIZE];
delete [] FM1o; FM1o = new RealT[SIZE];
+#endif
std::fill(F5o, F5o+L+1, RealT(NEG_INF));
std::fill(FCo, FCo+SIZE, RealT(NEG_INF));
@@ -3432,8 +3495,10 @@
std::fill(FM1o, FM1o+SIZE, RealT(NEG_INF));
#if PARAMS_HELIX_LENGTH || PARAMS_ISOLATED_BASE_PAIR
+#ifndef REUSE_MEMORY
delete [] FEo; FEo = new RealT[SIZE];
delete [] FNo; FNo = new RealT[SIZE];
+#endif
std::fill(FEo, FEo+SIZE, RealT(NEG_INF));
std::fill(FNo, FNo+SIZE, RealT(NEG_INF));
#endif
@@ -4162,7 +4227,9 @@
template<class RealT>
void InferenceEngine<RealT>::ComputePosterior()
{
+#ifndef REUSE_MEMORY
delete [] posterior; posterior = new RealT[SIZE];
+#endif
std::fill(posterior, posterior + SIZE, RealT(0));
//double starting_time = GetSystemTime();
diff -urN contrafold_v2.00.orig/src/InnerOptimizationWrapper.cpp contrafold/src/InnerOptimizationWrapper.cpp
--- contrafold_v2.00.orig/src/InnerOptimizationWrapper.cpp 2008-01-10 10:09:03.000000000 +0900
+++ contrafold/src/InnerOptimizationWrapper.cpp 2008-07-04 10:41:33.735151672 +0900
@@ -4,6 +4,7 @@
// Implementation of functors needed for optimization.
/////////////////////////////////////////////////////////////////
+#include <limits>
#include "InnerOptimizationWrapper.hpp"
/////////////////////////////////////////////////////////////////
diff -urN contrafold_v2.00.orig/src/Makefile contrafold/src/Makefile
--- contrafold_v2.00.orig/src/Makefile 2008-01-10 10:51:36.000000000 +0900
+++ contrafold/src/Makefile 2008-07-04 10:41:33.735151672 +0900
@@ -1,4 +1,6 @@
CXX = g++
+AR = ar
+RANLIB = ranlib
CXXFLAGS = -O3 -DNDEBUG -W -Wall -pipe -Wundef -Winline
LINKFLAGS = -lm
@@ -29,14 +31,22 @@
SStruct.cpp \
Utilities.cpp
+LIB_SRCS = \
+ wrapper.cpp \
+ Parameters.cpp \
+ SStruct.cpp \
+ Utilities.cpp
+
CONTRAFOLD_OBJS = $(CONTRAFOLD_SRCS:%.cpp=%.o)
MAKECOORDS_OBJS = $(MAKECOORDS_SRCS:%.cpp=%.o)
PLOTRNA_OBJS = $(PLOTRNA_SRCS:%.cpp=%.o)
+LIB_OBJS = $(LIB_SRCS:%.cpp=%.o)
.PHONY: all viz clean
-all: contrafold viz
+all: contrafold viz lib
viz: make_coords plot_rna
+lib: libcontrafold.a
contrafold: $(CONTRAFOLD_OBJS)
$(CXX) $(CXXFLAGS) $(OTHERFLAGS) $(CONTRAFOLD_OBJS) $(LINKFLAGS) -o contrafold
@@ -53,6 +63,10 @@
plot_rna: $(PLOTRNA_OBJS)
$(CXX) $(CXXFLAGS) $(OTHERFLAGS) $(PLOTRNA_OBJS) $(LINKFLAGS) $(GDLINKFLAGS) -o plot_rna
+libcontrafold.a: $(LIB_OBJS)
+ $(AR) r libcontrafold.a $(LIB_OBJS)
+ $(RANLIB) libcontrafold.a
+
##########################################
gccathlon64:
@@ -98,4 +112,4 @@
$(CXX) $(CXXFLAGS) $(OTHERFLAGS) -c $<
clean:
- rm -f contrafold make_coords plot_rna *.o Defaults.hpp
+ rm -f contrafold make_coords plot_rna *.o Defaults.hpp libcontrafold.a
diff -urN contrafold_v2.00.orig/src/Utilities.cpp contrafold/src/Utilities.cpp
--- contrafold_v2.00.orig/src/Utilities.cpp 2008-01-10 11:10:12.000000000 +0900
+++ contrafold/src/Utilities.cpp 2008-07-04 10:41:33.735151672 +0900
@@ -3,6 +3,7 @@
/////////////////////////////////////////////////////////////////
#include "Utilities.hpp"
+#include <climits>
bool toggle_error = false;
diff -urN contrafold_v2.00.orig/src/contrafold.h contrafold/src/contrafold.h
--- contrafold_v2.00.orig/src/contrafold.h 1970-01-01 09:00:00.000000000 +0900
+++ contrafold/src/contrafold.h 2008-07-11 10:51:30.789568491 +0900
@@ -0,0 +1,51 @@
+/*
+ * wrapper routines for CONTRAfold
+ *
+ * Copyright (C) 2008 Kengo Sato
+ *
+ * This program 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.
+ *
+ * This program 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 this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#ifndef __INC_CONTRAFOLD_H__
+#define __INC_CONTRAFOLD_H__
+
+#include <string>
+#include <vector>
+
+namespace CONTRAfold {
+
+ template < class T >
+ struct WSImpl;
+
+ template < class T >
+ struct WS {
+ WSImpl<T>* impl;
+
+ WS(unsigned int size);
+ ~WS();
+ };
+
+ template < class T >
+ const T* ComputePosterior(const std::string& seq, bool canonical_only = true);
+
+ template < class T >
+ const T* ComputePosterior(const std::string& seq, WS<T>& ws, bool canonical_only = true);
+};
+
+#endif // __INC_CONTRAFOLD_H__
+
+// Local Variables:
+// mode: C++
+// End:
diff -urN contrafold_v2.00.orig/src/wrapper.cpp contrafold/src/wrapper.cpp
--- contrafold_v2.00.orig/src/wrapper.cpp 1970-01-01 09:00:00.000000000 +0900
+++ contrafold/src/wrapper.cpp 2008-07-11 10:49:25.305786875 +0900
@@ -0,0 +1,99 @@
+/*
+ * wrapper routines for CONTRAfold
+ *
+ * Copyright (C) 2008 Kengo Sato
+ *
+ * This program 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.
+ *
+ * This program 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 this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#include "contrafold.h"
+#include <vector>
+#include "SStruct.hpp"
+
+#define REUSE_MEMORY
+#include "InferenceEngine.hpp"
+
+namespace CONTRAfold {
+ template < class T >
+ struct WSImpl
+ {
+ typename InferenceEngine<T>::WS ws;
+ WSImpl(uint size) : ws(size) { }
+ };
+};
+
+template < class T >
+CONTRAfold::WS<T>::
+WS(uint size) : impl(new WSImpl<T>(size))
+{
+}
+
+template < class T >
+CONTRAfold::WS<T>::
+~WS()
+{
+ delete impl;
+}
+
+template < class T >
+const T*
+CONTRAfold::
+ComputePosterior(const std::string& seq, bool canonical_only /*=true*/)
+{
+ WS<T> ws(seq.size());
+ return ComputePosterior(seq, ws, canonical_only);
+}
+
+template < class T >
+const T*
+CONTRAfold::
+ComputePosterior(const std::string& seq, WS<T>& ws, bool canonical_only /*=true*/)
+{
+ Parameters params;
+ std::vector<double> values =
+ canonical_only ? params.GetDefaultComplementaryValues() : params.GetDefaultValues();
+ InferenceEngine<T> engine(seq, canonical_only, &(ws.impl->ws));
+ engine.LoadParameters(ConvertVector<T>(values));
+ engine.ComputeInside();
+ engine.ComputeOutside();
+ engine.ComputePosterior();
+ return ws.impl->ws.posterior;
+ //T *posterior = engine.GetPosterior(0);
+
+#if 0
+ // an example of using the posterior array
+ uint x=0;
+ for (uint i=0; i!=seq.size(); ++i) {
+ std::cout << i << ' ' << seq[i] << ' ';
+ for (uint j=i; j!=seq.size(); ++j) {
+ if (posterior[x]>0.0) {
+ std::cout << j << ':' << posterior[x] << ' ';
+ }
+ ++x;
+ }
+ std::cout << std::endl;
+ }
+#endif
+
+ //return posterior;
+}
+
+// instantiation
+template
+const float*
+CONTRAfold::ComputePosterior<float>(const std::string& seq, bool canonical_only);
+
+template
+class CONTRAfold::WS<float>;