BUS error when reading input from file
hello to all, (*ignore title part about input file)
I am not sure where the problem is in my code. Everything is compiling correctly.
here is the code:
Code:
else if ((strcmp (command, "align")) == 0 )
{
printf("Align sequences ( not yet implemented)\n");
int firstSeq = atoi(strtok(NULL, " \n\t")) ;
int secondSeq = atoi(strtok(NULL, " \n\t")) ;
#define GAPSCORE -1
// m = top sequence
// n = bottom sequence
// Change bestScore setup to capture best score
// in outer column and row
// Note: this may not be the global bestScore
typedef struct DPCell { char rowChar ; char colChar ; int Score ; struct DPCell *traceBack ; int traceBackType; } DPCell ;
// Traceback type = 0 for vertical, 1 for horizontal, 2 for
// diagonal, -1 undefined
DPCell **DPArray ; // 2-D array
// collect sequences from seqTool (*Modify)
char *rowSeq = loadedSequences[firstSeq-1].data ; // top sequence (m) "left edge of matrix"
char *colSeq = loadedSequences[secondSeq-1].data ; // bottom sequence (n) "top edge of matrix"
// Add 1 for the first column that contains gap penalty ( = -1 )
int rowLength = strlen( rowSeq ) + 1 ;
int colLength = strlen( colSeq ) + 1 ;
// Arrays to collect final alignment (Must be reversed!!)
char *rowAlign = (char *) malloc( (strlen(rowSeq) + strlen(colSeq) + 1) * sizeof( char ) ) ;
char *colAlign = (char *) malloc( (strlen(rowSeq) + strlen(colSeq) + 1) * sizeof( char ) ) ;
// Allocate DP matrix
DPArray = (DPCell **) malloc( rowLength * sizeof( DPCell * ) ) ;
int i, j ;
for( i = 0 ; i < rowLength ; ++i )
{
DPArray[i] = (DPCell *) malloc( colLength * sizeof( DPCell ) ) ;
}
// Initialize Upper left corner
DPArray[0][0].rowChar = '\0' ;
DPArray[0][0].colChar = '\0' ;
DPArray[0][0].Score = 0 ;
DPArray[0][0].traceBack = NULL ;
DPArray[0][0].traceBackType = -1 ;
// Left edge
for( i = 1 ; i < rowLength ; ++i )
{
DPArray[i][0].rowChar = toupper(rowSeq[i-1]) ;
DPArray[i][0].colChar = '\0' ;
DPArray[i][0].Score = i * GAPSCORE ;
DPArray[i][0].traceBack = & DPArray[i-1][0] ;
DPArray[i][0].traceBackType = 0 ;
}
// Top edge
for( j = 1 ; j < colLength ; ++j )
{
DPArray[0][j].rowChar = '\0' ;
DPArray[0][j].colChar = toupper(colSeq[j-1]) ;
DPArray[0][j].Score = i * GAPSCORE ;
DPArray[0][j].traceBack = & DPArray[j-1][0] ;
DPArray[0][j].traceBackType = 1 ;
}
// Process the array interior
for( i = 1 ; i < rowLength ; ++i )
{
for( j = 1 ; j < colLength ; ++j )
{
// Simple match score ( +1 for match, 0 for mismatch)
if ( DPArray[i][j].rowChar == DPArray[i][j].colChar )
{
DPArray[i][j].Score = (DPArray[i-1][j-1].Score + 1) ;
}
else
{
DPArray[i][j].Score = DPArray[i-1][j-1].Score ;
}
{
// Horizontal
if( DPArray[i][j-1].Score + GAPSCORE > DPArray[i][j].Score )
{
DPArray[i][j].Score = DPArray[i][j-1].Score + GAPSCORE ;
DPArray[i][j].traceBack = & DPArray[i][j-1] ;
DPArray[i][j].traceBackType = 0 ;
}
// Vertical
else if( DPArray[i-1][j].Score + GAPSCORE > DPArray[i][j].Score )
{
DPArray[i][j].Score = DPArray[i-1][j].Score + GAPSCORE ;
DPArray[i][j].traceBack = & DPArray[i-1][j] ;
DPArray[i][j].traceBackType = 1 ;
}
// Diagonal
else
{
DPArray[i][j].traceBack = & DPArray[i-1][j-1] ;
DPArray[i][j].traceBackType = 2 ;
}
}
}
}
struct { int score ; struct DPCell *address ;} bestRowCell ;
struct { int score ; struct DPCell *address ;} bestColumnCell ;
struct { int score ; struct DPCell *address ;} bestCell ;
// Find the address of the highest value in the bottom row
bestRowCell.score = 0 ;
for(i = rowLength, j=1 ; j < (colLength-1) ; ++j )
{
if( DPArray[i][j].Score > DPArray[i][j+1].Score )
{
bestRowCell.score = DPArray[i][j].Score ;
bestRowCell.address = & DPArray[i][j] ;
}
else
{
bestRowCell.score = DPArray[i][j+1].Score ;
bestRowCell.address = & DPArray[i][j+1] ;
}
}
// Find the address of the highest value in the right-most column
bestColumnCell.score = 0 ;
for(i = 1, j= colLength ; i < (rowLength-1) ; ++i )
{
if( DPArray[i][j].Score > DPArray[i][j+1].Score )
{
bestColumnCell.score = DPArray[i][j].Score ;
bestColumnCell.address = & DPArray[i][j] ;
}
else
{
bestColumnCell.score = DPArray[i][j+1].Score ;
bestColumnCell.address = & DPArray[i][j+1] ;
}
}
// Take values for bestCell
bestCell.score = 0 ;
if(bestColumnCell.score > bestRowCell.score)
{
bestCell.score = bestColumnCell.score ;
bestCell.address = bestColumnCell.address ;
}
else
{
bestCell.score = bestRowCell.score ;
bestCell.address = bestRowCell.address ;
}
struct DPCell *currentTraceBack = bestCell.address ;
int alignPos = 0 ;
while( currentTraceBack )
{
if( currentTraceBack->traceBackType == 2 )
{
// Diagonal
rowAlign[alignPos] = currentTraceBack->rowChar ;
colAlign[alignPos] = currentTraceBack->colChar ;
++alignPos ;
}
else if( currentTraceBack->traceBackType == 1 )
{
// Horizontal
rowAlign[alignPos] = '-' ;
colAlign[alignPos] = currentTraceBack->colChar ;
++alignPos ;
}
else if( currentTraceBack->traceBackType == 0 )
{
// Veritcal
rowAlign[alignPos] = currentTraceBack->rowChar ;
colAlign[alignPos] = '-' ;
++alignPos ;
}
}
// Reverse the alignments
//char topSequence[] = reverse(rowAlign) ;
// char bottomSequence[] = reverse(colAlign) ;
// Print sequences
//printf("%c\n\n",topSequence) ;
//printf("%c\n",bottomSequence) ;
printf("%c\n\n",rowAlign) ;
printf("%c\n",colAlign) ;
}
Any suggestions?