]> git.donarmstrong.com Git - samtools.git/commitdiff
Improved efficiency of header parsing
authorPetr Danecek <pd3@sanger.ac.uk>
Fri, 26 Feb 2010 15:51:40 +0000 (15:51 +0000)
committerPetr Danecek <pd3@sanger.ac.uk>
Fri, 26 Feb 2010 15:51:40 +0000 (15:51 +0000)
AUTHORS
bam.h
bam_import.c
sam_header.c

diff --git a/AUTHORS b/AUTHORS
index 435431c9663d9fd55f5bc4dce07dad828a01fa49..95afabb14a29ac827a673003cdca7881fb43bb97 100644 (file)
--- a/AUTHORS
+++ b/AUTHORS
@@ -14,3 +14,7 @@ used in `faidx' for indexing RAZF compressed fasta files.
 
 Colin Hercus updated novo2sam.pl to support gapped alignment by
 novoalign.
+
+Petr Danecek contributed the header parsing library sam_header.c and 
+sam2vcf.pl script and added knet support to the RAZF library.
+
diff --git a/bam.h b/bam.h
index 291b30314f75d3d882b1d5dc9960e7b709a50c02..8d6c43142c3a3b837c392b1a36b10081dae0acbc 100644 (file)
--- a/bam.h
+++ b/bam.h
@@ -87,7 +87,7 @@ typedef struct {
        char **target_name;
        uint32_t *target_len;
        void *dict, *hash, *rg2lib;
-       int l_text;
+       size_t l_text, n_text;
        char *text;
 } bam_header_t;
 
index 3a4a5cdf64611d1aa23e98b703976fc85beda117..9d84328bdbdafb5f4e76a19e7edf176121aca5b4 100644 (file)
@@ -168,9 +168,24 @@ static inline void parse_error(int64_t n_lines, const char * __restrict msg)
 }
 static inline void append_text(bam_header_t *header, kstring_t *str)
 {
-       int x = header->l_text, y = header->l_text + str->l + 2; // 2 = 1 byte dret + 1 byte null
+       size_t x = header->l_text, y = header->l_text + str->l + 2; // 2 = 1 byte dret + 1 byte null
        kroundup32(x); kroundup32(y);
-       if (x < y) header->text = (char*)realloc(header->text, y);
+       if (x < y) 
+    {
+        header->n_text = y;
+        header->text = (char*)realloc(header->text, y);
+        if ( !header->text ) 
+        {
+            fprintf(stderr,"realloc failed to alloc %ld bytes\n", y);
+            abort();
+        }
+    }
+    // Sanity check
+    if ( header->l_text+str->l+1 >= header->n_text )
+    {
+        fprintf(stderr,"append_text FIXME: %ld>=%ld, x=%ld,y=%ld\n",  header->l_text+str->l+1,header->n_text,x,y);
+        abort();
+    }
        strncpy(header->text + header->l_text, str->s, str->l+1); // we cannot use strcpy() here.
        header->l_text += str->l + 1;
        header->text[header->l_text] = 0;
index 3879f3fc2a80fbd65ffd1dcb8c6858b280707378..238c5cbda847c07a16548f51d72986075cbba185 100644 (file)
@@ -58,6 +58,14 @@ static void debug(const char *format, ...)
     va_end(ap);
 }
 
+static list_t *list_prepend(list_t *root, void *data)
+{
+    list_t *l = malloc(sizeof(list_t));
+    l->next = root;
+    l->data = data;
+    return l;
+}
+
 static list_t *list_append(list_t *root, void *data)
 {
     list_t *l = root;
@@ -543,7 +551,7 @@ void *sam_header_parse2(const char *headerText)
     {
         hline = sam_header_line_parse(buf);
         if ( hline && sam_header_line_validate(hline) )
-            hlines = list_append(hlines, hline);
+            hlines = list_prepend(hlines, hline);
         else
         {
                        if (hline) sam_header_line_free(hline);