Example programs using the RNAlib C library
Using the 'old' API
The following program exercises most commonly used functions of the library. The program folds two sequences using both the mfe and partition function algorithms and calculates the tree edit and profile distance of the resulting structures and base pairing probabilities.
- Note
- This program uses the old API of RNAlib, which is in part already marked deprecated. Please consult the RNAlib API v3.0 page for details of what changes are necessary to port your implementation to the new API.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
void main()
{
char *seq1="CGCAGGGAUACCCGCG", *seq2="GCGCCCAUAGGGACGC",
*struct1,* struct2,* xstruc;
float e1, e2, tree_dist, string_dist, profile_dist, kT;
float *pf1, *pf2;
struct1 = (
char* )
space(
sizeof(
char)*(strlen(seq1)+1));
e1 =
fold(seq1, struct1);
struct2 = (
char* )
space(
sizeof(
char)*(strlen(seq2)+1));
e2 =
fold(seq2, struct2);
free(xstruc);
free(xstruc);
free(S1); free(S2);
printf("%s mfe=%5.2f\n%s mfe=%5.2f dist=%3.2f\n",
printf("%s free energy=%5.2f\n%s free energy=%5.2f dist=%3.2f\n",
}
In a typical Unix environment you would compile this program using:
1 cc ${OPENMP_CFLAGS} -c example.c -I${hpath}
and link using
1 cc ${OPENMP_CFLAGS} -o example -L${lpath} -lRNA -lm
where ${hpath} and ${lpath} point to the location of the header files and library, respectively.
- Note
- As default, the RNAlib is compiled with build-in OpenMP multithreading support. Thus, when linking your own object files to the library you have to pass the compiler specific ${OPENMP_CFLAGS} (e.g. '-fopenmp' for gcc) even if your code does not use openmp specific code. However, in that case the OpenMP flags may be ommited when compiling example.c
Using the 'new' v3.0 API
The following is a simple program that computes the MFE, partition function, and centroid structure by making use of the v3.0 API.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[]){
char *seq = "AGACGACAAGGUUGAAUCGCACCCACAGUCUAUGAGUCGGUGACAACAUUACGAAAGGCUGUAAAAUCAAUUAUUCACCACAGGGGGCCCCCGUGUCUAG";
char *mfe_structure =
vrna_alloc(
sizeof(
char) * (strlen(seq) + 1));
char *prob_string =
vrna_alloc(
sizeof(
char) * (strlen(seq) + 1));
double mfe = (double)
vrna_mfe(vc, mfe_structure);
printf("%s\n%s (%6.2f)\n", seq, mfe_structure, mfe);
printf("%s (%6.2f)\n", prob_string, en);
double dist;
free(cent);
free(prob_string);
free(mfe_structure);
return EXIT_SUCCESS;
}
Perl Examples
Using the Flat Interface
Example 1: "Simple MFE prediction"
#!/usr/bin/perl
use warnings;
use strict;
use RNA;
my $seq1 = "CGCAGGGAUACCCGCG";
# compute minimum free energy (mfe) and corresponding structure
my ($ss, $mfe) = RNA::fold($seq1);
# print output
printf "%s [ %6.2f ]\n", $ss, $mfe;
Using the Object Oriented (OO) Interface
The 'fold_compound' class that serves as an object oriented interface for vrna_fold_compound_t
Example 1: "Simple MFE prediction"
#!/usr/bin/perl
use warnings;
use strict;
use RNA;
my $seq1 = "CGCAGGGAUACCCGCG";
# create new fold_compound object
my $fc = new RNA::fold_compound($seq1);
# compute minimum free energy (mfe) and corresponding structure
my ($ss, $mfe) = $fc->mfe();
# print output
printf "%s [ %6.2f ]\n", $ss, $mfe;
Python Examples
Using the Flat Interface
Example 1: "Simple MFE prediction"
3 sequence =
"CUCGUCGCCUUAAUCCAGUGCGGGCGCUAGACAUCUAGUUAUCGCCGCAA" 6 (structure, mfe) = RNA.fold(sequence)
9 print "%s\n%s [ %6.2f ]" % (structure, mfe)
Using the Object Oriented (OO) Interface
The 'fold_compound' class that serves as an object oriented interface for vrna_fold_compound_t
Example 1: "Simple MFE prediction"
3 sequence =
"CGCAGGGAUACCCGCG" 6 fc = RNA.fold_compound(sequence)
12 print "%s [ %6.2f ]" % (ss, mfe)
Example 2: "Use callback while generating suboptimal structures"
3 sequence =
"GGGGAAAACCCC" 8 subopt_data = {
'counter' : 1,
'sequence' : sequence }
11 def print_subopt_result(structure, energy, data):
12 if not structure ==
None:
13 print ">subopt %d" % data[
'counter']
14 print "%s" % data[
'sequence']
15 print "%s [%6.2f]" % (structure, energy)
17 data[
'counter'] = data[
'counter'] + 1
20 a = RNA.fold_compound(sequence)
24 a.subopt_cb(500, print_subopt_result, subopt_data);
Example 3: "Revert MFE to Maximum Matching using soft constraint callbacks"
3 seq1 =
"CUCGUCGCCUUAAUCCAGUGCGGGCGCUAGACAUCUAGUUAUCGCCGCAA" 10 mm_data = {
'dummy': RNA.fold_compound(seq1),
'params': RNA.param() }
14 RNA.DECOMP_PAIR_HP:
lambda i, j, k, l, f, p: - f.eval_hp_loop(i, j) - 100,
15 RNA.DECOMP_PAIR_IL:
lambda i, j, k, l, f, p: - f.eval_int_loop(i, j, k, l) - 100,
16 RNA.DECOMP_PAIR_ML:
lambda i, j, k, l, f, p: - p.MLclosing - p.MLintern[0] - (j - i - k + l - 2) * p.MLbase - 100,
17 RNA.DECOMP_ML_ML_STEM:
lambda i, j, k, l, f, p: - p.MLintern[0] - (l - k - 1) * p.MLbase,
18 RNA.DECOMP_ML_STEM:
lambda i, j, k, l, f, p: - p.MLintern[0] - (j - i - k + l) * p.MLbase,
19 RNA.DECOMP_ML_ML:
lambda i, j, k, l, f, p: - (j - i - k + l) * p.MLbase,
20 RNA.DECOMP_ML_UP:
lambda i, j, k, l, f, p: - (j - i + 1) * p.MLbase,
21 RNA.DECOMP_EXT_STEM:
lambda i, j, k, l, f, p: - f.E_ext_loop(k, l),
22 RNA.DECOMP_EXT_STEM_EXT:
lambda i, j, k, l, f, p: - f.E_ext_loop(i, k),
23 RNA.DECOMP_EXT_EXT_STEM:
lambda i, j, k, l, f, p: - f.E_ext_loop(l, j),
24 RNA.DECOMP_EXT_EXT_STEM1:
lambda i, j, k, l, f, p: - f.E_ext_loop(l, j-1),
28 def MaximumMatching(i, j, k, l, d, data):
29 return revert_NN[d](i, j, k, l, data[
'dummy'], data[
'params'])
32 fc = RNA.fold_compound(seq1)
35 fc.sc_add_f(MaximumMatching)
36 fc.sc_add_data(mm_data,
None)
42 print "%s\n%s (MM: %d)\n" % (seq1, s, -mm)