# Parsing and Comparing - Functions to Manipulate Structures

## Representations of Secondary Structures

The standard representation of a secondary structure is the bracket notation, where matching brackets symbolize base pairs and unpaired bases are shown as dots. Alternatively, one may use two types of node labels, 'P' for paired and 'U' for unpaired; a dot is then replaced by '(U)', and each closed bracket is assigned an additional identifier 'P'. We call this the expanded notation. In Fontana et al. (1993) a condensed representation of the secondary structure is proposed, the so-called homeomorphically irreducible tree (HIT) representation. Here a stack is represented as a single pair of matching brackets labeled 'P' and weighted by the number of base pairs. Correspondingly, a contiguous strain of unpaired bases is shown as one pair of matching brackets labeled 'U' and weighted by its length. Generally any string consisting of matching brackets and identifiers is equivalent to a plane tree with as many different types of nodes as there are identifiers.

Bruce Shapiro (1988) proposed a coarse grained representation, which, does not retain the full information of the secondary structure. He represents the different structure elements by single matching brackets and labels them as 'H' (hairpin loop), 'I' (interior loop), 'B' (bulge), 'M' (multi-loop), and 'S' (stack). We extend his alphabet by an extra letter for external elements 'E'. Again these identifiers may be followed by a weight corresponding to the number of unpaired bases or base pairs in the structure element. All tree representations (except for the dot-bracket form) can be encapsulated into a virtual root (labeled 'R'), see the example below.

The following example illustrates the different linear tree representations used by the package. All lines show the same secondary structure.

    a) .((((..(((...)))..((..)))).)).
       (U)(((((U)(U)((((U)(U)(U)P)P)P)(U)(U)(((U)(U)P)P)P)P)(U)P)P)(U)
    b) (U)(((U2)((U3)P3)(U2)((U2)P2)P2)(U)P2)(U)
    c) (((H)(H)M)B)
       ((((((H)S)((H)S)M)S)B)S)
       (((((((H)S)((H)S)M)S)B)S)E)
    d) ((((((((H3)S3)((H2)S2)M4)S2)B1)S2)E2)R)

Above: Tree representations of secondary structures. a) Full structure: the first line shows the more convenient condensed notation which is used by our programs; the second line shows the rather clumsy expanded notation for completeness, b) HIT structure, c) different versions of coarse grained structures: the second line is exactly Shapiro's representation, the first line is obtained by neglecting the stems. Since each loop is closed by a unique stem, these two lines are equivalent. The third line is an extension taking into account also the external digits. d) weighted coarse structure, this time including the virtual root.

