00001
00002
00003 #include "open_prospect.h"
00004
00005
00006 pValType **_2d_array_malloc( int x, int y ) {
00007 pValType **array;
00008
00009 array = (pValType **)malloc(sizeof(pValType *) * x);
00010 array[0] = (pValType *)malloc(sizeof(pValType) * x * y );
00011
00012 for ( int i = 0; i < x; i++ ) {
00013 array[i] = array[0] + i * y;
00014 for (int j = 0; j < y; j++)
00015 array[i][j] = 0.0;
00016 }
00017 return array;
00018 }
00019
00020 void _2d_array_free( pValType **array ) {
00021 free( array[0] );
00022 free( array );
00023 }
00024
00025
00026 void DynamicAlign::SetDim( int x, int y ) {
00027 if ( energy_matrix ) {
00028 free( energy_matrix[0] );
00029 free( energy_matrix );
00030 }
00031 if ( score_matrix ) {
00032 free( score_matrix[0] );
00033 free( score_matrix );
00034 }
00035 if ( source_matrix ) {
00036 free( source_matrix[0] );
00037 free( source_matrix );
00038 }
00039 size_x = x;
00040 size_y = y;
00041 energy_matrix = (pValType **)malloc(sizeof(pValType*) * (x + 1) );
00042 energy_matrix[0] = (pValType *)malloc(sizeof(pValType) * (x + 1) * (y + 1) );
00043
00044 score_matrix = (pValType **)malloc(sizeof(pValType*) * (x + 1) );
00045 score_matrix[0] = (pValType *)malloc(sizeof(pValType) * (x + 1) * (y + 1) );
00046
00047 source_matrix = (char **)malloc(sizeof(char*) * (x + 1) );
00048 source_matrix[0] = (char *)malloc(sizeof(char) * (x + 1) * (y + 1) );
00049
00050 for (int i = 0; i < size_x+1; i++) {
00051 score_matrix[i] = score_matrix[0] + (size_y+1) * i;
00052 source_matrix[i] = source_matrix[0] + (size_y+1) * i;
00053 energy_matrix[i] = energy_matrix[0] + (size_y+1) * i;
00054 for (int j = 0; j < size_y + 1; j++) {
00055 score_matrix[i][j] = NAN;
00056 energy_matrix[i][j] = NAN;
00057 source_matrix[i][j] = 0;
00058 }
00059 }
00060 }
00061
00062
00063 void ProspectDynamicAlign::CalcEnergyTable( ProspectParam *param,
00064 WeightArray weights,
00065 TemplateStruct *template_data,
00066 TargetStruct *target_data ) {
00067 SetDim( target_data->len, template_data->len );
00068 gap_open = weights[ e_gapopen ];
00069 gap_extend = weights[ e_gapconst ];
00070 for (int i = 0; i < target_data->len; i++) {
00071 for (int j = 0; j < template_data->len; j++) {
00072 int template_residue_no = j;
00073 int target_residue_no = i;
00074 energy_matrix[i][j] = calc_single_energy(param, weights, template_data, target_data, template_residue_no, target_residue_no);
00075 assert(!isnan(energy_matrix[i][j]));
00076 }
00077 }
00078 }
00079
00080
00081
00082 void DynamicAlign::ContinueAlign( int start_x, int start_y ) {
00083 pValType val;
00084 char state;
00085 if ( start_x < size_x && start_y < size_y ) {
00086 if ( start_x > 0 && start_y > 0 ) {
00087 val = GetCellVal( start_x-1, start_y-1 );
00088 state = GetCellState( start_x-1, start_y-1 );
00089
00090 } else {
00091 if ( start_x == 0 && start_y == 0 ) {
00092 state = vMatch;
00093 val = 0;
00094 } else {
00095 if ( start_x > 0 ) {
00096 state = vLeft;
00097 val = gap_open + gap_extend * ( start_x );
00098 }
00099 if ( start_y > 0 ) {
00100 state = vUp;
00101 val = gap_open + gap_extend * ( start_y );
00102 }
00103 }
00104 }
00105 Align( start_x, start_y, size_x, size_y, state, val );
00106 }
00107 }
00108
00109
00110
00111 void DynamicAlign::ForceXDelete( int start_x, int len ) {
00112 for ( int x = start_x; x < start_x + len; x++) {
00113 for ( int y = 0; y < size_y; y++ ) {
00114 pValType score = score_matrix[ x ][ y+1 ];
00115 if ( source_matrix[x][y+1] != vLeft )
00116 score += gap_open;
00117 score += gap_extend;
00118 source_matrix[x+1][y+1] = vLeft;
00119 }
00120 }
00121 }
00122
00123 void DynamicAlign::ForceYDelete( int start_y, int len ) {
00124 for ( int y = start_y; y < start_y + len; y++) {
00125 for ( int x = 0; x < size_x; x++ ) {
00126 pValType score = score_matrix[ x+1 ][ y ];
00127 if ( source_matrix[x+1][y] != vUp )
00128 score += gap_open;
00129 score += gap_extend;
00130 source_matrix[x+1][y+1] = vUp;
00131 }
00132 }
00133 }
00134
00135 void DynamicAlign::ForceAlign( int start_x, int start_y, int len ) {
00136 for (int i = 0; i < len; i++) {
00137 if ( start_x+i < size_x && start_y+i < size_y &&
00138 start_x+i >= 0 && start_y+i >= 0) {
00139 score_matrix[start_x+i+1][start_y+i+1] = score_matrix[start_x+i][start_y+i] + energy_matrix[start_x+i][start_y+i];
00140 source_matrix[start_x+i+1][start_y+i+1] = vMatch;
00141
00142 }
00143 }
00144 }
00145
00146
00147
00148
00149 void DynamicAlign::Align(int start_x, int start_y, int end_x, int end_y, char start_state, pValType start_val) {
00150 int x,y;
00151 pValType score_left, score_up, score_match;
00152
00153 assert(start_x >= 0 && start_x < size_x);
00154 assert(start_y >= 0 && start_y < size_y);
00155 assert(end_x >= 0 && end_x <= size_x);
00156 assert(end_y >= 0 && end_y <= size_y);
00157 assert(start_x <= end_x);
00158 assert(start_y <= end_y);
00159
00160 score_matrix[start_x][start_y] = start_val;
00161 source_matrix[start_x][start_y] = start_state;
00162
00163
00164 for (y = start_y+1; y < end_y+1; y++) {
00165 if ( start_state != vUp )
00166 score_matrix[start_x][y] = start_val + (gap_open) + (gap_extend)*(y-(start_y));
00167 else
00168 score_matrix[start_x][y] = start_val + (gap_extend)*(y-(start_y));
00169 source_matrix[start_x][y] = vUp;
00170 }
00171 for (x = start_x+1; x < end_x+1; x++) {
00172 if ( start_state != vLeft )
00173 score_matrix[x][start_y] = start_val + (gap_open) + (gap_extend)*(x-(start_x));
00174 else
00175 score_matrix[x][start_y] = start_val + (gap_extend)*(x-(start_x));
00176 source_matrix[x][start_y] = vLeft;
00177 }
00178
00179 for (x = start_x; x < end_x; x++) {
00180 for (y = start_y; y < end_y; y++) {
00181 if ( x >= 0 && x < size_x ) {
00182 score_matrix[x+1][y+1] = energy_matrix[x][y];
00183 assert(!isnan( score_matrix[x+1][y+1] ) );
00184 } else {
00185 score_matrix[x+1][y+1] = 0;
00186 }
00187 }
00188 }
00189 for (x = start_x; x < end_x; x++) {
00190 for (y = start_y; y < end_y; y++) {
00191 score_match = score_matrix[x][y] + score_matrix[x+1][y+1];
00192
00193 score_up = score_matrix[x+1][y];
00194 if (source_matrix[x+1][y] == vMatch || source_matrix[x+1][y] == vLeft )
00195 score_up += gap_open;
00196 score_up += gap_extend;
00197
00198 score_left = score_matrix[x][y+1];
00199 if (source_matrix[x][y+1] == vMatch || source_matrix[x][y+1] == vUp )
00200 score_left += gap_open;
00201 score_left += gap_extend;
00202
00203 if ( score_match <= score_left && score_match <= score_up) {
00204 score_matrix[x+1][y+1] = score_match;
00205 source_matrix[x+1][y+1] = vMatch;
00206 } else {
00207 if ( score_left <= score_up ) {
00208 score_matrix[x+1][y+1] = score_left;
00209 source_matrix[x+1][y+1] = vLeft;
00210 } else {
00211 score_matrix[x+1][y+1] = score_up;
00212 source_matrix[x+1][y+1] = vUp;
00213 }
00214 }
00215 assert(!isnan(score_matrix[x+1][y+1]));
00216 }
00217 }
00218 }
00219
00220 #if 0
00221
00222 vector float cur_score_vector;
00223 vector unsigned char source_vec, tmp_vec1, tmp_vec2, tmp_vec3, vUp_vec, vMatch_vec;
00224 float *score_start, *score_row_start, *score_ptr, *top_ptr, *left_ptr, *top_left_ptr;
00225 char *source_start, *source_ptr, *source_row_start;
00226 vector bool select_vec1, select_vec2, select_vec3, select_vec4;
00227 vector float open_vec, extend_vec,
00228 score_vec1, score_vec2, score_vec3, score_vec4,
00229 top_vec1, top_vec2, top_vec3, top_vec4,
00230 top_left_vec1, top_left_vec2, top_left_vec3, top_left_vec4,
00231 left_vec1, left_vec2, left_vec3, left_vec4,
00232 score_match_vec1, score_match_vec2, score_match_vec3, score_match_vec4,
00233 score_up_vec1, score_up_vec2, score_up_vec3, score_up_vec4;
00234 vector unsigned char vAlign,
00235 vSourceAlign1 = (vector unsigned char)(0x00,0x04,0x08,0x0C,0x10,0x14,0x18,0x1C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00),
00236 vSourceAlign2 = (vector unsigned char)(0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x04,0x08,0x0C,0x10,0x14,0x18,0x1C),
00237 vSourceAlign3 = (vector unsigned char)(0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x18,0x19,0x20,0x21,0x22,0x23,0x24,0x25);
00238 long row_size = (((template_data->len/16)+2)*16);
00239 unsigned char blksize=4;
00240 unsigned char blkcount=1;
00241 short blkstride = 64;
00242 int cwrd = blkstride | (blkcount<<16) | (blksize<<24);
00243
00244 vUp_vec = vec_splat_u8( vUp );
00245 vMatch_vec = vec_splat_u8( vMatch );
00246
00247
00248 open_vec = vec_lde( 0, &open_score );
00249 vAlign = vec_lvsl( 0, &open_score );
00250 open_vec = vec_perm( open_vec, open_vec, vAlign );
00251 open_vec = vec_splat( open_vec, 0 );
00252
00253 extend_vec = vec_lde( 0, &extend_score );
00254 vAlign = vec_lvsl( 0, &extend_score );
00255 extend_vec = vec_perm( extend_vec, extend_vec, vAlign );
00256 extend_vec = vec_splat( extend_vec, 0 );
00257
00258 score_matrix = (float **)malloc(sizeof(float *) * (target_data->len+1) );
00259 score_start = (float *)valloc(sizeof(float) * (row_size) * (target_data->len+1) );
00260 score_matrix[0] = &score_start[3];
00261
00262 source_matrix = (char **)malloc(sizeof(char *) * (target_data->len+1));
00263 source_start = (char *)valloc(sizeof(char) * (row_size) * (target_data->len+1) );
00264 source_matrix[0] = &source_start[15];
00265
00266
00267 for (i = 1; i < target_data->len+1; i++) {
00268 score_matrix[i] = score_matrix[0] + (row_size) * i;
00269 source_matrix[i] = source_matrix[0] + (row_size) * i;
00270 }
00271
00272 score_ptr = score_start + row_size + 4;
00273 source_ptr = source_start + row_size + 16;
00274 top_ptr = score_start + 4;
00275 top_left_ptr = score_start + 3;
00276
00277 for (x = 0; x < target_data->len; x++) {
00278 score_row_start = score_ptr;
00279 source_row_start = source_ptr;
00280 for (y = 0; y < template_data->len; y+=16) {
00281 vec_dstst( score_ptr, cwrd, 0);
00282 vec_dstst( top_ptr, cwrd, 0);
00283 vec_dstst( (unsigned char *)source_ptr, cwrd, 0);
00284
00285 score_vec1 = vec_ld(0, score_ptr);
00286 score_vec2 = vec_ld(4, score_ptr);
00287 score_vec3 = vec_ld(8, score_ptr);
00288 score_vec4 = vec_ld(12, score_ptr);
00289
00290 source_vec = vec_ld(0, (unsigned char*)source_ptr);
00291
00292 top_vec1 = vec_ld(0, top_ptr);
00293 top_vec2 = vec_ld(4, top_ptr);
00294 top_vec3 = vec_ld(8, top_ptr);
00295 top_vec4 = vec_ld(12, top_ptr);
00296
00297 top_left_vec1 = vec_ld(0, top_left_ptr);
00298 vAlign = vec_lvsl ( 0 , top_left_ptr);
00299 top_left_vec1 = vec_perm ( top_left_vec1, top_vec1, vAlign);
00300 top_left_vec2 = vec_perm ( top_vec1, top_vec2, vAlign );
00301 top_left_vec3 = vec_perm ( top_vec2, top_vec3, vAlign );
00302 top_left_vec4 = vec_perm ( top_vec3, top_vec4, vAlign );
00303
00304 score_match_vec1 = vec_add(score_vec1, top_left_vec1);
00305 score_match_vec2 = vec_add(score_vec2, top_left_vec2);
00306 score_match_vec3 = vec_add(score_vec3, top_left_vec3);
00307 score_match_vec4 = vec_add(score_vec4, top_left_vec4);
00308
00309 score_up_vec1 = vec_add(score_vec1, top_vec1);
00310 score_up_vec2 = vec_add(score_vec1, top_vec2);
00311 score_up_vec3 = vec_add(score_vec1, top_vec3);
00312 score_up_vec4 = vec_add(score_vec1, top_vec4);
00313
00314 select_vec1 = vec_cmple( score_match_vec1, score_up_vec1 );
00315 select_vec2 = vec_cmple( score_match_vec2, score_up_vec2 );
00316 select_vec3 = vec_cmple( score_match_vec3, score_up_vec3 );
00317 select_vec4 = vec_cmple( score_match_vec4, score_up_vec4 );
00318
00319 tmp_vec1 = vec_perm( (vector unsigned char)select_vec1, (vector unsigned char)select_vec2, vSourceAlign1);
00320 tmp_vec2 = vec_perm( (vector unsigned char)select_vec3, (vector unsigned char)select_vec4, vSourceAlign2);
00321
00322 tmp_vec3 = vec_perm( tmp_vec1, tmp_vec2, vSourceAlign3);
00323
00324 source_vec = vec_sel( vMatch_vec, vUp_vec, tmp_vec3 );
00325
00326 score_vec1 = vec_sel( score_match_vec1, score_up_vec1, select_vec1 );
00327 score_vec2 = vec_sel( score_match_vec2, score_up_vec2, select_vec2 );
00328 score_vec3 = vec_sel( score_match_vec3, score_up_vec3, select_vec3 );
00329 score_vec4 = vec_sel( score_match_vec4, score_up_vec4, select_vec4 );
00330
00331 vec_st(source_vec, 0, (unsigned char *)source_ptr );
00332 vec_st(score_vec1, 0, score_ptr );
00333 vec_st(score_vec2, 4, score_ptr );
00334 vec_st(score_vec3, 8, score_ptr );
00335 vec_st(score_vec4, 12, score_ptr );
00336
00337 score_ptr += 16;
00338 source_ptr += 16;
00339 top_ptr += 16;
00340 top_left_ptr += 16;
00341 }
00342
00343 score_ptr += row_size-y;
00344 source_ptr += row_size-y;
00345 top_ptr += row_size-y;
00346 top_left_ptr += row_size-y;
00347
00348 for (i = 0; i < template_data->len; i++) {
00349 if (score_row_start[i] > score_row_start[i-1] + open_score) {
00350 source_row_start[i] = vLeft;
00351 score_row_start[i] = score_row_start[i-1] + open_score;
00352 }
00353
00354 }
00355 }
00356 #endif
00357
00358
00359
00360
00361
00362 void DynamicAlign::TraceAlign( int start_x, int start_y, int end_x, int end_y,
00363 int *align_x, int *align_y) {
00364 int x = end_x - 1;
00365 int y = end_y - 1;
00366 while( x >= start_x || y >= start_y) {
00367 if (source_matrix[x+1][y+1] == vUp) {
00368 align_y[y] = -1;
00369 y--;
00370 } else if (source_matrix[x+1][y+1] == vLeft) {
00371 align_x[x] = -1;
00372 x--;
00373 } else if (source_matrix[x+1][y+1] == vMatch ) {
00374 if (x >= 0 && x < size_x && y >= 0 && y <size_y ) {
00375 align_x[x] = y;
00376 align_y[y] = x;
00377 }
00378 x--;
00379 y--;
00380 } else {
00381 assert(0);
00382 return;
00383 }
00384 }
00385 }
00386
00387
00388 void DynamicAlign::TraceAlign( AlignmentStruct *out_align ) {
00389 int *target_align = (int *)malloc(sizeof(int) * size_x);
00390 int *template_align = (int *)malloc(sizeof(int) * size_y);
00391 TraceAlign( target_align, template_align );
00392 out_align->SetAlign( size_x, target_align, size_y, template_align);
00393 }
00394
00395 void ProspectDynamicCoreAlign:: CalcEnergyTable(ProspectParam *param,
00396 WeightArray weights,
00397 TemplateStruct *template_data,
00398 TargetStruct *target_data ) {
00399 ProspectDynamicAlign::CalcEnergyTable( param, weights, template_data, target_data );
00400 extra_del_score = _2d_array_malloc( target_data->len, template_data->len );
00401 extra_ins_score = _2d_array_malloc( target_data->len, template_data->len );
00402 for (int i = 0; i < template_data->len; i++) {
00403 for (int j = 0; j < target_data->len; j++) {
00404 extra_del_score[j][i] = calc_del_val( param, weights, template_data, target_data, i, j );
00405 }
00406 }
00407 for (int i = 0; i < template_data->len; i++) {
00408 for (int j = 0; j < target_data->len; j++) {
00409 extra_ins_score[j][i] = calc_ins_val( param, weights, template_data, target_data, i, j );
00410 }
00411 }
00412 }
00413
00414
00415 void ProspectDynamicCoreAlign::Align(int start_x, int start_y, int end_x, int end_y, char start_state, pValType start_val) {
00416 int x,y;
00417 pValType score_left, score_up, score_match;
00418
00419 assert(start_x >= 0 && start_x < size_x);
00420 assert(start_y >= 0 && start_y < size_y);
00421 assert(end_x >= 0 && end_x <= size_x);
00422 assert(end_y >= 0 && end_y <= size_y);
00423 assert(start_x <= end_x);
00424 assert(start_y <= end_y);
00425
00426 score_matrix[start_x][start_y] = start_val;
00427 source_matrix[start_x][start_y] = start_state;
00428
00429
00430 for (y = start_y+1; y < end_y+1; y++) {
00431 if ( start_state != vUp )
00432 score_matrix[start_x][y] = start_val + (gap_open) + (gap_extend)*(y-(start_y));
00433 else
00434 score_matrix[start_x][y] = start_val + (gap_extend)*(y-(start_y));
00435 for ( int y1 = start_y+1; y1 < y; y1++)
00436 score_matrix[start_x][y] += extra_del_score[ start_x ][ y1 ];
00437 source_matrix[start_x][y] = vUp;
00438 }
00439 for (x = start_x+1; x < end_x+1; x++) {
00440 if ( start_state != vLeft )
00441 score_matrix[x][start_y] = start_val + (gap_open) + (gap_extend)*(x-(start_x));
00442 else
00443 score_matrix[x][start_y] = start_val + (gap_extend)*(x-(start_x));
00444
00445 for ( int x1 = start_x; x1 < x; x1++)
00446 score_matrix[x][start_y] += extra_ins_score[ x1 ][ start_y ];
00447
00448 source_matrix[x][start_y] = vLeft;
00449 }
00450
00451 for (x = start_x; x < end_x; x++) {
00452 for (y = start_y; y < end_y; y++) {
00453 if ( x >= 0 && x < size_x ) {
00454 score_matrix[x+1][y+1] = energy_matrix[x][y];
00455 assert(!isnan( score_matrix[x+1][y+1] ) );
00456 } else {
00457 score_matrix[x+1][y+1] = 0;
00458 }
00459 }
00460 }
00461 for (x = start_x; x < end_x; x++) {
00462 for (y = start_y; y < end_y; y++) {
00463 score_match = score_matrix[x][y] + score_matrix[x+1][y+1];
00464 score_up = score_matrix[x+1][y] + extra_del_score[ x ][ y ];
00465 if (source_matrix[x+1][y] == vMatch || source_matrix[x+1][y] == vLeft )
00466 score_up += gap_open;
00467 score_up += gap_extend;
00468
00469 score_left = score_matrix[x][y+1] + extra_ins_score[ x ][ y ];
00470 if (source_matrix[x][y+1] == vMatch || source_matrix[x][y+1] == vUp )
00471 score_left += gap_open;
00472 score_left += gap_extend;
00473
00474 if ( score_match <= score_left && score_match <= score_up) {
00475 score_matrix[x+1][y+1] = score_match;
00476 source_matrix[x+1][y+1] = vMatch;
00477 } else {
00478 if ( score_left <= score_up ) {
00479 score_matrix[x+1][y+1] = score_left;
00480 source_matrix[x+1][y+1] = vLeft;
00481 } else {
00482 score_matrix[x+1][y+1] = score_up;
00483 source_matrix[x+1][y+1] = vUp;
00484 }
00485 }
00486 assert(!isnan(score_matrix[x+1][y+1]));
00487 }
00488 }
00489 }