void test_tadbit_on_real_input (void) { // -- Open input file (the code is hopelessly not portable) -- // char *pch; int i; int *obs[2]; obs[0] = malloc(1413*1413 * sizeof(int)); obs[1] = malloc(1413*1413 * sizeof(int)); FILE *f[2]; f[0] = fopen("data/HindIII_T60.tsv", "r"); f[1] = fopen("data/NcoI_T60.tsv", "r"); g_assert(f[0] != NULL); g_assert(f[1] != NULL); // `getline` is available because we define _GNU_SOURCE. char *line = NULL; size_t len = 0; ssize_t read; // Read both files the same way. for (int j = 0 ; j < 2 ; j++) { // Discard header. read = getline(&line, &len, f[j]); i = 0; while ((read = getline(&line, &len, f[j])) != -1) { pch = strtok(line, "\t"); pch = strtok(NULL, "\t"); while (pch != NULL) { obs[j][i++] = atoi(pch); pch = strtok(NULL, "\t"); } g_assert(i % 1413 == 0); } g_assert(i == 1413*1413); } fclose(f[0]); fclose(f[1]); free(line); tadbit_output *seg = malloc(sizeof(tadbit_output)); tadbit(obs, 1413, 2, 16, 1, 200, 0, 1, seg); int nTADs1 = seg->nbreaks_opt; tadbit(obs, 1413, 2, 16, 1, 200, 0, 0, seg); int nTADs2 = seg->nbreaks_opt; fprintf(stderr, "%d, %d\n", nTADs1, nTADs2); destroy_tadbit_output(seg); free(obs[0]); free(obs[1]); }
void test_tadbit (void) { // -- INPUT -- // int obs1[400] = { 99, 50, 35, 28, 25, 22, 20, 18, 17, 16, 7, 7, 6, 6, 6, 5, 5, 5, 4, 4, 50, 99, 50, 35, 28, 25, 22, 20, 18, 17, 8, 7, 7, 6, 6, 6, 5, 5, 5, 4, 35, 50, 99, 50, 35, 28, 25, 22, 20, 18, 9, 8, 7, 7, 6, 6, 6, 5, 5, 5, 28, 35, 50, 99, 50, 35, 28, 25, 22, 20, 10, 9, 8, 7, 7, 6, 6, 6, 5, 5, 25, 28, 35, 50, 99, 50, 35, 28, 25, 22, 11, 10, 9, 8, 7, 7, 6, 6, 6, 5, 22, 25, 28, 35, 50, 99, 50, 35, 28, 25, 13, 11, 10, 9, 8, 7, 7, 6, 6, 6, 20, 22, 25, 28, 35, 50, 99, 50, 35, 28, 16, 13, 11, 10, 9, 8, 7, 7, 6, 6, 18, 20, 22, 25, 28, 35, 50, 99, 50, 35, 20, 16, 13, 11, 10, 9, 8, 7, 7, 6, 17, 18, 20, 22, 25, 28, 35, 50, 99, 50, 28, 20, 16, 13, 11, 10, 9, 8, 7, 7, 16, 17, 18, 20, 22, 25, 28, 35, 50, 99, 50, 28, 20, 16, 13, 11, 10, 9, 8, 7, 7, 8, 9, 10, 11, 13, 16, 20, 28, 50, 99, 50, 35, 28, 25, 22, 20, 18, 17, 16, 7, 7, 8, 9, 10, 11, 13, 16, 20, 28, 50, 99, 50, 35, 28, 25, 22, 20, 18, 17, 6, 7, 7, 8, 9, 10, 11, 13, 16, 20, 35, 50, 99, 50, 35, 28, 25, 22, 20, 18, 6, 6, 7, 7, 8, 9, 10, 11, 13, 16, 28, 35, 50, 99, 50, 35, 28, 25, 22, 20, 6, 6, 6, 7, 7, 8, 9, 10, 11, 13, 25, 28, 35, 50, 99, 50, 35, 28, 25, 22, 5, 6, 6, 6, 7, 7, 8, 9, 10, 11, 22, 25, 28, 35, 50, 99, 50, 35, 28, 25, 5, 5, 6, 6, 6, 7, 7, 8, 9, 10, 20, 22, 25, 28, 35, 50, 99, 50, 35, 28, 5, 5, 5, 6, 6, 6, 7, 7, 8, 9, 18, 20, 22, 25, 28, 35, 50, 99, 50, 35, 4, 5, 5, 5, 6, 6, 6, 7, 7, 8, 17, 18, 20, 22, 25, 28, 35, 50, 99, 50, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 16, 17, 18, 20, 22, 25, 28, 35, 50, 99, }; int **obs = malloc(2 * sizeof(int *)); obs[0] = obs1; obs[1] = obs1; // -- OUTPUT -- // tadbit_output *seg = malloc(sizeof(tadbit_output)); double expected_weights[400] = { 16, 17.6, 18.5, 19.1, 19.6, 19.9, 20.1, 20.3, 20.5, 20.8, 20.8, 20.5, 20.3, 20.1, 19.9, 19.6, 19.1, 18.5, 17.6, 16, 17.6, 19.3, 20.3, 21, 21.5, 21.9, 22.1, 22.3, 22.5, 22.9, 22.9, 22.5, 22.3, 22.1, 21.9, 21.5, 21, 20.3, 19.3, 17.6, 18.5, 20.3, 21.5, 22.2, 22.7, 23.1, 23.3, 23.5, 23.7, 24.1, 24.1, 23.7, 23.5, 23.3, 23.1, 22.7, 22.2, 21.5, 20.3, 18.5, 19.1, 21, 22.2, 22.9, 23.5, 23.9, 24.1, 24.3, 24.5, 25, 25, 24.5, 24.3, 24.1, 23.9, 23.5, 22.9, 22.2, 21, 19.1, 19.6, 21.5, 22.7, 23.5, 24, 24.4, 24.7, 24.9, 25.1, 25.6, 25.6, 25.1, 24.9, 24.7, 24.4, 24, 23.5, 22.7, 21.5, 19.6, 19.9, 21.9, 23.1, 23.9, 24.4, 24.8, 25.1, 25.3, 25.5, 26, 26, 25.5, 25.3, 25.1, 24.8, 24.4, 23.9, 23.1, 21.9, 19.9, 20.1, 22.1, 23.3, 24.1, 24.7, 25.1, 25.4, 25.6, 25.8, 26.3, 26.3, 25.8, 25.6, 25.4, 25.1, 24.7, 24.1, 23.3, 22.1, 20.1, 20.3, 22.3, 23.5, 24.3, 24.9, 25.3, 25.6, 25.8, 26, 26.5, 26.5, 26, 25.8, 25.6, 25.3, 24.9, 24.3, 23.5, 22.3, 20.3, 20.5, 22.5, 23.7, 24.5, 25.1, 25.5, 25.8, 26, 26.2, 26.7, 26.7, 26.2, 26, 25.8, 25.5, 25.1, 24.5, 23.7, 22.5, 20.5, 20.8, 22.9, 24.1, 25, 25.6, 26, 26.3, 26.5, 26.7, 27.2, 27.2, 26.7, 26.5, 26.3, 26, 25.6, 25, 24.1, 22.9, 20.8, 20.8, 22.9, 24.1, 25, 25.6, 26, 26.3, 26.5, 26.7, 27.2, 27.2, 26.7, 26.5, 26.3, 26, 25.6, 25, 24.1, 22.9, 20.8, 20.5, 22.5, 23.7, 24.5, 25.1, 25.5, 25.8, 26, 26.2, 26.7, 26.7, 26.2, 26, 25.8, 25.5, 25.1, 24.5, 23.7, 22.5, 20.5, 20.3, 22.3, 23.5, 24.3, 24.9, 25.3, 25.6, 25.8, 26, 26.5, 26.5, 26, 25.8, 25.6, 25.3, 24.9, 24.3, 23.5, 22.3, 20.3, 20.1, 22.1, 23.3, 24.1, 24.7, 25.1, 25.4, 25.6, 25.8, 26.3, 26.3, 25.8, 25.6, 25.4, 25.1, 24.7, 24.1, 23.3, 22.1, 20.1, 19.9, 21.9, 23.1, 23.9, 24.4, 24.8, 25.1, 25.3, 25.5, 26, 26, 25.5, 25.3, 25.1, 24.8, 24.4, 23.9, 23.1, 21.9, 19.9, 19.6, 21.5, 22.7, 23.5, 24, 24.4, 24.7, 24.9, 25.1, 25.6, 25.6, 25.1, 24.9, 24.7, 24.4, 24, 23.5, 22.7, 21.5, 19.6, 19.1, 21, 22.2, 22.9, 23.5, 23.9, 24.1, 24.3, 24.5, 25, 25, 24.5, 24.3, 24.1, 23.9, 23.5, 22.9, 22.2, 21, 19.1, 18.5, 20.3, 21.5, 22.2, 22.7, 23.1, 23.3, 23.5, 23.7, 24.1, 24.1, 23.7, 23.5, 23.3, 23.1, 22.7, 22.2, 21.5, 20.3, 18.5, 17.6, 19.3, 20.3, 21, 21.5, 21.9, 22.1, 22.3, 22.5, 22.9, 22.9, 22.5, 22.3, 22.1, 21.9, 21.5, 21, 20.3, 19.3, 17.6, 16, 17.6, 18.5, 19.1, 19.6, 19.9, 20.1, 20.3, 20.5, 20.8, 20.8, 20.5, 20.3, 20.1, 19.9, 19.6, 19.1, 18.5, 17.6, 16, }; tadbit(obs, 20, 2, 1, 0, 20, 1, 1, seg); g_assert_cmpint(seg->maxbreaks, ==, 4); g_assert_cmpint(seg->nbreaks_opt, ==, 1); g_assert_cmpint(seg->nbreaks_opt, ==, 1); for (int i = 0 ; i < 20 ; i++) { g_assert_cmpint(seg->bkpts[i+1*20], == , i == 9); } for (int j = 0 ; j < 20 ; j++) { for (int i = j ; i < 20 ; i++) { g_assert_cmpfloat( abs(seg->weights[0][i+j*20] / 9272 - expected_weights[i+j*20]), <, .1 ); } } free(obs); destroy_tadbit_output(seg); //seg->weights = resized_weights; //seg->passages = resized_passages; //seg->llikmat = resized_llikmat; //seg->mllik = mllik; //seg->bkpts = resized_bkpts; }
SEXP tadbit_R_call( SEXP list, SEXP n_threads, SEXP verbose, SEXP max_tad_size, SEXP do_not_use_heuristic ){ /* * This is a tadbit wrapper for R. The matrices have to be passed * in a list (in R). Checks that the input consists of numeric * square matrices, with identical dimensions. The list is * is converted to pointer of pointers to doubles and passed * to 'tadbit'. * Assume that NAs can be passed from R and are ignored in the * computation. */ R_len_t i, m = length(list); int first = 1, N, *dim_int; SEXP dim; PROTECT(dim = allocVector(INTSXP, 2)); // Convert 'obs_list' to pointer of pointer to double. int **obs = (int **) malloc(m * sizeof(int **)); for (i = 0 ; i < m ; i++) { // This fails if list element is not numeric. obs[i] = INTEGER(coerceVector(VECTOR_ELT(list, i), INTSXP)); // Check that input is a matrix. if (!isMatrix(VECTOR_ELT(list, i))) { error("input must be square matrix"); } // Check the dimension. dim = getAttrib(VECTOR_ELT(list, i), R_DimSymbol); dim_int = INTEGER(dim); if (dim_int[0] != dim_int[1]) { error("input must be square matrix"); } if (first) { N = dim_int[0]; first = 0; } else { if (N != dim_int[0]) { error("all matrices must have same dimensions"); } } } UNPROTECT(1); tadbit_output *seg = (tadbit_output *) malloc(sizeof(tadbit_output)); // Call 'tadbit'. tadbit(obs, N, m, INTEGER(n_threads)[0], INTEGER(verbose)[0], INTEGER(max_tad_size)[0], INTEGER(do_not_use_heuristic)[0], seg); int maxbreaks = seg->maxbreaks; // Copy output to R-readable variables. SEXP nbreaks_SEXP; SEXP passages_SEXP; SEXP llikmat_SEXP; SEXP mllik_SEXP; SEXP bkpts_SEXP; PROTECT(nbreaks_SEXP = allocVector(INTSXP, 1)); PROTECT(passages_SEXP = allocVector(INTSXP, N)); PROTECT(llikmat_SEXP = allocVector(REALSXP, N*N)); PROTECT(mllik_SEXP = allocVector(REALSXP, maxbreaks)); PROTECT(bkpts_SEXP = allocVector(INTSXP, N*(maxbreaks-1))); int *nbreaks_opt = INTEGER(nbreaks_SEXP); int *passages = INTEGER(passages_SEXP); double *llikmat = REAL(llikmat_SEXP); double *mllik = REAL(mllik_SEXP); int *bkpts = INTEGER(bkpts_SEXP); nbreaks_opt[0] = seg->nbreaks_opt; for (i = 0 ; i < N ; i++) passages[i] = seg->passages[i]; for (i = 0 ; i < N*N ; i++) llikmat[i] = seg->llikmat[i]; for (i = 0 ; i < maxbreaks ; i++) mllik[i] = seg->mllik[i]; // Remove first column associated with 0 breaks. Itcontains only // 0s and shifts the index in R (vectors start at position 1). for (i = N ; i < N*(maxbreaks-1) ; i++) bkpts[i-N] = seg->bkpts[i]; // Set 'dim' attributes. SEXP dim_llikmat; PROTECT(dim_llikmat = allocVector(INTSXP, 2)); INTEGER(dim_llikmat)[0] = N; INTEGER(dim_llikmat)[1] = N; setAttrib(llikmat_SEXP, R_DimSymbol, dim_llikmat); SEXP dim_breaks; PROTECT(dim_breaks = allocVector(INTSXP, 2)); INTEGER(dim_breaks)[0] = N; INTEGER(dim_breaks)[1] = maxbreaks-1; setAttrib(bkpts_SEXP, R_DimSymbol, dim_breaks); free(obs); free(seg->passages); free(seg->llikmat); free(seg->mllik); free(seg->bkpts); free(seg); SEXP list_SEXP; PROTECT(list_SEXP = allocVector(VECSXP, 5)); SET_VECTOR_ELT(list_SEXP, 0, nbreaks_SEXP); SET_VECTOR_ELT(list_SEXP, 1, llikmat_SEXP); SET_VECTOR_ELT(list_SEXP, 2, mllik_SEXP); SET_VECTOR_ELT(list_SEXP, 3, bkpts_SEXP); SET_VECTOR_ELT(list_SEXP, 4, passages_SEXP); UNPROTECT(8); return list_SEXP; }