Algorithms In Bioinformatics (Ch 1,2) 第二章课后题 2.7.7

题目描述

 Consider the follwing alignment:

                A C - G G T T A T -

                -  C T G G -  - A T C

ans the following score matrix.

  A C G T
A 1      
C -1 2    
G -1 -2 1  
T -2 -1 -2 1

(a) Suppose the gap penalty is -5-g for a gap of size g.Compute the alignment score for the above alignment using the above score matrix.

(b) Is the above alignment an optimal global alignmeng? If not ,what should be the optimal global alignment score ans the corresponding optimal global alignment.

(c) Give a score matrix and a gap penalty so that the above alignment is an optimal global alignment

(a) -5-1+2+-5-1+1+1-5-2+1+1+-5-1 = -19

(b)

#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<algorithm>
using namespace std;
struct node{
	int row;
	int col;
	int score;
};
void initial(map<pair<char,char>,int> &mp){
	for(int i=0;i<10;i++){
		char c1,c2;
		int s;
		cin>>c1>>c2>>s;
		mp[make_pair(c1,c2)] = s;
		mp[make_pair(c2,c1)] = s;
	}
}
int main(){
	string str1 = "ACGGTTAT",str2 = "CTGGATC";
	int row = str1.length(),col = str2.length();
	map<pair<char,char>,int> mp;
	map<pair<char,char>,int>::iterator it;
	initial(mp);
	node dp[row+1][col+1];
	memset(dp,0,sizeof(dp));
	for(int i=1;i<=row;i++){
		dp[i][0].score = -5-i;
		dp[i][0].col = 1;
	}
	for(int j=1;j<=col;j++){
		dp[0][j].score = -5-j;
		dp[0][j].row = 1;
	}
	for(int i=1;i<=row;i++){
		for(int j=1;j<=col;j++){
			char c1 = str1[i-1],c2 = str2[j-1];
			if(c1 == c2) dp[i][j].score = dp[i-1][j-1].score + mp[make_pair(c1,c2)];
			else{
				int tmp = INT_MIN,penalty = 0,flag = -1;
				//str2加空格 -> dp[i][j] = dp[i-1][j] + penalty
				penalty = (dp[i-1][j].col == 0)?-6:-1;
				if(dp[i-1][j].score+penalty > tmp){
					tmp = dp[i-1][j].score+penalty;
					flag = 0;
				}
				//str1加空格 -> dp[i][j] = dp[i][j-1] + penalty
				penalty = (dp[i][j-1].row == 0)?-6:-1;
				if(dp[i][j-1].score+penalty > tmp){
					tmp = dp[i][j-1].score+penalty;
					flag = 1;
				}
				//mismatch
				penalty = mp[make_pair(c1,c2)];
				if(dp[i-1][j-1].score+penalty > tmp){
					tmp = dp[i-1][j-1].score+penalty;
					flag = 2;
				}
				if(flag == 0) dp[i][j].col = dp[i-1][j].col+1;
				else if(flag == 1) dp[i][j].row = dp[i][j-1].row+1;
				else{
					dp[i][j].col = dp[i-1][j-1].col;
					dp[i][j].row = dp[i-1][j-1].row;
				}
				dp[i][j].score = tmp;
			}
		}
	}
	for(int i=0;i<=row;i++){
		for(int j=0;j<=col;j++) cout<<dp[i][j].score<<" ";
		cout<<endl;
	}
	return 0;
} 

猜你喜欢

转载自blog.csdn.net/weixin_35338624/article/details/90140931