Skip to content

Commit

Permalink
Make sure -h -1 does not loose the first line
Browse files Browse the repository at this point in the history
  • Loading branch information
pd3 authored and whitwham committed May 9, 2024
1 parent 80bf6b8 commit b204d55
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
6 changes: 4 additions & 2 deletions annot-tsv.c
Original file line number Diff line number Diff line change
Expand Up @@ -373,12 +373,13 @@ void parse_header(dat_t *dat, char *fname, int nth_row, int autodetect)
buf[nbuf++] = strdup(dat->line.s);
}

int keep_line = 0;
if ( nth_row < 0 )
{
if ( nbuf!=-nth_row )
error("Found %d header lines in %s, cannot fetch N=%d from the end\n",nbuf,fname,-nth_row);
cols = cols_split(buf[0], NULL, dat->delim);

keep_line = 1;
}
else
cols = cols_split(dat->line.s, NULL, dat->delim);
Expand All @@ -400,6 +401,7 @@ void parse_header(dat_t *dat, char *fname, int nth_row, int autodetect)
cols = cols_split(str.s, NULL, dat->delim);
free(str.s);
dat->hdr.dummy = 1;
keep_line = 1;
}

dat->hdr.name2idx = khash_str2int_init();
Expand All @@ -421,7 +423,7 @@ void parse_header(dat_t *dat, char *fname, int nth_row, int autodetect)
khash_str2int_set(dat->hdr.name2idx, cols->off[i], i);
}
dat->hdr.cols = cols;
if ( !dat->hdr.dummy ) dat->line.l = 0;
if ( !keep_line ) dat->line.l = 0;

for (i=0; i<nbuf; i++) free(buf[i]);
free(buf);
Expand Down
1 change: 1 addition & 0 deletions test/test.pl
Original file line number Diff line number Diff line change
Expand Up @@ -1416,6 +1416,7 @@ sub test_annot_tsv
run_annot_tsv($opts,src=>'src.10.txt',dst=>'dst.10.txt',out=>'out.10.6.txt',args=>'-m smpl -x');
run_annot_tsv($opts,src=>'src.11.txt',dst=>'dst.11.txt',out=>'out.11.1.txt',args=>'-c 1,2,3:1,2,3 -f 4:5 -h 0:0');
run_annot_tsv($opts,src=>'src.11.txt',dst=>'dst.11.txt',out=>'out.11.1.txt',args=>'-c chr1,beg1,end1:chr,beg,end -f smpl1:src_smpl -h 2:2 -II');
run_annot_tsv($opts,src=>'src.11.txt',dst=>'dst.11.txt',out=>'out.11.1.txt',args=>'-c chr1,beg1,end1:chr,beg,end -f smpl1:src_smpl -h 2:-1 -II');
run_annot_tsv($opts,src=>'src.11.txt',dst=>'dst.11.txt',out=>'out.11.2.txt',args=>'-c chr1,beg1,end1:chr,beg,end -f smpl1:src_smpl -h 2:2');
run_annot_tsv($opts,src=>'src.11.txt',dst=>'dst.11.txt',out=>'out.11.2.txt',args=>'-c chr2,beg2,end2:chr,beg,end -f smpl2:src_smpl -h 3:2');
run_annot_tsv($opts,src=>'src.11.txt',dst=>'dst.11.txt',out=>'out.11.3.txt',args=>'-c chr1,beg1,end1:chr,beg,end -f smpl1:src_smpl -h 2:2 -I');
Expand Down

0 comments on commit b204d55

Please sign in to comment.