Exemple #1
0
int test1 (void) {
struct tagbstring t = bsStatic ("Hello world");
bstring b, c, d;
int ret = 0;

	printf ("TEST: bTail and bHead functions.\n");
	b = bTail (&t, 5);
	c = bHead (&t, 5);
	ret += 0 >= biseqcstr (b, "world");
	ret += 0 >= biseqcstr (c, "Hello");
	bdestroy (b);
	bdestroy (c);

	b = bTail (&t, 0);
	c = bHead (&t, 0);
	ret += 0 >= biseqcstr (b, "");
	ret += 0 >= biseqcstr (c, "");
	bdestroy (b);
	bdestroy (c);

	d = bstrcpy (&t);
	b = bTail (d, 5);
	c = bHead (d, 5);
	ret += 0 >= biseqcstr (b, "world");
	ret += 0 >= biseqcstr (c, "Hello");
	bdestroy (b);
	bdestroy (c);
	bdestroy (d);

	printf ("\t# failures: %d\n", ret);

	return ret;
}
Exemple #2
0
END_TEST

START_TEST(core_001)
{
	struct tagbstring t = bsStatic("Hello world");
	bstring b, c, d;
	int ret = 0;
	b = bTail(&t, 5);
	ck_assert(b != NULL);
	c = bHead(&t, 5);
	ck_assert(c != NULL);
	ret = biseqcstr(b, "world");
	ck_assert_int_eq(ret, 1);
	ret = biseqcstr(c, "Hello");
	ck_assert_int_eq(ret, 1);
	ret = bdestroy(b);
	ck_assert_int_eq(ret, BSTR_OK);
	ret = bdestroy(c);
	ck_assert_int_eq(ret, BSTR_OK);
	b = bTail(&t, 0);
	ck_assert(b != NULL);
	c = bHead(&t, 0);
	ck_assert(c != NULL);
	ret = biseqcstr(b, "");
	ck_assert_int_eq(ret, 1);
	ret = biseqcstr(c, "");
	ck_assert_int_eq(ret, 1);
	ret = bdestroy(b);
	ck_assert_int_eq(ret, BSTR_OK);
	ret = bdestroy(c);
	ck_assert_int_eq(ret, BSTR_OK);
	d = bstrcpy(&t);
	ck_assert(d != NULL);
	b = bTail(d, 5);
	ck_assert(b != NULL);
	c = bHead(d, 5);
	ck_assert(c != NULL);
	ret = biseqcstr(b, "world");
	ck_assert_int_eq(ret, 1);
	ret = biseqcstr(c, "Hello");
	ck_assert_int_eq(ret, 1);
	ret = bdestroy(b);
	ck_assert_int_eq(ret, BSTR_OK);
	ret = bdestroy(c);
	ck_assert_int_eq(ret, BSTR_OK);
	ret = bdestroy(d);
	ck_assert_int_eq(ret, BSTR_OK);
}
Exemple #3
0
Action *Action_create(const char *profile)
{
    Action *action = calloc(sizeof(Action), 1);
    check_mem(action);

    action->profile_dir = bfromcstr(profile);
    action->name = bTail(action->profile_dir, 
            blength(action->profile_dir) -
            bstrrchr(action->profile_dir, '/') - 1);

    action->depends = Profile_read_setting(action->profile_dir, "depends");

    return action;

error:
    return NULL;
}
Exemple #4
0
/**
 * Argument:
 * fn - file names
 * Return:
 * An ordered file names.
 */
