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 "NGS_ReadGroup.h"
28 
29 #include "NGS_ErrBlock.h"
30 #include <ngs/itf/Refcount.h>
31 #include <ngs/itf/ReadGroupItf.h>
32 
33 #include "NGS_String.h"
34 
35 #include <kfc/ctx.h>
36 #include <kfc/rsrc.h>
37 #include <kfc/except.h>
38 #include <kfc/xc.h>
39 
40 #include <klib/text.h>
41 
42 #include <vdb/vdb-priv.h>
43 
44 #include <stddef.h>
45 #include <assert.h>
46 #include <string.h>
47 
48 #include <sysalloc.h>
49 
50 /*--------------------------------------------------------------------------
51  * NGS_ReadGroup_v1
52  */
53 
54 #define Self( obj ) \
55     ( ( NGS_ReadGroup* ) ( obj ) )
56 
ITF_ReadGroup_v1_get_name(const NGS_ReadGroup_v1 * self,NGS_ErrBlock_v1 * err)57 static NGS_String_v1 * ITF_ReadGroup_v1_get_name ( const NGS_ReadGroup_v1 * self, NGS_ErrBlock_v1 * err )
58 {
59     HYBRID_FUNC_ENTRY ( rcSRA, rcRefcount, rcAccessing );
60     ON_FAIL ( NGS_String * ret = NGS_ReadGroupGetName ( Self ( self ), ctx ) )
61     {
62         NGS_ErrBlockThrow ( err, ctx );
63     }
64 
65     CLEAR ();
66     return ( NGS_String_v1 * ) ret;
67 }
68 
ITF_ReadGroup_v1_get_stats(const NGS_ReadGroup_v1 * self,NGS_ErrBlock_v1 * err)69 static struct NGS_Statistics_v1 * ITF_ReadGroup_v1_get_stats ( const NGS_ReadGroup_v1 * self, NGS_ErrBlock_v1 * err )
70 {
71     HYBRID_FUNC_ENTRY ( rcSRA, rcRefcount, rcAccessing );
72     ON_FAIL ( struct NGS_Statistics * ret = NGS_ReadGroupGetStatistics ( Self ( self ), ctx ) )
73     {
74         NGS_ErrBlockThrow ( err, ctx );
75     }
76 
77     CLEAR ();
78     return ( struct NGS_Statistics_v1 * ) ret;
79 }
80 
ITF_ReadGroup_v1_next(NGS_ReadGroup_v1 * self,NGS_ErrBlock_v1 * err)81 static bool ITF_ReadGroup_v1_next ( NGS_ReadGroup_v1 * self, NGS_ErrBlock_v1 * err )
82 {
83     HYBRID_FUNC_ENTRY ( rcSRA, rcRefcount, rcAccessing );
84     ON_FAIL ( bool ret = NGS_ReadGroupIteratorNext ( Self ( self ), ctx ) )
85     {
86         NGS_ErrBlockThrow ( err, ctx );
87     }
88 
89     CLEAR ();
90     return ret;
91 }
92 
93 #undef Self
94 
95 
96 NGS_ReadGroup_v1_vt ITF_ReadGroup_vt =
97 {
98     {
99         "NGS_ReadGroup",
100         "NGS_ReadGroup_v1",
101         0,
102         & ITF_Refcount_vt . dad
103     },
104 
105     ITF_ReadGroup_v1_get_name,
106     ITF_ReadGroup_v1_get_stats,
107     ITF_ReadGroup_v1_next
108 };
109 
110 
111 /*--------------------------------------------------------------------------
112  * NGS_ReadGroup
113  */
114 
115 #define VT( self, msg ) \
116     ( ( ( const NGS_ReadGroup_vt* ) ( self ) -> dad . vt ) -> msg )
117 
118 /* Init
119 */
NGS_ReadGroupInit(ctx_t ctx,NGS_ReadGroup * self,NGS_ReadGroup_vt * vt,const char * clsname,const char * instname)120 void NGS_ReadGroupInit ( ctx_t ctx, NGS_ReadGroup * self, NGS_ReadGroup_vt * vt, const char *clsname, const char *instname )
121 {
122     FUNC_ENTRY ( ctx, rcSRA, rcRow, rcConstructing );
123 
124     TRY ( NGS_RefcountInit ( ctx, & self -> dad, & ITF_ReadGroup_vt . dad, & vt -> dad, clsname, instname ) )
125     {
126         assert ( vt -> get_name != NULL );
127         assert ( vt -> get_reads != NULL );
128         assert ( vt -> get_read != NULL );
129         assert ( vt -> get_statistics != NULL );
130         assert ( vt -> get_next != NULL );
131     }
132 }
133 
134 /* GetName
135  */
NGS_ReadGroupGetName(const NGS_ReadGroup * self,ctx_t ctx)136 struct NGS_String * NGS_ReadGroupGetName ( const NGS_ReadGroup * self, ctx_t ctx )
137 {
138     if ( self == NULL )
139     {
140         FUNC_ENTRY ( ctx, rcSRA, rcDatabase, rcAccessing );
141         INTERNAL_ERROR ( xcSelfNull, "failed to get name" );
142     }
143     else
144     {
145         NGS_String* ret = VT ( self, get_name ) ( self, ctx );
146         if ( ret != NULL && string_cmp ( DEFAULT_READGROUP_NAME,
147                                          strlen ( DEFAULT_READGROUP_NAME ),
148                                          NGS_StringData ( ret, ctx ),
149                                          NGS_StringSize ( ret, ctx ),
150                                          ( uint32_t ) NGS_StringSize ( ret, ctx ) ) == 0 )
151         {
152             NGS_String* tmp = ret;
153             ret = NGS_StringSubstrOffsetSize ( ret, ctx, 0, 0 );
154             NGS_StringRelease ( tmp, ctx );
155         }
156         return ret;
157     }
158 
159     return NULL;
160 }
161 
162 #if READ_GROUP_SUPPORTS_READS
NGS_ReadGroupGetReads(const NGS_ReadGroup * self,ctx_t ctx,bool wants_full,bool wants_partial,bool wants_unaligned)163 struct NGS_Read * NGS_ReadGroupGetReads ( const NGS_ReadGroup * self, ctx_t ctx,
164     bool wants_full, bool wants_partial, bool wants_unaligned )
165 {
166     if ( self == NULL )
167     {
168         FUNC_ENTRY ( ctx, rcSRA, rcDatabase, rcAccessing );
169         INTERNAL_ERROR ( xcSelfNull, "failed to get read iterator" );
170     }
171     else
172     {
173         return VT ( self, get_reads ) ( self, ctx, wants_full, wants_partial, wants_unaligned );
174     }
175 
176     return NULL;
177 }
178 
NGS_ReadGroupGetRead(const NGS_ReadGroup * self,ctx_t ctx,const char * readId)179 struct NGS_Read * NGS_ReadGroupGetRead ( const NGS_ReadGroup * self, ctx_t ctx, const char * readId )
180 {
181     if ( self == NULL )
182     {
183         FUNC_ENTRY ( ctx, rcSRA, rcDatabase, rcAccessing );
184         INTERNAL_ERROR ( xcSelfNull, "failed to get read %ld", readId );
185     }
186     else
187     {
188         return VT ( self, get_read ) ( self, ctx, readId );
189     }
190 
191     return NULL;
192 }
193 #endif
194 
NGS_ReadGroupGetStatistics(const NGS_ReadGroup * self,ctx_t ctx)195 struct NGS_Statistics* NGS_ReadGroupGetStatistics ( const NGS_ReadGroup * self, ctx_t ctx )
196 {
197     if ( self == NULL )
198     {
199         FUNC_ENTRY ( ctx, rcSRA, rcDatabase, rcAccessing );
200         INTERNAL_ERROR ( xcSelfNull, "failed to get statistics" );
201     }
202     else
203     {
204         return VT ( self, get_statistics ) ( self, ctx );
205     }
206 
207     return NULL;
208 }
209 
210 
211 /*--------------------------------------------------------------------------
212  * NGS_ReadGroupIterator
213  */
214 
NGS_ReadGroupIteratorNext(NGS_ReadGroup * self,ctx_t ctx)215 bool NGS_ReadGroupIteratorNext ( NGS_ReadGroup* self, ctx_t ctx )
216 {
217     if ( self == NULL )
218     {
219         FUNC_ENTRY ( ctx, rcSRA, rcDatabase, rcAccessing );
220         INTERNAL_ERROR ( xcSelfNull, "failed to get next read group " );
221     }
222     else
223     {
224         return VT ( self, get_next) ( self, ctx );
225     }
226 
227     return false;
228 }
229 
230