For the output of aligned structures from string editing, different representations are needed, where we put the label on both sides. The above examples for tree representations would then look like:

    a) (UU)(P(P(P(P(UU)(UU)(P(P(P(UU)(UU)(UU)P)P)P)(UU)(UU)(P(P(UU)(U...
    b) (UU)(P2(P2(U2U2)(P2(U3U3)P3)(U2U2)(P2(U2U2)P2)P2)(UU)P2)(UU)
    c) (B(M(HH)(HH)M)B)
       (S(B(S(M(S(HH)S)(S(HH)S)M)S)B)S)
       (E(S(B(S(M(S(HH)S)(S(HH)S)M)S)B)S)E)
    d) (R(E2(S2(B1(S2(M4(S3(H3)S3)((H2)S2)M4)S2)B1)S2)E2)R)

Aligned structures additionally contain the gap character '_'.

## Parsing and Coarse Graining of Structures

Several functions are provided for parsing structures and converting to different representations.

    char  *expand_Full(const char *structure)

Convert the full structure from bracket notation to the expanded notation including root.

    char *b2HIT (const char *structure)

Converts the full structure from bracket notation to the HIT notation including root.

    char *b2C (const char *structure)

Converts the full structure from bracket notation to the a coarse grained notation using the 'H' 'B' 'I' 'M' and 'R' identifiers.

    char *b2Shapiro (const char *structure)

Converts the full structure from bracket notation to the weighted coarse grained notation using the 'H' 'B' 'I' 'M' 'S' 'E' and 'R' identifiers.

    char  *expand_Shapiro (const char *coarse);

Inserts missing 'S' identifiers in unweighted coarse grained structures as obtained from b2C().

    char *add_root (const char *structure)

Adds a root to an un-rooted tree in any except bracket notation.

    char  *unexpand_Full (const char *ffull)

Restores the bracket notation from an expanded full or HIT tree, that is any tree using only identifiers 'U' 'P' and 'R'.

    char  *unweight (const char *wcoarse)

Strip weights from any weighted tree.

    void   unexpand_aligned_F (char *align[2])

Converts two aligned structures in expanded notation.

    void   parse_structure (const char *structure)

Collects a statistic of structure elements of the full structure in bracket notation.

See also:
  RNAstruct.h for prototypes and more detailed description

## Distance Measures

A simple measure of dissimilarity between secondary structures of equal length is the base pair distance, given by the number of pairs present in only one of the two structures being compared. I.e. the number of base pairs that have to be opened or closed to transform one structure into the other. It is therefore particularly useful for comparing structures on the same sequence. It is implemented by

    int bp_distance(const char *str1,
                    const char *str2)

Compute the "base pair" distance between two secondary structures s1 and s2.

For other cases a distance measure that allows for gaps is preferable. We can define distances between structures as edit distances between trees or their string representations. In the case of string distances this is the same as "sequence alignment". Given a set of edit operations and edit costs, the edit distance is given by the minimum sum of the costs along an edit path converting one object into the other. Edit distances like these always define a metric. The edit operations used by us are insertion, deletion and replacement of nodes. String editing does not pay attention to the matching of brackets, while in tree editing matching brackets represent a single node of the tree. Tree editing is therefore usually preferable, although somewhat slower. String edit distances are always smaller or equal to tree edit distances.

The different level of detail in the structure representations defined above naturally leads to different measures of distance. For full structures we use a cost of 1 for deletion or insertion of an unpaired base and 2 for a base pair. Replacing an unpaired base for a pair incurs a cost of 1.

Two cost matrices are provided for coarse grained structures:

    /*  Null,   H,   B,   I,   M,   S,   E    */
       {   0,   2,   2,   2,   2,   1,   1},   /* Null replaced */
       {   2,   0,   2,   2,   2, INF, INF},   /* H    replaced */
       {   2,   2,   0,   1,   2, INF, INF},   /* B    replaced */
       {   2,   2,   1,   0,   2, INF, INF},   /* I    replaced */
       {   2,   2,   2,   2,   0, INF, INF},   /* M    replaced */
       {   1, INF, INF, INF, INF,   0, INF},   /* S    replaced */
       {   1, INF, INF, INF, INF, INF,   0},   /* E    replaced */

    /* Null,   H,   B,   I,   M,   S,   E   */
       {   0, 100,   5,   5,  75,   5,   5},   /* Null replaced */
       { 100,   0,   8,   8,   8, INF, INF},   /* H    replaced */
       {   5,   8,   0,   3,   8, INF, INF},   /* B    replaced */
       {   5,   8,   3,   0,   8, INF, INF},   /* I    replaced */
       {  75,   8,   8,   8,   0, INF, INF},   /* M    replaced */
       {   5, INF, INF, INF, INF,   0, INF},   /* S    replaced */
       {   5, INF, INF, INF, INF, INF,   0},   /* E    replaced */

The lower matrix uses the costs given in Shapiro (1990). All distance functions use the following global variables:

    int  cost_matrix;

Specify the cost matrix to be used for distance calculations.

    int   edit_backtrack;

Produce an alignment of the two structures being compared by tracing the editing path giving the minimum distance.

    char *aligned_line[4];

Contains the two aligned structures after a call to one of the distance functions with edit_backtrack set to 1.

See also:
  utils.h, dist_vars.h and stringdist.h for more details

### 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_distance().

    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.

See also:
  dist_vars.h and treedist.h for prototypes and more detailed descriptions

### 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.

See also:
  dist_vars.h and stringdist.h for prototypes and more detailed descriptions

### Functions for Comparison of Base Pair Probabilities

For comparison of base pair probability matrices, the matrices are first condensed into probability profiles which are the compared by alignment.

    float *Make_bp_profile_bppm ( double *bppm,
                                  int length)

condense pair probability matrix into a vector containing probabilities for upstream paired, downstream paired and unpaired.

    float profile_edit_distance ( const float *T1,
                                  const float *T2)

Align the 2 probability profiles T1, T2
.

See also:
  ProfileDist.h for prototypes and more details of the above functions

Next Page: Utilities