alder_seqid_file_t * alder_seqid_file_init(struct bstrList *fn)
{
    // Test if all of the files exist.
    // Determine the file type (text or gzip).
    // Use the first line to determine file formats (FASTA or FASTQ).
    // Read the first sequence name of each file.
    // Pair files using sequence names.
    alder_seqid_file_t *v = malloc(sizeof(alder_seqid_file_t));
    int numberOfFile = fn->qty;
    v->filename = bstrVectorCreate(numberOfFile);
    v->type = malloc(numberOfFile * sizeof(int));
    v->format = malloc(numberOfFile * sizeof(int));
    v->pair = malloc(numberOfFile * sizeof(int));
    
    for (int i = 0; i < fn->qty; i++) {
        int s = alder_file_exist(bdata(fn->entry[i]));
        if (s == 0) {
            alder_seqid_file_free(v);
            return NULL;
        }
    }
    for (int i = 0; i < fn->qty; i++) {
        int s = alder_file_isgzip(bdata(fn->entry[i]));
        if (s == 1) {
            v->type[i] = 2;
        } else {
            v->type[i] = 1;
        }
    }
    for (int i = 0; i < fn->qty; i++) {
        int s = alder_file_whatformat(bdata(fn->entry[i]));
        if (s == 1) {
            v->format[i] = 1;
        } else if (s == 2) {
            v->format[i] = 2;
        } else {
            alder_seqid_file_free(v);
            return NULL;
        }
    }
    
    struct bstrList *bnames = bstrVectorCreate(fn->qty);
    for (int i = 0; i < fn->qty; i++) {
        bstring s = alder_file_firstline(bdata(fn->entry[i]));
        int posFirstSpace = bstrchrp(s, ' ', 0);
        bstring bword = NULL;
        if (posFirstSpace != BSTR_ERR) {
            bword = bHead(s, posFirstSpace);
        } else {
            bword = bstrcpy(s);
        }
        bdestroy(s);
        bstring bname = bTail(bword, bword->slen - 1);
        bdestroy(bword);
        bstrVectorAdd(bnames, bdata(bname));
        bdestroy(bname);
    }
    
    for (int i = 0; i < fn->qty; i++) {
        v->pair[i] = -1;
        for (int j = 0; j < fn->qty; j++) {
            if (i == j) continue;
            if (!bstrcmp(bnames->entry[i], bnames->entry[j])) {
                v->pair[i] = j;
            }
        }
    }
    
    int *pairCount = malloc(numberOfFile * sizeof(int));
    memset(pairCount, 0, numberOfFile*sizeof(int));
    for (int i = 0; i < fn->qty; i++) {
        int j = v->pair[i];
        pairCount[j]++;
    }
    
    for (int i = 0; i < fn->qty; i++) {
        if (pairCount[i] > 1) {
            free(pairCount);
            alder_seqid_file_free(v);
            return NULL;
        }
    }
    free(pairCount);
    
    ///////////////////////////////////////////////////////////////////////////
    // Order pairs.
    int pairI = 0;
    alder_vector_pair_t * pair = alder_vector_pair_t_alloc(fn->qty);
    for (int i = 0; i < fn->qty; i++) {
        if (v->pair[i] >= 0) {
            int alreadPaired = 0;
            for (int j = 0; j < pairI; j++) {
                if (pair->data[j].first == i ||
                    pair->data[j].second== i) {
                    alreadPaired = 1;
                    break;
                }
            }
            if (alreadPaired == 0) {
                if (i < v->pair[i]) {
                    pair->data[pairI].first = i;
                    pair->data[pairI].second= v->pair[i];
                } else {
                    pair->data[pairI].first = v->pair[i];
                    pair->data[pairI].second= i;
                }
                pairI++;
            }
        }
    }
    for (int i = 0; i < fn->qty; i++) {
        if (v->pair[i] < 0) {
            pair->data[pairI].first = i;
            pair->data[pairI].second= -1;
            pairI++;
        }
    }
    alder_seqid_file_t *v2 = malloc(sizeof(alder_seqid_file_t));
    v2->filename = bstrVectorCreate(numberOfFile);
    v2->type = malloc(numberOfFile * sizeof(int));
    v2->format = malloc(numberOfFile * sizeof(int));
    v2->pair = malloc(numberOfFile * sizeof(int));
    int seqidIndex = 0;
    for (int i = 0; i < pairI; i++) {
        int first = pair->data[i].first;
        int second = pair->data[i].second;
        assert(first >= 0);
        bstrVectorAdd(v2->filename, bdata(fn->entry[first]));
        v2->type[seqidIndex] = v->type[first];
        v2->format[seqidIndex] = v->format[first];
        
        if (second >= 0) {
            v2->pair[seqidIndex] = seqidIndex + 1;
        } else {
            v2->pair[seqidIndex] = -1;
        }
        seqidIndex++;
        if (second >= 0) {
            bstrVectorAdd(v2->filename, bdata(fn->entry[second]));
            v2->type[seqidIndex] = v->type[second];
            v2->format[seqidIndex] = v->format[second];
            v2->pair[seqidIndex] = seqidIndex - 1;
            seqidIndex++;
        }
    }
    assert(seqidIndex == numberOfFile);
    
#if 0
    for (int i = 0; i < fn->qty; i++) {
        int j = v->pair[i];
        if (j < 0) {
            fprintf(stdout, "%2d %10s: %10s (%d) - no pair (%d)\n", i,
                    bdata(fn->entry[i]),
                    bdata(bnames->entry[i]), pairCount[i], j);
        } else {
            fprintf(stdout, "%2d %10s: %10s (%d) - pair %10s (%d)\n", i,
                    bdata(fn->entry[i]),
                    bdata(bnames->entry[i]), pairCount[i], bdata(fn->entry[j]), j);
        }
    }
    
    fprintf(stdout, "\n");
    for (int i = 0; i < fn->qty; i++) {
        fprintf(stdout, "%2d %10s: format (%d), type (%d), pair (%d)\n", i,
                bdata(v2->filename->entry[i]),
                v2->format[i], v2->type[i], v2->pair[i]);
    }
#endif
    
    alder_seqid_file_free(v);
    alder_vector_pair_t_free(pair);
    bstrVectorDelete(bnames);
    return v2;
}