#include <getopt.h>
#include <unistd.h>
#include <htslib/sam.h>
static void print_usage(FILE *fp)
{
fprintf(fp, "Usage: add_header infile\n\
Adds new header lines of SQ, RG, PG and CO typs\n");
return;
}
int main(int argc, char *argv[])
{
const char *inname = NULL, sq[] = "@SQ\tSN:TR1\tLN:100\n@SQ\tSN:TR2\tLN:50";
int c = 0, ret = EXIT_FAILURE;
samFile *infile = NULL, *outfile = NULL;
sam_hdr_t *in_samhdr = NULL;
kstring_t data = KS_INITIALIZE;
if (argc != 2) {
print_usage(stderr);
goto end;
}
inname = argv[1];
if (!(infile = sam_open(inname, "r"))) {
printf("Could not open %s\n", inname);
goto end;
}
if (!(outfile = sam_open("-", "w"))) { printf("Could not open stdout\n");
goto end;
}
if (!(in_samhdr = sam_hdr_read(infile))) {
printf("Failed to read header from file!\n");
goto end;
}
for (c = 0; c < argc; ++c) {
kputs(argv[c], &data);
kputc(' ', &data);
}
if (sam_hdr_add_lines(in_samhdr, &sq[0], 0)) { printf("Failed to add SQ lines\n");
goto end;
}
if (sam_hdr_add_line(in_samhdr, "RG", "ID", "RG1", "LB", "Test", "SM", "S1", NULL)) {
printf("Failed to add RG line\n");
goto end;
}
if (sam_hdr_add_pg(in_samhdr, "add_header", "VN", "Test", "CL", data.s, NULL)) { printf("Failed to add PG line\n");
goto end;
}
if (sam_hdr_add_line(in_samhdr, "CO", "Test data", NULL)) { printf("Failed to add PG line\n");
goto end;
}
if (sam_hdr_write(outfile, in_samhdr) < 0) {
printf("Failed to write output\n");
goto end;
}
ret = EXIT_SUCCESS;
end:
if (in_samhdr) {
sam_hdr_destroy(in_samhdr);
}
if (infile) {
sam_close(infile);
}
if (outfile) {
sam_close(outfile);
}
ks_free(&data);
return ret;
}