/*Takes in as input the vector of iterations from coarse BLAST, an index into *the vector (representing the index of which query we want to get the results *from), and the database we are using for search and returns a vector of *every original sequence section re-created from the calls to *cb_coarse_expand for the hits in the iteration we are expanding. */ struct DSVector *expand_blast_hits(struct DSVector *iterations, int index, struct cb_database_r *db){ struct DSVector *expanded_hits = ds_vector_create(), *hits = get_blast_hits( (xmlNode *)ds_vector_get(iterations, index)); int i = 0, j = 0, k = 0; for (i = 0; i < hits->size; i++) { struct hit *current_hit = (struct hit *)ds_vector_get(hits, i); struct DSVector *hsps = current_hit->hsps; for (j = 0; j < hsps->size; j++) { struct DSVector *oseqs; struct hsp *h = (struct hsp *)ds_vector_get(hsps, j); int32_t coarse_start = h->hit_from-1, coarse_end = h->hit_to-1, coarse_seq_id = current_hit->accession; oseqs = cb_coarse_expand(db->coarse_db, db->com_db, coarse_seq_id, coarse_start, coarse_end, 50); for (k = 0; k < oseqs->size; k++) ds_vector_append(expanded_hits, ds_vector_get(oseqs, k)); ds_vector_free_no_data(oseqs); } ds_vector_free(current_hit->hsps); free(current_hit); } ds_vector_free_no_data(hits); return expanded_hits; }
static zval *php_ds_vector_read_dimension(zval *obj, zval *offset, int type, zval *return_value) { ds_vector_t *vector = Z_DS_VECTOR_P(obj); zval *value; // Dereference the offset if it's a reference. ZVAL_DEREF(offset); // `??` if (type == BP_VAR_IS) { if (Z_TYPE_P(offset) != IS_LONG || ! ds_vector_isset(vector, Z_LVAL_P(offset), 0)) { return &EG(uninitialized_zval); } } // Enforce strict integer index. if (Z_TYPE_P(offset) != IS_LONG) { INTEGER_INDEX_REQUIRED(offset); return NULL; } // Access the value at the given index. value = ds_vector_get(vector, Z_LVAL_P(offset)); // If we're accessing by reference we have to create a reference. // This is for access like $deque[$a][$b] = $c if (value && type != BP_VAR_R) { ZVAL_MAKE_REF(value); } return value; }
/*Takes in a vector of expansion structs for the expanded BLAST hits from a query and outputs them to the FASTA file CaBLAST_fine.fasta.*/ void write_fine_fasta(struct DSVector *oseqs){ FILE *temp; int i; if (NULL == (temp = fopen("CaBLAST_fine.fasta", "w"))) { fprintf(stderr, "fopen: 'fopen %s' failed: %s\n", "CaBLAST_fine.fasta", strerror(errno)); exit(1); } if (!temp) { fprintf(stderr, "Could not open CaBLAST_fine.fasta for writing\n"); return; } for (i = 0; i < oseqs->size; i++) { struct cb_seq *current_seq = ((struct cb_hit_expansion *)ds_vector_get(oseqs, i))->seq; fprintf(temp, "> %s\n%s\n", current_seq->name, current_seq->residues); } fclose(temp); if (search_flags.fine_blast_db) system("makeblastdb -dbtype nucl -in CaBLAST_fine.fasta -out " "CaBLAST_fine.fasta"); }
/* tries to register current frame onto previous frame. * Algorithm: * discards fields with low contrast * select maxfields fields according to their contrast * check theses fields for vertical and horizontal transformation * use minimal difference of all possible positions * calculate shift as cleaned mean of all remaining fields * calculate rotation angle of each field in respect to center of fields * after shift removal * calculate rotation angle as cleaned mean of all angles * compensate for possibly off-center rotation */ Transform calcTransFields(MotionDetect* md, calcFieldTransFunc fieldfunc, contrastSubImgFunc contrastfunc) { Transform* ts = (Transform*) ds_malloc(sizeof(Transform) * md->fieldNum); Field** fs = (Field**) ds_malloc(sizeof(Field*) * md->fieldNum); double *angles = (double*) ds_malloc(sizeof(double) * md->fieldNum); int i, index = 0, num_trans; Transform t; #ifdef STABVERBOSE FILE *file = NULL; char buffer[32]; ds_snprintf(buffer, sizeof(buffer), "k%04i.dat", md->frameNum); file = fopen(buffer, "w"); fprintf(file, "# plot \"%s\" w l, \"\" every 2:1:0\n", buffer); #endif DSVector goodflds = selectfields(md, contrastfunc); // use all "good" fields and calculate optimal match to previous frame #ifdef USE_OMP #pragma omp parallel for shared(goodflds, md, ts, fs) // does not bring speedup #endif for(index=0; index < ds_vector_size(&goodflds); index++){ int i = ((contrast_idx*)ds_vector_get(&goodflds,index))->index; t = fieldfunc(md, &md->fields[i], i); // e.g. calcFieldTransYUV #ifdef STABVERBOSE fprintf(file, "%i %i\n%f %f %i\n \n\n", md->fields[i].x, md->fields[i].y, md->fields[i].x + t.x, md->fields[i].y + t.y, t.extra); #endif if (t.extra != -1) { // ignore if extra == -1 (unused at the moment) ts[index] = t; fs[index] = md->fields + i; } } t = null_transform(); num_trans = ds_vector_size(&goodflds); // amount of transforms we actually have ds_vector_del(&goodflds); if (num_trans < 1) { ds_log_warn(md->modName, "too low contrast! No field remains.\n" "(no translations are detected in frame %i)", md->frameNum); return t; } int center_x = 0; int center_y = 0; // calc center point of all remaining fields for (i = 0; i < num_trans; i++) { center_x += fs[i]->x; center_y += fs[i]->y; } center_x /= num_trans; center_y /= num_trans; if (md->show) { // draw fields and transforms into frame. // this has to be done one after another to handle possible overlap if (md->show > 1) { for (i = 0; i < num_trans; i++) drawFieldScanArea(md, fs[i], &ts[i]); } for (i = 0; i < num_trans; i++) drawField(md, fs[i], &ts[i]); for (i = 0; i < num_trans; i++) drawFieldTrans(md, fs[i], &ts[i]); } /* median over all transforms t= median_xy_transform(ts, md->field_num);*/ // cleaned mean t = cleanmean_xy_transform(ts, num_trans); // substract avg for (i = 0; i < num_trans; i++) { ts[i] = sub_transforms(&ts[i], &t); } // figure out angle if (md->fieldNum < 6) { // the angle calculation is inaccurate for 5 and less fields t.alpha = 0; } else { for (i = 0; i < num_trans; i++) { angles[i] = calcAngle(md, fs[i], &ts[i], center_x, center_y); } double min, max; t.alpha = -cleanmean(angles, num_trans, &min, &max); if (max - min > md->maxAngleVariation) { t.alpha = 0; ds_log_info(md->modName, "too large variation in angle(%f)\n", max-min); } } // compensate for off-center rotation double p_x = (center_x - md->fi.width / 2); double p_y = (center_y - md->fi.height / 2); t.x += (cos(t.alpha) - 1) * p_x - sin(t.alpha) * p_y; t.y += sin(t.alpha) * p_x + (cos(t.alpha) - 1) * p_y; #ifdef STABVERBOSE fclose(file); #endif return t; }
int main(int argc, char **argv){ FILE *query_file = NULL, *test_hits_file = NULL; struct cb_database_r *db = NULL; struct opt_config *conf; struct opt_args *args; struct DSVector *iterations = NULL, *expanded_hits = NULL, *queries = NULL, *oseqs = ds_vector_create(); struct fasta_seq *query = NULL; xmlDoc *doc = NULL; xmlNode *root = NULL; uint64_t dbsize = 0; int i = 0, j = 0; conf = load_search_args(); args = opt_config_parse(conf, argc, argv); if (args->nargs < 2) { fprintf(stderr, "Usage: %s [flags] database-dir fasta-file " "[ --blast_args BLASTN_ARGUMENTS ]\n", argv[0]); opt_config_print_usage(conf); exit(1); } system("rm CaBLAST_results.xml"); if (!search_flags.hide_progress) fprintf(stderr, "Loading database data\n\n"); db = cb_database_r_init(args->args[0], (search_flags.load_coarse_db || search_flags.load_coarse_residues), (search_flags.load_coarse_db || search_flags.load_coarse_links), search_flags.load_compressed_db, search_flags.link_block_size); dbsize = read_int_from_file(8, db->coarse_db->db->file_params); if (!search_flags.hide_progress) fprintf(stderr, "Running coarse BLAST\n\n"); blast_coarse(args, dbsize); if (NULL == (query_file = fopen(args->args[1], "r"))) { fprintf(stderr, "fopen: 'fopen %s' failed: %s\n", args->args[1], strerror(errno)); exit(1); } queries = ds_vector_create(); query = fasta_read_next(query_file, ""); while (query) { ds_vector_append(queries, (void *)query); query = fasta_read_next(query_file, ""); } fclose(query_file); if (!search_flags.hide_progress) fprintf(stderr, "Processing coarse BLAST hits for fine BLAST\n\n"); if (search_flags.show_hit_info) if (NULL == (test_hits_file = fopen("CaBLAST_hits.txt", "w"))) { fprintf(stderr, "fopen: 'fopen %s' failed: %s\n", "CaBLAST_hits.txt", strerror(errno)); exit(1); } //Parse the XML file generated from coarse BLAST and get its iterations. doc = xmlReadFile("CaBLAST_temp_blast_results.xml", NULL, 0); if (doc == NULL) { fprintf(stderr, "Could not parse CaBLAST_temp_blast_results.xml\n"); return 0; } root = xmlDocGetRootElement(doc); iterations = get_blast_iterations(root); for (i = 0; i < iterations->size; i++) { if (!search_flags.hide_progress) { int32_t digits_full = floor(log10((double)iterations->size)), digits_i = floor(log10((double)i)), spaces; char *bar = progress_bar(i, iterations->size); spaces = digits_full - digits_i; fprintf(stderr, "\r"); fprintf(stderr, "iteration: %d/%d", i+1, iterations->size); for (j = 0; j < spaces; j++) putc(' ', stderr); fprintf(stderr, " %s ", bar); free(bar); } /*Expand any BLAST hits we got from the current query sequence during coarse BLAST.*/ expanded_hits = expand_blast_hits(iterations, i, db); for (j = 0; j < expanded_hits->size; j++) ds_vector_append(oseqs, ds_vector_get(expanded_hits, j)); ds_vector_free_no_data(expanded_hits); } write_fine_fasta(oseqs); for (i = 0; i < oseqs->size; i++) cb_hit_expansion_free( (struct cb_hit_expansion *)ds_vector_get(oseqs, i)); ds_vector_free_no_data(oseqs); blast_fine(args, dbsize); if (!search_flags.hide_progress) fprintf(stderr, "\n"); //Make a newline after the progress bar for (i = 0; i < queries->size; i++) fasta_free_seq((struct fasta_seq *)ds_vector_get(queries, i)); ds_vector_free_no_data(queries); if (search_flags.show_hit_info) fclose(test_hits_file); //Free the XML data and expanded hits for (i = 0; i < iterations->size; i++) { struct DSVector *iteration = (struct DSVector *)ds_vector_get(iterations, i); for (j = 0; j < iteration->size; j++) { struct hit *h = (struct hit *)ds_vector_get(iteration, j); ds_vector_free(h->hsps); free(h); } } ds_vector_free_no_data(iterations); cb_database_r_free(db); xmlFreeDoc(doc); /*Free the coarse BLAST results file if the --no-cleanup flag is not being used.*/ if (!search_flags.no_cleanup) system("rm CaBLAST_temp_blast_results.xml"); opt_args_free(args); opt_config_free(conf); return 0; }