]> git.donarmstrong.com Git - biopieces.git/blob - code_c/Maasha/src/bipartite_decode.c
fixed encoding bug in read_454
[biopieces.git] / code_c / Maasha / src / bipartite_decode.c
1 /* Martin Asser Hansen (mail@maasha.dk) Copyright (C) 2008 - All right reserved */
2
3 #include "common.h"
4 #include "filesys.h"
5 #include "seq.h"
6
7 #define BITS_IN_BYTE      8                                /* number of bits in one byte. */
8 #define BLOCK_SPACE_MAX   64                               /* maximum space between two blocks. */
9 #define BLOCK_MASK        ( ( BLOCK_SPACE_MAX << 1 ) - 1 ) /* mask for printing block space. */
10  
11 /* Function declarations. */
12 void      run_decode( int argc, char *argv[] );
13 void      print_usage();
14 void      motif_print( uint motif, uint count );
15
16
17 /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> MAIN <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< */
18
19
20 int main( int argc, char *argv[] )
21 {
22     if ( argc == 1 ) {
23         print_usage();
24     }
25
26     run_decode( argc, argv );
27
28     return EXIT_SUCCESS;
29 }
30
31
32 /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> FUNCTIONS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< */
33
34
35 void print_usage()
36 {
37     /* Martin A. Hansen, September 2008 */
38
39     /* Print usage and exit. */
40
41     fprintf( stderr,
42         "Usage: bipartite_decode <bipartite file(s)> > result.tab\n"        
43     );
44
45     exit( EXIT_SUCCESS );
46 }
47
48
49 void run_decode( int argc, char *argv[] )
50 {
51     /* Martin A. Hansen, September 2008 */
52
53     /* For each file in argv decode the file of */
54     /* bipartite motifs and output the motifs */
55     /* and their count. */
56
57     FILE *fp    = NULL;
58     int   i     = 0;
59     uint  motif = 0;
60     uint  count = 0;
61
62     for ( i = 1; i < argc; i++ )
63     {
64         fp = read_open( argv[ i ] );
65
66         while ( fscanf( fp, "%u\t%u\n", &motif, &count ) )
67         {
68             assert( ferror( fp ) == 0 );
69
70             motif_print( motif, count );
71
72             if ( feof( fp ) ) {
73                 break;
74             }
75         }
76
77         close_stream( fp );
78     }
79 }
80
81
82 void motif_print( uint motif, uint count )
83 {
84     /* Martin A. Hansen, September 2008 */
85
86     /* Converts a binary encoded bipartite motif */
87     /* into DNA and output the motif, distance and */
88     /* count seperated by tabs: */
89     /* BLOCK1 \t BLOCK2 \t DIST \t COUNT */
90
91     uchar  bin1      = 0;
92     uchar  bin2      = 0;
93     ushort dist      = 0;
94     uint   motif_cpy = motif;
95
96     dist = ( ushort ) motif & BLOCK_MASK;
97
98     motif >>= sizeof( uchar ) * BITS_IN_BYTE;
99
100     bin2 = ( uchar ) motif;
101
102     motif >>= sizeof( uchar ) * BITS_IN_BYTE;
103
104     bin1 = ( uchar ) motif;
105
106     printf( "%u\t%s\t%s\t%d\t%d\n", motif_cpy, bin2dna[ bin1 ], bin2dna[ bin2 ], dist, count );
107 }
108
109
110 /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< */
111
112