#include <iostream>
#include <string>
using namespace std;
#include "../utils/struct.h"
#include "../utils/commons.h"
int main()
{
const int MAX_EDGE = 3;
const int MAX_NODE = 5;
DEFINE_INDEX;
DEFINE_LINEAR_PENALTY;
DEFINE_NODE(MAX_EDGE);
DEFINE_GRAPH(MAX_NODE);
DEFINE_WAVEFRONT_SET(100);
const int pattern_size = 5;
const int sequence_size = 5;
const int Bool_size = (sequence_size + 1) * (sequence_size + 1);
class GWF {
public:
Wavefront_set EXTEND(Wavefront_set original_wavefront_set, bool D[], int pattern_size, int sequence_size, char* t, Graph q)
{
Index temp_index;
Wavefront_set final_wavefront_set;
final_wavefront_set.index_num = 0;
final_wavefront_set.score = original_wavefront_set.score;
while (original_wavefront_set.index_num > 0)
{
temp_index = original_wavefront_set.index[original_wavefront_set.index_num - 1];
original_wavefront_set.index_num--;
if (D[(pattern_size + 1) * temp_index.h + temp_index.u])
continue;
D[(pattern_size + 1) * temp_index.h + temp_index.u] = 1;
int n = 0;
for (int i = 0; i < q.node[temp_index.u].edgenum; i++)
{
int w = q.node[temp_index.u].next[i];
if (temp_index.h <= sequence_size - 1 && tran(t[temp_index.h]) == q.node[w].base)
{
original_wavefront_set.index[original_wavefront_set.index_num].h = temp_index.h + 1;
original_wavefront_set.index[original_wavefront_set.index_num].u = w;
original_wavefront_set.index_num++;
n++;
}
}
if (q.node[temp_index.u].edgenum == 0 || q.node[temp_index.u].edgenum > n)
{
final_wavefront_set.index[final_wavefront_set.index_num] = temp_index;
final_wavefront_set.index_num++;
}
}
return final_wavefront_set;
}
void LINEAR_NEXT(Wavefront_set temp_wavefront_set[], bool D[], char* t, int sequence_size, int pattern_size, penalty penalty, int score, int Hash_size, Graph q)
{
int H_position = score % Hash_size;
int hash_gap = (score + penalty.gap) % Hash_size;
int hash_mismatch = (score + penalty.mismatch) % Hash_size;
for (int i = 0; i < temp_wavefront_set[H_position].index_num; i++)
{
Index temp_index = temp_wavefront_set[H_position].index[i];
if (temp_index.h <= sequence_size - 1 && !D[(temp_index.h + 1) * pattern_size + temp_index.u])
{
temp_wavefront_set[hash_gap].index[temp_wavefront_set[hash_gap].index_num] = { temp_index.h + 1,temp_index.u };
temp_wavefront_set[hash_gap].index_num++;
}
for (int j = 0; j < q.node[temp_index.u].edgenum; j++)
{
if (!D[temp_index.h * pattern_size + q.node[temp_index.u].next[j]])
{
temp_wavefront_set[hash_gap].index[temp_wavefront_set[hash_gap].index_num] = { temp_index.h,q.node[temp_index.u].next[j] };
temp_wavefront_set[hash_gap].index_num++;
}
if (temp_index.h <= sequence_size - 1 && !D[(temp_index.h + 1) * pattern_size + q.node[temp_index.u].next[j]])
{
temp_wavefront_set[hash_mismatch].index[temp_wavefront_set[hash_mismatch].index_num] = { temp_index.h + 1,q.node[temp_index.u].next[j] };
temp_wavefront_set[hash_mismatch].index_num++;
}
}
}
temp_wavefront_set[hash_gap].score = score + penalty.gap;
temp_wavefront_set[hash_mismatch].score = score + penalty.mismatch;
}
int LINEAR_ALIGN(char* t, Graph q, penalty p, int sequence_size, int pattern_size)
{
/* Initialize Wavefront */
static const int Hash_size = prime(MAX(p.gap, p.mismatch));
static Wavefront_set* H = MALLOC(Hash_size, Wavefront_set);
for (int i = 0; i < Hash_size; i++)
{
H[i].index_num = 0;
H[i].score = -1;
}
int score = 0;
static bool D[Bool_size];
for (int i = 0; i < (pattern_size + 1) * (sequence_size + 1); i++)
D[i] = 0;
/* Add the first Wavefront Index*/
H[0].index[0] = { 0,0 };
H[0].index_num++;
H[0].score = 0;
while (true)
{
int s_position = score % Hash_size;
/* Extend */
H[s_position] = EXTEND(H[s_position], D, pattern_size, sequence_size, t, q);
/* Output */
for (int i = 0; i < H[s_position].index_num; i++)
printf("%d\t%d\t%d\n", H[s_position].score, H[s_position].index[i].h, H[s_position].index[i].u);
/* Termination */
if (D[(pattern_size + 1) * (sequence_size + 1) - 1]==1)
break;
/* Next */
LINEAR_NEXT(H, D, t, sequence_size, pattern_size, p, score, Hash_size, q);
/* Find the next score*/
H[s_position].index_num = 0;
H[s_position].score = -1;
int next_score = INT_MAX;
for (int i = 0; i < Hash_size; i++)
{
printf("%d\t%d\n", H[i].index_num, H[i].score);
if (H[i].index_num > 0)
next_score = MIN(next_score, H[i].score);
}
score = next_score;
}
return score;
}
};
GWF Wavefront;
char sequence[6] = "ACTGC";
char* t = sequence;
Graph q;
q.num = 5;
q.node[0].base = -1;
string s = "TACGA";
for (int i = 1; i <= 5; i++)
{
q.node[i].base = tran(s[i - 1]);
}
q.node[0].edgenum = 1;
q.node[0].next[0] = 1;
q.node[1].edgenum = 1;
q.node[1].next[0] = 2;
q.node[2].edgenum = 2;
q.node[2].next[0] = 3;
q.node[2].next[1] = 4;
q.node[3].edgenum = 2;
q.node[3].next[0] = 1;
q.node[3].next[1] = 4;
q.node[4].edgenum = 1;
q.node[4].next[0] = 5;
q.node[5].edgenum = 1;
q.node[5].next[0] = 1;
penalty p = { 4,3 };
int score=Wavefront.LINEAR_ALIGN(t, q, p, 5, 5);
//cout << score << endl;
return 0;
}
用到的两个头文件
#include <iostream>
#include <string.h>
#define DEFINE_AFFINE_PENALTY typedef struct penalty \
{ \
int mismatch; \
int gap_open; \
int gap_extention; \
}penalty;
#define DEFINE_LINEAR_PENALTY typedef struct penalty \
{ \
int mismatch; \
int gap; \
}penalty;
#define DEFINE_NODE(MAX_EDGE) typedef struct Node \
{ \
int base; \
int next[MAX_EDGE]; \
int edgenum; \
}Node;
#define DEFINE_GRAPH(pattern_size) typedef struct Graph \
{ \
int num; \
Node node[pattern_size + 1]; \
}Graph;
#define DEFINE_INDEX typedef struct Index \
{ \
int h; \
int u; \
};
#define DEFINE_WAVEFRONT_SET(MAX_INDEX_SET) typedef struct Wavefront_set \
{ \
int score; \
Index index[MAX_INDEX_SET]; \
int index_num; \
};
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<string.h>
#include<time.h>
#include<cmath>
using namespace std
/*
* Common numerical data processing/formating
*/
#define MIN(a,b) (((a)<=(b))?(a):(b))
#define MAX(a,b) (((a)>=(b))?(a):(b))
#define ABS(a) (((a)>=0)?(a):-(a))
/*
* Pseudo-Random number generator
*/
#define rand_init() srand(time(0))
#define rand_i(min,max) ( min + ( rand()%(max-min+1) ) )
#define rand_f(min,max) ( min + ((double)rand()/(double)(RAND_MAX+1)) * (max-min+1) )
/*
minimum prime number
*/
int prime(int infnum)
{
int i = infnum;
while (true)
{
bool factor = 0;
for (int k = 2; k <= sqrt(i); k++)
{
if (i % k == 0)
{
factor = 1;
break;
}
}
if (!factor)
break;
i++;
}
return i;
}
/* malloc */
#define MALLOC(n,type)\
((type*)malloc(n))
int tran(char a)
{
int c;
if (a == 'A') {
c = 0;
}
if (a == 'G') {
c = 1;
}
if (a == 'C') {
c = 2;
}
if (a == 'T')
{
c = 3;
}
return c;
}
写了如上的代码,在逐行运行到return 0时出现这样的情况
有时也会出现其他的内存情况(在没有修改代码的前提下),请问这是什么原因呀😭