boolean read_event_definitions (char *filename) { char tmpbuf [128]; initialize_events(); s_elog_hfp = fopen (filename, "rt"); if (s_elog_hfp == NULL) { sprintf (tmpbuf, "Couldn't open %s\n", filename); infobox ("Open Failed", tmpbuf); return(FALSE); } /* Presume "elog.h". Note fallthrough... */ if (read_header_files()) { sort_event_definitions(); create_point_selector(); recompute_ps_vscrollbar(); fclose(s_elog_hfp); view1_display_when_idle(); remove_needed = TRUE; return(TRUE); } fclose(s_elog_hfp); s_hfp = fopen (filename, "rt"); if (s_hfp == NULL) { sprintf (tmpbuf, "Couldn't open %s\n", filename); infobox ("Read Event Definition Failure", tmpbuf); return(FALSE); } read_header_file(); /* Happens if the user feeds us the wrong file, for example */ if (g_neventdefs == 0) { sprintf (tmpbuf, "No event definitions found in %s\n", filename); infobox ("No Event Definitions?", tmpbuf); return(FALSE); } finalize_events(); return(TRUE); }
int do_map_header(char *date) { char temp[100]; /* set color and font */ set_ps_color(&hdr.color); fontsize = (double)hdr.fontsize; fprintf(PS.fp, "(%s) FN %.1f SF\n", hdr.font, fontsize); /* set start of first line */ dy = 1.2 * fontsize; y = 72.0 * (PS.page_height - PS.top_marg) - fontsize - 1.5; if (hdr.fp == NULL) { if (PS.celltitle[0]) { fprintf(PS.fp, "/t (TITLE: %s) def\n", PS.celltitle); fprintf(PS.fp, "t SW pop %.1f XS D2 t exch %.1f MS\n", 72 * PS.page_width, y); y -= dy; } strcpy(temp, G_myname()); G_strip(temp); if (*temp == 0) strcpy(temp, G_location()); fprintf(PS.fp, "/t (LOCATION: %s) def\n", temp); fprintf(PS.fp, "t SW pop %.1f XS D2 t exch %.1f MS\n", 72 * PS.page_width, y); y -= 0.25 * dy; if (PS.min_y > y) PS.min_y = y; return 0; } x = 72.0 * PS.left_marg + 1.5; read_header_file(date); y -= 0.25 * dy; if (PS.min_y > y) PS.min_y = y; return 0; }
static void reheader_vcf(args_t *args) { kstring_t hdr = {0,0,0}; htsFile *fp = hts_open(args->fname, "r"); if ( !fp ) error("Failed to open: %s\n", args->fname); while ( hts_getline(fp, KS_SEP_LINE, &fp->line) >=0 ) { kputc('\n',&fp->line); // hts_getline eats the newline character if ( fp->line.s[0]!='#' ) break; kputsn(fp->line.s,fp->line.l,&hdr); } int nsamples = 0; char **samples = NULL; if ( args->samples_fname ) samples = hts_readlines(args->samples_fname, &nsamples); if ( args->header_fname ) { free(hdr.s); hdr.s = NULL; hdr.l = hdr.m = 0; read_header_file(args->header_fname, &hdr); } if ( samples ) { set_samples(samples, nsamples, &hdr); int i; for (i=0; i<nsamples; i++) free(samples[i]); free(samples); } int out = STDOUT_FILENO; if ( write(out, hdr.s, hdr.l)!=hdr.l ) error("Failed to write %d bytes\n", hdr.l); free(hdr.s); if ( fp->line.l ) { if ( write(out, fp->line.s, fp->line.l)!=fp->line.l ) error("Failed to write %d bytes\n", fp->line.l); } while ( hts_getline(fp, KS_SEP_LINE, &fp->line) >=0 ) // uncompressed file implies small size, we don't worry about speed { kputc('\n',&fp->line); if ( write(out, fp->line.s, fp->line.l)!=fp->line.l ) error("Failed to write %d bytes\n", fp->line.l); } hts_close(fp); }
static boolean read_header_files (void) { char *cp, *name; char tmpbuf [1024]; int basenum; boolean rv=FALSE; while (fgets (tmpbuf, sizeof (tmpbuf), s_elog_hfp)) { cp = tmpbuf; if (strncmp (cp, "#define", 7)) continue; cp += 7; /* skip ws after #define */ while (*cp && isspace ((int)*cp)) cp++; if (*cp == 0) continue; /* skip EV_COMPxxx_START */ while (*cp && !isspace((int)*cp)) cp++; if (*cp == 0) continue; /* skip ws after EV_COMPxxx_START */ while (*cp && isspace ((int)*cp)) cp++; if (*cp == 0) continue; basenum = atol (cp); /* skip #define */ while (*cp && (*cp != '/')) cp++; if (*cp == 0) continue; cp++; if (*cp != '*') continue; cp++; /* skip ws after comment start */ while (*cp && isspace ((int)*cp)) cp++; if (*cp == 0) continue; if (strncmp (cp, "pointdefs:", 10)) continue; cp += 10; /* skip ws after comment start */ while (*cp && isspace ((int)*cp)) cp++; name = cp; while (*cp && !isspace ((int)*cp)) cp++; *cp = 0; s_hfp = fopen (name, "rt"); if (s_hfp == NULL) { g_warning ("Couldn't open header file %s\n", name); continue; } rv = TRUE; s_basenum = basenum; read_header_file(); fclose (s_hfp); } return(rv); }
static void reheader_bcf(args_t *args, int is_compressed) { htsFile *fp = hts_open(args->fname, "r"); if ( !fp ) error("Failed to open: %s\n", args->fname); bcf_hdr_t *hdr = bcf_hdr_read(fp); if ( !hdr ) error("Failed to read the header: %s\n", args->fname); kstring_t htxt = {0,0,0}; int hlen; htxt.s = bcf_hdr_fmt_text(hdr, 1, &hlen); htxt.l = hlen; int i, nsamples = 0; char **samples = NULL; if ( args->samples_fname ) samples = hts_readlines(args->samples_fname, &nsamples); if ( args->header_fname ) { free(htxt.s); htxt.s = NULL; htxt.l = htxt.m = 0; read_header_file(args->header_fname, &htxt); } if ( samples ) { set_samples(samples, nsamples, &htxt); for (i=0; i<nsamples; i++) free(samples[i]); free(samples); } bcf_hdr_t *hdr_out = bcf_hdr_init("r"); bcf_hdr_parse(hdr_out, htxt.s); if ( args->header_fname ) hdr_out = strip_header(hdr, hdr_out); // write the header and the body htsFile *fp_out = hts_open("-",is_compressed ? "wb" : "wbu"); bcf_hdr_write(fp_out, hdr_out); bcf1_t *rec = bcf_init(); while ( bcf_read(fp, hdr, rec)==0 ) { // sanity checking, this slows things down. Make it optional? bcf_unpack(rec, BCF_UN_ALL); if ( rec->rid >= hdr_out->n[BCF_DT_CTG] || strcmp(bcf_hdr_int2id(hdr,BCF_DT_CTG,rec->rid),bcf_hdr_int2id(hdr_out,BCF_DT_CTG,rec->rid)) ) error("The CHROM is not defined: \"%s\"\n", bcf_hdr_int2id(hdr,BCF_DT_CTG,rec->rid)); for (i=0; i<rec->d.n_flt; i++) { int id = rec->d.flt[i]; if ( id >= hdr_out->n[BCF_DT_ID] ) break; if ( !bcf_hdr_idinfo_exists(hdr_out,BCF_HL_FLT,id) ) break; if ( strcmp(hdr->id[BCF_DT_ID][id].key,hdr_out->id[BCF_DT_ID][id].key) ) error("FIXME: Broken FILTER ids: %s vs %s\n", hdr->id[BCF_DT_ID][id].key,hdr_out->id[BCF_DT_ID][id].key); } if ( i!=rec->d.n_flt ) error("The FILTER is not defined: \"%s\"\n", bcf_hdr_int2id(hdr,BCF_DT_ID,rec->d.flt[i])); for (i=0; i<rec->n_info; i++) { int id = rec->d.info[i].key; if ( id >= hdr_out->n[BCF_DT_ID] ) break; if ( !hdr_out->id[BCF_DT_ID][id].key ) break; if ( !bcf_hdr_idinfo_exists(hdr_out,BCF_HL_INFO,id) ) break; if ( strcmp(hdr->id[BCF_DT_ID][id].key,hdr_out->id[BCF_DT_ID][id].key) ) error("FIXME: Broken INFO ids: %s vs %s\n", hdr->id[BCF_DT_ID][id].key,hdr_out->id[BCF_DT_ID][id].key); } if ( i!=rec->n_info ) error("The INFO tag is not defined: \"%s\"\n", bcf_hdr_int2id(hdr,BCF_DT_ID,rec->d.info[i].key)); for (i=0; i<rec->n_fmt; i++) { int id = rec->d.fmt[i].id; if ( id >= hdr_out->n[BCF_DT_ID] ) break; if ( !hdr_out->id[BCF_DT_ID][id].key ) break; if ( !bcf_hdr_idinfo_exists(hdr_out,BCF_HL_FMT,id) ) break; if ( strcmp(hdr->id[BCF_DT_ID][id].key,hdr_out->id[BCF_DT_ID][id].key) ) error("FIXME: Broken FORMAT ids: %s vs %s\n", hdr->id[BCF_DT_ID][id].key,hdr_out->id[BCF_DT_ID][id].key); } if ( i!=rec->n_fmt ) error("The FORMAT tag is not defined: \"%s\"\n", bcf_hdr_int2id(hdr,BCF_DT_ID,rec->d.fmt[i].id)); bcf_write(fp_out,hdr_out,rec); } bcf_destroy(rec); free(htxt.s); hts_close(fp_out); hts_close(fp); bcf_hdr_destroy(hdr_out); bcf_hdr_destroy(hdr); }
static void reheader_vcf_gz(args_t *args) { BGZF *fp = bgzf_open(args->fname,"r"); if ( !fp || bgzf_read_block(fp) != 0 || !fp->block_length ) error("Failed to read %s: %s\n", args->fname, strerror(errno)); kstring_t hdr = {0,0,0}; char *buffer = (char*) fp->uncompressed_block; // Read the header and find the position of the data block if ( buffer[0]!='#' ) error("Could not parse the header, expected '#', found '%c'\n", buffer[0]); int skip_until = 1; // end of the header in the current uncompressed block while (1) { if ( buffer[skip_until]=='\n' ) { skip_until++; if ( skip_until>=fp->block_length ) { kputsn(buffer,skip_until,&hdr); if ( bgzf_read_block(fp) != 0 || !fp->block_length ) error("FIXME: No body in the file: %s\n", args->fname); skip_until = 0; } // The header has finished if ( buffer[skip_until]!='#' ) { kputsn(buffer,skip_until,&hdr); break; } } skip_until++; if ( skip_until>=fp->block_length ) { kputsn(buffer,fp->block_length,&hdr); if (bgzf_read_block(fp) != 0 || !fp->block_length) error("FIXME: No body in the file: %s\n", args->fname); skip_until = 0; } } int nsamples = 0; char **samples = NULL; if ( args->samples_fname ) samples = hts_readlines(args->samples_fname, &nsamples); if ( args->header_fname ) { free(hdr.s); hdr.s = NULL; hdr.l = hdr.m = 0; read_header_file(args->header_fname, &hdr); } if ( samples ) { set_samples(samples, nsamples, &hdr); int i; for (i=0; i<nsamples; i++) free(samples[i]); free(samples); } // Output the modified header BGZF *bgzf_out = bgzf_dopen(fileno(stdout), "w"); bgzf_write(bgzf_out, hdr.s, hdr.l); free(hdr.s); // Output all remainig data read with the header block if ( fp->block_length - skip_until > 0 ) { if ( bgzf_write(bgzf_out, buffer+skip_until, fp->block_length-skip_until)<0 ) error("Error: %d\n",fp->errcode); } if ( bgzf_flush(bgzf_out)<0 ) error("Error: %d\n",bgzf_out->errcode); // Stream the rest of the file without as it is, without decompressing ssize_t nread; int page_size = getpagesize(); char *buf = (char*) valloc(page_size); while (1) { nread = bgzf_raw_read(fp, buf, page_size); if ( nread<=0 ) break; int count = bgzf_raw_write(bgzf_out, buf, nread); if (count != nread) error("Write failed, wrote %d instead of %d bytes.\n", count,(int)nread); } if (bgzf_close(bgzf_out) < 0) error("Error: %d\n",bgzf_out->errcode); if (bgzf_close(fp) < 0) error("Error: %d\n",fp->errcode); free(buf); }
tan_t* tan_read_header_file_ext_only(const char* fn, int ext, tan_t* dest) { return read_header_file(fn, ext, TRUE, dest, call_tan_read_header); }
tan_t* tan_read_header_file_ext(const char* fn, int ext, tan_t* dest) { return read_header_file(fn, ext, FALSE, dest, call_tan_read_header); }
tan_t* tan_read_header_file(const char* fn, tan_t* dest) { return read_header_file(fn, 0, FALSE, dest, call_tan_read_header); }
sip_t* sip_read_header_file_ext_only(const char* fn, int ext, sip_t* dest) { return read_header_file(fn, ext, TRUE, dest, call_sip_read_header); }
sip_t* sip_read_header_file(const char* fn, sip_t* dest) { return read_header_file(fn, 0, FALSE, dest, call_sip_read_header); }