/* colors just rotate around, works for now, can be confusing when you have some fairly high values on the board */ void draw_grid(WINDOW *gamewin) { // mvwprintw will sometimes have a useless arg, this is warned, but doesn't affect the program char *scr = sl ? "SCORE: %d (+%d)\n" : "SCORE: %d\n"; mvwprintw(gamewin, 0, 0, scr, s, sl); mvwprintw(gamewin, 1, 0, "HISCR: %d\n", hs); ITER(SZ*(MAXVAL + 2) + 1, waddch(gamewin, '-')); int i, j, xps = 0, yps = 3; for (i = 0; i < SZ; i++, xps = 0, yps++) { mvwprintw(gamewin, yps, xps++, "|"); for (j = 0; j < SZ; j++) { if (g[i][j]) { wattron(gamewin, COLOR_PAIR(flog2(g[i][j]) & 7)); mvwprintw(gamewin, yps, xps, "%*d", MAXVAL, g[i][j]); wattroff(gamewin, COLOR_PAIR(flog2(g[i][j]) & 7)); mvwprintw(gamewin, yps, xps + MAXVAL, " |"); } else { ITER(MAXVAL + 1, waddch(gamewin, ' ')); waddch(gamewin, '|'); } xps += (MAXVAL + 2); } } ITER(SZ*(MAXVAL + 2) + 1, waddch(gamewin, '-')); wrefresh(gamewin); }
inline bitcount_t flog2(uint64_t v) { #if defined(_M_X64) || defined(_M_ARM) || defined(_M_ARM64) unsigned long i; _BitScanReverse64(&i, v); return i; #else // 32-bit x86 uint32_t high = v >> 32; uint32_t low = uint32_t(v); return high ? 32+flog2(high) : flog2(low); #endif }
// Calculate secondary structure for given HMM and return prediction void CalculateSS(HMM& q, char *ss_pred, char *ss_conf) { char tmpfile[]="/tmp/hhCalcSSXXXXXX"; if (mkstemp(tmpfile) == -1) { cerr << "ERROR! Could not create tmp-file!\n"; exit(4); } // Write log-odds matrix from q to tmpfile.mtx char filename[NAMELEN]; FILE* mtxf = NULL; strcpy(filename,tmpfile); strcat(filename,".mtx"); mtxf = fopen(filename,"w"); if (!mtxf) OpenFileError(filename); fprintf(mtxf,"%i\n",q.L); fprintf(mtxf,"%s\n",q.seq[q.nfirst]+1); fprintf(mtxf,"2.670000e-03\n4.100000e-02\n-3.194183e+00\n1.400000e-01\n2.670000e-03\n4.420198e-02\n-3.118986e+00\n1.400000e-01\n3.176060e-03\n1.339561e-01\n-2.010243e+00\n4.012145e-01\n"); for (int i = 1; i <= q.L; ++i) { fprintf(mtxf,"-32768 "); for (int a = 0; a < 20; ++a) { int tmp = iround(50*flog2(q.p[i][s2a[a]]/pb[s2a[a]])); fprintf(mtxf,"%5i ",tmp); if (a == 0) { // insert logodds value for B fprintf(mtxf,"%5i ",-32768); } else if (a == 18) { // insert logodds value for X fprintf(mtxf,"%5i ",-100); } else if (a == 19) { // insert logodds value for Z fprintf(mtxf,"%5i ",-32768); } } fprintf(mtxf,"-32768 -400\n"); } fclose(mtxf); // Calculate secondary structure CalculateSS(ss_pred, ss_conf, tmpfile); q.AddSSPrediction(ss_pred, ss_conf); // Remove temp-files std::string command = "rm " + (std::string)tmpfile + "*"; runSystem(command,v); }
void params::select_value(float bts) { for ( int i=0; ; i++ ) { if ( v_n <= partition_tbl[i].upper_bound ) break; } if ( bts == 0.0 ) v_bits = partition_tbl[i].bits; else v_bits = bts; v_r = 0; v_b = ( v_n > 0 ) ? (int)( float(v_n*v_bits) / (1.0+flog2(v_n)) ) : 0; v_p1 = (int)( partition_tbl[i].p1 * v_n); v_p2 = (int)( partition_tbl[i].p2 * v_b); if ( v_p1 == 0 ) v_p1++; if ( v_p2 == 0 ) v_p2++; v_seed = 1; }
/* * Convert a f77 tree statement to something that looks like a * pcc expression tree. */ NODE * putx(bigptr q) { struct bigblock *x1; NODE *p = NULL; /* XXX */ int opc; int type, k; #ifdef PCC_DEBUG if (tflag) { printf("putx %p\n", q); fprint(q, 0); } #endif switch(q->tag) { case TERROR: ckfree(q); break; case TCONST: switch(type = q->vtype) { case TYLOGICAL: type = tyint; case TYLONG: case TYSHORT: p = mklnode(ICON, q->b_const.fconst.ci, 0, types2[type]); ckfree(q); break; case TYADDR: p = mklnode(ICON, 0, 0, types2[type]); p->n_name = copys(memname(STGCONST, (int)q->b_const.fconst.ci)); ckfree(q); break; default: p = putx(putconst(q)); break; } break; case TEXPR: switch(opc = q->b_expr.opcode) { case OPCALL: case OPCCALL: if( ISCOMPLEX(q->vtype) ) p = putcxop(q); else { putcall(q); p = callval; } break; case OPMIN: case OPMAX: p = putmnmx(q); break; case OPASSIGN: if (ISCOMPLEX(q->b_expr.leftp->vtype) || ISCOMPLEX(q->b_expr.rightp->vtype)) { frexpr(putcxeq(q)); } else if (ISCHAR(q)) p = putcheq(q); else goto putopp; break; case OPEQ: case OPNE: if (ISCOMPLEX(q->b_expr.leftp->vtype) || ISCOMPLEX(q->b_expr.rightp->vtype) ) { p = putcxcmp(q); break; } case OPLT: case OPLE: case OPGT: case OPGE: if(ISCHAR(q->b_expr.leftp)) p = putchcmp(q); else goto putopp; break; case OPPOWER: p = putpower(q); break; case OPSTAR: /* m * (2**k) -> m<<k */ if (XINT(q->b_expr.leftp->vtype) && ISICON(q->b_expr.rightp) && ((k = flog2(q->b_expr.rightp->b_const.fconst.ci))>0) ) { q->b_expr.opcode = OPLSHIFT; frexpr(q->b_expr.rightp); q->b_expr.rightp = MKICON(k); goto putopp; } case OPMOD: goto putopp; case OPPLUS: case OPMINUS: case OPSLASH: case OPNEG: if( ISCOMPLEX(q->vtype) ) p = putcxop(q); else goto putopp; break; case OPCONV: if( ISCOMPLEX(q->vtype) ) p = putcxop(q); else if (ISCOMPLEX(q->b_expr.leftp->vtype)) { p = putx(mkconv(q->vtype, realpart(putcx1(q->b_expr.leftp)))); ckfree(q); } else goto putopp; break; case OPAND: /* Create logical AND */ x1 = fmktemp(TYLOGICAL, NULL); putexpr(mkexpr(OPASSIGN, cpexpr(x1), mklogcon(0))); k = newlabel(); putif(q->b_expr.leftp, k); putif(q->b_expr.rightp, k); putexpr(mkexpr(OPASSIGN, cpexpr(x1), mklogcon(1))); putlabel(k); p = putx(x1); break; case OPNOT: /* Logical NOT */ x1 = fmktemp(TYLOGICAL, NULL); putexpr(mkexpr(OPASSIGN, cpexpr(x1), mklogcon(1))); k = newlabel(); putif(q->b_expr.leftp, k); putexpr(mkexpr(OPASSIGN, cpexpr(x1), mklogcon(0))); putlabel(k); p = putx(x1); break; case OPOR: /* Create logical OR */ x1 = fmktemp(TYLOGICAL, NULL); putexpr(mkexpr(OPASSIGN, cpexpr(x1), mklogcon(1))); k = newlabel(); putif(mkexpr(OPEQ, q->b_expr.leftp, mklogcon(0)), k); putif(mkexpr(OPEQ, q->b_expr.rightp, mklogcon(0)), k); putexpr(mkexpr(OPASSIGN, cpexpr(x1), mklogcon(0))); putlabel(k); p = putx(x1); break; case OPCOMMA: for (x1 = q; x1->b_expr.opcode == OPCOMMA; x1 = x1->b_expr.leftp) putexpr(x1->b_expr.rightp); p = putx(x1); break; case OPEQV: case OPNEQV: case OPADDR: case OPBITOR: case OPBITAND: case OPBITXOR: case OPBITNOT: case OPLSHIFT: case OPRSHIFT: putopp: p = putop(q); break; default: fatal1("putx: invalid opcode %d", opc); } break; case TADDR: p = putaddr(q, YES); break; default: fatal1("putx: impossible tag %d", q->tag); } return p; }
//////////////////////////////////////////////////////////////////////// // Main prefilter function //////////////////////////////////////////////////////////////////////// void Prefilter::prefilter_db(HMM* q_tmp, Hash<Hit>* previous_hits, const int threads, const int prefilter_gap_open, const int prefilter_gap_extend, const int prefilter_score_offset, const int prefilter_bit_factor, const double prefilter_evalue_thresh, const double prefilter_evalue_coarse_thresh, const int preprefilter_smax_thresh, const int min_prefilter_hits, const int maxnumdb, const float R[20][20], std::vector<std::pair<int, std::string> >& new_prefilter_hits, std::vector<std::pair<int, std::string> >& old_prefilter_hits) { Hash<char>* doubled = new Hash<char>; doubled->New(16381, 0); int element_count = (VECSIZE_INT * 4); //W = (LQ+15) / 16; // band width = hochgerundetes LQ/16 int W = (q_tmp->L + (element_count - 1)) / element_count; // query profile (states + 1 because of ANY char) unsigned char* qc = (unsigned char*)malloc_simd_int((hh::NUMCOLSTATES+1)*(q_tmp->L+element_count)*sizeof(unsigned char)); stripe_query_profile(q_tmp, prefilter_score_offset, prefilter_bit_factor, W, qc); simd_int ** workspace = new simd_int *[threads]; std::vector<std::pair<int, int> > first_prefilter; std::vector<std::pair<double, int> > hits; int count_dbs = 0; int gap_init = prefilter_gap_open + prefilter_gap_extend; int gap_extend = prefilter_gap_extend; int LQ = q_tmp->L; const float log_qlen = flog2(LQ); const double factor = (double) num_dbs * LQ; for (int i = 0; i < threads; i++) workspace[i] = (simd_int*) malloc_simd_int( 3 * (LQ + element_count) * sizeof(char)); #pragma omp parallel for schedule(static) // Loop over all database sequences for (size_t n = 0; n < num_dbs; n++) { int thread_id = 0; #ifdef OPENMP thread_id = omp_get_thread_num(); #endif // Perform search step int score = ungapped_sse_score(qc, LQ, first[n], length[n], prefilter_score_offset, workspace[thread_id]); score = score - (int) (prefilter_bit_factor * (log_qlen + flog2(length[n]))); #pragma omp critical first_prefilter.push_back(std::pair<int, int>(score, n)); } //filter after calculation of ungapped sse score to include at least min_prefilter_hits std::vector<std::pair<int, int> >::iterator it; sort(first_prefilter.begin(), first_prefilter.end()); std::reverse(first_prefilter.begin(), first_prefilter.end()); std::vector<std::pair<int, int> >::iterator first_prefilter_begin_erase = first_prefilter.end(); std::vector<std::pair<int, int> >::iterator first_prefilter_end_erase = first_prefilter.end(); count_dbs = 0; for (it = first_prefilter.begin(); it < first_prefilter.end(); it++) { if (count_dbs >= min_prefilter_hits && (*it).first <= preprefilter_smax_thresh) { first_prefilter_begin_erase = it; break; } else { count_dbs++; } } first_prefilter.erase(first_prefilter_begin_erase, first_prefilter_end_erase); HH_LOG(INFO) << "HMMs passed 1st prefilter (gapless profile-profile alignment) : " << count_dbs << std::endl; #pragma omp parallel for schedule(static) // Loop over all database sequences // for (int n = 0; n < count_dbs; n++) { for (size_t i = 0; i < first_prefilter.size(); i++) { int thread_id = 0; #ifdef OPENMP thread_id = omp_get_thread_num(); #endif int n = first_prefilter[i].second; // Perform search step int score = swStripedByte(qc, LQ, first[n], length[n], gap_init, gap_extend, workspace[thread_id], workspace[thread_id] + W, workspace[thread_id] + 2 * W, prefilter_score_offset); double evalue = factor * length[n] * fpow2(-score / prefilter_bit_factor); if (evalue < prefilter_evalue_coarse_thresh) { #pragma omp critical hits.push_back(std::pair<double, int>(evalue, n)); } } //filter after calculation of evalues to include at least min_prefilter_hits sort(hits.begin(), hits.end()); std::vector<std::pair<double, int> >::iterator second_prefilter_begin_erase = hits.end(); std::vector<std::pair<double, int> >::iterator second_prefilter_end_erase = hits.end(); std::vector<std::pair<double, int> >::iterator it2; count_dbs = 0; for (it2 = hits.begin(); it2 < hits.end(); it2++) { if (count_dbs >= min_prefilter_hits && (*it2).first > prefilter_evalue_thresh) { second_prefilter_begin_erase = it2; break; } else { count_dbs++; } } hits.erase(second_prefilter_begin_erase, second_prefilter_end_erase); count_dbs = 0; for (it2 = hits.begin(); it2 < hits.end(); it2++) { // Add hit to dbfiles count_dbs++; char db_name[NAMELEN]; strcpy(db_name, dbnames[(*it2).second]); char name[NAMELEN]; RemoveExtension(name, db_name); if (!doubled->Contains(db_name)) { doubled->Add(db_name); std::pair<int, std::string> result; result.first = length[(*it2).second]; result.second = std::string(db_name); // check, if DB was searched in previous rounds strcat(name, "__1"); // irep=1 if (previous_hits->Contains(name)) { old_prefilter_hits.push_back(result); } else { new_prefilter_hits.push_back(result); } } if (count_dbs >= maxnumdb) { HH_LOG(WARNING) << "Number of hits passing 2nd prefilter (reduced from " << hits.size() << " to allowed maximum of " << maxnumdb << ").\n" <<"You can increase the allowed maximum using the -maxfilt <max> option.\n"; break; } } // Free memory free(qc); for (int i = 0; i < threads; i++) free(workspace[i]); delete[] workspace; if (doubled) delete doubled; }
//////////////////////////////////////////////////////////////////////// // Prepare query profile for prefitering //////////////////////////////////////////////////////////////////////// void Prefilter::stripe_query_profile(HMM* q_tmp, const int prefilter_score_offset, const int prefilter_bit_factor, const int W, unsigned char* qc) { int LQ = q_tmp->L; float** query_profile = NULL; int a, h, i, j, k; // Build query profile with 219 column states query_profile = new float*[LQ + 1]; for (i = 0; i < LQ + 1; ++i) query_profile[i] = (float*) malloc_simd_int(hh::NUMCOLSTATES * sizeof(float)); const cs::ContextLibrary<cs::AA>& lib = *cs_lib; // log (S(i,k)) = log ( SUM_a p(i,a) * p(k,a) / f(a) ) k: column state, i: pos in ali, a: amino acid for (i = 0; i < LQ; ++i) for (k = 0; k < hh::NUMCOLSTATES; ++k) { float sum = 0; for (a = 0; a < 20; ++a) sum += ((q_tmp->p[i][a] * lib[k].probs[0][a]) / q_tmp->pav[a]); query_profile[i + 1][k] = sum; } ///////////////////////////////////////// // Stripe query profile with chars int element_count = (VECSIZE_INT * 4); for (a = 0; a < hh::NUMCOLSTATES; ++a) { h = a * W * element_count; for (i = 0; i < W; ++i) { j = i; for (k = 0; k < element_count; ++k) { if (j >= LQ) qc[h] = (unsigned char) prefilter_score_offset; else { float dummy = flog2(query_profile[j + 1][a]) * prefilter_bit_factor + prefilter_score_offset + 0.5; if (dummy > 255.0) qc[h] = 255; else if (dummy < 0) qc[h] = 0; else qc[h] = (unsigned char) dummy; // 1/3 bits & make scores >=0 everywhere } ++h; j += W; } } } // Add extra ANY-state (220'th state) h = hh::NUMCOLSTATES * W * element_count; for (i = 0; i < W; ++i) { j = i; for (k = 0; k < element_count; ++k) { if (j >= LQ) qc[h] = (unsigned char) prefilter_score_offset; else qc[h] = (unsigned char) (prefilter_score_offset - 1); h++; j += W; } } for (i = 0; i < LQ + 1; ++i) free(query_profile[i]); delete[] query_profile; }