Changeset: 6ac8a891a8d4 for MonetDB
URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=6ac8a891a8d4
Modified Files:
        clients/mapiclient/mclient.c
Branch: DVframework_bam
Log Message:

Extended mclient with a renderer for the FASTQ format. This renderer expects 
five string columns: qname, seq1, qual1, seq2, qual2.
To use the fastq renderer, the following command can be entered inside mclient:

\f fastq[/path/to/file1, /path/to/file2]

The renderer will then write output to these files and will ignore the 
toConsole variable on writing the query result.


diffs (185 lines):

diff --git a/clients/mapiclient/mclient.c b/clients/mapiclient/mclient.c
--- a/clients/mapiclient/mclient.c
+++ b/clients/mapiclient/mclient.c
@@ -115,12 +115,15 @@ enum formatters {
        CSVformatter,
        XMLformatter,
        TESTformatter,
-       CLEANformatter
+       CLEANformatter,
+       FASTQformatter
 };
 static enum formatters formatter = NOformatter;
 char *output = NULL;           /* output format as string */
 char *separator = NULL;                /* column separator for CSV/TAB format 
*/
 int csvheader = 0;             /* include header line in CSV format */
+char *fastq_filepath1 = NULL; 
+char *fastq_filepath2 = NULL;
 
 #define DEFWIDTH 80
 
@@ -715,6 +718,50 @@ CSVrenderer(MapiHdl hdl)
 }
 
 static void
