void shape_free(shape_t* shape) { assert( shape!=NULL ); kv_destroy(shape->polys); kv_destroy(shape->points); kv_destroy(shape->hull); free(shape); }
static void *worker(void *data) { worker_t *w = (worker_t *)data; for(size_t i = w->start; i < w->n; i+= w->step) { kseq_t *s = &kv_A(w->reads, i); aln_v result = align_read(s, w->ref_seqs, w->config); kstring_t str = { 0, 0, NULL }; write_sam_records(&str, s, result, w->ref_seqs, w->read_group_id, w->config->n_keep, w->config->max_drop); w->sams[i] = str; for(size_t j = 0; j < kv_size(result); j++) free(kv_A(result, j).cigar); kv_destroy(result); kseq_stack_destroy(s); } return 0; }
void test_double_float() { test_header(); double d = 2.2354e-10; float f = d; uint32_t i = 7821334; uchar_vec b; unsigned char *buffer = NULL; kv_init(b); dump_double(d, &b); buffer = b.a; fprintf(stderr, "%g == %g - ", d, load_double(&buffer)); fprintf(stderr, "buffer used: %zu\n", buffer - b.a); kv_clear(b); dump_float(f, &b); buffer = b.a; fprintf(stderr, "%g == %g - ", d, load_float(&buffer)); fprintf(stderr, "buffer used: %zu\n", buffer - b.a); kv_clear(b); dump_uint32(i, &b); buffer = b.a; fprintf(stderr, "%d == %d - ", i, load_uint32(&buffer)); fprintf(stderr, "buffer used: %zu\n", buffer - b.a); kv_destroy(b); }
void shapes_free(shapes_v* shapes) { for(uint32_t i=0; i< kv_size(*shapes); i++) { shape_free(kv_A(*shapes, i)); } kv_destroy(*shapes); free(shapes); }
int main(void) { int a[4] = {50, 2, 1, 9}; int b[3] = {5, 50, 56}; vec_vec_u8_t vv = digify_array(b, 3); qsort (vv.a, 3, sizeof(vec_u8_t), compare_vec); for(size_t i = 0 ; i < kv_size(vv) ; i++) { printf("%d", undigify(kv_A(vv,i))); } printf("\n"); for(size_t i = 0 ; i < kv_size(vv) ; i++) { kv_destroy(kv_A(vv,i)); } kv_destroy(vv); return 0; }
static void free_dellist(dl_list *dl) { unsigned int i; for (i = 0; i < kv_size(*dl); i++) free(kv_A(*dl, i)); kv_destroy(*dl); }
// creat Chan's hull void shape_build_hull(shape_t* shape) { assert(shape!= NULL); points_v hull; kv_init(hull); kv_copy(point_t, hull, shape->points); uint32_t s = chanhull(hull.a, hull.n); uint32_t l = hull.n-s; // copy part of points vector kv_resize(point_t, shape->hull, l+1); shape->hull.n = l+1; // memcpy(shape->hull.a, hull.a+s, sizeof(point_t)*l); // shape->hull.a[l] = *(hull.a+s); // revert point direction for(uint32_t i=0; i<=l;i++) { shape->hull.a[l-i] = hull.a[s+i%l]; } kv_destroy(hull); }
void test_forest_serialization() { test_header(); ET_problem prob; ET_params params; ET_forest *forest, *forest2; uchar_vec buffer; unsigned char *mobile_buffer; float vector[] = {3, 1, 1, 6, 6, 2}; kv_init(buffer); problem_init(&prob, big_vectors, big_labels); EXTRA_TREE_DEFAULT_REGR_PARAMS(prob, params); params.number_of_trees = 100; params.number_of_features_tested = 1; params.select_features_with_replacement = true; forest = ET_forest_build(&prob, ¶ms); ET_forest_dump(forest, &buffer, true); fprintf(stderr, "forest dump: %zu bytes\n", kv_size(buffer)); mobile_buffer = buffer.a; forest2 = ET_forest_load(&mobile_buffer); forest2->params = forest->params; // FIXME fprintf(stderr, "orig forest pred: %g\n", ET_forest_predict(forest, vector)); fprintf(stderr, "cloned forest pred: %g\n", ET_forest_predict(forest2, vector)); kv_destroy(buffer); ET_forest_destroy(forest); ET_forest_destroy(forest2); free(forest); free(forest2); }
static int pkg_jobs_universe_process_deps(struct pkg_jobs_universe *universe, struct pkg *pkg, unsigned flags) { struct pkg_dep *d = NULL; int (*deps_func)(const struct pkg *pkg, struct pkg_dep **d); int rc; struct pkg_job_universe_item *unit; struct pkg *npkg, *rpkg; pkg_chain_t *rpkgs = NULL; bool found = false; rpkg = NULL; rc = EPKG_OK; if (flags & DEPS_FLAG_REVERSE) { deps_func = pkg_rdeps; } else { deps_func = pkg_deps; } while (deps_func(pkg, &d) == EPKG_OK) { HASH_FIND_STR(universe->items, d->uid, unit); if (unit != NULL) { continue; } rpkgs = NULL; npkg = NULL; if (!(flags & DEPS_FLAG_MIRROR)) { npkg = pkg_jobs_universe_get_local(universe, d->uid, 0); } if (!(flags & DEPS_FLAG_FORCE_LOCAL)) { /* Check for remote dependencies */ rpkgs = pkg_jobs_universe_get_remote(universe, d->uid, 0); } if (npkg == NULL && rpkgs == NULL) { pkg_emit_error("%s has a missing dependency: %s", pkg->name, d->name); if (flags & DEPS_FLAG_FORCE_MISSING) { continue; } return (EPKG_FATAL); } if (npkg != NULL) { if (pkg_jobs_universe_process_item(universe, npkg, &unit) != EPKG_OK) { continue; } } if (rpkgs == NULL) continue; /* * When processing deps, we should first try to select a dependency * from the same repo. * Otherwise, we would have ping-pong of dependencies instead of * the situation when this behaviour is handled by * CONSERVATIVE_UPGRADES. * * Important notes here: * 1. We are looking for packages that are dependencies of a package * `pkg` * 2. Now if `pkg` belongs to repo `r` and `rpkg` belongs to repo * `r` then we just select it. * 3. If `rpkg` is not found in `r` we just scan all packages */ /* * XXX: this is the proper place to expand flexible dependencies */ found = false; /* Iteration one */ for (int i = 0; i < kv_size(*rpkgs); i++) { rpkg = kv_A(*rpkgs, i); if (pkg->reponame && rpkg->reponame && strcmp (pkg->reponame, rpkg->reponame) == 0) { found = true; break; } } /* Fallback if a dependency is not found in the same repo */ if (!found) { for (int i = 0; i < kv_size(*rpkgs); i++) { rpkg = kv_A(*rpkgs, i); if (npkg != NULL) { /* Set reason for upgrades */ if (!pkg_jobs_need_upgrade(rpkg, npkg)) continue; /* Save automatic flag */ rpkg->automatic = npkg->automatic; } rc = pkg_jobs_universe_process_item(universe, rpkg, NULL); /* Special case if we cannot find any package */ if (npkg == NULL && rc != EPKG_OK) { kv_destroy(*rpkgs); free(rpkgs); return (rc); } } } else { assert (rpkg != NULL); if (npkg != NULL) { /* Set reason for upgrades */ if (!pkg_jobs_need_upgrade(rpkg, npkg)) continue; /* Save automatic flag */ rpkg->automatic = npkg->automatic; } rc = pkg_jobs_universe_process_item(universe, rpkg, NULL); if (npkg == NULL && rc != EPKG_OK) { kv_destroy(*rpkgs); free(rpkgs); return (rc); } } kv_destroy(*rpkgs); free(rpkgs); } return (EPKG_OK); }
void align_reads(const char *ref_path, const char *qry_path, const char *output_path, const int32_t match, /* 2 */ const int32_t mismatch, /* 2 */ const int32_t gap_o, /* 3 */ const int32_t gap_e, /* 1 */ const uint8_t n_threads, /* 1 */ const int32_t n_keep, const int32_t max_drop, const char *read_group, const char *read_group_id) { gzFile read_fp, ref_fp; FILE *out_fp; int32_t j, k, l; const int m = 5; kseq_t *seq; int8_t *mat = (int8_t *)calloc(25, sizeof(int8_t)); /* This table is used to transform nucleotide letters into numbers. */ uint8_t table[128] = { 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 0, 4, 1, 4, 4, 4, 2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 3, 0, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 0, 4, 1, 4, 4, 4, 2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 3, 0, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4 }; // initialize scoring matrix for genome sequences for(l = k = 0; LIKELY(l < 4); ++l) { for(j = 0; LIKELY(j < 4); ++j) mat[k++] = l == j ? match : -mismatch; /* weight_match : -weight_mismatch */ mat[k++] = 0; // ambiguous base } for(j = 0; LIKELY(j < 5); ++j) mat[k++] = 0; // Read reference sequences ref_fp = gzopen(ref_path, "r"); assert(ref_fp != NULL && "Failed to open reference"); seq = kseq_init(ref_fp); kseq_v ref_seqs; ref_seqs = read_seqs(seq, 0); kseq_destroy(seq); gzclose(ref_fp); fprintf(stderr, "[sw_align] Read %lu references\n", kv_size(ref_seqs)); // Print SAM header out_fp = fopen(output_path, "w"); fprintf(out_fp, "@HD\tVN:1.4\tSO:unsorted\n"); for(size_t i = 0; i < kv_size(ref_seqs); i++) { seq = &kv_A(ref_seqs, i); fprintf(out_fp, "@SQ\tSN:%s\tLN:%d\n", seq->name.s, (int32_t)seq->seq.l); } if(read_group) { fputs(read_group, out_fp); fputc('\n', out_fp); } align_config_t conf; conf.gap_o = gap_o; conf.gap_e = gap_e; conf.m = m; conf.table = table; conf.mat = mat; conf.n_keep = n_keep; conf.max_drop = max_drop; read_fp = gzopen(qry_path, "r"); assert(read_fp != NULL && "Failed to open query"); size_t count = 0; seq = kseq_init(read_fp); while(true) { kseq_v reads = read_seqs(seq, 5000 * n_threads); const size_t n_reads = kv_size(reads); if(!n_reads) { break; } worker_t *w = calloc(n_threads, sizeof(worker_t)); kstring_t *sams = calloc(n_reads, sizeof(kstring_t)); for(size_t i = 0; i < n_threads; i++) { w[i].start = i; w[i].n = n_reads; w[i].step = n_threads; w[i].ref_seqs = ref_seqs; w[i].reads = reads; w[i].sams = sams; w[i].config = &conf; w[i].read_group_id = read_group_id; } if(n_threads == 1) { worker(w); } else { pthread_t *tid = calloc(n_threads, sizeof(pthread_t)); for(size_t i = 0; i < n_threads; ++i) pthread_create(&tid[i], 0, worker, &w[i]); for(size_t i = 0; i < n_threads; ++i) pthread_join(tid[i], 0); } free(w); for(size_t i = 0; i < n_reads; i++) { fputs(sams[i].s, out_fp); free(sams[i].s); } free(sams); count += n_reads; kv_destroy(reads); } kseq_destroy(seq); fprintf(stderr, "[sw_align] Aligned %lu reads\n", count); // Clean up reference sequences kvi_destroy(kseq_stack_destroy, ref_seqs); gzclose(read_fp); fclose(out_fp); free(mat); }
int exec_which(int argc, char **argv) { struct pkgdb *db = NULL; struct pkgdb_it *it = NULL; struct pkg *pkg = NULL; char pathabs[MAXPATHLEN]; char *p, *path, *match, *savedpath; int ret = EPKG_OK, retcode = EX_SOFTWARE; int ch, i; int res, pathlen = 0; bool orig = false; bool glob = false; bool search = false; bool search_s = false; charlist patterns; struct option longopts[] = { { "glob", no_argument, NULL, 'g' }, { "origin", no_argument, NULL, 'o' }, { "path-search", no_argument, NULL, 'p' }, { "quiet", no_argument, NULL, 'q' }, { NULL, 0, NULL, 0 }, }; path = NULL; while ((ch = getopt_long(argc, argv, "+gopq", longopts, NULL)) != -1) { switch (ch) { case 'g': glob = true; break; case 'o': orig = true; break; case 'p': search_s = true; break; case 'q': quiet = true; break; default: usage_which(); return (EX_USAGE); } } argc -= optind; argv += optind; if (argc < 1) { usage_which(); return (EX_USAGE); } if (pkgdb_open(&db, PKGDB_DEFAULT) != EPKG_OK) { return (EX_IOERR); } if (pkgdb_obtain_lock(db, PKGDB_LOCK_READONLY) != EPKG_OK) { pkgdb_close(db); warnx("Cannot get a read lock on a database, it is locked by another process"); return (EX_TEMPFAIL); } if (search_s) { if ((path = getenv("PATH")) == NULL) { printf("$PATH is not set, falling back to non-search behaviour\n"); search_s = false; } else { pathlen = strlen(path) + 1; } } while (argc >= 1) { kv_init(patterns); retcode = EX_SOFTWARE; if (search_s) { if ((argv[0][0] == '.') || (argv[0][0] == '/')) { search = false; } else { search = true; if (strlen(argv[0]) >= FILENAME_MAX) { retcode = EX_USAGE; goto cleanup; } p = malloc(pathlen); if (p == NULL) { retcode = EX_OSERR; goto cleanup; } strlcpy(p, path, pathlen); match = NULL; savedpath=p; for (;;) { res = get_match(&match, &p, argv[0]); if (p == NULL) break; if (res == (EX_USAGE)) { printf("%s was not found in PATH, falling back to non-search behaviour\n", argv[0]); search = false; } else if (res == (EX_OSERR)) { retcode = EX_OSERR; free(savedpath); goto cleanup; } else { pkg_absolutepath(match, pathabs, sizeof(pathabs)); /* ensure not not append twice an entry if PATH is messy */ if (already_in_list(&patterns, pathabs)) continue; kv_push(char *, patterns, strdup(pathabs)); free(match); } } free(savedpath); } } if (!glob && !search) { pkg_absolutepath(argv[0], pathabs, sizeof(pathabs)); kv_push(char *, patterns, strdup(pathabs)); } else if (!search) { if (strlcpy(pathabs, argv[0], sizeof(pathabs)) >= sizeof(pathabs)) { retcode = EX_USAGE; goto cleanup; } } for (i = 0; i < kv_size(patterns); i++) { if ((it = pkgdb_query_which(db, kv_A(patterns, i), glob)) == NULL) { retcode = EX_IOERR; goto cleanup; } pkg = NULL; while ((ret = pkgdb_it_next(it, &pkg, PKG_LOAD_BASIC)) == EPKG_OK) { retcode = EX_OK; if (quiet && orig) pkg_printf("%o\n", pkg); else if (quiet && !orig) pkg_printf("%n-%v\n", pkg, pkg); else if (!quiet && orig) pkg_printf("%S was installed by package %o\n", kv_A(patterns, i), pkg); else if (!quiet && !orig) pkg_printf("%S was installed by package %n-%v\n", kv_A(patterns, i), pkg, pkg); } if (retcode != EX_OK && !quiet) printf("%s was not found in the database\n", kv_A(patterns, i)); pkg_free(pkg); pkgdb_it_free(it); } kv_destroy(patterns); argc--; argv++; } cleanup: pkgdb_release_lock(db, PKGDB_LOCK_READONLY); pkgdb_close(db); return (retcode); }
static void instance_destroy(struct instance *inst) { lua_close(inst->state); kv_destroy(inst->callstack); free(inst); }
mem_alnreg_v mem_fmeas_fliter_se(mem_alnreg_v a , int n , int l_seq , int mode) { mem_alnreg_v aa ; int i , j ; kvec_t(FF_t) k_ff_t ; kv_init(k_ff_t); kv_init(aa); // caculate FMEAS value if(n == 0) return aa ; for( i = 0 ; i < a.n ; i++){ mem_alnreg_t *p_ar = a.a + i ; for( j = i + 1 ; j < a.n ; j++){ FF_t tmp ; mem_alnreg_t *q_ar = a.a + j ; double sens , spec ; int FN = 0 , TP = 0 ,TN = 0 , FP = 0 ; int A,B,C,D; if( p_ar->qb < q_ar->qb || (p_ar->qb == q_ar->qb && p_ar->qe >= q_ar->qe)){ // p q A = p_ar->qb ; B = p_ar->qe - 1 ; C = q_ar->qb ; D = q_ar->qe - 1 ; }else { // p q A = q_ar->qb ; B = q_ar->qe - 1; C = p_ar->qb ; D = p_ar->qe - 1; } if(B < C){ TP = B - A + D - C + 2 ; FN = l_seq - D - 1 + A + C - B - 1 ; TN = l_seq ; FP = 0 ; }else if( D <= B){ // contain continue ; }else{ TP = D - A + 1 ; FN = l_seq - D - 1 + A ; FP = B - C + 1 ; TN = l_seq - FP; } sens = (double)TP/(double)(TP+FN); spec = (double)TN/(double)(TN+FP); tmp.FMEAS = (2*spec*sens)/(spec+sens); tmp.score = p_ar->score + q_ar->score; tmp.x = i , tmp.y = j ; if(tmp.FMEAS > 0.95) kv_push(FF_t,k_ff_t,tmp); } } ks_introsort(ff_mem_flt, k_ff_t.n, k_ff_t.a); kv_push(mem_alnreg_t,aa,a.a[0]); double max_feas ; // int score ; if( k_ff_t.n == 0 ) return aa; max_feas = k_ff_t.a[0].FMEAS ; // score = k_ff_t.a[0].score ; if(mode){ int cnt = 0 ; for( i = 0 ; i < kv_size(k_ff_t) ; i++){ FF_t p = kv_A(k_ff_t,i); if(p.x == 0 && cnt == 0){ kv_push(mem_alnreg_t,aa,a.a[p.y]); cnt = 1 ; }else if(p.x == 0){ kv_push(mem_alnreg_t,aa,a.a[0]); kv_push(mem_alnreg_t,aa,a.a[p.y]); } } for( i = 0 ; i < kv_size(k_ff_t); i++){ FF_t p = kv_A(k_ff_t,i); if(max_feas != p.FMEAS ) break; if(p.x == 0) continue ; kv_push(mem_alnreg_t,aa,a.a[p.x]); kv_push(mem_alnreg_t,aa,a.a[p.y]); } }else{ int cnt = 0 ; for( i = 0 ; i < kv_size(k_ff_t); i++){ FF_t p = kv_A(k_ff_t,i); if(max_feas != p.FMEAS ) break; if(p.x == 0 && cnt == 0){ kv_push(mem_alnreg_t,aa,a.a[p.y]); continue ; }else if( p.x == 0 ){ kv_push(mem_alnreg_t,aa,a.a[0]); kv_push(mem_alnreg_t,aa,a.a[p.y]); continue ; } kv_push(mem_alnreg_t,aa,a.a[p.x]); kv_push(mem_alnreg_t,aa,a.a[p.y]); } } kv_destroy(k_ff_t); #if 0 for( i = 0 ; i < kv_size(aa); i++){ mem_alnreg_t *q = aa.a + i; printf("%db: %d %de:%d \t" , i, q->qb , i, q->qe); if( i == kv_size(aa) -1 ) printf("\n"); } #endif return aa ; }