Esempio n. 1
0
unsigned int kernel_launch_l (cl_kernel kernel, cl_context context, cl_command_queue cmd_queue, const char ** p, const char ** t, const struct gapmis_params * in, float * scores, struct gapmis_align * out)
{
	unsigned int 	i;
		 int	start;
	unsigned int	pats = 1;
	unsigned int	txts = 1;

	unsigned int	maxTxtLen = strlen(t[0]);
	unsigned int	maxPatLen = strlen(p[0]);
	unsigned int	pBlockSize = 1;
	unsigned int 	hproVecLen = pats * pBlockSize * (maxTxtLen + 1);
	unsigned int 	dproVecLen = pats * pBlockSize * (maxPatLen + 1);
	
	unsigned int * txtsVec = calloc(maxTxtLen*pBlockSize, sizeof(unsigned int));
	unsigned int * patsVec = calloc(maxPatLen*pats, sizeof(unsigned int));
	         int * argsVec = malloc(sizeof(int)*(pats+7));		
		 int * txtsLenVec = calloc(pBlockSize,sizeof(int));
	       float * pensVec = malloc(sizeof(float)*2);
	         int * hproVec = calloc(hproVecLen,sizeof(int));
	         int * dproVec = calloc(dproVecLen,sizeof(int));
       unsigned int ** H;

	H = malloc((maxTxtLen+1)*sizeof(unsigned int*));
	H[0] = calloc ((maxTxtLen+1)*(maxPatLen+1), sizeof(unsigned int));
	for ( i = 1 ; i < maxTxtLen + 1 ; ++ i )
     		H[i] = (void*)H[0] + i*(maxPatLen+1)* sizeof(unsigned int);


	if(patsVec == NULL || txtsVec == NULL    || argsVec == NULL || 
           pensVec == NULL || txtsLenVec == NULL || hproVec == NULL || 
	   dproVec == NULL || H == NULL)
	{
	 	errno = MALLOC;
      		return ( 0 );	
	}

	fill_txtsVec (txts, pBlockSize, t, txtsVec,in->scoring_matrix);
	fill_patsVec (pats, maxPatLen, p, patsVec,in->scoring_matrix);
	fill_argsVec (pats, txts, p, in->max_gap, pBlockSize, maxPatLen, maxTxtLen, argsVec);
	fill_txtsLenVec (txts, t, txtsLenVec);

	pensVec[0] = - in -> gap_open_pen;
	pensVec[1] = - in -> gap_extend_pen;


	if(in->scoring_matrix==0)
		kernel_one_to_one_dna (0, 0, patsVec, txtsVec, argsVec, txtsLenVec, pensVec, hproVec, dproVec, scores, H, out, &start);
	else
		kernel_one_to_one_pro (0, 0, patsVec, txtsVec, argsVec, txtsLenVec, pensVec, hproVec, dproVec, scores, H, out, &start);  

   	if ( out -> min_gap > 0 ) 
        	backtracing ( H, maxPatLen, maxTxtLen, start, out );


   	if ( out -> where == 1 ) 
     		num_mismatch ( t[0], maxTxtLen, p[0], maxPatLen, out );
   	else                   
     		num_mismatch ( p[0], maxPatLen, t[0], maxTxtLen, out );

	free (txtsVec);
	free (patsVec);
	free (argsVec);
	free (txtsLenVec);
	free (pensVec);
	free (hproVec);
	free (dproVec);
        free ( H[0] );
	free (H);
	
	return 1;
}
Esempio n. 2
0
int main ( int argc, char ** argv)
 {
   struct TSwitch  sw;
   char   * out_file;

   unsigned int MAXnumgaps;	//input argument		
   double gap_open_pen;		//input argument
   double gap_extend_pen;	//input argument
   unsigned int scoring_matrix; //input argument
   unsigned int MAXgap;	
   unsigned int num_mat;        //input argument + 1

   double MAXscore;		//to be computed
   unsigned int MINnumgaps;	//to be computed		
   
   unsigned int istart;		//where to start backtracing
   unsigned int jstart;		//where to start backtracing
   unsigned int iend;		//where to end backtracing
   unsigned int jend;		//where to end backtracing
   unsigned int * gaps_pos;	//position of the gap(s)
   unsigned int * gaps_len;	//len of the gap(s)
   unsigned int * where;	//where is the gap(s): text [1] or pattern [2]
   
   struct TSeq * t;		//text t
   unsigned int n; 		//length of text
   struct TSeq * p;		//pattern p
   unsigned int m; 		//length of pattern
   unsigned int L;		//local alignment	
   
   double ***       G; 		//dynamic programming matrix
   int *** 	 H; 		//backtracing matrix
   	
   unsigned int swap;           //swap the text and the pattern in case m < n        
   unsigned int i, j;

   /* checks the arguments */
   i = decode_switches ( argc, argv, &sw );

   if ( i < 5 || ! sw . seq_a || ! sw . seq_b ) 
    {
      usage ();
      return ( 1 );
    }
   else 
    {
      gap_open_pen   = - sw . gap_open_pen;	//the penalties should have a negative value
      gap_extend_pen = - sw . gap_extend_pen;
      out_file       =   sw . out_file;
      L		     =   sw . L;

      if ( ! strcmp ( "EDNAFULL", sw . matrix ) )       scoring_matrix = 0;
      else if ( ! strcmp ( "EBLOSUM62", sw . matrix ) ) scoring_matrix = 1;
      else
       {
         fprintf ( stderr, "Error: scoring matrix argument should be `EDNAFULL' for nucleotide sequences or `EBLOSUM62' for protein sequences!!!\n" );
         return ( 1 );
       }
      if ( L > 1 )
       {
         fprintf (stderr, "Error: wrong argument with L: should be either 0 (semi-global) or 1 (local)\n" );
         return ( 1 );
       }
    }
   
   /* reads the text */
   t = read_fasta_file ( sw . seq_a );
   if ( ! t )
    {
      fprintf (stderr, "Error: cannot read file %s!!!\n", sw . seq_a );
      return ( 1 );
    }
   if ( ! t -> header ) t -> header = strdup ( "Seq A" );
   
   /* reads the pattern */
   p = read_fasta_file ( sw . seq_b );
   if ( ! p )
    {
      fprintf( stderr, "Error: cannot read file %s!!!\n", sw . seq_b );
      return ( 1 );
    }
   if ( ! p -> header ) p -> header = strdup ( "Seq B" );
   
   /* calculate text's and pattern's length */
   n = strlen ( t -> data );
   m = strlen ( p -> data );
   
   /* checks the lengths of text and pattern and swaps if needed */
   if ( m > n )
    {
      swap_txt_pat ( &t, &n, &p, &m );
      swap = 1;
    }
   else
      swap = 0;
   
   /* checks the max num of gaps: MAXnumgaps < n */
   MAXnumgaps =  ( sw . max_num_gaps <= -1 ) ?  2 : sw . max_num_gaps;
   if( MAXnumgaps >= n )
    {
      fprintf ( stderr, "Error: the max gap length should be less than the length of the text!!!\n" );
      return ( 1 );
    }

   if ( L == 1 ) num_mat = MAXnumgaps + 1;
   else num_mat = MAXnumgaps;

   MAXgap =  ( sw . max_gap <= -1 ) ?  n - 1 : sw . max_gap;
   if( MAXgap >= n )
    {
      fprintf ( stderr, "Error: the max gap length should be less than the length of the text!!!\n" );
      return ( 1 );
    }
   
   /* 3d dynamic memory allocation for matrices G and H*/
   if( ( G = ( double *** ) malloc ( ( num_mat ) * sizeof( double ** ) ) ) == NULL )
    {
      fprintf( stderr, "G could not be allocated\n" );
      return 0;
    } 
	
   for ( i = 0; i < num_mat; i ++ )
    {
      if( ( G[i] = ( double ** ) malloc ( ( n + 1 ) * sizeof( double * ) ) ) == NULL )
       {
         fprintf( stderr, "G could not be allocated\n" );
         return 0;
       }
 
      if( ( G[i][0] = ( double * ) calloc ( ( n + 1 ) * ( m + 1 ), sizeof( double ) ) ) == NULL )
       {
	  fprintf( stderr, "G could not be allocated\n" );
	  return 0;
       } 

      for ( j = 1; j < n + 1; j++ )
        G[i][j] = ( void * ) G[i][0] + j * ( m + 1 ) * sizeof( double );
    }

   if( ( H = ( int *** ) malloc ( ( num_mat ) * sizeof( int ** ) ) ) == NULL )
    {
      fprintf( stderr, "H could not be allocated\n" );
      return 0;
    } 
	
   for ( i = 0; i < num_mat; i ++ )
    {
      if( ( H[i] = ( int ** ) malloc ( ( n + 1 ) * sizeof( int * ) ) ) == NULL )
       {
         fprintf( stderr, "H could not be allocated\n" );
         return 0;
       }
 
      if( ( H[i][0] = ( int * ) calloc ( ( n + 1 ) * ( m + 1 ), sizeof( int ) ) ) == NULL )
       {
	  fprintf( stderr, "H could not be allocated\n" );
	  return 0;
       } 

      for ( j = 1; j < n + 1; j++ )
        H[i][j] = ( void* ) H[i][0] + j * ( m + 1 ) * sizeof( int );
    }

   /* dynamic programming algorithm */
   if ( L == 0 )	
    {
     if ( MAXgap == n - 1 )
      {
        if ( ! ( dp_algorithm( G, MAXnumgaps, H, t -> data, n, p -> data, m, scoring_matrix, gap_open_pen, gap_extend_pen ) ) )
         {
           fprintf ( stderr, "Error: dp_algorithm() failed!!!\n" );
           return ( 1 );	
         }
      }
     else
      {
        if ( ! ( dp_algorithm_pruned( G, MAXnumgaps, H, t -> data, n, p -> data, m, scoring_matrix, gap_open_pen, gap_extend_pen, MAXgap ) ) )
         {
           fprintf ( stderr, "Error: dp_algorithm_pruned() failed!!!\n" );
           return ( 1 );	
         }
      }
    }
   else
    {
     if ( MAXgap == n - 1 )
      {
        if ( ! ( dp_algorithm_lcl( G, MAXnumgaps, H, t -> data, n, p -> data, m, scoring_matrix, gap_open_pen, gap_extend_pen ) ) )
         {
           fprintf ( stderr, "Error: dp_algorithm() failed!!!\n" );
           return ( 1 );	
         }
      }
     else
      {
        fprintf ( stderr, "Error: option `- m' is only for global alignment!!!\n" );
        return ( 1 );	
      }
    }


   MINnumgaps = 0;	//to be computed		
   /* finds the optimal alignment based on the matrices scores */
   if ( L == 0 ) opt_solution ( G, MAXnumgaps, n, m, &MAXscore, &istart, &MINnumgaps );
   if ( L == 1 ) opt_solution_lcl ( G, MAXnumgaps, n, m, &MAXscore, &istart, &jstart, &MINnumgaps );

   if( ( gaps_pos = ( unsigned int * ) calloc ( MINnumgaps, sizeof( unsigned int ) ) ) == NULL )
    {
      fprintf( stderr, "gaps_pos could not be allocated\n" );
      return 0;
    }

   if( ( gaps_len = ( unsigned int * ) calloc ( MINnumgaps, sizeof( unsigned int ) ) ) == NULL )
    {
      fprintf( stderr, "gaps_pos could not be allocated\n" );
      return 0;
    }

   if( ( where = ( unsigned int * ) calloc ( MINnumgaps, sizeof( unsigned int ) ) ) == NULL )
    {
      fprintf( stderr, "where could not be allocated\n" );
      return 0;
    }
 
   /* computes the position of the gap */
   if ( L == 0 ) backtracing ( H[MINnumgaps - 1], m, n, istart, gaps_pos, MINnumgaps, gaps_len, where );
   if ( L == 1 ) backtracing_lcl ( G[MINnumgaps], m, n, H[MINnumgaps], istart, jstart, gaps_pos, MINnumgaps, gaps_len, where, &iend, &jend );

   #if 0
   int s;
   for ( s = 0;  s < num_mat; s++ )
        {

                for(i = 0; i < n+1; i++)                //Matrix G output
                {
                        for(j = 0; j < m+1; j++)
                        {
                                fprintf( stderr,"%d\t", (int) G[s][i][j] );
                        }
                        fprintf(stderr,"\n");
                }

                fprintf(stderr,"\n");

                #if 1
                for(i = 0; i < n+1; i++)                //Matrix H output
                {
                        for(j = 0; j < m+1; j++)
                        {
                                fprintf( stderr,"%d\t", H[s][i][j]);
                        }
                        fprintf(stderr,"\n");
                }

                fprintf(stderr,"\n");
                #endif
        }
   if ( L == 1 ) fprintf( stderr,"\n[%d,%d]-->[%d,%d]\n", istart, jstart, iend, jend );
   #endif

   /* outputs the results */
   if ( L == 0 )
     if ( ! ( results ( out_file, t, n, p, m, MAXscore, gaps_pos, MINnumgaps, gaps_len, where, swap, scoring_matrix, gap_open_pen, gap_extend_pen, L ) ) )
      {
        fprintf(stderr, "Error: results() failed!!!\n");
        return ( 1 );	
      }

   if ( L == 1 )
     if ( ! ( results_lcl ( out_file, t, n, p, m, MAXscore, gaps_pos, MINnumgaps, gaps_len, where, istart, iend, jstart, jend, swap, scoring_matrix, gap_open_pen, gap_extend_pen, L ) ) )
      {
        fprintf(stderr, "Error: results() failed!!!\n");
        return ( 1 );	
      }

   for ( i = 0;  i < num_mat; i++ )
    {
      free ( G[i][0] );
      free ( G[i] );
      free ( H[i][0] );
      free ( H[i] );
    }
   free ( G );
   free ( H );
   free ( gaps_pos );
   free ( gaps_len );
   free ( where );
   free ( t -> header );
   free ( p -> header );
   free ( t );
   free ( p );
   free ( sw . out_file );
   free ( sw . matrix );
   return ( 0 );
 }