Advertisement
dark-Matter

Sequence-matching-2

Jan 7th, 2021
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.57 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. void Alignment (char *, char *);
  6. int Alignment_Q1 (char *A, char *B);
  7. void Backward_Alignment (char *, char *);
  8. void Forward_Alignment (char *, char *);
  9. void Print(vector<char>);
  10. int min(int ,int, int);
  11.  
  12. // Global Variables are CAPITALISED by convention
  13. const int SIZE = 1000;
  14. int F[SIZE], G[SIZE];
  15. int GAP_PENALTY=2, MISMATCH_PENALTY=3;
  16. int MATCH_PENALTY = 0;
  17. int SCORE = 0;
  18. vector < char > A_FINAL, B_FINAL;
  19.  
  20. int main(int argc, char **argv) {
  21.    char a[SIZE], b[SIZE];  
  22.  
  23.    cout << "s1: ";
  24.    cin >> a;
  25.    cout << "s2: ";
  26.    cin >> b;
  27.  
  28.  
  29.    Alignment(a,b);
  30.    cout << "Gap Score: " << SCORE << "\n";
  31.    Print(A_FINAL);
  32.    Print(B_FINAL);
  33.  
  34.    return 0;
  35. }
  36.  
  37. int Alignment_Q1 (char *A, char *B) {
  38.    // getting length of the strings
  39.    int m;
  40.    for (m = 0; A[m] != '\0'; m++);
  41.  
  42.    int n;
  43.    for (n = 0; B[n] != '\0'; n++);
  44.  
  45.  
  46.    int arr[m+1][n+1];
  47.  
  48.    // Initializing Alignment Matrix
  49.    for (int i = 0; i <= m; i++)
  50.        arr[i][0] = i * GAP_PENALTY;
  51.    for (int i = 0; i <= n; i++)
  52.        arr[0][i] = i * GAP_PENALTY;
  53.  
  54.    // Store 0, mismatch_penalty depending on A[i] == B[j]
  55.    int temp;
  56.  
  57.  
  58.    for (int i = 1; i <= m; i++) {
  59.        for (int j = 1; j <= n; j++) {
  60.            // temp = 0 if Match-Found
  61.            if (A[i-1] == B[j-1])
  62.                temp = MATCH_PENALTY;
  63.          
  64.            // temp = PENALTY if Match Not Found
  65.            else
  66.                temp = MISMATCH_PENALTY;
  67.          
  68.            // Recurrence Relation
  69.            arr[i][j] = min(
  70.                temp + arr[i-1][j-1],
  71.                GAP_PENALTY + arr[i][j-1],
  72.                GAP_PENALTY + arr[i-1][j] );
  73.        }
  74.    }
  75.  
  76.    // Obtaining the Matched Sequences With Gaps
  77.    char A_new[2*m + 1], B_new[2*n + 1];
  78.    int i = m, j = n, a = 0, b = 0;
  79.    while (i > 0 && j > 0)
  80.    {
  81.        // temp = 0 if Match-Found
  82.        if (A[i-1] == B[j-1])
  83.            temp = MATCH_PENALTY;
  84.      
  85.        // temp = PENALTY if Match Not Found
  86.        else
  87.            temp = MISMATCH_PENALTY;
  88.      
  89.        // Go diagonal => Match
  90.        if (arr[i-1][j-1] + temp <= arr[i-1][j] + GAP_PENALTY && arr[i-1][j-1] + temp <= arr[i][j-1] + GAP_PENALTY)
  91.        {
  92.            A_new[a++] = A[--i];
  93.            B_new[b++] = B[--j];
  94.        }
  95.  
  96.        // Go Right => Anew.append(-), Bnew.append(B[j])
  97.        else if (arr[i][j-1] + GAP_PENALTY <= arr[i-1][j-1] + temp && arr[i][j-1] +GAP_PENALTY <= arr[i-1][j] + GAP_PENALTY)
  98.        {
  99.            A_new[a++] = '-';
  100.            B_new[b++] = B[--j];
  101.        }
  102.  
  103.        // Go Down => Anew.append(A[i]), Bnew.append(-)
  104.        else if (arr[i-1][j] + GAP_PENALTY <= arr[i-1][j-1] + temp && arr[i-1][j] + GAP_PENALTY <= arr[i][j-1] + GAP_PENALTY)
  105.        {
  106.            A_new[a++] = A[--i];
  107.            B_new[b++] = '-';
  108.        }
  109.    }
  110.    // Finishing Sequences: Edge Case
  111.    while (i > 0) {
  112.        A_new[a++] = A[--i];
  113.        B_new[b++] = '-';
  114.    }
  115.    // Finishing Sequences: Edge Case
  116.    while (j > 0) {
  117.        A_new[a++] = '-';
  118.        B_new[b++] = B[--j];
  119.    }
  120.  
  121.    A_new[a++] = '\0';
  122.    B_new[b++] = '\0';
  123.  
  124.    // Appending A i reverse order
  125.    for (i = a - 2; i >= 0; i--)
  126.        A_FINAL.push_back(A_new[i]);
  127.  
  128.    // Appending B in reverse order
  129.    for (i = b - 2; i >= 0; i--)
  130.        B_FINAL.push_back(B_new[i]);
  131.  
  132.    return arr[m][n];
  133. }
  134.  
  135. void Forward_Alignment (char *A, char *B) {
  136.    // Length of Sequence A
  137.    int m;
  138.    for (m = 0; A[m] != '\0'; m++);
  139.  
  140.    // Length of Sequnce B
  141.    int n;
  142.    for (n = 0; B[n] != '\0'; n++);
  143.  
  144.    // Alignment Matrix
  145.    int arr[m+1][2];
  146.  
  147.    // Initializing Alignment Matrix
  148.    for (int i = 0; i <= m; i++)
  149.        arr[i][0] = i * GAP_PENALTY;
  150.  
  151.    // Store 0, mismatch_penalty depending on A[i] == B[j]
  152.    int temp;
  153.  
  154.    // Evaluating Alignment Matrix Using Recurrence Relation
  155.    // As per Keinberg and Tardos relation 6.16
  156.    for (int j = 1; j <= n; j++) {
  157.        arr[0][1] = j * GAP_PENALTY;
  158.        for (int i = 1; i <= m; i++) {
  159.            // temp = 0 if Match-Found
  160.            if (A[i-1] == B[j-1])
  161.                temp = 0;
  162.          
  163.            // temp = PENALTY if Match Not Found
  164.            else
  165.                temp = MISMATCH_PENALTY;
  166.          
  167.            // Recurrence Relation
  168.            arr[i][1] = min(
  169.                temp + arr[i-1][0],
  170.                GAP_PENALTY + arr[i-1][1],
  171.                GAP_PENALTY + arr[i][0] );
  172.        }
  173.        for (int i = 0; i <= m; i++)
  174.            arr[i][0] = arr[i][1];
  175.    }
  176.  
  177.    // returning f(:len_B)
  178.    for ( int i = 0; i <= m; i++)
  179.        F[i] = arr[i][1];
  180. }
  181.  
  182. void Backward_Alignment (char *A, char *B) {
  183.    // Length of Sequence A
  184.    int m;
  185.    for (m = 0; A[m] != '\0'; m++);
  186.  
  187.    // Length of Sequnce B
  188.    int n;
  189.    for (n = 0; B[n] != '\0'; n++);
  190.  
  191.    // Alignment Matrix
  192.    int arr[m+1][2];
  193.  
  194.    // Initializing Alignment Matrix
  195.    for (int i = 0; i <= m; i++)
  196.        arr[i][1] = (m-i) * GAP_PENALTY;
  197.  
  198.    // Store 0, mismatch_penalty depending on A[i] == B[j]
  199.    int temp;
  200.  
  201.    // Evaluating Alignment Matrix Using Recurrence Relation
  202.    // As per Keinberg and Tardos relation 6.16
  203.    for (int j = n-1; j >= 0; j--) {
  204.        arr[m][0] = (n-j) * GAP_PENALTY;
  205.        for (int i = m-1; i >= 0; i--) {
  206.            // temp = 0 if Match-Found
  207.            if (A[i] == B[j])
  208.                temp = 0;
  209.          
  210.            // temp = PENALTY if Match Not Found
  211.            else
  212.                temp = MISMATCH_PENALTY;
  213.          
  214.            // Recurrence Relation
  215.            arr[i][0] = min(
  216.                temp + arr[i+1][1],
  217.                GAP_PENALTY + arr[i+1][0],
  218.                GAP_PENALTY + arr[i][1] );
  219.        }
  220.        for (int i = 0; i <= m; i++)
  221.            arr[i][1] = arr[i][0];
  222.    }
  223.  
  224.    // returning g(:len_B)
  225.    for ( int i = 0; i <= m; i++)
  226.        G[i] = arr[i][0];
  227. }
  228.  
  229. // Sequencing in Linear Space-time
  230. void Alignment(char *A, char *B) {
  231.    // Length of Sequence A
  232.    int m;
  233.    for (m = 0; A[m] != '\0'; m++);
  234.  
  235.    // Length of Sequnce B
  236.    int n;
  237.    for (n = 0; B[n] != '\0'; n++);
  238.  
  239.    // Base Case
  240.    if (m <= 2 || n <= 2) {
  241.        SCORE  += Alignment_Q1(A, B);
  242.        return ;
  243.    }
  244.  
  245.    // Splitting B
  246.    int i = 0;
  247.    char B_first[n/2 + 1], B_last[n - n/2];
  248.    for (i = 0; i <= n/2; i++)
  249.        B_first[i] = B[i];
  250.    B_first[i] = '\0';
  251.    for (i = n/2 ; i < n; i++)
  252.        B_last[i - n/2] = B[i];
  253.    B_last[i - n/2] = '\0';
  254.  
  255.    // Dividing the problem
  256.    Forward_Alignment(A, B_first);
  257.    Backward_Alignment(A, B_last);
  258.  
  259.    // Conquering the problem
  260.    // Minimising f(q, n/2) + g(q, n/2)
  261.    int temp, index = 0, Min = F[0] + G[0];
  262.    for (i = 1; i <= m; i++) {
  263.        temp = F[i] + G[i];
  264.        if (temp < Min) {
  265.            Min = temp;
  266.            index = i;
  267.        }
  268.    }
  269.  
  270.    // Splitting A
  271.    char A_first[index + 1], A_last[m - index + 1];
  272.    for (i = 0; i < index; i++)
  273.        A_first[i] = A[i];
  274.    A_first[i] = '\0';
  275.  
  276.    for (i = index; i < m; i++)
  277.        A_last[i - index] = A[i];
  278.    A_last[i - index] = '\0';
  279.  
  280.    // Splitting B
  281.    for (i = 0; i < n/2; i++)
  282.        B_first[i] = B[i];
  283.    B_first[i] = '\0';
  284.  
  285.    for (i = n/2 ; i < n; i++)
  286.        B_last[i - n/2] = B[i];
  287.    B_last[i - n/2] = '\0';
  288.  
  289.    // Dividing the Problem Recursively
  290.    Alignment(A_first, B_first);
  291.    Alignment(A_last, B_last);
  292. }
  293.  
  294.  
  295. int min(int a, int b, int c) {
  296.    if (a <= b && a <= c)
  297.        return a;
  298.    else if (b <= a && b <= c)
  299.        return b;
  300.    else
  301.        return c;
  302. }
  303.  
  304. void Print(vector<char> v) {
  305.    for (auto it = v.begin(); it < v.end(); it++)
  306.        cout << (*it) << "";
  307.    cout << "\n";
  308. }
  309.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement