RNAlib-2.5.1
C Examples

Hello World Examples

helloworld_mfe.c

The following is an example showing the minimal requirements to compute the Minimum Free Energy (MFE) and corresponding secondary structure of an RNA sequence

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ViennaRNA/fold.h>
int
main()
{
/* The RNA sequence */
char *seq = "GAGUAGUGGAACCAGGCUAUGUUUGUGACUCGCAGACUAACA";
/* allocate memory for MFE structure (length + 1) */
char *structure = (char *)vrna_alloc(sizeof(char) * (strlen(seq) + 1));
/* predict Minmum Free Energy and corresponding secondary structure */
float mfe = vrna_fold(seq, structure);
/* print sequence, structure and MFE */
printf("%s\n%s [ %6.2f ]\n", seq, structure, mfe);
/* cleanup memory */
free(structure);
return 0;
}
MFE calculations for single RNA sequences.
float vrna_fold(const char *sequence, char *structure)
Compute Minimum Free Energy (MFE), and a corresponding secondary structure for an RNA sequence.
void * vrna_alloc(unsigned size)
Allocate space safely.
General utility- and helper-functions used throughout the ViennaRNA Package.
See also
examples/helloworld_mfe.c in the source code tarball

helloworld_mfe_comparative.c

Instead of using a single sequence as done above, this example predicts a consensus structure for a multiple sequence alignment

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int
main()
{
/* The RNA sequence alignment */
const char *sequences[] = {
"CUGCCUCACAACGUUUGUGCCUCAGUUACCCGUAGAUGUAGUGAGGGU",
"CUGCCUCACAACAUUUGUGCCUCAGUUACUCAUAGAUGUAGUGAGGGU",
"---CUCGACACCACU---GCCUCGGUUACCCAUCGGUGCAGUGCGGGU",
NULL /* indicates end of alignment */
};
/* compute the consensus sequence */
char *cons = consensus(sequences);
/* allocate memory for MFE consensus structure (length + 1) */
char *structure = (char *)vrna_alloc(sizeof(char) * (strlen(sequences[0]) + 1));
/* predict Minmum Free Energy and corresponding secondary structure */
float mfe = vrna_alifold(sequences, structure);
/* print consensus sequence, structure and MFE */
printf("%s\n%s [ %6.2f ]\n", cons, structure, mfe);
/* cleanup memory */
free(cons);
free(structure);
return 0;
}
Functions for comparative structure prediction using RNA sequence alignments.
float vrna_alifold(const char **sequences, char *structure)
Compute Minimum Free Energy (MFE), and a corresponding consensus secondary structure for an RNA seque...
Various utility- and helper-functions for sequence alignments and comparative structure prediction.
See also
examples/helloworld_mfe_comparative.c in the source code tarball

helloworld_probabilities.c

