Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speed-up reading of large header files #204

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/bwa.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -623,3 +623,36 @@ char *bwa_insert_header(const char *s, char *hdr)
bwa_escape(hdr + len);
return hdr;
}

char *bwa_insert_header_file(const FILE *fp, char *hdr)
{
// Copy all lines beginning with @ from file to hdr at the same time
//
// This is more efficient than calling bwa_insert_header for every
// single line since bwa_insert_header computes strlen of existing hdr
// and reallocs it every time the function is called resulting in quadratic
// complexity.
char *buf, *p;
int file_size = 0, i;

fseek(fp, 0L, SEEK_END);
file_size = ftell(fp);
rewind(fp);

buf = p = (char *) calloc(1, file_size);
assert(buf != NULL);
while (fgets(p, 0xffff, fp))
{
if (p[0] == '@') {
i = strlen(p);
assert(p[i-1] == '\n');
p += i;
}
}
if (p != buf)
// bwa_insert_header expects a string without a trailing \n
p[-1] = 0;
hdr = bwa_insert_header(buf, hdr);
free(buf);
return hdr;
}
1 change: 1 addition & 0 deletions src/bwa.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ extern "C" {
void bwa_print_sam_hdr(const bntseq_t *bns, const char *hdr_line, FILE *fp);
char *bwa_set_rg(const char *s);
char *bwa_insert_header(const char *s, char *hdr);
char *bwa_insert_header_file(const FILE *fp, char *hdr);
#ifdef __cplusplus
}
#endif
Expand Down
12 changes: 1 addition & 11 deletions src/fastmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -739,17 +739,7 @@ int main_mem(int argc, char *argv[])
FILE *fp;
if ((fp = fopen(optarg, "r")) != 0)
{
char *buf;
buf = (char *) calloc(1, 0x10000);
assert(buf != NULL);
while (fgets(buf, 0xffff, fp))
{
i = strlen(buf);
assert(buf[i-1] == '\n');
buf[i-1] = 0;
hdr_line = bwa_insert_header(buf, hdr_line);
}
free(buf);
bwa_insert_header_file(fp, hdr_line);
fclose(fp);
}
} else hdr_line = bwa_insert_header(optarg, hdr_line);
Expand Down