Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,420 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,982 people online right now. Registration is fast and FREE... Join Now!




sequence alignment (string)

 
Reply to this topicStart new topic

sequence alignment (string), the code to input two sequence (string) and compare similarity between

hanturaya
26 Jan, 2008 - 11:43 AM
Post #1

New D.I.C Head
*

Joined: 26 Jan, 2008
Posts: 4

Hi to all,

i'm newbie in here n nice to meet u all.. biggrin.gif

i have problem that code below has errors... blink.gif

it is to input two sequences of string from user, then the code will compare similarity between the two sequences by developing a matrix score. The problems maybe connected with the structure and pointer, because the process of scoring matrix cannot read the two sequences.

i hope anyone can help me to repair this code...
thank you so much.. smile.gif

==========================================================================
CODE

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<assert.h>
#include<malloc.h>
#include<math.h>
#include<time.h>


struct TwoSequence
{
    char *sequence1[200];
    char *sequence2[200];
};


typedef struct
{
     int match;
     int mismatch;
     int open;
     int extend;
} Payoff;


Payoff gPayoff = { 1, -1, -1, -1 };


typedef struct Cell
{
     int          row;
     int          col;
     int          score;
     struct Cell *prev;
} Cell;


typedef struct
{
     Payoff   payoff;
     char    *sequence1;
     char    *sequence2;
     int      nRows;
     int      nCols;
     Cell   **ppCell;
} SW;


extern SW   *New      (char *sequence1, char *sequence2, Payoff payoff);
extern void  Delete   (SW *psw);
extern void  Score    (SW *psw);
extern void  DumpScore(SW *psw);

static char *Unshift(char *pszSource, int c);

static void  AlignString(char *sequence1, char *sequence2, Payoff payoff);
static void  TimeTest();
static char *RandString (int n);


main()
{
        struct TwoSequence twosequence;

        int i;

            printf("\nEnter sequence 1 : ");
            gets(twosequence.sequence1);

            printf("\nEnter sequence 2 : ");
            gets(twosequence.sequence2);

            for (i=0; i<2; i++)
                AlignString(twosequence[i].sequence1, twosequence[i].sequence2, gPayoff);

            TimeTest();

            printf("\nThank You");

        return 0;
}


void AlignString(char *sequence1, char *sequence2, Payoff payoff)
{
     SW *psw = New(sequence1, sequence2, payoff);
     Score(psw);
     DumpScore(psw);
     Delete(psw);
}

*SW *New(char *sequence1, char *sequence2, Payoff payoff)
{
     int row, col;

     SW *psw = (SW *) malloc(sizeof (SW));

     fprintf(stderr, "New");
     psw->payoff = payoff;
     psw->sequence1   = Unshift(sequence1, ' ');
     psw->sequence2   = Unshift(sequence2, ' ');

     psw->nRows  = strlen(psw->*sequence1);
     psw->nCols  = strlen(psw->*sequence2);

     psw->ppCell = (Cell**) malloc(psw->nRows * sizeof (Cell *));

     for (row=0; row<psw->nRows; row++)
     {
    fprintf(stderr, ".");
    psw->ppCell[row] = (Cell *) malloc(psw->nCols * sizeof(Cell));

    for (col=0; col<psw->nCols; col++)
    {
         Cell *pCell = &psw->ppCell[row][col];
         pCell->row   = row;
         pCell->col   = col;
         pCell->score = 0;
         pCell->prev  = 0;
    }
     }

     fprintf(stderr, "\n");
     return psw;
}

void Delete(SW *psw)
{
     int row;

     free(psw->sequence1);
     free(psw->sequence2);

     for (row=0; row<psw->nRows; row++)
    free(psw->ppCell[row]);

     free(psw);
}


void Score(SW *psw)
{
     int row, col;

     Payoff payoff = psw->payoff;
     fprintf(stderr, "Score");

     for (row=1; row<psw->nRows; row++)
     {
    int cA = psw->sequence1[row];
    fprintf(stderr, ".");

    for (col=1; col<psw->nCols; col++)
    {
         int   cB       = psw->sequence2[col];
         int   pay      = cA==cB ? payoff.match : payoff. mismatch;
         int   ulScore  = psw->ppCell[row-1][col-1].score;

         int   maxScore = ulScore + pay;
         Cell* pCell    = &psw->ppCell[row-1][col-1];

         int r, c;
         for (r=0; r<row; r++)
         {
        int uScore   = psw->ppCell[r][col].score;
        int gapScore = uScore + payoff.open + payoff.extend * (row-r);
        if (gapScore < maxScore) continue;

        maxScore = gapScore;
        pCell    = &psw->ppCell[r][col];
         }

         for (c=0; c<col; c++)
         {
        int lScore   = psw->ppCell[row][c].score;
        int gapScore = lScore + payoff.open + payoff.extend * (col-c);
        if (gapScore < maxScore) continue;

        maxScore = gapScore;
        pCell    = &psw->ppCell[row][c];
         }

         psw->ppCell[row][col].score = maxScore;
         psw->ppCell[row][col].prev  = pCell;
    }
     }
     fprintf(stderr, "\n");
}


