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 written 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 
27 #include "spot_position.h"
28 #include <sysalloc.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <assert.h>
32 
33 static const char * sidx_names[ N_SIDX ] =
34 {
35     "READ_START"
36 };
37 
38 
make_spot_pos(spot_pos * self,const VDatabase * db)39 rc_t make_spot_pos( spot_pos *self,
40                     const VDatabase * db )
41 {
42     rc_t rc = VDatabaseOpenTableRead( db, &self->table, "SEQUENCE" );
43     if ( rc != 0 )
44     {
45         LogErr( klogInt, rc, "VDatabaseOpenTableRead(SEQUENCE) failed\n" );
46         self->table = NULL;
47         self->cursor = NULL;
48     }
49     else
50     {
51         rc = VTableCreateCursorRead( self->table, &self->cursor );
52         if ( rc != 0 )
53             LogErr( klogInt, rc, "VTableCreateCursorRead(SEQUENCE) failed\n" );
54         else
55         {
56             rc = add_columns( self->cursor, self->columns, sidx_names, N_SIDX );
57             if ( rc == 0 )
58             {
59                 rc = VCursorOpen ( self->cursor );
60                 if ( rc != 0 )
61                 {
62                     LogErr( klogInt, rc, "cannot open cursor on SEQUENCE\n" );
63                 }
64             }
65         }
66     }
67     return rc;
68 }
69 
70 
query_spot_pos(spot_pos * self,const uint32_t seq_read_id,const uint64_t spot_id,uint32_t * pos_offset)71 rc_t query_spot_pos( spot_pos *self,
72                      const uint32_t seq_read_id,
73                      const uint64_t spot_id,
74                      uint32_t *pos_offset )
75 {
76     rc_t rc;
77     if ( self->cursor == NULL )
78     {
79         rc = RC ( rcApp, rcNoTarg, rcAccessing, rcParam, rcNull );
80         LogErr( klogInt, rc, "cannot query spot-position, cursor is NULL\n" );
81     }
82     else
83     {
84         rc = read_cells( self->cursor, spot_id, self->columns, sidx_names, N_SIDX );
85         if ( rc == 0 )
86         {
87             const uint32_t * rd_start   = self->columns[ SIDX_READ_START ].base;
88             uint32_t rd_start_len = self->columns[ SIDX_READ_START ].row_len;
89             if ( seq_read_id > rd_start_len )
90             {
91                 rc = RC ( rcApp, rcNoTarg, rcAccessing, rcParam, rcInvalid );
92                 PLOGERR( klogInt, ( klogInt, rc,
93                      "asking for read_start of read #$(read_nr) but we only have $(n_read) reads at row #$(row_nr)",
94                      "read_nr=%u,n_read=%u,row_nr=%lu", seq_read_id, rd_start_len, spot_id ) );
95             }
96             else
97             {
98                 *pos_offset = rd_start[ seq_read_id - 1 ];
99                 /*
100                 OUTMSG(( "SPOT_ID %lu / SEQ_READ_ID %u ---> %u\n",
101                          spot_id, seq_read_id, *pos_offset ));
102                 */
103             }
104         }
105         else
106         {
107             PLOGERR( klogInt, ( klogInt, rc,
108                  "cannot read sequence row #$(row_nr)", "row_nr=%lu", spot_id ) );
109         }
110     }
111     return rc;
112 }
113 
114 
whack_spot_pos(spot_pos * self)115 void whack_spot_pos( spot_pos *self )
116 {
117     if ( self->cursor != NULL )
118         VCursorRelease( self->cursor );
119     if ( self->table != NULL )
120         VTableRelease( self->table );
121 }
122