关闭

Sequence Alignment

标签: C++ Sequence Alignme
326人阅读 评论(0) 收藏 举报
分类:
</pre><p class="p1"><pre name="code" class="cpp">#include <iostream>
#include <vector>
#include <string>
#include <algorithm>


using namespace std;

// Edit penalty:
// gap penalty: theta
// mismatch penalty: alpha
int optimalAlignment(string s1, string s2, int theta, int alpha, vector<string> &align)
{
    int m = s1.length();
    int n = s2.length();
    vector<vector<int>> M(m + 1, vector<int>(n + 1, 0));
    for (int i = 0; i <= m; ++i)
        M[i][0] = i * theta;
    for (int j = 0; j <= n; ++j)
        M[0][j] = j * theta;
    
    for (int i = 1; i <= m; ++i)
    {
        for (int j = 1; j <= n; ++j)
        {
            int cost = 0; int kind = 1;
            if (s1[i - 1] == s2[j - 1])
                cost = M[i - 1][j - 1];
            else
                cost = M[i - 1][j - 1] + alpha;
            cost = min(cost, theta + M[i - 1][j]);
            cost = min(cost, theta + M[i][j - 1]);
            M[i][j] = cost;
            
        }
    }
    
    int i = m, j = n;
    // backtracking to get the solution
    while (i >= 1 || j >= 1)
    {
        if ((i > 0 && M[i][j] == theta + M[i - 1][j]) || j == 0)
        {
            align[0].push_back(s1[i - 1]);
            align[1].push_back('-');
            --i;
        }
        else if ((j > 0 && M[i][j] == theta + M[i][j - 1]) || i == 0)
        {
            align[0].push_back('-');
            align[1].push_back(s2[j - 1]);
            --j;
        }
        else if (M[i][j] == M[i - 1][j - 1] + alpha || M[i][j] == M[i - 1][j - 1] )
        {
            align[0].push_back(s1[i - 1]);
            align[1].push_back(s2[j - 1]);
            --i, --j;
        }
    }
    reverse(align[0].begin(), align[0].end());
    reverse(align[1].begin(), align[1].end());
    
    return M[m][n];
}

// x   y  cost
// ------------
// A   T   1
// A   A   0
// C   -   2
// A   A   0
// G   G   0
// T   G   1
// T   T   0
// A   -   2
// C   C   0
// C   A   1
//        ---
//		   7

int main()
{
    string x = "";
    string y = "TAAGGTCA";
    string x1 = "CTACCG";
    string y1 = "TACATG";
    vector<string> ret;
    ret.resize(2);
    cout << "s1: " << x << endl;
    cout << "s2: " << y << endl;
    cout << "The minimum cost of alignment: " << optimalAlignment(x, y, 2, 1, ret) << endl;
    cout << ret[0] << endl;
    cout << ret[1] << endl;
    ret.clear();
    ret.resize(2);
    cout << "s1: " << x1 << endl;
    cout << "s2: " << y1 << endl;
    cout << "The minimum cost of alignment: " << optimalAlignment(x1, y1, 2, 1, ret) << endl;
    cout << ret[0] << endl;
    cout << ret[1] << endl;

    return 0;
}

Reference:

http://www.cs.princeton.edu/courses/archive/fall14/cos126/assignments/sequence.html

0
0
查看评论

Sequence alignment

concept  In computer realm,Sequence is not continuous,on the contrary,String is continuous;in the biology,sequence is called gapped sequence,string is...
  • u012562273
  • u012562273
  • 2017-02-20 16:54
  • 364

List of sequence alignment software

List of sequence alignment software From Wikipedia, the free encyclopedia   (Redirected from Sequence alignment software) Jump to: na...
  • dongliang1991
  • dongliang1991
  • 2013-10-10 22:02
  • 1599

sequence alignment

# this problem is also called 'edit distance' # consider sequence X = (x1,x2,..,xm) and Y=(y1,y2, ..., yn); # an alignment is a subset A belon...
  • u013805817
  • u013805817
  • 2017-10-20 06:01
  • 118

Linear Space Sequence Alignment

For explanation to linear space sequence alignment, please refer to http://ai.stanford.edu/~serafim/CS262_2007/notes/lecture3.pdf. ...
  • bonny95
  • bonny95
  • 2013-04-06 05:53
  • 786

常用生物信息学格式介绍

转载于:http://ju.outofmemory.cn/entry/193943前言  在各个行业都是有行业标准的,这样才能统一规范而方便后面的分析,在生物信息学领域中主要是各种大量序列数据、注释数据等,这些都是有特定的格式去表示,下面列举几种常见的格式。了解这些是进行后续生物信息学分析的必备知识...
  • u012150360
  • u012150360
  • 2017-04-23 23:11
  • 859

poj 1080 dp(sequence alignment)

题意:经典sequence alignment。给定两个ACGT串,求其最优距离。其中字母配对值给定。 思路:dp。 #include #include #define N 105 char s[N],t[N]; int dp[N][N],p[256][256],T; int max...
  • dumeichen
  • dumeichen
  • 2014-11-26 16:40
  • 304

alignment fault

什么是对齐异常? 简单来说,当CPU访问内存地址时,如果发现访问的地址是不对齐的,硬件(部分)就会自动触发对齐异常。对齐即要求被访问的地址满足其数据类型的位宽要求,比如要访问一个4字节int型的数据,但是提供的地址不是4字节对齐的,那就是不对齐了。也就是说要访问的数据的位宽长度是多少,那么访问的地...
  • fivedoumi
  • fivedoumi
  • 2016-12-14 21:32
  • 684

对对齐(alignment)的一些认识

关于内存地址对齐,尤其是struct中成员的对齐导致的struct的size问题很多人(包括我:()似乎都没有一个比较清晰的认识,所以产生了整理这方面思路和帖子的想法,下面的文字是资料、文档、实验和推测的混合体,有错误是肯定的:)。能给您提供一点帮助,是我最大的愿望。(有点麻了) 引: struct...
  • zhy05
  • zhy05
  • 2007-05-12 13:11
  • 761

sublime 插件 alignment自动对齐快捷键

1、http://blog.csdn.net/qdujunjie/article/details/38805323 2、http://segmentfault.com/q/1010000000145442
  • janicefan15
  • janicefan15
  • 2015-08-27 16:02
  • 1573

Alignment trap

“Alignment trap”:遇到字节对齐的问题,直译意思为“对齐陷阱”。  Linux下对Alignment trap的处理有下面几种方式:(cat /proc/cpu/alignment)  0 (ignored)  1 (warn)  2 (fixu...
  • chunlovenan
  • chunlovenan
  • 2015-07-20 11:24
  • 885
    个人资料
    • 访问:13566次
    • 积分:735
    • 等级:
    • 排名:千里之外
    • 原创:61篇
    • 转载:1篇
    • 译文:0篇
    • 评论:0条
    文章分类