C++ 史密斯·沃特曼(C+)+;(Visual Studio 14.0)

C++ 史密斯·沃特曼(C+)+;(Visual Studio 14.0),c++,alignment,C++,Alignment,我想一石二鸟,因为问题非常相似: 1: 我在GITHUB上遵循这个代码,在C++中创建了SmithWeWMADN。经过一些研究,我明白了这一点 double H[N_a+1][N_b+1]是不可能的。因此,为了创建常量变量,我将此行更改为: double **H = new double*[nReal + 1]; for (int i = 0; i < nReal + 1; i++) H[i] = new double[nSynth + 1]; 这里怎么了?已调试,并且程序在(i

我想一石二鸟,因为问题非常相似:

1:
我在GITHUB上遵循这个代码,在C++中创建了SmithWeWMADN。经过一些研究,我明白了这一点
double H[N_a+1][N_b+1]是不可能的。因此,为了创建常量变量,我将此行更改为:

double **H = new double*[nReal + 1];
for (int i = 0; i < nReal + 1; i++)
    H[i] = new double[nSynth + 1];
这里怎么了?已调试,并且程序在(int i=0;i
上方抛出异常

2:
此代码使用
std::strings
作为参数。是否也可以为
cv::Mat
创建史密斯-沃特曼算法

为了进一步澄清,我的完整代码如下所示:

#include "BinaryAlignment.h"
#include "WallMapping.h"

//using declarations
using namespace cv;
using namespace std;

//global variables
std::string bin;
cv::Mat temp;
std::stringstream sstrMat;

const int maxMismatch = 2;
const float mu = 0.33f;
const float delta = 1.33;
int ind;


BinaryAlignment::BinaryAlignment() { }
BinaryAlignment::~BinaryAlignment() { }

/**
*** Convert matrix to binary sequence
**/
std::string BinaryAlignment::matToBin(cv::Mat src, std::experimental::filesystem::path path) {

    cv::Mat linesMat = WallMapping::wallMapping(src, path);

    for (int i = 0; i < linesMat.size().height; i++) {
        for (int j = 0; j < linesMat.size().width; j++) {
            if (linesMat.at<Vec3b>(i, j)[0] == 0
                && linesMat.at<Vec3b>(i, j)[1] == 0
                && linesMat.at<Vec3b>(i, j)[2] == 255) {
                src.at<int>(i, j) = 1;
            }
            else {
                src.at<int>(i, j) = 0;
            }
            sstrMat << src.at<int>(i, j);
        }
    }
    bin = sstrMat.str();

    return bin;
}


double BinaryAlignment::similarityScore(char a, char b) {
    double result;

    if (a == b)
        result = 1;
    else
        result = -mu;

    return result;
}

double BinaryAlignment::findArrayMax(double array[], int length) {
    double max = array[0];
    ind = 0;

    for (int i = 1; i < length; i++) {
        if (array[i] > max) {
            max = array[i];
            ind = i;
        }
    }
    return max;
}


/**
*** Smith-Waterman alignment for given sequences
**/
int BinaryAlignment::watermanAlign(std::string seqSynth, std::string seqReal, bool viableAlignment) {   
    const int nSynth = seqSynth.length();                                               //length of sequences
    const int nReal = seqReal.length();

    //H[nSynth + 1][nReal + 1]
    double **H = new double*[nReal + 1];
    for (int i = 0; i < nReal + 1; i++)
        H[i] = new double[nSynth + 1];

    cout << "passt";

    for (int m = 0; m <= nSynth; m++)
        for (int n = 0; n <= nReal; n++)
            H[m][n] = 0;

    double temp[4];
    int **Ii = new int*[nReal + 1];
    for (int i = 0; i < nReal + 1; i++)
        Ii[i] = new int[nSynth + 1];

    int **Ij = new int*[nReal + 1];
    for (int i = 0; i < nReal + 1; i++)
        Ij[i] = new int[nSynth + 1];

    for (int i = 1; i <= nSynth; i++) {
        for (int j = 1; j <= nReal; j++) {
            temp[0] = H[i - 1][j - 1] + similarityScore(seqSynth[i - 1], seqReal[j - 1]);
            temp[1] = H[i - 1][j] - delta;
            temp[2] = H[i][j - 1] - delta;
            temp[3] = 0;
            H[i][j] = findArrayMax(temp, 4);

            switch (ind) {
            case 0:                                  // score in (i,j) stems from a match/mismatch
                Ii[i][j] = i - 1;
                Ij[i][j] = j - 1;
                break;
            case 1:                                  // score in (i,j) stems from a deletion in sequence A
                Ii[i][j] = i - 1;
                Ij[i][j] = j;
                break;
            case 2:                                  // score in (i,j) stems from a deletion in sequence B
                Ii[i][j] = i;
                Ij[i][j] = j - 1;
                break;
            case 3:                                  // (i,j) is the beginning of a subsequence
                Ii[i][j] = i;
                Ij[i][j] = j;
                break;
            }
        }
    }

    //Print matrix H to console 
    std::cout << "**********************************************" << std::endl;
    std::cout << "The scoring matrix is given by  " << std::endl << std::endl;
    for (int i = 1; i <= nSynth; i++) {
        for (int j = 1; j <= nReal; j++) {
            std::cout << H[i][j] << " ";
        }
        std::cout << std::endl;
    }

    //search H for the moaximal score
    double Hmax = 0;
    int imax = 0, jmax = 0;

    for (int i = 1; i <= nSynth; i++) {
        for (int j = 1; j <= nReal; j++) {
            if (H[i][j] > Hmax) {
                Hmax = H[i][j];
                imax = i;
                jmax = j;
            }
        }
    }

    std::cout << Hmax << endl;
    std::cout << nSynth << ", " << nReal << ", " << imax << ", " << jmax << std::endl;
    std::cout << "max score: " << Hmax << std::endl;
    std::cout << "alignment index: " << (imax - jmax) << std::endl;

    //Backtracing from Hmax
    int icurrent = imax, jcurrent = jmax;
    int inext = Ii[icurrent][jcurrent];
    int jnext = Ij[icurrent][jcurrent];
    int tick = 0;
    char *consensusSynth = new char[nSynth + nReal + 2];
    char *consensusReal = new char[nSynth + nReal + 2];

    while (((icurrent != inext) || (jcurrent != jnext)) && (jnext >= 0) && (inext >= 0)) {

        if (inext == icurrent)
            consensusSynth[tick] = '-';                         //deletion in A
        else
            consensusSynth[tick] = seqSynth[icurrent - 1];      //match / mismatch in A

        if (jnext == jcurrent)
            consensusReal[tick] = '-';                          //deletion in B
        else
            consensusReal[tick] = seqReal[jcurrent - 1];        //match/mismatch in B

        //fix for adding first character of the alignment.
        if (inext == 0)
            inext = -1;
        else if (jnext == 0)
            jnext = -1;
        else
            icurrent = inext;
        jcurrent = jnext;
        inext = Ii[icurrent][jcurrent];
        jnext = Ij[icurrent][jcurrent];

        tick++;
    }


    // Output of the consensus motif to the console
    std::cout << std::endl << "***********************************************" << std::endl;
    std::cout << "The alignment of the sequences" << std::endl << std::endl;
    for (int i = 0; i < nSynth; i++) {
        std::cout << seqSynth[i];
    };
    std::cout << "  and" << std::endl;
    for (int i = 0; i < nReal; i++) {
        std::cout << seqReal[i];
    };
    std::cout << std::endl << std::endl;
    std::cout << "is for the parameters  mu = " << mu << " and delta = " << delta << " given by" << std::endl << std::endl;
    for (int i = tick - 1; i >= 0; i--)
        std::cout << consensusSynth[i];
    std::cout << std::endl;
    for (int j = tick - 1; j >= 0; j--)
        std::cout << consensusReal[j];
    std::cout << std::endl;


    int numMismatches = 0;
    for (int i = tick - 1; i >= 0; i--) {
        if (consensusSynth[i] != consensusReal[i]) {
            numMismatches++;
        }
    }

    viableAlignment = numMismatches <= maxMismatch;
    return imax - jmax;
}
#包括“BinaryAlignment.h”
#包括“WallMapping.h”
//使用声明
使用名称空间cv;
使用名称空间std;
//全局变量
std::字符串库;
cv::垫温;
标准:stringstream sstrMat;
常量int MAXMASCHMATCH=2;
常数浮点μ=0.33f;
常数浮动增量=1.33;
int ind;
BinaryAlignment::BinaryAlignment(){}
BinaryAlignment::~BinaryAlignment(){}
/**
***将矩阵转换为二进制序列
**/
std::string二进制对齐::matToBin(cv::Mat src,std::experimental::filesystem::path){
cv::Mat linesMat=WallMapping::WallMapping(src,路径);
对于(int i=0;i更精确地说,可变长度数组不是标准的C++(虽然有些编译器支持它们作为扩展)。至于坏的代码,当你分配时,<代码> nReals>代码>和<代码> nSyth< <代码>的值是什么?我的意思是VLAs在C++中从来没有标准化,它不是关于“从未版本”的。而是编译器扩展。这也没有告诉我您试图分配多少内存。2MB?2GB?错误的alloc意味着它无法分配内存,这意味着您试图分配的内存量可能是相关的。请注意,您可以使用
std::vector name(行,std::vector)替换所有新的、删除的和指针(科尔斯);
如果您的机器有8GB,而代码需要7GB,那么您可能没有足够的RAM将所有内容加载到内存中。您需要足够大的存储桶,操作系统和其他程序也将消耗RAM。@Viktoria您仍然会出错,因为您的机器无法运行代码。这需要大量RAM。Mo再精确地说,可变长度数组不是标准C++(虽然有些编译器支持它们作为扩展)。至于坏的代码,当你分配时,<代码> nReals>代码>和<代码> nSyth< <代码>的值是什么?我的意思是VLAs在C++中从来没有标准化,它不是关于“从未版本”的。而是编译器扩展。这也没有告诉我您试图分配多少内存。2MB?2GB?错误的alloc意味着它无法分配内存,这意味着您试图分配的内存量可能是相关的。请注意,您可以使用
std::vector name(行,std::vector)替换所有新的、删除的和指针(科尔斯);
如果您的机器有8GB,而代码需要7GB,那么您可能没有足够的RAM将所有内容加载到内存中。您需要足够大的存储桶,操作系统和其他程序也将消耗RAM。@Viktoria您仍然会出错,因为您的机器无法运行代码。它需要大量的RAM。
#include "BinaryAlignment.h"
#include "WallMapping.h"

//using declarations
using namespace cv;
using namespace std;

//global variables
std::string bin;
cv::Mat temp;
std::stringstream sstrMat;

const int maxMismatch = 2;
const float mu = 0.33f;
const float delta = 1.33;
int ind;


BinaryAlignment::BinaryAlignment() { }
BinaryAlignment::~BinaryAlignment() { }

/**
*** Convert matrix to binary sequence
**/
std::string BinaryAlignment::matToBin(cv::Mat src, std::experimental::filesystem::path path) {

    cv::Mat linesMat = WallMapping::wallMapping(src, path);

    for (int i = 0; i < linesMat.size().height; i++) {
        for (int j = 0; j < linesMat.size().width; j++) {
            if (linesMat.at<Vec3b>(i, j)[0] == 0
                && linesMat.at<Vec3b>(i, j)[1] == 0
                && linesMat.at<Vec3b>(i, j)[2] == 255) {
                src.at<int>(i, j) = 1;
            }
            else {
                src.at<int>(i, j) = 0;
            }
            sstrMat << src.at<int>(i, j);
        }
    }
    bin = sstrMat.str();

    return bin;
}


double BinaryAlignment::similarityScore(char a, char b) {
    double result;

    if (a == b)
        result = 1;
    else
        result = -mu;

    return result;
}

double BinaryAlignment::findArrayMax(double array[], int length) {
    double max = array[0];
    ind = 0;

    for (int i = 1; i < length; i++) {
        if (array[i] > max) {
            max = array[i];
            ind = i;
        }
    }
    return max;
}


/**
*** Smith-Waterman alignment for given sequences
**/
int BinaryAlignment::watermanAlign(std::string seqSynth, std::string seqReal, bool viableAlignment) {   
    const int nSynth = seqSynth.length();                                               //length of sequences
    const int nReal = seqReal.length();

    //H[nSynth + 1][nReal + 1]
    double **H = new double*[nReal + 1];
    for (int i = 0; i < nReal + 1; i++)
        H[i] = new double[nSynth + 1];

    cout << "passt";

    for (int m = 0; m <= nSynth; m++)
        for (int n = 0; n <= nReal; n++)
            H[m][n] = 0;

    double temp[4];
    int **Ii = new int*[nReal + 1];
    for (int i = 0; i < nReal + 1; i++)
        Ii[i] = new int[nSynth + 1];

    int **Ij = new int*[nReal + 1];
    for (int i = 0; i < nReal + 1; i++)
        Ij[i] = new int[nSynth + 1];

    for (int i = 1; i <= nSynth; i++) {
        for (int j = 1; j <= nReal; j++) {
            temp[0] = H[i - 1][j - 1] + similarityScore(seqSynth[i - 1], seqReal[j - 1]);
            temp[1] = H[i - 1][j] - delta;
            temp[2] = H[i][j - 1] - delta;
            temp[3] = 0;
            H[i][j] = findArrayMax(temp, 4);

            switch (ind) {
            case 0:                                  // score in (i,j) stems from a match/mismatch
                Ii[i][j] = i - 1;
                Ij[i][j] = j - 1;
                break;
            case 1:                                  // score in (i,j) stems from a deletion in sequence A
                Ii[i][j] = i - 1;
                Ij[i][j] = j;
                break;
            case 2:                                  // score in (i,j) stems from a deletion in sequence B
                Ii[i][j] = i;
                Ij[i][j] = j - 1;
                break;
            case 3:                                  // (i,j) is the beginning of a subsequence
                Ii[i][j] = i;
                Ij[i][j] = j;
                break;
            }
        }
    }

    //Print matrix H to console 
    std::cout << "**********************************************" << std::endl;
    std::cout << "The scoring matrix is given by  " << std::endl << std::endl;
    for (int i = 1; i <= nSynth; i++) {
        for (int j = 1; j <= nReal; j++) {
            std::cout << H[i][j] << " ";
        }
        std::cout << std::endl;
    }

    //search H for the moaximal score
    double Hmax = 0;
    int imax = 0, jmax = 0;

    for (int i = 1; i <= nSynth; i++) {
        for (int j = 1; j <= nReal; j++) {
            if (H[i][j] > Hmax) {
                Hmax = H[i][j];
                imax = i;
                jmax = j;
            }
        }
    }

    std::cout << Hmax << endl;
    std::cout << nSynth << ", " << nReal << ", " << imax << ", " << jmax << std::endl;
    std::cout << "max score: " << Hmax << std::endl;
    std::cout << "alignment index: " << (imax - jmax) << std::endl;

    //Backtracing from Hmax
    int icurrent = imax, jcurrent = jmax;
    int inext = Ii[icurrent][jcurrent];
    int jnext = Ij[icurrent][jcurrent];
    int tick = 0;
    char *consensusSynth = new char[nSynth + nReal + 2];
    char *consensusReal = new char[nSynth + nReal + 2];

    while (((icurrent != inext) || (jcurrent != jnext)) && (jnext >= 0) && (inext >= 0)) {

        if (inext == icurrent)
            consensusSynth[tick] = '-';                         //deletion in A
        else
            consensusSynth[tick] = seqSynth[icurrent - 1];      //match / mismatch in A

        if (jnext == jcurrent)
            consensusReal[tick] = '-';                          //deletion in B
        else
            consensusReal[tick] = seqReal[jcurrent - 1];        //match/mismatch in B

        //fix for adding first character of the alignment.
        if (inext == 0)
            inext = -1;
        else if (jnext == 0)
            jnext = -1;
        else
            icurrent = inext;
        jcurrent = jnext;
        inext = Ii[icurrent][jcurrent];
        jnext = Ij[icurrent][jcurrent];

        tick++;
    }


    // Output of the consensus motif to the console
    std::cout << std::endl << "***********************************************" << std::endl;
    std::cout << "The alignment of the sequences" << std::endl << std::endl;
    for (int i = 0; i < nSynth; i++) {
        std::cout << seqSynth[i];
    };
    std::cout << "  and" << std::endl;
    for (int i = 0; i < nReal; i++) {
        std::cout << seqReal[i];
    };
    std::cout << std::endl << std::endl;
    std::cout << "is for the parameters  mu = " << mu << " and delta = " << delta << " given by" << std::endl << std::endl;
    for (int i = tick - 1; i >= 0; i--)
        std::cout << consensusSynth[i];
    std::cout << std::endl;
    for (int j = tick - 1; j >= 0; j--)
        std::cout << consensusReal[j];
    std::cout << std::endl;


    int numMismatches = 0;
    for (int i = tick - 1; i >= 0; i--) {
        if (consensusSynth[i] != consensusReal[i]) {
            numMismatches++;
        }
    }

    viableAlignment = numMismatches <= maxMismatch;
    return imax - jmax;
}