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>
int
main()
{
char *seq = "GAGUAGUGGAACCAGGCUAUGUUUGUGACUCGCAGACUAACA";
char *structure = (
char *)
vrna_alloc(
sizeof(
char) * (strlen(seq) + 1));
printf("%s\n%s [ %6.2f ]\n", seq, structure, mfe);
free(structure);
return 0;
}
- 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()
{
const char *sequences[] = {
"CUGCCUCACAACGUUUGUGCCUCAGUUACCCGUAGAUGUAGUGAGGGU",
"CUGCCUCACAACAUUUGUGCCUCAGUUACUCAUAGAUGUAGUGAGGGU",
"---CUCGACACCACU---GCCUCGGUUACCCAUCGGUGCAGUGCGGGU",
NULL
};
char *cons = consensus(sequences);
char *structure = (
char *)
vrna_alloc(
sizeof(
char) * (strlen(sequences[0]) + 1));
printf("%s\n%s [ %6.2f ]\n", cons, structure, mfe);
free(cons);
free(structure);
return 0;
}
- 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>
int
main()
{
char *seq = "GAGUAGUGGAACCAGGCUAUGUUUGUGACUCGCAGACUAACA";
char *propensity = (
char *)
vrna_alloc(
sizeof(
char) * (strlen(seq) + 1));
float en =
vrna_pf_fold(seq, propensity, &pair_probabilities);
printf("%s\n%s [ %6.2f ]\n", seq, propensity, en);
for (ptr = pair_probabilities; ptr->
i != 0; ptr++)
printf(
"p(%d, %d) = %g\n", ptr->
i, ptr->
j, ptr->
p);
free(pair_probabilities);
free(propensity);
return 0;
}
- 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>
int
main()
{
char *structure = (
char *)
vrna_alloc(
sizeof(
char) * (strlen(seq) + 1));
printf("%s\n%s [ %6.2f ]\n", seq, structure, mfe);
free(seq);
free(structure);
return 0;
}
- 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>
int
main()
{
char *structure = (
char *)
vrna_alloc(
sizeof(
char) * (strlen(seq) + 1));
printf("%s\n%s [ %6.2f ]\n", seq, structure, mfe);
free(structure);
return 0;
}
- 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)
{
if (structure)
printf("%d.\t%s\t%6.2f\n", (*((int *)data))++, structure, energy);
}
int
main()
{
int counter = 0;
free(seq);
return 0;
}
- 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>
int
main()
{
char *structure = (
char *)
vrna_alloc(
sizeof(
char) * 51);
printf("%s\n%s [ %6.2f ]\n", seq, structure, mfe);
free(seq);
free(structure);
return 0;
}
- 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>
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;
}
- 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"
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",
pf_scale = exp(-e1 / kT / strlen(seq1));
printf("%s free energy=%5.2f\n%s free energy=%5.2f dist=%3.2f\n",
}
- See also
examples/example_old.c
in the source code tarball