#include <getopt.h>
#include <unistd.h>
#include <htslib/sam.h>
static void print_usage(FILE *fp)
{
fprintf(fp, "Usage: read_refname infile minsize\n\
This shows name of references which has length above the given size\n");
return;
}
int main(int argc, char *argv[])
{
const char *inname = NULL, *id = NULL;
int c = 0, ret = EXIT_FAILURE, linecnt = 0, pos = 0;
samFile *infile = NULL;
sam_hdr_t *in_samhdr = NULL;
kstring_t data = KS_INITIALIZE;
int64_t minsize = 0, size = 0;
if (argc != 3 && argc != 2) {
print_usage(stdout);
goto end;
}
inname = argv[1];
if (argc == 3) {
minsize = atoll(argv[2]);
}
if (!(infile = sam_open(inname, "r"))) {
printf("Could not open %s\n", inname);
goto end;
}
if (!(in_samhdr = sam_hdr_read(infile))) {
printf("Failed to read header from file!\n");
goto end;
}
linecnt = sam_hdr_count_lines(in_samhdr, "SQ"); if (linecnt <= 0) {
if (!linecnt) {
printf("No reference line present\n");
}
else {
printf("Failed to get reference line count\n");
}
goto end;
}
for (pos = 1, c = 0; c < linecnt; ++c) {
if ((ret = sam_hdr_find_tag_pos(in_samhdr, "SQ", c, "LN", &data) == -2)) {
printf("Failed to get length\n");
goto end;
}
else if (ret == -1) {
continue;
}
size = atoll(data.s);
if (size < minsize) {
continue;
}
if (!(id = sam_hdr_line_name(in_samhdr, "SQ", c))) { printf("Failed to get id for reference data\n");
goto end;
}
printf("%d,%s,%s\n", pos, id, data.s);
pos++;
}
ret = EXIT_SUCCESS;
end:
if (in_samhdr) {
sam_hdr_destroy(in_samhdr);
}
if (infile) {
sam_close(infile);
}
ks_free(&data);
return ret;
}