6 // then alias bgzf_*() functions
9 static inline int bam_is_big_endian()
12 return !(*((char *)(&one)));
15 glf_header_t *glf_header_init()
17 return (glf_header_t*)calloc(1, sizeof(glf_header_t));
20 glf_header_t *glf_header_read(glfFile fp)
24 if (bam_is_big_endian()) {
25 fprintf(stderr, "[glf_header_read] Big endian is detected. Abort.\n");
28 h = (glf_header_t*)calloc(1, sizeof(glf_header_t));
29 bgzf_read(fp, magic, 4);
30 if (strncmp(magic, "GLF\2", 4)) {
31 fprintf(stderr, "[glf_header_read] invalid magic. Abort.\n");
34 bgzf_read(fp, &h->l_text, 4);
36 h->text = (uint8_t*)calloc(h->l_text + 1, 1);
37 bgzf_read(fp, h->text, h->l_text);
42 void glf_header_write(glfFile fp, const glf_header_t *h)
44 bgzf_write(fp, "GLF\2", 4);
45 bgzf_write(fp, &h->l_text, 4);
46 if (h->l_text) bgzf_write(fp, h->text, h->l_text);
49 void glf_header_destroy(glf_header_t *h)
55 char *glf_ref_read(glfFile fp)
59 if (bgzf_read(fp, &n, 4) != 4) return 0;
61 fprintf(stderr, "[glf_ref_read] invalid reference name length: %d.\n", n);
64 str = (char*)calloc(n + 1, 1); // not necesarily n+1 in fact
65 bgzf_read(fp, str, n);
69 void glf_ref_write(glfFile fp, const char *str)
71 int32_t n = strlen(str);
73 bgzf_write(fp, &n, 4);
74 bgzf_write(fp, str, n);
77 void glf_view_normal(const char *ref_name, glf2_t *g1)
80 printf("%s\t%d\t%c\t%d\t%d\t%d", ref_name, g1->pos + 1, "XACMGRSVTWYHKDBN"[g1->ref_base],
81 g1->depth, g1->max_mapQ, g1->min_lk);
82 for (j = 0; j != 10; ++j) printf("\t%d", g1->lk[j]);
86 static char *glf_read_indel(glfFile fp, char *str, int *max, int16_t indel)
88 int l = indel > 0? indel : -indel;
91 str = (char*)realloc(str, *max);
93 bgzf_read(fp, str, l);
98 void glf_view(glfFile fp)
105 h = glf_header_read(fp);
107 while ((name = glf_ref_read(fp)) != 0) {
108 while (bgzf_read(fp, &g2, sizeof(glf2_t))) {
109 if (g2.type == GLF_TYPE_END) break;
110 else if (g2.type == GLF_TYPE_NORMAL) glf_view_normal(name, &g2);
111 else if (g2.type == GLF_TYPE_INDEL) {
112 int16_t indel1, indel2;
113 printf("%s\t%d\t*\t%d\t%d\t%d\t", name, g2.pos + 1, g2.depth, g2.max_mapQ, g2.min_lk);
114 printf("%d\t%d\t%d\t", g2.lk[0], g2.lk[1], g2.lk[2]);
115 indel1 = *(int16_t*)(g2.lk + 3);
116 indel2 = *(int16_t*)(g2.lk + 5);
117 printf("%d\t%d\t", indel1, indel2);
119 str = glf_read_indel(fp, str, &max, indel1);
120 printf("%c%d%s\t", indel1>0? '+':'-', indel1>0?indel1:-indel1, str);
121 } else printf("*\t");
123 str = glf_read_indel(fp, str, &max, indel2);
124 printf("%c%d%s\n", indel2>0? '+':'-', indel2>0?indel2:-indel2, str);
125 } else printf("*\n");
130 glf_header_destroy(h);
134 int glf_view_main(int argc, char *argv[])
138 fprintf(stderr, "Usage: glfview <in.glf>\n");
141 fp = (strcmp(argv[1], "-") == 0)? bgzf_fdopen(fileno(stdin), "r") : bgzf_open(argv[1], "r");
143 fprintf(stderr, "Fail to open file '%s'\n", argv[1]);
152 int main(int argc, char *argv[])
154 return glf_view_main(argc, argv);