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 #include <vdb/extern.h>
27 #include <insdc/insdc.h>
28 #include <klib/defs.h>
29 #include <klib/rc.h>
30 #include <vdb/table.h>
31 #include <vdb/xform.h>
32 #include <vdb/schema.h>
33 #include <kdb/meta.h>
34 #include <klib/data-buffer.h>
35 #include <bitstr.h>
36 #include <sysalloc.h>
37 
38 #include <stdint.h>
39 #include <stdlib.h>
40 #include <assert.h>
41 #include <string.h>
42 #include <stdio.h>
43 
44 
45 static INSDC_4na_bin  map[]={
46 /*0  0000 - 0000*/ 0,
47 /*1  0001 - 1000*/ 8,
48 /*2  0010 - 0100*/ 4,
49 /*3  0011 - 1100*/ 12,
50 /*4  0100 - 0010*/ 2,
51 /*5  0101 - 1010*/ 10,
52 /*6  0110 - 0110*/ 6,
53 /*7  0111 - 1110*/ 14,
54 /*8  1000 - 0001*/ 1,
55 /*9  1001 - 1001*/ 9,
56 /*10  1010 - 0101*/ 5,
57 /*11  1011 - 1101*/ 13,
58 /*12  1100 - 0011*/ 3,
59 /*13  1101 - 1011*/ 11,
60 /*14  1110 - 0111*/ 7,
61 /*15  1111 - 1111*/ 15
62 };
63 
64 
65 
66 static
raw_restore_read_impl(void * data,const VXformInfo * info,int64_t row_id,VRowResult * rslt,uint32_t argc,const VRowData argv[])67 rc_t CC raw_restore_read_impl ( void *data, const VXformInfo *info, int64_t row_id,
68     VRowResult *rslt, uint32_t argc, const VRowData argv [] )
69 {
70     rc_t rc;
71     int i, j;
72     const INSDC_4na_bin	*read 	= argv[ 0 ].u.data.base;
73     const uint32_t	read_len 	= (uint32_t)argv[ 0 ].u.data.elem_count;
74     const uint8_t	*strand		= argv[ 1 ].u.data.base;
75     const uint32_t	strand_len 	= (uint32_t)argv[ 1 ].u.data.elem_count;
76 
77     INSDC_4na_bin *dst;
78 
79     assert( argv[ 0 ].u.data.elem_bits == 8 );
80     assert( argv[ 1 ].u.data.elem_bits == 8 );
81     assert( strand_len == 1 );
82 
83     read   += argv[ 0 ].u.data.first_elem;
84     strand += argv[ 1 ].u.data.first_elem;
85 
86     /* resize output row for the total number of reads */
87     rslt -> data -> elem_bits = 8;
88     rc = KDataBufferResize ( rslt -> data, read_len );
89     if ( rc != 0 )
90         return rc;
91     rslt -> elem_count = read_len;
92     dst = rslt -> data -> base;
93 
94     /**** MAIN RESTORATION LOOP ***/
95     if ( strand[ 0 ] == false ) /*** nothing to do **/
96     {
97         memmove( dst, read, read_len );
98     }
99     else for ( i = 0, j = read_len - 1; i < (int)read_len; i++, j-- )
100     {
101         dst[ i ] = map[ read[ j ]&15 ];
102     }
103     return 0;
104 }
105 
106 
107 /*
108  * function
109  * INSDC:4na:bin ALIGN:raw_restore_read #1( INSDC:4na:bin ref_read, bool ref_orientation);
110  */
111 VTRANSFACT_IMPL ( ALIGN_raw_restore_read, 1, 0, 0 ) ( const void *Self, const VXfactInfo *info,
112     VFuncDesc *rslt, const VFactoryParams *cp, const VFunctionParams *dp )
113 {
114 
115     rslt->u.rf = raw_restore_read_impl;
116     rslt->variant = vftRow;
117     rslt -> whack = NULL;
118     return 0;
119 }
120