关闭

RNA secondary structure(Dynamic Programming)

标签: Dynamic Programming
196人阅读 评论(0) 收藏 举报
分类:
//
//  main.cpp
//  RNA
//
//  Created by Longxiang Lyu on 6/1/16.
//  Copyright (c) 2016 Longxiang Lyu. All rights reserved.
//

#include <iostream>
#include <string>
#include <vector>

using namespace std;

bool complement(char c1, char c2)
{
    return  (c1 == 'A' && c2 == 'U') ||
            (c1 == 'U' && c2 == 'A') ||
            (c1 == 'C' && c2 == 'G') ||
            (c1 == 'G' && c2 == 'C');
}

void findSolution(const vector<vector<int>> &M, int i, int j, vector<pair<int, int>> &pairs)
{
    for (int t = i; t < j - 4; ++t)
    {
        if (M[i][j] == 1 + M[i][t - 1] + M[t + 1][j - 1])
        {
            pairs.push_back({t, j});
            findSolution(M, i, t - 1, pairs);
            findSolution(M, t + 1, j - 1, pairs);
        }
    }
}

int RNA(string seq, vector<pair<int, int>> &pairs)
{
    for (auto &c : seq)
        c = toupper(c);
    int n = static_cast<int>(seq.length() - 1);
    vector<vector<int>> M(n + 1, vector<int>(n + 1, 0));
    
    for (int interval = 5; interval <= n -1; ++interval)
    {
        for (int i = 1; i <= n - interval; ++i)
        {
            // compute M[i][j]
            int j = i + interval;
            int opt = M[i][j - 1];      // case 1: bj is not involved in pair
            
            
            for (int t = i; t < j - 4; ++t)
            {
                if (complement(seq[t], seq[j]) && opt < 1 + M[i][t - 1] + M[t + 1][j - 1])
                {
                    opt = 1 + M[i][t - 1] + M[t + 1][j - 1];        // case 2: choose the largest pair sum
                }
            }
            M[i][j] = opt;
        }
    }
    findSolution(M, 1, n, pairs);
    return M[1].back();
}

int main(int argc, const char * argv[])
{
    string seq = "*AUGUGGGCAU";
    vector<pair<int, int>> ret;
    cout << "Max number of pairs: " << RNA(seq, ret) << endl;
    for (auto a : ret)
    {
        cout << a.first << " - " << a.second << endl;
    }

    return 0;
}

Reference:

http://www.cs.princeton.edu/~wayne/kleinberg-tardos/pdf/06DynamicProgrammingI.pdf

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:7320次
    • 积分:592
    • 等级:
    • 排名:千里之外
    • 原创:54篇
    • 转载:1篇
    • 译文:0篇
    • 评论:0条
    文章分类