This example shows how to compute the partition function and base pair probabilities with minimal implementation effort.

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ViennaRNA/fold.h>
int
main()
{
/* The RNA sequence */
char *seq = "GAGUAGUGGAACCAGGCUAUGUUUGUGACUCGCAGACUAACA";
/* allocate memory for pairing propensity string (length + 1) */
char *propensity = (char *)vrna_alloc(sizeof(char) * (strlen(seq) + 1));
/* pointers for storing and navigating through base pair probabilities */
vrna_ep_t *ptr, *pair_probabilities = NULL;
float en = vrna_pf_fold(seq, propensity, &pair_probabilities);
/* print sequence, pairing propensity string and ensemble free energy */
printf("%s\n%s [ %6.2f ]\n", seq, propensity, en);
/* print all base pairs with probability above 50% */
for (ptr = pair_probabilities; ptr->i != 0; ptr++)
if (ptr->p > 0.5)
printf("p(%d, %d) = %g\n", ptr->i, ptr->j, ptr->p);
/* cleanup memory */
free(pair_probabilities);
free(propensity);
return 0;
}
float vrna_pf_fold(const char *sequence, char *structure, vrna_ep_t **pl)
Compute Partition function (and base pair probabilities) for an RNA sequence using a comparative met...
int i
Start position (usually 5' nucleotide that starts the element, e.g. base pair)
Definition: structures.h:483
float p
Probability of the element.
Definition: structures.h:485
int j
End position (usually 3' nucleotide that ends the element, e.g. base pair)
Definition: structures.h:484
Data structure representing a single entry of an element probability list (e.g. list of pair probabil...
Definition: structures.h:482
Partition function implementations.
See also
examples/helloworld_probabilities.c in the source code tarball

First Steps with the Fold Compound

fold_compound_mfe.c

Instead of calling the simple MFE folding interface vrna_fold(), this example shows how to first create a vrna_fold_compound_t container with the RNA sequence to finally compute the MFE using this container. This is especially useful if non-default model settings are applied or the dynamic programming (DP) matrices of the MFE prediction are required for post-processing operations, or other tasks on the same sequence will be performed.

#include <stdlib.h>
#include <stdio.h>
#include <ViennaRNA/mfe.h>
int
main()
{
/* initialize random number generator */
/* Generate a random sequence of 50 nucleotides */
char *seq = vrna_random_string(50, "ACGU");
/* Create a fold compound for the sequence */
/* allocate memory for MFE structure (length + 1) */
char *structure = (char *)vrna_alloc(sizeof(char) * (strlen(seq) + 1));
/* predict Minmum Free Energy and corresponding secondary structure */
float mfe = vrna_mfe(fc, structure);
/* print sequence, structure and MFE */
printf("%s\n%s [ %6.2f ]\n", seq, structure, mfe);
/* cleanup memory */
free(seq);
free(structure);
return 0;
}
The Basic Fold Compound API.
void vrna_fold_compound_free(vrna_fold_compound_t *fc)
Free memory occupied by a vrna_fold_compound_t.
vrna_fold_compound_t * vrna_fold_compound(const char *sequence, const vrna_md_t *md_p, unsigned int options)
Retrieve a vrna_fold_compound_t data structure for single sequences and hybridizing sequences.
#define VRNA_OPTION_DEFAULT
Option flag to specify default settings/requirements.
Definition: fold_compound.h:375
The most basic data structure required by many functions throughout the RNAlib.
Definition: fold_compound.h:148
float vrna_mfe(vrna_fold_compound_t *vc, char *structure)
Compute minimum free energy and an appropriate secondary structure of an RNA sequence,...
char * vrna_random_string(int l, const char symbols[])
Create a random string using characters from a specified symbol set.
void vrna_init_rand(void)
Initialize seed for random number generator.
Compute Minimum Free energy (MFE) and backtrace corresponding secondary structures from RNA sequence ...
General utility- and helper-functions for RNA sequence and structure strings used throughout the Vien...
See also
examples/fold_compound_mfe.c in the source code tarball

fold_compound_md.c

In the following, we change the model settings (model details) to a temperature of 25 Degree Celcius, and activate G-Quadruplex precition.

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ViennaRNA/mfe.h>
int
main()
{
/* initialize random number generator */
/* Generate a random sequence of 50 nucleotides */
char *seq = vrna_random_string(50, "ACGU");
/* allocate memory for MFE structure (length + 1) */
char *structure = (char *)vrna_alloc(sizeof(char) * (strlen(seq) + 1));
/* create a new model details structure to store the Model Settings */
/* ALWAYS set default model settings first! */
/* change temperature and activate G-Quadruplex prediction */
md.temperature = 25.0; /* 25 Deg Celcius */
md.gquad = 1; /* Turn-on G-Quadruples support */
/* create a fold compound */
/* predict Minmum Free Energy and corresponding secondary structure */
float mfe = vrna_mfe(fc, structure);
/* print sequence, structure and MFE */
printf("%s\n%s [ %6.2f ]\n", seq, structure, mfe);
/* cleanup memory */
free(structure);
return 0;
}
double temperature
The temperature used to scale the thermodynamic parameters.
Definition: model.h:181
int gquad
Include G-quadruplexes in structure prediction.
Definition: model.h:214
void vrna_md_set_default(vrna_md_t *md)
Apply default model details to a provided vrna_md_t data structure.
The data structure that contains the complete model details used throughout the calculations.
Definition: model.h:180
The model details data structure and its corresponding modifiers.
See also
examples/fold_compound_md.c in the source code tarball

Writing Callback Functions

callback_subopt.c

Here is a basic example how to use the callback mechanism in vrna_subopt_cb(). It simply defines a callback function (see interface definition for vrna_subopt_callback) that prints the result and increases a counter variable.

#include <stdlib.h>
#include <stdio.h>
void
subopt_callback(const char *structure,
float energy,
void *data)
{
/* simply print the result and increase the counter variable by 1 */
if (structure)
printf("%d.\t%s\t%6.2f\n", (*((int *)data))++, structure, energy);
}
int
main()
{
/* initialize random number generator */
/* Generate a random sequence of 50 nucleotides */
char *seq = vrna_random_string(50, "ACGU");
/* Create a fold compound for the sequence */
int counter = 0;
/*
* call subopt to enumerate all secondary structures in an energy band of
* 5 kcal/mol of the MFE and pass it the address of the callback and counter
* variable
*/
vrna_subopt_cb(fc, 500, &subopt_callback, (void *)&counter);
/* cleanup memory */
free(seq);
return 0;
}
void vrna_subopt_cb(vrna_fold_compound_t *fc, int delta, vrna_subopt_callback *cb, void *data)
Generate suboptimal structures within an energy band arround the MFE.
RNAsubopt and density of states declarations.
See also
examples/callback_subopt.c in the source code tarball

Application of Soft Constraints

soft_constraints_up.c

In this example, a random RNA sequence is generated to predict its MFE under the constraint that a particular nucleotide receives an additional bonus energy if it remains unpaired.

#include <stdlib.h>
#include <stdio.h>
#include <ViennaRNA/mfe.h>
int
main()
{
/* initialize random number generator */
/* Generate a random sequence of 50 nucleotides */
char *seq = vrna_random_string(50, "ACGU");
/* Create a fold compound for the sequence */
/* Add soft constraint of -1.7 kcal/mol to nucleotide 5 whenever it appears in an unpaired context */
/* allocate memory for MFE structure (length + 1) */
char *structure = (char *)vrna_alloc(sizeof(char) * 51);
/* predict Minmum Free Energy and corresponding secondary structure */
float mfe = vrna_mfe(fc, structure);
/* print seqeunce, structure and MFE */
printf("%s\n%s [ %6.2f ]\n", seq, structure, mfe);
/* cleanup memory */
free(seq);
free(structure);
return 0;
}
int vrna_sc_add_up(vrna_fold_compound_t *vc, int i, FLT_OR_DBL energy, unsigned int options)
Add soft constraints for unpaired nucleotides.
Functions and data structures for secondary structure soft constraints.
See also
examples/soft_constraints_up.c in the source code tarball

Other Examples

example1.c

A more extensive example including MFE, Partition Function, and Centroid structure prediction.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ViennaRNA/eval.h>
#include <ViennaRNA/fold.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));
/* get a vrna_fold_compound with default settings */
/* call MFE function */
double mfe = (double)vrna_mfe(vc, mfe_structure);
printf("%s\n%s (%6.2f)\n", seq, mfe_structure, mfe);
/* rescale parameters for Boltzmann factors */
/* call PF function */
FLT_OR_DBL en = vrna_pf(vc, prob_string);
/* print probability string and free energy of ensemble */
printf("%s (%6.2f)\n", prob_string, en);
/* compute centroid structure */
double dist;
char *cent = vrna_centroid(vc, &dist);
/* print centroid structure, its free energy and mean distance to the ensemble */
printf("%s (%6.2f d=%6.2f)\n", cent, vrna_eval_structure(vc, cent), dist);
/* free centroid structure */
free(cent);
/* free pseudo dot-bracket probability string */
free(prob_string);
/* free mfe structure */
free(mfe_structure);
/* free memory occupied by vrna_fold_compound */
return EXIT_SUCCESS;
}
Use ViennaRNA/datastructures/basic.h instead.
Functions and variables related to energy evaluation of sequence/structure pairs.
char * vrna_centroid(vrna_fold_compound_t *vc, double *dist)
Get the centroid structure of the ensemble.
double FLT_OR_DBL
Typename for floating point number in partition function computations.
Definition: basic.h:43
void vrna_exp_params_rescale(vrna_fold_compound_t *vc, double *mfe)
Rescale Boltzmann factors for partition function computations.
float vrna_eval_structure(vrna_fold_compound_t *fc, const char *structure)
Calculate the free energy of an already folded RNA.
FLT_OR_DBL vrna_pf(vrna_fold_compound_t *vc, char *structure)
Compute the partition function for a given RNA sequence, or sequence alignment.
Functions to deal with sets of energy parameters.
See also
examples/example1.c in the source code tarball

Deprecated Examples

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include "utils.h"
#include "fold_vars.h"
#include "fold.h"
#include "part_func.h"
#include "inverse.h"
#include "RNAstruct.h"
#include "treedist.h"
#include "stringdist.h"
#include "profiledist.h"
void
main()
{
char *seq1 = "CGCAGGGAUACCCGCG", *seq2 = "GCGCCCAUAGGGACGC",
*struct1, *struct2, *xstruc;
float e1, e2, tree_dist, string_dist, profile_dist, kT;
Tree *T1, *T2;
swString *S1, *S2;
float *pf1, *pf2;
FLT_OR_DBL *bppm;
/* fold at 30C instead of the default 37C */
temperature = 30.; /* must be set *before* initializing */
/* allocate memory for structure and fold */
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_arrays(); /* free arrays used in fold() */
/* produce tree and string representations for comparison */
xstruc = expand_Full(struct1);
T1 = make_tree(xstruc);
S1 = Make_swString(xstruc);
free(xstruc);
xstruc = expand_Full(struct2);
T2 = make_tree(xstruc);
S2 = Make_swString(xstruc);
free(xstruc);
/* calculate tree edit distance and aligned structures with gaps */
tree_dist = tree_edit_distance(T1, T2);
free_tree(T1);
free_tree(T2);
printf("%s\n%s %3.2f\n", aligned_line[0], aligned_line[1], tree_dist);
/* same thing using string edit (alignment) distance */
string_dist = string_edit_distance(S1, S2);
free(S1);
free(S2);
printf("%s mfe=%5.2f\n%s mfe=%5.2f dist=%3.2f\n",
aligned_line[0], e1, aligned_line[1], e2, string_dist);
/* for longer sequences one should also set a scaling factor for
* partition function folding, e.g: */
kT = (temperature + 273.15) * 1.98717 / 1000.; /* kT in kcal/mol */
pf_scale = exp(-e1 / kT / strlen(seq1));
/* calculate partition function and base pair probabilities */
e1 = pf_fold(seq1, struct1);
/* get the base pair probability matrix for the previous run of pf_fold() */
bppm = export_bppm();
pf1 = Make_bp_profile_bppm(bppm, strlen(seq1));
e2 = pf_fold(seq2, struct2);
/* get the base pair probability matrix for the previous run of pf_fold() */
bppm = export_bppm();
pf2 = Make_bp_profile_bppm(bppm, strlen(seq2));
free_pf_arrays(); /* free space allocated for pf_fold() */
profile_dist = profile_edit_distance(pf1, pf2);
printf("%s free energy=%5.2f\n%s free energy=%5.2f dist=%3.2f\n",
aligned_line[0], e1, aligned_line[1], e2, profile_dist);
}
Parsing and Coarse Graining of Structures.
int edit_backtrack
Produce an alignment of the two structures being compared by tracing the editing path giving the mini...
char * aligned_line[4]
Contains the two aligned structures after a call to one of the distance functions with edit_backtrack...
Here all all declarations of the global variables used throughout RNAlib.
void free_arrays(void)
Free arrays for mfe folding.
float fold(const char *sequence, char *structure)
Compute minimum free energy and an appropriate secondary structure of an RNA sequence.
double temperature
Rescale energy parameters to a temperature in degC.
double pf_scale
A scaling factor used by pf_fold() to avoid overflows.
FLT_OR_DBL * export_bppm(void)
Get a pointer to the base pair probability array.
float pf_fold(const char *sequence, char *structure)
Compute the partition function of an RNA sequence.
void free_pf_arrays(void)
Free arrays for the partition function recursions.
void unexpand_aligned_F(char *align[2])
Converts two aligned structures in expanded notation.
char * expand_Full(const char *structure)
Convert the full structure from bracket notation to the expanded notation including root.
Inverse folding routines.
float * Make_bp_profile_bppm(FLT_OR_DBL *bppm, int length)
condense pair probability matrix into a vector containing probabilities for unpaired,...
void free_profile(float *T)
free space allocated in Make_bp_profile
float profile_edit_distance(const float *T1, const float *T2)
Align the 2 probability profiles T1, T2 .
Functions for String Alignment.
swString * Make_swString(char *string)
Convert a structure into a format suitable for string_edit_distance().
float string_edit_distance(swString *T1, swString *T2)
Calculate the string edit distance of T1 and T2.
Tree data structure.
Definition: dist_vars.h:49
Some other data structure.
Definition: dist_vars.h:57
Functions for Tree Edit Distances.
Tree * make_tree(char *struc)
Constructs a Tree ( essentially the postorder list ) of the structure 'struc', for use in tree_edit_d...
float tree_edit_distance(Tree *T1, Tree *T2)
Calculates the edit distance of the two trees.
void free_tree(Tree *t)
Free the memory allocated for Tree t.
void * space(unsigned size)
Allocate space safely.
See also
examples/example_old.c in the source code tarball