void DumpScore(SW *psw)
{
     int row, col;

     printf("   ");
     for (col=1; col<psw->nCols; col++)
    printf("%3c", psw->sequence2[col]);
     printf("\n");

     for (row=1; row<psw->nRows; row++)
     {
    printf("%3c", psw->sequence1[row]);
    for (col=1; col<psw->nCols; col++)
         printf("%3d", psw->ppCell[row][col].score);
    printf("\n");
     }
}


char *Unshift(char *pszSource, int c)
{
     int n = strlen(pszSource);
     char *pszDest = (char *) malloc(n+2);
     assert(pszDest);
     pszDest[0] = c;
     strcpy(pszDest+1, pszSource);
     return pszDest;
}

void TimeTest()
{
     char *sequence1 = RandString(200);
     char *sequence2 = RandString(200);

     int start, end;
     SW *psw;

     start = time(0);

     psw = New(sequence1, sequence2, gPayoff);
     Score(psw);
     Delete(psw);

     end = time(0);
     printf("%d seconds\n", end-start);
}

char *RandString(int n)
{
     char *psz = (char *) malloc(n+1);
     int i;

     for (i=0; i<n; i++)
    psz[i] = ' ' + (rand() & 0x3f);

     psz[n] = 0;

     return psz;
}


User is offlineProfile CardPM
+Quote Post

#include<wmx010>
RE: Sequence Alignment (string)
26 Jan, 2008 - 01:14 PM
Post #2

D.I.C Head
**

Joined: 19 Jan, 2008
Posts: 75



Thanked: 1 times
My Contributions
One of the problems is in your main() program, you are returning 0 to a void main ()

CODE

main()
{
        //................

        return 0;
}


it should be
CODE

int main()
{
        //................

        return 0;
}


This post has been edited by #include<wmx010>: 26 Jan, 2008 - 01:26 PM
User is offlineProfile CardPM
+Quote Post

hanturaya
RE: Sequence Alignment (string)
26 Jan, 2008 - 09:33 PM
Post #3

New D.I.C Head
*

Joined: 26 Jan, 2008
Posts: 4

thanks,...but its not effect at all.... smile.gif
this code's problem is with the structure and pointers.

CODE

main()
{
        //................

        return 0;
}


it should be
CODE

int main()
{
        //................

        return 0;
}

[/quote]

User is offlineProfile CardPM
+Quote Post

#include<wmx010>
RE: Sequence Alignment (string)
26 Jan, 2008 - 11:01 PM
Post #4

D.I.C Head
**

Joined: 19 Jan, 2008
Posts: 75



Thanked: 1 times
My Contributions
If you compile this program without any errors I will give you $100.
CODE

#include <iostream>

main ()
{
    std::cout << "It doesn't compile\n";

    return 0;
}


Anyway this seems like a C code, may be someone else can help you out I don't know anything about C. yet ^^
User is offlineProfile CardPM
+Quote Post

hanturaya
RE: Sequence Alignment (string)
27 Jan, 2008 - 06:49 AM
Post #5

New D.I.C Head
*

Joined: 26 Jan, 2008
Posts: 4

hihihi... biggrin.gif

sorry i;m not familiar with std::cout<<.....because the type qualifier "std" must be a struct or class name in function main.

but if u do it like this

CODE


#include <iostream.h>

main ()
{
    cout << "It doesn't compile\n";

    return 0;
}



the code can be compiled and get the output.... smile.gif
so how much u want to give me...coz its not use 'std', u just give me $50 ... biggrin.gif
sorry...i'm just kidding...

Yes, my code is a C code, and hope so that someone will help me...smile.gif


User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Sequence Alignment (string)
27 Jan, 2008 - 01:12 PM
Post #6

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,859



Thanked: 51 times
Dream Kudos: 550
My Contributions
so what do you expect when you write:

char *sequence1[200];

if you were expecting to get a char array with 200 elements you have a problem, since this will create an array of 200 pointers... So sequence1 is of type 'char **' and sequence1[0] is of type 'char *'

I think what you actually want is:

char sequence1[200];

Once you do this, then the variable 'sequence1' points to an array of 200 chars of which sequence1[0] is the first element.... that is to say that
sequence1 == &sequence1[0].


User is offlineProfile CardPM
+Quote Post

hanturaya
RE: Sequence Alignment (string)
27 Jan, 2008 - 06:11 PM
Post #7

New D.I.C Head
*

Joined: 26 Jan, 2008
Posts: 4

thanks NickDMax..
when i use

CODE

struct TwoSequence
{
     char sequence1[200];
     char sequence2[200];
};


its reducing the errors from 11 errors to 5 errors.
its still error in statement :

CODE
                
AlignString(twosequence.sequence1, twosequence.sequence2, gPayoff);


Error SCORINGN.CPP 74: Undefined symbol 'sequence1' in function main()
Error SCORINGN.CPP 74: Undefined symbol 'sequence2' in function main()


CODE

*SW *New(char *sequence1, char *sequence2, Payoff payoff)


Error SCORINGN.CPP 92: Multiple declaration for 'SW'
Error SCORINGN.CPP 92: Type mismatch in redeclaration of 'SW'
Error SCORINGN.CPP 92: Declaration syntax error

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/5/08 04:17AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month