+FASTQrenderer(MapiHdl hdl)
+{
+       int fields;
+       char *qname, *seq1, *qual1, *seq2, *qual2;
+       FILE *file1 = fopen(fastq_filepath1, "w"), *file2 = 
fopen(fastq_filepath2, "w");
+       
+       if(file1 == NULL || file2 == NULL) {
+           mnstr_printf(toConsole, "Couldn't open FASTQ file for writing.");
+           return;
+       }
+
+       while (!mnstr_errnr(toConsole) && (fields = fetch_row(hdl)) != 0) {
+               qname = mapi_fetch_field(hdl, 0);
+               seq1  = mapi_fetch_field(hdl, 1);
+               qual1 = mapi_fetch_field(hdl, 2);
+               seq2  = mapi_fetch_field(hdl, 3);
+               qual2 = mapi_fetch_field(hdl, 4);
+               
+               if (qname == NULL || seq1 == NULL || qual1 == NULL || seq2 == 
NULL || qual2 == NULL) {
+                   mnstr_printf(toConsole, "Attributes couldn't be fetched 
from query result");
+                   return;
+               }
+               fputc('@'    , file1);
+               fputs(qname  , file1);
+               fputc('\n'   , file1);
+               fputs(seq1   , file1);
+               fputs("\n+\n", file1);
+               fputs(qual1  , file1);
+               fputc('\n'   , file1);
+               
+               fputc('@'    , file2);
+               fputs(qname  , file2);
+               fputc('\n'   , file2);
+               fputs(seq2   , file2);
+               fputs("\n+\n", file2);
+               fputs(qual2  , file2);
+               fputc('\n'   , file2);
+       }
+       
+       fclose(file1);
+       fclose(file2);
+}
+
+static void
 SQLseparator(int *len, int fields, char sep)
 {
        int i, j;
@@ -1325,6 +1372,40 @@ SQLrenderer(MapiHdl hdl, char singleinst
        free(numeric);
 }
 
+static int
+readFilenameAndOpenFile(char **s, char file[], char endsymbol) {
+    int index = 0;
+    FILE *testfile;
+    while(index < 255 && **s != '\0' && **s != endsymbol) {
+        file[index++] = **s;
+        (*s)++;
+    }
+    file[index] = '\0';
+    if(index == 255) {
+        mnstr_printf(toConsole, "Filename of file %s too long. Only 255 
characters are allowed.\n", file);
+        return 0;
+    }
+    if(**s == '\0') {
+        mnstr_printf(toConsole, "Parse error: End of string character found 
before '%c' character.\n", endsymbol);
+        return 0;
+    }
+    
+    /* to prevent a FASTQ result to be calculated but being unable to write to 
file, test the actual creation */
+    testfile = fopen(file, "w");
+    if(testfile == NULL) {
+        mnstr_printf(toConsole, "File %s couldn't be opened for writing.\n", 
file);
+        return 0;
+    }
+    
+    /* creation worked, now close the file and even remove it, 
+       since we don't want any unneccesary files hanging around */
+    fclose(testfile);
+    remove(file);
+    
+    (*s)++;
+    return 1;
+}
+
 static void
 setFormatter(char *s)
 {
@@ -1362,7 +1443,22 @@ setFormatter(char *s)
                _set_output_format(_TWO_DIGIT_EXPONENT);
 #endif
                formatter = TESTformatter;
-       } else {
+       } else if (strncmp(s, "fastq[", 6) == 0) {
+           char *s2 = s + 6;
+           char file1[256];
+           char file2[256];
+        if(readFilenameAndOpenFile(&s2, file1, ',') && 
readFilenameAndOpenFile(&s2, file2, ']')) {
+            mnstr_printf(toConsole, "Query results will be written to FASTQ 
files '%s' and '%s'.\n", file1, file2);
+            fastq_filepath1 = strdup(file1);
+            fastq_filepath2 = strdup(file2);
+            formatter = FASTQformatter;
+        }
+    }
+    else if (strcmp(s, "fastq") == 0) {
+        mnstr_printf(toConsole, "The fastq renderer needs two filepaths to 
write to."
+            " Expected form: \\f fastq[path/to/file1,path/to/file2]\n:");
+    }
+       else {
                mnstr_printf(toConsole, "unsupported formatter\n");
        }
 }
@@ -1577,6 +1673,9 @@ format_result(Mapi mid, MapiHdl hdl, cha
                                        break;
                                }
                                break;
+                       case FASTQformatter:
+                           FASTQrenderer(hdl);
+                           break;
                        default:
                                RAWrenderer(hdl);
                                break;
@@ -1789,7 +1888,7 @@ showCommands(void)
                mnstr_printf(toConsole, "\\a      - disable auto commit\n");
        }
        mnstr_printf(toConsole, "\\e      - echo the query in sql formatting 
mode\n");
-       mnstr_printf(toConsole, "\\f      - format using a built-in renderer 
{csv,tab,raw,sql,xml}\n");
+       mnstr_printf(toConsole, "\\f      - format using a built-in renderer 
{csv,tab,raw,sql,xml,fastq}\n");
        mnstr_printf(toConsole, "\\w#     - set maximal page width 
(-1=unlimited, 0=terminal width, >0=limit to num)\n");
        mnstr_printf(toConsole, "\\r#     - set maximum rows per page 
(-1=raw)\n");
        mnstr_printf(toConsole, "\\L file - save client/server interaction\n");
@@ -2472,6 +2571,8 @@ doFile(Mapi mid, const char *file, int u
                                                case XMLformatter:
                                                        mnstr_printf(toConsole, 
"xml\n");
                                                        break;
+                                               case FASTQformatter:
+                                                   mnstr_printf(toConsole, 
"fastq\n");
                                                default:
                                                        mnstr_printf(toConsole, 
"none\n");
                                                        break;
@@ -2605,7 +2706,7 @@ usage(const char *prog, int xit)
 #ifdef HAVE_ICONV
        fprintf(stderr, " -E charset  | --encoding=charset specify encoding 
(character set) of the terminal\n");
 #endif
-       fprintf(stderr, " -f kind     | --format=kind      specify output 
format {csv,tab,raw,sql,xml}\n");
+       fprintf(stderr, " -f kind     | --format=kind      specify output 
format {csv,tab,raw,sql,xml,fastq}\n");
        fprintf(stderr, " -H          | --history          load/save cmdline 
history (default off)\n");
        fprintf(stderr, " -i          | --interactive[=tm] interpret \\ 
commands on stdin, use time formatting {ms,s,m}\n");
        fprintf(stderr, " -l language | --language=lang    {sql,mal}\n");
@@ -3079,5 +3180,11 @@ main(int argc, char **argv)
        mapi_destroy(mid);
        mnstr_destroy(stdout_stream);
        mnstr_destroy(stderr_stream);
+       if(fastq_filepath1 != NULL) {
+           free(fastq_filepath1);
+    }
+    if(fastq_filepath2 != NULL) {
+           free(fastq_filepath2);
+    }
        return c;
 }
_______________________________________________
checkin-list mailing list
[email protected]
http://mail.monetdb.org/mailman/listinfo/checkin-list

Reply via email to