Source code

Download source archive ↓

upstream-dependencies/bcftools/reheader.c

1/*  reheader.c -- reheader subcommand.
2
3    Copyright (C) 2014-2022,2024 Genome Research Ltd.
4
5    Author: Petr Danecek <pd3@sanger.ac.uk>
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software and associated documentation files (the "Software"), to deal
9in the Software without restriction, including without limitation the rights
10to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11copies of the Software, and to permit persons to whom the Software is
12furnished to do so, subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in
15all copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23THE SOFTWARE.  */
24
25#include <stdio.h>
26#include <unistd.h>
27#include <getopt.h>
28#include <ctype.h>
29#include <string.h>
30#include <errno.h>
31#include <sys/stat.h>
32#include <sys/types.h>
33#include <inttypes.h>
34#include <fcntl.h>
35#include <math.h>
36#ifdef _WIN32
37#include <windows.h>
38#endif
39#include <htslib/vcf.h>
40#include <htslib/bgzf.h>
41#include <htslib/tbx.h> // for hts_get_bgzfp()
42#include <htslib/kseq.h>
43#include <htslib/thread_pool.h>
44#include <htslib/faidx.h>
45#include <htslib/khash_str2int.h>
46#include "bcftools.h"
47#include "khash_str2str.h"
48
49typedef struct _args_t
50{
51    char **argv, *fname, *samples_fname, *header_fname, *output_fname;
52    char *fai_fname;
53    htsFile *fp;
54    faidx_t *fai;
55    htsFormat type;
56    htsThreadPool *threads;
57    int argc, n_threads;
58}
59args_t;
60
61static inline int is_escaped(const char *min, const char *str)
62{
63    int n = 0;
64    while ( --str>=min && *str=='\\' ) n++;
65    return n%2;
66}
67static char *copy_and_update_contig_line(faidx_t *fai, char *line, void *chr_seen, kstring_t *dst)
68{
69    kstring_t key = {0,0,0}, val = {0,0,0}, tmp = {0,0,0};
70    char *chr_name = NULL, *p, *q = line + 9;   // skip ##contig=
71    char *end = q;
72    int nopen = 1;
73    hts_pos_t chr_len = 0;
74    while ( *end && *end!='\n' ) end++;
75    while ( *q && *q!='\n' && nopen>0 )
76    {
77        p = ++q;
78        while ( *q && (*q==' ' || *q=='\t') ) { p++; q++; }
79        // ^[A-Za-z_][0-9A-Za-z_.]*$
80        if (p==q && *q && (isalpha(*q) || *q=='_'))
81        {
82            q++;
83            while ( *q && (isalnum(*q) || *q=='_' || *q=='.') ) q++;
84        }
85        int n = q-p;
86        int m = 0;
87        while ( *q && (*q==' ' || *q=='\t') ) { q++; m++; }
88        if ( *q!='=' || !n )
89        {
90            char *x = q;
91            while ( *x && *x!='\n' ) x++;
92            *x = '\0';
93            error("Could not parse the line: %s [%s][%s]\n", line,p,q);
94        }
95        key.l = 0;
96        kputsn(p,q-p-m,&key);
97        p = ++q;
98        while ( *q && (*q==' ' || *q=='\t') ) { p++; q++; }
99        int quoted = *p=='"' ? 1 : 0;
100        if ( quoted ) p++, q++;
101        while ( *q && *q != '\n' )
102        {
103            if ( quoted ) { if ( *q=='"' && !is_escaped(p,q) ) break; }
104            else
105            {
106                if ( *q=='<' ) nopen++;
107                if ( *q=='>' ) nopen--;
108                if ( !nopen ) break;
109                if ( *q==',' && nopen==1 ) break;
110            }
111            q++;
112        }
113        char *r = q;
114        while ( r > p && r[-1] == ' ' ) r--;
115        val.l = 0;
116        kputsn(p,r-p,&val);
117        if ( quoted && *q=='"' ) q++;
118        if ( *q=='>' ) { nopen--; q++; }
119        if ( !strcmp("length",key.s) ) continue;
120        if ( !strcmp("ID",key.s) )
121        {
122            if ( khash_str2int_has_key(chr_seen,val.s) ) continue;
123            chr_len = faidx_seq_len64(fai, val.s);
124            if ( chr_len==-1 )
125            {
126                free(val.s); free(key.s); free(tmp.s);
127                return end;   // the sequence is not in fai, remove
128            }
129            chr_name = strdup(val.s);
130            khash_str2int_inc(chr_seen, chr_name);
131            continue;
132        }
133        kputc(',',&tmp);
134        kputs(key.s,&tmp);
135        kputc('=',&tmp);
136        if ( quoted ) kputc('"',&tmp);
137        kputs(val.s,&tmp);
138        if ( quoted ) kputc('"',&tmp);
139    }
140    if ( !chr_name ) return end;
141    ksprintf(dst,"##contig=<ID=%s,length=%"PRIhts_pos"%s>",chr_name,chr_len,tmp.l ? tmp.s : "");
142    free(key.s); free(val.s); free(tmp.s);
143    return q;
144}
145char *init_tmp_prefix(const char *tmp_prefix)
146{
147    kstring_t prefix = {0,0,0};
148    if ( tmp_prefix )
149    {
150        ksprintf(&prefix,"%sXXXXXX",tmp_prefix);
151        return prefix.s;
152    }
153
154    char *tmpdir = getenv("TMPDIR");
155    if ( tmpdir )
156        kputs(tmpdir, &prefix);
157    else
158    {
159        #ifdef _WIN32
160            char tmp_path[MAX_PATH];
161            int ret = GetTempPath(MAX_PATH, tmp_path);
162            if (!ret || ret > MAX_PATH)
163                error("Could not get the path to the temporary folder\n");
164            kputs(tmp_path, &prefix);
165        #else
166            kputs("/tmp", &prefix);
167        #endif
168    }
169    kputs("/bcftools.XXXXXX", &prefix);
170    return prefix.s;
171}
172static void update_from_fai(faidx_t *fai, kstring_t *hdr_txt)
173{
174    kstring_t hdr_txt_new = {0,0,0};
175
176    // update the existing contig lines and remove lines not present in the fai file
177    void *chr_seen = khash_str2int_init();
178    char *tmp, *beg = hdr_txt->s;
179    while ( beg && *beg )
180    {
181        tmp = strstr(beg, "\n##contig=<");
182        if ( !tmp ) break;
183        kputsn(beg, tmp-beg+1, &hdr_txt_new);
184        size_t l_prev = hdr_txt_new.l;
185        beg = copy_and_update_contig_line(fai,tmp+1,chr_seen, &hdr_txt_new);
186        if ( l_prev==hdr_txt_new.l ) hdr_txt_new.l--;   // nothing was added, remove the newline
187    }
188    if ( !beg || !(tmp=strstr(beg,"\n#CHROM")) ) error("Failed to parse the header, #CHROM not found\n");
189    kputsn(beg, tmp-beg+1, &hdr_txt_new);
190
191    // add any new contig lines
192    int i, n = faidx_nseq(fai);
193    for (i=0; i<n; i++)
194    {
195        if ( khash_str2int_has_key(chr_seen,faidx_iseq(fai,i)) ) continue;
196        ksprintf(&hdr_txt_new,"##contig=<ID=%s,length=%"PRIhts_pos">\n",faidx_iseq(fai,i),faidx_seq_len64(fai,faidx_iseq(fai,i)));
197    }
198    kputs(tmp+1,&hdr_txt_new);
199
200    // Switch the new header content for the old
201    free(hdr_txt->s);
202    memcpy(hdr_txt, &hdr_txt_new, sizeof(*hdr_txt));
203
204    khash_str2int_destroy_free(chr_seen);
205}
206
207static void read_header_file(char *fname, kstring_t *hdr)
208{
209    kstring_t tmp = {0,0,0};
210    hdr->l = 0;
211
212    htsFile *fp = hts_open(fname, "r");
213    if ( !fp ) error("Could not read: %s\n", fname);
214    while ( hts_getline(fp, KS_SEP_LINE, &tmp) > 0 )
215    {
216        kputsn(tmp.s,tmp.l,hdr);
217        kputc('\n',hdr);
218    }
219    if ( hts_close(fp) ) error("Close failed: %s\n", fname);
220    free(tmp.s);
221
222    while ( hdr->l>0 && isspace(hdr->s[hdr->l-1]) ) hdr->l--;  // remove trailing newlines
223    kputc('\n',hdr);
224}
225
226static int set_sample_pairs(char **samples, int nsamples, kstring_t *hdr, int idx)
227{
228    int i, j, n;
229    kstring_t key = {0,0,0};
230    kstring_t val = {0,0,0};
231
232    // Are these samples "old-name new-name" pairs?
233    void *hash = khash_str2str_init();
234    for (i=0; i<nsamples; i++)
235    {
236        char *ptr = samples[i];
237        key.l = val.l = 0;
238        int escaped = 0;
239        while ( *ptr )
240        {
241            if ( *ptr=='\\' && !escaped ) { escaped = 1; ptr++; continue; }
242            if ( isspace(*ptr) && !escaped ) break;
243            kputc(*ptr, &key);
244            escaped = 0;
245            ptr++;
246        }
247        if ( !*ptr ) break;
248        while ( *ptr && isspace(*ptr) ) ptr++;
249        while ( *ptr )
250        {
251            if ( *ptr=='\\' && !escaped ) { escaped = 1; ptr++; continue; }
252            if ( isspace(*ptr) && !escaped ) break;
253            kputc(*ptr, &val);
254            escaped = 0;
255            ptr++;
256        }
257        khash_str2str_set(hash,strdup(key.s),strdup(val.s));
258    }
259    free(key.s);
260    free(val.s);
261    if ( i!=nsamples )  // not "old-name new-name" pairs
262    {
263        khash_str2str_destroy_free_all(hash);
264        return 0;
265    }
266
267    while ( hdr->l>0 && isspace(hdr->s[hdr->l-1]) ) hdr->l--;  // remove trailing newlines
268    hdr->s[hdr->l] = 0;
269
270    kstring_t tmp = {0,0,0};
271    i = j = n = 0;  // i:traverse the #CHROM line 1 by 1; j:points to the last column
272    while ( hdr->s[idx+i] )
273    {
274        if ( hdr->s[idx+i]=='\t' )
275        {
276            hdr->s[idx+i] = 0;
277
278            if ( ++n>9 )
279            {
280                char *new_name = khash_str2str_get(hash,hdr->s+idx+j);
281                kputs(new_name ? new_name : hdr->s+idx+j, &tmp);
282            }
283            else
284                kputs(hdr->s+idx+j, &tmp);
285
286            kputc('\t',&tmp);
287
288            j = ++i;
289            continue;
290        }
291        i++;
292    }
293    char *new_name = khash_str2str_get(hash,hdr->s+idx+j);
294    kputs(new_name ? new_name : hdr->s+idx+j, &tmp);
295
296    khash_str2str_destroy_free_all(hash);
297
298    hdr->l = idx;
299    kputs(tmp.s, hdr);
300    kputc('\n', hdr);
301    free(tmp.s);
302
303    return 1;
304}
305
306static void set_samples(char **samples, int nsamples, kstring_t *hdr)
307{
308    // Find the beginning of the #CHROM line
309    int i = hdr->l - 2, ncols = 0;
310    while ( i>=0 && hdr->s[i]!='\n' )
311    {
312        if ( hdr->s[i]=='\t' ) ncols++;
313        i--;
314    }
315    if ( i<0 || strncmp(hdr->s+i+1,"#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO\tFORMAT",45) )
316    {
317        if ( i>0 && !strncmp(hdr->s+i+1,"#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO",38) )
318            error("Error: missing FORMAT fields, cowardly refusing to add samples\n");
319
320        error("Could not parse the header: %s\n", hdr->s);
321    }
322
323    // Are the samples "old-sample new-sample" pairs?
324    if ( set_sample_pairs(samples,nsamples,hdr, i+1) ) return;
325
326    // Replace all samples
327    if ( ncols!=nsamples+8 )
328        fprintf(stderr, "Warning: different number of samples: %d vs %d\n", nsamples,ncols-8);
329
330    ncols = 0;
331    while ( ncols!=9 )
332    {
333        i++;
334        if ( hdr->s[i]=='\t' ) ncols++;
335    }
336    hdr->l = i;
337
338    for (i=0; i<nsamples; i++)
339    {
340        kputc('\t', hdr);
341        kputs(samples[i], hdr);
342    }
343    kputc('\n', hdr);
344}
345
346BGZF *hts_get_bgzfp(htsFile *fp);
347static void reheader_vcf_gz(args_t *args)
348{
349    BGZF *fp = hts_get_bgzfp(args->fp);
350    if ( !fp || bgzf_read_block(fp) != 0 || !fp->block_length )
351        error("Failed to read %s: %s\n", args->fname, strerror(errno));
352
353    kstring_t hdr = {0,0,0};
354    char *buffer = (char*) fp->uncompressed_block;
355
356    // Read the header and find the position of the data block
357    if ( buffer[0]!='#' ) error("Could not parse the header, expected '#', found '%c'\n", buffer[0]);
358
359    int skip_until = 1;     // end of the header in the current uncompressed block
360    while (1)
361    {
362        if ( buffer[skip_until]=='\n' )
363        {
364            skip_until++;
365            if ( skip_until>=fp->block_length )
366            {
367                kputsn(buffer,skip_until,&hdr);
368                if ( bgzf_read_block(fp) != 0 ) error("Error reading %s\n", args->fname);
369                if ( !fp->block_length ) break;
370                skip_until = 0;
371            }
372            // The header has finished
373            if ( buffer[skip_until]!='#' )
374            {
375                kputsn(buffer,skip_until,&hdr);
376                break;
377            }
378        }
379        skip_until++;
380        if ( skip_until>=fp->block_length )
381        {
382            kputsn(buffer,fp->block_length,&hdr);
383            if ( bgzf_read_block(fp) != 0 ) error("Error reading %s\n", args->fname);
384            if ( !fp->block_length ) break;
385            skip_until = 0;
386        }
387    }
388
389    int nsamples = 0;
390    char **samples = NULL;
391    if ( args->samples_fname )
392    {
393        samples = hts_readlines(args->samples_fname, &nsamples);
394        if ( !samples || !nsamples ) error("Error reading the --samples file \"%s\"\n", args->samples_fname);
395    }
396    if ( args->header_fname )
397    {
398        free(hdr.s); hdr.s = NULL; hdr.l = hdr.m = 0;
399        read_header_file(args->header_fname, &hdr);
400    }
401
402    if ( args->fai )
403        update_from_fai(args->fai, &hdr);
404
405    if ( samples )
406    {
407        set_samples(samples, nsamples, &hdr);
408        int i;
409        for (i=0; i<nsamples; i++) free(samples[i]);
410        free(samples);
411    }
412
413    // Output the modified header
414    BGZF *bgzf_out = bgzf_open(args->output_fname ? args->output_fname : "-","w");;
415    if ( bgzf_write(bgzf_out, hdr.s, hdr.l) < 0 ) error("Can't write BGZF header (code %d)\n", bgzf_out->errcode);
416    free(hdr.s);
417
418    // Output all remaining data read with the header block
419    if ( fp->block_length - skip_until > 0 )
420    {
421        if ( bgzf_write(bgzf_out, buffer+skip_until, fp->block_length-skip_until)<0 ) error("Error: %d\n",fp->errcode);
422    }
423    if ( bgzf_flush(bgzf_out)<0 ) error("Error: %d\n",bgzf_out->errcode);
424
425    // Stream the rest of the file without as it is, without decompressing
426    ssize_t nread;
427    const size_t page_size = 32768;
428    char *buf = (char*) malloc(page_size);
429    while (1)
430    {
431        nread = bgzf_raw_read(fp, buf, page_size);
432        if ( nread<=0 ) break;
433
434        int count = bgzf_raw_write(bgzf_out, buf, nread);
435        if (count != nread) error("Write failed, wrote %d instead of %d bytes.\n", count,(int)nread);
436    }
437    if (bgzf_close(bgzf_out) < 0) error("Error closing %s: %d\n",args->output_fname ? args->output_fname : "-",bgzf_out->errcode);
438    if (hts_close(args->fp)) error("Error closing %s: %d\n",args->fname,fp->errcode);
439    free(buf);
440}
441static void reheader_vcf(args_t *args)
442{
443    kstring_t hdr = {0,0,0};
444    htsFile *fp = args->fp;
445    while ( hts_getline(fp, KS_SEP_LINE, &fp->line) >=0 )
446    {
447        kputc('\n',&fp->line);  // hts_getline eats the newline character
448        if ( fp->line.s[0]!='#' ) break;
449        kputsn(fp->line.s,fp->line.l,&hdr);
450    }
451
452    int nsamples = 0;
453    char **samples = NULL;
454    if ( args->samples_fname )
455    {
456        samples = hts_readlines(args->samples_fname, &nsamples);
457        if ( !samples || !nsamples ) error("Error reading the --samples file \"%s\"\n", args->samples_fname);
458    }
459    if ( args->header_fname )
460    {
461        free(hdr.s); hdr.s = NULL; hdr.l = hdr.m = 0;
462        read_header_file(args->header_fname, &hdr);
463    }
464
465    if ( args->fai )
466        update_from_fai(args->fai, &hdr);
467
468    if ( samples )
469    {
470        set_samples(samples, nsamples, &hdr);
471        int i;
472        for (i=0; i<nsamples; i++) free(samples[i]);
473        free(samples);
474    }
475
476    int out = args->output_fname ? open(args->output_fname, O_WRONLY|O_CREAT|O_TRUNC, 0666) : STDOUT_FILENO;
477    if ( out==-1 ) error("%s: %s\n", args->output_fname,strerror(errno));
478    if ( write(out, hdr.s, hdr.l)!=hdr.l ) error("Failed to write %"PRIu64" bytes\n", (uint64_t)hdr.l);
479    free(hdr.s);
480    if ( fp->line.l )
481    {
482        if ( write(out, fp->line.s, fp->line.l)!=fp->line.l ) error("Failed to write %"PRIu64" bytes\n", (uint64_t)fp->line.l);
483    }
484    while ( hts_getline(fp, KS_SEP_LINE, &fp->line) >=0 )   // uncompressed file implies small size, we don't worry about speed
485    {
486        kputc('\n',&fp->line);
487        if ( write(out, fp->line.s, fp->line.l)!=fp->line.l ) error("Failed to write %"PRIu64" bytes\n", (uint64_t)fp->line.l);
488    }
489    if ( hts_close(fp)!=0 ) error("[%s] Error: close failed .. %s\n", __func__,args->fname);
490    if ( close(out)!=0 ) error("[%s] Error: close failed .. %s\n", __func__,args->output_fname);
491}
492
493static bcf_hdr_t *strip_header(bcf_hdr_t *src, bcf_hdr_t *dst)
494{
495    bcf_hrec_t *src_hrec, *dst_hrec, *tmp;
496    bcf_hdr_t *out = bcf_hdr_init("r");
497    int i;
498    for (i=0; i<dst->nhrec; i++)
499    {
500        // first insert lines which do not code BCF ids, their order does not matter
501        dst_hrec = dst->hrec[i];
502        if ( dst_hrec->type==BCF_HL_FLT || dst_hrec->type==BCF_HL_INFO || dst_hrec->type==BCF_HL_FMT || dst_hrec->type== BCF_HL_CTG ) continue;
503        bcf_hdr_add_hrec(out, bcf_hrec_dup(dst_hrec));
504    }
505    for (i=0; i<src->nhrec; i++)
506    {
507        // now transfer header lines which define BCF ids
508        src_hrec = src->hrec[i];
509
510        if ( src_hrec->type==BCF_HL_FLT || src_hrec->type==BCF_HL_INFO || src_hrec->type==BCF_HL_FMT || src_hrec->type== BCF_HL_CTG )
511        {
512            int j = bcf_hrec_find_key(src_hrec, "ID");
513            dst_hrec = bcf_hdr_get_hrec(dst, src_hrec->type, "ID", src_hrec->vals[j], NULL);
514            if ( !dst_hrec ) continue;
515
516            tmp = bcf_hrec_dup(dst_hrec);
517
518            j = bcf_hrec_find_key(src_hrec, "IDX");
519            if ( j>=0 )
520            {
521                j = atoi(src_hrec->vals[j]);
522                if (hrec_add_idx(tmp, j) < 0)
523                    error_errno("[%s] Failed to add IDX header", __func__);
524            }
525            bcf_hdr_add_hrec(out, tmp);
526        }
527    }
528    if (bcf_hdr_sync(out) < 0)
529        error_errno("[%s] Failed to update header", __func__);
530    for (i=0; i<dst->nhrec; i++)
531    {
532        // finally add new structured fields
533        dst_hrec = dst->hrec[i];
534        if ( dst_hrec->type==BCF_HL_FLT || dst_hrec->type==BCF_HL_INFO || dst_hrec->type==BCF_HL_FMT || dst_hrec->type== BCF_HL_CTG )
535        {
536            int j = bcf_hrec_find_key(dst_hrec, "ID");
537            tmp = bcf_hdr_get_hrec(out, dst_hrec->type, "ID", dst_hrec->vals[j], NULL);
538            if ( !tmp )
539                bcf_hdr_add_hrec(out, bcf_hrec_dup(dst_hrec));
540        }
541    }
542    for (i=0; i<dst->n[BCF_DT_SAMPLE]; i++) bcf_hdr_add_sample(out, dst->samples[i]);
543    bcf_hdr_destroy(dst);
544    return out;
545}
546
547static void reheader_bcf(args_t *args, int is_compressed)
548{
549    htsFile *fp = args->fp;
550
551    if ( args->n_threads > 0 )
552    {
553        args->threads = (htsThreadPool *) calloc(1, sizeof(htsThreadPool));
554        if ( !args->threads ) error("Could not allocate memory\n");
555        if ( !(args->threads->pool = hts_tpool_init(args->n_threads)) ) error("Could not initialize threading\n");
556        hts_set_thread_pool(fp, args->threads);
557    }
558
559    bcf_hdr_t *hdr = bcf_hdr_read(fp); if ( !hdr ) error("Failed to read the header: %s\n", args->fname);
560    kstring_t htxt = {0,0,0};
561    bcf_hdr_format(hdr, 1, &htxt);
562
563    int i, nsamples = 0;
564    char **samples = NULL;
565    if ( args->samples_fname )
566    {
567        samples = hts_readlines(args->samples_fname, &nsamples);
568        if ( !samples || !nsamples ) error("Error reading the --samples file \"%s\"\n", args->samples_fname);
569    }
570    if ( args->header_fname )
571    {
572        free(htxt.s); htxt.s = NULL; htxt.l = htxt.m = 0;
573        read_header_file(args->header_fname, &htxt);
574    }
575
576    if ( args->fai )
577        update_from_fai(args->fai, &htxt);
578
579    if ( samples )
580    {
581        set_samples(samples, nsamples, &htxt);
582        for (i=0; i<nsamples; i++) free(samples[i]);
583        free(samples);
584    }
585
586    bcf_hdr_t *hdr_out = bcf_hdr_init("r");
587    if ( bcf_hdr_parse(hdr_out, htxt.s) < 0 ) error("An error occurred while parsing the header\n");
588    if ( args->header_fname ) hdr_out = strip_header(hdr, hdr_out);
589
590    // write the header and the body
591    htsFile *fp_out = hts_open(args->output_fname ? args->output_fname : "-",is_compressed ? "wb" : "wbu");
592    if ( !fp_out ) error("%s: %s\n", args->output_fname ? args->output_fname : "-", strerror(errno));
593    if ( args->threads )
594        hts_set_thread_pool(fp_out, args->threads);
595    if ( bcf_hdr_write(fp_out, hdr_out)!=0 ) error("[%s] Error: cannot write the header to %s\n", __func__,args->output_fname ? args->output_fname : "standard output");
596
597    bcf1_t *rec = bcf_init();
598    while ( bcf_read(fp, hdr, rec)==0 )
599    {
600        // sanity checking, this slows things down. Make it optional?
601        bcf_unpack(rec, BCF_UN_ALL);
602        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)) )
603            error("The CHROM is not defined: \"%s\"\n", bcf_hdr_int2id(hdr,BCF_DT_CTG,rec->rid));
604
605        for (i=0; i<rec->d.n_flt; i++)
606        {
607            int id = rec->d.flt[i];
608            if ( id >= hdr_out->n[BCF_DT_ID] ) break;
609            if ( !bcf_hdr_idinfo_exists(hdr_out,BCF_HL_FLT,id) ) break;
610            if ( strcmp(hdr->id[BCF_DT_ID][id].key,hdr_out->id[BCF_DT_ID][id].key) )
611                error("FIXME: Broken FILTER ids: %s vs %s\n", hdr->id[BCF_DT_ID][id].key,hdr_out->id[BCF_DT_ID][id].key);
612        }
613        if ( i!=rec->d.n_flt )
614            error("The FILTER is not defined: \"%s\"\n", bcf_hdr_int2id(hdr,BCF_DT_ID,rec->d.flt[i]));
615
616        for (i=0; i<rec->n_info; i++)
617        {
618            int id = rec->d.info[i].key;
619            if ( id >= hdr_out->n[BCF_DT_ID] ) break;
620            if ( !hdr_out->id[BCF_DT_ID][id].key ) break;
621            if ( !bcf_hdr_idinfo_exists(hdr_out,BCF_HL_INFO,id) ) break;
622            if ( strcmp(hdr->id[BCF_DT_ID][id].key,hdr_out->id[BCF_DT_ID][id].key) )
623                error("FIXME: Broken INFO ids: %s vs %s\n", hdr->id[BCF_DT_ID][id].key,hdr_out->id[BCF_DT_ID][id].key);
624        }
625        if ( i!=rec->n_info )
626            error("The INFO tag is not defined: \"%s\"\n", bcf_hdr_int2id(hdr,BCF_DT_ID,rec->d.info[i].key));
627
628        for (i=0; i<rec->n_fmt; i++)
629        {
630            int id = rec->d.fmt[i].id;
631            if ( id >= hdr_out->n[BCF_DT_ID] ) break;
632            if ( !hdr_out->id[BCF_DT_ID][id].key ) break;
633            if ( !bcf_hdr_idinfo_exists(hdr_out,BCF_HL_FMT,id) ) break;
634            if ( strcmp(hdr->id[BCF_DT_ID][id].key,hdr_out->id[BCF_DT_ID][id].key) )
635                error("FIXME: Broken FORMAT ids: %s vs %s\n", hdr->id[BCF_DT_ID][id].key,hdr_out->id[BCF_DT_ID][id].key);
636        }
637        if ( i!=rec->n_fmt )
638            error("The FORMAT tag is not defined: \"%s\"\n", bcf_hdr_int2id(hdr,BCF_DT_ID,rec->d.fmt[i].id));
639
640        if ( bcf_write(fp_out,hdr_out,rec)!=0 ) error("[%s] Error: cannot write to %s\n", __func__,args->output_fname ? args->output_fname : "standard output");
641    }
642    bcf_destroy(rec);
643
644    free(htxt.s);
645    if ( hts_close(fp_out)!=0 ) error("[%s] Error: failed to close the file %s\n",__func__,args->output_fname ? args->output_fname : "standard output");
646    if ( hts_close(fp)!=0 ) error("[%s] Error: close failed .. %s\n", __func__,args->fname);
647    bcf_hdr_destroy(hdr_out);
648    bcf_hdr_destroy(hdr);
649    if ( args->threads )
650    {
651        hts_tpool_destroy(args->threads->pool);
652        free(args->threads);
653    }
654}
655
656
657static void usage(args_t *args)
658{
659    fprintf(stderr, "\n");
660    fprintf(stderr, "About:   Modify header of VCF/BCF files, change sample names.\n");
661    fprintf(stderr, "Usage:   bcftools reheader [OPTIONS] <in.vcf.gz>\n");
662    fprintf(stderr, "\n");
663    fprintf(stderr, "Options:\n");
664    fprintf(stderr, "    -f, --fai FILE             update sequences and their lengths from the .fai file\n");
665    fprintf(stderr, "    -h, --header FILE          new header\n");
666    fprintf(stderr, "    -o, --output FILE          write output to a file [standard output]\n");
667    fprintf(stderr, "    -s, --samples FILE         new sample names\n");
668    fprintf(stderr, "    -T, --temp-prefix PATH     ignored; was template for temporary file name\n");
669    fprintf(stderr, "        --threads INT          use multithreading with <int> worker threads (BCF only) [0]\n");
670    fprintf(stderr, "\n");
671    fprintf(stderr, "Example:\n");
672    fprintf(stderr, "   # Write out the header to be modified\n");
673    fprintf(stderr, "   bcftools view -h old.bcf > header.txt\n");
674    fprintf(stderr, "\n");
675    fprintf(stderr, "   # Edit the header using your favorite text editor\n");
676    fprintf(stderr, "   vi header.txt\n");
677    fprintf(stderr, "\n");
678    fprintf(stderr, "   # Reheader the file\n");
679    fprintf(stderr, "   bcftools reheader -h header.txt -o new.bcf old.bcf\n");
680    fprintf(stderr, "\n");
681    exit(1);
682}
683
684int main_reheader(int argc, char *argv[])
685{
686    int c;
687    args_t *args  = (args_t*) calloc(1,sizeof(args_t));
688    args->argc    = argc; args->argv = argv;
689
690    static struct option loptions[] =
691    {
692        {"temp-prefix",1,0,'T'},
693        {"fai",1,0,'f'},
694        {"output",1,0,'o'},
695        {"header",1,0,'h'},
696        {"samples",1,0,'s'},
697        {"threads",1,NULL,1},
698        {0,0,0,0}
699    };
700    while ((c = getopt_long(argc, argv, "s:h:o:f:T:",loptions,NULL)) >= 0)
701    {
702        switch (c)
703        {
704            case  1 : args->n_threads = strtol(optarg, 0, 0); break;
705            case 'T': break; // unused - was temp file prefix
706            case 'f': args->fai_fname = optarg; break;
707            case 'o': args->output_fname = optarg; break;
708            case 's': args->samples_fname = optarg; break;
709            case 'h': args->header_fname = optarg; break;
710            case '?': usage(args); break;
711            default: error("Unknown argument: %s\n", optarg);
712        }
713    }
714
715    if ( optind>=argc )
716    {
717        if ( !isatty(fileno((FILE *)stdin)) ) args->fname = "-";  // reading from stdin
718        else usage(args);
719    }
720    else args->fname = argv[optind];
721
722    if ( args->fai_fname ) {
723        args->fai = fai_load3(args->fai_fname,args->fai_fname,NULL,FAI_FASTA);
724        if ( !args->fai ) error("Could not parse %s\n", args->fai_fname);
725    }
726    if ( !args->samples_fname && !args->header_fname && !args->fai) usage(args);
727    if ( !args->fname ) usage(args);
728
729    args->fp = hts_open(args->fname,"r");
730    if ( !args->fp ) error("Failed to read from %s\n", !strcmp("-",args->fname)?"standard input":args->fname);
731    args->type = *hts_get_format(args->fp);
732
733    if ( args->type.format==vcf )
734    {
735        if ( args->type.compression==bgzf )
736            reheader_vcf_gz(args);
737        else if ( args->type.compression==no_compression )
738            reheader_vcf(args);
739        else if ( args->type.compression==gzip )
740            error("Error: cannot reheader gzip-compressed files, first convert with `bcftools view --output-type` to a supported format\n");
741        else
742            error("Error: the compression type of \"%s\" is not recognised/supported\n", args->fname);
743    }
744    else
745        reheader_bcf(args, args->type.compression==bgzf || args->type.compression==gzip);
746
747    if (args->fai)
748        fai_destroy(args->fai);
749    free(args);
750    return 0;
751}