Exemplo n.º 1
0
/*===  FUNCTION  ============================================================*
Name:           test_qes_seqfile_write
Description:    Tests the qes_seqfile_write function from qes_seqfile.c
 *===========================================================================*/
static void
test_qes_seqfile_write (void *ptr)
{
    struct qes_seq *seq = qes_seq_create();
    size_t expt_bytes = 0;
    ssize_t res = 0;
    struct qes_seqfile *sf = NULL;
    char *fname = NULL;
    char *crc = NULL;

    (void) ptr;
    /* Make a seq to write */
    qes_seq_fill_name(seq, "HWI-TEST", 8);
    qes_seq_fill_comment(seq, "testseq 1 2 3", 13);
    qes_seq_fill_seq(seq, "ACTCAATT", 8);
    qes_seq_fill_qual(seq, "IIIIIIII", 8);
    expt_bytes = 1 + 8 + 1 + 13 + 1 +   /* @ + name + ' ' + comment + '\n' */
                 8 + 1 + 2 + 8 + 1;     /* seq + '\n' + "+\n" + qual + '\n' */
    /* Test with a FASTQ seqfile */
    fname = get_writable_file();
    tt_assert(fname != NULL);
    sf = qes_seqfile_create(fname, "wT");
    qes_seqfile_set_format(sf, FASTQ_FMT);
    res = qes_seqfile_write(sf, seq);
    tt_int_op(res, ==, expt_bytes);
    qes_seqfile_destroy(sf); /* Has to happen here to flush it */
    crc = crc32_file(fname);
    tt_str_op(crc, ==, "d4665941");
    clean_writable_file(fname);
    free(crc);
    fname = NULL;
    crc = NULL;
    /* Test with a FASTA seqfile */
    expt_bytes = 1 + 8 + 1 + 13 + 1 +   /* @ + name + ' ' + comment + '\n' */
                 8 + 1;                 /* seq + '\n'*/
    fname = get_writable_file();
    tt_assert(fname != NULL);
    sf = qes_seqfile_create(fname, "wT");
    qes_seqfile_set_format(sf, FASTA_FMT);
    /* do the write */
    res = qes_seqfile_write(sf, seq);
    tt_int_op(res, ==, expt_bytes);
    qes_seqfile_destroy(sf); /* Flush it */
    crc = crc32_file(fname);
    tt_str_op(crc, ==, "0a295c77");
    clean_writable_file(fname);
    fname = NULL;
    /* Check with bad params that it returns -2 */
    res = qes_seqfile_write(NULL, seq);
    tt_int_op(res, ==, -2);
    res = qes_seqfile_write(sf, NULL);
    tt_int_op(res, ==, -2);
end:
    qes_seqfile_destroy(sf);
    qes_seq_destroy(seq);
    if (fname != NULL) free(fname);
    if (crc != NULL) free(crc);
}
Exemplo n.º 2
0
inline int
qes_seq_fill(struct qes_seq *seqobj, const char *name, const char *comment,
             const char *seq, const char *qual)
{
    if (!qes_seq_ok(seqobj) || name == NULL || comment == NULL || seq == NULL \
            || qual == NULL) {
        return 1;
    }
    if (qes_seq_fill_name(seqobj, name, strlen(name)) != 0) return 1;
    if (qes_seq_fill_comment(seqobj, comment, strlen(comment)) != 0) return 1;
    if (qes_seq_fill_seq(seqobj, seq, strlen(seq)) != 0) return 1;
    if (qes_seq_fill_qual(seqobj, qual, strlen(qual)) != 0) return 1;
    return 0;
}
Exemplo n.º 3
0
/*******************************************************************************
*                                    MAIN                                     *
*******************************************************************************/
int
convert_main(int argc, char *argv[])
{
    int c = 0;
    bool fasta = true;
    const char *filename = NULL;

    /* Parse CLI options */
    while ((c = getopt(argc, argv, convert_optstr)) >= 0) {
        switch (c) {
        case 'a':
            fasta = true;
            break;
        case 'q':
            fasta = false;
            break;
        case 'h':
            convert_usage(stdout);
            return EXIT_SUCCESS;
            break;
        case '?':
            convert_usage(stderr);
            return EXIT_FAILURE;
            break;
        }
    }
    if (optind >= argc) {
        convert_usage(stderr);
        return EXIT_FAILURE;
    }
    filename = argv[optind];

    struct qes_seq *seq = qes_seq_create();
    struct qes_seqfile *sf = qes_seqfile_create(filename, "r");
    ssize_t res;
    char *tmpqual = NULL;
    size_t tmpqual_sz = 0;
    while ((res = qes_seqfile_read(sf, seq)) > 0) {
        if (fasta) {
            qes_seq_print(seq, stdout, true, 0);
        } else {
            size_t seqlen = seq->seq.len;
            if (seq->qual.len < seqlen) {
                if (seqlen > tmpqual_sz) {
                    tmpqual_sz = seqlen + 1;
                    tmpqual = realloc(tmpqual, seqlen);
                    if (tmpqual == NULL) {
                        fputs("OUT OF MEM\n", stderr);
                        return EXIT_FAILURE;
                    }
                }
                memset(tmpqual, 'I', seqlen);
                tmpqual[seqlen] = 0;
                qes_seq_fill_qual(seq, tmpqual, seqlen);
            }
            qes_seq_print(seq, stdout, false, 0);
        }
    }
    if (tmpqual) free(tmpqual);
    qes_seqfile_destroy(sf);
    qes_seq_destroy(seq);
    return EXIT_SUCCESS;
}