Changeset: 26e61351bc15 for MonetDB
URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=26e61351bc15
Added Files:
        sql/backends/monet5/bam/bam_globals.c
Modified Files:
        sql/backends/monet5/bam/Makefile.ag
        sql/backends/monet5/bam/bam_db_interface.c
        sql/backends/monet5/bam/bam_export.c
        sql/backends/monet5/bam/bam_globals.h
        sql/backends/monet5/bam/bam_wrapper.c
        sql/backends/monet5/bam/bam_wrapper.h
Branch: bamloader
Log Message:

Implemented sam_export function.


diffs (truncated from 318 to 300 lines):

diff --git a/sql/backends/monet5/bam/Makefile.ag 
b/sql/backends/monet5/bam/Makefile.ag
--- a/sql/backends/monet5/bam/Makefile.ag
+++ b/sql/backends/monet5/bam/Makefile.ag
@@ -39,7 +39,7 @@ lib__bam = {
        SOURCES = bam_loader.c bam_loader.h \
                  bam_wrapper.c bam_wrapper.h \
                  bam_db_interface.c bam_db_interface.h \
-                 bam_globals.h \
+                 bam_globals.c bam_globals.h \
                  bam_lib.c bam_lib.h \
                  bam_export.c bam_export.h
        LIBS = ../../../../monetdb5/tools/libmonetdb5 \
diff --git a/sql/backends/monet5/bam/bam_db_interface.c 
b/sql/backends/monet5/bam/bam_db_interface.c
--- a/sql/backends/monet5/bam/bam_db_interface.c
+++ b/sql/backends/monet5/bam/bam_db_interface.c
@@ -297,7 +297,6 @@ next_file_id(mvc * m, sql_table * files,
        sql_column *c;
        BAT *b = NULL;
        BATiter li;
-       sht i;
        BUN p = 0, q = 0;
        lng max_file_id = 0;
 
@@ -310,20 +309,16 @@ next_file_id(mvc * m, sql_table * files,
                      "Could not retrieve the next file id: Error binding 
file_id column of 'files' table");
        }
 
-       /* Loop through all BATs for this column, i.e. the real BAT
-        * and the delta BATs and find the maximum file_id */
-       for (i = 0; i < 3; ++i) {
-               b = store_funcs.bind_col(m->session->tr, c, i);
-               if (b != NULL) {
-                       li = bat_iterator(b);
-                       BATloop(b, p, q) {
-                               lng t = *(lng *) BUNtail(li, p);
+       /* Loop through BAT for this column and find the maximum file_id */
+    b = store_funcs.bind_col(m->session->tr, c, RD_INS);
 
-                               max_file_id = MAX(max_file_id, t);
-                       }
-                       BBPreleaseref(b->batCacheid);
-               }
-       }
+    li = bat_iterator(b);
+    BATloop(b, p, q) {
+        lng t = *(lng *) BUNtail(li, p);
+        max_file_id = MAX(max_file_id, t);
+    }
+    BBPreleaseref(b->batCacheid);
+        
        *next_file_id = max_file_id + 1;
        return MAL_SUCCEED;
 }
diff --git a/sql/backends/monet5/bam/bam_export.c 
b/sql/backends/monet5/bam/bam_export.c
--- a/sql/backends/monet5/bam/bam_export.c
+++ b/sql/backends/monet5/bam/bam_export.c
@@ -26,20 +26,133 @@
  */
  
 #include "monetdb_config.h"
+
+#include "bam_globals.h"
 #include "bam_export.h"
 
+
+typedef struct bam_field {
+    str name;
+    sql_column *c;
+    BAT *b;
+    BATiter iter;
+    BUN cur;
+} bam_field;
+
+
+#define CUR_STR(field, i) ((str) BUNtail(field.iter, (field.cur+i)))
+#define CUR_SHT(field, i) (*(sht *) BUNtail(field.iter, (field.cur+i)))
+#define CUR_INT(field, i) (*(int *) BUNtail(field.iter, (field.cur+i)))
+
 str 
 sam_export(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
 {
     /* arg 1: path to desired output file */
        str output_path = *(str *) getArgReference(stk, pci, pci->retc);
-       
-    (void)output_path;
-    (void)cntxt;
-    (void)mb;
+    
+    mvc *m;
+    sql_schema *s_bam;
+    sql_table *t_export;
+    bam_field fields[11];
+    
+    stream *output = NULL;
+    
+    int cnt, i;
+    str msg = MAL_SUCCEED;
+    
+    memset(fields, 0, 11 * sizeof(bam_field));
+    
+    if ((output = bsopen(output_path)) == NULL) {
+        msg = createException(MAL, "sam_export", "Could not open output file 
'%s' for writing", output_path);
+        goto cleanup;
+    }
+    
+    if ((msg = getSQLContext(cntxt, mb, &m, NULL)) != MAL_SUCCEED) {
+        REUSE_EXCEPTION(msg, MAL, "sam_export", "Could not retrieve SQL 
context: %s", msg);
+        goto cleanup;
+       }
+    
+    if ((s_bam = mvc_bind_schema(m, "bam")) == NULL) {
+        msg = createException(MAL, "sam_export", "Could not find bam schema");
+        goto cleanup;
+    }
+    
+    if ((t_export = mvc_bind_table(m, s_bam, "export")) == NULL) {
+        msg = createException(MAL, "sam_export", "Could not find bam.export 
table");
+        goto cleanup;
+    }
+    
+    fields[0].name = "qname";
+    fields[1].name = "flag";
+    fields[2].name = "rname";
+    fields[3].name = "pos";
+    fields[4].name = "mapq";
+    fields[5].name = "cigar";
+    fields[6].name = "rnext";
+    fields[7].name = "pnext";
+    fields[8].name = "tlen";
+    fields[9].name = "seq";
+    fields[10].name = "qual";
+    
+    for (i=0; i<11; ++i) {
+        int cnt_tmp;
+        if ((fields[i].c = mvc_bind_column(m, t_export, fields[i].name)) == 
NULL) {
+            msg = createException(MAL, "sam_export", "Could not find 
bam.export.%s column", fields[i].name);
+            goto cleanup;
+        }
+        if ((fields[i].b = store_funcs.bind_col(m->session->tr, fields[i].c, 
RDONLY)) == NULL) {
+            msg = createException(MAL, "sam_export", RUNTIME_OBJECT_MISSING);
+            goto cleanup;
+        }
+        cnt_tmp = BATcount(fields[i].b);
+        if (cnt_tmp <= 0) {
+            msg = createException(MAL, "sam_export", "The bam.export table is 
empty");
+            goto cleanup;
+        }
+        if (i > 0 && cnt != cnt_tmp) {
+            msg = createException(MAL, "sam_export", "Misalignment in 
bam.export table: "
+                    "column '%s' has %d values; expected %d",
+                    fields[i].name, cnt_tmp, cnt);
+            goto cleanup;
+        }
+        cnt = cnt_tmp;
+        fields[i].iter = bat_iterator(fields[i].b);
+        fields[i].cur = BUNfirst(fields[i].b);
+    }
+    
+    /* Build header */
+    mnstr_printf(output, "@HD VN:1.0 SO:%s\n",
+            BATtordered(fields[0].b) ? "queryname" : 
+                (BATordered(fields[2].b) && BATordered(fields[3].b) ? 
"coordinate" : 
+                    "unsorted"));
+                    
+    /* TODO: Build SQ tags */
+                    
+    mnstr_printf(output, "@CO SAM file generated by MonetDB\n");
+    
+    
+    for (i=0; i<cnt; ++i) {
+        mnstr_printf(output, "%s\t%d\t%s\t%d\t%d\t%s\t%s\t%d\t%d\t%s\t%s\n", 
+                CUR_STR(fields[0], i), CUR_SHT(fields[1], i), 
CUR_STR(fields[2], i), 
+                CUR_INT(fields[3], i), CUR_SHT(fields[4], i), 
CUR_STR(fields[5], i), 
+                CUR_STR(fields[6], i), CUR_INT(fields[7], i), 
CUR_INT(fields[8], i), 
+                CUR_STR(fields[9], i), CUR_STR(fields[10], i));
+    }
+    
     (void)stk;
     (void)pci;
-    throw(MAL, "sam_export", "Not implemented yet...");
+    
+cleanup:
+    for (i=0; i<11; ++i) {
+        if (fields[i].b != NULL) {
+            BBPreleaseref(fields[i].b->batCacheid);
+        }
+    }
+    if (output) {
+        close_stream(output);
+    }
+    
+    return msg;
 }
 
 
diff --git a/sql/backends/monet5/bam/bam_globals.c 
b/sql/backends/monet5/bam/bam_globals.c
new file mode 100644
--- /dev/null
+++ b/sql/backends/monet5/bam/bam_globals.c
@@ -0,0 +1,51 @@
+/*
+ * The contents of this file are subject to the MonetDB Public License
+ * Version 1.1 (the "License"); you may not use this file except in
+ * compliance with the License. You may obtain a copy of the License at
+ * http://www.monetdb.org/Legal/MonetDBLicense
+ *
+ * Software distributed under the License is distributed on an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
+ * License for the specific language governing rights and limitations
+ * under the License.
+ *
+ * The Original Code is the MonetDB Database System.
+ *
+ * The Initial Developer of the Original Code is CWI.
+ * Portions created by CWI are Copyright (C) 1997-July 2008 CWI.
+ * Copyright August 2008-2014 MonetDB B.V.
+ * All Rights Reserved.
+ */
+
+/*
+ * (author) R Cijvat
+ * This file contains some global definitions, used by multiple bam
+ * library files
+ */
+
+
+
+
+#include "monetdb_config.h"
+
+#include "bam_globals.h"
+
+stream *
+bsopen(str filepath)
+{
+       stream *s;
+       stream *wbs;
+
+       if ((s = open_wastream(filepath)) == NULL) {
+               return NULL;
+       }
+       if (mnstr_errnr(s)) {
+               close_stream(s);
+               return NULL;
+       }
+       if ((wbs = wbstream(s, BSTREAM_CHUNK_SIZE)) == NULL) {
+               close_stream(s);
+               return NULL;
+       }
+       return wbs;
+}
\ No newline at end of file
diff --git a/sql/backends/monet5/bam/bam_globals.h 
b/sql/backends/monet5/bam/bam_globals.h
--- a/sql/backends/monet5/bam/bam_globals.h
+++ b/sql/backends/monet5/bam/bam_globals.h
@@ -28,6 +28,7 @@
 
 #include <string.h>
 #include "gdk.h"
+#include "stream.h"
 
 
 
@@ -109,5 +110,13 @@ hash_fprintf(FILE *f, const char *format
                GDKfree(msg);                                           \
                msg = msg_tmp;                                          \
        } while (0)
+    
+    
+    
+#define BSTREAM_CHUNK_SIZE BUFSIZ
+    
+    
+stream *
+bsopen(str filepath);
 
 #endif
diff --git a/sql/backends/monet5/bam/bam_wrapper.c 
b/sql/backends/monet5/bam/bam_wrapper.c
--- a/sql/backends/monet5/bam/bam_wrapper.c
+++ b/sql/backends/monet5/bam/bam_wrapper.c
@@ -63,26 +63,6 @@ get_ordering(str ord)
        return ORDERING_UNKNOWN;
 }
 
-static stream *
-bsopen(str filepath)
-{
-       stream *s;
-       stream *wbs;
-
-       if ((s = open_wastream(filepath)) == NULL) {
-               return NULL;
-       }
-       if (mnstr_errnr(s)) {
-               close_stream(s);
-               return NULL;
-       }
-       if ((wbs = wbstream(s, BSTREAM_CHUNK_SIZE)) == NULL) {
-               close_stream(s);
-               return NULL;
_______________________________________________
checkin-list mailing list
[email protected]
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to