1 /*===========================================================================
2 *
3 *                            PUBLIC DOMAIN NOTICE
4 *               National Center for Biotechnology Information
5 *
6 *  This software/database is a "United States Government Work" under the
7 *  terms of the United States Copyright Act.  It was readten as part of
8 *  the author's official duties as a United States Government employee and
9 *  thus cannot be copyrighted.  This software/database is freely available
10 *  to the public for use. The National Library of Medicine and the U.S.
11 *  Government have not placed any restriction on its use or reproduction.
12 *
13 *  Although all reasonable efforts have been taken to ensure the accuracy
14 *  and reliability of the software and data, the NLM and the U.S.
15 *  Government do not and cannot warrant the performance or results that
16 *  may be obtained by using this software or data. The NLM and the U.S.
17 *  Government disclaim all warranties, express or implied, including
18 *  warranties of performance, merchantability or fitness for any particular
19 *  purpose.
20 *
21 *  Please cite the author in any work or product based on this material.
22 *
23 * ===========================================================================
24 *
25 */
26 #include <align/extern.h>
27 
28 #include <klib/rc.h>
29 #include <klib/text.h>
30 #include <insdc/insdc.h>
31 #include <vdb/manager.h>
32 #include <vdb/table.h>
33 #include <vdb/cursor.h>
34 #include <os-native.h>
35 #include <sysalloc.h>
36 
37 #include "reader-cmn.h"
38 #include "reference-cmn.h"
39 #include "reader-wgs.h"
40 #include "debug.h"
41 
42 #include <stdlib.h>
43 #include <stdio.h>
44 #include <string.h>
45 #include <errno.h>
46 #include <ctype.h>
47 #include <assert.h>
48 
49 static const TableReaderColumn TableReaderWGS_cols[] =
50 {
51     /* order important, see code below! */
52     {0, "(INSDC:dna:text)READ", {NULL}, 0, 0},
53     {0, "(INSDC:4na:bin)READ", {NULL}, 0, ercol_Skip},
54     {0, NULL, {NULL}, 0, 0}
55 };
56 
57 struct TableReaderWGS {
58     TableReader const *base;
59     TableReaderColumn cols[sizeof(TableReaderWGS_cols)/sizeof(TableReaderWGS_cols[0])];
60     TableReaderColumn const *read;
61 };
62 
TableReaderWGS_MakeTable(TableReaderWGS const ** const pself,VDBManager const * vmgr,VTable const * const table,uint32_t const options,size_t const cache)63 rc_t TableReaderWGS_MakeTable(TableReaderWGS const **const pself,
64                               VDBManager const *vmgr,
65                               VTable const *const table,
66                               uint32_t const options,
67                               size_t const cache)
68 {
69     assert(pself != NULL);
70     assert(table != NULL);
71     {
72         TableReaderWGS *const self = calloc(1, sizeof(*self));
73 
74         memmove(self->cols, TableReaderWGS_cols, sizeof(TableReaderWGS_cols));
75         self->read = &self->cols[0];
76 
77         if (options != 0) {
78             self->cols[0].flags |=  ercol_Skip;
79             self->cols[1].flags &= ~ercol_Skip;
80             self->read = &self->cols[1];
81         }
82         {
83             rc_t const rc = TableReader_Make(&self->base, table, self->cols, cache);
84             if (rc == 0) {
85                 *pself = self;
86                 return 0;
87             }
88             free(self);
89 
90             return rc;
91         }
92     }
93 }
94 
TableReaderWGS_Whack(TableReaderWGS const * const self)95 void TableReaderWGS_Whack(TableReaderWGS const *const self)
96 {
97     if (self) {
98         TableReader_Whack(self->base);
99         free((TableReaderWGS *)self);
100     }
101 }
102 
TableReaderWGS_SeqLength(TableReaderWGS const * const self,int64_t row,INSDC_coord_len * const result)103 rc_t TableReaderWGS_SeqLength(TableReaderWGS const *const self, int64_t row, INSDC_coord_len *const result)
104 {
105     assert(self != NULL);
106     assert(result != NULL);
107     {
108         rc_t const rc = TableReader_ReadRow(self->base, row);
109         if (rc == 0)
110             *result = self->read->len;
111 
112         ALIGN_DBGERR(rc);
113         return rc;
114     }
115 }
116 
TableReaderWGS_Circular(TableReaderWGS const * const self,int64_t row,bool * const result)117 rc_t TableReaderWGS_Circular(TableReaderWGS const *const self, int64_t row, bool *const result)
118 {
119     assert(self != NULL);
120     assert(result != NULL);
121 
122     *result = false;
123 
124     return 0;
125 }
126 
TableReaderWGS_MD5(TableReaderWGS const * const self,int64_t row,uint8_t const ** const result)127 rc_t TableReaderWGS_MD5(TableReaderWGS const *const self, int64_t row, uint8_t const **const result)
128 {
129     assert(self != NULL);
130     assert(result != NULL);
131 
132     *result = NULL;
133 
134     return 0;
135 }
136 
TableReaderWGS_Read(TableReaderWGS const * const self,int64_t const row,INSDC_coord_zero const offset,INSDC_coord_len const len,uint8_t * const buffer,INSDC_coord_len * const written)137 rc_t TableReaderWGS_Read(TableReaderWGS const *const self, int64_t const row,
138                          INSDC_coord_zero const offset,
139                          INSDC_coord_len const len,
140                          uint8_t *const buffer,
141                          INSDC_coord_len *const written)
142 {
143     assert(self != NULL);
144     assert(buffer != NULL);
145     assert(written != NULL);
146 
147     *written = 0;
148     if (len == 0)
149         return 0;
150     {
151         rc_t const rc = TableReader_ReadRow(self->base, row);
152         if (rc == 0) {
153             INSDC_coord_len const max = self->read->len;
154 
155             if (offset >= max)
156                 return 0;
157             {
158                 uint8_t const *const src = self->read->base.u8 + offset;
159                 INSDC_coord_len const end = offset + len;
160                 INSDC_coord_len const N = end < max ? (end - offset) : (max - offset);
161 
162                 *written = N;
163                 memmove(buffer, src, N);
164             }
165         }
166         ALIGN_DBGERR(rc);
167         return rc;
168     }
169 }
170